Frequency Response of Varying Frequency Signals

Question: I am trying to study the frequency response of the output acceleration of an object versus the input acceleration. In order to do that, I am inputting an increasing frequency sine wave acceleration to the object and measuring the output acceleration to get a frequency response. I read in different articles that since my input acceleration has a varying frequency, I cannot simply find the Fast Fourier Transform of the Response as my signal is non-stationary.
My solution now has been to calculate the envelope of the acceleration using a Hilbert Transform and plot the response of these envelopes versus the instantaneous frequencies of the acceleration.
Is the second method an accurate way to determine the frequency response of acceleration outputs/inputs?
Answer: In your case, I don't expect Hilbert Transform (HT) to do the job better than Fourier transform (FT). Your input signal is a real signal, so its spectrum is symmetric about DC ($f=0$). HT outputs a single-side-band complex signal, whose negative frequency component is zero while the positive frequency component is same as the input signal. Thus, HT does not provide more (also nor less) information than FT about the system's frequency response.
In your case, you may consider to use wavelet transform (WT) which is suitable to analyze non-stationary signals.


VueJS/Quasar cordova android gera app em branco

Question: Olá,
Comecei a usar o Quasar Framework afim de criar um app hibrido para Android.
Após seguir os passos de instalação (WINDOWS 10), criei um projeto padrão, ou seja, ainda não desenvolvi nada, executei o comando para rodar o app no emulador:
$ quasar dev -m cordova -T android
o processo não apresentou nenhum erro, o app foi instalado com sucesso no emulador, porem quando eu abro o app no emulador apresenta uma tela em branco.
alguem consegue me ajudar com isso?
Desde de já muito obrigado!
Comment: Já tive esse problema com angular, ou seja, a tela branca se da pq no apk gerado não é possível fazer requisições internas, solucionei emulando um mini servidor com ngrok e linkei todas as requisições novamente, passou a funcionar tanto em mobile quanto em desktop
Comment: vc poderia por gentileza me dar mais orientações de como fazer isso? pra eu tentar? pelo que eu entendi então o problema não é do quasar nem do cordova, é isso?
Comment: vou montar uma resposta
Comment: Vlw Felipe, muito obrigado
Comment: Usando o ngrok como fica quando eu for distribuir? Ele já vai junto com o APK?
Comment: Perdão, não tive tempo de concluir ontem, mas já respondi
Answer: Em ambiente de desenvolvimento cada requisição é feita localmente, a não ser que você linke tudo externamente, por exemplo em...

Você pega a lib localmente como se fosse localhost:8000/lib/ionic/js/ionic.bundle.js
Em desktop esse endereço faz sentido, pois você está emulando um servidor interno, mas e em outro dispositivo? ele não saberá oque é esse localhost:8000
Aí entra o ngrok, basta criar um servidor na porta 8000 com o comando ngrok http 8000, ele irá gerar 2 url´s com acesso ao seu servidor uma com certificado e a outra sem.
aí basta linkar corretamente as suas chamadas, exemplo...


Doesn't the non-Gaussian source assumption of ICA render it practically useless?

