Pattern identification in time series data

Question: Imagine a set of users who always perform the same set of tasks and in the same order. Say - user A - enters the store, reads the newspaper, looks at the TV, orders coffee, eats a donut and leaves. User B, enters the store, orders coffee, reads a magazine, leaves and eats a donut.
However, what we see are only user actions/activity and user identity isn't captured. So we see events like - someone entered the store, read the paper, ordered coffee, ordered coffee, reads a magazine, eats a donut, leaves, leaves, eats a donut.
Given the activity log, is it possible to re-thread actions to identify recurring patterns that would give us users? Obviously, we see the same "user" over and over, billions of times in the data set so the pattern repeats but is interspersed with activity of other users.
What I want to predict is - given an event at time $t_i$, does the event fit a known user's pattern/order of events or is this part of a new pattern we have not seen before.
I bit more formally, if our inputs are 1......n, the set of possible
events generated A....Z (an actual sequence of events generated by an
input would be a permutation with repetition), and X~i~ denotes an
event where X is an event for the ith input, then the sequence of
events generated by say,

input 1 is:

A1, B1, E1, X1, Y        1

    input 2:
    C2, D2, A2, X2

    input 3:

Z3, D3, N3, E3, Y        3

    If all inputs – 1, 2 and 3 are triggered simultaneously or in close
    succession, the events could occur in this order:

A1, B1, C2, D2, E1, Z3, D3, N3, X1, Y1, A2, X2, E        3, Y3

    But what we observe as event log:

    A, B, C, D, E, Z, D, N, X, Y, A, X, E, Y

