How to include a form with Ajax callback using form_load_include

Question: i have a location.inc file that have a form with a 3 level dependent dropdown. when i execute this file by itself the code works 100% fine. but now i need to call that form into another form (register_contratista.inc). The form_load_include i'm using in register_contratista.inc its working...it render the form items into the new form but just when i select an options at the first dropdown it gives me an ajax error. I have no clue why it's happening been stuck for hours with this error.
I believe the problem is that the callback functions from location.inc are not working through the form_load_include.
this is the code from location.inc:
 <?php
//define types of locations.
define("LOCATION_TYPE_COUNTRY", 1);
define("LOCATION_TYPE_STATE", 2);
define("LOCATION_TYPE_CITY", 3);

// start location form creator.
function location_form($form, $form_state) {

  $countries = _location_get_countries(); //get all active countries

  $flat_countries = array(
    '' => '-- Seleccione país --',
  );

  $flat_countries = location_flatten_locations_array($countries, $flat_countries);
  $flat_states = array(
    '' => '-- Seleccione departamento --',
  );

   $country_id = '';

 if(isset($form_state['values']['p_ctry'])) {
    $country_id = $form_state['values']['p_ctry'];
    $states = _location_get_states($country_id);
    $flat_states = location_flatten_locations_array($states, $flat_states);

 }

 $flat_cities = array(
    '' => '-- Seleccione ciudad --',
    );
  $state_id = '';

   if(isset($form_state['values']['p_dept'])) {
    $state_id = $form_state['values']['p_dept'];

    $cities = _location_get_cities($state_id);

    $flat_cities = location_flatten_locations_array($cities, $flat_cities);
  }

    $form['p_ctry'] = array(
    '#type' => 'select',
    '#title' => t('Seleccione país del proyecto'),
    '#options' => $flat_countries,
    '#default_value' => $country_id,
    '#required' => TRUE,
    '#ajax' => array(
        'callback' => 'location_country_change_callback',
        'wrapper' => 'state-wrapper',
        ),
    );

    $form['p_dept'] = array(
    '#type' => 'select',
    '#title' => t('Seleccione el departamento/estado'),
    '#prefix' => '
',
    '#options' => $flat_states,
    '#default_value' => $state_id,
    '#suffix' => '
',
    '#ajax' => array(
        'callback' => 'location_city_change_callback',
        'wrapper' => 'city-wrapper'
        ),
    );

    $form['p_city'] = array(
    '#type' => 'select',
    '#title' => t('Seleccione ciudad'),
    '#prefix' => '
',
    '#options' => $flat_cities,
    '#suffix' => '
',
    '#default_value' => isset($form_state['values']['p_city']) ? $form_state['values']['p_city'] : '',
    );

  return $form;

}

function location_country_change_callback($form, $form_state) {
  return $form['p_dept'];
}

function location_city_change_callback($form, $form_state) {
  return $form['p_city'];
}

function location_flatten_locations_array($array, $start = array()) {

  if(is_array($start)) {
    $result = $start;
  } else {
    $result = array();
  }

  asort($array['by_name']);

  foreach($array['by_name'] as $item) {
    $result[$item['id']] = $item['name'];
  }

  return $result;
}

function _location_get_locations($location_type){

    $location_type = intval($location_type);
    static $locations = array();

    if(!array_key_exists($location_type, $locations)) {

       db_set_active('civilproconstru');

      $query = 'SELECT * FROM `location` WHERE `location_type` = '.$location_type.' AND `is_visible` = 1';

      $result = db_query($query);

      foreach ($result as $row) {
          $name = $row->name;
          $id = $row->location_id;
          $parent = $row->parent_id;

          $record = array(
            'name' => $name,
            'id' => $id,
            'parent' => $parent,
          );

          $locations[$location_type]['by_name'][$name] = $record;
          $locations[$location_type]['by_id'][$id] = $record;
      }

       db_set_active();

    }
    return $locations[$location_type];
}

function _location_get_countries() {
  return _location_get_locations(LOCATION_TYPE_COUNTRY);
}

function _location_get_states($country_id = null) {
  $states = _location_get_locations(LOCATION_TYPE_STATE);
  if(!is_null($country_id)) {
    $closure = function($item) use($country_id) {
      return $item['parent'] == $country_id;
    };
    $states['by_name'] = array_filter($states['by_name'], $closure);
    $states['by_id'] = array_filter($states['by_id'], $closure);
  }
  return $states;
}

function _location_get_cities($state_id = null) {
  $cities = _location_get_locations(LOCATION_TYPE_CITY);
  if(!is_null($state_id)) {
    $closure = function($item) use($state_id) {
      return $item['parent'] == $state_id;
    };
    $cities['by_name'] = array_filter($cities['by_name'], $closure);
    $cities['by_id'] = array_filter($cities['by_id'], $closure);
  }
  return $cities;
}

And this is the code from register_contratista.inc where i'm doing the form_load_include
<?php

function registro_contratista_page($form, &$form_state){

  form_load_include($form_state, 'inc', 'lc_core', 'location');
  $renderable = drupal_get_form('location_form', $form_state);
  $location = render($renderable);

  $form['usuario'] = array(
  '#type' => 'fieldset',
  '#title' => 'Registro de usario',
  '#description' => 'Información necesaria para el registro de usuario en la plataforma.',
  '#tree' => TRUE,
  );

  $form['usuario']['name'] = array(
  '#title' => 'Nombre de usuario: ',
  '#description' => 'Nombre de usuario con el cual se identificará en la plataforma, puede ser diferente a su nombre y debe contener máximo 12 letras.',
  '#type' => 'textfield',
  '#size' => '20',
  '#required' => TRUE,
  );

  $form['usuario']['mail'] = array(
  '#type' => 'textfield',
  '#title' => 'Correo electrónico: ',
  '#description' => 'Correo electrónico con el cual administrará su cuenta en la plataforma.',
  '#required' => TRUE,
  );

  $form['ubicacion'] = array(
  '#type' => 'markup',
  '#markup' => $location,  //HERE I RENDER THE FORM FROM location.inc
);
     $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );

  return $form;
}

function registro_contratista_page_submit($form, &$form_state) {

    $new_user = array(
          'name' => $form_state['values']['usuario']['name'],
          'pass' => user_password(),
          'mail' => $form_state['values']['usuario']['mail'],
          'init' => $form_state['values']['usuario']['mail'],
          'status' => 1,
          'access' => REQUEST_TIME,
          'field_pais' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_pais']))),
            'field_dept' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_dept']))),
            'field_city' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_city']))),
    );
 user_save(null, $new_user);
}

And this is the error i'm getting:

Any idea where this error it's coming from???

UPDATE: NEW THEORY.
I'm not sure if this is the problem or not, i just got this new theory that might be the cause of the problem. As you can see at location.inc i have two functions like this:
function location_country_change_callback($form, $form_state) {
  return $form['p_dept'];
}

That are the ones to update the options value of the second and third select list, this function works properly when executing the location.inc separately but when using it with form_load_include it is not working because the function can't find the field (in this case) "['p_dept']" so when i choose an option at the first dropdown i'm getting error because it doesn't know where to put the new values.
If this is the problem is there any way to specify to put those values at a specific field from a different form? like using $form_id? something like:
$form_id = 'my_new_form_id';
function location_country_change_callback($form, $form_state) {
  return $form['p_dept'] at $form_id;
}

First let me know if you agree or disagree with this theory and if you do, how could i fix it?
Answer: Ive been through a lot of that kind of error. When you are sure there's really nothing wrong with your code, check your values queried from the database. There might be unwanted characters in your data. Sanitize your data.
In my case, I encountered ñ characters, so what I did is clean the string before saving it in a variable.
 foreach ($result as $row) {
     $name = myid_clean_string($row->name);
 }

 //A function that cleans a string by replacing '?' by 'ñ'
 function myid_clean_string($word){
     return str_replace('?','ñ', trim($word));
 }

You might do the same. Just check your data. Abba Yahweh bless you.
Comment: I'm 100% sure that the code from location.inc is working perfect because I tested outside the register_contratista.inc and I'm using it in another module a and it's working perfect too... I've been reading more about it but haven't found anything yet....
Comment: I'm almost sure it got it be something with the form_load_include.
Comment: Is this Drupal 7?
Comment: Yes Alyssa it is Drupal7.
Comment: It isnt the answer, but I think drupal_get_form has only parameter.
Comment: Did u have Jquery Update Module installed? Try disabling it. Sometimes it is the culprit of AJAX Http error 200 then tell me if it works.
Answer: The callback functions might not be visible to the calling form.  You can test it by moving the callbacks back into register_contratista.inc.


Adding a join in a Views field handler class constructor?

Question: In a Views field handler, one can define additional fields to accompany the base field defined in hook_field_data. These field values are based on database columns on the same row, and they can be used in the render function. Somewhat like this:
my_views_field_handler extends views_handler_field {

  function construct() {
    parent::construct();
    $this->additional_fields['property2'] = 'property2';
  }

  function render($values) {
    return $this->get_value($values) . $this->get_value($values, 'property2');
  }

}

But is it also possible to define a join in the constructor? One of my database columns is an id of another entity. I have been using it somewhat like this, but this generates quite a lot of overhead (loading an entire entity for each row).
  function render($values) {
    $id = $this->get_value($values, 'property2');
    $entity = entity_load_single('my_type', $id);

    if ($entity->some_other_field == TRUE) {
      return $this->get_value($values);
    }
    else {
      return 'none';
    }
  }

So is there something like a join I can use in the constuctor, so I can query the other entity table directly instead of having to load the entity?
Answer: Yes, but you need to have already defined that relationship in the view. Per the docs for views_handler_field::add_additional_fields($fields):

@param $fields
  An array of fields. The key is an identifier
  used to later find the field alias used. The value is either a
  string in which case it's assumed to be a field on this handler's
  table; or it's an array in the form of
  @code array('table'
  => $tablename, 'field' => $fieldname) @endcode

Thus, say you had a field handler for a field on the node table with a defined author relationship to the users table. You could do this:
  function construct() {
    parent::construct();
    $this->additional_fields['mail'] = array(
      'table' => 'users',
      'field' => 'mail',
    );
  }

Which would allow you to do this:
function render($values) {
  //Get the mail column from the users table.
  $this->get_value($values, 'mail');
}

For your case, you'll need to define the entity relationship to your base table before you can do this.
Comment: Yes, I do remember reading something like this in the past. I'll try it later today :)
Comment: This did it, bounty unlocks in a few more hours.


How do I "read" a variable on a while loop

Question: How can I read from variable with while read line?
For example:
the_list=$(..code..)

while read line
do
        echo $line

done < $the_list

using the code above gives me error:
./copy.sh: line 25: $the_list: ambiguous redirect

Answer: You can write:
while IFS= read -r line
do
    echo "$line"
done <<< "$the_list"

