É possível armazenar um ArrayList em um Banco de Dados em Java?

Question: Tratando-se de Banco de Dados utilizando Java, é possível armazenar um objeto ArrayList em um Banco de Dados MySql? Se sim, como posso armazenar este tipo de dado? E como posso recuperá-lo da base de Dados? Se possível, poderiam mostrar um exemplo simples?
Comment: Eu quero armazenar um objeto ArrayList como um todo dentro do banco. Exemplo, tenho `ArrayList` `myArray` = `[2,43,1,4,11,78,43]`. Gostaria de poder armazenar o objeto `myArray`. E posteriormente, pode realizar um `select` e resgatar o objeto `myArray` por completo (`[2,43,1,4,11,78,43]`). O armazenamento do `myArray` deverá ficar em uma única tupla, não em 7 tuplas
Comment: Você quer armazenar os dados que contem no ArrayList?
Answer: A pergunta está feita de forma inapropriada. Dá para entender que você tem uma lista de valores e quer salvar isso num banco MySQL, mas esses são dois mundos completamente diferentes e você precisa entender como eles se relacionam.
A resposta correta depende de vários fatores.
Modelo de dados
O banco de dados não sabe nada de Java e nem precisa saber. Então, primeiramente, você deve definir um modelo de dados capaz de armazenar os valores.
O modelo mais "correto" seria armazenar os valores cada um em um registro, talvez numa tabela com um relacionamento 1:N (um para muitos) com uma tabela principal, no caso da lista estar vinculada a outro registro.
Pelo seu comentário, você quer armazenar os valores em um único campo. Enquanto isso é geralmente mais "confortável" do ponto de vista do programador, pode gerar diversos efeitos colaterais. O caso mais simples é caso você precisa pesquisar registros procurando por valores dessa lista, pois nesse caso você não pode simplesmente usar uma cláusula WHERE, mas precisará percorrer todos os registros.
Se isso não for um problema, provavelmente seu modelo vai conter um campo VARCHAR para armazenar a lista em formato texto. Nesse caso, a pergunta poderia ser simplesmente sobre como converter uma lista para texto e depois de novo para uma lista.
Armazenando uma lista num campo texto
Caso a ideia seja apenas armazenar números inteiros e você esteja usando Java 8, dá pra converter uma lista para uma String facilmente, assim:
List<Integer> lista = Arrays.asList(1, 2, 3);
String texto = lista.stream()
        .map(String::valueOf)
        .collect(Collectors.joining(","));

O código basicamente pega a lista de inteiros, converte para uma lista de strings e junta tudo separando por vírgula.
Depois, para pegar o texto e reconstruir a lista, pode fazer assim:
List<Integer> novaLista = Arrays.stream(texto.split(","))
        .map(Integer::valueOf)
        .collect(Collectors.toList());

O código quebra o texto separando por vírgulas, converte cada item para um Integer e finalmente recupera o resultado numa lista.
Você precisa tomar cuidado com o tamanho do campo, para não exceder o número de caracteres.
Além disso, esta abordagem não é segura para outros tipos de dados que podem conter vírgulas nos valores.
Armazenando a lista usando serialização
Outra abordagem seria serializar o ArrayList usando a API de serialização do Java, armazenando o objeto como uma sequência de bytes, que poderia ser desserializada posteriormente.
É apenas uma ideia e existem outras respostas aqui no SO ensinando como fazer isso.
Esta abordagem é flexível pois permite armazenar qualquer tipo de lista, desde que os objetos dentro dela sejam também serializáveis.
O problema é que uma sequência de bytes no banco de dados não é algo muito agradável de lidar.
Armazenando a lista usando JSON
Outra alternativa mais flexível é converter o ArrayList em um objeto JSON e armazenar isso no banco de dados. JSON é um formato de representação de objeto em texto, por exemplo:
{ itens = [ 1, 2, 3 ] }

Esta abordagem é a mais flexível porque permite que qualquer tipo de dado seja armazenado.
A desvantagem é você ter que usar uma API mais complexa para converter ente ArrayList e JSON. Existem várias respostas aqui no SO que ensinam como gerar JSON ou vice-versa.
Answer: Caso você esteja trabalhando com o JPA, acredito que a própria documentação da Oracle aborde o assunto de forma bastante clara.
No link acima, é exemplificado de forma bastante simplória como irá ser manipulado a anotação que faz o "trabalho pesado".


Carbon show time different days hours minutes second

Question: i try to make time difference with carbon
$dt = Carbon::parse('2018-07-15 00:00:00');
$now = Carbon::now('Asia/Dubai'); //set current time
$seconds = $now->diffInSeconds( $dt ); //difference turn into second
$days = $dt->diffInDays($dt->copy()->addSeconds($seconds));
$hours = $dt->diffInHours($dt->copy()->addSeconds($seconds)->subDays($days));
$minutes = $dt->diffInMinutes($dt->copy()->addSeconds($seconds)->subHours($hours));

$days result are 12 (its right).
$hours result are 8 (seems not right).
$minutes  result are 17299 (clearly wrong).
how to get the result for example 12 day 5 hours 45 minutes
Answer: Actually functions like diffInSeconds give total difference in seconds that's why the number is so large,to get the minutes for the time difference right you can use -:
$minutes = ($now->minute - $dt->minute);


shell script - while loop does not terminate

Question: I have a while loop in shell and it does not terminate even though it has a break condition, this piece of code run well in other languages but i can't figure out how while loop runs in shell script.
Here is my code:
NAME=""
while [[ $NAME != "q"  || $NAME != "Q" ]]
do
    read NAME
    //do something

done

Comment: NAME is always not "q" or not "Q". For example, "q" is not "Q"; "Q" is not "q"; and "x" is neither "q" nor "Q". You want `&&`, not `||`. (This would behave identically in other languages, the error is purely logical.)
Comment: @Amadan yes thank  you very much, code in shell looks different from others, i get it now.
Answer: $NAME != "q" || $NAME != "Q"

Unless NAME can exist in a strange " Schrödinger's cat" sort of state where it can be both Q and q at the same time, this expression will always be true.
Think about it:

If NAME is neither q nor Q, both sub-expressions will be true so the full expression will be true.
If it's Q, then the first sub-expressions will be true, leading to the full expression being true as well.
If it's q, then the second sub-expressions will be true, leading to the full expression being true as well.

What you probably need is:
$NAME != "q" && $NAME != "Q"

Of course, if it's bash that you're using, it provides a way to uppercase and lowercase a string to make these comparisons easier:
while [[ "${NAME^^}" != "Q" ]] ; # upper-case variant
while [[ "${NAME,,}" != "q" ]] ; # lower-case variant

Comment: Or you could use `[[ $NAME != [Qq] ]]`


Is the following set connected given that the union and intersection is connected

Question: Suppose $U_1, U_2$ are open sets in a space $X$. Suppose $U_1 \cap U_2$ and $U_1 \cup U_2$ are connected. Can we conclude that $U_1$ must be connected??
I am trying to find a counterexample, but I failed. PErhaps it is true? Can someone help me find a counter example? thanks
Answer: Suppose $A$ and $B$ form a separation of $U_1$, i.e., $A$ and $B$ are disjoint nonempty open sets such that $A\cup B = U_1$. Because $U_1 \cap U_2$ is a connected subset of $U_1$, it must be entirely contained in either $A$ or $B$ (else we would get a separation of $U_1 \cap U_2$ by intersecting $A$ and $B$ with $U_1 \cap U_2$); WLOG let $U_1 \cap U_2 \subset A$. Then, $A\cup U_2$ and $B$ forms a separation of $U_1 \cup U_2$, a contradiction.
Answer: Suppose $U_1$ is disconnected. Then $U_1 = V \cup W$ where $V$ and $W$ are non-empty, disjoint, open subsets of $X$. Since $U_1 \cap U_2$ is connected, one of $V \cap U_2$ and $W \cap U_2$ must be empty. Suppose without loss of generality that $V \cap U_2 = \varnothing$. We have $U_1 \cup U_2 = V \cup W \cup U_2$. Note that $V$ and $W \cup U_2$ are non-empty and open. Furthermore, $V \cap (W \cup U_2) = \varnothing$. It follows that $U_1 \cup U_2$ is disconnected.
Answer: We assume that both $U_1$ and $U_2$ are nonempty (otherwise the result is trivial). If $U_1=A\cup B$, being $A$ and $B$ disjoint open sets, then $U_1\cap U_2$ is the disjoint union of the open sets $A\cap U_2$ and $B\cap U_2$, so by connectedness one of the sets is empty, say $A\cap U_2=\emptyset$. Therefore $U_1\cup U_2$ is the disjoint union of the open disjoint sets $A$ and $B\cup U_2$, so again by connectedness one of these sets is empty, namely $A$ (because $U_2\ne\emptyset$ by assumption). This proves that $U_1$ is connected.
Answer: I'm assuming all of the above are non-empty.
If we can't separate $U_1 \cap U_2$ into disjoint sets, how could we possible separate $U_1$ into disjoint sets?
That is, suppose for contradiction that $U_1 = S_1 \sqcup S_2$ (which is to say $U_1 = S_1 \cup S_2$ with $S_1 \cap S_2 = \emptyset$). Then we have
$$U_1 \cap U_2 = (S_1 \sqcup S_2) \cap U_2 = (S_1 \cap U_2) \sqcup (S_2 \cap U_2).$$ But then we've just written $U_1 \cap U_2$ as a disjoint union, contradicting our assumption that $U_1 \cap U_2$ is connected.
Comment: How do you know that $S_1 \cap U_2$ and $S_2 \cap U_2$ are non-empty?
Comment: @AymanHourieh That's a very good point. If this weren't the case, then it would follow that $U_1 \cup U_2$ wasn't connected; if (say) $S_1 \cap U_2$ was empty, then $U_1 \cup U_2$ would be the disjoint union $S_1 \sqcup (U_2 \cup S_2)$.


Import a local pdf file in vue project

Question: I am trying to import a local pdf file in my vue project (bootstrapped using vue-cli@latest, using default config) but vue tries to parse it and gives this error:
./src/assets/myFile.pdf 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)

I just want to make the file available for download from a button click so I don't need to parse it but I need it to go through webpack so that the filename will get hashes, hence I want to avoid putting it in public folder and use env.BASE_URL.
Here the my .vue file for the component.
<template>

      Download as pdf

</template>

I found similar questions on SO, but none of the answers solve the problem. I only solution I found was on vue docs to use public folder but I don't want that because I want hashes in filename. I am new to vue, I come from React, and in React you can simply import a pdf, word etc. file in js and use it in href of a and users can view/download it.
Answer: You don't have to import it. Just create a  tag with correct URL to your pdf file.
You can check if the path is correct downloading directly in the browser before.
Edit:
 In case you are facing the same problem and you are using webpack, you have to make sure you are using a loader for the pdf extension. In this case, the user was using vue-cli, and the way to change webpack config is on vue.config.js. I created a gist to show how add it.
Comment: Sorry, I almost forgot, on vue-cli or any other project using webpack, to load a pdf you have to add pdf loader rule. Otherwise, webpack won't understand that extension of file. I created a gist to show you how.  https://gist.github.com/aquilesb/f65bda1a8f96e82f73e6f95cedb09625. In this example I am presuming you have your pdfs on the folder: `src/assets/pdf/`
Comment: It is simple like create an image. If you are using vue-cli, it will be something like
`download pdf`. On vue-cli you can access your `assets` folder directly by `/assets/` in your application. You can try opening in the browser `http://localhost:{{PORT}}/assets/my.pdf`
Comment: works like a charm thanks. It would be nice if you could put the vue config code from your gist in the actual answer so that future readers don't have to go to an external website.
Comment: How do I know the link to my pdf? `http://localhost:8080/assets` is a blank html page for me, rather than a folder. I tried importing it in `router.js` but I get this error message: "You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders" even though I have put the gist you created in my `vue.config.js`
Comment: Thanks, I had a `babel.config.js` file. In order to make it work I recomend to create the `vue.config.js` file at the root of the project folder and serve it again.