Question: Gaussian distributions appear everywhere in nature, indeed this was largely the justification for most classical methods' reliance on assumption of normality.
ICA assumes non-Gaussian sources, indeed it requires this assumption to hold for the derivation of components via Central Limit Theorem.
But what then is the value of a method that only works with assumptions contrary to the known reality? Note that unlike the many previous questions I am not asking why or how ICA requires non-Gaussianity. I understand and accept that ICA requires this. What I don't understand is how this doesn't destroy the perceived usefulness of ICA.
What if one wished to separate a mixture of Gaussian sources? What if one didn't know the distributions of the sources (as is typical), then it would seem the 'default assumption' should be for rather than against their Gaussianity - in this case a method that can only give non-Gaussian answers is surely misleading at best?
Are scientists really just 'flip-flopping' on how Gaussian they assume the world to be when they use ICA versus classical methods?
Comment: Not familiar with ICA enough to provide an answer so commenting instead: Isn't the point that the mixture of two Gaussians (with different means) is non-gaussian? So in this case the ICA would find a projection that separates the two Gaussians, because two Gaussians added together give non-Gaussian distribution.
Comment: @KarolisKoncevičius I understood it to be the other way round: the sum of independent random variables is more Gaussian than the variables themselves as per CLT. ICA therefore seeks to reverse this. A quick Google gives: http://mfviz.com/central-limit/ ; http://www.matlabtips.com/ica-demystified/ . You highlight my point though, that ICA seemingly has little to say about the case where the random variables being summed are themselves Gaussian (and this is surely the default/typical case)
Answer: ICA essentially reverses the central limit theorem: if averages (or generally speaking: linear combinations) of data becomes more and more Gaussuan, then one way to uncover the original signals from a linear combination of unknown sources is to look for those that are most non-Gaussian. The more non-Gaussian data you find, the closer to the source you should be.
ICA for all Gaussian signals won't work. Use PCA then, also since co-variance structure is all you need to know anyways about the data if its multivariate Gaussian.
Re Gaussianity:
An interesting article i can recommend is "Why Gaussianity?" (https://ieeexplore.ieee.org/document/4472249/)
One takeaway is that Gaussianity is the max entropy distribution for given mean and variance. So the reason we often assume it in methodologies/models is because we want to assume a world under most uncertainty possible for any method to (still) work.
Re reality is Gaussian:
While averages become more Gaussian, "reality" is in fact not (!) Gaussian (speech, income, # of people you know, stock market, color distribution in images, word frequency, music, traffic delay times, temperature, neural brain activity, seismic activity, ecg heart signals, ...). So most of the times you see Gaussian in 'reality' it's either averages / combinations of other signals (-> ICA comes in handy) or brownian motion - i doubt you see the latter very often ;).
Comment: Thank you for the link, it sounds enlightening. I always assumed that the abundance of measured Gaussian distributions comes from the fact the quantity you're measuring is usually some composite of the effects of yet deeper, more hidden/fundamental processes. If so, then you are saying ICA would 'cut through' that level of measurable Gaussian variables to the non-Gaussian fundamentals? And you are saying the necessarily non-Gaussian nature of these as returned by ICA is not a misrepresentation of nature?
Comment: In other words, take for example height whose mean varies between populations (gender, age, nutrition...) but is normally distributed within each. If I had an aggregate population with many genders, ages or nutritional histories, ICA on the heights of these could not deliver the component subpopulations but would instead attempt to separate the myriad biological and environmental factors that contribute to height?
Comment: re "cut through": I'm by no means an ICA expert, but yes that's one way I would interpret what ICA does (e.g., when applied to the cocktail party problem of separating speech signals; or when applied to stock market data to find underlying 'shocks', or applied to brain activity data to find information exchange, ...).

re height example: not sure I follow what the data looks like.  ICA is applied to a multivariate signal; you can't apply ICA to one signal ("height") to uncover subpopulations (use a mixture distribution or clustering for that).
Comment: Yes it was a confused example, but I think it works if you consider each individual('s height) to be a variable...
Comment: what are your measurements then? Over time?  it's still not clear to me what the "X" matrix (N rows, p columns) represents: height of p individuals measured over time (N time points)?


A connected component of a locally Euclidean space is open

Question: I want to prove that a connected component of a locally Euclidean space X is open in this space.
I start the proof taking a point y in the connected component Y of X. In particular, y is a element of X and have an open neighborhood U, and there is an open subset in an euclidean space and a homeomorphism. I'm thinking on taking the intersection of this neighborhood with Y, defining an open ball contained in the image of this intersection under the homeomorphism and taking it back with the inverse function, but is not clear for me how to use the fact that Y is connected.
Comment: Are you familiar with the fact that the connected components of a locally connected space are open sets of this space?
Answer: Let $C$ be a component. By local connectedness, for
all $x$ in $C$, some open connected $U(x)$ with $x \in U(x)$.
Show for all $x$, $U(x) \subseteq C$ and $C = \cup\{ U(x) : x \in C \}$.
Answer: If $X$ is (weakly) locally connected (in the sense that every point has a connected neighbourhood) then every component of $X$ is open.
Proof: Let $C$ be a component of $X$, i.e. a maximal connected subset of $X$.
Let $x \in C$. Then let $U_x$ be a connected neighbourhood of $x$. Then $C \cup U_x$ is connected as the union of two connected intersecting subsets (as $x \in U_x \cap C$). As $C \subseteq C \cup U_x$ ,by maximality of $C$ we have that $C = C \cup U_x$ which implies $x \in U_x \subseteq C$. This shows that $x$ is an interior point of $C$ and as $x \in C$ was arbitrary, $C$ is open.
Now, let $X$ be locally Euclidean. So for every $x \in X$, we have a neighbourhood $U_x$ of $x$ and a homeomorphism $h: U_x \to U$ where $U$ is an open subset of $\mathbb{R}^n$. Now $y=h(x) \in U$ has a standard Euclidean ball neighbourhood $B(y,r) \subseteq U$ for some $r>0$, and it's a standard fact that $B(y,r)$, as all open balls in $\mathbb{R}^n$, is connected. But then $h^{-1}[B(y,r)]$ is a connected neighbourhood of $x$ in $X$, and so the previous fact applies, and all components of $X$ are open.


Displaying insignificant figures in a console with integers

Question: I have a program that displays songs read in from a text file along with the time and displays them in a neat organized manner in the console. I'm having trouble however having an integer display 01 or 06 as opposed to 1 or 6 for my time format (00:00:00). How would i go about manipulating the output to display a zero infront of the integer when it is below 10?
An example output from my program:
SONG TITLE
The once and future carpenter      0:4:52
I never knew you                   0:7:48
Life                               0:10:52
February seven                     0:15:0
My lfe                             0:24:12
Yuor frei                          0:37:20
Lisjlf                             0:49:23
Lasifj                             0:53:39
Longsong                           1:9:2
Longer                             1:22:3
Almost done                        1:34:48

Process returned 0 (0x0)   execution time : 0.280 s
Press any key to continue.

Desired Output:
SONG TITLE
The once and future carpenter      00:04:52
I never knew you                   00:07:48
Life                               00:10:52
February seven                     00:15:00
My lfe                             00:24:12
Yuor frei                          00:37:20
Lisjlf                             00:49:23
Lasifj                             00:53:39
Longsong                           01:09:02
Longer                             01:22:03
Almost done                        01:34:48

Process returned 0 (0x0)   execution time : 0.280 s
Press any key to continue.

Answer: printf("%02d:%02d:%02\n", h, m, s);

Comment: Oh, C++? `std::printf("%02d:%02d:%02\n", h, m, s);` then.
Comment: i'd prefer the C++ solution over the C solution, but i'll keep that in mind.
Comment: I prefer this over all solutions since it's the most simple one as well as efficient.
Answer: And how do you display your values? Using stream utilities provided by standard library? if so, you could use setw manipulator in conjunction with setfill. stdio counterparts also allow specifying of widht and fill, using appropriate flags with format specifier:
#include <iomanip>
#include <iostream>

...
cout << setw(2) << setfill('0') << h << ':'
    << setw(2) << setfill('0') << m << ':'
    << setw(2) << setfill('0') << s << '\n';


Eigenvalue and Eigenvector a linear operator

Question: Let $A$ be a real symmetric $2 \times 2$ matrix that is not a scalar multiple of the identity matrix. Consider the linear mapping $L$ on the vector space of real 2 × 2 matrices defined by $$L(X)=AX-XA$$
then what is the eigenvalues and eigenvectors of $L$.
My Approach
I have proved that $A$ is diagonalizable. After that how should I proceed in order to get eigenvalues and eigenvectors?
Answer: Let $A=SDS^{-1}$ with a diagonal matrix $D.$ Then $AX-XA=\lambda X$ is equivalent to
$$
S^{-1}AS\,S^{-1}XS \;-\;S^{-1}XS\,S^{-1}AS = \lambda S^{-1}XS
$$
We substitute $X=SYS^{-1}$ and we get
$$
DY-YD = \lambda Y
$$
It is easy to see that $d_i-d_j$ is an eigenvalue of $DY-YD$ with eigenvector $e_ie_j^T$, where $e_i$ is the $i$-th element of the standard basis of $\mathbb{R}^2$ and $d_i$ is the $i$-th diagonal element of $D.$
As the $e_ie_j^T$ are linearly independent, we have found a complete eigenvector basis for $\mathbb{R}^{2\times 2}$.
The only thing left to do is to get the eigenvectors of $AX-XA$ from the eigenvectors of $DY-YD$ by unrolling the substitution.
Comment: How to find the eigenvectors?
Comment: Use the substitution from the beginning: If $Y$ is an eigenvector of $DY-YD$, then $X=SYS^{-1}$ is an eigenvector of $AX-XA.$
Comment: Zero is also the eigenvalue of DY-YD, isn't it?
Comment: Sure, $d_i-d_j=0$ if $i=j.$


How do I fetch the password from the database?

Question: Suppose I entered ABC as password; its hash is like $S$DNcgO9.CUWosQrms0/CqWvLIUFikmoczZ42n9bL6wxiH1OhIQbd5, which is stored in the user table.  I need to retrieve the plain password.
How can I achieve this?
Comment: No you cant convert a hashed password. Unless you have to implement some complex password cracker tools which is not ideal to have. What is your goal to make the password readable?
Comment: i want to give all users of their password. so that they can login through their entered password.
Answer: The short answer is you cannot. Drupal is using a SHA-512 which is a cryptographic hash function. SHA-512 is one way which means it is not possible to get the original data back.
You can check this api. https://api.drupal.org/api/drupal/includes%21password.inc/function/user_check_password/7.x and this https://api.drupal.org/api/drupal/includes%21password.inc/function/user_hash_password/7.x.
Answer: You cannot get the password from its hash, if not by brute force. That is why the hash is saved in the database: Even if somebody would be able to read the database content, they would not get any password from the database.
There isn't any need to give the user their password; if they forgot their password, Drupal has a way to let them set a new one, which is done by sending an email with a link.
Directly disclosing passwords from the site is a bad idea. Are you sure the user asking for the password is the legitimate person, and not somebody that is trying to get access to somebody else's account?
Answer: To how many users do you want to give their passwords? If you have very less users on your site, then you can reset password for each user from admin panel (from edit user page) and send updated password to each user.
You will not get password from the database as password are saved using SHA-512 (cryptographic hash function).
Comment: There are 50,000 of users in my sites
Comment: You can loop through all the 50,00 users and update their password pragmatically. While updating passwords, save them in temporary table for each user and then send an email to each with their updated password.
Answer: You can reset the password and sent email about new password to the users.


One icon has a width bigger than others after identify width in css

Question: after i using a CSS for the fa icon to identify width and height for the container, and the first icon has a bigger width than others, and i couldn't get it a right width. Is there anyway that could make it right.

#features {
    margin-top: 35px;
}

.grid {
    display: flex;
}

#features .icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20vw;
    height: 125px;
}

#features .desc {
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 5px;

}

<link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">

Premium Materials

Our trombones use the shiniest brass which is sourced locally. This will increase the longevity of your purchase.

Fast Shipping

We make sure you recieve your trombone as soon as we have finished making it. We also provide free returns if you are not statisfied.

Quality Assurance

For every purchase you make, we will ensure there are no damages or faults and we will check and test the pitch of your instrument.

Answer: Change width to min-width

#features {
    margin-top: 35px;
}

.grid {
    display: flex;
}

#features .icon {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 20vw; /* Change Here*/
    height: 125px;
}

#features .desc {
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 5px;

}

<link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">

Premium Materials

Our trombones use the shiniest brass which is sourced locally. This will increase the longevity of your purchase.

Fast Shipping

We make sure you recieve your trombone as soon as we have finished making it. We also provide free returns if you are not statisfied.

Quality Assurance

For every purchase you make, we will ensure there are no damages or faults and we will check and test the pitch of your instrument.


Change the color of AlertDialo Title

Question: I want to change the color of alert Dialog Title
enter code here

 String title="AlertDialog";

 builder.setTitle(  Html.fromHtml(" title"));

So is there a way to pass the string title into the above method??
Comment: which programming language are you using?
Comment: Okay, thanks! I've edited the tags of your question to reflect this.
Answer: try putting the color in a variable:
String color = "#FF7F27";
builder.setTitle(  Html.fromHtml("" + title + ""));

Change the color of AlertDialo Title


Install Mono Runtime Locally