See §3.6.7 "Here Strings" in the Bash Reference Manual.
(I've also taken the liberty of adding some double-quotes, and adding -r and IFS= to read, to avoid too much mucking around with the contents of your variables.)
Comment: @doubleDown: `IFS=` sets `$IFS` to the empty string (so it doesn't contain any characters at all). In this case, since there's only one field, its only effect is to prevent the removal of leading IFS-characters from the start of the line. (To see what I mean, compare `read foo <<< ' bar' ; echo "$foo"` with `IFS= read foo <<< ' bar' ; echo "$foo"`.)
Comment: Does `IFS=` set the IFS to null character? Why do you need it in this case?
Answer: If you do not use the variable for anything else, you can even do without it:
while read line ; do
    echo $line
done < <( ... code ... )

Answer: You can just use
your_code | while read line;
do
    echo $line
done

if you don't mind the while loop executing in a subshell (any variables you modify won't be visible in the parent after the done).
Comment: They won't, as I said. You're creating and/or modifying variables in a different process. However, variables are not the only form of output: the loop can still print output directly to the shared stdout (as, in fact, this example does), and it can still operate on files which persist after the subshell exits.
Comment: This is an old reply. I am interested in the "...(any variables you modify won't be visible in the parent after the done)." part of the statement. How will they be then _visible_? The following example `echo "${NAMES/,/$'\n'}" |while read -r NAME ;do TMP=${NAME#*_} ;TMP2=${TMP%_*_*_*_*_*} ;OUTPUT+=${TMP2/$'\n'/,} ;done` won't return anything with `echo $OUTPUT` after the loop, for example.
Comment: Thanks (again)! The following somewhat elaborated example works exactly the way I need :-): Looping over `NAMES=x_myd11a1_lst_night_2019_01_01T01_30_00,x_myd21a1_lst_night_2019_01_01T01_30_00` via `while read -r NAME; do TMP=${NAME#*_}  ;TMP2="${TMP%_*_*_*_*_*}" ;if [ -z $OUTPUT ] ;then OUTPUT+=${TMP2:-$TMP2} ;else OUTPUT+=${OUTPUT:+,$TMP2} ;fi ;done <<< ${NAMES//,/$'\n'*}` derives the `OUTPUT` variable  `echo $OUTPUT`: `myd11a1_lst_night,myd21a1_lst_night`.
Answer: Script file should be in Linux mode. Previously it was in dos mode. I changed it by using  dos2unix filename.
e.g.:
dos2unix sshcopy.sh

Now it works for me.
Comment: The answer proposed by [ruakh](http://stackoverflow.com/a/13122491/622391) was accepted by the asker. This means that the problem was _not_ related to the script's file format.


Retirar palavras reservadas de SQL de uma string Java

Question: Estou a fazer uma view que tem um campo aberto (textarea) onde o utilizador pode escrever SQL e depois pode executar o mesmo SQL.
A questão é que o user oracle onde ele vai correr o SQL têm permissões para tudo e não é suposto ele fazer delete's ou truncate ou insert. Só é permitido consultas (select).
Não estou a ver outra maneira senão ter as palavras mapeadas em memória e verificar se a string contém essas palavras reservadas.
Alguma ideia de como resolver de outra forma?
Answer: Usando uma conexão somente leitura
A única forma totalmente segura de fazer isto e a mais simples também, é criar um segundo usuário no Oracle com permissões somente para leitura das tabelas e então executar essas consultas numa conexão diferente das demais consultas do sistema.
O único trabalho que você vai ter é modificar um pouco a estrutura do programa para permitir isso, talvez configurar um segundo data source no seu servidor de aplicação e assim por diante.
Criando uma sublinguagem a partir do SQL do Oracle
Esta alternativa é bem menos segura e bem mais complicada.
Consiste em criar um interpretador de SQL (praticamente um compilador) que faz a análise léxica, sintática e semântica e assim permite apenas a sub linguagem do SQL que você definir. Tudo o que tiver a mais, fora dessa sublinguagem, seria acusado como um erro de sintaxe do código, ainda que seja um SQL válido.
Essa proposta é viável se você tem proficiência para definir a gramática para uma linguagem formal (prestou atenção na aula de compiladores da faculdade?) para implementar ou gerar um parser e se o usuário estaria contente em ter acesso a um conjunto limitado de funcionalidades, afinal tudo o que você não incluir na sua sublinguagem não pode ser usado, mesmo que o Oracle dê acesso.
A boa notícia é que não é difícil reusar algo que outros já fizeram. Uma biblioteca muito conhecida no Java, chamada Antlr, é capaz de gerar um parser com base numa linguagem formal e existem várias gramáticas disponíveis para download, inclusive PL/SQL.
Agora, basta você remover da gramática aquilo que não quer. Você vai precisar entender o que está fazendo, então ainda precisa lembrar um pouco daquelas aulas de compiladores.
Com a gramática pronta, você gera o parser. Ao ser executado, o parser interpreta o código (SQL neste caso) e gera uma AST, que é uma árvore com os tokens encontrados.
No seu caso, você não precisa fazer nada com essa árvore, apenas verificar se a interpretação da sublinguagem SQL ocorreu com sucesso.
Filtro de conteúdo usando uma "lista negra"
Filtrar o SQL usando uma lista de palavras proibidas vai muito além de delete ou insert. Você precisará incluir qualquer comando que possa:

Alterar a estrutura do banco
Alterar configurações do banco ou da sessão
Eliminar dados e tabelas, como truncate
Executar funções ou procedimentos com efeitos destrutivos
A lista continua...

Existem procedimentos no Oracle capazes de executar comandos SQL arbitrários se o usuário possui as permissões para isso.
Talvez seja perfeitamente possível chegar a um nível razoável de segurança, mas o problema  disso tudo é que pode acabar filtrando um SQL válido por acaso, um falso positivo, caso algum nome de campo ou tabela ou string contenha o nome de um comando bloqueado.
Não vou deixar aqui porque eu sei que não conseguiria compilar algo totalmente seguro e não quero incentivar essa prática e indiretamente causar precedentes de segurança por aí.
No entando, eu diria que o melhor caminho é olhar o manual do Oracle, pois lá tem todos os comandos e a sintaxe de cada um bem explicadas. Portanto, basta consultar um a um e adicionar os que você não quer que sejam executados numa lista.
Depois que tiver a lista, basta iterativa sobre ela e verificar se alguma palavra está contida no SQL. Na comparação, n ao se esqueça de ignorar maiúsculas e minúsculas e também de considerar a codificação de caracteres.
Implementando segurança usando medo
Uma alternativa seria avisar o usuário que os comandos digitados serão armazenados e se ele for pego fazendo algo de errado vai perder o emprego ou ser processado.
Claro que isso é uma brincadeira.
O que eu diria é que seria uma boa prática armazenar quem digitou o que, independente da abordagem escolhida. Segurança também inclui investigar ataques maliciosos.
Outro princípio é que, dependendo do uso do sistema, por exemplo se ele for usado internamente por um no úmero limitado de pessoas, não vale a pena investir em algo elaborado.
Comment: @user2989745 quanto a políticas da empresa ou se depende de você ou não, acho que você precisa rever isso. Como uma pessoa técnica, você não pode assumir que as coisas são do jeito que são e precisam continuar assim. Depois se houver algum problema o culpado vai ser você que não fez a segurança direito. Você no mínimo precisa avisar formalmente o chefe, gerente ou responsável de que tal e tal ação seriam necessárias e as consequências disso não ser feito. Aí sim, se eles insistirem na solução pior, você pode lavar as mãos deixando todos a par do risco que estão correndo.
Comment: Obrigado pela resposta, sim foram as duas possibilidades a questão é que a primeira não depende de mim, penso que não será possível. Achas a verificação de string removendo palavras reservadas como insert, delete, etc.. falível ? Muito insegura ?
Comment: @user2989745 Para usar algum tipo de "lista negra de palavras" você vai precisar de muito mais que duas ou três palavras. Precisa evitar que o usuário crie e execute funções ou procedimentos, qualquer alteração na estrutura das tabelas, alteração de variáveis e configurações no ambiente, além de uma série de comandos que podem ser destrutivos como truncate.
Comment: sim, tem que ser uma "lista negra de palavras" algo extensa, é de longe a pior hipótese, Outra questão da opção de criar uma conexão só para leitura é que tenho que inserir numa tabela o sql final. Corro sempre o risco de injection ou outro tipo de ataque.
Comment: @user2989745 quanto a inserir em outra tabela, basta executar o comando com uma conexão e inserir os dados usando outra. Ou ainda, na hipótese de um usuário com permissão read-only, você poderia dar permissão de escrita apenas para a tabela de saída. Veja, eu atualizei a resposta com alguns detalhes a mais, mas não vou arriscar dar uma lista de palavras porque tenho 100% de certeza que estaria errada e poderia ser minha culpa causar incidentes de segurança  por aí
Comment: agradeço as dicas, concordo contigo quanto a solução. Não é possível definir privilégios ao nível do jdbc ? Por statement ?
Comment: @user2989745 Achei na [documentação](https://docs.oracle.com/database/121/JJDBC/apxtips.htm#JJDBC28958) uma parte que diz que o driver suporta [conexões read-only](https://docs.oracle.com/javase/8/docs/api/java/sql/Connection.html#setReadOnly-boolean-), mas isso é apenas uma otimização e não garante que efeitos colaterais não ocorrerão, por exemplo um `drop table`.


Poll Rules integration

Question: How can I integrate core poll module with rules?
I found the advpoll module but it is still in dev version and throws errors everywhere.
I am looking to simply trigger a rule after user votes on a poll.
Are there any alternatives to achieve the same functionality?
Comment: I'm thinking about adding a submit handler through `hook_form_alter()` and then making that a custom rules event. Will post back here the results if they work.
Answer: It turned out to be fairly easy to do through custom module:
MYMODULE.module
<code>/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'poll_view_voting':
      $form['vote']['#submit'][] = 'MYMODULE_poll_view_voting_submit';
      break;
  }
}

/**
 * Submit handler for poll_view_voting form.
 */
function MYMODULE_poll_view_voting_submit($form, &$form_state) {
  // Invoke our custom rule and pass the loaded node object as an argument.
  rules_invoke_event('MYMODULE_poll_vote', node_load($form['#node']->nid));
}
</code>
MYMODULE.rules.inc
<code>/**
 * Implements hook_rules_event_info().
 */