Import a local pdf file in vue project


use several css class for transition

Question: Is it possible to use several css classes for transition ?
If I have this CSS :
.fade {
  opacity: 0;
  transition: opacity .5s linear;
}
.left {
  left: 10px;
  transition: left .3s ease;
}

Then I can't use the 2 classes on 1 html element, one transition property will override the other. I know I could create a class .fade-left which would do both (see this question), but I'd like to keep this modularity.
Is there a way to add both transitions ?
Comment: I'm just covering my ass here in case the OP comes back and says "but that question is about having a single class, I want to have multiple classes!"
Comment: Having multiple classes that set the same property is no different than having a single class set the same property twice.
Comment: @cimmanon Won't that override the transition?
Comment: @PraveenKumar That's the point I'm trying to make.  If I write `.foo { color: red } .bar { color: green }`, it is the same as writing `.foo.bar  { color: red; color: green }`.
Comment: @cimmanon So there'll be only one colour. Green. Not both. But in this case, we need both the transitions.
Comment: @cimmanon `transition` can be an array, while `color` is a single value. Do you understand where I come to?
Comment: If I wanted to have a single class .fade-left, I could apply both transitions. I thought I could be able to apply 2 independent classes, not creating one especially for this case (see comment on @PraveenKumar answer)
Comment: @ElodiePrevot What part of anything I said do you not understand?
Comment: @cimmanon I could ask you the same question, but I can imagine it's my fault if I'm not clear.
Maybe there is a way to define a transition only for one property with sass ? Something like "transition-opacity: .2s linear" ? So that the 2 animations don't override themselves.
Comment: Sass doesn't have magic specification defying powers, it can only compile to CSS.  I don't know how you can sit there and not "get" that trying to use 2 classes to set the transition property is no different from using 2 classes to set any other property in existance.
Comment: Well, you can set the font-family AND the font-size because they are both about font but they don't contradict. It seems to me that a transition on opacity and a transition on left are not contradicting each other. I don't know all css properties, there could be some property like opacity-transition or so. Or some other way than defining a css transition property. Or maybe by adding a transition in the end of the array instead of replacing the hole array. And if you can't think of another way than to create a .fade-left class, then it's not a big deal to say it without insulting me
Answer: You need to use this way for using multiple transitions:
transition: opacity .5s linear, left .3s ease;

Expanded way:
-webkit-transition: opacity .5s linear, left .3s ease;
   -moz-transition: opacity .5s linear, left .3s ease;
    -ms-transition: opacity .5s linear, left .3s ease;
        transition: opacity .5s linear, left .3s ease;

Since the other class doesn't change the left property, you don't need to worry. In simple ways, you can use all.
Comment: Thank you for your help, but this is what I would write if I wanted to create a .fade-left class, which I don't. When I want a button to be big and red, I can add 2 classes .big and .red to it, no need to create a class .big-red because they change 2 independent CSS properties. A transition on left and a transition on opacity are 2 independent actions too, I thought there was a way to keep these separate classes.
Comment: @ElodiePrevot I don't think that's possible.

use several css class for transition


Problem when connecting to FTP server through android?

Question: I've an UnknownHostException when i used this method for uploading files from ddms:
try {
SimpleFTP ftp = new SimpleFTP();

// Connect to an FTP server on port 21.
ftp.connect("ftp://*******", 21, "*****", "*****");

// Set binary mode.
ftp.bin();

// Change to a new working directory on the FTP server.
ftp.cwd("web");

// Upload some files.
ftp.stor(new File("data/data/com.android/file/contacts"));

// Quit from the FTP server.
ftp.disconnect();
}
catch (IOException e) {
// Jibble.
}

What is the problem for this method? Anyone clarify me.
Comment: You should include the logcat
Comment: @David Olsson : how to include the logcat. Please tell me the method.
Comment: Use adb logcat in the terminal. Or logcat view in eclipse.
Answer: There are two major problems with using FTP on the android emulator:

The emulator takes hold of a specific port on the host machine (between 5554 and 5584) to access the internet.
See http://developer.android.com/guide/developing/tools/emulator.html

FTP communicates on two ports.  The initial port (the one the emulator is using) and a secondary data communication port (usually defined by the FTP client and server).
See http://www.troubleshootingnetworks.com/ftpinfo.html for information on how FTP works.

This means that the initial communication with the FTP server works the way it is intended, but once you are attempting to pass data to / from the server the emulator cannot communicate with the port the FTP server requests because your computer doesn't know what to do with the traffic on that port.  See the link above to get a better grasp on FTP communications.
If you want to test FTP on Android you will need to have a device with its own internet connection.
Comment: perfect ! ! ! that's why im not uploading my images with simulator. Now i understand. Thanks a lot for this answer!
Comment: is this answer still valid with the new Android emulator versions? The links mentioned are no longer existing. Kindly update.
Answer: Cursory look at SimpleFTP example suggests that you need to use host name without ftp:// prefix. Also make sure that you include INTERNET permission in manifest.
Comment: yes, you're right. if we declare the prefix in our ftp address. then, it'll no longer work. Thank you.


How do I make this postscript code work again

Question: I have the following postscript code snippet. What it does is read a PDF filename from an array ConvertItem get the page information for this through getfirstpagesize and store the values that are coming out of this back in the array ConvertItem (8 - 9). This used to work perfectly until ghostscript 9.27. In this version the pdfdict is deprecated.
/ConvertItems
[
     [ (...) (...) <...> (...) (...) () 0 0 0 0 0 0 ]
] def

/getfirstpagesize
{
    1 1 1
    {
        pdfgetpage /MediaBox pget dup pop
        {
            /MediaBox exch def
            /PageWidth MediaBox 2 get def
            /PageHeight MediaBox 3 get def
        } if

        (First page - width [) print PageWidth 10 string cvs print
        (], height [) print PageHeight 10 string cvs print
        (]\n) print
    } for
} bind def

------- snip -------
ConvertItem 0 get (r) file"
pdfdict begin"
pdfopen begin"
getfirstpagesize"
ConvertItem 8 PageWidth put"
ConvertItem 9 PageHeight put"
currentdict pdfclose"
end % temporary dict"
end % pdfdict"

------- snip -------
I tried to solve it by using pdfrunbegin and pdfrunend like this
------- snip -------
ConvertItem 0 get (r) file
runpdfbegin
getfirstpagesize
ConvertItem 8 PageWidth put
ConvertItem 9 PageHeight put
runpdfend
(*First page - width [) print ConvertItem 8 get 10 string cvs print
(], height [) print ConvertItem 9 get 10 string cvs print
(]\n) print

------- snip -------
But when I try to print the ConvertItem 8 and 9 value then these are always 0 so it seems like runpdfend is clearing the memory that is shared with these values. So my question is if there is some way around this?
Comment: ConvertItem is an element from an array.

/ConvertItems
[
     [ (...) (...) <...> (...) (...) () 0 0 0 0 0 0 ]
] def

I can't move the print section. I know it will work if I do this but the print section was just to show that the values get reset. I need the values that I store back in the ConvertItem object some where else in my postscript code.... thats the problem
Answer: Well it seems that runpdfbegin does a save of the interpreter memory and restores that after runpdfend is executed. This is the reason why the value gets "reset" to it's devault value. To work around this problem the PageWidth and PageHeight variables have to be stored in the globaldict, this doesn't get reset when the memory of the interpreter is restored.
/getfirstpagesize
{
    1 1 1
    {
        pdfgetpage dup
        /MediaBox pget
        {
            /MediaBox exch def
            true setglobal
            globaldict begin
            /PageWidth MediaBox 2 get def
            /PageHeight MediaBox 3 get def
            false setglobal
            end
        } if

        pop

        (First page - width [) print PageWidth 10 string cvs print
        (], height [) print PageHeight 10 string cvs print
        (]\n) print
    } for
} bind def


Best practice for getting field values of entity chains

Question: I really would like to make this if/else field value check just a little bit more sexy.
What's Best Practice for dealing with nested functions chains to get entity field values (check if they exist / check if they are not empty)
 if($entity->hasField('field_intro_teaser_image_media_')){
      $media_field = $entity->field_intro_teaser_image_media_;

      if($media_field->entity){
        $media_field_entity = $media_field->entity;

        if($media_field_entity->hasField('field_media_image')){
          $media_field_entity_image = $media_field_entity->field_media_image;

          if($media_field_entity_image){
            $media_field_entity_image_entity = $media_field_entity_image->entity;

            if($media_field_entity_image_entity){
              $media_field_entity_image_entity_uri = $media_field_entity_image->entity->getFileUri();

              if($media_field_entity_image_entity_uri){

                // ... yay we got the damn uri!

              }

            }
          }
        }
      }
    }

The one liner would be:
$media_field = $entity->field_intro_teaser_image_media_->entity->field_media_image->entity->getFileUri();