Question: Here is my situation. I am on a Mac using Brew as my package manager. I had mono installed on my machine and I was doing all sorts of development in C#. I was doing web development, gui development, ect. When I tried to update my machine using Brew, I got an error. Uninstalling Mono solved the issue.
So, what I did is I toyed around and I created a 2nd user on my machine. Now, this user doesn't need anything to work. I just need Mono and Mono Develop to work on this user without installing anything globally. Everything needs to be installed locally.
So to be specific, I am the only on who uses my machine. My /Users directory looks like this.
<code>/Users/dillon
/Users/dummy
</code>
So if I could install mono, in let's say:
<code>/Users/dummy/.mono
</code>
Then in my .profile:
<code>export MONO_PATH=/Users/dummy/.mono
</code>
Obviously this isn't a perfect world but I was wondering if anyone is an expert on this subject?
(Also, I use this dummy user for other stuff, I use it for Wine and I have a collection of software from school on this user.)
Answer: You need to build Mono from source to install it into a custom location (in which case you can install it anywhere you like).
You can install MonoDevelop.app anywhere, but getting it to use your custom Mono is a bit trickier. You might be able to make it work by mucking around with the relevant environment variables though.
How to install Mono in a custom location and the relevant environment variables is all covered here: http://mono-project.com/Parallel_Mono_Environments
Comment: Ya I was on that link and it is a bit outdated (the download links give 404s). It's also relevant to GNOME, its a bit trickier in my situation. I'll get back to you after toying around some more.
Comment: Okay I have a question, this is kind of unrelated/amateur. I need to log into root to install mono. If I install this as root using ./configure --PREFIX=/Users/dummy/.mono then I get mono running locally on dummy?
Comment: You shouldn't need to log in as root to install into /Users/dummy
Comment: Your `MonoDevelop.app` won't use a custom mono, the paths to the runtime are hardcoded in its launcher script.  And running `mono MonoDevelop.exe` is more than tricky.  I had this working once after many hours of experimenting, but unfortunately forgot which environment variables I was using.

For testing, you can simply load `main/Main.sln` in MonoDevelop, select your target runtime, compile and run.
Comment: If someone wants to do some research here, last time I got this working, I set a breakpoint in `MonoDevelop.Startup.MonoDevelopMain.Main`, dumped all environment variables then set exactly these variables on the command line.

To run `MonoDevelop.app` with another runtime, you need to edit `main/build/MacOSX/monostub.m` and change the paths in there.
Comment: I made a [blog post](http://baulig.blogspot.de/2012/12/running-monomac-application-with-custom.html) which explains how to run MonoDevelop with a custom Mono runtime.

Install Mono Runtime Locally


Is it possible to provide content editor ability to create sortable table?

Question: I know how to create a sortable table using
<code>theme('table', $headers, $rows)
</code>
but is it possible to create a functionality so that a content creator can create a sortable table inside a content.
For example creating a sortable table inside a node.
Comment: I'm not aware of any Drupal module that will do what you're looking for. Take a look at this library http://tablesorter.com/docs/#Demo is this what your looking for?
Comment: Where is the data coming from?
Comment: I guess you should look for a javascript solution
Comment: Data will be entered by user while creating a node.
Comment: Is there any javascript solution provided by drupal ? I mean something that I can use from existing code base or should I consider using 3rd party library ?
Comment: What's the content of the table?
Comment: I have resolved this issue by creating a custom module using tablesorter javascript library.
Comment: @Randell Content of table are alphabets and numbers in form of HTML.
Comment: @user11, it's kinda hard to visualize it with just "alphabets and numbers in form of HTML". Would you be able to provide more info? I'm asking this because there could possibly be other ways of inputting the data instead of doing in within a the rich text editor. E.g. Entity reference of a content type acting like a node, containing another content type acting like rows.
Comment: @Randell I want to have a feature in content editor so that user can create a sortable data table. Table data will be in form of text, table data is not a separate entity.
Comment: Please do not edit answers directly into question. If you want me to delete my answer so you could post yours without double content issue, I can, no problem, just tell. Would be fair as it seems you solved it similar way in comments before I posted. But keep question part for question only.
Answer: You can use TableField module to create table that is a real html table. Sorting is currently unsupported, sadly, although it would be wonderful.
If all you need is sorting on display, you can create a patch or your own module, and implement one of the jQuery plugins for sorting. Personally I found tablesorter to work pretty OK.
Comment: A patch to add tablesorter to the display options via Libraries would be pretty easy.  Probably an hour or two.  Though if the output is run through `theme_table()`, you would just need to wire up the $sort option.  That would also be fairly easy (I think).
Answer: see drupal_add_dragtable()

Assists in adding the tableDrag JavaScript behavior to a themed table.
Draggable tables should be used wherever an outline or list of
  sortable items needs to be arranged by an end-user. Draggable tables
  are very flexible and can manipulate the value of form elements placed
  within individual columns.
Comment: I have resolved this issue by creating a custom module using tablesorter javascript library.


Build bot for a WebGL/Unity game

Question: There is a card game that can be played in the browser (through a WebGL application) or with Steam. I would like to improve my machine learning skills by developing a bot that can play this game automatically. I think the developers don't provide an API for that.
Can I do that in some way? How can I get data from the game application (for example, capturing data from "screenshot")?
Answer: Reading the data from screenshots is possible but you will need to recognise every single card. For example you could take the color value of a few pixels on every card and map the combination to the card. Depending on how many cards there are and how different each card is, you will need more or less pixels. With this database you will have to check every place on the screen where a card could be played (assuming cards are always placed in the same location) and check the pixels to find the played card. You will also need a similar process for other relevant information that is needed by the bot (e.G. the players and the enemies health points).
There is a tutorial in Python for this process here: https://code.tutsplus.com/tutorials/how-to-build-a-python-bot-that-can-play-web-games--active-11117
Another option is finding the values in the computers memory. I fear, I can't provide you any help for this.
Be careful though, developing/using a bot is most certainly against the terms and conditions of the game and may result in a ban if it is detected by the developers.

Build bot for a WebGL/Unity game


Find an ortho-normal basis of a given subspace

Question: This is from last year question paper and I am confused about the question. It states:-
find an orthonormal basis for a subspace : $$W = \{(x_1,x_2,x_3,x_4) \in \mathbb{C}^4 : x_1 +ix_2 =0,\; x_2+x_3-x_4 = 0 \}$$
I came up with the solution:-
$x_1 + ix_2 = 0 \implies x_1=x_2=0$
Also since $x_2=0, x_2+x_3-x_4 = 0 \implies x_3=x_4$
Thus $$W = \{\alpha(0,0,1,1): \alpha \in \mathbb{R} \}$$
however now $W$ has dimension equal to $1$ and thus no other orthonormal basis exist. Am I missing something?
Comment: $x_1+ix_2=0$ does not imply that $x_1=x_2=0$. Consider $x_1=1, x_2=i$
Answer: Using the equations satisfied by the vectors in $W$, you can conclude that:$$W = \{(-i\alpha, \alpha, \beta, \alpha+\beta): \alpha,\beta \in \mathbb{C}\}$$
This easily gives you a basis for $W$. Namely $v_1 = (-i,1,0,1)$ and $v_2 = (0,0,1,1)$. However, $\{v_1,v_2\}$ is not an orthonormal basis. All you have to do is use the Gram-Schmidt procedure to orthonormalize it.
You should get $e_1 = \frac{1}{\sqrt3}(-i,1,0,1)$ and $e_2 = \frac{1}{\sqrt{15}}(i,-1,3,2)$.
Answer: The subspace $W$ is the kernel of the linear map $f\colon\mathbb{C}^4\to\mathbb{C}^2$ induced by the matrix
$$
\begin{bmatrix}
1 & i & 0 & 0 \\
0 & 1 & 1 & -1
\end{bmatrix}
$$
The reduced row echelon form is
$$
\begin{bmatrix}
1 & 0 & -i & i \\
0 & 1 & 1 & -1
\end{bmatrix}
$$
so a basis of the kernel is
$$
\left\{\,
\begin{bmatrix} i \\ -1 \\ 1 \\ 0 \end{bmatrix},
\begin{bmatrix} -i \\ 1 \\ 0 \\ 1 \end{bmatrix}
\,\right\}
$$
A simple Gram-Schmidt orthogonalization will give the required orthogonal basis.


How to add a Location field in drupal 7

Question: I have my location module enabled but I cannot see a location field while creating a new form.How can I add location field to a content type.
Answer: You need to have the Geofield module installed instead. You can then add location information e.g Longitude/Latitude.
Answer: You might have missed installing the location_cck sub module it provides location field type to add on any content type out of the box.

How to add a Location field in drupal 7


How can I get a view from an invisible tab?

Question: I am designing an app for Android and I have a certain activity that uses a tabbed interface (3 tabs, one fragment per tab). So far, so good.
The problem is that, once the user has entered all the required information in the tabs and presses a button, I need to retrieve all that information. However, when I invoke getViewById to try and get a view that is in one of the not visible tabs, it returns NULL.
I have read several tutorials and SO threads explaining the tabbed interface, but none of them provided a working example; they all just created empty tabs.
How should I do this?
EDIT: I have three .xml files, one for each fragment/tab. This is the relevant code in the activity that handles the tabs:
public class SetupActivity extends FragmentActivity implements ActionBar.TabListener {

ActionBar actionBar;
ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_setup);

    // Set up the action bar.
    actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Set up the ViewPager with the sections adapter.
    viewPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();

    /** Defining a listener for pageChange */
    ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener(){
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            actionBar.setSelectedNavigationItem(position);
        }
    };

    /** Setting the pageChange listner to the viewPager */
    viewPager.setOnPageChangeListener(pageChangeListener);

    /** Creating an instance of FragmentPagerAdapter */
    MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(fm);

    viewPager.setAdapter(fragmentPagerAdapter);
    actionBar.setDisplayShowTitleEnabled(true);

    /** Defining tab listener */
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

        @Override
        public void onTabReselected(Tab arg0, android.app.FragmentTransaction arg1) {

        }

        @Override
        public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {
            viewPager.setCurrentItem(tab.getPosition());

        }

        @Override
        public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {

        }
    };

    /** Creating Android Tab */
    Tab tab = actionBar.newTab().setText("Partido").setTabListener(tabListener);
    actionBar.addTab(tab);
    tab = actionBar.newTab().setText("Equipo").setTabListener(tabListener);
    actionBar.addTab(tab);
    tab = actionBar.newTab().setText("Alineación").setTabListener(tabListener);
    actionBar.addTab(tab);
}