The idea is to take the observed events from the log, and identify what
input or sequence of events does a given event belong to.
While I am not a mathematician or statistician, I have tried to research
or apply the following algorithms but failed to find a match.
Fourier transform : I tried to transform the events into some sort
of signal that can then be decomposed into individual components/inputs.
However, while each input does repeat but interval between successive
repetitions isn't fixed. Also, the time delta between successive events
in the same sequence isn't fixed either – we just know the order is
fixed.
Motif discovery : My understanding is that for motif discovery –
each sequence of events appears in order and together in a dataset so
events from different sequences are not interspersed. Also, looks like
sequence of events are the same size which is not the same with my use
case.
Hidden Markov Model : First, the observations have to be equally
spaced along time. That isn't the case with my dataset but I can flatten
the time stamps and assume all events are at equal and repeated time
intervals. However, the same hidden process does not generate our
observations and events have dependencies – that is – for say, input 1,
B1 will only happen after A1.
Frequent Pattern Mining : Intuitively, I understand these are more
for identifying events that occur/appear together, at a given time.
Also, order isn't important for these patterns because, for example, if
milk and eggs are always bought together then it doesn't matter which
one is bought first, the other will follow.
Few others like Association rules or Random forests – however these seem
to rely on events leading up to a result of some sort – which again
doesn't match my use case since I do not have any results/marker
generated by a sequence of events.
Comment: Do you have training data with user labels for events? Or is this more [motif discovery](https://en.wikipedia.org/wiki/Sequence_motif#Computational_discovery_of_motifs)?
Comment: Sorry, don't have any labeled data. Looking up and reading about motif discovery now. Thanks.
Answer: If you don't have training dataset and don't have a result set that you want to get but rather understand a pattern, clustering algorithms might be your best bet. There are many clustering algorithms with pros and cons of each but you would be able to feed the data in and you'd get a clusters based on distance. Since it seems that you only have categorical data, you might need to use something like k-modes algorithm which would give you a heuristic solution.
Comment: I have used KMeans and Gaussian Mixture Modeling (from Spark's ML library) but best I understand, a clustering algorithm with cluster all the _A_ s, _B_ s and other event types together. It is not going to find any pattern in sequence or occurrence of these events - nothing to do with whether the events have categorical or numerical attributes.


Adicionando valores em tabelas existentes com RMySQL

Question: Eu tinha feito antes uma pergunta sobre banco de dados Mysql em usando a library RODBC e devido a um erro nos tipos,eu vi em uma das respostas me falaram sobre a library RMySQL e resolvi experimentar ela.
Porém eu não estou conseguindo adicionar tabelas data.frame em uma tabela já existente.
Este é a tabela que fiz no mysql:
CREATE TABLE teste ( codigo varchar(5), nome varchar(5) );
+--------+------------+------+-----+---------+-------+
| Field  | Type       | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+
| codigo | varchar(5) | YES  |     | NULL    |       |
| nome   | varchar(5) | YES  |     | NULL    |       |
+--------+------------+------+-----+---------+-------+

E este é o código que estou fazendo:
library(RMySQL)
#Criando conexão
con = dbConnect(RMySQL::MySQL(),host = "localhost" ,dbname = "Banco",username= "****", password= "****")

#Listando tabelas
dbListTables(con)

#Elementos que serão adicionados
x = list("co1","nome1")
y = list("co2","nome2")

z = rbind(x,y)
colnames(z) = list("codigo","nome")
rownames(z) = NULL

z = data.frame(z)
#==============================

#Escrevendo elementos na tabela
dbWriteTable(con, name="teste", value=z ,overwrite = TRUE,row.names = FALSE)

Mas quando eu executo a ultima linha (escrever) o meu banco cria uma nova coluna e todos os tipos viram 'text':
+-----------+------+------+-----+---------+-------+
| Field     | Type | Null | Key | Default | Extra |
+-----------+------+------+-----+---------+-------+
| row_names | text | YES  |     | NULL    |       |
| codigo    | text | YES  |     | NULL    |       |
| nome      | text | YES  |     | NULL    |       |
+-----------+------+------+-----+---------+-------+

Como que eu poderia resolver isso e manter os tipos do mysql?
Comment: A resposta de Eduardo Leoni de certo?Se não monte a instrução sql diretamente.
Answer: Coloque overwrite=FALSE. Você está sobreescrevendo a tabela, por isso perde os tipos.


Use character string as options for plotting

Question: Say I have the different options for plotting saved as a character. For the sake of this question, let's assume I have received tons of these from elsewhere.
option1 = "type = 'p', col = 'red', pch = 19, cex = 2"
option2 = "type = 'l', lty = 2, lwd = 2, col = 'blue'"

I suppose I'd have to parse them and identify the different options. But before I do that I was wondering if there is a way to use them directly when plotting.
Here is a code that doesn't work.
#Data
set.seed(42)
x = rnorm(20)
y = rnorm(20)

plot(x, y, option1)
plot(x, y, option2)

Answer: How about this:
eval(parse(text = paste0("plot(x, y, ", option1, ")")))
eval(parse(text = paste0("plot(x, y, ", option2, ")")))

Answer: You could construct the plotting call as a string and evaluate its parsed form:
eval(parse(text = paste0("plot(x,y,",option1,")")))


FULLTEXT search SQL

Question: table structure (type : MyISAM)
    user_name              varchar(30)
function_type              varchar(30)
function_name              varchar(30)
function_description       text

FULLTEXT :
KEYNAME:             TYPE:                          FIELD:
PRIMARY BTREE       BTREE                          function_name
full_text           FULLTEXT                       function_description

AND i have these values on the column "function_description" :
function_description:

akram lofaif
that is das
akram is blum test
adnan is fine

And when i tried to get back some values like this :
SELECT * FROM functions
     WHERE MATCH (function_description)
     AGAINST ('akram' IN NATURAL LANGUAGE MODE)

it did give back NOTHING !
Answer: The documentation for Fulltext Indexes states, that a fulltext search for a word yields no results, if the word appears too often:

A word that matches half of the rows in a table is less likely to
  locate relevant documents. In fact, it most likely finds plenty of
  irrelevant documents. We all know this happens far too often when we
  are trying to find something on the Internet with a search engine. It
  is with this reasoning that rows containing the word are assigned a
  low semantic value for the particular data set in which they occur. A
  given word may reach the 50% threshold in one data set but not
  another.

If you're just trying it out, add some more data to your table, where the word akram is not included, in order to reduce the number of rows identified.
If this should be your production table, you might want to try the BOOLEAN mode, which does not follow the natural language limitation:
SELECT * FROM functions
     WHERE MATCH (function_description)
     AGAINST ('akram' IN BOOLEAN MODE)

FULLTEXT search SQL


Saxophone upper register adjustment

Question: As is common for a lot of instruments from the saxophone family, the upper register (notes played with the octave key pressed) on mine is a bit high (by about 10-15 cents). I practise a lot with a tuner to make sure that everything is within range. I have recently discovered that I can fix the misalignment by simply changing airflow direction: if I blow downwards by about 15 degrees as opposed to blowing straight whenever I play a note requiring the octave key, the 10-15 cents sharpness goes away.
What I am curious about is whether it is a good practice that I should adhere to. I am making no embouchure changes whatsoever, simply changing the airflow's direction. How does it fare in the long run? If I change instruments, if I use a different instrument from the saxophone family?
Answer: It is common for saxophones to go sharp in the higher register because of the conical shape of the instrument. Saxophonists do learn to adjust their embouchure to up for this. (Changing the airflow IS changing the embouchure. You must change the position of your soft palate and tongue in order to change air direction.)  However, it is important to make sure there is not an inherent problem with your saxophone, so I would first take it to a shop.
Additionally, a different mouthpiece can do wonders. I was having problems with intonation with my saxophone. I went shopping for a new mouthpiece and bingo, the problem was solved and I had to do no more than the minor adjustments for pitch. Some saxophonists find they need a different neck, but whether this is an option for you depends on the brand/model of saxophone you are using.
Also make sure you have a good quality reed and ligature, and that the reed strength is appropriate for you. All of these things contribute to intonation.
Comment: @Pyromonk, no one can tell you whether or not your set-up works for you. Each person is unique and will respond differently to different reeds, mouthpiece bores and apertures, etc. One reason there are so many to choose from! You can't decide from looking at a picture or knowing a name. These things must be tried out. If you have any questions, make an appointment with a good shop and try out some things. Do you have a teacher? Someone watching you play may know if there is something that changes in your physical playing to cause notes to go out of tune.
Comment: Also, saxophones are extremely sensitive to being bumped. It could be that a key is no longer covering a hole exactly right.
Comment: Thank you, Heather, as always. I did not know the notion of embouchure encompassed airflow. I thought embouchure was the positioning of the muscles, the jaw and the throat (the physicalities). This is a professional-level instrument that is 1.5 years old and has recently been serviced. It's in great condition. I do not want this to become a shopping question, but for curiosity's sake I will mention the components in a separate comment (running short here).
Comment: I am playing classical and jazz on an unlacquered Trevor James Signature Custom. The mouthpiece in use is 10MFAN Merlot (8*), the reeds are Rigotti Jazz Cut (3.5), and the ligature is a silver-plated François Louis. I haven't had any trouble with intonation or anything with this setup. The slight deviation in the upper register only came about around 1 year ago. There might've been some change to my embouchure since then as well.
Comment: Thank you very much, Heather! Yeah, I know. I just mentioned the exact brands and sizes in case you happened to have tried them out. I do have a teacher, and he couldn't find any problems with my playing or the instrument during the last lesson.

Saxophone upper register adjustment


Existence of a transversal map prevents density?

Question: Let $S$ be a $C^{\infty}$-submanifold of $N$ and suppose that $N-S$ is dense in $N$, where $M,N$ are $m$ and $n$ dimensional $C^{\infty}$-manifolds, respectively.
In this post the answering poster makes the following arguments:
If $S$ is of codimension $\leq m$ in $N$, then exists $C^{\infty}$ smooth map $f:M \to N$ which intersects $S$ transversely in at least one point.  So $f$ cannot be in the $C^0$-closure of $C(M,N')$, where $N'=N-S$.
Why is this true?  I've never had to use any intersection theory before so the argument has left me at a loss of direction.
I've been reading and this may have some link with this result?  But its still not clear to me.
Answer: The terminology is a bit odd: One says that two submanifolds intersect transversally or that a map is transversal to a submanifold. The terminology that a map intersects a submanifold (transversally) is nonstandard.
Option 1: What's meant is that there exists $p\in M$ such that $f(p)\in S$ and $f$ is transversal to $S$ at $p$.
Option 2: What's meant is  that there exists a point $q\in S$ such that $q\in f(M)$ and $f$ is transversal to $S$ at every $p\in f^{-1}(S)$.
The existence of $f$ holds regardless, just one needs to do more work in the case of Option 2.
The first step is simple linear algebra. Suppose that $W$ is a linear subspace of ${\mathbb R}^n$ whose codimension is $\le m$. Then there exists an $m$-dimensional linear subspace $V\subset {\mathbb R}^n$ transversal to  $W$. (Their intersection contains the origin, of course.)
Now, since $S$ is a submanifold in $N$, we can take a point $q\in S$ and local chart
$$
\psi: U\subset N\to {\mathbb R}^n
$$
such that $\psi(S\cap U)$ is an open neighborhood of $0$ in a linear subspace $W\subset {\mathbb R}^n$ and $\psi(q)=0$.
Then $W$ has codimension at most $m$.
Take $V\subset {\mathbb R}^n$ as above. Since $M$ is $m$-dimensional, there exists a local chart $\phi: M\to V$ sending the given $p\in M$ to $0$. Extend $\phi$ to a smooth map $\eta: M\to V\cap \phi(U)$ (after shrinking the open neighborhood of $p\in M$ appropriately). Now, compose $\eta$ with $\phi^{-1}$ and obtain a smooth map $f: M\to U$
which is transversal to $S$ at $p$. This takes care of Option 1.
To deal with Option 2, one first the following lemma (I leave you a proof as an exercise):
Lemma. Let $M$ be an $m$-dimensional manifold. Then there exists a smooth map $\theta: M\to D^m$ to the open disk $D^m\subset V={\mathbb R}^m$, such that $0\in \theta(M)$ and $0$ is a regular value of $\theta$.
Use this map $\theta$ as $\eta$ in the above proof.
Edit.
Proposition.  Suppose that $f: M\to N$ satisfies Option 1. Then $f$ does not belong to the closure of $C(M,N')$ equipped with topology of uniform convergence on compacts, where $N'=N-S$.
Proof. By density of smooth maps, it suffices to show that $f$ cannot appear as a limit (uniform on compacts) of a sequence of smooth maps $f_i: M\to N'$.  (Here I equip $N$ with some auxiliary Riemannian metric.)
The proof is a variation on the standard transversality arguments which you can find, for instance, in "Differential Topology" by Guillemin and Pollack.
Pick a submanifold $M'\subset M$ of dimension equal to codimension of $S$ in $N$, containing $p$ and such that $f: M'\to N$ is transversal to $S$ at $p$. Restricting $f$ (and a sequence $(f_i)$) to $M'$, the problem is reduced to the case when $m=dim(M)=codim_N(S)$.
Let $U$ as above be a coordinate neighborhood of $q=f(p)$ in $N$; we can assume that the closure of $U$ is diffeomorphic to a closed $n$-ball in ${\mathbb R}^n$.  Then (by taking $U$ sufficiently small) we can also assume that there exists a compact codimension 0 submanifold with smooth boundary $\Omega\subset M$ such that $f(\partial \Omega)\subset \partial D$,
$f(\Omega)\subset D$ and
$$
\{p\}= f^{-1}(S)\cap \Omega.
$$
For all sufficiently large $i$, each map $f_i|\Omega$ are homotopic to $f|\Omega$ by the "straight-line homotopy" $H_i: \Omega\times [0,1]\to N$ with $H_i(x,0)=f(x), H_i(x,1)=f_i, x\in \Omega$.
For the sake of a contradiction, I will assume that $f_i(\Omega)\cap S=\emptyset$.  Then, WLOG (by replacing the straight-line homotopy by its small smooth perturbation), we can assume that each $H_i$ is transversal to $S$. In particular, each
$$
H_i^{-1}(S)
$$
is a compact 1-dimensional submanifold $s_i$ in $\Omega\times [0,1]$.
When $i$'s are sufficiently large, $H_i(\partial \Omega \times[0,1])\cap S=\emptyset$. Therefore, the boundary of $s_i$ is contained in $int(\Omega)\times \{0\}$. This boundary is nonempty since $p\in \partial s_i$. Hence, there exists  $p'\in \Omega -\{p\}$ such that $f(p')\in S$. A contradiction. qed
Comment: @AnnieTheKatsu: It is a transversality argument. But first, you should revise the question and explain what $N'$ is.
Comment: but why does this map fail to be in the $C^0$ closure of $C(M,N')$ then?


Integral of $\frac{(x^2+1)}{(x^2-2)(x+1)^3}$?

Question: I was trying to do integral of $$\frac{x^2+1}{(x^2-2)(x+1)^3}$$ starting off by simplifying the equation into something like:
$$\frac{A}{(x^2-2)} + \frac{Bx + C}{(x+1)^3}$$ and then determine A, B and C.
I notice that $(x+1)^3$ can still be done by breaking the $(x+1)^3$ down itself, but when it is something like by the power of 4 or 5 or something beyond, it becomes too complicated to be done.
I would like to ask if there is any easier way for me to break the equation down and do the integral. Thanks
Comment: For such integrals, the normal approach is to "roll up your sleeves".
Comment: I would start by asking Wolfram Alpha. You can use "partial fraction (x^2+1)/((x^2-2)*(x+1)^5)"
Comment: @Jam It is power of 3
Comment: I'm voting to close this question as off-topic because is just a homework
Answer: Using partial fraction decomposition, just write
$$\frac{x^2+1}{(x^2-2)(x+1)^3}=\frac A{x+1}+\frac B{(x+1)^2}+\frac C{(x+1)^3}+\frac {Dx+E}{x^2-2}$$ Reduce to common denominator to get
$$x^2+1=A(x+1)^2(x^2-2)+B(x+1)(x^2-2)+C(x^2-2)+(Dx+E)(x+1)^3$$ Expand and identify the coefficients.
A bit tedious but not difficult.
Answer: You should write $\dfrac{x^2+1}{(x^2-2)(x+1)^3}$as$$\frac A{x+\sqrt2}+\frac B{x-\sqrt2}+\frac C{x+1}+\frac D{(x+1)^2}+\frac E{(x+1)^3},$$which is equivalent to solving the system$$\left\{\begin{array}{l}-\sqrt2A+\sqrt2B-2C-2D-2E=1\\-3\sqrt2A+A+3\sqrt2B+B-4C-2D=0\\-3\sqrt2A+3A+3\sqrt2B+3B-C+D+E=1\\-\sqrt{2}A+3A+\sqrt2B+3B+2C+D=0\\A+B+C=0.\end{array}\right.$$It turns out the the solution is$$(A,B,C,D,E)=\left(\frac34\left(10+7\sqrt2\right),-\frac34\left(-10+7\sqrt2\right),-15,6,-2\right).$$Can you take it from here?


Optional notes in baroque clavier score?

Question: I'm not a keyboard player so I'm not sure what the smaller notes in this score mean. Are they optional like an ossia or something to be played on a repeat or something else?
Answer: These are notes not present in the autograph, but suggested by the editor. The original score probably consisted only of the bass and the melody (with the melody having some chords at the end of phrases, which is quite a common thing for the period). So I suppose the editor suggested some additional notes to make the harmony less sparse.
I do not know anything about the piece, whether it was conceived as a keyboard piece or as a piece for a melody instrument with a continuo. As such it is hard to really estimate if these notes "should" be played. In the latter case it would not have been uncommon to have instruments playing improvised harmonic parts. In the first case probably not so much.
So we need to be clear: The small notes are not the original composer's notation, but the editor's notation. And to consider whether to play them is what the composer wanted would require more information on the piece and also historic performance practice, which I am not an expert in.
So as it goes: Play the notes if you do not care about that and you like them. If you do care about such stuff you'll need to start reading up on historical performance practice of that particular time and area as well as the actual instrumentation of the piece.
Answer: The small notes are the editor's realization of a figured bass. At the time John Blow was composing, it was common that keyboard parts would be written as a bass line with figures indicating how the harmonies progressed. Below is an example from Blow's With Cheerful Hearts, mm. 11–12.

(Image source: IMSLP)
Comment: Maybe, but is it an arrangement of a melody and bc or is a keyboard solo work? In the latter case I would find it unprobable to be a realization of bc.


binary to buffer in C

Question: I'm trying to fix this problem, but i dont know why it doesn't work! In my main function I'm getting the name of the binary file (argv[1])! Now I'm trying to get the content into a char Array (Buffer) to handle the content in another function. But i dont know why I get a 0 when i printf "readed" and why theres is no output when i printf the buffer!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//only allowed libaries

int readBin (char *bin_file_name, char *txt_file_name)
{

  int readed = 0;
  FILE *input = fopen(bin_file_name, "rb");
  int file_size = 0;
  char *buffer;

  //get Filesize
  fseek(input, 0, SEEK_END);
  file_size = ftell(input);
  rewind(input);

  //Allocate memory for buffer
  buffer = (char*) malloc(sizeof(char)*file_size);

  //Fill Buffer
  readed = fread(buffer,file_size,0, input);

  printf("%i\n", readed);
  printf("%s\n", buffer);

  return 0;
}

Thank you for your help!!!
Comment: You should check to make sure that your file is actually being opened. Simple if statement that opens the file like `if( (input = fopen(bin_file_name, "rb")) == NULL){ printf("unable to open file"); exit(1);}
`
Answer: readed = fread(buffer,file_size,0, input);

should be
readed = fread(buffer,file_size,1, input);
//                              ^

Your current code requests to read 0 blocks of file_size bytes.
You still won't be able to print the contents of file_size.  The %s format specifier expects a null terminated string but file_size is a block of binary data.  You'll need to allocate one byte extra and add a \0 in that extra byte if you want to have a try at printing as a string.  Or you could iterate through the memory using the %02x format specifier to print as hex instead.
As an aside,
buffer = (char*) malloc(sizeof(char)*file_size);

would be better as
buffer = malloc(file_size);

(You shouldn't cast the return from malloc and sizeof(char) is guaranteed to be 1)
and you should free this memory later in your program.
Comment: Thank you! Works so far! Now I can see an unreadble char! But there should be more chars!
Comment: Ok! Thank you! So far i understand that this problem only happens, when i try to printf it, but in my buffer everything is, like it should be! So it should be possible to handle all the chars in the buffer, because the task isn't to print it (I've just done this, to look if it works), the task is to write it in an own txt file!
Comment: That's correct, the contents of your buffer are fine.  They're just not suitable for viewing as a string.

binary to buffer in C


Operator '&&' cannot be applied to operands of type 'bool' and 'bool?'

Question: Trying to read a dataContext class like this
var users = new List<User>();
var roles = new int[] { 1, 2 };
// here I need to get users who's role is in role (1, 2), hence above line
// and also I need to get user who's isValid field true, please note that isValid is SQL 'bit' field and is nullable
//This is what I am doing
foreach (var user in dataContext.Users
                                .Where(u => roles.Contains(u.RoleID.Value)
                                            && u.isValid ?? false == true)) // here '&&' part I'm struggling and getting above error
{
    users.Add(new User()
    {
         // Users Added in collection
    });
}

So the question is in where clause I need to get user's who are in role(1,2) && isValid == true, if isValid is 'null' make it false.
Thanks
Comment: You have to remove possibility of a null on either side of the operand, whether you do it by wrapping it in parenthesis or some other method--> .Where(u => roles.Contains(u.RoleID.Value) && u.isValid == true) should also work.
Answer: You have to wrap it in parentheses:
roles.Contains(u.RoleID.Value) && (u.isValid ?? false)

bit of confused with (u.isValid ?? false), does this not mean that if
  u.isValid == null then make it false and look for users where
  u.isValid is false, this is not what I want.

No, it just means that nulls are treated as false and that all users are taken which isValid is neither null nor false. It works because the ??-operator converts the Nullable<bool> to a bool, so you can use it with &&. I don't like it, i prefer explicit code that i understand later:
roles.Contains(u.RoleID.Value) && u.isValid.HasValue && u.isValid.Value

or simpler by using the ==-operator with the bool?:
roles.Contains(u.RoleID.Value) && u.isValid == true

Comment: But lose the `== true`
Comment: bit of confused with (u.isValid ?? false), does this not mean that if u.isValid == null then make it false and look for users where u.isValid is false, this is not what I want.
Comment: Thanks Tim. This means filter if u.isValid == true and if u.isValid is null filter u.isValid == false. Is that correct?
Comment: If this is correct then I don't want this behaviour, I want to return only if isValid == true. Since isValid is nullable, I wanted to make it false to avoid error and return only records where isValid is true.
Answer: Given bool will default to false anyway you could use GetValueOrDefault
roles.Contains(u.RoleID.Value) && u.isValid.GetValueOrDefault()

Note - GetValueOrDefault does not appear to be supported by EF - see ticket
Comment: This is particularly nice, because `GetValueOrDefault()` is a bare field read with no extra logic.
Comment: `GetValueOrDefault()` is not translated into SQL by EntityFramework
Comment: @AlexG that was the only doubt in my mind - can you confirm that this is definitely the case?
Comment: [Here's the issue ticket](http://entityframework.codeplex.com/workitem/112)
Comment: @AlexG thanks, however, the question itself is more related to the compiler error rather than the runtime one that would be thrown by EF (although, ultimately this will be the case if the OP uses this code). I will leave the answer but update just to point out that the support does still appear to be lacking for EF (although funnily enough I think this *is* supported by L2S).
Answer: You can try  like this.
 foreach (var user in dataContext.Users
          .Where(u => roles.Contains(u.RoleID.Value)
             && (u.isValid ?? false))) // note the bracket around (u.isValid ?? false)

Or
foreach (var user in dataContext.Users
       .Where(u => roles.Contains(u.RoleID.Value)
        && (u.isValid.HasValue && u.isValid))) //you can use HasValue to check for null

Comment: Thanks @sudhAnsu63, but i don't want to filter where u.isValid == null. I want to get records where u.isValid == true and if u.isValid is null, (to avoid compile error, convert this to null to false)
Comment: but return only  records where u.isValid == true
Comment: I have updated my answer(2nd One).  1stOne was already working as expected.
Comment: Thanks, what does this mean in your 1st answer (u.isValid ?? false), does this not mean that if u.isValid == null then make it false and look for users where u.isValid is false, this is not what I want
Comment: null-coalescing operator. It returns the left-hand operand if the operand is not null; otherwise it returns the right hand operand.
Comment: @sky: No, it doesn't mean look for users where `u.isValid` is false.  Just as `if (u.IsValid)` looks for users where `u.IsValid` is true, `if (u.IsValid ?? false)` means "look for users where `u.IsValid` is true (and treat null the same as false)"  That's all the `??` does, it provides an instruction to treat null the same as some other value.  It doesn't invert the overall meaning of a filter condition.


Section of a vector bundle as a submanifold

Question: I am currently working on one part of a problem surrounding sections and submanifolds.
Given a real vector bundle $\pi: E\rightarrow M$ of rank k, with a smooth global section $s:M\rightarrow E$, can the section be considered a submanifold of $E$ under its graph parametrization? Namely, for an open set $U\subseteq M$, consider $\Gamma(s)=\{(p,v)\in M\times \mathbb{R}^k|p\in U, v=s(p)\}$, which is a properly embedded m-dimensional submanifold of the product space $M\times \mathbb{R}^k$.
But the vector bundle itself has a local trivialization $h:\pi^{-1}(U)\rightarrow U\times \mathbb{R}^k$ which is a diffeomorphism from its open sets ($\pi^{-1}(U)$) to open sets in the product space($U\times \mathbb{R}^k$). Can one piece together a submanifold of $E$ using these local trivializations?
I am asking all this because, the problem I am working on involves showing that for a compact manifold, where the rank of the bundle is greater than the dimension of the space, has a non-vanishing section. Since the bundle $E$ has dimension $m+k$, and the zero section is a submanifold of dimension $m$, my plan was to try and use transversality to show the two sections can not intersect due to dimensional considerations. Also unclear on where I might use compactness so I'm still struggling.
Answer:
Yes. The image of a (sufficiently smooth) section is generally a submanifold. But it might not be nice. Consider the trivial line bundle over the reals, which I'll visualize as $\mathbb R^2$, with projection $(x, y) \mapsto x$. Then a section really is just the graph of a function on the reals. When you think about things like $y = |x|$ and $y = x^3$ and $y = x^{3/2}$, you realize how awkward things can be. You clearly need some smoothness conditions.
Your proof sounds as if it's headed in exactly the right direction. There's a small difficulty: if you start wtih the zero-section, $z$, and another section, $s$, and make them transverse by adjusting $s$, will the perturbed $s$ still be a section? The perturbation might move it so that more than one point of the image of $s$ happens to lie in a single fiber. Back to the trivial $R^2 \to R$ example: suppose we have the section $s(x)  = x^{(1/3)}$, i.e., $x = y^3$. You can perturb that to $x = y^3 - \epsilon y$, and now it's no longer a section.

So you need to apply transversality to a fiber-by-fiber perturbation; that may take a little more work (or not...I haven't done this in 30 years!)
Comment: Why would $x=y^3-\epsilon y$ not be a section in the above case? Is it because composing the the projection map with this function is not the identity on that fibre (due to the graph intersecting the x-axis multiple times)?
Comment: right: if you graph y = x^3 - x, the $x$-axis intersects the graph at $x =-1, 0, 1$; rotate that 90 degrees, and you have the situation I described. (The epsilon is in there to show that you can get this problem with a *small* perturbation).
Comment: Additionally, can this be given as a submanifold of a non-trivial bundle? In my case, smoothness is assumed.
Comment: I don't understand your question; certainly you can embed the key features of what I described into a nontrivial bundle -- the problem I described was purely local.


integrating complex functions

Question: I need to integrate $$\int\limits_0^{\frac{\pi}{2}} e^{t+it} dt$$. This is equal to $\int\limits_0^{\frac{\pi}{2}} e^t \cdot e^{it} dt$ However I'm not sure if I can substitute $e^{it}=\cos(t)+i\sin(t)$ and then solve the integral.
Comment: Treat $i$ as a normal constant.
Comment: Why would you not be able to make that substitution?
Answer: Why not just using the primitive method?
\begin{align*}
\int_{0}^{\pi/2}e^{t+it}dt&=\int_{0}^{\pi/2}e^{(1+i)t}dt\\
&=\dfrac{1}{1+i}e^{(1+i)t}\bigg|_{t=0}^{t=\pi/2}\\
&=\dfrac{1}{1+i}(e^{(1+i)\pi/2}-1)\\
&=\dfrac{1}{1+i}(e^{\pi/2}i-1).
\end{align*}
Comment: $e^{(1+i)\pi/2}=e^{\pi/2}e^{(\pi/2)i}=e^{\pi/2}(\cos(\pi/2)+i\sin(\pi/2))=e^{\pi/2}i$.
Answer: The complex number is just like any constant:
$$I=\int_0^{\frac{\pi}{2}} e^{t+it} dt=\int_0^{\frac{\pi}{2}} e^{t(1+i)} dt=\int_0^{\frac{\pi}{2}} e^{Ct} dt$$
Answer: it can be done the way you suggested, however. first evaluate these two integrals:
$$
I_1 = \int_0^\frac{\pi}2 e^t \cos t \, dt = \bigg[e^t \cos t \bigg]_0^\frac{\pi}2 + \int_0^\frac{\pi}2 e^t \sin t \, dt = -1 + I_2
$$
where
$$
I_2 = \int_0^\frac{\pi}2 e^t \sin t \, dt = \bigg[e^t \sin t \bigg]_0^\frac{\pi}2 - \int_0^\frac{\pi}2 e^t \cos t \,dt = e^{\frac{\pi}2} - I_1
$$
so now we have
$$
2I_1 = e^{\frac{\pi}2} -1
$$
and
$$
2I_2 = e^{\frac{\pi}2} +1
$$
now all you need to do is to compute:
$$
I_1+iI_2 = \frac{(1+i)}2  e^{\frac{\pi}2} + \frac{i-1}2
$$
this differs slightly from the value given in a previous answer, because user284331 has slipped in the erroneous assumption that $e^{i\frac{\pi}2} = 1$, whereas that should, of course be $i$. (i make happy assumptions like this all the time ;-)
Answer: Because $e^{it} = \cos(t) + i \sin(t)$ is true, you can substitute $\cos(t) + i \sin(t)$ anywhere where you have $e^{it}$ and vice versa.
Thus you do indeed have
$$\int_0^{\frac{\pi}{2}} e^t \cdot e^{it} dt
= \int_0^{\frac{\pi}{2}} e^t \cdot \left( \cos(t) + i \sin(t) \right) dt
$$

integrating complex functions


Hidden geometrical gems in Euclid's Elements?

Question: I am teaching a course in Euclidean geometry at the University of South Carolina, and it seemed highly appropriate and interesting to read Euclid himself. (See here for a wonderful, and completely free, translation and guide.)
We are up through Proposition 17 now, and although this is very instructional for both the students and myself, I sense that before too long the novelty will wear off and it will be good to return to a modern treatment. (I will, though, want to say a little bit about his treatment of parallel lines and elliptic and hyperbolic geometry.)
That said, there are some gems. In Book 2, Euclid constructs square roots, and in Book 4 he describes how to draw a regular pentagon (which is surely not obvious). And, the constructions in the first few books are all very interesting. (Of course there is lots of fascinating number theory too, but my course is on geometry.)
What other particularly fascinating tidbits do the Elements contain, which it may be easy to overlook?
Comment: To me, the basic experience of reading book I of Euclid is that he proves a series of dull and obvious propositions, and then all of a sudden Prop. 47, the Pythagorean theorem, comes along, and suddenly it becomes amazing how much he's accomplished with so little to work from. I don't think I've ever read every single theorem from 1 through 46 -- it's like reading the "begats" in Genesis. What is your audience? Is this course remedial? Penrose's book The Road to Reality has some nice modern interpretation of the postulates as empirically testable statements about physical space.
Comment: The course is certainly not remedial. Math majors of mixed background, most of them future high school teachers. In my opinion it's been interesting so far, in the same way that point set topology is interesting (you have to reconsider what's "obvious" strictly in terms of what you've proved so far)... but it's beginning to wear thin.
Comment: Since you seem to have quite a free reign over what you teach, what about letting your students rein a bit? Surely it would help if they are more interested in some particular area of geometry or even specific problems, and often there would be relationships to other areas that you can point out along the way. And if anything is really important you should be able to convince your students without much effort that it is really interesting. =)
Comment: @BenCrowell About your last comment's sentence: what pages in the book are you thinking of? (It's a big book.)
Answer: If I were to ever incorporate Euclid's elements in a class, it would be through a critical exposition of Euclid's work, not necessarily directly with his books.
The best source I've gotten into for that is Hartshorne's Euclid and beyond. It really helps you see the importance and impact of Euclid's work, without really having to trudge through it.
As I recall, the gems are strewn along the path of good exposition. The book also introduces Hilbert's axioms, which was the next major revolution of the foundations of geometry that students should really hear about.
It never lingers on Euclid's presentation long enough to become boring. It revisits it throughout the book, of course. Really, there is no reason to linger on Euclid after students have gotten the general feel of the material.
Maybe you can take a look at how Hartshorne presents results, and then devise a plan.
Comment: excellent answer
Answer: For me, I like Book 1 Prop 35, whichis the first use of "equal" to mean equi-areal rather than congruent.
Book 2 Prop 11, where Pythagoas is used geometrically to prove a construction for the golden ratio (later used for the regular pentagon construction)
3:35 again a geometrical use of Pythagoras to prove the "power of a point"
6:3 showing that the angle bisector cuts the base of a triangle in the same ratio as the other sides.
6:19 showing that "similar triangles are to one another in the duplicate ratio of their corresponding sides
6:31 pythagoras generalized and proved using ratios


Insert a collapsible fieldset or region in a panels layout

Question: I created a custom layout for a panels display and I would like to have a region or multiple regions in my custom layout be collapsible with javascript/jquery. Ideally the state of the collapible region would also be remembered by a cookie, but first thing is the collapsible functionality. Can anyone help?
<fieldset class="collapsible collapsed">
<legend>
Title of fieldset goes here.
</legend>

Content goes here.

</fieldset>

Comment: I found your question while seeking the same.  Did you ever find your solution?  I can share what doesn't work:

* looking for fieldset/div as something out of the box in Panels
* hook_form_alter to move fields around (works but ctools throws PHP notice)
* adding fieldset/div (via field_group module) to node in Manage Fields screen
* adding fieldset And scooting some fields in (in manage fields screen)

Any other ideas?  Sorry about the formatting here-- I can't seem to break a line for you.
Answer: The best I've come up with so far is a custom module's hook_form_alter to create a fieldset and move fields in.  Note that in order to avoid something like:
Notice: Undefined index: field_txt_student_id in ctools_entity_form_field_content_type_render() (line 96 of [...]/sites/all/modules/contrib/ctools/plugins/content_types/form/entity_form_field.inc).

because the field is moved and unset(), it is not known by ctools to have been moved (or something).  Its render function does basic data sanity checking but then goes with the assumption that this field still exists by name where it was before the alter.
To get around that, instead of unset()'ing the original field, set it to a non-kind-of-field per this thread.
//  unset($form[$fieldname]);
$form[$fieldname] = array('#language' => NULL);

Would love to hear what other solutions exist!  I spilled my "solutions that don't work" in a comment on your original question.


Finding the difference in time in excel

Question: Hopefully this is a better phrased question...  Ultimately what I'm trying to do is figure out if someone is late or not.  So I have their shift start times... it's in a string format of XXX-####, e.i. Sun-0320.   I also have a timestamp(Macro) of when they clocked in same format as Now()
So currently I have it as
<code>A1 = Now()
B1 = Right(F1,4)
F1 = ShiftCode

C1 = A1-B1
</code>
A1 and B1 are both formatted to custom time of hh:mm
C1 is formatted as numbers. i tried time also
Comment: share a copy of your sheet
Answer: The NOW() function returns both the date and time, which changes as your cell/worksheet/workbook is being updated.
https://support.microsoft.com/en-us/office/now-function-3337fd29-145a-4347-b2e6-20c904739c46
There is a lot of options to add or subtract the time in excel, like here:
https://superuser.com/questions/89045/how-to-get-time-difference-as-minutes-in-excel
https://support.microsoft.com/en-us/office/calculate-the-difference-between-two-times-e1c78778-749b-49a3-b13e-737715505ff6
https://www.ablebits.com/office-addins-blog/2015/06/24/calculate-time-excel/
https://www.excel-easy.com/examples/time-difference.html
https://exceljet.net/formula/calculate-number-of-hours-between-two-times
but regarding your case I would rather use something like this:
instead of
<code>  =NOW()
</code>
expand the formula, by adding another TEXT function and defining the time units for it:
<code> =TEXT(NOW(),"hh:mm:ss")
</code>
Then if you have another cell with a time value, you can easily add or subtract from the cell defined by the aforementioned formula.
Comment: Its a macro doing the now()... what I'm trying to check is if C1 is greater say 10 minutes
Comment: The NOW() is the Excel function, no macro needed.
Comment: While I can use now() as a timestamp function, having it as a recursive formula such as. ... IF(A1="",""",NOW())... and messing with the iterations settings.. the issue is whenever the document is refreshed.. it updates it.. especially if other macros are running also.  Which why the macro is used.
Comment: That's also going off topic.   I just want to be able to compare the two time and see if its greater than say 10 minutes or not.  So I really should do is  A1-B1>10... and I can't seem figure out that part

Finding the difference in time in excel


2 php versions on same server

Question: Is it possible to install 2 php versions on same server.
I have some old sites on my local system which supports php5.
But latest CMS and framework supports php5.3 . I don't want to change my old sites. But want all work perfectly.
So is it possible that I can install both versions or any other way to make workable all?
Comment: This is more of a serverfault question.
Comment: Yes, it is possible, see here: http://stackoverflow.com/questions/524508/how-can-one-run-multiple-versions-of-php-5-x-on-a-development-lamp-server
Comment: Sounds like a perfect opportunity/excuse to learn about Docker if you ask me ;)
Answer: First
You can use something like fastCGI or patch php-fpm for versions up to 5.2.x
For versions PHP 5.3.x < 5.3.3 you can get php-fpm from the php svn like this;
cd php-5.3.x (download and unzip php first)
svn co http://svn.php.net/repository/php/php-src/trunk/sapi/fpm sapi/fpm
./buildconf --force
./configure --enable-fpm ...your other configure options, etc...
make && make install

After that you need to adjust the default config file (installed into $prefix/etc) and run "php-fpm".

(Instructions from http://php-fpm.org/download/)
For php 5.3.3 and later php-fpm is included in the core. So  the only thing you need to do is compile php with --enable-fpm, se more here.
Second
Set up the different versions you need in different paths, and start them.
Third
Then you can point your webserver to use different running php-processes depending on which php version you need.
Answer: It heavily depends on your web server. Apache for example has the concept of "handlers" which can be simply set to different values for your PHP files in the virtual directory configuration or within a .htaccess file like this:
 AddHandler php53-cgi .php

This way, you could set PHP versions per directory easily after you've defined the appropriate Action directives for different PHP versions.

2 php versions on same server


Gatsby Js redirects all pages to index page when the page is opened directly

Question: I have a website in Gatsby Js. I have deployed it to AWS Amplify.
When I try to open a page directly, say /pricing, it is redirecting to the home page. I am able to open the page when I click on any  tag linking to that page.
Following is my build settings for AWS Amplify:
<code>version: 1
frontend:
  phases:
    preBuild:
      commands:
        - npm install -g gatsby-cli
        - npm install
    build:
      commands:
        - gatsby build
  artifacts:
    baseDirectory: public
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*
</code>
Any clue where the issue can be?
Comment: It seems related to some AWS configuration rather than Gatsby but without seeing some implementation code I can be sure
Comment: Are you creating the pages with trailing slash?
Comment: https://www.mangochip.com/privacy If I open it directly it will redirect to home. If I go through link in footer it works properly.
Comment: Thanks for the hint. It is actually an AWS Amplify issue. It was redirecting all the traffic to index.html. I removed the rule and it is working perfectly fine.
Answer: For those who may face the same issue. It has been fixed by double-checking AWS Amplify redirection rules.
Comment: Can you please be more specific? :)
Comment: Each case should be handled specifically. However, the resolution, in this case, was checking the AWS Amplify dashboard, in the rules section, to remove the rule that redirects all the traffic to `/index.html`


Verifying $\sqrt{\frac{2}{3}}\cos\left(\frac{1}{3}\arccos\sqrt{\frac{3^3}{2^5}}\,\right)=\cos\frac\pi5$

Question: I can verify the equation using Excel. But is there another derivation?
$$\sqrt{\frac{2}{3}}\cos\left[\frac{1}{3}\arccos\left(\!\!\sqrt{\frac{3^3}{2^5}}\right)\right]=\cos\left(\frac{\pi}{5}\right)$$
Thank you for help.
Comment: 1) Find a third degree equation verified by $\cos \pi/5$. 2) Solve this equation by the trigonometric method.
Comment: In fact $\cos(\pi/5) =\frac{1+\sqrt{5}}{4}$ which is half the golden ratio.
Comment: Heide, if you put more context in your question, I will write an answer. Please, read [this](https://math.meta.stackexchange.com/questions/9959/how-to-ask-a-good-question) carefully and do it.
Answer: Let $u=\sqrt{\frac{2}{3}}\cos\left(\theta\right)$, where $\theta=\frac{1}{3}\arccos\left(\sqrt{\frac{3^3}{2^5}}\right)$.
By the triple angle formula, we have
$$4\cos^3(\theta) -3\cos(\theta)-\frac{3}{4}\sqrt{\frac{3}{2}} = 0.$$
i.e.  $u$ is a root of the cubic $$q(x) = 6\sqrt{\frac{3}{2}}x^3-3\sqrt\frac{3}{2}x-\frac{3}{4}\sqrt{\frac{3}{2}}.$$
or, after clearing coefficients,
$$ p(x)=8x^3-4x-1.$$
On the other hand, $\cos\left(\frac{\pi}{5}\right)$ is also a root of $p(x)$, as
\begin{align*}
p\left(\cos\left(\frac{\pi}{5}\right)\right)&=8\cos^3\left(\frac{\pi}{5}\right)-4\cos\left(\frac{\pi}{5}\right)-1\\
&= 2\left(3\cos\left(\frac{\pi}{5}\right)+\cos\left(\frac{3\pi}{5}\right)\right)-4\cos\left(\frac{\pi}{5}\right)-1\\
&= 2\cos\left(\frac{\pi}{5}\right)+2\cos\left(\frac{3\pi}{5}\right)-1\\
&= 0.
\end{align*}
Factoring $p(x)$, we have
$$p(x)=(2x+1)(4x^2-2x-1).$$
We see that $p(x)$ has a unique positive root. Since $u$ and $\cos\left(\frac{\pi}{5}\right)$ are both positive, we must have $u=\cos\left(\frac{\pi}{5}\right)$.
Comment: Why didn't I try rational root theorem lol. Nice one


How to evaluate $I = \int_{\partial B} {x_1}^2 dx$ where $B$ is a sphere in 3D?

Question: Let $B$ represent a sphere in 3D with radius $R$. I would like to evaluate the following integral
$$
I = \int_{\partial B} {x_1}^2 dx.
$$
That is, each point on the surface of the sphere is represented by the vector $x = (x_1,x_2,x_3)^\top$, and I want to integrate the square of the first component over the entire sphere. Can this be done analytically?
Answer: In this specific case, by symmetry, we can say that all $$\int_{\partial B}{x_i}^2dS(x)$$ are equal ($i=1,2,3$), but their sum is $R^2 vol(\partial B)=4\pi R^4$, so...
Comment: Oops, you're right, I didn't consider the $R^2$ factor. But what I wrote is stiil essentially the answer.
Comment: Ok summing them I have $$\int_{\partial B} x_1^2 + x_2^2 + x_3^2 dx = \int_{\partial B} |x|^2 dx = \int_{\partial B} R^2 dx = 4\pi R^4$$ so the actual integral I'm looking for evaluates to $\frac{4}{3}\pi R^4$? Why do you say their sum is $4\pi R^2$?


A step in deriving the solution to heat equation

Question: I'm trying to understand how to solve the heat equation. I'm stuck on this particular step:
$$Q(x,t) = \dfrac{1}{2} + \dfrac{1}{\sqrt\pi}\int_{0}^{\dfrac{x}{\sqrt{4kt}}}e^{-s^2}ds, t > 0.$$
I want to compute $\frac{\partial Q}{\partial x}.$ This should turn out to be
$$ \dfrac{1}{\sqrt {4k\pi}t}e^{-\dfrac{x^2}{4kt}}.$$ I think we have to use Leibniz integral rule but not sure how to do it. Any help appreciated.
Answer: Let $$H(x,t) = Q(x\sqrt{4kt},t) = \frac{1}{2}+\frac{1}{\sqrt{\pi}}\int\limits_0^x e^{-s^2}ds $$
So $$ \frac{\partial H}{\partial x} = \frac{1}{\sqrt{\pi}}e^{-x^2} $$ by Leibnitz or just plain differentiation under the integral sign.
Can you finish from this point on?
Comment: just a question how can we apply Leibniz here. I posed a similar question to the above answer. Do you mind elaborating on the technicalities?
Answer: Never forget the fundamental theorem of calculus!
$$\mathrm{\partial}_x\left[\int_{a(x)}^{b(x)}f(s)\mathrm{d}s\right]=f(b(x))b'(x)-f(a(x))a'(x)$$
So in your example,
$$\partial_x Q(x,t)=\partial_x\left[\frac{1}{\sqrt{\pi}}\int\limits_0^{x/\sqrt{4 kt}}\exp(-s^2)\mathrm{d}s\right]=\frac{1}{\sqrt{4\pi kt}}\exp\left(-\frac{x^2}{4kt}\right)$$
Comment: I am slightly confused between FTOC and Leibniz integral rule which is as follows: $$\frac{d}{dx}\bigg(\int_{a(x)}^{b(x)}f(x,t)dt\bigg)  = f(x,b(x)) \cdot \frac{d}{dx}b(x) - f(x,a(x)) \cdot \frac{d}{dx}a(x) + \int_{a(x)}^{b(x)}\frac{\partial}{\partial x} f(x,t)dt.$$ Do you mind elaborating why we have this extra term in the Leibniz rule and why it is not valid here ?
Comment: The reason the result is so simple in this case is because the integrand doesn't have any $x$ in it.


Studio Monitors for rehearsals

Question: I'm starting to have band rehearsals in my recording studio.
We need to amplify all instruments to the level of the drum set. The volume is not so high (mostly world music).
I own M-Audio BX8 monitors and I heard that it's not recommended to rehearse on the studio monitors and it's best to buy PA speakers.
However, buying a decent PA is more expensive than the current monitors and sound inferior than the monitors.
So... is it really dangerous to rehearse on the monitors?
Comment: No, you maybe need to make some instruments audible, if they depend on amplification (mainly electric guitars and keyboards).  Leave wind instruments etc. unamplified.  The drummer's job is to play DOWN to this semi-acoustic level, not for you to come up to his.
Comment: Yep, his job is to go down, but my job is to set a proper environment. I need to amplify vocals etc...
Answer: You don't want a PA to be flat or to sound good. That's not its job. The role of a PA is to make it so you can hear the vocals and other quiet instruments over all of the other instruments that are already loud. If stage monitors had a flat frequency response, they would be terrible for the job. When I set up stage monitors, I actually cut out a lot of low frequencies because they are just going to suck up power without making the vocals easier to hear.
Unfortunately, you're right about cost. Like everything else in audio, the better stuff almost always costs more. And to power monitors that can really take your head off with vocals in front of a fairly loud band you probably want at least 500 Watts per channel. And feedback is always going to be a concern.
Your near field monitors are not going to cut it. I'd be surprised if they were even that audible. And yes, you may be tempted to crank them so loud that you damage them.
You can start with a single powered PA speaker to act as a monitor, you don't have to buy a huge system. The larger the band, the more speakers you potentially want to make sure everyone can hear. More speakers spread out and set to lower volume is better than a few speakers turned up really loud because you want to control reflections and dispersion to help prevent feedback. At the same time, how you arrange the speakers can create comb filtering which can actually encourage feedback.
If you invest in a pair of decent quality powered PA speakers, you will almost always have a good use for them. Even though they are usually marketed to be used as floor monitors, and they can do a decent job as floor monitors, PA speakers are never the best floor monitors. That's because PA speakers should have fairly wide dispersion and floor monitors should be very very tight.
For some reason the only speakers I've found that are designed to be dedicated floor monitors are all unpowered (except some very expensive ones). So I have two unpowered wedges, an amp that pushes about 500 watts per channel, and I also have an automatic feedback suppression unit with two channels. Many sound engineers hate automatic feedback suppression, but for the monitors I find it to be so valuable. I literally never worry about feedback.
Regarding feedback, one of the best things you can do to prevent it is to reduce reflections inside the practice space. Actual acoustic treatment is most effective at this, but putting rugs down on any hard floors and hanging drapes everywhere will help in a pinch. Large plate windows will be your worst enemy unless you can hang drapes in front of them.
Building a good monitor system is not easy. If you are gigging, it would be a good idea try get a business card or contact info from any mix engineers that you like, and perhaps ask them if they would come by and give you any advice.
Answer: It's more a question of the volume you put through them. Presumably it's only vox, as gtrs and bass should have their own amps. Sounds like the usual syndrome of drummers who can't/won't play quietly. It's actually a great achievement to be able to do that, and still groove. If it's a complete studio, it could have a drumming booth - what a boon!
However, with maybe only mics going through the studio monitors, it'll only be a problem for them at high volumes. They are designed to sound flat, so in fact they may sound worse than a full range p.a., but in a studio, room for maybe a dozen players, why would the volume need to be the same as it would at a 200 person gig?
Comment: Thanks Tim! Yes, it's mostly vocals i need to amplify, though a bit piano and violin just as a monitor for the drummer.

Even if we use the monitors in a relative low volume, i'm worried that feedbacks might harm the monitors... What do you think about it?
Comment: If things are feeding back, they're too loud, or speaker/mic placement needs changing. Feedback in itself won't harm speakers, unless it's at a tremendous volume, but your ears would say stop before that.

Studio Monitors for rehearsals


Open source Graph-based SVM kernels

Question: Recent research proposes the classification or characterization of graphs (instead of flat feature vectors) with Support vector machines. Is there any open source available in C/C++ which can perform such classification?
Comment: That's very interesting! Can you please post a link to that paper?
Comment: @Pedrom. Spatio-spectral Remote Sensing Image Classification with Graph Kernels
Answer: ChemCPP is oriented towards chemoinformatics but has some graph kernel computation code that you may find useful.
Karsten Borgwardt has done a lot of work on Graph Kernels. His page having code computing those kernels is here.

Open source Graph-based SVM kernels


Find the remainder attained by dividing the polynomial $x^{2019}-2x^{1992}+x^{1965}+x^{55}+2x^{28}+x^5+2019$ by $(x^{18}+x^9+1)(x^6+x^3+1)$

Question: Find the remainder attained by dividing the polynomial $x^{2019}-2x^{1992}+x^{1965}+x^{55}+2x^{28}+x^5+2019$ by $(x^{18}+x^9+1)(x^6+x^3+1)$
I honestly don't know how to start this problem without trying to use long division. Any help would be appreciated.
Comment: Hint: note that the divisor times $x^3-1$ equals $x^{27}-1$. Do you find it easier to find the remainder when dividing by $x^{27}-1$? Can you get from there to the final answer?
Comment: Since $y^2 + y + 1 = \frac{y^3 - 1}{y - 1}$, try substituting $y = x^{9}$ and $y = x^{3}$. When you do this, apply it to your divisor & simplify, you'll get what Greg Martin's comment says.
Answer: As @Greg Martin noted, we can expand to find
$$\left(x^6+x^3+1\right) \left(x^{18}+x^9+1\right)=x^{24}+x^{21}+x^{18}+x^{15}+x^{12}+x^9+x^6+x^3+1.$$
Notice that this is the geometric sum $\sum_{i=0}^8 x^{3i}=\frac{1-x^{27}}{1-x^3}$, and thus
$$\left(x^6+x^3+1\right) \left(x^{18}+x^9+1\right)(1-x^3)=1-x^{27}.$$
In the ring $\Bbb Z[x]$, consider the ideal $I=\left(\left(x^6+x^3+1\right) \left(x^{18}+x^9+1\right)\right)$. We have seen that $I$ contains $1-x^{27}$, so in $\Bbb Z[x]/I$, $x^{27}=1$. In the quotient,
$$x^{2019}-2x^{1992}+x^{1965}+x^{55}+2x^{28}+x^5+2019$$
becomes
$$x^{21}-2x^{21}+x^{21}+x+2x+x^5+2019=x^5+3x+2019.$$
Comment: Wow! This solution is exquisite. Thank you sir so much.


Como contar linhas de código em uma solution .NET?

Question: Quais são todas as soluções (livres ou não) para contar linhas de código em uma solução .NET?
Uso dois Visual Studios: 2012 e 2013.
Answer: Clique com o botão direito sobre a solução depois clique em Calculate Code Metrics.

Vai aparecer esta janela:

Uma outra forma, é usar a tela de pesquisar todos os documentos ctrl+shift+F
com a seguinte regex: ^(?([^\r\n])\s)*[^\s+?/]+[^\n]*$
não esqueça de marcar a caixa de regex
Answer: Basta ir no menu Analyze -> Calculate Code Metrics
Uma alternativa seria usar o PowerShell, em que se pode contar todas as linhas que não estão em branco de uma solution. Para isso, basta especificar o diretório e a extensão dos arquivos nos quais a contagem será realizada (.cs, .xaml, etc):
PS> Set-Location CaminhoDaSolution
PS CaminhoDaSolution> (dir -include *.extensao1, *.extensao2, *.extensao3 -recurse | select-string .).Count


Fetch currently logged in user relation with a given model instance

Question: Assume we have a model called Author as such:
class Author(models.Model):
    name = models.CharField(max_length=250)

We also have a feature which enables users to follow a certain author:
class UserFollow(models.Model):
    author = models.ForeignKey(Author)
    user = models.ForeignKey(User)

If you have a button in your UI that enables logged in users to follow / unfollow these authors. One way to check if this user is already following a certain author is to make a query on the existence of a record in UserFollow. However, when you're fetching a list of 10 / 20 .. etc authors, how would you check each author for the currently logged in user?
The usual approach would result in +X number of extra queries depending on how many items you're loading per page. Is there a more efficient way to achieve the same effect?
Thanks in advance
Answer: you can fetch all instances of UserFollow in view, pass it as a variable to the template and then you can check if given author is in this list
def my_view(request):
    following = list(UserFollow.objects.filter(user=request.user))
    template_context = {
        'following': following
    }

template:
{% for author in authors %}
    {% if author in following %}
        {# do some magic #}
    {% endif %}
{% endfor %}


Can age ever be confounded if it is the independent variable in an observational study?

Question: This is a follow-up to my previous question: "Basic understanding of control variables in observational studies"
I have understood the answer, but am still trying to figure out what I think of the Wikipedia example that I'd cited.
Basically, the example is the effect of age on happiness, and discusses whether you should ever control for health. The example says no, because:

To identify the control variables needed here, one could ask what
other variables determine not only someone's life satisfaction but
also their age. Many other variables determine life satisfaction. But
no other variable determines how old someone is (as long as they
remain alive). (All people keep getting older, at the same rate, no
matter what their other characteristics.) So, no control variables are
needed here.

What I understand by this explanation is that age can never (or close to never) be confounded if it is the independent variable in the study.
I have three questions about this explanation:

Is it not mixing up the individual with the population? E.g., let's say wealth completely determines happiness and has a strong effect on longevity. You have a population of 100 people, 60 are age sixty and 40 age seventy. Happiness is measured as 1 = happy and 0 = not. At sixty, half the people are wealthy. At seventy, 3/4s are. Average happiness at sixty = 0.5 (30x1/60) and at seventy = 0.75. Happiness correlates positively with age, but controlling for wealth eliminates this. In this case, shouldn't one control for wealth?

Is their a clear theoretical reason to, or not to, control for health in the Wiki example? To my understanding, if Age = X, Happiness = Y, and Health = Z, you have a number of valid relationships: X → Y, X → Z → Y, Z → X, and U → Z. This seems to give contradictory reasons to, or not to, control.

Does it depend on what question you want to ask? In my original question, FP0 said:

Maybe they consider that since age changes by itself without external
influence, then they want to consider that the additional effects of
the variables I imagined above should be "included" in the global
effect of age, since age has a causal impact on them. In my opinion it
depends on whether you want to aggregate the different effects of age
since age is causal on those factors, or if you want to identify them
individually, and estimate the remaining effects of age separately.

If the answers to 1 and 3 are 'yes,' I have issues with the Wiki example.
Comment: I think the qualifier "(as long as they remain alive)" is really important here.
Comment: You can substantially edit your title if needed without mentioning `[fixed title]`.
Answer: First image below: With variables 'health' and 'wealth' you describe a situation with mediators. Age has a causal effect on health and wealth, and these in their turn have an effect on happiness.
Second image below: Controlling would relate to a (confounding) variable that has a causal effect on both the exposure variable (age in the example) and the outcome variable (happiness). Since there are few parameters that cause age, there is not much to control for.

I agree that the example is not so strong and in the example this is also nuanced by the sentence:

(as long as they remain alive)

There might be a confounding survivorship bias if one would argue that age causes happiness because we observe older people are more happy. The bias is that one assumes that there is no causal relationship from happiness to age which might be false.
Comment: Your title seems to be a different question compared to the situation/example in your body text *"Should you ever control for the independent variable 'age' in an observational study?"* Here one studies the effect of some variable $X$ on another variable $Y$ and 'age' can be a confounding variable in that relationship by having causal relationships with $X$ and $Y$.
Comment: The reason for the title is because the Wiki example that I cite maintains (if I read it correctly) that you should *never* control for the ind var age because it cannot be affected. That is my 'top level' question.
Comment: Re. the first image: yes, this I believe that I understand this. If health or wealth is a mediator, it should not be controlled.

My Q1 is related to my title. I give an example to challenge that one can *never* control for age.

Assuming that the answer to Q1 is that age *can* be determined (e.g., by age if you are talking about the general population), my Q2 is whether health can then be seen as a common cause of both age and happiness.
Comment: @sean.mcgrath the phrase 'control for age' relates to the situation where age is the confounding variable in another relationship. You control for age when you perform an experiment where you keep 'age' constant or when you correct for it in the analysis. The example on the Wikipedia page is the situation when 'age' is a variable that *is being* confounded (and they argue that that situation doesn't often occur).
Comment: I see. It looks like I misused the terminology. I struggled a bit on the terminology because the Wiki example only wrote about 'control.' Should my title then read "Can age ever be confounded if it is the independent variable in an observational study?" As far as I can see, they argue that it can *never* occur. Is this incorrect?
Comment: @sean.mcgrath as long as a person remains alive (and as long as there is no other selection effect) I do not see what would *cause* age in an observational study.
Comment: Correction of that last comment. I *can* imaging a confounding effect. You might argue that being born in a different year sort of causes age, (it doesn't literally cause age, but it does cause people in a study to have different age) and that could be a confounding variable. It can be that certain effects of being born in a particular year cause happiness and not the age of the people that are born in a particular year. ---- Possibly this effect is assumed to be small. The example from Wikipedia is not a strict formal 'never'. It is an informal example to explain confounding.
Comment: A good example of confounding effects when age is the exposure variable, as described in the last comment, would be when you compare exclusive Bordeaux wines from two different vintages. If the older ones tastes better than the younger ones, then it doesn't need to be that older is neccesarily better or causes the wine to be better. It can be that the particular vintage happens to have good effects.
Comment: With humans, instead of wines, you can also have vintage effects. For instance when some virus infects mostly a particular age group, then it might not be the age that has the effect, and it could be the different history or exposure that the age group had (e.g. [Hoskin's effect](https://en.m.wikipedia.org/wiki/Original_antigenic_sin) or different vaccination programs in different era's like end of the smallpox vaccination program).
Comment: Thank you @sextus for the explanations. And given that 2/3 of my cellar is Bordeaux, I can certainly resonate with the example. I understand what you say, and I believe that I can follow it. However, I still struggle with my Q1. Can you point out what is the fallacy in it? Also can you confirm my suggested title. If so, I will change it.
Comment: @sean.mcgrath I don't follow the numbers in your example and why you choose them but "let's say wealth completely determines happiness and has a strong effect on longevity." sounds like the survival bias and relatively more unhappy people die such that at an older age you have more happy people (but caused by wealth instead of age). So yes indeed, the Wikipedia example is a bit problematic.
Comment: The alternative title sounds better.
Comment: thank you. My (contrived, to make my point clear) Q1 was an attempt to show that the example is problematic. Assuming then that age can be confounded due to a survival bias, this leads to my Q2:

Moving back to the Wiki example: Health clearly can create a survival bias and it can also impact happiness. My question is whether there is a clear theoretical reason whether to choose to control for health or not.
Answer: Observational studies adjust for age all the time! I would not be surprised if age was the most common adjustment variable in observational studies.
If age is the exposure (as in both examples of @SextusEmpiricus), the variable of interest, then you must include it in the model (how else are you going to estimate its effect?) and it - by definition - cannot be a confounding variable and be "adjusted for". Adjusting for just means that you include the variable in some model or condition on it in some other way (e.g. stratified analysis).
You would adjust for age if it isn't the variable of interest and lies on a backdoor path between your exposure and outcome in the DAG. An extremely simple example is shown below:

Here, we're interested in the causal effect of physical activity (PA) on cardiovascular disease (CVD). If age is both causally related to physical activity and cardiovascular disease, there is a backdoor path from PA <- Age -> CVD. By adjusting for age (conditioning on it), you block the path. So in this example, you must adjust for age to get the unbiased causal effect of PA on CVD.
Comment: it looks like my terminology was incorrect. I am interested in the case where age is *the* independent variable. My first question, if I understand the wording correctly, is whether in this situation 'age' can possibly be confounded (it seems the Wiki page says that it cannot).


How to make my navbar-brand responsive on low resolution?

Question: My navbar-brand goes down when I resize my browser or either I use it on my low resolution mobile device.
Here's my jsfiddle
My NavBar Code:

        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">

        </button>
        FEU - EAC  E - Project System

Home

Client Application

About Us

Contact Us

                Login

Faculty

Student

Guest

        <form class="navbar-form navbar-left">

                <input type="text" class="form-control col-lg-8" placeholder="Search" style="width:300px; height:30px;">
        </form>

Here's some examples:
Good:

Bad:

Worse:
Comment: we don't mind if you show us your code or create a fiddle at http://jsfiddle.net Questions with links only wan't help anyone if the links get lost one day
Comment: Sure, I'll make it fast thanks.
Comment: Here's my jsfiddle: http://jsfiddle.net/patootiegarcia/8TxEc/
Comment: Well, if you have a long title and a small screen, it's going to wrap. The only thing you can do is use media queries to make the text smaller, or re-structure that bar so when it does wrap it doesn't look as bad.
Comment: Patricia, good start on the fiddle, but you should read their [tutorial](http://doc.jsfiddle.net/tutorial.html) to clean it up a little. The fiddle markup should only contain what you would traditionally put in the body tag.  Everything else belongs somewhere else.  Also, you should attach bootstrap as an external resources instead of inlining the css so it's easier to tell which styles you've applied and which ones are provided by the library.  Here's a [bootstrap template in fiddle](http://jsfiddle.net/KyleMit/kcpma/) to get you started.
Answer: I've had luck using FitText, a jQuery plugin that fits text to it's parent element.
From their website:

FitText makes font-sizes flexible. Use this plugin on your fluid or responsive layout to achieve scalable headlines that fill the width of a parent element.

Not exactly an "answer" (which there may be none of for this question), but a decent solution.
Good luck!
Comment: +1 Good suggestion, even if it has been a long day! :)


java.lang.IllegalStateException: Fragment already added even after .isAdded()

Question: Everytime I run my code this error comes up

java.lang.IllegalStateException: Fragment already added: MyFragment{19704129 #4 id=0x7f0e0077}

Here is the code
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (!myFragment.isAdded())
    transaction.add(R.id.container, myFragment, myFragment.getTag());
transaction.commit();

What? Why? How?
Answer: Seems like a lot of people run into this.  The same problem keeps coming up and only one person found a reasonable answer.
The commit call does not actually add your fragment to the activity.  It queues a request to be processed later.  If a second request for the new fragment is queued ahead of the commit request, isAdded() will return false, and a second request to add the fragment will be queued, and that duplicate fragment exception will be thrown.
The clean solution is to call FragmentManager.executePendingTransactions() before check to see if you fragment has been added.
Answer: Fragments can be added to the Activity and not be visible. By default, a Fragment is visible.
Whether a fragment is added or detached from an Activity is unrelated to the visibility. You can only add a Fragment once.
If you are attempting to control the visibility of a particular fragment there are other methods that allow you to do this. If this is for toggling between different fragments, I would recommend having a look at the hidden state.
http://developer.android.com/reference/android/app/Fragment.html#isHidden()
Comment: But is `isAdded()` always true? I'm using `Fragment.hide()` and `Fragment.show()`.
Comment: But it does. And it cannot be added because it's already added. This is so strange.
Comment: If you haven't added the fragment to the fragment manager `isAdded()` should not return true.


What is the mandatory information a HTTP Request Header must contain?

Question: What is the mandatory information a HTTP Request Header must contain ?
Comment: related http://stackoverflow.com/questions/6686261/what-at-the-bare-minimum-is-required-for-an-http-request
Answer: GET / HTTP/1.0 is a legal HTTP request.
If there's no Host header field, you may not get the results you were hoping for if the destination server is a virtual host that doesn't have its own IP address to distinguish itself from other virtual hosts.
HTTP 1.1 requires the Host field.
Comment: I did a bit research myself and you are right @gbroiles. For Name Based Virtual Hosting a Browser with HTTP 1.1 is required in general. The browsers sends the hostname in the host header and the Web server serves name based virtual hosts if configured. With older browsers and HTTP 1.0 that is somewhat possible, but you need a workaround like apache's ServerPath Directive. Luckily newer browsers implement extensions when using HTTP 1.0 to ensure sending the host header field. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
Comment: Are there still browsers that actually do HTTP 1.0?
Comment: Is `Host` the *only* required request header in HTTP/1.1?
Comment: Lots of user agents use HTTP 1.0. People using web browsers will use HTTP 1.1 or 2.0 where possible but they may access via a proxy that only supports HTTP 1.0. Crawlers or bots may use HTTP 1.0. Simple clients like `wget` use HTTP 1.0.  Note that there is no effort server-side required to support HTTP 1.0 clients, because a HTTP 1.1 compliant server will support HTTP 1.0 clients (HTTP 1.1 is built to be a progressive enhancement on top of HTTP 1.0; non-compatible features such as chunked encoding and keep-alive default to off for HTTP 1.0 requests).
Answer: None of the HTTP Headers are required in an HTTP/1.0 Request. There are no required Response headers either.
All that and more in HTTP 1.1 - RFC 2616
Comment: You're mistaken. According to the RFC you linked here: "A client MUST include a Host header field in all HTTP/1.1 request messages". So the `Host` header _is_ mandatory.
Comment: I cant downvote but this answer is wrong. Please fix or remove.
Comment: I agree with vikingsteve. Your answer implies that HTTP 1.1 does not have any required headers.
Comment: Correct, but there's no requirement that a client use HTTP/1.1 in the first place.
Comment: Content-Length or Transfer-Encoding are only mandatory if an entity is delivered with the request or response, and in many cases a request or response will lack an entity (like a GET request, or a 302 response).
Comment: Content-Length or Transfer-Encoding is mandatory (either of them must be present)
Comment: this is very misleading.. please correct or remove..
Comment: If you reference the HTTP 1.1 protocol, It's on you to abide by those requirements in your post. To expand on that, the statement "no requirement that a client use HTTP/1.1" is irrelevant to your answer - because your answer is heavily tied to the reference *you* make. This is why this post is misleading and wrong and I'm a bit surprised this hasn't been amended in 10 years.


Does nix upgrade support restarting and reconfiguring services?

Question: I'm new to nix. I see that there is --upgrade option that allows to install a new version package, but what if this is just a configuration change and the service just needs a signal to reload the configuration?
It could be notified on the filesystem event but will nix touch file belonging to a different version of a package?
Answer: It depends.
Some services are restarted when their configuration changes, while others are not.
I don't know how it's implemented, but no, content in the Nix store is immutable so no file touching happens within the Nix store (/nix/store). However, on NixOS there are some files which are not in the Nix store; Some examples are in /etc. Such files are not subject to the immutability of the Nix store.
Answer: If the service is configured using NixOS options then the systemd unit will usually refer to the configuration file directly. Changing the options will generate a new config file, cascading to cause the unit file to change. NixOS will interpret this as a signal to restart the service.
--upgrade doesn't do what you probably think it does
That said, nixos-rebuild switch --upgrade doesn't change how the new configuration is applied. It's just an alias for nix-channel --update && nixos-rebuild switch.


syntax errors when opening VS 2017 project with VS 2015

Question: I have a Visual Studio 2017 project and want to open it with Visual Studio 2015.
In my C# code I use this method
    public static bool TryGetValue(this CustomProperties props, string key, out string value)
    {
        try
        {
            CustomProperty prop = props[key];
            value = prop.Value;
            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            value = string.Empty;
            return false;
        }
    }

to get a value from a collection. When building the project I get invalid expression terms.
When I use this line of code
        if (props.TryGetValue("username", out string username)) // string is an invalid expression
        {
            edtUsername.Text = username; // this is unknown because the expression above throws errors
        }

Visual Studio 2015 says that "string" is an invalid expression. How can I fix this?
Comment: *Don't* use Visual Studio 2015. `out string username` is a C# 7 feature. You can't use C# 7 in VS 2015
Comment: You mean this code compiles successful in VS2017?
Answer: declare string variable outside of out operator
string username;
if (props.TryGetValue("username", out username))
{
    edtUsername.Text = username;
}

Comment: This reverts to C# 6, it doesn't make VS 2015 work with C# 7


FileNotFoundException - java

Question: I am Using SAX Parser But after debugging the error is shown at this line
saxParser.parse("C:\\Sample.XML", handler);

Error is:- java.io.FileNotFoundException: C:\Sample.XML (The system cannot find the file specified)
Comment: So, does the file exist? Is it readable?
Comment: Are you sure you really have a file at C:\ called Sample.XML ?
Comment: the exception itself tells you the solution put your file in the right path.
Comment: Java may not have permission to read the file
Comment: It is in readable format as well as i have copied the link from address bar and pasted it over and above i have tried changing with the Caption with capital and backslash but still did not worked. I am using Jbuilder
Comment: If you are using Windows Vista or above, Java may not be allowed to read from the root drive.  Try placing it some where else, like your documents folder
Comment: I am using xp and jbuilder as compiler
Comment: Try something simpler.  Start with `System.out.println(new File("C:\\Sample.XML").exists());` to see if Java can see it and opening the file with something like a `FileInputStream` to see if you can actually read it...
Comment: @MadPrgrammer it is returning false statement. Is there any permission needed to give to the compiler to access the file.  The attributes of file are not ticked so that they can read and write them.
Answer: The backslash "\" is the escape character in a java string.
You need to double escape the backslash "\\".
If in doubt consult the Oracle
Comment: "\\" escapes the escape character, allowing it to be used like this (do it all the time).  Alternatively the OP could use "/" instead, but I can't see it making any difference
Comment: Not sure if this answer is valid after OP was edited!
Comment: I see, that's mean of them, don't you hate that ;)

FileNotFoundException - java


Django - Mascara CPF, DDD e número do telefone

Question: Preciso colocar máscara na digitação do CPF, DDD e do número do telefone.
Sou iniciante em Python e Django e gostaria de saber como posso apresentar esta máscara no momento da digitação:
Para o CPF: 999.999.999-99.
Para o Telefone: (99) 99999-9999.
Criei somente os arquivos models.py e admin.py e como mágica já tenho formulários funcionais, mas quero melhorar a apresentação colocando a máscara.
O que tenho que fazer?
Onde tenho que colocar esta instrução?
Preciso de algum exemplo se for possível.
models.py
class Cliente(models.Model):

    SEXO_CHOICES = (
        ('M', u'Masculino'),
        ('F', u'Feminino'),
    )

    ESTADO_CIVIL_CHOICES = (
        ('S', u'Solteiro'),
        ('C', u'Casado'),
        ('D', u'Divorciado'),
        ('V', u'Viúvo'),
    )

    nome = models.CharField(max_length=60)
    cpf = models.CharField(max_length=11, blank=True, null=True)
    dtNascimento = models.DateField(blank=True, null=True, verbose_name='Data de nascimento')
    sexo = models.CharField(max_length=1, choices=SEXO_CHOICES)
    estado_civil = models.CharField(max_length=1, choices=ESTADO_CIVIL_CHOICES, verbose_name='Estado civil')
    nrTelCelular = models.CharField(max_length=11, blank=True, null=True, verbose_name='Nº telefone celular')
    nrTelFixo = models.CharField(max_length=11, blank=True, null=True, verbose_name='Nº telefone fixo')

    def __str__(self):
        return self.nome

admin.py
class ClienteAdmin(admin.ModelAdmin):
    model = Cliente
    list_display = ['nome','cpf', 'dtNascimento', 'sexo',
                    'estado_civil', 'nrTelCelular', 'nrTelFixo']
    list_filter = ['sexo', 'estado_civil']
    search_fields = ['nome']
admin.site.register(Cliente, ClienteAdmin)

Comment: Qual versão do `python` e `django` está utilizando ?
Comment: Python 3 com Django 1.11.4
Comment: [Django Input Mask](https://github.com/caioariede/django-input-mask) Talvez consiga fazer funcionar, mas creio que está obsuleto.
Comment: Mais recomendo o uso [jQuery Mask Plugin](http://igorescobar.github.io/jQuery-Mask-Plugin/)
Comment: @WéllingthonM.deSouza, django-input-maks não está obsoleto, o [último commit foi feito em 07/02/17.](https://github.com/caioariede/django-input-mask)
Comment: Eu nem tinha reparado,estava na empresa e lá sabe como é, os caras não gosta muito que fica usando internet.
Comment: Infelizmente é incompatível com Django 2.0
Answer: você pode usar o django-input-mask
dentro do seu form você pode facilmente criar suas mascaras.
from input_mask.widgets import InputMask

class MyCustomInput(InputMask):
   mask = {'cpf': '000.000.000-00'}


C - Reading unknown formatted input

Question: I have a text file as input that has an unknown number of lines and a semi-unknown format. An example would be:
p1 a x1 300
p2 b x3 200 c x2 x1 500
p3 c x1 x3 1500 b x2 100 a x3 800

Where the 'c' operations involve two 'x' objects and the others involve one. The order of the operations can be random, and the amount of operations is also random. I need to store the 'p' number, operation type, associated 'x' object(s) and the integer at the end of the operation.
I don't know how to go about storing this data as scanf() only works with formatted input that is known. I've tried to traverse each line character-by-character using getc() but I can't detect the end of a file this way while also trying to grab and check multiple characters.
If anyone could help me figure out a way to store this data I would appreciate it.

EDIT:
My text file has other lines, too, but the final portion is the one I've specified. Currently this only grabs the first operation in the line 'n' times, where 'n' is the total number of operations in the line.
    for (i = 0; i < lines; i++) {
    if(fgets(line, MAXCHAR, fp)) {
        if (line[0] == 'p' && isdigit(line[1])) { // Check if we are interested in parsing the line
            k = opCount(line); // Amount of operations in line - Working
            data[c_count].t_num = k;
            data[c_count].trans = calloc(data[c_count].t_num, sizeof(clients));
            for (j = 0; j < k; j++) {
                tempBuf = strdup(line);
                if(tempBuf) {
                    token = strtok(tempBuf, delim);
                    if(token) {
                        //1st of n sections = p1 a x1 300
                        token = strtok(NULL, " ");//parsing on space now
                        if(token) {
                            strcpy(data[c_count].trans[j].lineNum, token);
                            //2nd section is opType
                            token = strtok(NULL, " ");
                            if (token) {
                                data[c_count].trans[j].op_type = atoi(token);
                                token = strtok(NULL, " ");
                                if (token) {
                                    data[c_count].trans[j].val = atoi(token);
                                }
                            }
                        }
                    }
                }
                free(tempBuf);
            }
            c_count++;
        }
    }

I'm also not sure this accounts for the 'c' case, where two objects are involved...
Comment: Read whole lines into a buffer, and then parse the line in the buffer. If the maximum possible size of the lines are known and not overly large, then you can use plain arrays and [`fgets`](https://en.cppreference.com/w/c/io/fgets).
Comment: I think you need to add more information. For instance: What is the data structure where data shall be stored? The code you have tried so far.
Comment: "only works with formatted input that is known". You do know the format of the input. It's not the same on each line but it's a known format. So read/parse each "token" and depending on the value of that token you then know how to interpret the next token.
Comment: You really should try to include a [mcve], but even so, your question has merit.  The syntax of your example input text is known well enough... `Where the 'c' operations involve two 'x' objects and the others involve one. The order of the operations can be random, and the amount of operations is also random. I need to store the 'p' number, operation type, associated 'x' object(s) and the integer at the end of the operation.`   ...that it should be able to be parsed into an array of `struct`.  See answer below for a suggested approach.
Comment: @Someprogrammerdude I'd like to use `fgets`, however when I read a whole line into a buffer using this, I can't iterate over the line without getting a segmentation fault/weird characters.
Comment: That could with a little work be a good question on its own.
Answer: ...don't know how to go about storing this data as scanf()....
From what you have described in your post, the syntax of your input is actually known well enough to parse it.  Here are some suggestions, and some pseudo code to get you started:
Note:  scanf is not the best choice for parsing strings, even if there is some predictability to their syntax.  I suggest learning to use fgets in conjunction with strtok (or strtok_r) as an alternative.
const char filespec[] = {"C:\\dev\\play\\data.txt"};

typedef struct { //container for operation/line
    char lineNum[10];
    char opType;
    char op[2];
    int val; //you did not describe this in question
}OP;

typedef struct { //container for each line
     int opsCountOnLine;
     OP *section;
 }LINE;

const char *subs[] = {" a ", " b ", " c "};

int getLineCount(const char *filespec);//Counts '\n'.  Adjust function if other criteria
int countOfOpsPerLine(const char line);//Counts occurances of 'a', 'b', 'c', ... on a line

     int main(void)
     {
        int lineCount = getLineCount(filespec);
        int i;
        char line[280]; //adjust as necessary
        FILE *fp = NULL;

        LINE *lines = calloc(lineCount, sizeof(*lines));
        if(lines)
        {
            fp = fopen(filespec, "r");
            if(fp)
            {
                for(i=0;i<lineCount;i++)
                {
                    if(fgets(line, 280, fp))
                    {
                       //here is where LINE can be populated with content of each line
                       //1. determine operations in 'line'.
                       lines[i].opsCountOnLine = countOfOpsPerLine(line);
                       //2. allocate memory for sections.
                       lines[i].section = calloc(lines[i].opsCountOnLine, sizeof(OP));
                       for(j=0;j<lines[i].opsCountOnLine;j++)
                       {

                           tempBuf = strdup(line);
                           if(tempBuf)
                           {
                               //SEE EDIT below for suggested content in this inner loop.
                               //3. Parse line into lines[i].section[0].line, lines[i].section[0].opType, etc.
                               //   using delimiter of "abc" (...def...) with strtok()
                               //4. Other steps???
                               //then free tempBuf so you can use it again in the loop
                               free(tempBuf);
                        }

                    }
                    else
                    {
                        //notify user of error, exit
                        free(lines);
                    }
                }
                fclose(fp);
            }
        }
         return 0;
     }

    int countOfOpsPerLine(const char *line) //Counts occurances of 'a', 'b', 'c', ... on a line
    {
        int count=0, i;
        char *tok;
        // Parse line to find number of sections
        for(i=0;i<sizeof(subs)/sizeof(subs[0]);i++)
        {
            tok = strstr(line,subs[i]);
            if(tok != NULL) count++;
        }

        return count;
    }

int getLineCount(const char *filespec)
{
    int c=0, count=0;
    FILE *fp = fopen(filespec, "r");
    if(fp)
    {
        for (c = getc(fp); c != EOF; c = getc(fp))
        {
            if (c == '\n') // Increment count if this character is newline
            {
                count = count + 1;
            }
        }
        fclose(fp);
    }
    return count;
}

EDIT to include suggestion for inner loop  (and edited main struct above)
Since your input text seems to use a or b or c for opType, you  can use them as the delimiteror...
you can use
const char delim[] = {"abc"};

char *tok;

tok = strtok(tempBuf, delim);
if(tok)
{
    //1st of n sections = p1 a x1 300
    tok = strtok(NULL, " ");//parsing on space now
    {
        if(tok)
        {
            strcpy(lines[i].section[j].lineNum, tok);
            //2nd section is opType
            tok = strtok(NULL)
            if(tok)
            {
                lines[i].section[j].opType = atoi(tok);
                //3rd section is op (and so on...)
                //repeat the same process here until you
                //parsed and placed all members
            }
        }

    }
}

Comment: Thanks. I already had the line count; and I follow until I have to parse the line into sections. With `strtok()` it would just return the first part of the line (_p1, etc._) correct? How would I parse the rest of the line to find opType, etc.?
Comment: @Alex - There will be two levels of parsing per line, once you know the number of operations in the line, for example in this line: p3 c x1 x3 1500 b x2 100 a x3 800, you know there are 3 operations: c x1 x3 1500, b x2 100 and a x3 800, you would parse the main line into these, then you would parse these sub-strings into opType, op and val. So, you are going to have at lease two loops, the main loop uses the i index in my example, then an inside loop, indexed perhaps with j to pars and assign the values to the struct. Added some more detail to the inner loop...
Comment: @Alex - See edits for how a way to parse in the lnner loop area.
Comment: I'm using your code suggestions but I fear I'm still not getting the results I'm looking for. See my edit
Comment: @Alex - The fundamentals of how to do everything you have described has been provided, with illustration. If there are modifications to fine tune to your needs, then make them. Use conditional statements such as if-else if needed to surround special conditions, such as the two operations in c type operations: `if(opType==c){parse 2 operations}else{parse 1 operation}`. Make adjustments to the member types in the struct as needed to accommodate what you want to store. (strings or integer values). Beyond that, good luck. I hope I have helped.

C - Reading unknown formatted input


Variables that do not converge in winbugs

Question: I conducted a Bayesian analysis in winbugs and then checked the convergence from the history plots. The regression coefficients look stabilized, but the variance parameters don't. I got the number of iteration up to 5000, but it still does not look stabilized. I attached the history plots of a regression coefficient and variances. Can you please tell me what I might be doing wrong? Or is is converged?
Comment: Ideally, you could provide a reproducible example (code, data pasted in or provided as a supplement)
Answer: 5000? That's nothing :-) In papers it is usual to use like 200,000. Variance parameters are always the worse for estimation. You have a huge autocorrelation in the chains - you should increase your thin parameter at least 16 times. What is your thin parameter now?
Anyway, I think the variables would converge quite well if you let the chains run longer. So set the thin and do at least 50,000 iterations.
Comment: @Edo Or days. C'est la Gibbs.
Comment: The model is likely to take hours to run, but even with that I suspect that phiA and tauA chains won't converge. They seem to exhibit poor mixing and autocorrelation. Sometimes long burn-ins and lots of thinning can solve this, but often doesn't in my experience. Centering or scaling to N(0, 1) can help. I'm actually looking for advice on what else to do to improve mixing and convergence in hierarchical Bayesian models, so someone else might be of more help.
Comment: Oh God! 200.000 could take hours :(
Answer: The second and third picture don't seem to converge. You can look at Rhat value. If it is 1.0, then it is converged. If it is far away from that, say 1.5, probably it doesn't converge.


How to detect 404 in THEME file

Question: I'd like to attach a library to a custom 404 page.  I'd like to do this in my theme file.  How can I know if the current response will be a 404 Page Not Found?
Answer: Got it.
In your hook within your theme file:
<code>  $status = \Drupal::requestStack()->getCurrentRequest()->attributes->get('exception');
  if ($status && $status->getStatusCode() == 404) {
    $page['#attached']['library'][] = 'themename/notfound';
  }
</code>

How to detect 404 in THEME file