Comment: The one liner would fail if anything in the chain does not have the field or relational entity, so you'd want to wrap it in a try catch statement.
Comment: Not tried it on entity fields yet, but [Null-safe operator](https://php.watch/versions/8.0/null-safe-operator)?
Comment: Or use Twig, see https://drupal.stackexchange.com/questions/271579/get-video-url-from-media-entity-reference-field. Twig is null safe by default, if you don't set strict_variables.
Answer: When you access fields by magic methods, you don't actually have to check for their existence by using hasField() method.:
$entity->field_that_does_not_exist->entity; // NULL.
$entity->get('field_that_does_not_exist'); // Exception.
$entity->get('field_that_does_not_exist')->entity; // Exception.

Using $entity->field_intro_teaser_image_media_->entity is sufficient in this case, if the field does not exist, the expression will be simply NULL, without any errors.
But of course you still have to check if referenced ->entity is not NULL. PHP 8.0 Null-safe operator may be a way to do it, as suggested by Clive.
$media_field = $entity
  ->field_intro_teaser_image_media_
  ->entity
  ?->field_media_image
  ->entity
  ?->getFileUri();

In case of using Null-safe operator, it will be probably faster to access entity fields by ->get(), to avoid additional logic.
$media_field = $entity
  ->get('field_intro_teaser_image_media_')
  ?->entity
  ?->get('field_media_image')
  ?->entity
  ?->getFileUri();

Comment: thank you . Love the Null Safe Operator. Never used it so far.
Answer: Assuming you've built the chain correctly, you can do it as follows:
$media_field = NULL;
try {
  $media_field = $entity->field_intro_teaser_image_media_->entity->field_media_image->entity->getFileUri();
}
catch (\Exception $e) {
  watchdog_exception('Media retrieval');
}

if (!is_null($media_field)) {
  // Do stuff.
}

Comment: Thanks. I Did it this way.


Probablity calculation for at-least clause case

Question: I saw the question:  What is the probability of 1 out of 5 randomly selected people agree with the statement that men having more right for job compare to women?
P(agree) = 0.36
P(disagree) = 1 - .36
p(at least 1 agree) = 1 - 0.74^4 (because 1 is already agree with sentence)

Is that correct?
Comment: Did you find the solution for your problem?
Answer: Assuming your sample is being done with replacement, the only experiment that wouldn't result in success is when all 5 sampled people disagree. Thus
$$P(at\space least\space 1\space  agree) = 1 - 0.64^5 $$
Which yields $0.8926$ . First notice that it is $0.64$ (instead of 0.74) and the power is $5$ (not $4$).
Just to be sure, this little python snippet confirms this result for you:
import numpy as np
import random

def test(pop, samp_size):
  return 1 if (1.0 in random.sample(pop, samp_size)) else 0

if __name__== '__main__':
  NUM_OF_EXPERIMENTS = 100000;
  samp_size = 5
  pop = np.concatenate((np.zeros(64000000), np.ones(36000000)))
  succes_count = 0.
  for i in range(0, NUM_OF_EXPERIMENTS):
    succes_count += test(pop, samp_size)

  print succes_count / NUM_OF_EXPERIMENTS


rotate a string n characters to the left, except the special characters

Question: Hi I need help rotating a string to the left n amount of times, I have done so: btw Strings is a list of strings:
 finaltext = ""
 for i in strings:
    first = i[0 : n]
    second = i[n :]
    i = second + first
    finaltext += i

However,  i'm not sure how to do this so that in a given string, say: "The intern", the space or any special characters would not move.
s1 = "The intern"

Right now my output is:
    ternThe in
output I want:
 ern eThein
any ideas? I currently created a function that indicates when a special character and its index in a string, I used that in a for loop to know that the current character is a special character, but when it comes to rotation how would i avoid that character
Comment: I'm asking a question because I'm stuck, I've already tried the logic and having trouble implementing that. All i'm asking is for some direction not the coded answer...
Comment: Create a string with only the characters to be rotated, and a data-structure to hold those that cannot be rotated with the given positions. Rotate and insert.
Comment: why is the output `ern eTheint`? before the second work contain 6 characters, after your function it changes to 7 ?
Comment: Your question is asking for help with problem analysis and solution design, rather than a SO-type question.  You've already identified what you need to do; how would you carry that out with pencil & paper?
Comment: For your given example, does rotation `n=7`? Also what is the definition of a "special" character?
Comment: @Mr_U4913 sorry that was a typo. Fixed now, rotation was 6 to the left
Comment: special character is anything that isn't in the alphabet, like punctuation
Comment: "Some direction" is not a specific programming question; it's out of scope for Stack Overflow.  Again, please refer to the link in my first comment.
Comment: @DanielMesejo thanks, I figured it out!
Comment: @Prune In the time you typed up the 3rd comment someone already gave me the answer I was looking for LOL. I'll keep in mind what you said about asking specific questions, but sometimes someone needs a little clarity. That's all :)
Comment: Happy to help, but in you expected output `ern eThein`, it's not obvious to me why the space has shifted and where the extra `e` next to `T` came from.
Comment: @pylang, Hi, I think it's because the space is seen as a character and thus when I rotated it, everything got shifted to the left. I figured out how to do it though, so it's all good!
Answer: An intriguing question.  How to rotate a string while ignoring specific characters?
Here we remove, rotate, reinsert characters.
Given
import collections as ct

def index(s):
    """Return a reversed dict of (char, [index, ...]) pairs."""
    dd = ct.defaultdict(list)
    for i, x in enumerate(s):
        dd[x].append(i)
    return dd

s1 = "The intern"
s2 = "Hello world!"

Code
def rotate(s, n=0, ignore=""):
    """Return string of rotated items, save ignored chars."""
    s0 = s[:]

    # Remove ignored chars
    for ig in ignore:
        s = s.replace(ig, "")

    # Rotate remaining string, eqiv. to `res = s[-n:] + s[:-n]`
    tail = s[-n:]
    head = ""

    for c in s[:-n]:
        head += c
    res = tail + head

    # Reinsert ignored chars
    if ignore:
        res = list(res)
        lookup = index(s0)
        for ig in ignore:
            for idx in lookup[ig]:
                res.insert(idx, ig)
        res = "".join(res)
    return res

Tests
assert rotate(s1, n=0, ignore="") == "The intern"
assert rotate(s1, n=1, ignore="") == "nThe inter"
assert rotate(s1, n=1, ignore=" ") == "nTh einter"
assert rotate(s1, n=3, ignore=" ") == "ern Theint"

assert rotate(s2, n=12, ignore="") == "Hello world!"
assert rotate(s2, n=1, ignore="") == "!Hello world"
assert rotate(s2, n=1, ignore="H !") == "Hdell oworl!"
assert rotate(s2, n=1, ignore="!") == "dHello worl!"


Evaluating $\int_0^\infty\frac{\ln(1+x^2)}{e^{\pi x}-1}dx$

Question: I want to evaluate

$$\int_0^\infty\frac{\ln(1+x^2)}{e^{\pi x}-1}\,\mathrm dx$$

I tried to let $$I(a)=\int_0^\infty\frac{\ln(1+a^2x^2)}{e^{\pi x}-1}\,\mathrm dx,$$ and then
$$
\begin{align}
I'(a)&=\int_0^\infty\frac{2ax^2}{(1+a^2x^2)(e^{\pi x}-1)}\,\mathrm dx\\[25pt]
&=2a\int_0^\infty\mathscr L^{-1}\Big(\frac{x}{1+a^2x^2}\Big)\mathscr L\Big(\frac{x}{e^{\pi x}-1}\Big)\,\mathrm dx\\[25pt]
&=-2a^{-2}\pi^{-2}\int_0^\infty \sin\frac xa \psi^{(1)}\left(1+\frac x\pi\right)\,\mathrm dx
\end{align}
$$
I can't go further.
Comment: This is a similar one: https://math.stackexchange.com/questions/650213/integrating-int-0-infty-frac-log-1z2ez-1dz-using-residue-calculus
Comment: I found some post containing similar integrals with $e^{\pi x}+1$, but them do not help a lot.
Comment: This should be reasonably related to $\log\Gamma\left(\frac{\pm i}{2\pi}\right)$ through Binet's formula.
Comment: Via integration by parts and $\mathcal{L},\mathcal{L}^{-1}$ the origin integral can be written as $$ 2\int_{0}^{+\infty}\frac{\cos(\pi t)}{\pi t}\left(\gamma+\psi(1+t)\right)\,dt = \frac{2}{\pi}\int_{0}^{+\infty}\sum_{n\geq 1}\frac{\cos(n t)}{n}\cdot\frac{dt}{t+\pi}=-\frac{2}{\pi}\int_{0}^{+\infty}\log\left|2\sin\frac{t}{2}\right|\frac{dt}{t+\pi}$$
Comment: I read the similar one with $e^x-1$. Probably turning $e^x$ into $e^{\pi x}$ can make it has a nice closed-form?
Answer: This is not a full solution, but a reduction to an integral that looks a bit simpler (though I am not sure whether it admits a closed-form expression).
We integrate by parts and use the series expansion of the logarithm to obtain
$$I \equiv \int \limits_0^\infty \frac{\ln(1+x^2)}{\mathrm{e}^{\pi x}-1} \, \mathrm{d} x = \frac{2}{\pi} \int \limits_0^\infty \frac{-x \ln(1-\mathrm{e}^{-\pi x})}{1+x^2} \, \mathrm{d} x = \frac{2}{\pi} \sum \limits_{n=1}^\infty \frac{1}{n} \int \limits_0^\infty \frac{x \mathrm{e}^{-n \pi x}}{1+x^2} \, \mathrm{d} x \, .$$
Now we employ the Laplace transform identity
$$ \mathcal{L} \left(t \mapsto \frac{t}{1+t^2}\right) (p) = \sin(p) \left[\frac{\pi}{2} - \operatorname{Si}(p)\right] - \cos(p) \operatorname{Ci}(p) $$
and the representation
$$ \operatorname{Ci} (p) = \gamma + \ln(p) - \operatorname{Cin}(p)$$
of the cosine integral ($\gamma$ is the Euler-Mascheroni constant and the trigonometric integrals are defined here) to write
$$ I = \frac{2}{\pi} \sum \limits_{n=1}^\infty \frac{(-1)^{n-1}}{n} \operatorname{Ci}(n \pi) = \frac{2}{\pi} \left[[\gamma + \ln(\pi)] \ln(2) - \eta'(1) - \sum \limits_{n=1}^\infty \frac{(-1)^{n-1}}{n} \operatorname{Cin}(n \pi)\right] \, .$$
$\eta$ is the Dirichlet eta function and since $\eta'(1) = \gamma \ln(2) - \ln^2 (2) /2$ , we find
$$ I = \frac{\ln(2) \ln(2 \pi^2)}{\pi} - J \, ,$$
where
$$ J \equiv \frac{2}{\pi}\sum \limits_{n=1}^\infty \frac{(-1)^{n-1}}{n} \operatorname{Cin}(n \pi) = \frac{2}{\pi} \int \limits_0^{\pi/2} \frac{1}{t} \sum \limits_{n=1}^\infty \frac{(-1)^{n-1}}{n} [1-\cos(2 n t)] \, \mathrm{d} t \, . $$
Finally, the Fourier series  of $\ln(\cos)$ yields
$$ J = \frac{2}{\pi} \int \limits_0^{\pi/2} \frac{- \ln(\cos(t))}{t} \, \mathrm{d} t = \int \limits_0^1 - \ln(s) \tan \left(\frac{\pi}{2}s\right) \, \mathrm{d} s \approx 0.59921 \, .$$
I have not found a nice expression for $J$ yet, but maybe someone else knows how to calculate this integral.
Power series expansions of the integrands lead to the following representations in terms of the Dirichlet lambda function and the dilogarithm:
$$ J = \frac{1}{\pi} \sum \limits_{n=1}^\infty \frac{\lambda(2n)}{n^2} = \frac{1}{\pi} \sum \limits_{k=0}^\infty \operatorname{Li}_2 [(2k+1)^{-2}] \, . $$
Answer: The integral can be transformed to
$$I=\int_0^\infty \frac{\log{(1+x^2)}}{e^{\pi x} -1}dx = \frac{4}{\pi}\int_0^1 \frac{du}{1-u^2} \big(h(1/u)-h(u)\big)$$
where
$$ h(u) = u\Big( \log{\Gamma(u/2)}-\frac{1}{2}(u-1)\log{(u/2)} + u/2 - \frac{1}{2}\log{(2\pi)} \Big) $$
An integration by parts enables one to pull out known constants involving $\pi$ and $\log{2},$  but the remaining part is ugly, with integrands involving
$\log(1-u^2)\psi(u/2)$, for instance.  I derived the formula from
$$\int_0^\infty \frac{ \tan^{-1}(x\,u)}{u(1-u^2)}du = \frac{\pi}{4} \log(1+x^2). $$
The integral is a principal value integral where care must be taken at $u=1.$  Insert into definition, switch $\int$, and use the Binet formula for the log of the Gamma function in the inner integral. (The equation within the large parenthesis of the penultimate formula comes from the  Binet formula.)   The form presented comes from splitting the remaining integral at $u=1$ and making the transformation $u \to 1/u$ in the part that had previously extended to $\infty.$  It has been checked numerically with the original definition.


How to add a view to UINavigationController that is visible above any child view

Question: I'm creating an app which shows data in a UITableView. This data also contains location information, so I want to add a MapView to display this data. When a user taps a UITableViewCel, I need to segue to another UITableView to show the detailed data. I still want to show the MapView with the same data above this detailed data, and I still want the NavigationBar to update, i.e. the back buttons returns the user to the first UITableView.
In image form to make it easier to understand

I was thinking of making a subclass of a UINavigationController and putting the container underneath the MapView, but I don't really know where to go from there.
Comment: did you put the mapView and the tableView under UIViewController?
Comment: you can do this with `self.title` in viewdidload method and pass the data to your second vc with prepareforsegue method.
Comment: Yes, but I want to be able to segue from the first TableView to the second while still using the same MapView and being able to use the NavigationBar to go back to the first TableView.
Comment: How do you show the data in first view, do you use any model or just an array with strings.
Comment: It's an array, which is retrieved from the model.
Comment: if you have put mapview in one viewcontroller through storyboard then in another viewcontroller you need to put another mapview. the flow you want is not a good idea. so place two mapview in different view.
Comment: But the map views show the same information, which would cause an unnecessary instance of the MapView to be created.
Answer: Have VC0 embedded in a navigation controller or subclass UINavigationController.  Create an Info ViewController and push that on the navigation controller.
In your particular case you'll want to use the didSelectRowAt method from UITableViewDelegate.
<code>func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  // use indexPath.section and indexPath.row to use your data
  let infoVC = yourInfoViewController()
  infoVC.title = "Info Cell Name"
  navigationController?.pushViewController(infoVC, animated: true)
}
</code>