/**
 * This method is invoked when the user presses a button on tab number 3
 */
private void checkTeam() throws MatchNotReadyException {
    EditText teamName = (EditText) findViewById(R.id.editTeamName); // this EditText is in tab number 1
    String name = teamName.getText().toString(); // I get a NullPointerException here

    if(name == null || name.isEmpty()) {
        throw new MatchNotReadyException();
    }

    //some more actions
}

So, what do I have to do to get a valid reference to the EditText, instead of a null?
Comment: please post a code snippet to show how you are building the tabs and using the fragments
Comment: Ok, I have added some code to better explain the issue. Sorry for the delay.
Answer: If you are using a FragmentStatePagerAdapter as your base class for your adapter, it will delete the Fragments as you move along to many tabs.
Use a FragmentPagerAdapter as your base adapter. I am not sure if this is your problem because you don't have the adapter code, but I had a similar issue and that fixed it.


"Convex" polynomials

Question: Let me define "convex" polynomials, as the smallest class $\mathcal{C}$ of functions $p:\mathbb{R}\rightarrow \mathbb{R}$ defined (inductively) as:
UPDATED (case 0 was missing):
0) $p(x)=x$, i.e., the identity, is in $\mathcal{C}$.
1) $p(x)= 1$ is in $\mathcal{C}$
2) $p(x)= 1- q(x)$, for $q\in \mathcal{C}$, is in $\mathcal{C}$.
3) $p(x) = c \cdot q(x) + (1-c) r(x)$, for some $c\in [0,1]$ and $q,r\in\mathcal{C}$, is in $\mathcal{C}$.
4) $p(x) = q(x) \cdot r(x)$, with $q,r\in\mathcal{C}$, is in $\mathcal{C}$.
So basically $\mathcal{C}$ include all constant $c\in[0,1]$ functions, and is closed under the operations of convex combination, $1-$, and multiplication.
Clearly for every $p\in \mathcal{C}$ and $x\in [0,1]$, $p(x)\in [0,1]$ too.
Question: Is this class of functions known? Can anything interesting be said about this class? For example I think that every continuous function $f:[0,1]\rightarrow[0,1]$ can be approximated by functions in $\mathcal{C}$. Would this be some (trivial?) consequence of the Stone–Weierstrass theorem?
Thanks!
Comment: I'm not happy with your choice of terminology.  You have a convex set of functions, not a set of convex functions.
Comment: (info) Often, "smallest" is written as "for every $\mathcal{C}'$ satisfies the condition, $\mathcal{C} \subset \mathcal{C}'$".
Comment: Wait, without $p(x) = x \in \mathcal{C}$, $\mathcal{C}$ becomes uninteresting.
Comment: Thanks, let me fix this JiminP
Comment: Is there a easy proof that $\mathcal{C}$ includes all constant functions? I am not seeing it right now.
Comment: p(x)=c, is obtained as   c*1+ (1-c)*0, where 1 and 0 are the constant 1 and 0 functions.
Comment: @RaMa2013 But, how to show that $p(x)=2 \in \mathcal{C}$?
Comment: @tetori, no just constants with $c\in [0,1]$, as specified.
Comment: Let $p\in\mathbb R[x]$ be a polynomial such that $p(x)\in [0,1]$ for each $x\in [0;1]$. Is $p$ in $\mathcal C$?
Comment: Alex, I guess that's the heart of the problem: I don't know.
Answer: (Too long for a comment)
Lemma 1. If $f\in\mathcal C$ is not constant then $f^{-1}(\{0,1\})\subseteq \{0,1\}$.
Proof. The claim clearly holds for the functions from rules 0 and 1.
If in rule 2 it holds for $q$, then also for $p$ because $0$ and $1$ are simply swapped. If in rule 3 it holds for $q$ and $r$, it also holds for $p$ because a convex combination within $[0,1]$ can yield a boundary value only if at least one input is this boundary value. If in rule 4 it holds for $q$ and $r$, it also holds for $p$ because a product i s$0$ iff one factor is $0$ and the product is $1$ if both factos are $1$.
Hence the claim follows by induction. $_\square$
As a corollary, Alex Ravsky's tentative conjecture is busted: $\mathcal C$ does not contain all polynomials that map $[0,1]\to[0,1]$. For example, $p(x)=4x^2-4x$ is missing.
(I thought I had more, but had to scratch it again)
Comment: Wait, $f(x)=\frac12x$ is in $\mathcal C$ but $f^{-1}(\{0,1\})) = \{0,2\} \not\subseteq \{0,1\}$. Also, $p(x)=4x^2-4x$ maps $[0,1]$ to $[-1,0]$ not $[0,1]$. Am I missing something?
Comment: I guess you are only considering the restriction of $f$ to $[0,1]$, and you mean $p(x)=4x-4x^2$.

"Convex" polynomials


Lighthill explanation of delta function/generalized functions

Question: I am reading Jaynes' "probability theory: the logic of science" which refers at one point to Dirac's delta function an example of a so called "generalized function".
Jaynes then says that "mathematicians thinking in terms of the set theory definition of a 'function'" considered delta-functions non-rigorous.
He explains how Lighthill justifies delta-functions/generalized functions using a different basis than set theory.
My question is: What is a good resource on (or could you explain) how to define functions in this non-set-theory way of Lighthill, and how the delta function is justified on the basis of it?
I am not very familiar with Fourier Analysis.
Here is the entire section in Jayne's book, in case it helps:
Comment: The statements in Jaynes, particularly the criticism of Schwartz ("he persisted in defining the word 'function' in a way inappropriate to analysis"), suggest to me that it's better to pay no attention to his book. Analysis is what analysts do.  Analysts like Schwartz (Fields Medalist) and Hörmander (ditto) used the old, "inappropriate" concept of function.  While there may be other ways of constructing a theory of distributions/generalized functions, Schwartz's way has prevailed.
Comment: @AsafKaragila: Well, the problem is that even if you define the $\delta$-function as taking a value $\infty$, that is not enough to usefully describe it.  For instance, you want $2\delta$ to be different from $\delta$.  You also want to be able to talk about the derivative $\delta'$.  Overall, there are a lot of things you want to do with the $\delta$-function that cannot be conveniently done by treating it as an object that has no more data than the values it takes at points.
Comment: That statement *always* rubbed me the wrong way. Since we can always consider $\infty$ to be an object of the ambient mathematical universe (while not being a real number), there is absolutely no problem with the definition of the $\delta$-function as a set theoretic function. Once again ignorance in set theory lead to ridiculous claims about the limits of set theoretic foundations. *Sigh.*
Comment: Looking briefly at Lighthill, he presumes the normal definition of function, defines a class of "good functions" which seems to me sort of like what is usually called $\cal{S}$, and then defines a generalized function as an equivalence class of sequences $f_n$ of "good" functions which converge in the sense that $\int f_n F$ converges for all "good" functions $F$; two sequences $f_n$ and $g_n$ are equivalent if $\lim_n \int f_n F=\lim_n\int g_n F$ for all "good" functions $F$.
Comment: Of course, it is ridiculous to call this a problem with "set theory"--it is just an issue with the notion of "function" being used in this context.
Comment: @Eric: You're right, of course. Just having $\infty$ is not enough to characterize the $\delta$-function, but it is often a complaint about its domain and codomain, etc, being "incomplete" in that sense. Which is why I made that comment.
Comment: @ForgotALot that sounds like the way I was taught distributions in my analysis class. I thought that was the original construction by Schwartz though?
Comment: @s.harp No, Schwartz took a space of test functions with a suitable topology, and defined the space of [corresponding] distributions as the topological dual of that. Taking the space $\mathscr{D}(M)$ of smooth functions with compact support on a manifold $M$ yields the space of (all) distributions on $M$, taking the space $\mathscr{S}(\mathbb{R}^n)$ of rapidly decreasing functions on $\mathbb{R}^n$ gives the tempered distributions (this is appropriate for Fourier theory).
Answer: For references try:
"Delta Functions: Introduction to Generalized Functions", R.F. Hoskins;
"Mathematics for the Physical Sciences", Laurent Schwartz;
"Generalized Functions vol 1", I.M. Gelfand, G. E. Shilov.
The last 2 are classics in the areas of generalized functions and distributions.
But basically $δ(x)$ or $δ(x-a)$ is defined by
$$f(0)=\int f(x)δ(x)dx$$
or
$$f(a)=\int f(x)δ(x-a)dx ,$$
Where f is adequately well behaved.  Either suffices.  So δ(.) is usually defined by its action under the integral sign. Sometimes, in addition, its value is given as zero everywhere except at the single point, a, where its value is infinity, but this may not be needed.
However there are problems with this definition since a value at a single point should not change the value of an integral. So instead sometimes a 'inner product' notation is used
$$f(a) = (f(x) , δ(x-a)) $$
or
$$f(a) = <f(x) , δ(x-a)> $$
which transforms $f(x)$ to $f(0)$, or $f(x)$ to $f(a)$  --see Gelfand p4 or Schwartz p77,
Also it is better to use the terms 'Delta Distribution' or 'Dirac Delta' since $δ(x-a)$ is not properly a function.