function MYMODULE_rules_event_info() {
  return array(
    'MYMODULE_poll_vote' => array(
      'label' => t('User casts a poll vote'),
      'help' => t('Triggers when users are casting votes on polls.'),
      'group' => t('MYMODULE HUMAN NAME'),
      'variables' => array(
        'poll' => array(
          'type' => 'node',
          'label' => t('Poll node'),
          'description' => t('Voted poll node.'),
        ),
      ),
    ),
  );
}
</code>
Comment: Hey Aram, interesting self answered question (and topic). Nice that you also created that "Poll Rules Integration"-sandbox (which I think you should rename to just "Poll Rules". While reviewing a bit that sandbox, I noticed a crucial typo in line 2 of the readme.txt: I think you should change "action" to "event" there, no? Also, maybe you should expand it with relevant Rules Conditions related to the actual poll vote (the selected option in the poll). Finally, what's stopping you to get it promoted to a full project?

Poll Rules integration


What's the criteria for swapping the order of floor and limit?

Question: You can't always swap limits and floors.
$$
\lim_{n\rightarrow\infty}\left\lfloor\sum_{k=1}^{n}9\cdot 10^{-k}\right\rfloor=0,
$$
but
$$
\left\lfloor\lim_{n\rightarrow\infty}\sum_{k=1}^{n}9\cdot 10^{-k}\right\rfloor=1.
$$
What is the criteria for this?
Comment: You can swap a limit and a function when the function is continuous at the argument's limit.
Comment: The criterion to swap limit with any function is called continuity.
Comment: o swick thank u i should have rmemembered that from the class i took
Comment: However, we do have $$\left\lfloor \lim_{n\to\infty} a_n\right\rfloor = \lim_{n\to \infty}\lfloor a_n\rfloor$$ if $\lim_{n\to\infty} a_n\notin\mathbb Z$ or $a_n$ is eventually non-increasing.
Answer: In general one has
$$
  \lim f(a_n) = f(\lim a_n)
$$
if $f$ is continuous at $a = \lim a_n$.
In your case the function $f(x) = \lfloor x \rfloor$ is continuous on $\mathbb R \setminus \mathbb Z$. Hence if $a\not \in \mathbb Z$ you can exchange the limit with the floor function.


Running a function in sapply is returning faulty results when first ID is all missing

Question: I'm having a very odd, specific problem that I'm struggling to google, so I'm hoping I can just show someone.
I've written a function that will fill in some missing data according to a few conditions. For example, for panel data like this:
library(tidyverse)
library(data.table)

dt <- data.frame(id =  c(rep('a', 5),
                         rep('b', 5),
                         rep('c', 5)),
                 var1 = c(rep('', 4), 'bonjour',
                          'bye', NA, 'bye', 'bye', NA,
                          'hi', 'hi', NA, 'hi', 'hi'),
                 year = c(2005:2009,
                          1995:1998, 2002,
                          1995:1999))
dt
    id    var1 year
 1:  a         2005
 2:  a         2006
 3:  a         2007
 4:  a         2008
 5:  a bonjour 2009
 6:  b     bye 1995
 7:  b    <NA> 1996
 8:  b     bye 1997
 9:  b     bye 1998
10:  b    <NA> 2002
11:  c      hi 1995
12:  c      hi 1996
13:  c    <NA> 1997
14:  c      hi 1998
15:  c      hi 1999

I use the following function to update some of the missing values:
fill.in <- function(var, yr, finyr) {
  leadv <- lead(var, n=1, order_by = yr)
  lagv <- lag(var, n=1, order_by = yr)

  leadyr <- lead(yr, n=1, order_by = yr)
  lagyr <- lag(yr, n=1, order_by = yr)

  # ------- build the updated var w/ sequential conditions
  # keep the var as it is if not missing
  try1 <- ifelse(test = !is.na(var),
                 yes = var,
                 no = NA)

  # fill in if the lead and lag match and no more than 2 missing years
  try2 <- ifelse(test = is.na(try1) & leadv == lagv &
                        abs(leadyr-lagyr) <= 3 &
                        !is.na(leadv),
                 yes = leadv,
                 no = try1)

  # fill in with the lag if it's the final year of observed data
  ifelse(test = is.na(try2) & yr == finyr &
                abs(yr-lagyr) <= 3 & !is.na(lagv),
         yes = lagv,
         no = try2)
}

After a little bit of set-up, by and large I get good results:
# ------------ Set-up
# real data is big so use data.table
setDT(dt)

dt[, finalyr := max(year), by = id]

# don't want to fill in factor values
dt$var1 <- as.character(dt$var1)

# make empty strings NAs
dt[, var1 := na_if(var1, '')]

# useful for when i'm filling in many variables
fill.in.vs <- c('var1')
fixed.vnames <- paste0('fixed.', fill.in.vs)

# ------------ Call the function and results
dt[, (fixed.vnames) := sapply(.SD,
                              FUN = fill.in,
                              year,
                              finalyr,
                              simplify = FALSE, USE.NAMES = FALSE),
   by = id, .SDcols = fill.in.vs]

# this gives me what I want:
dt
    id    var1 year finalyr fixed.var1
 1:  a    <NA> 2005    2009       <NA>
 2:  a    <NA> 2006    2009       <NA>
 3:  a    <NA> 2007    2009       <NA>
 4:  a    <NA> 2008    2009       <NA>
 5:  a bonjour 2009    2009    bonjour
 6:  b     bye 1995    2002        bye
 7:  b    <NA> 1996    2002        bye
 8:  b     bye 1997    2002        bye
 9:  b     bye 1998    2002        bye
10:  b    <NA> 2002    2002       <NA>
11:  c      hi 1995    1999         hi
12:  c      hi 1996    1999         hi
13:  c    <NA> 1997    1999         hi
14:  c      hi 1998    1999         hi
15:  c      hi 1999    1999         hi

The problem is that when the first set of IDs--e.g. all the 'a' values--have empty strings that I turn into NAs, all values of the "fixed" variable end up NAs as well.
So using that same code but with the following data, I get all NAs in the new variable:
# id of 'a' now is all empty strings in var1:
dt <- data.frame(id =  c(rep('a', 5),
                         rep('b', 5),
                         rep('c', 5)),
                 var1 = c(rep('', 5),
                          'bye', NA, 'bye', 'bye', NA,
                          'hi', 'hi', NA, 'hi', 'hi'),
                 year = c(2005:2009,
                          1995:1998, 2002,
                          1995:1999))

# which results in this final data after running the same code above:
dt
    id var1 year finalyr fixed.var1
 1:  a <NA> 2005    2009         NA
 2:  a <NA> 2006    2009         NA
 3:  a <NA> 2007    2009         NA
 4:  a <NA> 2008    2009         NA
 5:  a <NA> 2009    2009         NA
 6:  b  bye 1995    2002         NA
 7:  b <NA> 1996    2002         NA
 8:  b  bye 1997    2002         NA
 9:  b  bye 1998    2002         NA
10:  b <NA> 2002    2002         NA
11:  c   hi 1995    1999         NA
12:  c   hi 1996    1999         NA
13:  c <NA> 1997    1999         NA
14:  c   hi 1998    1999         NA
15:  c   hi 1999    1999         NA

For brevity I won't show you all the things I've tried, but a few observations about when it happens:

All empty strings in the first ID isn't a problem if I do not convert empty strings to NA.
I only get this result if it's the first ID that has all empty strings; if it's the second set, the results are fine.
How I convert "" to NA doesn't matter, i.e. it's not an issue with na_if because it also happens when I use ifelse.

Overall I'm pretty stumped as to what's happening or how to investigate it further. I would really appreciate any help.
Comment: At the risk of asking you to lengthen an already long question, I think it would help if you include either or both of: (1) expected output; (2) your "conditions" for imputing data.
Comment: I think both are covered in the second and third code sections, but maybe they're still unclear?
Comment: Sorry, I was confused by the *"gives me what I want except"*, assuming that your sample data matched the "except" clause so therefore something was wrong.
Answer: When I run your code, I get this warning:
1: In [.data.table(dt, , :=((fixed.vnames), sapply(.SD, FUN = fill.in,  : Coercing 'character' RHS to 'logical' to match the type of the target column (column 0 named '').
I get it twice, once for the second, and for the third group.
As it says, the variable fixed.var1 is initialised as logical variable (for the group id==a); values that are added later are then converted to the same class 'logical'.
The major culprit here is your function fill.in(), since e.g.
logicalVar <- fill.in( var=rep(NA,5), yr=2005:2009, finyr=rep(2009,5)); class(logicalVar)
returns a logical variable.
So all you need to do is to wrap as.character() around the return of your function.
Comment: This worked -- thank you!


Unable to set cache max-age on ResourceResponse

Question: I'm trying to create a REST API GET response. My class extends ResourceBase and this is inside its get() method.
$response = new ResourceResponse(['time' => time()], 200);

$cache_meta_data = new CacheableMetadata();
$cache_meta_data->setCacheMaxAge(2);
$response->addCacheableDependency($page);
$response->addCacheableDependency($cache_meta_data);

return $response;

This will return the timestamp, but it will be cached forever until I manually clear the cache. When I run curl 'https://mylocaldrupal.dd:8443/rest/upcoming_matches/1?_format=json' -i -k, this is the output for the first hit.
HTTP/1.1 200 OK
Date: Sun, 11 Feb 2018 17:03:06 GMT
Server: Apache/2.4.17 (Unix) OpenSSL/1.0.1h mod_fcgid/2.3.9
X-Powered-By: PHP/7.0.14
Cache-Control: must-revalidate, no-cache, private
X-Drupal-Dynamic-Cache: UNCACHEABLE
X-UA-Compatible: IE=edge
Content-language: en
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expires: Sun, 19 Nov 1978 05:00:00 GMT
X-Generator: Drupal 8 (https://www.drupal.org)
X-Drupal-Cache: MISS
Transfer-Encoding: chunked
Content-Type: application/json

{"time":1518368586}

This is the output of the second hit, after a few seconds.
HTTP/1.1 200 OK
Date: Sun, 11 Feb 2018 17:04:41 GMT
Server: Apache/2.4.17 (Unix) OpenSSL/1.0.1h mod_fcgid/2.3.9
X-Powered-By: PHP/7.0.14
Cache-Control: must-revalidate, no-cache, private
X-Drupal-Dynamic-Cache: UNCACHEABLE
X-UA-Compatible: IE=edge
Content-language: en
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Expires: Sun, 19 Nov 1978 05:00:00 GMT
X-Generator: Drupal 8 (https://www.drupal.org)
X-Drupal-Cache: HIT
Transfer-Encoding: chunked
Content-Type: application/json

{"time":1518368586}

I can't really figure it out. Drupal.org documents don't really seem to help me neither. Am I missing something obvious?
Comment: see this example where 3600 seconds are set in a response event subscriber https://drupal.stackexchange.com/questions/196469/anonymous-user-cache-control/196557#196557
Comment: There is only one other place where responses are cached, check if this database table `cache_dynamic_page_cache` is filled after you've cleared the cache. Though this cache normally respects the cache metadata you've added to the response already.
Comment: Now you return no cache data, which means expire -1.
Comment: Correct! If I add the cache meta data to the response, and then set the max-age and expiration in the EventSubscriber it works. I'll update my question with the solution and credit you and Berdir :)
Comment: See this issue [Bubbling of elements' max-age to the page's headers and the page cache](https://www.drupal.org/project/drupal/issues/2352009) and this question https://drupal.stackexchange.com/questions/207800/rest-response-gets-cached-for-guest-user-requests
Comment: @4k4 I don't want to disable the cache completely! I want to set it to X seconds.
Comment: @4k4 I tried the event subscriber solution. I do see that the cache HIT and MISSES are correct with the example. However the data itself is still cached forever.
Comment: After you've cleared the cache a request should show this header: `Expires: Mon, 12 Feb 2018 12:22:00 GMT` with a time in the future. Following requests should show a cache HIT until the Expires time is reached and there is a cache MISS with a new Expires time.
Comment: Correct, I do see that the Expires headers are correct every time in line with the HIT and MISSES. However the actual data is still returning the cached value. In this case it always returns the `time()` from the first request. Until I do a manual cache clear.
Comment: It shows the line with from the REST interface. However I did notice the "expire" column is set to -1. This is maybe related?
Comment: Then there is something wrong with the cache metadata, for example I don't know what is in $page. Try to set only the cache max-age, nothing else.
Comment: I should have left out the $page parameter from the example. But it contains an URL parameter. Integer value of what "page" to load. Pagination like behaviour. Setting the max-age only doesn't work either.
This is what I have now: `$result = ['time' => time()]; return new ResourceResponse($result, 200);` And then the basic EventSubscriber to set the Expires header.
Answer: The referenced question from 4k4 describes how to disable the internal page cache completely.
To not disable but set an expiration on the anonymous/internal page cache, you can use the Expires header, just set it to a date in the future and that will be respected. That's the only thing that the internal page cache module currently respects.
See the linked core issue for more information, it's partially simply an overlook that it wasn't done before 8.0 but now it would be an API change to start respecting it and lots of things would then make Drupal uncacheable, like the language switcher block. So we'll have to be very careful with any change we make.
Comment: This is what we do in a response event subscriber, should also work directly in the controller IMHO, FinishResponseSubcriber should only set a custom expires header if none is set yet: `$response->setExpires(new \DateTime($cache_max_age . 'sec'));`
Comment: Thank you for pointing out that indeed MaxAge is ignored. I also found the referenced issue on DO. Do you have an example on how to set the Expires header? I tried to do it but somehow it always get overridden with the 19 nov 1978 date.
Comment: I tried this event subscriber approach and the Expires header is correct. However the data itself seems to be cached forever. Do I need to change the Cache-Control headers as well (see original question above)


what is the margin above the text

Question: What can be this possibly be above the helloblank fragment text.I have used

((AppCompatActivity)getActivity()).getSupportActionBar().hide();

to hide the Action Bar. The code for the xml is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.anshul.testfindout.InfotabFragment">

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"/>
</RelativeLayout>

Comment: refer to this [question](http://stackoverflow.com/questions/24956902/hiding-actionbar-left-blank-space-on-top)
Comment: To get a clear picture of your layout, check View Hierarchy in android device monitor. Its locator is *Tools -> Android -> Android Device Monitor* in Android Studio
Comment: Can you post your tabs layout?
Answer: If you want to hide the toolbar from all the pages use this in style.

what is the margin above the text


How to insert a comment in the middle of a SQL statement?

Question: I have a long and complicated SQL statement, in the middle of it, I want to insert a comment.
For example:
SELECT * FROM TABLE_A
WHERE Column1 IN ('Code1' --Comment here--, 'Code2' --Comment here--)

Please notice how the part after 'Code1' --Comment here-- is greyed out. That's because the whole line after -- is considered a comment.
Currently I have to do something like this:
SELECT * FROM TABLE_A
WHERE Column1 ='Code1' --Comment here--
   OR Column1 ='Code2' --Comment here--

I wonder if anyone has a better way to write this. Thanks..
Comment: Use `/* . . . */`.  Those are the delimited comment characters recognized by SQL Server.
Answer: Use inline comments for this:
SELECT * FROM TABLE_A
WHERE Column1 IN ('Code1' /*Comment here*/, 'Code2' /*Comment here*/)

Comment: I'm aware of the multi-line comment. Just never thought I would use it to take care of a one-liner issue. Thanks :)
Answer: Assuming you're using MySQL,
mysql> SELECT 1 /* this is an in-line comment */ + 1;

Source: https://dev.mysql.com/doc/refman/5.7/en/comments.html
Comment: Tagged as SQL Server, but is still correct if you remove the command line `mysql>`
Answer: For your case:
SELECT *
FROM TABLE_A
WHERE Column1 IN ('Code1' /* CODE1 */, 'Code2' /* CODE2 */)

I will add that this type of embedded comment is likely to make the code harder to follow rather than easier.  One such comment is probably okay, but if you want to include comments on all values, I would go with the multi-line version in your question.
Normally, by the way, the /* . . . */ is used for multi-line comments or to quickly comment out a block of code.
Comment: Thanks for the additional advice :)


Can't attach EC2 instance to Network Interface

Question: When trying to attach a network interface, it says...

No instances were found for this availability zone.

My instance is in us-east-1c and my network interface is in us-east-1b. Is that significant?
If so, how do I create the VPC in the same zone and if not then why this error?
EDIT:
I've re-created the VPC and the Network Interface is now us-east-1c and the EC2 instance is also us-east-1c. Same error message though!
Comment: are the interface and the instance in the same VPC? They must be.
Comment: did you try putting everything in the same VPC yet?
Answer: The subnet you are trying to connect to must be in the same availability zone as the instance. When you go to create a VPC, you usually create it with one subnet. Click "edit public subnet" and then select an availability zone. For example I see us-east-1a, us-east-1c, and us-east-1d. By default it is "no preference", which I believe means it will choose one for you.
Alternatively you can add a new subnet to your VPC. You can set an availability zone for a subnet in the same manner as above.
Just to be clear - us-east-1c and us-east-1b are distinctly different availability zones.
Also, see the VPC FAQ
Q. Can I attach a network interface in one AZ to an instance in another AZ?
Network interfaces can only be attached to instances residing in the same AZ.
Q. Can I attach a network interface in one VPC to an instance in another VPC?
Network interfaces can only be attached to instances in the same VPC as the interface.
Comment: I've updated the question.


A field Isomorphism $f:\mathbb{C} \rightarrow \mathbb{C}$

Question: Any suggestions for a field Isomorphism $f:\mathbb{C} \rightarrow \mathbb{C}$ other than the identity map?
Comment: Complex conjugation?
Comment: or of [Field isomorphism of $\mathbb{C}$ onto itself](http://math.stackexchange.com/q/705127). Seriously, try searching.
Answer: Complex field automorphisms other than the identity and the complex conjugation do exist, and are called "wild". They are described in the expository paper by Paul B. Yale, Automorphisms of the complex numbers, Math. Mag. 39 (1966), 135-141.
Answer: How about the map $\phi:\Bbb C\to\Bbb C$ given by $\phi(z) = \overline{z}$? It satisfies $\phi(0) = 0$, $\phi(1) = 1$, $\phi(z_1+z_2) = \phi(z_1)+\phi(z_2)$ and $\phi(z_1z_2) = \phi(z_1)\phi(z_2)$.
Answer: Hint: Define $f: \mathbb C \to \mathbb C$ by $f(z) = \overline {z} $.
Answer: The only continuous automorphisms of $\mathbf C$ are the identity map and conjugation.
However, using the axiom of choice, it can be shown there is a infinity of non-continuous field automorphisms of $\mathbf C$. You can look at this paper.


Inner product of scaled Hermite functions

Question: I'm attempting to find a closed form expression for
$$\int_{-\infty}^{\infty}e^{-\frac{x^2\left(1+\lambda^2\right)}{2}}H_{n}(x)H_m(\lambda x)dx$$
where $H_n(x)$ are the physicist's hermite polynomials, but haven't had any luck. Anyone know of a way to compute this?
Answer: Denoting the integral by $I_{m,n}$, write the generating function
\begin{align*}I(s,t)=&\sum_{m=0}^{\infty}\sum_{n=0}^{\infty}I_{m,n}\frac{s^m}{m!}\frac{t^n}{n!}=\\
=&\int_{-\infty}^{\infty}e^{-\frac{x^2(1+\lambda^2)}{2}}
\underbrace{\left(\sum_{m=0}^{\infty}H_m(\lambda x)\frac{s^m}{m!}\right)}_{e^{2\lambda x s-s^2}}
\underbrace{\left(\sum_{n=0}^{\infty}H_n( x)\frac{t^n}{n!}\right)}_{e^{2xt-t^2}}dx=\\
=&e^{-s^2-t^2}\int_{-\infty}^{\infty}e^{-\frac{x^2(1+\lambda^2)}{2}+2x(\lambda s+t)}dx=\\
=&\exp\left\{\frac{2(\lambda s+t)^2}{1+\lambda^2}-s^2-t^2\right\}\sqrt{\frac{2\pi}{1+\lambda^2}}.
\end{align*}
The rest should be clear. Of course, $I_{m,n}=0$ if $m,n$ are of different parity.
Comment: Well now, there's a trick worth remembering.
Comment: @KHAAAAAAAAN We know how to expand the exponential $e^{a t^2+b t}$ in $t$ around $0$, since we can expand separately $e^{at^2}$ and $e^{bt}$ and then multiply the resulting series. The coefficients of this resulting series will be *finite* sums of products of the coefficients of $e^{at^2}$ and $e^{bt}$, something like $\sum_{k,l}\frac{a^kb^l}{k!l!}$. It is a closed-form expression and we cannot get anything better. I hope this clarifies the statement.
Comment: I hate to bump a 3 year old thread, but could you expand on this? I'm faced with basically the same problem (with a 1+lambda in the exponent rather than 1+lambda^2), but I do not see how to go from the expression for I(s,t) to an expression just for I_{m,n}. Any guidance would be greatly appreciated!
Comment: In particular, I can find I_{n,m} by taking the n^th and m^th derivative of the exponential term, and seeing s and t to 0, but this is not a very closed form solution.
Comment: @KHAAAAAAAAN I have found a closed form solution in two cases, $n=m$, $n\ge m$ and will post the third case soon.
Answer: The integral when $n=m$ is
$$
I_{nn} = 2^{2n}\sqrt{2\pi}\left(n!\right)^{2} \frac{\lambda^n}{{\left(\lambda^{2} + 1\right)^{n + \frac{1}{2}}}} {\sum_{k=0}^{\left \lfloor \frac{n}{2} \right \rfloor} \frac{\left(-1\right)^{k} }{2^{4k}  (k!)^{2} \left(n-2k \right)!}}\left(\frac{{\lambda^{2} - 1}}{\lambda}\right)^{2k}.
$$
The integral is zero whenever $n$ and $m$ have opposite parity.
When $n\ge m$, define $s=\frac{n-m}{2}$ (which is guaranteed to be an integer) and the integral becomes
$$
I_{nm}=2^{2m}\sqrt{2\pi}m! n!\frac{\lambda^{m}{\left(1-\lambda^{2} \right)}^{s}   }{{\left(\lambda^{2} + 1\right)}^{m + s + \frac{1}{2}}}{\sum_{l=0}^{\left \lfloor \frac{m}{2} \right \rfloor} \frac{\left(-1\right)^{l} }{2^{4l}  \left(l + s\right)! l! \left(m-2l\right)!}\left(\frac{{\lambda^{2} - 1}}{\lambda}\right)^{2l}}
$$
The general case is
$$
I_{nm}=2^{2m}\sqrt{2\pi}m! n!\frac{\lambda^{\operatorname{min}(n,m)}{\left(1-\lambda^{2} \right)}^{s}}{{\left(\lambda^{2} + 1\right)}^{\operatorname{max}(n,m) + \frac{1}{2}}}{\sum_{l=0}^{\left \lfloor \frac{\operatorname{min}(n,m)}{2} \right \rfloor} \frac{\left(-1\right)^{l} }{2^{4l}  \left(l + s\right)! l! \left(\operatorname{min}(n,m)-2l\right)!}\left(\frac{{\lambda^{2} - 1}}{\lambda}\right)^{2l}}
$$
In order to derive this result, first change to probabilists Hermite polynomials
$$
H_n(x)=2^{\frac{n}{2}}\operatorname{He}_n(\sqrt{2}x)
$$
so the integral becomes
$$
I_{nm} = 2^{\frac{n+m}{2}}\int_{-\infty}^\infty e^{-\frac{x^2}{2}(1+\lambda^2)}\operatorname{He}_n(\sqrt{2}x)\operatorname{He}_m(\lambda\sqrt{2}x)dx.
$$
Change the integration variable in order to recover the probabilists  weighting function $y=x\sqrt{1+\lambda^2}$
$$
I_{nm} = \frac{2^{\frac{n+m}{2}}}{\sqrt{1+\lambda^2}}
\int_{-\infty}^\infty e^{-\frac{y^2}{2}}
\operatorname{He}_n\left(y\sqrt{\frac{2}{1+\lambda^2}}\right)\operatorname{He}_m\left(y\sqrt{\frac{2\lambda^2}{1+\lambda^2}}\right)dy.
$$
Use the scaled Hermite polynomial on both polynomials
$$
\operatorname{He}_n(\gamma x) = n!\sum_{k=0}^{\left \lfloor \frac{n}{2} \right \rfloor}\frac{1}{2^kk!(n-2k)!}\gamma^{n-2k}\left(\gamma^2-1\right)^k H_{n-2k}(x)
$$
leads to a very long expression, from which all three cases ($n=m$, $n\ge m$, $n\le m$) obtain
$$
I_{nm} = \frac{2^{\frac{n+m}{2}}}{\sqrt{1+\lambda^2}}n!m!\sum_{k=0}^{\left\lfloor\frac{n}{2}\right\rfloor}\sum_{l=0}^{\left\lfloor\frac{m}{2}\right\rfloor}\frac{(-1)^k\left(\sqrt{\frac{2}{1+\lambda^2}}\right)^{n-2k+m-2l}\lambda^{m-2l}\left(\frac{\lambda^2-1}{\lambda^2+1}\right)^{k+l}}{2^kk!(n-2k)!2^ll!(m-2l)!}\\
\times\int_{-\infty}^\infty\operatorname{He}_{n-2k}(x)\operatorname{He}_{m-2l}(x)e^{-\frac{x^2}{2}}dx.
$$
Orthogonality will constrain one of the sums -- always choose the sum associated with the maximum of $n,m$.  Take the case $n\ge m$, as previously stated, the parity of $n$ and $m$ has to be the same, otherwise the integrand is odd and the integral vanishes.  Therefore write $n=m+2s$ for $s\in \mathbb{Z}$.  The orthogonality constraint can be written $k=l+s$ and, after algebraic manipulation, the result above obtains.
Comment: By applying the reflection property of the Gamma function to $(min(n,m)-2l)!$ then using Legendre's duplication formula (introducing half-integers) you may easily transform your sum into a Gauss hypergeometric function of the polynomial type, see my proposed answer.
Answer: There is a more compact closed-form solution expressed with hypergeometric functions, using known Laplace transforms. The method used can be recycled to find a closed-form solution for other integrals of the same type; this is why I am adding this answer despite the fact that it is an old question.
Noting that for $k \in \mathbb{N}_0$:
$$H_{2k}(x) = (-1)^k 2^{2k}\, k! \, L_{k}^{-\frac{1}{2}}(x^2) $$
$$H_{2k+1}(x) = (-1)^k 2^{2k+1}\, k! \, x\, L_{k}^{\frac{1}{2}}(x^2), $$
in which $L_k^\alpha$ is a generalized Laguerre polynomial; for even values of the indices of the posted integral $I_{n,m}$, an obvious change of variables gives:
\begin{align}
I_{2k,2p} &= (-1)^{k+p}2^{2(k+p)}k!\,p! \int_{0}^\infty u^{-\frac{1}{2}} e^{-\frac{u}{2} (1+\lambda^2)} L_k^{-\frac{1}{2}}(u)L_p^{-\frac{1}{2}}(\lambda^2 u)\,du \\
&= (-1)^{k+p}2^{2(k+p)}k!\,p!\; \mathscr{L}\left(u^{-\frac{1}{2}} L_k^{-\frac{1}{2}}(u)L_p^{-\frac{1}{2}}(\lambda^2 u); \frac{1}{2}(1+\lambda^2)\right) \tag{1}
\end{align}
in which $\mathscr{L}(f; y)$ is the standard Laplace transform of function $f$ taken at point $y$.
This Laplace transform is a classic that is to be found in published tables. The general formula is [Gradshtein 2015, eq. 7.414.4 p.817] :
$$\mathscr{L}\left(u^\alpha L_n^\alpha(b\, u)L_m^\alpha(c\,u); y\right) = \frac{\Gamma(m+n+\alpha+1)(y-b)^n(y-c)^m}{m! \, n! \, y^{m+n+\alpha+1}}{_2}F{_1}\left(-m, -n; -m-n-\alpha; \frac{y(y-b-c)}{(y-b)(y-c)}\right), \tag{2}$$
where ${_2}F_{1}$ denotes the Gauss hypergeometric function, under the provisions that $\Re \alpha > -1$ and that $\Re y > 0$.
Setting in $(2)$: $\alpha = -\frac{1}{2};\,b= 1;\, c= \lambda^2;  y =\frac{1}{2}(1+\lambda^2)$, all provisions are obviously satisfied, for $\lambda$ real. Note that the question does not make it specific that $\lambda$ be real. If it were a complex number instead, there is good reason to believe that there would be no solutions for $\Re \lambda^2 \leq -1$, considering the validity domain of the Laplace transform formula, a non-trivial reservation that it would be harder to make following other approaches.
With these reservations, we have:
$$\mathscr{L}\left(u^{-\frac{1}{2}} L_k^{-\frac{1}{2}}(u)L_p^{-\frac{1}{2}}(\lambda^2\, u); \frac{1}{2}(1+\lambda^2)\right) = (-1)^k\frac{\sqrt{2}\Gamma(p+k+\frac{1}{2})(1-\lambda^2)^{p+k}}{p! \, k! \, (1+\lambda^2)^{p+k+\frac{1}{2}}}{_2}F{_1}\left(-p, -k; -p-k+\frac{1}{2}; \left(\frac{1+\lambda^2}{1-\lambda^2}\right)^2\right) \tag{3}$$
which gives the solution to the question, for even indices:
\begin{align}
I_{2k,2p} &= (-1)^{p}2^{2(k+p)+\frac{1}{2}} \frac{\Gamma(p+k+\frac{1}{2})(1-\lambda^2)^{p+k}}{(1+\lambda^2)^{p+k+\frac{1}{2}}}{_2}F{_1}\left(-p, -k; -p-k+\frac{1}{2}; \left(\frac{1+\lambda^2}{1-\lambda^2}\right)^2\right) \tag{4}
\end{align}
This formula can also be recast using only factorials, by the Legendre duplication formula, as follows:
\begin{align}
I_{2k,2p} &= (-1)^{p} 2\sqrt{2 \pi}  \frac{(2(p+k)-1)! \, (1-\lambda^2)^{p+k}}{(p+k-1)!\,(1+\lambda^2)^{p+k+\frac{1}{2}}}{_2}F{_1}\left(-p, -k; -p-k+\frac{1}{2}; \left(\frac{1+\lambda^2}{ 1-\lambda^2}\right)^2\right) \tag{5}
\end{align}
For odd indices of the Hermite polynomials, $(1)$ becomes
\begin{align}
I_{2k+1,2p+1} &= (-1)^{k+p}2^{2(k+p+1)}k!\,p!\; \lambda\; \mathscr{L}\left(u^{\frac{1}{2}} L_k^{\frac{1}{2}}(u)L_p^{\frac{1}{2}}(\lambda^2 u); \frac{1}{2}(1+\lambda^2)\right) \tag{6},
\end{align}
as we now take $\alpha = \frac{1}{2}$, so that $(4)$ becomes
\begin{align}
I_{2k+1,2p+1} &= (-1)^{p}2^{2(k+p+1)+\frac{3}{2}} \lambda \;\frac{\Gamma(p+k+\frac{3}{2})(1-\lambda^2)^{p+k}}{(1+\lambda^2)^{p+k+\frac{3}{2}}}{_2}F{_1}\left(-p, -k; -p-k-\frac{1}{2}; \left(\frac{1+\lambda^2}{1-\lambda^2}\right)^2\right) \tag{7}
\end{align}
Note that the Gauss function has a negative integer first parameter and so can only be a polynomial, which explains the polynomial form of Tom Davis's solution.
The above solution has been numerically tested using Mathematica 14.

References
Gradshtein, I. S. (Izrail Solomonovich), rev. Daniel Zwillinger
[Tablitsy integralov, summ, riadov i proizvedenii. English] Table of integrals, series, and products. – Eighth edition, Elsevier, 2015.


Meaning of risk

Question: Suppose you want to look at the risk of glaucoma in 7 groups (e.g. grouped by weight: 100-110 lbs, 111-120 lbs, etc..). Is risk measured by the proportion of people that have glaucoma? Or should you look at excess risk/relative risk? I guess it shouldn't matter what you look at because the risk pattern would be the same?
Answer: I don't know if this is the right site to ask, as some domain knowledge about medical situations may be needed, but let me give it a try:
Classically, risk involves both the probability of a outcome and the severity of a bad outcome.  In your situation, this means that risk relates to both the prevalence of the disorder (the chance of a random patient in the group getting glaucoma over a year, say) and the severity of the disorder (e.g., how significant the loss of visual function is).
Are you trying to determine the likelihood of developing glaucoma as a function of weight?  If so, one thing you may need to take into account is the possibility of confounding factors, such as age, gender, or diet.  As a made-up hypothetical, suppose that men were more likely to develop glaucoma than women.  Since men tend to weigh more than women, you might discover that the higher weight groups have a higher incidence of glaucoma.  However this would not necessarily mean that weight is a risk factor for glaucoma, or that (all else being equal) losing weight will reduce the likelihood of developing glaucoma: in my hypothetical it is possible that this once you control for gender, the dependence on weight disappears.  This is only intended to be an illustrative example -- I'd imagine that the risk of glaucoma is not actually dependent upon one's gender, but for all I know it might well be dependent upon other factors that are correlated to weight.  Therefore, you will need to be very careful in your statistical analysis to account for these potential confounding factors.
Comment: I am just looking at logistic regression. Is risk basically just odds ratio?

Meaning of risk


Can someone help me? Json bad string at line 20

Question: <code>{
  "suffix": {},
  "chat": {
    "Depreloid": "local"
  },
  "spy-mode": {
    "Depreloid": true
  },
  "last-pm-interlocutor": {
    "BLACKCAT12321": "Depreloid",
    "Depreloid": "BLACKCAT12321"
  },
  "prefix": {
    "Depreloid": "\u0026c[jfhkj] ",
    "Sovaqa": "\u00266[jrqkrq] ",
    "KorolSombra": "\u0026b [ljwljf] ",
    "BLACKCAT12321": "\u0026b [test] ",
    "Nikof2": "\u00262[Лfkwokfw] ",
    "Yra": "\u00262[fkffkfjfj] ",
    },
}

</code>
well, I tried to move brackets) in short, I want to fix the bad string error and that's it...
Comment: Remove the trailing comma(`,`) after: `"Yra": "\u00262[fkffkfjfj] "`
Comment: now bad object at line 21
Answer: You need to remove:
<code>{
  "suffix": {},
  "chat": {
    "Depreloid": "local"
  },
  "spy-mode": {
    "Depreloid": true
  },
  "last-pm-interlocutor": {
    "BLACKCAT12321": "Depreloid",
    "Depreloid": "BLACKCAT12321"
  },
  "prefix": {
    "Depreloid": "\u0026c[jfhkj] ",
    "Sovaqa": "\u00266[jrqkrq] ",
    "KorolSombra": "\u0026b [ljwljf] ",
    "BLACKCAT12321": "\u0026b [test] ",
    "Nikof2": "\u00262[Лfkwokfw] ",
    "Yra": "\u00262[fkffkfjfj] "
    }
}
</code>
So basically the trailing comma after "Yra": "\u00262[fkffkfjfj] " needs to be removed and the trailing comma at the end of the enclosing object needs to be removed.


Como colocar EditText ao lado de Edit Text dinâmicamente?

Question: Estou criando um formulário dinâmico e precisava exibir em um formulário, Idade e Endereço na mesma linha. Eu criei um "for" para criar n vezes o mesmo formulário mas está dando erro. segue o código:
<code>public void form() {

    for (int i = 0; i < 3; i++) {

        TextView tv = new TextView(this);
        tv.setText("Nome");
        layout.addView(tv);
        EditText et = new EditText(this);
        et.setText("");
        layout.addView(et);

        TextView tv1 = new TextView(this);
        tv1.setText("Idade");
        layout2.addView(tv1);
        EditText et1 = new EditText(this);
        et1.setText("");
        layout2.addView(et1);

        TextView tv2 = new TextView(this);
        tv2.setText("Endereco");
        layout2.addView(tv2);
        EditText et2 = new EditText(this);
        et2.setText("");
        layout2.addView(et2);
        // AQUI COLOCO O LAYOUT HORIZONTAL DENTRO DO VERTICAL, FUNCIONA A LÓGICA, PORÈM ESTÁ DANDO ERRO POR CAUSA DO FOR
        layout.addView(layout2);

        TextView tv3 = new TextView(this);
        tv3.setText("Telefone");
        layout.addView(tv3);
        EditText et3 = new EditText(this);
        et3.setText("");
        layout.addView(et3);

    }

}
</code>
Obs: layout -> LinearLayout vertical
     layout2 - > LinearLayout horizontal.
Se eu tirar o "for" do código, ele  funciona, mas eu preciso usar o "for". Alguém pode dar uma ajuda. Agradeço desde já!!!
Comment: Não seria pelo fato de que você esta criando os campos com os mesmos nomes?
Por Ex, se você tirar esse for e pegar esses Texts e copiar e colar três vezes ele irá apresentar erro né? Tem como mostrar a mensagem de erro?
Comment: é exatamente esta a mensagem de erro, será que tem algum jeito de manter o for. Porque se eu tirar o "layout.addView(layout2);" funciona mesmo com o for.
Answer: Uma view não pode ser adicionada 2 vezes dentro de outra view. Tu tem que tornar a criação de layout2 dinâmica.
Para fazer isso tu pode criar essa view em um xml separado e inflar cada vez que quiser uma nova instância.


label and input in same line on form-group

Question: I have a form group with both a label and input

     Name
     <input type="text" class="form-control" name="name" id="name" [(ngModel)]="person.name" disabled/>

However the label displays above the input field, i need it at its side. I have bootstrap 4.0 installed.
I've tried with class="col-sm-2 col-form-label" and does not work either.
Any suggestions?
Answer: The col-sm-2 shouldn't be nested directly in col-md-12. Use the grid like this...
https://www.codeply.com/go/Gdt1BZs9Hg
<form class="col-12">

     Name
     <input type="text" class="form-control col-sm-10" name="name" id="name" [(ngModel)]="person.name" disabled/>

</form>

Notice that form-row must used to contain the col-. The col-sm-10 controls the width of the input, so you can change that as needed. Read more in the docs: https://getbootstrap.com/docs/4.0/components/forms/#form-grid
Also, note the correct use of the grid row > columns from the Bootstrap docs...

In a grid layout, content must be placed within columns and only
  columns may be immediate children of rows... You may also swap .row for .form-row, a variation of our standard grid row that overrides the default column gutters for tighter and more compact layouts.
Answer: You can achieve it by using class form-inline

         Name
         <input type="text" class="form-control" name="name" id="name" [(ngModel)]="person.name" disabled/>

Answer: The suggestions in other posts didn't work in Bootstrap 5. So I tested it by updating one of the demos in the Bootstrap 5 documentation and it worked. With this demo application, , <input> and <button> elements can be defined in a single line:

<link href="https://firstname.lastname@example.com/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">

<form role="form">

    Article Title
    <input type="text" class="form-control" placeholder="Please enter the new article name..." aria-label="Username" aria-describedby="addon-wrapping">
    <button class="btn btn-primary" type="submit" id="article-update-button">Update</button>

</form>

The form screenshot is available below: For more information, visit the Bootstrap 5 Input Group page.
Answer: See Bootstrap 4 documentation about Forms and use form-inline

     Name
     <input type="text" class="form-control" name="name" id="name" [(ngModel)]="person.name" disabled/>


Configuration for Jupyter Notebook on a home Ubuntu server: kernel is not connecting

Question: I set up an Ubuntu server to host a Jupyter Notebook that I can access from the web.
Everything seems to work but whenever I open the notebook, the Python Kernel is stuck and it does not connect:

In the terminal, with the current configuration it does not report warnings or errors:

but I have tried many similar configurations and sometimes, when the kernel tried to connect, it was showing warnings like:

[W 01:51:26.202 NotebookApp] 400 GET /api/kernels/aa45474d-7fb8-4685-98dc-5ebeba378f41/channels?
[W 16:06:07.386 NotebookApp] 403 GET /api/kernels/f589fe39-b21c-4eb1-9543-dcf2b9c5881d/channels?

I have found various similar issues online and they usually point to either the Jupyter configuration or the Apache/nginx configuration, however I have tried many of the proposed configurations and none of them solved my problem. I cannot exclude the problem is somewhere else.
jupyter_notebook_config.py:

Notice that I have commented out the references to the certificate, if I don't, then the notebook browser will give a "proxy error" and it won't load. However, the certificate is loaded in the apache configuration and seems to work fine there. Is it necessary to have it loaded here as well?
apache configuration:

Commented out are some of the many lines I tried to add following suggestions from other pages. Any help will be really welcome, I have been stuck on this for one week now.
Answer: After a full day of attempts I found a solution.

In the notebook configuration I uncommented the certificate lines.
In the apache configuration I added:
SSLProxyEngine On
I have changed the reverse proxy lines using https:
ProxyPass https://localhost:5000/notebook
ProxyPassReverse https://localhost:5000/notebook
I removed the authentication lines:
AuthName "Please login"
AuthType basic
AuthBasicProvider file
AuthUserFile "/home/pomi/auth_file_name"
Require valid-user


Problema CSS, não funciona

Question: Alguns comandos em CSS não estão funcionando.
Segue os comandos:
HTML

Departamentos

Blusas e Camisas

Manga curta

Manga cumprida

Camisa social

Camisa casual

Calças

Saias

Vestidos

Sapatos

Bolsas e Carteiras

Acessórios

CSS
   a[href^=http://]:after {
  content: '(externo)';
}

li ul {
    display: none;
}

Não faz o que era pra fazer, eu posso colocar color:red; no segundo que nada acontece.
Nada que eu coloque entre chaves funciona com o li ul separado por espaço, só funciona se eu separá-los por virgula li,ul.
Alguém sabe me dizer o porque?
Já testei com Chrome e com o Safira, ambos atualizados.
Uso o Sublime Text.
Objetivo
Retirar o submenu de Blusas e Camisas utilizando display:none; no CSS
Comment: Posta o HTML também. Mas assim, está errado.
Comment: @DiegoSouza Coloquei o **HTML** e mais algumas explicações, se puder me ajudar, eu agradeço.
Answer: Correção
Amigo meu descobriu o problema (estava bem debaixo dos meus olhos).
Segue a correção:

Blusas e Camisas

Manga curta

Manga cumprida

Camisa social

Camisa casual

Calças

Código correto:

Blusas e Camisas

Manga curta

Manga cumprida

Camisa social

Camisa casual

Calças

CSS
ul li ul{
    display:none;
}

Motivo
O motivo pelo qual o código não estava dando certo, era porque não fechei o li do submenu como explicado acima.
Agradecimento
Obrigado a todos que tentaram de alguma forma me ajudar a solucionar o problema,  que no caso foi minha falta de atenção ao código.
Ignorei totalmente revisar o HTML.
Que fique de aprendizado para os futuros leitores desse tópico, se o seu CSS não funcionar, procure revisar seu HTML, ele pode estar errado, O que pode influenciar ao tentar usar o CSS.
Answer: li ul

O comando acima não é sequência, então se fizer isso não vai funcionar mesmo.
O certo seria assim:
ul li

Porque no HTML é assim:

 1

 1

 1

Se você separar por vírgula vai funcionar, porquê o CSS não entende como sequência e sim como tags separadas.
ul, li = li, ul
Comment: Mas qual o seu problema ? Eu te expliquei o porquê não está funcionando o que você quer.
Comment: Você foi quem chegou mais próximo da solução do problema.

meu código é esse, quero que o "submenu" de **Blusas e Camisetas**não apareça.

segue o Código:
Comment: Coloquei o HTML e mais algumas explicações, se puder me ajudar, eu agradeço.
Comment: css utiliza elementos em cascata, você poderia fazer de duas maneiras:

1ª: ul li ul li {display:none};

2ª: você poderia acrescentar uma classe nos  ou  que deseja ocultar e o css referenciar a mesma. Exemplo:
HTML:Manga curta ou
HTML:...
CSS: .submenu{display:none;}
Comment: Por que eu fui negativado ? Legal isso, hein! O cara posta uma pergunta mal feita, eu tento ajudar, ainda peço para ele postar mais coisas. Daí vem alguém e negativa.

Problema CSS, não funciona


Get data as response from php script on server

Question: I'm sending some data(options) via POST with javascript to a php script that's on my server, which does a mysql query and should return data back to the current page.
This is the jquery code I'm using -
<code>$.post( "contact.php",  {min_age:"25"})
  .done(function(data) {
  console.log(data);
});
</code>
Now I've got the required data extracted from mysql and encoded it to json, and stored it in a variable in my php script in server. How do I post it back to the current page?
Comment: So what is your question here? The `data` variable in the jQuery post would be what is returned from your web server
Comment: try reading the jQuery Docs
Comment: The response is a JSON data? You want to write the content on your page?
Comment: All you need to do is parse `data`.
Comment: To place it back into your page you can use `$("#response").html(data);` provided you have a container named `response` and you have the `data` as suggested above. This would be in your success function.
Comment: @Rasclatt that won't work well with JSON as mentioned by OP
Comment: @charlietfl yeah, true, I missed the whole jSON comment there. Thanks...
Answer: In order to 'send the data' from php, you need to echo it, after setting the correct header:
<code>header('Content-Type: application/json');
echo json_encode($data);
</code>
Comment: You may also set, `}, 'json');` as the last argument to the `$.post()` method to have your `data` interpreted as JSON.


stochastic processes

Question: Can someone please help me with this problem?
Consider the probability space $\Omega = \{1,2,3,4,\ldots\}$  with a probability $P$ given by $P({i}) =p_i.$  Naturally, $p_i > 0$ and $\sum p_i = 1.$ The filtration $F_n$  is $\{ \{1\},\{2\},\ldots,\{n\}, \{n+1,n+2,\ldots\}\}.$  Let $X$ be a random variable defined by $X(i) = x_i.$
what does it means in this case that $E(X^2)$  is finite. Find $E(X\mid F_n).$
Thanks!
Answer: For $\mathbb{P} = \sum_{i = 1}^{\infty} p_i \cdot \delta_i$ (where $\delta_i$ denotes the dirac measure on $\{i\}$), $f \in L^1(\mathbb{P})$ the equality
$$\int_{\Omega} f \, d\mathbb{P} = \sum_{i=1}^{\infty} p_i \cdot f(i)$$
holds. Thus "$\mathbb{E}(X^2)$ finite" means $$\mathbb{E}(X^2) = \sum_{i=1}^{\infty} p_i \cdot X(i)^2 = \sum_{i=1}^{\infty} p_i \cdot x_i^2 < \infty$$

Concerning ${F}_n$:  Note that $(F_n)_{n \in \mathbb{N}}$ is not a filtration! Filtration means

$F_n$ is a $\sigma$-algebra for $n \in \mathbb{N}$
$\mathcal{F}_n \subseteq \mathcal{F}_{n+1}$ for all $n \in \mathbb{N}$

Neither is fulfilled for the given $F_n$. So probably you want to consider the $\sigma$-algebra generated by the given sets?

To find $\mathbb{E}(X \mid F_n)$ you should use the following theorem

Let $X \in L^1$ a random variable, $\mathcal{F}$ a sub-$\sigma$-algebra and $Y \in L^1(\mathcal{F})$. Then $Y = \mathbb{E}(X \mid \mathcal{F})$ if and only if $$\forall F \in \mathcal{F}: \int_F Y \, d\mathbb{P} = \int_F X \, d\mathbb{P}$$ If $\mathcal{F} = \sigma(\mathcal{G})$, then it suffices to check the equality for $G \in \mathcal{G}$.

(If you don't get along with it, don't hesitate to ask. I'll add some more hints in this case.)

stochastic processes


How do I get ASP.NET WebForms Routing to route .asmx JSON calls properly?

Question: I am attempting to implement multi-tenancy in a legacy ASP.NET WebForms app.  I want the URL to indicate the proper client, like so:
http://example.com/client_name/Default.aspx
http://example.com/client_name/MyWebService.asmx

However, I cannot get it to route the .asmx's properly. This routing rule picks up all incoming urls just fine:
routes.Add("ClientSelector", new System.Web.Routing.Route
(
   "{client}/{*path}",
   routeHandler: new ClientRoute()
));

But I am having issues with handling .asmx calls.  Here's my IRouteHandler, below.  The error I get is:
A first chance exception of type 'System.Web.Services.Protocols.SoapException' occurred in System.Web.Services.dll

Additional information: Unable to handle request without a valid action parameter. Please supply a valid soap action.

It's supposed to be JSON, but for some reason it's not working.  I am setting the content-type - if I send this same exact request without routing, it works fine.
public class ClientRoute : System.Web.Routing.IRouteHandler
{
    private string m_Path;
    private string m_Client;

    public ClientRoute() { }
    public bool IsReusable { get { return true; } }

    public IHttpHandler GetHttpHandler(System.Web.Routing.RequestContext requestContext)
    {
        this.m_Path = (string)requestContext.RouteData.Values["path"];
        this.m_Client = (string)requestContext.RouteData.Values["client"];

        string virtualPath = "~/" + this.m_Path;

        bool shouldValidate = false;

        if (shouldValidate && !UrlAuthorizationModule.CheckUrlAccessForPrincipal(
            virtualPath, requestContext.HttpContext.User,
                          requestContext.HttpContext.Request.HttpMethod))
        {
            requestContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            requestContext.HttpContext.Response.End();
            return null;
        }
        else
        {
            HttpContext.Current.RewritePath(virtualPath);
            HttpContext.Current.Items.Add("Client", this.m_Client);

            if (virtualPath.EndsWith(".aspx"))
                return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));
            else
            {
                var asmxPos = virtualPath.IndexOf(".asmx", StringComparison.OrdinalIgnoreCase);
                if (asmxPos >= 0)
                {
                    // What goes here?  This isn't working...
                    var asmxOnlyVirtualPath = virtualPath.Substring(0, asmxPos + 5);
                    return new System.Web.Services.Protocols.WebServiceHandlerFactory().GetHandler(
                        HttpContext.Current, HttpContext.Current.Request.HttpMethod, asmxOnlyVirtualPath, HttpContext.Current.Server.MapPath(asmxOnlyVirtualPath));
                }
                else
                    return new StaticRoute();
            }
        }
    }
}

Relevant links:

Getting ScriptHandlerFactory handler
Comment: What happens, your code that is commented out looks correct, do you get an error?
Comment: I tweaked my code and added the error msg...
Answer: The open source http://www.teamlab.com project is built with ASP.NET Webforms, and uses a multitenant/saas model.  I noticed you posted another question inquiring about multitenancy.
Perhaps you can look into their code for reference ideas.
Answer: I tried my best, ended up failing, and converted all my web services to WCF .svc services instead.


Visualise output from David Blei's HDP topic model algorithm in R

Question: I've been experimenting with David Blei's C++ HDP implementation but am struggling to print the topics using the R.
The package includes an R script for printing the identified topics, print.topics.r and the documentation that comes with the package provides some instructions for its use as follows:

A R script (print.topics.R) is included to print topics. Make sure it is executable. (chmod +x print.topics.R) For example,
print.topics.R mode-topics.dat vocab.dat topics.dat 10

I've made the script executable, but I'm struggling to get it to work. I have a range of files in the output directory from running the algorithm, but don't seem to be able to identify the right combination of files to pass in as arguments to the script.
The files I'm working with are:
test-mode-topics.dat
test-mode-word-assignments.dat
I don't have a vocab.dat file, but I do have a vocab.txt file (which I also renamed to vocab.dat to no avail).
I've tried running the following command:
./print.topics.r test-mode-word-assignments.dat vocab.dat test-mode-topics.dat
This returns the following error:

Error in sum(prob) : invalid 'type' (character) of argument
  Calls: print.topics
  Execution halted

Has anyone figured out the right way to structure the command with the available files?
Comment: Please add a [reproducible example](http://stackoverflow.com/q/5963269/1217536) for people to work with.
Comment: I'm voting to close this question as off-topic because it is about how to use R without a reproducible example.
Comment: The problem can be replicated by running the code in the linked git repo.
Answer: After a bit of wrangling, I managed to get the following command to play ball:
./print.topics.r /path/to/mode-topics.dat/ /path/to/vocab.dat topics.dat 4

The first arg passes in the mode-topics.dat file generated by HDP.
The second arg passes in the file containing a list of the words in the corpus
The third argument passes in the number of terms per topic (the default is 5)


Como gerar uma matriz de 3 colunas onde para cada coluna existem 3 possibilidades distintas?

Question: O problema é o seguinte: Em um vetor eu tenho 3 conjuntos (A, B e C). Cada conjunto pode ter até 3 valores distintos(0, 1 ou 2). Dessa forma eu preciso gerar todas as combinações possíveis para esse caso, com fórmula 3³ = 27 possibilidades.
Exemplo: print -> {0,0,1} - {0,1,0}  - {0,1,1} -  {1,0,0} - {1,0,1} - {1,1,0} - {1,1,1} - {0,0,2} - {0,2,0} - {2,0,0} - {0,1,2} - {0,2,1} - {1,2,1} - {...} - {2,2,2}
Alguém tem alguma ideia de como realizar isso? Conhece algum algoritmo que faça isso ou algo parecido? Pode ser em Java ou C#.
Comment: Bom, creio que uma grande gama de usuários poderiam responder a sua questão com um mínimo esforço possível. Inclusive eu mesmo poderia, porém, creio que essa questão possua um intuito bem didático, portanto, acredito fortemente que seria uma boa oportunidade de você iniciar no mundo do desenvolvimento, caso queira seguir essa área.
Comment: Bem vindo ao sopt. Primeiro decida uma linguagem, citar varias sem relação atrapalha um pouco a resposta.
Answer: Este problema é conhecido como permutação com repetição. Pesquisei outras fontes e em Português para explicar o problema melhor, mas infelizmente só encontrei lixo (desculpe o termo, mas estou me referindo a sites que copiam o conteúdo mal formatado e mal explicado de algum lugar).
Quanto ao algoritmo, geralmente este problema é resolvido mais facilmente usando um algoritmo recursivo. Cada chamada deve gerar as combinações para uma das posições do conjunto.
Por exemplo, dado o conjunto:
int[] conjunto = {0, 0, 0};

E a posição p que a chamada atual deve permutar, vamos definir uma rotina:
permutar(int[] conjunto, int p)
Lembrando que os elementos do conjunto tem os valores 0, 1 ou 2 e p se refere à posição do vetor, portanto, 0 <= p <= 2.
Cada vez que você for implementar recursividade, você precisa deixar claro qual é o critério para a recursão. Neste caso, poderia ser algo como:

Se p <= 2

Permutar p+1
Incrementar conjunto[p]
Repetir passos 1 e 2 enquanto conjunto[p] < 3
Limpar conjunto[p] (atribuir zero para futuras iterações)

Se p == 3

Imprime conjunto e retorna

Eu sei que é complicado, mas a ideia aqui é que o método permutar vai gerar as combinações para uma posição p e chamar a si mesmo recursivamente para gerar a posição p+1 e assim por diante até o limite do conjunto. Quando o limite for atingido, imprime o conteúdo atual e retorna.
O teste de mesa seria assim:
C0 C1 C2 p
0  0  0  0    permutar(conjunto, p)
0  0  0  1      |_ permutar(conjunto, p + 1)
0  0  0  2         |_ permutar(conjunto, p + 1)
0  0  0  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  0  1  2             |_ Incrementar `conjunto[p]`
0  0  1  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  0  2  2             |_ Incrementar `conjunto[p]`
0  0  2  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  0  0  2             |_ Limpar `conjunto[p]`
0  1  0  1         |_ Incrementar `conjunto[p]`
0  1  0  2         |_ permutar(conjunto, p + 1)
0  1  0  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  1  1  2             |_ Incrementar `conjunto[p]`
0  1  1  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  1  2  2             |_ Incrementar `conjunto[p]`
0  1  2  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  1  0  2             |_ Limpar `conjunto[p]`
0  2  0  1         |_ Incrementar `conjunto[p]`
0  2  0  2         |_ permutar(conjunto, p + 1)
0  2  0  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  2  1  2             |_ Incrementar `conjunto[p]`
0  2  1  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  2  2  2             |_ Incrementar `conjunto[p]`
0  2  2  3             |_ permutar(conjunto, p + 1)
         3                 |_ imprime
0  2  0  2             |_ Limpar `conjunto[p]`
0  0  0  1         |_ Limpar `conjunto[p]`
1  0  0  0     |_ Incrementar `conjunto[p]`
...

Após a última linha, essa tabela repete duas vezes agora com o primeiro valor 1 e depois com o valor 2.
Note que a versão mais externa do permutar manteve o valor 0 na primeira posição enquanto as chamadas recursivas foram alterando as demais posições, gerando todas as permutações com repetição possíveis.
O algoritmo em Java para isso fico assim:
public static void permutar(int[] conjunto, int p) {
    if (p == 3) {
        System.out.printf("{%d, %d, %d}%n", conjunto[0], conjunto[1], conjunto[2]);
    } else {
        do {
            permutar(conjunto, p + 1);
            conjunto[p]++;
        } while (conjunto[p] < 3);
        conjunto[p] = 0;
    }
}

E a chamada assim:
public static void main(String[] args) {
    int[] conjunto = {0, 0, 0};
    permutar(conjunto, 0);
}

Note que o algoritmo faz exatamente o que está descrito acima, exceto que os comandos podem estar em uma ordem um pouco diferente.
Embora o ideal fosse que cada desenvolvedor fosse capaz de resolver os problemas acima sem olhar código alheio, infelizmente isso não funciona, principalmente para quem está começando.
Se você está estudando algoritmos, minha sugestão é olhar implementações como acima e tentar entender como ela funciona. Depois, volte para seu editor e tente reimplementar como você lembra dela. Mesmo tendo olhado você ainda provavelmente vai perder algum detalhe.
Com o tempo, após exercitar bastante você pode ser capaz de criar a solução para este e outros tipos de problema sem consulta. Mas é uma utopia e nem é correto achar que todos os desenvolvedores podem simplesmente resolver esses desafios por si mesmos. Isso seria como dizer para os alunos de matemática que eles precisam derivar todas as fórmulas novamente ao invés de consultar um livro.
Comment: Obrigado, me ajudou bastante. Na verdade a minha tarefa é um pouco mais complexa do que essa, eu usei esse exemplo apenas para ter uma base por onde seguir, já que eu conheço pouco de algoritmo. No problema real os conjuntos não possuem números inteiros, mas sim objetos/instâncias de classes e associações de um modelo UML. Você poderia me indicar um livro que fale sobre algoritmos recursivos?
Comment: @Marcos infelizmente não tenho em mente nenhum material sobre o assunto, nem sei se existe um. Geralmente você precisa primeiro identificar o tipo do problema e então fica fácil encontrar um algoritmo para resolver, sendo que muitos têm a versão recursiva e a iterativa.


Controllers granularity in AngularJs

Question: Maybe it's silly question but I'm coming from Asp.net MVC world and normal manner is t create single controller per model ie.:
public class FooController : Controller
{
    public ActionResult Index()
    {...}
    public ActionResult Details(long id)
    {...}
    public ActionResult Edit(long id)
    {...}
}

On the other hand in AngularJs app I'm creating controller per view like:
app.controller('FooDetailsController', function ($scope, $routeParams, FooService) {
    $scope.foo = FooService.get({ id: $routeParams.id })
});

app.controller('FooListController', function ($scope, FooService) {
    $scope.foos = FooService.query();
});

...

How should I create controllers in AngularJs, what is best practice? This one looks really awkward for me.
Comment: I've normally seen it the way you describe with one controller per view, but you can use the same controller for multiple views if you want.
Comment: Yes, but how "single" controller should looks like? ie. on Details view I don't want to load collection of objects and other way on Collection view I don't want to get detail model.
Answer: Basically you're checking the $routeParam to see if there is a detail to look for, if not, looking for the list. http://plnkr.co/edit/jAK31F?p=preview
Comment: Yes,but I was hopping for something else than if-else madness :) Thanks though.
Comment: If it's one controller for 2 views, that's where you are. I think you'll find the one controller per view is not that awkward because most of the item-specific manipulation should be in the FooService anyway. That would look more like "Foo.detail", "Foo.list",  "Foo.getChildren", etc.
Comment: After "couple" of lines of code I need to strongly agree and I feel pretty comfortable with controller per view. Thanks
Comment: Where are you guys now ? I've been struggling to find the right balance. Is it your FIRST WAY TO GO to make 1 view = 1 controller when starting a project now ??? Do you guys systematicaly push it to a directive as its a natural way to tie controller and view in angular ???
Comment: If you're struggling with how to make a controller fit a use case, chances are the functionality doesn't belong in a controller.

Controllers granularity in AngularJs


delete spam with mailenable

Question: I am using mailenable on my VPS server.
How can I set up mailenable to checking incoming emails to my server if they are spam or not and if so, delete them?
Comment: First hit on google was MailEnable-supplied documentation on how to do this. I'm inclined to believe you haven't researched this first
Answer: The question you asked cannot possibly be responded to in a reasonable amount of time. But not to worry, the answers have already been written down by the thoughtful people over at MailEnable.
What you have on your hands is a complex system that can be used for very dangerous things if not managed correctly. Please, for your sake, for my sake... for everyone's sake, take the time to learn about everything surrounding the email infrastructure.
Questions like this are fine to ask, but you have to know when and where to ask them. When should this question have been asked? Before you got a VPS with MailEnable. Where should it have been asked? At your desk, rhetorically, just moments before you decided to spend two weeks reading everything you could find about the product, SMTP and email in general.
Go in peace (and get a subscription to Safari Books).
Answer: In most cases, when service providers/packages bundle MailEnable, it's with the "Standard" version which does not include any built-in content filtering capabilities. Its anti-spam functionality is limited to SMTP transactional white/black-list/IP processing, nothing that actually assesses email content itself. In order to perform spam filtering based on email content assessment, there are two possible approaches:

MailEnable does open up an API/hook for add-ons to assess content during its routing/processing - look for 3rd party solutions that take advantage of this
"Front-end" MailEnable with a anti-spam/virus relay/gateway solution that sits in front of MailEnable - maybe something like the GFI MailSecurity product

The MailEnable support forums have information on both approach, as the MailEnable folks have been extremely generous toward the folks who are making their livings off of MailEnable's free "Standard" product - they do not censor the free exchange of this type information across their forum.

delete spam with mailenable


Modelling overdispersed rate data using a negative-binomial distribution

Question: A quick overview of the analysis I'm wanting to do:
I am wanting to analyze the relationship between habitat factors and the capture of my research species over a network of traps, in order to be able to predict captures. The habitat factors include binary (e.g. presence/absence of a plant species within a plot around the trap), continuous (e.g. distance to the nearest body of water), and categorical variables (e.g. substrate type). I am wanting to analyze the captures as a rate $$rate=y/T=captures/100 days$$ to account for the fact that the traps have been operating for different amounts of time (due to maintenance, misfires, etc.).
Initially, the plan was to do a generalized linear model (GLM) based on the Poisson distribution since that seems to be recommended for rate data, and that my initial exploration of the response data seemed to work for it. For the model, I used $ln(T)$ as an offset parameter. Unfortunately, the resulting model is overdispersed (dispersion parameter was 1.76; residual deviance was 178.59 on 84 d.o.f.).
I understand that for count data, the negative binomial distribution can be used as an alternative for a GLM. However, I'm struggling to find information about whether it can also be applied to rate data. Which, brings me to my question:
Can I construct a negative binomial GLM for rate data in the same way as I would for Poisson (i.e. using the offset parameter), and if so, are there any special considerations I need to make when using it?
Comment: If I understand you correctly the answer is yes, you can use negative binomial regression for count data with an offset if scientifically needed.
Comment: A dispersion parameter of 1.76 doesn't seem so bad to me.
Answer:
Can I construct a negative binomial GLM for rate data in the same way as I would for Poisson (i.e. using the offset parameter), and if so, are there any special considerations I need to make when using it?

Yes, the important thing when using offset with Poisson regression for modeling rates, is that you need a log link function. Do the same with negative binomial, that is, a log link function, and you will be fine.
Some posts using offset with negative binomial regression:

Offset term in negative binomial regression
Offset for Negative Binomial Regression
When to use offset() in negative binomial/poisson GLMs in R


Getting websites to detect our mobile browser

Question: I've been asked to find out a sensible way to make the majority of popular websites detect our browser - which is functionally complete, but is running on rather constrained hardware - as a "mobile" browser.  The idea is that the heaviest popular websites seem to have mobile versions, which render much faster and fit better on the screen.
I've looked at the inverse question, which tells me that there isn't an obvious standard way of doing it - http://www.brainhandles.com/techno-thoughts/detecting-mobile-browsers is a case in point.  This is borne out by looking at a variety of User-Agent strings from popular mobile and desktop browsers.
So far the best idea we can come up with is to add "Mobile" to the string somewhere, since this is the main visible difference between Safari for iPad/iPhone and for Windows/Mac.  Does anyone have a better idea?
Answer: If uniquely identifying yourself is unimportant you could simply copy the important parts of the User Agent string for a popular mobile device... one which would be an obvious omission from any mobile detection script.
Answer:
So far the best idea we can come up with is to add "Mobile" to the string somewhere, since this is the main visible difference between Safari for iPad/iPhone and for Windows/Mac. Does anyone have a better idea?

This won't work. The vast majority of mobile phones don't include "Mobile" in their user agents. Here's a fairly huge list of mobile user agents. As you can see, there's no consistency. On top of that, new user agents are being added to the market on a weekly basis.
Comment: It's true that *looking* for "Mobile" would not, in itself, be a reliable method for *detecting* a mobile browser.  But I'm looking for a way to *mark* a browser as mobile.
Comment: It's going to vary, then, and there's not going to be a *reliable* method. Some sites are going to look for "Mobile" in conjunction with "iPhone", others are going to look for "MobileSafari", etc.


PHP setting of datetime field in CCK through Rules, How to stop continuous updates?

Question: Drupal 6. Using Rules and CCK
So, I have a content type that gets automatically generated with Rules. Several CCK fields get populated at creation time, including a datetime field. I want this field to get populated with the current date/time.
For some reason, the Global Token fields don't work, the interface throws an error when I try to use a token instead of a standard formatted date/time stamp.
So, I use advanced PHP code box to set the date/time value.
return array(
0 => array('value' => date("m/d/Y - H:i")),
);
This code works, but when I do it this way it seems that whenever I refresh the node page the date/time stamp in the field is updated to whatever the current time is. This is not the behavior I want. I want the date/time to get inserted at creation time, and have it remain unchanged for perpetuity.
Thoughts?
Comment: Just a thought...why not use `$node->created` instead of having a custom field with identical data?
Comment: Yeah fair point. I can't think of any good reason why a node creation rule would be run on a node view though, it doesn't really make sense.
Comment: I can't say for sure (I've never actually used them) but that sounds very much like what's happening
Comment: Hmm. Could it be a db issue? Has the column somehow been created as a MySQL timestamp (which updates when the record is accessed)?
Comment: No worries, glad you got it sorted
Comment: I'll look into it. The PHP aspect of Drupal is not my strong point.
Comment: Still, there are areas where I will need to set a date/time value for a custom field, above and beyond just the node creation date. I'm talking about additional metadata.
Comment: It seems to me (not expert) that the CCK field's value is set to the PHP code. So, whenever the Node page gets refreshed in the browser the field value gets retrieved (the PHP code) and the PHP code happens to dynamically get the timestamp of *right now*. Is that how Drupal node/CCK should operate with PHP input?
Comment: I just tested using your $node-> created suggestion, and it does the same thing.
Comment: I used this PHP code to assign the date value to the CCK field --- return array(0 => array('value' => $node->created)); --- it set the date properly, but when I refreshed the web page a minute later, the field updated and then had the current time.
Comment: Ah! I think that was it. There were several similar seeming Date/Time field types available to use, I was using one called "datetime". I switched to another one called "datestamp" and the datestamp field retained its original value after refreshing. I guess the "datetime" field is designed to always return the current datetime value of now(). Thanks for helping me work through this friend!
Answer: Thanks to Clive's help, I determined the problem was with the field type. There were several similar seeming Date/Time field types available to use, I was using one called "datetime". I switched to another one called "datestamp" and the datestamp field retained its original value after refreshing. I guess the "datetime" field is designed to always return the current datetime value of now(). Thanks for helping me work through this friend!