Edit after first comment:
I'm not sure what the best way of doing this is but one way you can do it is:

Modify your table view based on what is being shown, ie: first set of data or cell detail data
Update the navigation bar's title and left bar buttons per set of data you're displaying

One of those buttons will only appear in the cell detail data set and it sets your switch back to displaying the first set of data
Comment: But I want to keep using the same map view, so I don't have to instantiate a new one.


Solution of differential equation by taylor series

Question: I have the following differential equation $\frac{dy}{dx} = 3x+2y+xy$ with $y(0)=-1$ (I don't have the exact solution to this). I want to find the approximate value of $y(0.1)$ using a 6th order Taylor Series. I used Runge-Kutta 4 and it says that $y(0.1) \approx -1.2114$.
What I come up using the Taylor Series method is $\approx -1.23$.
I thought Taylor Series would be more accurate, or maybe I did something wrong in my Taylor Series solution?
Comment: Where is your Taylor Series solution that you think is wrong?
Comment: My expansion of the differential equation into a Taylor Series was incorrect.
Answer: Note that the factorization below
suggests a the simpler equation,
i.e. translating $(-2,-3)$ to the origin
$$ y'=(x+2)(y+3)-6 $$
and the initial condition $(0,-1)$ to $(2,2)$.
This, however, leaves $(y+3)'=y'$ unchanged:
$$
\eqalign{
y' &= xy - 6 \\
y' - xy &= - 6 \\
(y' - xy) \, e^{-x^2/2} &= - 6\,e^{-x^2/2} \\
(y \, e^{-x^2/2})' &= - 6\,e^{-x^2/2} \\
y \, e^{-x^2/2} &= - 6 \int e^{-x^2/2} dx \\
y \, e^{-x^2/2} &= c-6 \sqrt{\frac\pi2}\,
\text{erf}\frac{x}{\sqrt{2}}
\qquad \implies \quad c=y_0 \\
y &= e^{x^2/2} \left( y_0 - 3 \sqrt{2\pi}\,
\text{erf}\frac{x}{\sqrt{2}}\right) .
}
$$
Translating back, we get
$$
y = e^{(x+2)^2/2} \left( c - 3 \sqrt{2\pi}\,
\text{erf}\frac{x+2}{\sqrt{2}}\right)-3
$$
for the same constant $c$ which, however,
is no longer equal to $y_0$, but rather to
$$
\eqalign{
c &= (y_0+3) e^{-\frac12(x_0+2)^2}
   + 3\sqrt{2\pi}\,\text{erf}\frac{x_0+2}{\sqrt{2}}
\\&=2e^{-2}+3\sqrt{2\pi}\,\text{erf}\sqrt{2}
\approx 7.44839864640532
}
$$
which would give
$$
\eqalign{
y(0.1)
&= e^{1/200} \left( c - 3 \sqrt{2\pi}\,
   \text{erf}\frac{2.1}{\sqrt{2}}\right)-3
\\&\approx -1.21143171766941
\,.
}
$$
If we had just let $u=x+2$ and $v=y+3$ to begin with,
we could have rewritten our differential equation as
$v'=uv-6$
which is easier to differentiate by hand anyway:
$$
\eqalign{
\\v^{(1)}&=\left(u\right)\cdot&v+\left(-6\right)
\\v^{(2)}&=\left(u^{2}+1\right)\cdot&v+\left(-6 \, u\right)
\\v^{(3)}&=\left(u^{3}+3\,u\right)\cdot&v+\left(-6\,u^{2}-12\right)
\\v^{(4)}&=\left(u^{4}+6\,u^{2}+3\right)\cdot&v+\left(-6\,u^{3}-30\,u\right)
\\v^{(5)}&=\left(u^{5}+10\,u^{3}+15\,u\right)\cdot&v
          +\left(-6\,u^{4}-54\,u^{2}-48\right)
\\v^{(6)}&=\left(u^{6}+15\,u^{4}+45\,u^{2}+15\right)\cdot&v
          +\left(-6\,u^{5}-84\,u^{3}-198\,u\right)
\\v^{(7)}&=\left(u^{7}+21\,u^{5}+105\,u^{3}+105\,u\right)\cdot&v
          +\left(-6\,u^{6}-120\,u^{4}-522\,u^{2}-288\right)
\\v^{(8)}&=\left(u^{8}+28\,u^{6}+210\,u^{4}+420\,u^{2}+105\right)\cdot&v
          +\left(-6\,u^{7}-162\,u^{5}-1110\,u^{3}-1674\,u\right)
\\v^{(9)}&=\left(u^{9}+36\,u^{7}+378\,u^{5}+1260\,u^{3}+945\,u\right)\cdot&v
          +\left(-6\,u^{8}-210\,u^{6}-2070\,u^{4}-5850\,u^{2}-2304\right)
}
$$
especially if one notices the recursion
$$
\left.\matrix{
v^{(n)}=a_nv+b_n\\\\
a_1=u,~b_1=-6}\right\}
\quad\implies\quad
\left\{\matrix{
a_{n+1}=a_n'+u\,a_n\\\\
b_{n+1}=b_n'-6\,a_n\,,}\right.
$$
which I managed to calculate in sage:
var('a,b,u,v,z')
a(u) = u
b(u) = -6
for n in range(1,10):
    # print n, a, b #Note: a & b are functions of u
    # print n, a(u).simplify_full(), b(u).simplify_full()
    print '\\\\v^{(%d)}&=\\left(%s\\right)\cdot v+\\left(%s\\right)' \
    % (n, latex(a(u).simplify_full()), latex(b(u).simplify_full()))
    # print 'v^{(%d)}=%d' % (n, 2*a(2)+b(2))
    z(u) = a
    a(u) = (diff(a,u) + u*z).simplify_full()
    b(u) = (diff(b,u) - 6*z).simplify_full()

Using the initial condition $(u,v)=(2,2)$
corresponding to $(x,y)=(0,-1)$, we get
$$
v^{(1)}=-2\\
v^{(2)}=-2\\
v^{(3)}=-8\\
v^{(4)}=-22\\
v^{(5)}=-76\\
v^{(6)}=-262\\
v^{(7)}=-980\\
v^{(8)}=-3794\\
v^{(9)}=-15428
$$
so that, factoring out the $(-1)$,
I computed the following terms and partial sums:
t = [1, 2, 2, 8, 22, 76, 262, 980, 3794, 15428]
x = 0.1
y = 0
for n in range(10):
    tn = t[n]*x^n/factorial(n)
    y = y + tn
    print '%-4d %-14g %-16.12g' % (n, tn, y)

0    1              1
1    0.2            1.2
2    0.01           1.21
3    0.00133333     1.21133333333
4    9.16667e-05    1.211425
5    6.33333e-06    1.21143133333
6    3.63889e-07    1.21143169722
7    1.94444e-08    1.21143171667
8    9.40972e-10    1.21143171761
9    4.25154e-11    1.21143171765

So the analytic and Taylor series solutions agree.
Comment: I was actually just looking for the Taylor Series expansion (your final step) but you've overdone yourself. Thank you!


Div horizontally center and vertically middle

Question: I want to align a div horizontally center and vertically middle of the body of a page.
The css:
.loginBody {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    background: #999; /* for non-css3 browsers */
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cccccc', endColorstr='#000000'); /* for IE */
    background: -webkit-gradient(linear, left top, left bottom, from(#ccc), to(#000)); /* for webkit browsers */
    background: -moz-linear-gradient(top,  #ccc,  #000); /* for firefox 3.6+ */
}
.loginDiv {
    position: absolute;
    left: 35%;
    top: 35%;
    text-align: center;
    background-image: url('Images/loginBox.png');
    width:546px;
    height:265px;
}

And I have this html:

    <form id="form1">

    </form>

Now it is behaving as I want it to, but if I resize the browser, it became completely distorted, perhaps this is because the absolute positioning. I am showing some of the screenshots:
in resized firefox:

in resized chrome:

in resized ie:

in maximized window it is:

Is there any way to solve this problem and achieve this centered alignment using relative positioning?
Thanks.

Edit:
In firefox no scrollbar appears while resizing but it appears in other browsers.
Answer: Try this:
.loginDiv {
    position: absolute;
    left: 50%;
    top: 50%;
    text-align: center;
    background-image: url('Images/loginBox.png');
    width:546px;
    height:265px;
    margin-left: -273px; /*half width*/
    margin-top: -132px; /*half height*/
}

You move it to the center, and than back left and up by half the dimensions - that will center it even on resize
Comment: Thanks for help. it is now working but one problem in firefox there is no scrollbar while resizing but it is present in other browsers. I have updated my question. Please look at it.
Comment: if you don't want scrollbars - just add `overflow:hidden` to body
Comment: no I want scrollbar, so I set `overflow: auto` in body but still there is no scrollbar as depicted.
Comment: if you want a scrollbar, you have to add `overflow:scroll;` It would be helping to see that live. maybe setup a dummy-page. if the page gets to narrow, you might want to add a media-query with special styles for smaller devices.  PS - if my answer was the solution for your initial problem, I would appreciate an up-vote. - want to get past the 15 points ;-)
Comment: Thanks for your help. But the scrollbar of the body is not working in small size of the browser. I have accepted your answer since it solved my original problem. I have forked another thread for the second one.
Answer: Make a div that is for the content of the page and make a "content" class with the following css. This worked for me on Jquery Mobile with Phonegap for Android devices. Hope it helps someone.
CSS
.content {
    width: 100%;
    height: 100%;
    top: 25%;
    left: 25%;
    right: 25%;
    text-align: center;
    position: fixed;
}

Comment: `left: 25%` is missing a `;`
Answer: #div {
    height: 200px;
    width: 100px;
    position:absolute;
    top: 50%;
    left: 50%;
    margin-top: -100px;
    margin-left: -50px;
}

This is a common method to center a div. You position it absolutely, then move it half way across and half way down. Then adjust the position with margins by half the dimensions of the div you are positioning.
For reference (this uses fixed positioning though to keep the div in place even when scrolling, helpful when doing modal popups..
http://css-tricks.com/quick-css-trick-how-to-center-an-object-exactly-in-the-center/
Comment: Thanks for help. it is now working but one problem in firefox there is no scrollbar while resizing but it is present in other browsers. I have updated my question. Please look at it.
Comment: http://jsfiddle.net/mAVd6/2/ does this basically reflect how your page is built? Does it produce scroll bars in Firefox? other browsers? - if you swap absolute with fixed what happens to the scroll bars?!
Comment: If I changed the position to absolute to fixed then no scrollbar appears in any browser.


Number of fields with characteristic of 3 and less than 10000 elements?

Question: it's exam time again over here and I'm currently doing some last preparations for our math exam that is up in two weeks. I previously thought that I was prepared quite well since I've gone through a load of old exams and managed to solve them correctly.
However, I've just found a strange question and I'm completely clueless on how to solve it:
How many finite fields with a charateristic of 3 and less than 10000 elements are there?
I can only think of  Z3 (rather trivial), but I'm completely clueless on how to determine the others (that is of course - if this question isn't some kind of joke question and the answer really is "1").
Comment: There is (up to isomorphism) one field with $3$ elements, one with $9$, one with $27$, one with $81$.  Continue. Only four more to go.
Answer: Galois' theorem states that there exists precisely one finite field of characteristic $p$ with $p^n$ elements for each $n$ and that these are all of the finite fields of characteristic $p$. So all you have to do is calculate (or estimate) $\log_3(10000)$.
Comment: OK, so now heishe knows there is a field of order 9, but heishe doesn't know what it is, doesn't know how to show that it's isomorphic to every other field of order 9, and doesn't know how to show there aren't any fields of order 6. I'm glad heishe is happy with the answer, but it's a very shallow sort of knowledge that has been transmitted here.
Comment: You're right in a sense; my answer doesn't explain the technology of the result and the same information could have been found by an (informed) Google search. On the other hand, it does answer the question. While some people go to great lengths to write clear and comprehensive answers (something which I can only admire), I do not feel it should be a requirement to write up a derivation of any and all results one needs in answering a particular question. In any case, if, as you presume, heishe hasn't seen this theorem in class, it doesn't really matter much.
Comment: You are right, in all senses. I wouldn't expect anyone to write up, in this forum, a derivation of all the results you give in your answer, especially as those derivations are widely available elsewhere. I think I was mostly letting off steam, and maybe trying to point heishe to some further directions to explore when the exam is over.
Answer: It's not a joke question. Presumably, the year that was on the exam, the class was shown a theorem completely describing all the finite fields. If they didn't do that theorem this year, you don't have to worry about that question (but you'd better make sure!). It's not the kind of thing you'd be expected to answer on the spot, if it wasn't covered in class.


OpenSSH relay for specific host

Question: at the moment I take care of a network for a company which has different subdomains pointing all at the same IP-address for different purposes.
We have example.com for their website and gitlab.example.com for their Gitlab instance.
Currently we want to move the Gitlab instance to a different server on the same network. For the Web-UI of Gitlab we use nginx as a reverseproxy to the other server's IP and for manage the SSL-certificates and the host.
But now we have the problem, that we can't establish a ssh connection to this other server, because the WAN-port is forwarded to the webserver and not to the new gitlab-server and we need this port pointing to the webserver. But also because of the amount of projects we have, we can't to change the port. (We have no time to reconfigure all these projects in git)
Is there a posibility to let the webserver act like a relay for the gitlab-server? So that every connection to gitlab.example.com is forwarded to this specific server?
Thanks for your help!!
Comment: ssh has no provision for this sort of thing. Get another IP address.
Answer: A simple answer would be the following:

Change the command and control SSH port for the non-gitlab server to be 2222 so there's no port collision
Install HA Proxy and setup a L4 (TCP, not HTTP) listener at port 22
Set the backend of the HA Proxy pool to be the gitlab server, port 22

This will redirect all SSH (port 22) traffic from the external server to the internal server.
This can also be accomplished with DNAT/SNATing via IPTables, but it's much more complex than the above solution. An untested example is here:
<code>
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 22 -j DNAT --to <gitlabIP>:22
iptables -t nat -A PREROUTING -p tcp --dport 22 -j DNAT --to <gitlabIP>:22
iptables -t nat -A POSTROUTING -p tcp -d <gitlabIP> --dport 22 -j MASQUERADE
</code>
Comment: @Tero - I literally mention that in step 1...? `1. Change the command and control SSH port for the non-gitlab server to be 2222 so there's no port collision`.
Comment: The drawback not mentioned here is that then you have to configure the first server's SSH to use another port than 22.
Comment: True, sorry for missing that.

OpenSSH relay for specific host


Twitter bootstrap tooltip on chosen-select not working

Question: Did anyone succeed in adding a tooltip on chosen-select box?
I created a jsFiddle to demonstrate the problem.
This is the relevant code:
$('body').tooltip({
    selector: '.chosen-select',
    animate: true,
    delay: 400,
    placement: 'right',
    trigger: 'click hover focus manual',
});
My guess why this doesn't work, is because the select tag has a style of display: none.
Comment: "chosen" hide the select element and replace it by a div, so your guess is right. You have to pick another selector.
Answer: The problem is you weren't initializing the tooltip properly. You must have the data-toggle="tooltip" title="Some tooltip text!" attributes on any element to make a tool tip work properly. I also changed which element you were binding the tooltip too.
See fiddle. http://jsfiddle.net/e62Wu/50/
HTML
<select class="chosen-select" data-toggle="tooltip" title="Some tooltip text!">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>

JS
$('.chosen-container').tooltip({
    animate: true,
    delay: 400,
    placement: 'right',
    trigger: 'click hover focus manual'
});

Comment: The jsfiddle you changed works just fine. I tried to implement it that way but I only get the yellow tooltip under the `chosen-select`. Any idea what I might be doing wrong?


Resources for learning Markov chain and hidden Markov models

Question: I am looking for resources (tutorials, textbooks, webcast, etc) to learn about Markov Chain and HMMs. My background is as a biologist, and I'm currently involved in a bioinformatics-related project.
Also, what are the necessary mathematical background I need to have a sufficient understanding of Markov models & HMMs?
I've been looking around using Google but, so far I have yet to find a good introductory tutorial. I'm sure somebody here knows better.
Comment: This would be very useful: https://web.stanford.edu/~jurafsky/slp3/A.pdf
Answer: Here are some tutorials (available as PDFs):

Dugad and Desai, A tutorial on hidden markov models
Valeria De Fonzo1, Filippo Aluffi-Pentini2 and Valerio Parisi (2007). Hidden Markov Models in Bioinformatics. Current Bioinformatics, 2, 49-61.
Smith, K. Hidden Markov Models in Bioinformatics with Application to Gene Finding in Human DNA

Also take a look at Bioconductor tutorials.
I assume you want free resources; otherwise, Bioinformatics from Polanski and Kimmel (Springer, 2007) provides a nice overview (§2.8-2.9) and applications (Part II).
Comment: The tutorial is not downloadable, returns "Download Limit Exceeded", accessible from this link: https://www.researchgate.net/publication/2348637_A_Tutorial_On_Hidden_Markov_Models
Answer: There is also a really good book by Oliver Cappe et. al: Inference in Hidden Markov Models.  However, it is fairly theoretical and very light on the applications.
There is another book with examples in R, but I couldn't stand it - Hidden Markov Models for Time Series.
P.s. The speech recognition community also has a ton of literature on this subject.
Answer: It is quite surprising to see that none of the answers mention the Rabiner tutorial paper on HMMs.
While the practical implementation (the latter part of the paper) is focused on speech recognition, this paper is probably the most commonly cited one in the HMM literature, thanks to its clear and well-presented nature.
It starts by introducing markov chains and then moves on to HMMs.
Answer: For bioinformatics applications, the classic text on HMMs would be Durbin, Eddy, Krough & Michison, "Biological Sequence Analsysis - Probabilistic Models of Proteins and Nucleic Acids", Cambridge University Press, 1998, ISBN 0-521-62971-3.  It is technical, but very clear and I found it very useful.
For MCMC there is a recent (version of a) book by Robert and Casella, "Introducing Monte Carlo Methods with R", Springer, which looks good, but I haven't had a chance to read it yet (uses R for examples, which is a good way to learn, but I need to learn R first ;o)
Answer: Already nice suggestions, I would like to add the following articles that describe HMMs from perspective of application in biology by Sean Eddy.

Hidden Markov Models
Profile hidden Markov models
What is a hidden Markov model?
Answer: I learned HMMs using the great book by Walter Zucchini and Iain L. MacDonald
Hidden Markov Models for Time Series: An Introduction Using R
It's really good and features examples in R.
Answer: Take a look at the (HMM) Toolbox for Matlab by Kevin Murphy and also section Recommended reading on HMMs on this site.
You can also get Probabilistic modeling toolkit for Matlab/Octave with some examples of using Markov Chains and HMM.
You can also find lectures and labs on HMM, for example:

Labs
Lecture1 and Lecture2
Answer: My 2 cents
Beautifully explained and free.
Hidden Markov Models, Theory and Applications
University of Leeds Tutorial
Answer: Here are some notes by Ramon van Handel at Princeton:

This course is an introduction to some of the basic mathematical,
  statistical and computational methods for hidden Markov models.

The first section includes a nice set of applications of HMMs in biology, finance,...
Answer: Here is a nice interactive introduction to Markov Chains http://setosa.io/ev/markov-chains/
Comment: Since the question is asking for resources for learning Markov Chains, a link to a tutorial is appropriate here - this is one of the cases where we wouldn't expect your answer to be self-contained and attempt to completely summarise the contents of the link  - but it would be much better if you could accompany the link with a bit of description as to what this resource covers, e.g. at what level it's pitched (any mathematical prerequisites?) and what topics it covers.
Answer: There are only 3 video i have found very useful for understanding maths behind hidden markov models-
https://www.youtube.com/watch?v=E3qrns5f3Fw
https://www.youtube.com/watch?v=cjlhpaDXihE
https://www.youtube.com/watch?v=5sGEF-e82yY
These are really good and taught by one the best Indian professors from IIT krg.
Answer: This playlist is a great explanation and is based on the paper by Rabiner mentioned in the answer above. -
https://www.youtube.com/watch?v=J_y5hx_ySCg&list=PLix7MmR3doRo3NGNzrq48FItR3TDyuLCo
This above playlist is a 12 series lecture which begins with explanation of Markov Chains/ observable Markov Models and then moves on to HMMs


Question about limits in Indeterminate form

Question: The derivative of a function $f(x)$ is defined as $\normalsize \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}$, substituting with $h=0$ yields the indeterminate form $\frac{0}{0}$, so we try to do some algebraic manipulation to cancel the $h$ in the denominator and substitute again with $h=0$, hoping that we will get some valid value.
However, according to my understanding, it is not clear that this is valid, since we were not allowed to substitute with $h=0$ in the first place since $\normalsize \frac{f(x+h) - f(x)}{h}$ is not defined at $h=0$,so why in the end we are allowed to do this substitution?
For example if we have $f(x) = x^2$ then $\normalsize \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}$ = $\normalsize \lim_{h \to 0} \frac{(x+h)^2 - x^2}{h}$ = $\normalsize\lim_{h \to 0} \frac{2xh +h^2}{h}$ = $\normalsize \lim_{h \to 0} {2x + h}$, and we substitute with $h=0$ to get the result $2x$ : why are we allowed to make this substitution now?
As I understand, we can prove that this process is valid with some $\epsilon$ and $\delta$ proof, but what troubles me is that I found multiple calculus textbooks making this substitution without justifying it at all, as it was clear that it is valid, while I see that this is something not immediately clear and needs to be justified, so am I missing something? Can you make a reference of some textbook that justify this process? I would like to see its proof.
Edit
I think the general statement which needs to be proved will be something like: when $f(x) = g(x)$ for all points on some interval except at $x_0$, then their limit at $x_0$ will be equal (and when g(x) is continuous at $x_0$ the limit will be of course equal to $g(x_0)$).
What is the proof of this general statement (if I'm correct and indeed this is the statement that needs to be proved)? And why it seems that calculus books ignore this issue totally
Thanks a lot
Comment: See related https://math.stackexchange.com/a/1822706/72031 and also the discussion in comments to this answer.
Comment: Most typical calculus textbooks don't discuss these issues because the goal of such books is to make students aware of the technique and not necessarily the theory of calculus.
Comment: @ParamanandSingh thanks a lot
Answer: Be careful: the derivative of a function at a point $x$ is defined by $f'(x):=\lim_{h \to 0} \frac{f(x+h)-f(x)}{h}$ provided that the latter limit exists and is finite. It's different from what you said.
You cannot substitute $h=0$ because the difference ratio $\frac{f(x+h)-f(x)}{h}$ is not defined at $h=0$; this has nothing to do with limits or indeterminate forms, it is something that appears way before in the theory of functions.
We can evaluate limits at a point where a function is not defined (actually, they are the most interesting cases, for instance $\lim_{x \to 0} \frac{\sin x}{x}$), because limits are defined in the so called accumulation points. Accumulation points, intuitively, are points such that you can go as near as you want to them but never reach them. So, in limits, we exclude the value where the variable tends to; that is, in $\lim_{h \to 0} \frac{f(x+h)-f(x)}{h}$ we consider $h\ne 0$.
Technically, in general you can't say that when $h \to 0 $ it is $f(x+h)\to f(x)$ because this is true only if $f$ is continuous at $x$. Hence, the substitution $h=0$ to deduce something about the value of the limit you are referring to is a tool to evaluate the limit is only if you already know the continuity at $x$ of the functions involved under the limit sign. Even assuming that $f$ is continuous at $x$, you could say that indeed that the numerator $f(x+h)-f(x)$ tends to $0$ as $h \to 0$ and the denominator $h$ tends to $0$ as $h\to 0$, but this are only separated informations on the behavior of the numerator and the denominator. In general, you cannot deduce the behavior of the limit of a ratio by the behaviors single limits of the numerator and the denominator. You can only deduce something when you can apply the algebraic theorems of limit of ratio; and guess what? Those algebraic theorems I'm referring to apply when the famous cases of the indeterminate forms don't occur.
Hence, the point is that "indeterminate form" is just a shorthand that means: "We cannot deduce the limit of a function $F$ only knowing the behavior of the single limits of the functions $f_1,f_2,...,f_n$ involved in the limit of $F$". A priori, we don't know if it exists and it is finite or infinite, or if it doesn't exist; a deeper analysis must be done when an indeterminate form appears.
So, in the context of limit defining the derivative, you think as $h\ne0$ and you are interested in what happens when $h$ becomes arbitrarily small but it is never $0$. So, using your example $f(x)=x^2$, you have:
$$(x^2)'=\lim_{h \to 0}\frac{(x+h)^2-x^2}{h}=\lim_{h \to 0}\frac{2xh+h^2}{h}=\lim_{h \to 0}\left(2x\frac{h}{h}+\frac{h^2}{h}\right)$$
Here comes the important part about accumulation points. Since $h\ne 0$, the functions $h/h$ and $h^2/h$ are equivalent, respectively, to the functions $1$ and $h$ (if this seems strange to you, just think about this: $h/h$ and $h^2/h$ are not defined at $h=0$ because the division by $0$ is not defined, hence for $h=0$ their expressions are meaningless; but, if $h\ne0$, then for any $h\ne0$ the division between two real numbers is defined and if a factor appears both in the numerator and in the denominator you can cancel it out; $h$ in the numerators and it is different from $0$, so we can cancel it out in this case. I know this seems a delirium at first, but details are important in math). So, in the limit where $h\to0$, being $0$ an accumulation point we can write:
$$\lim_{h \to 0}\left(2x\frac{h}{h}+\frac{h^2}{h}\right)=\lim_{h\to 0}(2x+h)$$
Now, you can evaluate the latter limit with the definition as follows.
Let $\epsilon>0$, choose $\delta=\epsilon$. Hence $(|h|<\delta=\epsilon) \implies (|2x+h-(2x)|=|h|<\delta=\epsilon)$. So $\lim_{h \to 0} (2x+h)=2x$. As you've seen, no substitution of $h=0$ occurs at all in all this reasoning.
Comment: thanks for the clarification, but then what is the proof of the general case(can you refer to my edit?). Also why it seems that calculus books ignore this issue totally?
Comment: @LoaiGhoraba for the edit, notice that if $f(x)=g(x)$ for any $x \ne x_0$, for what I said about accumulation points, when $x \to x_0$ it is $x \ne x_0$ and so in the limit $\lim_{x \to x_0}f(x)$ we can substitute $g(x)$ in place of $f(x)$; hence, $\lim_{x \to x_0} f(x)=\lim_{x \to x_0} g(x)$. For what I said about continuity, if we add the hypothesis that $g$ is continuous at $x_0$ it is $\lim_{x \to x_0} g(x)=g(x_0)$ (as I said, in the case of continuity the limit can be evaluated by substituting the value in the argument of $g$). Hope this helps.
Answer: This question trigged me also and it's good : it shows that you are curious !  In fact you have mentionned the key answer, something proper to analysis : the $\delta-\varepsilon$ method.
The fact is that when you take the limit of something when something else goes to $x$, you are not allowing to be equal $x$. This first explains why we need to work on the expression involved in the limit :
$$
\lim_{h\to 0}\frac{x^2 + 2xh + h^2 - x^2}{h} = \lim_{h\to 0}\frac{h\left( 2x + h\right)}{h} = \lim_{h\to 0}(2x+h) = 2x+\lim_{h\to 0}h
$$
Now to conclude, you need to compute the limit of $h$ when $h$ tends to $0$ using the definition of the limit. A naturel guess for the limit is $0$ :
$$
\forall\varepsilon>0,\exists\delta>0,\forall h\in \mathbb{R} : \lvert h\rvert\leq\delta\implies\lvert h\rvert\leq\varepsilon
$$
The definition is thus just an implication that relies on the existence of a such $\delta$, here it is simple, juste take $\delta = \varepsilon$ !
What the mathematical expression of the limit says is that whatever the epsilon you take, you can find a neighborhood such that the implication holds.
Thus
$$
\lim_{h\to0}h = 0
$$
And you get your result. The fact to keep in mind, is that the derivative at a point is a limit process, you need to use the tool provides by analysis !

We say that the limit of a function $f(x)$ when $x$ tends to $x_0$ is $L$ when the following holds :
$$
\forall\epsilon>0, \exists\delta>0, \forall x\in\mathbb{R} : \lvert x-x_0\rvert\leq\delta\implies\lvert f(x) - L\rvert\leq\varepsilon
$$
In our case $f(x) = x$ and $x_0 = 0$. The intuition (at least for me) is exactly the same as the one provided above.

I hope this will help you ! Let me know if something is not clear !
Comment: thanks for the clarification, can you refer to my edit in the question?


Remove module folder when uninstalling

Question: is there any method/module that allow removing module folder when uninstalling the module
or delete the files via dashboard
Comment: If you want to "automatically replace files when a new version is released", you can use Drush.  Drush allows you to enter a command like "drush up views" to automatically update Views to the latest version.
Comment: Out of curiosity, what is the use case for this?
Comment: Automatically replace files when new version is released.
Comment: That doesn't make sense to me. You don't want to un-install a module to perform an upgrade.
Answer: To take a different angle:
It's very possible to do this, but you shouldn't. Your web server user should not have the capacity to write to the web root (except for designated folders like the public files folder). You should therefore not be able to delete module folders from code. Doing so presents a security risk to your server.
This is one of the checks the Security Review module performs, and throws up a nice big red error if it find the web server can write to such files:

I understand this might be unavoidable if you're on shared hosting, but I think it's a good piece of information to have either way.
Comment: If I could edit this to make all of the text bold, red, and blinking, I would.
Comment: @MPD Shame the `` and `` tags aren't available in Markdown eh? ;) I've tried to make it a bit more shouty, hopefully the point will get across
Answer: A module could implement hook_modules_uninstalled to do this for other modules, but I'm not aware of such a module, no.
Comment: `hook_uninstall` only "Remove any information that the module sets."
Comment: @NikhilM I was a bit to quick to write there >_< I didn't mean a module could do it for _itself_, but for others, though I refered to a hook that isn't invoked for other modules. Fixed that now.
Comment: I agree to @NikhilM ; OP is asking for a method to remove the module folder on uninstalling it..
Comment: @Letharion, Saw the edit, Seems helpful for the OP now.
Comment: I think we need a cpanel module to browse the _sites_ directory

Remove module folder when uninstalling


Question about Going-Up

Question: Let $k$ be a field, not necessarily algebraically closed. Then how would you show that the extension $k[x] \subset k[x,y]$ does or does not satisfy Going-Up?
Answer: No, that extension does not satisfy Going-Up.
Indeed the associated morphism $f:\mathbb A^2_k \to \mathbb A^1_k$ is the first projection and is well known not to be a closed map:
the closed hyperbola $V(xy -1)\subset \mathbb A^2_k $ projects to $\mathbb A^1_k \setminus V(x)=\mathbb A^1_k \setminus \lbrace O \rbrace \subset \mathbb A^1_k $, which is not closed in $\mathbb A^1_k$
What has this got to do with Going-Up? A lot! Atiyah-Macdonald prove in (Exercise 6.11, page 79)  that a morphism of rings $A\to B$ with $B$ noetherian satisfies Going-Up if and only if the associated morphism of affine schemes $Spec(B)\to Spec(A)$ is closed.
This proves that your inclusion$k[x] \to k[x,y]$ does not satisfy Going-Up.
The direct way Alternatively, we can atttack the problem frontally.
Consider the inclusion  of prime ideals $(0)\subsetneq (x)$ in $k[x]$ and the prime ideal $(xy-1)$ in $k[x,y]$ above $(0)$ (= whose intersection with $k[x]$ is $(0)$).
Going-Up does not hold because there is no prime ideal  ${\mathfrak q}\subset k[x,y]$ with both $(xy-1) \subset {\mathfrak q}$ and ${\mathfrak q} \cap k[x]=(x)$ [since then we would have $xy-1,x\in {\mathfrak q}$, so that $-1\in {\mathfrak q}$: this is  impossible for a prime ideal].
Comment: Thank you for the direct way. While I am aware of the closed map criterion, I was not sure how to show it does not hold.

Question about Going-Up


Is every subset of $\mathbb{R}^n$ an $n+k$ topological manifold?

Question: Is every subset of $\mathbb{R^n}$ a $n+k$ topological manifold ?
It seems intuitively true because you can just decide to think of the subset as a subset of $\mathbb{R}^{n+k}$
Practically what you do is you map each point $p =(p_1,\cdots,p_n)$ to $ f(p)=\tilde{p} = (p,0,\cdots,0)$ and there's $k$ zeroes of course.
And this mapping is very analogous to the identity mapping so I guess it's bi-continuous, otherwise it would be surprising if it actually isn't.
But I just learned the definition of a topological manifold some minutes ago and I haven't yet become very comfortable with the concepts.
So if that's true how would you show it formally ? and if it's false (which I doubt) why ?
Any clarifications will be much appreciated.
Comment: No. Consider the rationals as a subset of the reals. It is not a manifold of any dimension in any way.
Comment: If that seems intuitive to you then your intuition needs adjustment.
Comment: you need to go with open sets of $\mathbb{R}^n$
Comment: What about two intersecting lines in the plane?  What is the neighborhood of the point of intersection?
Comment: @simonsays I see, no matter how you surround the intersecting point with an open set it doesn't look mappable with a bijective map, is that it ?
Answer: I suppose you intend $k > 0$.
No.  To be a $n+k$-dimensional topological manifold (without boundary), every point must have an open neighborhood homeomorphic to $\mathbb{R}^{n+k}$, which is not going to happen if you clamp $k$ coordinates to $0$.
Further, a single point is a subset of $\mathbb{R}^n$ and is neither an $n$-dimensional manifold nor an $n+k$-dimensional manifold, unless $n = k = 0$.  The integers are a subset of $\mathbb{R}^1$ and aren't even a $0$-dimensional manifold (because they are not Hausdorff in the induced topology).
Another way to come at this is that a neighborhood of a point in an $n$-dimensional manifold has positive measure using the $n-$fold product of Lebesgue measures.
(If we want to talk about manifolds with boundary and points on the boundary, each has a neighborhood homemorphic to the half space $\mathbb{R}^n$ with last coordinate $\geq 0$, so has neighbors with $n$-dimensional neighborhoods and has a neighborhood with positive $n$-dimensional measure.)
Answer: the definition you later work with will probably look like this: A $n$-dimensional manifold is a set $M$ with an open cover $\{U_i\}_I$ for which there exists homeomorphisms $\phi_i:U_i \rightarrow \mathbb{R}^n$ for each $i \in I$. The intuition you keep in mind is that you think of $M$ as an object that looks locally like $\mathbb{R}^n$.
Answer: What you have from that is that every subset of $\mathbb{R}^n$ has a homeomorphic copy in $\mathbb{R}^{n+k}$ for $k\geq0.$


Changing file permissions on web server to follow Least Privilege Security Principle: chmod 750 -R /

Question: I'm not sure what details are relevant, but here is some general context. I'm configuring a Ubuntu 12.04 web server on AWS to process customer payment info before redirecting them to a hosted pay-page, which will not return transaction details for further processing. Pre hosted pay-page processing will involve connecting remotely to our on-premise database so customer info can be verified and account details can be returned for display.
I'm the only one administering the server. The default permissions appear to be 755 for most directories and files, with owners being root:root. In trying to follow the security principle of least privilege, I'm wondering about removing all world read/execute permissions so the Server Root is left with 750, adding myself to the root group to retain usability. I have Apache running as www-data, and I've set permissions for the Web Root to 2750 and owners to root:www-data -- adding myself to the www-data group for usability.
It seems common to change a systems umask from a default of 022 to 027 so anything new created has permissions 750 instead of 755, but I haven't come across anything that talks about making that sort of change "retroactive" for the entire file system. Is this a good idea, and is there a proper way to do it?
Answer: Do not do that, your webserver will no longer have permission to read anything on the box.  You need permissions from the root of the filesystem all the way up to the directory where the webserver will be reading from.
That is why you don't find anyone talking about doing it, it won't work.  Your webserver starts as root, but it immediately changes to another user after binding to the privileged ports.
It won't even work removing the -R.  Run chmod 750 / on your box then try to load a web page.
Comment: Thanks. I am new and still learning the basics. So, if my web root /var/www is 750 with owners root:www-data, as long as / and /var have r-x permissions it should work? Or maybe having most of your file system world readable is not a security risk? Maybe I should look more into chroot if I want to accomplish something similar?


Botan::DSA_PrivateKey to DSA_PublicKey how to?

Question: I've succeeded with creating a new DSA_PrivateKey and Botan::PKCS8::PEM_encode to save to a .pem file. Now I need to save the PublicKey to a separate .pem also. Anybody know how to copy the DSA_PrivateKey to a DSA_PublicKey? I tried many different things none worked.. Shall appreciate this, thanks!
Answer: Figured this out, use:
Botan::DSA_PublicKey(DL_Group, privatekey->get_y());

then
keystring = Botan::X509::PEM_encode(pubkey);


firewalld: interface autonomously changing zones

Question: I have an RHEL8 system serving as a Docker Swarm worker node. It has firewalld enabled, and has a docker zone to which the docker0 and docker_gwbridge interfaces are assigned.
$ cat /etc/firewalld/zones/docker.xml
<?xml version="1.0" encoding="utf-8"?>
<zone version="1.0" target="ACCEPT">
  <short>docker</short>
  <description>zone for docker bridge network interfaces</description>
  <interface name="docker_gwbridge"/>
  <interface name="docker0"/>
</zone>

After reboot, or firewalld restart or reload, these interfaces appear in the correct zone, per firewall-cmd --get-active-zones.
$ firewall-cmd --get-active-zones
docker
  interfaces: docker_gwbridge docker0
internal
  interfaces: vethb6daacd veth0a3a13c veth3922477 veth1fc2c24 veth35f6f77 veth172d461 vethf457e97 vethed46b94 vethc3293eb vethe6c08de vethb1c5fb6 vethd6bcfd8 eth0

However, after some minutes (usually less than an hour), they move to internal zone instead, breaking networking in containers.
$ firewall-cmd --get-active-zones
internal
  interfaces: vethb6daacd veth0a3a13c veth3922477 veth1fc2c24 veth35f6f77 veth172d461 vethf457e97 vethed46b94 vethc3293eb vethe6c08de vethb1c5fb6 vethd6bcfd8 eth0 docker_gwbridge docker0 veth5686e56 vetha51060c vethde79c75

A firewall-cmd --reload fixes it again for a little while.
This question looked relevant, but these interfaces (if I'm interpreting correctly) are not managed by NetworkManager, so I don't think that's at fault.
$ nmcli device
DEVICE           TYPE      STATE                   CONNECTION
eth0             ethernet  connected               eth0
docker0          bridge    connected (externally)  docker0
docker_gwbridge  bridge    connected (externally)  docker_gwbridge
veth5686e56      ethernet  unmanaged               --
vetha51060c      ethernet  unmanaged               --
vethde79c75      ethernet  unmanaged               --
lo               loopback  unmanaged               --

$ ls /etc/sysconfig/network-scripts/
ifcfg-eth0

I can't find anything interesting in /var/log/firewalld. I have several other nodes that are theoretically configured the same way where this problem doesn't occur.
I didn't set the nodes up, and I'm not a sysadmin, but I'm trying to figure it out! Any words of wisdom?
Answer: Looks like a Chef task is the cause of this, with Chef inexplicably configured differently between working and broken servers.


Alter node before render or after save

Question: I need to alter the content of a node either before it is viewed or after it is saved. In order alter the fields I will need the create date of the node. I am currently attempting to do this via hook_node_view, but is there a better way?
Comment: Are you using Drupal 6 or 7?
Comment: Drupal version 7.
Answer: hook_node_presave() can help you making the changes

Act on a node being inserted or updated. This hook is invoked from node_save() before the node is saved to the
  database.

so inside this hook you can get the created time and can change the title.
As an example code: Here I am making node title as the time article node is created.
function mymodulename_node_presave($node) {
  if($node->type == "article") {
    $node->title = $node->created;
  }
}

Make sure you do not call node_save() inside this function, else this will turn out to non breking recursion.
Comment: Is it possible to do this shorter? To only speak to article node and no need to check if node == article?
Answer: hook_node_insert() responds to the creation of a new node.
This hook is invoked from node_save() after the node is inserted into the node table in the database, after the type-specific hook_insert() is invoked, and after field_attach_insert() is called.
Comment: Laxman13, I tried using hook_node_insert() unsuccessfully. Here is more detail about what I want to do, whenever a node(article) is created, I would like to get its created date and depending on the date change the value of certain content in the node-say the title for example. So, when the node is saved and displayed in the browser the title should be changed. I was somewhat able to accomplish this with hook_node_view. With hook_node_view when I save the node the database value is changed but the browser does not reflect the change. When I refresh the browser the new database value is shown.
Comment: No @gilzero, hook_node_insert is invoked before the node is saved, not after it. I had a long email process telling users that the new node was created, taking 5 minutes, inside hook_node_insert but the people who got the email and went to the site didn't see the email until the process was finished.

Alter node before render or after save


Let $P_{1},P_{2}$ be orthogonal projectors. With what condition is $P_{1} + P_{2}$ an orthogonal projector and on which space is it projecting?

Question:
Let $P_{1},P_{2}$ be orthogonal projectors on finite unitary space. With what condition is $P_{1} + P_{2}$ an orthogonal projector and on which space is it projecting?

I have concluded that the condition is $P_{1} * P_{2} = - P_{2} * P_{1}$.
For the space it is projecting, I believe I should observe $Im (P_{1} + P_{2})$ but since $Im (P_{1} + P_{2}) \subseteq Im (P_{1}) + Im(P_{2}) $ I don't see how to conclude anything from there. Any hint helps!
Comment: Note that the images of $P_1$ and $P_2$ mist have null intersection, and that the rank of $p_1+p_2$ must be its trace, which is sum sun of the traces (thus the ranks) of $p_1$ and d $p_2$, so the image of $p_1+p_2$ must be the sum of the images of $p_1$ and $p_2$. In other words: $p_1(p_1(y)+p_2(z))+p_2(p_1(y)+p_2(z))$ must always equal $p_1(y)+p_2(z)$, which entails $p_1 \circ p_2=p_2 \circ p_1=0$.
Comment: If $x$ is in the image of $P_1$ and $P_2$, then $(P_1+P_2)(x)=2x$. This is impossible when $P_1+P_2$ is a projector, unless $x=0$.
Comment: @Mindlack can you explain further why do images of $P_{1}$ and $P_{2}$ have only null intersection? I don't know if the text of the task is misleading, but $P_{1}$ and $P_{2}$ are not mutually orthogonal, they are orthogonal projectors. Am I missing something obvious? I am kind of beginner in this scope.
Answer: Note that $P_1+P_2$ will always be self adjoint. Hence $P_1+P_2$ is a projection precisely when $(P_1+P_2)^2=P_1+P_2$, which occurs precisely when $P_1 P_2=-P_2 P_1$ (1). If this equality holds note that we can rewrite the operator $-P_1P_2P_1P_2$ as $-(-P_2P_1)P_1P_2=P_2P_1P_2$, but we can also rewrite it as $-P_1(-P_1P_2)P_2=P_1P_2$. Hence $P_1P_2=P_2P_1P_2$. Taking adjoints on both sides we get $P_2P_1=P_2P_1P_2$ so we see that actually $P_1P_2=P_2P_1$. In conjunction with (1) this clearly shows that $P_1P_2=P_2P_1=0$ so $P_1H$ and $P_2H$ must be mutually orthogonal subspaces. Conversely, if $P_1H$ and $P_2H$ are mutually orthogonal subspaces it's easy to see that (1) holds (both sides are then $0$).
Comment: Can you explain why does it work when $P_1 P_2=P_2 P_1=0$ precisely? I got that the $P_1 P_2=-P_2 P_1$ and I can not see why it doesn't work.
Comment: You are right, my original argument was far from complete. I've edited now so hopefully this works.


Is a storage-space-efficient RAID with 3 arbitrary disks failing without data loss possible?

Question: Beforehand: A RAID 1 or RAID 1+0 is not a solution because they're not storage-space-efficient as they don't share the property of RAID 5 and RAID 6 that the number of additional disks needed is constant as the total number of disks increases.

In storage management, it's often a desired property that individual disks can fail without data being lost. For this, there are different approaches called "RAID level" which solve different problems.
I'm interested in a hypothetical RAID levels which I'm going to call RAID 7 (not to be confused with the non-standard RAID level 7 created by a company).
RAID 4 allows that of $n$ disks, 1 arbitrary disk fails and no data is lost whilst allowing the data which can be stored on $n-1$ disks to be usable. This is achieved by XORing the bits of all but 1 disk and writing them onto a different disk. So if disk $n$ is the parity disk and bit $k$ of disk $m$ is called $\text{bit}_{k, m}$, $\text{bit}_{k, n}$ can be determined by XORing $\text{bit}_{k, i}$ for all $i$ from 1 to $n-1$. If 1 disk fails, a different one can be swapped in and all others can be determined by XORing over the bits of all the still-working disks (the assumption "if disk $n$ is the parity disk" isn't required).
RAID 5 is basically the same thing, just a practical detail is added: The parity information is distributed over all disks, not concentrated on one. This is a detail you don't need to regard.
RAID 6 allows that of $n$ disks, 2 arbitrary disks fail and no data is lost whilst allowing the data which can be stored on $n-2$ disks to be usable. Its parity computation consists of a simple XOR (just like RAID 4 and RAID 5) and math I don't understand.
My question now is whether RAID 7 is mathematically possible where RAID 7 allows that of $n$ disks, 3 arbitrary disks fail and no data is lost whilst allowing the data which can be stored on $n-3$ disks to be usable.
I find it surprising that it just stops after 2 arbitrary disks failing without data loss without me ever heard of RAID 7 being mathematically impossible. Is it possible and just computationally very expensive?
Answer: Yes, it is possible to construct such an array. See: http://blog.ronnyegner-consulting.de/2014/12/10/parity-based-redundancy-raid56triple-parity-and-beyond-on-btrfs-and-mdadm-dec-2014/comment-page-1/


Let $T \in L(\mathbb{R}^n,\mathbb{R}^m)$. Prove that $T$ is differentiable at every $x \in \mathbb{R}^n$.

Question: Let $T \in L(\mathbb{R}^n, \mathbb{R}^m)$. Prove that $T$ is differentiable at every $x \in \mathbb{R}^n$.
Thinking to start with the fact that $T = Ax$, for some $m × n$ matrix $A$, but not sure where to go with that.
Lost in the notation.
I think I need to demonstrate $$\lim_{H \to 0} \frac{f(x_0 + H)-f(x_0)-T(H)}{||H||} = 0.$$
Please help steer me. 1st round of analysis in $\mathbb{R}^n$.
Comment: But $f(x)=T(x)=Ax$. Put that into your numerator.
Comment: Is this as easy as I just made it:    $\lim_{H}\to{0}$ $\frac{A(x+H)-A(x)-A(H)}{||H||} = 0$.
Comment: Because that limit is equal to the limit of $\frac {A(x) + A(H) - A(x) - A(H)}{||H||}$
Comment: Yup, and the numerator's pretty simple!
Answer: Here, we have $f(x) = T(x)$. In the notation you're using, $f':\Bbb R^n \to L(\Bbb R^n, \Bbb R^m)$, which is to say that we should have $f'(x) \in L(\Bbb R^n,\Bbb R^m)$ for every $x \in \Bbb R^n$.  In fact, it turns out that we should have $f'(x) = T$ for every $x \in \Bbb R^n$; consider the fact that your linear function has a constant derivative (i.e $f'(x)$ should be the same linear transformation for all $x$).
So in particular, what you need to show is that
$$
\lim_{h \to 0} \frac{f(x + h) - f(h) - Th}{\|h\|} = 0
$$
This is very easy to show, since the numerator of that fraction is always $0$.


MySQL select unused/stale id based on timestamp period

Question: I have an attendance table with simple .user and .timestamp and want to identify any .user without a timestamp since a specific time (eg 1 year ago).
I have tried several combinations of between and is not between etc., but can't seem to get the correct output (my logic and SQLs is obviously different :( )
The want to remove 'stale' users (haven't attended for a year).  Once I have the data from the attendance table I will use that to delete records from other tables containing their details.
Comment: Welcome to StackOverflow. Please take a moment to read the guidelines about asking questions on this site. You need to provide complete information and ask specifically targeted questions. Please provide the code you've tried and explain HOW it doesn't work.
Answer: To Delete:
<code>DELETE FROM userattendance WHERE user_id IN (SELECT user_id FROM users WHERE mytimestamp < (UNIX_TIMESTAMP() - 31557600))
</code>
It's pretty much saying DELETE data from the user attendance table where mytimestamp field is less than the current unix timestamp minus a year for any user.
Comment: I think DELETE is supposed to be deleting from other table :) don't let op to delete original data accidentally.
Comment: Thanks, that got it - even though on my first post I missed adding the code etc, you got it done.  Cheers.


How to destructure dictionary into eval command as function arguments

Question: I have create a sample function where I want to pass all the keys in dict third_party to eval statement.
Using itemgetter is not possible as dictionary key are variable.
def sample(third_party):
    '''
    :param third_party: is a dictionary number of keys and exact keys name varies
    '''
    #Types can be of multiple type, using if/else not possible
    #type will always a key
    tp_type = third_party['type']
    if tp_type[-1] == 's':
        type_execution = tp_type + '_s'
    else:
        type_execution = tp_type + 's'
    #How to destructure dictionary into eval command as function arguments
    eval_statement = "inital.constant.command" + type_execution +  ".create("**third_party")"
    eval(eval_statement)

Comment: **dont' use eval here**. `eval_statement = "inital.constant.command" + type_execution +  ".create("**third_party")"` Use `getattr`.
Answer: Short answer: there's no syntactical sugar for what you're trying to do, as far as I'm aware.
Here's one way you can achieve your desired result though:
args = ", ".join(f"{k}={v}" for k, v in third_party.items())
eval_statement = f"inital.constant.command{type_execution}.create({args})"

That said, it's not really clear why you need to use eval(). Note that using eval() is generally considered bad practice so I strongly recommend reconsidering your approach to this problem.
For instance, if third_party["type"] returns the actual name of an attribute of the initial.constant object, whatever it is, you could simply do
exec_type = third_party["type"]
func = getattr(initial.constant, f"command{exec_type}")
func.create(**third_party)

Here's a simple demo:
In [1]: class A:
   ...:     def f(self, x):
   ...:         return x**2
   ...:
   ...:     def g(self, x):
   ...:         return x**3
   ...:

In [2]: a = A()

In [3]: getattr(a, "f")
Out[3]: <bound method A.f of <__main__.A object at 0x0000019A291D3790>>

In [4]: f = getattr(a, "f")

In [5]: f(3)
Out[5]: 9

But without knowing what your data look like, and what the objects you're working with look like I can't say for sure.
Comment: Having all methods store in dictionary is better than if/else.
But these type are around 50. Want to know if some thing better exists.
Comment: Yeah, I mean assuming you have a limited number of methods, it's vastly more superior than needing to use `eval()`.
Comment: Aye, that downvote was me. The eval just shows the user how to shoot themselves in the foot. Building dictionary is not helpful in the case the  are unknown ahead of time (or there are very many of them), i.e. that is not a truly dynamic solution. The obvious dynamic approach that your answer is missing is using `getattr` for the "type_execution". But actually, I don't think this question should be answered at all (yet) because it has a syntax error and isn't clear.
Comment: It's clear that `third_party['type']` is string (since it's added to another string) and that's the one that matters, the usage would be like `getattr(inital.constant, "command" + type_execution)`. .... annnnd you deleted the comment that I was replying to :)
Comment: But it's also clear that the values that `third_party["type"]` takes on don't necessarily correspond 1:1 with an attribute, since OP is adding `_s` in some cases. Either way, I've added exactly what you're talking about in my latest edit.
Comment: Well yeah that's why I said to do `getattr(inital.constant, "command" + type_execution)` and not `getattr(inital.constant, "command" + third_party['type'])`. There is no harm in leaving that existing if statement in there.


Wronskian of a fundamental set of solutions

Question: (instead of the dot above, i used ' and ", am I correct in thinking that these are equivalent?)
Consider the system of equations,
$$x'_1=x_2$$
$$x'_2=-q(t)x_1-p(t)x_2$$
where $q(t)$ and $p(t)$ are continuous functions on all of the real numbers. Find an expression for the Wronskian of a fundamental set of solutions.
I know what a wronskian is,  $W(t)=detM(t)$ but I guess I am confused about how to find the fundamental set of solutions.
I was looking at a similar example in the book but I couldn't quite follow it either:
The Euler differential equation with nonconstant coefficients $t^2y"-2ty'+2y=0$ has 2 solutions $y^1(t)=t$ and $y^2(t)=t^2$. (now i realize this might be obvious, but is the superscript number just symbolizing "first" solution and "second" solution, not actually squaring?) This second order differential equation corresponds to the first-order system of differential equations $x'_1=x_2$ and $x'_2=\frac{2}{t^2}x_1+\frac{2}{t}x_2$ (so I don't understand how they got these equations?)
The preceding scalar solutions correspond to the vector solutions $x^1(t)=(t,1)^T$ and $x^2(t)=(t^2,2t)^T$ (what is this T?? and how did they get these answers?)
Answer: Yes, there are two solutions, $y_1(t)$ and $y_2(t)$.
$T$ = Transpose
To write the DEQ as a system of first order equations, we let:
$x_1 =y $, so:
$$x_1' = y' = x_2$$
$$x_2' = y'' = \dfrac{2 y'}{t} - \dfrac{2 y }{t^2} = - \dfrac{2 x_1}{t^2} + \dfrac{2 x_2}{t} $$
To solve the Euler-Cauchy equation, we let:
$$y = t^m \implies y' = m t^{m-1} \implies y'' = m(m-1)t^{m-2}$$
Substituting this into the DEQ, yields:
$$(m^2-3m+2)t^m = 0 \implies m^2 - 3m + 2 = 0 \implies m_1 = 1, m_2 = 2$$
Thus, we have two solutions:
$$y = y_1(t) + y_2(t) = c_1 t + c_2 t^2$$
Comment: How did you get the line $y"=" I don't understand where the t is coming from
Comment: You have $t^2y"-2ty'+2y=0$. Solve this for $y''$, which results in $y'' = (1/t^2) (2 t y' - 2y)$. Clear?


How do I write a mathematical equation for ARIMA (2,1,0) x (0,2,2) period 12

Question: I would appreciate if someone could help me write the mathematical equation for the seasonal ARIMA (2,1,0) x (0,2,2) period 12. I'm a little confused with how to go about this. I would prefer an equation involving $Y_t , e_t, θ$ and $Θ$.
Answer: $$(1-\phi_1B-\phi_2B^2)(1-B)(1-B^{12})^2Y_t=(1+\Theta_1B^{12}+\Theta_2B^{24})e_t$$
$$(1-\phi_1B-\phi_2B^2)(1-B)(1-2B^{12}+B^{24})Y_t=(1+\Theta_1B^{12}+\Theta_2B^{24})e_t$$
$$(1-\phi_1B-\phi_2B^2-B+\phi_1B^2+\phi_2B^3)(1-2B^{12}+B^{24})Y_t=(1+\Theta_1B^{12}+\Theta_2B^{24})e_t$$
$$(1-\phi_1B-\phi_2B^2-B+\phi_1B^2+\phi_2B^3-2B^{12}+2\phi_1B^{13}+2\phi_2 B^{14}+2B^{13}-2\phi_1B^{14}-2\phi_2B^{15}+B^{24}-\phi_1B^{25}-\phi_2B^{26}-B^{25}+\phi_1B^{26}+\phi_2B^{27})Y_t=(1+\Theta_1 B^{12}+\Theta_2B^{24})e_t$$
$$Y_t-\phi_1 Y_{t-1} -\phi_2 Y_{t-2}-Y_{t-1}+\phi_1Y_{t-2}+\phi_2Y_{1-3}-2Y_{t-12}+2\phi_1Y_{t-13}+2\phi_2Y_{t-14}+2Y_{t-13}-2\phi_1Y_{t-14}-2\phi_2Y_{t-15}+Y_{t-24}-\phi_1Y_{t-25}-\phi_2Y_{t-26}-Y_{t-25}+\phi_1Y_{t-26}+\phi_2Y_{t-27}=e_t+\Theta_1 e_{t-12}+\Theta_2 e_{t-24}$$
Comment: was this what you wanted?