JqGrid keyboard navigation

Question: I have an editable Jqgrid(where when im clicking on a row, that row get editable) with 3 dropdown lists in it, what i want is:

when im clicking on a row, its get editable and selects first dropdown in a row
after i choosed an object in dropdown list and pressed tab it jumps to the next dropdown (already happends)
same as the 2. but it have to jump to the third dropdown list
when im done editing, when i press down key or just press somewhere else on the screen data which i edited should be saved to DB. so it have to happend withount me pressing enter key, which i have to do now.
if i press down key, selection have to jump down to next row and save the previouse row which i just edited

I know its a long one, but i cant get path keyboard selection because im stuck with clickable edit....
Answer: If you don't use multiselect:true you can use bindKeys method to have keyboard support.
If you need to change the order of focus on Tab pressing you can change tabindex attribute on the editable fields (input or select elements). You can use oneditfunc parameter of editRow for such initialization actions.

JqGrid keyboard navigation


Criei uma navbar com um form ao lado mas quando adiciono varios inputs no form a pagina perde o background

Question:
view do form:

Cadastro de clientes

             <form method="POST" action="">

Dados do cliente:

                        Nome:
                        <input class="form-control " type="text" placeholder="Insira o nome" required>

                        Data de Vencimento:
                        <input class="form-control" id="date" type="date">

Dados do carro:

                        Placa
                        <input class="form-control" type="text" placeholder= "Informe a placa do veiculo"  >

                        Cor
                        <input class="form-control" type="text" placeholder="Informe a cor do veiculo" >

                        Marca
                        <input class="form-control" type="text" placeholder="Informe a Marca do veiculo"  >

                        Modelo
                        <input class="form-control" type="text" placeholder="Informe o Modelo do veiculo"  >

            </form>

view da nav:
`

Inicio

Clientes

Cadastrar cliente

Ver clientes

Data de pagamento

Remover Cliente

Sair

`

CSS
body {
    width: 100%;
    height: 100% ;
    background-color: lightgray;
    color:lightgray;
  }

.spot{
    background-color: black;
    height: 100%;
    display: block;
    width: 12vw;
    position: fixed!important;
}
ul {
    margin: 0;
    padding: 0;
    list-style: none;
    width: 100%;
    text-align: center;
    }
        ul li {
        position: relative;
        }

        li ul {
            position: absolute;
            left: 12vw;
            top: 0;
            display: none;

            }
            ul li a {
                display: block;
                text-decoration: none;
                color: white;

                font-family: 'Lucida Console', Monospace;

                background:black;
                font-size: x-large;
                padding: 12px;
                border: 0.5px solid rgb(0, 0, 0);
             }
             li:hover ul { display: block; }

             ul li a:hover{
                text-decoration:none;
                color: black;
                background: white;
             }
.registerUser{
    text-align:center;
    position: absolute;
    top: 15vh;
    left: 30vw;
    background-color: white;
    padding: 25px 15vw 0 00px;
    border: 3px outset #fff;
    border-radius: 0.3rem;
div.esquerda{
margin-left: 5vw;
    display: inline-block;
  width: 320px;
  text-align: left
}

.leftTitle{

   display: inline-block;
   width: 320px;
   text-align: left
}

.escuro{
font-weight: 600;
}

Answer: Acredito que se você tentar usar a propriedade min-height invés de 'height' no body possa resolver, pois ela vai se adaptar ao seu conteúdo máximo, caso ele ultrapasse o mínimo.
Comparação entre os dois(px será usado no exemplo mas o funcionamento é o mesmo)

#box{
  height: 100px;
  background-color: #bbb;
}

Um paragrafo

Um paragrafo

Um paragrafo

Um paragrafo

Um paragrafo

Agora com min-height:

#box{
  min-height: 100px;
  background-color: #bbb;
}

Um paragrafo

Um paragrafo

Um paragrafo

Um paragrafo

Um paragrafo

Dependendo de como está o seu menu, quando o body crescer, ele irá ficar no topo, uma solução interessante também é usar overflow-y

#box{
  height: 100px;
  background-color: #bbb;
  overflow-y: scroll
}

Um paragrafo

Um paragrafo

Um paragrafo

Um paragrafo

Um paragrafo

Também notei um erro no seu código:
.registerUser{
text-align:center;
position: absolute;
top: 15vh;
left: 30vw;
background-color: white;
padding: 25px 15vw 0 00px;
border: 3px outset #fff;
border-radius: 0.3rem;
div.esquerda{
margin-left:
blabla...

Uma classe está dentro da outra, faltou o fechamento da classe .registerUser
Espero ter ajudado!!
Comment: Quanto ao espaço em branco no background, eu resolvi de outra forma, descobri que eu tinha linkado uma bilbioteca de css que tava me atrapalhando com isso, retirei ela e ficou tudo normal :)
Comment: Mas aceitei sua resposta por ter ajudado nas outras coisas que foram de grande ajuda, Valeuzão! :D
Comment: Que bom então, fico feliz em ter ajudado
Answer: Creio que isso possa estar acontecendo devido ao:
.registerUser {
  position: absolute
}

Dessa forma, esse elemento terá uma maior prioridade em relação aos outros elementos(estará fora do fluxo normal do documento), além de suas dimensões serem definidas pelo conteúdo interno, que no seu caso ultrapassa as dimensões do body, por estas estarem definidas como 100%:
body {
  width: 100%;
  height: 100%;
}

Assim sendo, creio que uma solução possível seria remover esse dimensionamento do body e deixar a sua classe .registerUser assim:
.registerUser {
  position: relative
}

Estou aprendendo, mas espero ter ajudado.
Segue a documentação: position - CSS | MDN
Comment: Tambem estou desbravando um pouco das funcionalidades do CSS entao as vezes fico um pouco perdido, mas infelizmente essa nao foi a solução pq aquela parte branca ainda continua :(


How to read parts

Question: I have a problem with this program: I was able to make another draft program but it only reads the first three lines and stops. Unfortunately, the input file is not symmetrical so I should use while loops for some parts only to avoid repeating parts of the program. With strcpy I could copy the work done up to a certain point and then continue from that point on. I was trying but visual studio tells me to move the memory to the heap but I don't know how to do it ... Practically I should only extrapolate certain data from the input file and order them in an output file according to a precise line.
I also know there's a problemi with char buf[200], rec[200] because I have to allocate much more memory...
Comment: [Why is "while ( !feof (file) )" always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong)
Comment: I don't know to change what i've done...
Comment: Use `while (1)` or `for (;;)` and `break` the loop if one of the `fscanf`s returns something  diiferent to 1
Comment: Can you help me please?
Comment: start checking  with the debugger what happens when you use `buf` uninitialized.
Comment: @David Ranieri I'll try what you suggested ... As far as memory allocation is concerned, how can I do it? Should I use malloc?
Comment: No, you don't need dynamic memory, since you are writing to a file a buffer large enough will suffice, but you should initialize your objects before reading them. I want to help but I'm not able to see a pattern in your source file :(
Comment: Thank you again, I published a pattern of the source file , for example "session 1 (COPY MODE):
number of sequences: 15

registration 1:
characters given: CALOR

registration 2:
characters given: CARINO" you don't see it?
Comment: Hope you can help me now ahaha
Comment: The problem is that you don't follow the same rules in the sections, you want COPY MODE or ONLINE_FREE_MODE?  you want given or recognized? You need a more solid design, `fscanf` has poor regex support.
Comment: @David Ranieri the problem is that the input file is exactly that one, as I said unfortunately it isn't symmetrical so I have to do different loops during the scan. So I think I should save what the fscan does up to a certain point with strcpy and they start a new loop from that point forward to scan a new section...
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/216514/discussion-between-sandra-boccia-and-david-ranieri).
Comment: the duplicate is wrong for me, the problem is not about `feof` but how to extract the data from the non regular file
Answer: the first time in

while ( !feof(fd) ) {
  fscanf(fd, "session %d (COPY MODE):\n\n", &session);

you call feof before any read so feof cannot work proprely, an easy change is to do
while (fscanf(fd, "session %d (COPY MODE):\n\n", &session) == 1) {

Out of that the mode can be something else that COPY MODE and the two \n after are not mandatory, you also do not check your next fscanf success and globaly the algorithm you use is wrong
A possible way to do the job also checking the validity of the input file is :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  FILE * fp = fopen("summary.txt", "r");

  if (fp == NULL) {
    perror("cannot open summary.txt");
    exit(1);
  }

  int session;
  char mode[64];
  const char * separator = "_____________________________________________";

  while (fscanf(fp, " session %d (%[^)]):", &session, mode) == 2) {
    if (strcmp(mode, "COPY MODE") && strcmp(mode,"ONLINE FREE MODE"))
    {
       fprintf(stderr, "invalid file, invalid mode '%s'\n", mode);
       exit(1);
    }

    char line[64];
    char given[64];
    char recognized[64];
    int registration, registration_set = 0;
    int sequences, sequences_set = 0;

    for(;;) {
      /* read next line, bypass possible newline(s) before */
      if (fscanf(fp, " %63[^\n]", line) != 1) {
        if (!feof(fp)) {
          perror("reading file");
          exit(1);
        }
        *line = 0;
      }
      else if (sscanf(line, "number of sequences: %d", &sequences) == 1) {
        sequences_set = 1;
        continue;
      }
      else if ((*given == 0) &&
               (sscanf(line, "characters given: %63s", given) == 1)) {
        if (!registration_set) {
          fprintf(stderr,
                  "registration is missing before 'characters given: %s'\n",
                  given);
          exit(1);
        }
        if (!sequences_set) {
          fprintf(stderr,
                  "sequences is missing before 'characters given: %s'\n",
                  given);
          exit(1);
        }
        continue;
      }
      else if ((*recognized == 0) &&
               (sscanf(line, "characters recognized: %63s", recognized) == 1)) {
        if (!*given) {
          fprintf(stderr,
                  "given is missing before 'characters recognized: %s'\n",
                  recognized);
          exit(1);
        }
        continue;
      }

      if (registration_set) {
        if (!*given) {
          fputs("invalid file, given is missing\n", stderr);
          exit(1);
        }
        printf("%d %d %d %s %s\n", session, registration, sequences, given, recognized);

        if (!*line || !strcmp(line, separator))
          break;
      }

      if (sscanf(line, "registration %d:", &registration) != 1) {
        fprintf(stderr,
                "invalid file, 'registration <n>:' expected rather than '%s'\n",
                line);
        exit(1);
      }
      else {
        registration_set = 1;
        *given = *recognized = 0;
      }
    }
  }

  fclose(fp);
  return 0;
}

Note the space at the beginning of the format of the fscanf, that allows to bypass character considered to be space including the newline
Compilation and execution :
pi@raspberrypi:/tmp $ gcc -Wall c.c
pi@raspberrypi:/tmp $ cat summary.txt
session 1 (COPY MODE):
number of sequences: 15

registration 1:
characters given: CALOR

registration 2:
characters given: CARINO

registration 3:
characters given: SUSHI

_____________________________________________
session 2 (COPY MODE):
registration 1:
number of sequences: 15

characters given: SUSHI
characters recognized: SUSHI

_____________________________________________
session 3 (ONLINE FREE MODE):
number of sequences: 15

registration 1:

characters given: PERA
characters recognized: PFRA

registration 2:

characters given: SALON
characters recognized: SALON

registration 3:

characters given: PERRO
characters recognized: PERRO

_____________________________________________
session 4 (ONLINE FREE MODE):

registration 1:
number of sequences: 7

characters given: TORTUGA
characters recognized: TORTUGA

registration 2:
number of sequences: 4

characters given: ANAEROBIO
characters recognized: ANAERPBIO

registration 3:
number of sequences: 4

characters given: PAPELES
characters recognized: PAPELEX
pi@raspberrypi:/tmp $ ./a.out
1 1 15 CALOR
1 2 15 CARINO
1 3 15 SUSHI
2 1 15 SUSHI SUSHI
3 1 15 PERA PFRA
3 2 15 SALON SALON
3 3 15 PERRO PERRO
4 1 7 TORTUGA TORTUGA
4 2 4 ANAEROBIO ANAERPBIO
4 3 4 PAPELES PAPELEX
pi@raspberrypi:/tmp $

Comment: Thank you very much! Can you just make it more simple please? It's a bit difficult for me to understand it all...
Comment: @SandraBoccia it can be a little simplified removing the tests checking the input file is correct, anyway to do that is not a good idea, I encourage you to *always* check the validity of the inputs
Comment: @SandraBoccia why did you edited my answer to remove the part compilation & execution ? (I rejected your edit) And why did you also edit your question to remove a large part of it ? (I rollback)

How to read parts


Is there any way around this check on int numbers?

Question: I've coded a check for integers which returns a boolean value:
def NoZerosFives(n: str):
    if '0' in n or '5' in n: #It checks if any of the digits is 0 or 5
        return False
    return True

which I then put in a script:
for n in range(10**20000, 10**100000000):
    d = str(n)
    if NoZerosFives(d):
        pass
    else:
        print('Failed the check.')

n is obviously extremely large, and the else:print('Failed the check.') is here just to tell me if the script's going or not, but it's not printing anything.
My first thought is that the problem is not with Python but with my hardware (I have a pretty powerful laptop, but still a laptop). Is my computer not powerful enough? Should I have done something to my PC that I didn't do?
Or is the code not fit for the objective and I just need to do something different?
Comment: Why are you working with such ridiculously large numbers in the first place? Did you first test it with smaller numbers to sense-check it?
Comment: You are starting at `10**20000`. I bet that code will run for days before it hits a number without a zero.
Comment: That code should never terminate: do you have any intuition for just how *big* `10**100000000` is?
Comment: Can you explain what exactly your objective is?
Comment: Where you're trying to debug it, why are you using `pass`? Put something informative there like `print('Passed the check.')` BTW welcome to SO! Check out the [tour] and [ask] for advice.
Comment: @DavidBuck I'm doing this for the multiplicative persistence conjecture, which states that no number has multiplicative persistence over 12. Since this is known for numbers bigger than 10^20000, that's what I set out for.
Comment: @wjandrea Thanks for the links! I actually worried posting a question like this would be dumb (and the ridicoulous post with the number printed out made me realize it), so I'll definetely check those for future improvement on the questions I ask!
I tried putting the more useful print in my actual script, but to no avail obviously
Comment: @jordanm thank you, I realize the problem, and it's betweem my chair and my keyboard.
Answer: As @jordanm points out in a comment, NoZerosFives will not return True (which is the only way your code produces any output at all) for a very long time. Think about how long it's going to take, starting from 10**20000 (shown below), to reach a value with no zeros in it by just adding 1 at each step.
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
Comment: @jordanm It's *way* bigger than that. The estimated number of *particles* in the universe is something like `10**80`. This is 19,920 *orders of magnitude* larger. (`10**80` itself is merely 80 orders of magnitude larger than one.)
Comment: The number is over 10x higher than the estimated number of atoms in the universe.
Comment: Seriously, I did this mainly to see if it gets flagged somehow :)
Comment: I'm just about to post a link into the chat room. It's given me a giggle. Well played :)
Comment: Lol, it's like a social media troll post :P
Comment: Updated to give jordanm proper credit; all I did was provide a visual.
Comment: @chepner well... damn, I should have known better, shouldn't I? I'll have to cook up stuff better then... Holy cow


Is it more effecient to use std::cout once rather than multiple times to display the same amount of data?

Question: For example,
Would it be more memory efficient to display these variables like this:
std::cout << "First char is " << char1 << " and second char is " << char2;

rather than this:
std::cout << "First char is " << char1;
std::cout << " and second char is " << char2;

Of course, I'm not literally worried about two lines of code.. But I'm trying to learn to write code more efficiently
Thank you
Comment: If you're printing anything to the console the console will be the bottleneck.
Comment: There is no difference what-so-ever. It's all just function calls, and you either pass the argument implicitly (when chaining) or explicitly (when writing a new statement).
Comment: I think the second one is easier to read. That might be more efficient for the people working with the code.
Answer: Having it be a single statement could theoretically be faster as the compiler can rearrange the order of argument evaluation more freely. However, this is talking about 0.00000000000001% difference and is pointless. Don't care about this - the bottleneck is in the console itself.
Anyway, column alignment is really helpful for readability and so try this:
std::cout <<       "First char is " << char1;
std::cout << " and second char is " << char2;

Or this:
std::cout <<       "First char is " << char1
          << " and second char is " << char2;

(I prefer the first because I find it easier to format in my text editor).
Comment: Oh, argument reordering is indeed a good point. Not that I can see how you'd get a performance boost out of it, but hey.
Answer: It makes no difference with programs that small. The toss-up comes with readability. Do u want it quick and easy or more readable?
Comment: It doesn't make any difference for large programs either. You can have a single statement with 10000-chained operators or 10000 statements with no chaining, it'll be the same in the end.


no viable alternative at input ANTLR4

Question: I'm designing a language that allows you to make predicates on data. Here is my lexer.
lexer grammar Studylexer;

fragment LETTER : [A-Za-z];
fragment DIGIT : [0-9];
fragment TWODIGIT : DIGIT DIGIT;
fragment MONTH:  ('0' [1-9] | '1' [0-2]);
fragment DAY: ('0' [1-9] | '1' [1-9] | '2' [1-9] | '3' [0-1]);

TIMESTAMP: TWODIGIT ':' TWODIGIT; // représentation de la timestamp

DATE : TWODIGIT TWODIGIT MONTH DAY; // représentation de la date

ID : LETTER+; // match identifiers

STRING : '"' ( ~ '"' )* '"' ; // match string content

NEWLINE:'\r'? '\n' ; // return newlines to parser (is end-statement signal)

WS  :   [ \t]+ -> skip ; // toss out whitespace

LIST: ( LISTSTRING | LISTDATE | LISTTIMESTAMP ) ; // list of variabels;

// list of operators

GT: '>';
LT: '<';
GTEQ: '>=';
LTEQ:'<=';
EQ: '=';
IN: 'in';

fragment LISTSTRING: STRING ',' STRING (',' STRING)*; // list of strings
fragment LISTDATE : DATE   ',' DATE   (',' DATE)*;    // list of dates
fragment LISTTIMESTAMP:TIMESTAMP ',' TIMESTAMP (',' TIMESTAMP )*; // list of timestamps

NAMES: 'filename' | 'timestamp' | 'tso' | 'region' | 'processType' | 'businessDate' | 'lastModificationDate'; // name of variables in the where block

KEY: ID '[' NAMES ']' | ID '.' NAMES; // predicat key

and here is a part of my grammar.
expr: KEY op = ('>' | '<') value = (  DATE  | TIMESTAMP )  NEWLINE          # exprGTORLT
    | KEY op = ('>='| '<=') value = ( DATE  | TIMESTAMP )  NEWLINE          #  exprGTEQORLTEQ
    | KEY  '=' value = ( STRING | DATE      | TIMESTAMP )  NEWLINE          # exprEQ
    | KEY 'in'   LIST                                      NEWLINE          #exprIn

When I make a predicate for example.
tab [key]  in  "value1", "value2"

ANTLR generates an error.

no viable alternative at input  tab [key]  in

What can I do to resolve this problem?
Comment: You are trying to use ID as both a lexer fragment rule and lexer token. So, in your rules, ID matches "tab" first and returns that token. Then, it comes across "[", and it doesn't know what that is, so it outputs an error. Then, it comes across "key", which matches ID, and returns ID token. What you want is change lexer rule "KEY" to parser rule "key", and use that in "expr" rule. After that, you have "NAMES" defined ("filename", "timestamp", etc), but "key" is not a valid alternative. I highly recommend you print out the token stream and parse tree so you can work past these problems.
Comment: @kaby76 I agree that `key` should almost certainly be a parser rule, but it is not true that `ID` will match the `tab` in `tab [key]` no matter what. If we use an input that actually matches the `KEY` rule (like `tab[filename]` for example), that will indeed produce a `KEY` token. The fact that `ID` could match `tab` doesn't interfere with that because longer matches are given precedence as per the maximal munch rule.
Answer: First tab [key] does not produce a KEY token like you want it to for two reasons:

It contains spaces and KEY doesn't allow any spaces. The best way to fix that would be to remove the KEY rule from your lexer and instead turn it into a parser rule (meaning you also need to turn [ and ] into their own tokens). Then the white space in your input would be between tokens and thus successfully skipped.
key is not actually one of the words listed in NAMES.

Then another issue is that in is recognized as an ID token, not an IN token. That's because both ID and IN would produce a match of the same length and in cases like that the rule that's listed first takes precedence. So you should define ID after all of the keywords because otherwise the keywords will never be matched.

no viable alternative at input ANTLR4


Good way to catch exception in one method among several classes

Question: Currently I have several classes, each of which deals with different sites. They act like the same type, in the sense that they all have crawl_item() method.
class CrawlA
  def crawl_item
    ...
    A.create()
  end
end

class CrawlB
  def crawl_item
    B.create()
  end
end

Now I want to catch one model validation exception when calling any of these crawl_item methods, and perform the same rescue action. What would be a good way to implement this?
Comment: I guess we can assume that the methods `crawl_item` are different in each class, aren't they?
Comment: @tokland yes they do crawl but the implementations are quite different
Answer: To keep it DRY you can write a wrapper using the classical mix-in structure (module + class include):
module CrawlValidator
  def with_validations
    begin
      yield
    rescue => exc
      ...
    end
  end
end

class CrawlA
  include CrawlValidator

  def crawl_item
    with_validations { A.create }
  end
end


Expected number of unique states visited in Markov process

Question: Is there a way to calculate the expected number of unique states visited in a Markov chain after n steps?
For example, say I have 4 states {A,B,C,D} with transition probabilities 1/4 across the board; i.e., the transition matrix is uniformly 1/4. The initial condition is {0,1,0,0}. What is the expected number of states visited after 3 steps?
This is a simplified example. The problem I'm working on is much more complicated (many more states and steps, and heterogeneity in transition probabilities). So I'm looking for a general solution.
I thought that I could do something like this:
E[unique states visited] = P(visited A) + P(visited B) + P(visited C) + P(visited D)
= 1-P(haven't visited A) + 1-P(haven't visited B) + 1-P(haven't visited C) + 1-P(haven't visited D)
= 1-(1-P(A,step 1))(1-P(A,step 2))(1-P(A,step 3)) + ...
But this gives me the wrong answer - I'm guessing because P(A,step 1) is not independent of P(A,step 2), etc.
Answer: For this problem in particular, at least, we can find the solution.
Let $p(k)$ be the probability that you transition to a state you haven't visited yet, given that there are $k$ states left that you haven't visited yet.
In the case of this problem, $p(k) = \frac{k}{4}$.

For the sake of choosing a convention, I'll suppose that you are considered to have already visited the state you start on.
Let $E(k, t)$ be the expected number of states you will have visited given that there are $k$ states that you haven't visited yet, and you have $t$ transitions remaining. The base case is that $E(k, 0) = 4-k$.
Now, let's consider the recursive case. If your current number of unvisited states is $k$, you will with probability $\frac{k}{4}$ transition to a new state, so that $k\mapsto k-1$. Otherwise, with probability $1-\frac{k}{4}$, you will visit a state that you have already visited.
So $E(k, t) = \frac{k}{4} E(k-1, t-1) + \frac{4-k}{4} E(k, t-1).\qquad(t>0)$
If you solve this recurrence relation for $k=3$ initial unvisited states and $t=3$ steps, you get $175/64 \approx 2.73$ as the expected number of states visited. If you consider the intial state to be unvisited as well ($k=4$), the number drops to $148/64 \approx 2.3125$.

For Markov models with more complicated probabilities, I can only think of a brute-force solution: starting from a Markov chain with $n$ states, create a new Markov chain where each new state represents a subset of states that you've visited so far, along with the state you're currently on. You can compute the transition probabilities of this new chain from the probabilities of the old chain; then you can compute the expected number of visited states in the old chain from the expected final state of this new chain.
Answer: Let us consider some special cases where it is easy to determine the correct value:

Atomic starting distribution (one 1 and the rest 0).
After 0 steps the expected number of states visited should always be 1.
After 1 step the expected number of places visited should be 1 + 1-(chance to stay).

Assuming $\bf v$ is our initial state vector $\bf P$ is our transition probability matrix, $\bf 1$ is the column vector of ones, the product sign is assumed to mean Schur/Hadamard (element-wise) product. A method which fulfills these is to take
$${\bf 1}^T \left( {\bf 1}- \prod_{i=0}^n\left({\bf 1}-{\bf P}^i{\bf v}\right) \right)$$
This is a matrix way to write sum over vector, since scalar product with the 1 vector is a sum. I think this does something similar to what you meant in the question.
For our example:
$${\bf P} = \left[\begin{array}{cccc}0.25&0.25&0.25&0.25\\0.25&0.25&0.25&0.25\\0.25&0.25&0.25&0.25\\0.25&0.25&0.25&0.25\end{array}\right], {\bf 1} = \left[\begin{array}{c}1\\1\\1\\1\end{array}\right], {\bf v} = \left[\begin{array}{c}0\\1\\0\\0\end{array}\right]$$

After 0 ($n=0$): $1.00$ - we always start somewhere.
After 1 ($n=1$): $1.75$ - the $0.25$ missing to reach $2.00$ is because chance to stay is $25\%$
After 3 ($n=3$): $2.7344 \approx 2.73$ as the other answer by @user326210 .
After 16 ($n=16$): $3.9699<4$ - reasonable since we can't visit more states than there exist!

The first two and the last make sure it passes our sanity check and the third conforms with previous more theory focused answer by @user326210 .
This is of course by no means a proof, more of a practical contribution.
Answer: Given a Markov chain with transition matrix ${\bf{T}}$, what is the expected number of states visited by a random walk during $m$ steps starting in state $s$?
Say
$$X_i=
\begin{cases}
  1, & \text{if the random walk touches state } i \\
  0, & \text{otherwise}
\end{cases}
$$
The quantity we are looking for is the expectation
\begin{equation}
\left<\sum_{i=1}^n X_i\right> = \sum_{i=1}^n\left< X_i\right> \tag{1}
\end{equation}
where $n$ is the number of states. Clearly
$$
\left< X_i\right> = \text{probability that the random walk touches state }i
$$
We can compute that probability as follows: Define a modified transition matrix ${\bf{U}}$, which is equal to ${\bf{T}}$, except that state $i$ only transitions into itself, namely
$$
U_{jk}=
\begin{cases}
  \delta_{j,i}, & \text{if } k=i \\
  T_{jk}, & \text{otherwise}
\end{cases}
$$
Now all the paths that touch state $i$ get absorbed there. Starting in state $s$, compute the occupancy after $m$ steps of this modified chain:
$$
\vec a = {\bf{U}}^m \vec x
$$
where
$$
x_j = \delta_{s,j}
$$
Then the answer is the final occupancy of state $i$
$$
\left< X_i\right> = a_i
$$
Do this for all $i$ and compute the sum in Eqn (1).
Notes:

Obviously $\left< X_s\right> = 1$, so you can save yourself $1/n$ of the work.

If you want the average result starting from an arbitrary starting state, then change the starting vector $\vec x$ to $x_j=1/n$ for all $j$.


Which method should I use for this differential equation?

Question: this is my first time doing calculus so I do not have a good book to study.. Any recommendations?
$$ y' =\sqrt{x + y + 8} $$
Comment: @OussamaBoussif both would be great but a book is better than a particular case!
Comment: Thank a lot @OussamaBoussif
Comment: Are you asking this question on how to solve the ODE or you want recommendations on books?
Comment: Internet is a valuable source of informations, I learned a lot just by searching the internet and especially MATH SE ^^
Answer:
$$ y' =\sqrt{x + y + 8} $$

Hint:
$\nu(x):=y(x)+x$
$$\frac{d\nu (x)}{dx}-1=\sqrt{\nu (x)+8}$$
Solve for $\frac{d \nu}{dx}$:
$$\frac{d \nu (x)}{dx}=\sqrt{\nu (x)+8}+1$$
Divide both sides by $\sqrt{\nu (x)+8}+1$:
$$\frac{\frac{d \nu (x)}{dx}}{\sqrt{\nu (x)+8}+1}=1$$
I hope that you can finish from here
Answer: Hint: try to make a substitution, set $h(x)=y+x$, differentiate this function with respect to $x$, then you should obtain: $\frac {dh}{dx} -1 = \sqrt{h(x) + 8}$ then you can apply the common separation of variables.


How do I translate the weekday in a date?

Question: I created a view from a JSON file; the dates in that file follow the 2020-07-28 20:00 format. I do a rewrite of the date to {{ value_1|date("l d/m/Y") }} and I get Tuesday 28/07/2020, but I want to get the weekday in Dutch.
Is there code for this?
Answer: Use the below example as a temporal solution, I'm not sure if this is the proper way. The approach is to split the problem.
{{ value_1|date("l")|t }} {{ value_1|date("d/m/Y") }}

Regarding the next link, the proper use of translation on twig is using "t" filter.
How do I translate strings in Twig templates?
Comment: thnx @diego-restrepo


new Promise failing to wrap callback

Question: Here's the working code which uses a callback (this in coffeescript)
requestUtils.post = (path, data) ->
  requestUtils.request.post(
    "#{requestUtils.apiHostname}#{path}",
    { json: data },
    logFn
  )

When I run this I see the HTTP response logged. I want to turn the logFn callback in a resolve.
Here's my attempt:
requestUtils.post = (path, data) ->
  new Promise (resolve, reject) ->
    requestUtils.request.post(
      "#{requestUtils.apiHostname}#{path}",
      { json: data },
      resolve
    )

Seems straightforward enough. When I call the function and pass a promise handler like so:
  GM.requestUtils.post("/getVehicleInfoService", {id: "#{id}"})
  .then () ->
    console.dir arguments

When I run this I don't see anything printed. The then handler seems to not be called. I'm not sure why. I thought I had a grip on promises but apparently not
Comment: You don't pass any returned argument to the callback of `then` like `.then(args => console.dir(args))`
Answer: Figured it out, guess I have to wrap the resolve in another function like so:
requestUtils.post = (path, data) ->
  new Promise (resolve, reject) ->
    requestUtils.request.post(
      "#{requestUtils.apiHostname}#{path}",
      { json: data },
      () -> resolve arguments
    )

The difference is instead of just passing resolve as the callback I'm passing
() -> resolve arguments

Not entirely sure why this is the case as it seems to be doing the exact same thing as passing resolve by itself.
Would be interested if anyone can explain this.

new Promise failing to wrap callback


Cluster analysis for multi-response question

Question: Let's say I have check-all-that-apply survey question.  What kind of analysis can I run to understand if there are meaningful clusters (i.e. there's a cluster of people who choose A, B, C, and another cluster that choose A, D and E)?
Comment: Multiple response data are data of binary variables. There exist a lot of proximity metrics for such data (Jaccard being among the most popular). You base your clustering on the matrix of such distances.
Comment: Please be more specific with your question. If there are people who chose A and B then they will show up in clusters choosing (A,B,C) and (A,B,D). What sort of clusters are you particularly interested in?
Answer: Two key approaches:

Consider the options to be binary, and use an appropriate distance measure such as Jaccard similarity, which he developed for biological research: "check all species that live in this region". Then you have a wide variety of clustering algorithms available.
Use frequent itemset mining, and check if you have interesting frequent patterns. The benefit is that these patterns may overlap, and can be transformed to rules such as "users that chose A and B also picked C in 90% of the cases".
Comment: Can you recommend a source to read about frequent itemset mining, including its algorithm, so that one may want to try to code it oneself?
Comment: That should be covered in every data mining book. Apriori is the most known data mining algorithm (and it's one of the top 10 important data mining algorithms: https://link.springer.com/article/10.1007/s10115-007-0114-2 ). Although I'd rather use FPGrowth which is more clever (but also more complicated).