Microsoft SSIS Designer ver 16.0 not able to connect to MySQL when running a data flow

Question: I try to develop SSIS ETL with MySQL as both the OLTP source and OLAP target. My DEV environment has Visual Studio 2019 + SSIS/SSDT ver 16.0 and MySQL 32bit ODBC driver 5.3 (64bit won't work for the SSIS version, and driver 8.x doesn't seem to have 32bit). I create "user DSN" to my ETL source and target -- "system DSN" won't work for SSIS.
I was able to create ODBC connection managers.
I was able to drag-n-drop ODBC source and target on a data flow.
For the ODBC source, I was able to use Table Name to view the table columns but got error when preview the data. So I use SQL with the MySQL syntax. Then I was able to preview the data.
For the ODBC target, I was able to see the columns and map the column.
But when I "Debug --> Start" or run the data flow without debugging in the VS SSIS designer, I got:
Error: 0xC0014009 at Sure BI: There was an error trying to establish an Open Database Connectivity (ODBC) connection with the database server.
Error: 0xC020801C at Claim Party, Get catastrophe [2]: SSIS Error Code DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER.  The AcquireConnection method call to the connection manager "SCE_QA_32BIT_USR" failed with error code 0xC0014009.  There may be error messages posted before this with more information on why the AcquireConnection method call failed.
What I did wrong?
Answer: One of my colleagues helped find the root cause and a solution.
Root Cause: Visual Studio 2019 is 32bit IDE and therefore everything that runs in the debugger should be 32bit. However, VS by default tries to make ODBC connection by 64bit drivers.
Solution: Choose menu option Debug --> Debug Properties.

Set Debugging --> Run64bitRuntime to False


How to loop through controls to get specific type of controls?

Question: I have a div like this:

            <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>

In run time I create listitems(HtmlGenericControl outer_li = new HtmlGenericControl("li");) in the (ul which has attribute runat ="server").
Now what i wanna to do is:
looping through div id="columns" to get each (ul which has attribute runat ="server") then loop through each (ul) to get each list item to save contents .

EDIT: according to answers:
foreach (Control c in columns.Controls.OfType<HtmlGenericControl>())
        {
            var ctrl = (HtmlGenericControl)c;

            if (ctrl.TagName == "ul" && ctrl.ID != "column1")
            {
                foreach (Control li in ctrl.Controls.OfType<HtmlGenericControl>())
                {

                    var ctrl_li = (HtmlGenericControl)li;
                    if (ctrl_li.TagName == "li")
                    {
                        string id = ctrl_li.ID;
                    }
                }
            }
        }

this doesn't work i can't get the 'li' at all although there are li on my page

Edit 2:
 protected void CreateBlockOfData(string widget_color, int column_par, string process_name, int block_type, int block_id)
        {
            HtmlGenericControl outer_li = new HtmlGenericControl("li");
            outer_li.Attributes.Add("class", widget_color);
            if (column_par == 1)
            {
                column1.Controls.Add(outer_li);
            }
            else if (column_par == 2)
            {
                //uppnl_2.ContentTemplateContainer.Controls.Add(outer_li);
                column2.Controls.Add(outer_li);
            }
            else if (column_par == 3)
            {
                column3.Controls.Add(outer_li);
            }
            else if (column_par == 4)
            {
                column4.Controls.Add(outer_li);
            }
            ////////////////////////////////////////////////////////////
            HtmlGenericControl div_head = new HtmlGenericControl("div");
            div_head.Attributes.Add("class", "widget-head");
            outer_li.Controls.Add(div_head);
            ////////////////////////////////////////////////////////////
            HtmlGenericControl h3 = new HtmlGenericControl("h3");
            div_head.Controls.Add(h3);
            ///////////////////////////////////////////////////////////
            Label lbl_process_name = new Label();
            lbl_process_name.Text = process_name.TrimEnd();
            h3.Controls.Add(lbl_process_name);
            ///////////////////////////////////////////////////////////
            HtmlGenericControl div_content = new HtmlGenericControl("div");
            div_content.Attributes.Add("class", "widget-content");
            outer_li.Controls.Add(div_content);
            ////////////////////////Data//////////////////////////////
            Control crl_data = FormTheData(block_type, block_id);
            PlaceHolder1.Controls.Add(crl_data);
            crl_data.DataBind();
            div_content.Controls.Add(crl_data);
        }

Comment: If you are adding those ``'s as html-text to the placeholder, then they are *not* 'Controls' as far as ASP.Net is concerned. You can't add a 'runat=server' as plain attribute to make it a Control, you would need to add it as HtmGenericControl or something like that.
Comment: -Why not use `BulletedList` class here?
Comment: Can you check my last edit please ?
Answer: I tested this and added some
's manually. I could get them to show up if I set runat="server" in their tag. So the code you're using to generate and add the ListItems is not creating them with the runat.
Edit
You made need to consider using an actual ASP .NET Control such as a DataGrid or Repeater or BulletedList as MAW74656 suggested instead of HTML
's.
Comment: hmmm, I try to add attribute .
`outer_li.Attributes.Add("runat","server");`
but invain..although it appears in the view source and with `runat="server"`
Answer: Maybe you could change your list to use the ASP listitem control, which would allow you to...
Try something like this.  Using is to cast a control for testing is efficient.
  foreach (Control c in Page.Controls)
    {
        if (c is ListItem)
        {
            //do something with the control
        }
    }


How do I analyze a simulation study?

Question: Say I want to simulate from a density $f$.
I come up with two procedures for doing so, $proc1$ and $proc2$.
Now, I can obviously plot the histograms that come from simulating from $proc1$ and $proc2$ and plot $f$ on top of them, and say "yeah looks like they both work".
But what more can I say? Preferably, I want to be able to say something about the accuracy of those procedures. I want to be able to compare them and say something about which one is better or worse.
What can typically be done in this situation?
Right now, my only idea is to calculate the sample mean from both $proc1$ and $proc2$, and calculate its standard error, and then the simulation with a smaller error is better?
Comment: What would you like to measure as "accuracy"? Set aside for now how to measure and assess it; that's what we'll help you do. But what do you want to measure to determine accuracy? Two examples are discrepancies between the simulated PDFs and the population PDFs and discrepancies between the simulated CDFs and population CDFs.
Comment: calculating the mean isn't the point, testing the simulation procedures is.
Comment: Hi. I am not sure exactly, but let me give you an example. Say you want to use *proc1* and *proc2* to calculate the mean of $f$. So you sample 10.000 samples from *proc1* and you take the average, and you do the same for *proc2*. Now, the "better/more accurate procedure" should be the one whose sample mean has a smaller standard error, right? So can those standard errors be used to compare accuracy?
Comment: Why not just calculate the mean of $f$ the usual way?
Comment: The trouble with just assessing which procedure gives the lowest variance is that either procedure could be biased. I do not think that would be a good method. For determining which procedure is more accurate. My suggestion is to do something with the PDF or CDF. Do you have an equation for the PDF or CDF of $f$?
Comment: I do have the PDF but it's not analytically tractable so I can only plot it.
Comment: Then you can use that plot to compare to your simulated PDF or integrate it to approximate the CDF.
Answer: Depending on your needs, some characteristics per sample ($\times 10^n$ as appropriate) of simulated $f$ (plus the CDF, $F=\int f$, and quantile function $F^{-1}$) that may be of interest for procedure 1 vs procedure 2:

Computational time (e.g., measured using clock cycles, seconds, or represented analytically using big O notation)
Memory cost
Energy cost
Uniformity of $p$ (from $F^{-1}$)
Accuracy in the tails of $f$
Accuracy for the distribution overall from an analytic formulation of $f$ (e.g., using a one-sample Kolmogorov-Smirnof test for each procedure for many samples of size $N$ and comparing the mean $p$ values).

The particular statistics tracked from simulating a univariate distribution will necessarily depend on the purposes envisioned for using it. For example, if the number of needed simulations is anticipated to be on the order of, say, $10^6$ per day or less, then you may not need to calculate compute time, memory, or energy costs. However, if anticipated need for simulated draws from $f$ is at a much greater rate, say $10^9$ per hour or more, then those costs per sample may be relevant.
Comment: +1 I hadn't even thought to consider the computational aspects. Just what do you mean by uniformity of p?
Comment: @Dave I was imagining that deviations of $p$ from true uniform(0,1) imply some kind of bias from the target distribution, but haven't thought that through all the way. :)
Comment: You mean hitting the simulated distributions with the population CDF for a probability integral transform?
Comment: @Dave depending on the nature of $f$, $F$, and $F^{-1}$ (any of which may or may not have a closed-form expression, and may necessarily entail approximation), procedure 1 and procedure 2 may be specific to one of these functions (standing on its own, not merely a probability transform of a simulation of one of the others). If I recall correctly, simulating the non-central $t$ distributions $f$ and $F$ are separate simulation algorithms, not simply probability transformations, for example.
Comment: @Dave I hope I understood your question. (And am not making too bad of a fool of myself in these comments. ;)
Comment: I was thinking of doing something like hitting the simulated distribution with the population CDF, then testing if the result is normal. Maybe that could help with the struggles KS has with tail differences. I might try simulating that to see if I can improve the power of KS.
Comment: @Dave I thought KS was *overpowered*?
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/98947/discussion-between-dave-and-alexis).

How do I analyze a simulation study?


how to change the skin of a button

Question: Hello all I'm trying to change the skin (shape) of the button and textArea skin to another one. Some thing like the second picture .
Answer: You've got more than one possibility to do this:
Use a Background image (works in every browser, but I personally don't like it to re-edit and save the image file for every small detail again)
CSS:
button {
  background: url('some image..');
  width: <width of the background image>;
  height: <height of the background image>;
  ...
}

button:hover {
  background: url('mouseover image');
  ...
}

Alternatively you can use the newer CSS properties to create buttons. They offer everything you want and it is WAY cooler than using background images, but the downside is not many browsers support this fully today (and some will not for a long time, yeah IE, I mean you):
button {
  border: solid 1px somecolor;
  color: #eee;
  border-radius:5px;
 -moz-border-radius:5px;
  background: -moz-linear-gradient(top, #5E5E5E 0%, #474747 51%, #0a0e0a 51%, #0a0809 100%);
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#5E5E5E), color-stop(51%,#474747), color-stop(51%,#0a0e0a), color-stop(100%,#0a0809));
}
button:hover {
  color: white;
}

Look here to create CSS Gradients: http://www.colorzilla.com/gradient-editor/
Impressions and tutorials of what's possible using pure CSS3:

http://technology.posterous.com/make-css3-buttons-that-are-extremely-fancy
http://www.kulturbanause.de/2010/05/css3-als-photoshop-ersatz-buttons-und-grafiken/
(in German, but hey, you'll be able
to see the buttons and read the CSS
neverthenless)
Simpler example, might be just what
you need:
http://capturedsparks.com/pure-css3-buttons/

You can use them same properties for styling a textarea: border, background, color for font color, etc.
Answer: <button type="submit" style="border: 0; background: transparent">

</button>

Comment: Never seen it written that way. Does it really work?, I like it!
Comment: I didn't check all browsers but it works in FF
Answer: You can apply an style to the bottom or submit HTML tag, like:
<input type="submit" class="mybotton" value="Continue" />

In this way you can add a background image:
.mybotton{
    background-image: url(buttonimage.png);
}

Regards!
Answer: if u want exactly the same design u described above then just use this
<input type="image" src="submitbutton.png" />

how to change the skin of a button


How to display only specific columns of a table in entity framework?

Question: how to display the some specific columns of table instead of whole table in entity framework.
using (DataEntities cxt = new DataEntities())
{
    notes note = cxt.notes.Where(no => no.id == accID).SingleOrDefault();
    return notes;
}

Answer: For this purpose, I would suggest you to make use of ViewModel like following :-
notes note = cxt.notes.SingleOrDefault(no => no.id == accID);
var model = new YourViewModel // Your viewModel class
            {
                   ID = note.ID,
                   PropertyOne = note.PropertyOne, // your ViewModel Property
                   PropertyTwo = note.PropertyTwo
            };

Answer: You can do this with QueryView.
This implies editing your model directly in XML as there is no designer support for this, but you will get an independant entity with less fields than the original one.
Advantages:

You can then query data base for this truncated entity directly (you will get only fields you need from the data base - no need to get whole entity from DB and trancate it in code)
It is good in scenarios where you want to send this truncated entity to the
client
       with WCF to minimize traffic (e.g. when building big lists on client
       that basically need only name and ID and no other entity specific
       information).

Disadvantages:

This QueryView-based entity is read only. To make it writeable you will have to add r/w functionality yourself


Good books for learning Applied Probability?

Question: I'm looking for a book that provides deep, rigorous coverage of probability theory, but with an emphasis on material that is mostly useful outside of a mathematics department. I've heard "The Theory of Probability: Explorations and Applications" is pretty good, but I wanted to get some other suggestions.
For example, Achim Klenke's book is way too much for me...it's organized for theorem proving, not applications as far as I could tell. Also not a big fan of Durrett's books from what I've perused, or Billingsley or Feller...again, too much of a mathematics research focus.
Answer: 1000 Exercices is the best book you can buy.
Why ?

Cover the full range of probability.
Provide exercice based of the probability: Put in practise because
  there is a big gap between Theory books and real Proba problems.
Prepare you well for Quizz, and make your brain in good shape....
You can keep forever as a book of how to solve problems...

http://www.amazon.com/Thousand-Exercises-Probability-Geoffrey-Grimmett/dp/0198572212
Comment: Wow! Impressive coverage!
Comment: I think you'd want in addition to this, the actual book:

http://www.amazon.com/Probability-Random-Processes-Geoffrey-Grimmett/dp/0198572220/ref=pd_bxgy_14_img_2?ie=UTF8&refRID=0VSR0J8K3NZ3HKYS07NA

which has the exact same exercises (but no solutions).

I should add that this is one of the few books I've seen which presents advanced probability theory with essentially no measure theory.
Comment: Yes, this is focused on how to use Proba rather than how to read the theory... (and never know to use it).
Answer: Frederick Mosteller's Fifty Challenging Problems in Probability sounds made to order for you. Check out the comments on Amazon about it...
http://www.amazon.com/Challenging-Problems-Probability-Solutions-Mathematics/dp/0486653552/ref=sr_1_1?ie=UTF8&qid=1459445736&sr=8-1&keywords=frederick+mosteller
Comment: I think this one does not cover the full range of Probability. This is mainly for brainteaser testing.
Comment: @quantCode  How useful is Grimmet's book "outside of a mathematics department?" Mosteller's has two significant advantages:  1) meeting this criterion of being truly applied, and 2) being written by Mosteller, one of the great 20th c statisticians. What it may lack in terms of the "full range" of probability is more than compensated for by it's non-academic focus.
Answer: Applied Probability by Kenneth Lange

Huge breadth of applied probability topics, with a few chapters on computational probability.
Tons of examples (albeit sometimes too concise)
Very good balance between theory and applications.
HUGE collection of good problems, difficulty range from mildly trivial to pretty challenging. I particularly like his problems because most of them are very difficult to do, until you see how to do it and the proof becomes less than 1/3 of a page.

I used this book for a stochastic process class, and it remains one of my all time favorite books.
"Theory divorced from applications runs the risk of alienating many potential practitioners of the art of stochastic modeling. Applications without a clear statement of relevant theory drift in a sea of confusion."
https://www.amazon.com/Applied-Probability-Springer-Texts-Statistics/dp/1441971645/ref=sr_1_1?ie=UTF8&qid=1509045816&sr=8-1&keywords=applied+probability+lange


Quantify the similarity of bags of words

Question: I have two datasets that contain the most common words and their frequencies of two different articles.
e.g.:
A = [apple: 23, healthy: 15, tasty: 4]
B = [apple: 19, healthy: 21, bad: 7]

Both datasets contain similar words. I want to find a measure that will give me an idea of whether or not the two articles are talking about the same thing. In this case, they might be discussing apples and their health benefits.
I can get a simple measure like similarity = words in A and B / total number of words but is there a more formal measure, perhaps one that utilises the frequencies as weights?
Comment: The previous title suggested that you know that both bags have the same number of distinct words. Is this indeed what you mean? Also, why is this guaranteed; is it that you're selecting only the $n$ most frequent words from each article?
Comment: I am able to choose `n` for both articles. It is possible that they are different sizes but I am only returning the top 10 most common words so we can essentially guarantee it.
Comment: There are lots of techniques.. Tfidf (term frequency inverse document frequency)  and bm25 might be useful to start with
Comment: TF-IDF, of which Okapi-BM25 is just a special case, is a way to transform the BoW counts, but in and off itself it does not give you a similarity between the (transformed) vectors.
Answer: Let me address this by describing the four maybe most common similarity metrics for bags of words and document (count) vectors in general, that is comparing collections of discrete variables.
Cosine similarity is used most frequently in general, but you should always measure first and make sure that no other similarity would produce better results for your problem, evaluating if you can use some of the more complex measures. Except for Jaccard's set similarity, you can (indeed, might want to) apply some form of TF-IDF reweighing (ff) to your document counts/vectors before using these measures. The TF weighting term might even be  Okapi-BM25, and the IDF term replaced with corpus entropy, but in the end has little to do with the original question at hand (BoW similarity measures).
Note that the first measure, Jaccard similarity, suffers from a significant downside: It is the most heavily affected by length differences among documents, and does not take word frequencies into account, either. For Cosine similarity and the $\chi^2$-distance you can (or rather, should) adjust the vectors by normalizing your vectors to unit length (i.e., in addition to TF-IDF re-weighting). But even then, shorter documents will have more sparse counts (i.e., more zeros in their vectors) compared to longer documents, for obvious reasons. (One way around that sparsity difference could be to only include words above some minimum cutoff, using a dynamic cutoff value that increases with document length.)
Before we go over the measures, it should be mentioned that this discussion  focuses only on symmetric measures, while asymmetric measure exist as well (particularly, the KL-divergence).
1. Jaccard similarity $J$
This is the most simplistic metric: Collect the set of all words in both documents (tweets, sentences, ...), ${A}$ and ${B}$, and measure the fraction of words the two sets have in common:
$$
J_{A,B} = \frac{|{A} \cap {B}|}{|{A} \cup {B}|}
$$
This measure also works for words (and documents) by collecting sets of character (or word) $n$-grams, where $n$ typically is 1, 2, and maybe 3.
Notice that Jaccard similarity has its limits, though; If the sequence of $A$ had been $(x, y, z, x, y, z, x, y, z)$ and that of $B$ were just $(x, y, z)$, their Jaccard similarity nonetheless is perfect (i.e., $1.0$), unless you use the n-gram "trick" just described.
Finally, it should be noted that the Dice coefficient $D$ is a slightly more involved version of the Jaccard similarity, but also does not take word counts into account, and it can easily be calculated from $J$ as: $D = \frac{2J}{1+J}$.
2. Cosine similarity $\alpha$
Cosine similarity is probably by far the most popular metric. Collect ordered sets of all word counts from both documents (tweets, sentences, ...), $A$ and $B$, that is, effectively, two "document vectors" $\vec{a}$ and $\vec{b}$, and measure the Cosine angle between the two vectors:
$$
\alpha_{A,B} = \frac{\vec{a} \cdot \vec{b}}{|\vec{a}||\vec{b}|} = \frac{\sum{a_i b_i}}{\sqrt{\sum{a_i^2}}\sqrt{\sum{b_i^2}}}
$$
Now, because you can normalize your vectors to unit vectors (i.e., the $|\vec{z}|$ parts) before doing this calculation, you actually only need to calculate the dot product between the two vectors to calculate the similarity - which is nothing more than the sum of the products of all pairs. The dot product can be calculated particularly efficient on modern CPUs.
3. Spearman's rank correlation coefficient $\rho$
Instead of using the counts of each word, you order the words of each document by their counts and thereby assign a rank to each word, for either document, $A$ and $B$. That therefor saves you from having to length-normalize the document vectors. Let's use the Wikipedia nomenclature and call the rank generating function (that finds the rank for a given word $i$ from a (word, count) tuple list $X$, or a document vector $\vec{x}$) $rg$. Second, lets define that distance $d$ between the same word $i$ among the two documents as $d_i = rg(A, i) - rg(B, i)$. That is, $d$ the difference among the two ranks, zero if equal, and non-zero otherwise. With this, the coefficient can be calculated from whole numbers as:
$$
\rho_{A,B} = 1 - \frac{6-\sum d_i^2}{n(n^2-1)}
$$
Note that this formulation has a strong requirement: All ranks must be distinct, so even for ties (if two words have the same count in the same document), you need to assign them distinct ranks; I suggest, you use alphanumeric ordering in those cases. And for words that only occur in one document, you place them in the last position(s) in the other document (again using the same alphanumeric ordering). It should be noted that by re-weighting your counts with TF-IDF before calculating this similarity, you can have far less ties among words with non-zero counts.
If you want to maintain ties (i.e., do not want to "artificially" make all ranks distinct) or if you are using cutoffs to remove some of the (word, count) tuples or selecting only the top $n$ most frequent words, however, you should be using the standard formula for $\rho$ (and define $rg$ as a function that generates the complete rank vector for all your document's alphanumerically ordered (word, count) tuples $X$ in the document vector $\vec{x}$):
$$
\rho_{A,B} = \frac{cov(rg(\vec{a}), rg(\vec{b}))}{\sigma_{rg_A} \sigma_{rg_B}}
$$
Where $cov$ is the covariance among the ranks of the the two documents, and $\sigma_{rg_X}$ is the standard deviation of the (possibly cut-off) ranks $rg$ (with ties) of document $X$.
In a nutshell, you could see this approach as half-way between the Jaccard similarity and the Cosine similarity. Particularly, it is (a bit more - see next measure) robust against distributional differences between word counts among documents, while still taking overall word frequency into account. However, in most works, Cosine similarity seems to out-perform Spearman's rank correction - but you should test this, and might get good results with TF-IDF and Spearman, while, as said, this approach does not require you to do length normalization of your document vectors.
4. (Pearson's $\chi^2$ test-based) $\chi^2$ distance
So far, all our similarity measures assume the homogeneity of word frequencies, that is assuming that the variances of our word counts are equal among all documents of a collection (a "corpus"), something that is (hopefully, kind of intuitively) not the case. That is, earlier, Kilgarriff (1997) had demonstrated that the $\chi^2$ is a better fit to compare word counts than our above measures. Note that if you are following current research, for corpus comparisons today (e.g., to do feature selection), however, you probably should be using the Bootstrap test instead of $\chi^2$.
Kilgarriff's $\chi^2$ comparison can be applied just as well to documents (by assuming that there are only two "classes", your two documents, and therefore, $d.f. = 1$), and due to its robustness it might be preferable over the similarity measures shown so far. Note that this approach gives you a distance so to convert this value to a similarity, you'd have to take its inverse (i.e., the larger this distance, the smaller the document similarity). Let us define the count of all words in a document $X$ as $n_X = \sum x_i$ and total of both documents $A$ and $B$ as $n = n_A + n_B$. Then, a $\chi^2$-statistic-based distance for two documents (document vectors) can be derived from Pearson's base formula $\sum \frac{(O_i-E_i)^2}{E_i}$ as:
$$
\chi^2_{A,B} = n \left[ \sum{ \frac{a_i^2}{n_A(a_i+b_i)} } + \sum{ \frac{b_i^2}{n_B(a_i+b_i)} } \right] - n
$$
However, this calculation is computationally far more costly to run, probably in itself explaining why most approaches stick to Cosine similarity, and even more so if you can verify that your classification/retrieval/clustering results don't gain from this latter distance measure. Also, as shown by the linked paper, this distance measure does not seem to out-compete Cosine similarity with TF-IDF reweighing. But note that the linked paper uses the "raw" term frequency counts to calculate this distance measure and compares that to TF-IDF re-weighted Cosine similarity. As discussed already, it usually is a good idea to first normalize the document vectors on their word counts (i.e., lengths) to get good results, i.e., transform $X / n_X$ first (and you might even try applying TF-IDF document vector re-weighting before that, too) in which case the above formula simplifies (a bit) to:
$$
\chi^2_{A,B} = 2 \left[ \sum{ \frac{a_i^2}{a_i+b_i} } + \sum{ \frac{b_i^2}{a_i+b_i} } \right] - 2
$$


Multithreaded programming in MATLAB

Question: I need help in parallel programming in MATLAB.
I have a list of filenames;
for each file I have to do an independent calculation that returns a table row.
All the tableRows should be integrated into one table.
The order of the rows has no meaning.
How do I process all the files in parallel and insert the rows into one table?
samples=dir('*.txt');
for smpl=samples'
   row=processSamples(smpl,prm1,prm2); //should be parallel
   table=[table;row];
end

Thanks
Answer: Matlab has a very useful and easy to use implementation of the parallelized for loop called parfor. See e.g. doc parfor.
Code would be similar to the below depending on the dimension and type of your "row" variable. The point is you have to index into table for parfor to be able to work.
samples=dir('*.txt');
parfor k=1:length(samples)
   smpl = samples(k)
   row=processSamples(smpl,prm1,prm2); //should be parallel
   table(k) = row % Preserves order
end

When the code hits the parfor line it will spend some time (seconds) to set up parallel workers on your local computer. Alternatively you can use parpool to set up the workers on your local computer or a cluster fx.
Exemplifying
n = 10;
y = randi(10,1,n);
squaredy = zeros(n,1) % Not necessary for parfor to work
parfor k = 1:n
    squaredy(k) = y(k)^2
end
disp(y)
disp(squaredy)

Comment: Are you sure this usage of a growing array in `parfor` is legal? It looks fishy to me but I don't have MATLAB right now to test it. Usually `parfor` doesn't allow use with unexpected outcomes.
Comment: the 'parfor' loop can't work like 'foreach' (as it is in the current program), but i changed some lines and it works perfectly
thanks a lot.
Comment: Yeah you might be right about the growing, probably works with preallocated array and indexing. I'll test that, hold on
Comment: Yep, you are right about the growing. You need to use indexing depending on the dimensions and type of your output "row". But good to hear you got it working, I'll update my answer correspondingly

Multithreaded programming in MATLAB


Cyclic modules over a polynomial ring

Question: I think I proved the following proposition.
Is this correct and well-known?
Proposition:
Let $V$ be a finite dimensional vector space over a field $K$.
Let $f: V \to V$ a $K$-linear map.
Let $K[X]$ be the polynomial ring.
$V$ can be regarded as a $K[X]$-module via $f$.
If the characteristic polynomial of $f$ is the minimal polynomial, then $V$ is a cyclic $K[X]$-module,
i.e. generated by a single element of $V$ over $K[X]$.
Comment: @JimBelk "*This should be covered in virtually any graduate-level algebra book.*"
I have Bourbaki and Lang on algebra. I'm afraid I cannot find it covered by them.
Would you please tell me the names of some books that do so?
Comment: Yes, this is a well-known result.
Comment: Is there any reference?
Comment: This should be covered in virtually any graduate-level algebra book.  Check the section on modules, or the material leading up to the proof of Jordan canonical form.
Comment: It will also appear in the material leading up to the Rational Canonical Form; e.g., in Friedberg, Insel, and Spence
Comment: It is covered in chapter 3 of Jacobson's Basic Algebra I.
Answer: This is a consequence of the structure theorem for finitely generated modules over a principal ideal domain $R$, which says that every such module can be written a direct sum of cyclic modules $\bigoplus_{i=1}^lR/(d_i)$ where the generators $d_i$ (invariant factors) divide one another in order: $d_1\mid d_2\mid \cdots \mid d_l$. Take the module to be $V$ as in the question, then the final generator $d_l\in K[X]$ annihilates all the cyclic factors, so (assuming the $d_i$ are taken to be monic) it is the minimal polynomial of $f$, while the characteristic polynomial is the product of all polynomials $d_i$. Clearly the two are equal if and only if $l=1$ (the invariant factors $d_i$ are never $1$) which is the case if and only if $V$ is a cyclic module.
See also this answer to a related question.

Cyclic modules over a polynomial ring


AngularJS : Access an object from any controller

Question: What is the best way to store a "user" object that is accessible across all controllers in an Angular application?
I also have a function that fetches & sets the user.
Option 1: $rootScope
At first I was just setting <code>$rootScope.user = myUser</code>. Then in every control I could use $rootScope. This was fine, until I ended up with a lot of duplicate code since I had to check if it existed at the start of each controller.
<code>// At the start of each controller

if (!$rootScope.user) {
  // fetch and set user
}
</code>
Option 2: Angular service?
Option 3: Angular factory?
References:
angular.service vs angular.factory
Comment: back to the question...if using UI router can add a resolve to abstract state that is parent of all states that require auth. Then you only need a check one place for many routes. Just wire it up to your Auth service
Comment: Missing a fundamental part of $scope inheritance. If it's on $rootScope...$scope in controller will already inherit that property [ultra simple demo](http://plnkr.co/edit/4TfpZzSaACDD2odFCA3s). In other words when you were injecting it and adding it to $scope...it was already there and available. I'm not saying this is how it should be done...more just pointing it out.
Comment: Thanks @charlietfl - I understand that part :) But since a person can open the app on many different controllers, I always need the check.
Comment: That's a really interesting solution. Let me look into that.
Answer: I would use a factory over a service, or $rootScope.
As Ed pointed out below, both a service and a  factory are singletons.
$rootScope, as the name implies is the root of all scopes in an angularjs  application.
Here is something for reference: http://ilikekillnerds.com/2014/12/angularjs-service-vs-factory/
Comment: Still wrong ;) service creates a new instance once, but returns that same instance subsequently.
Comment: This is incorrect. A factory also returns a singleton. Services and factories are very similar but are declared with a slightly different syntax.
Comment: Your are correct, I swapped em accidentally. Corrected above.
Answer: If you are creating a SPA and not creating a scope without parent then use rootScope it will work like a charm.


Proof of an inequality involving $e^x$.

Question:
Prove that $e^{x-1} \geq x, $ for every $x$.

I'm not allowed to use MVT or integrals, but IVT and derivatives are allowed.
I tried to define a function $f(x)=e^{x-1}-x$ and then $\,f'(x)=e^{x-1}-1$ so the function has minimum in $x=1$ where $y=0$, thus the inequality holds.
Is that good? Is there another way which does not involve derivatives?
Thanks!
Comment: The idea is good. How do you know it's a minimum though?
Comment: Second derivative is the easiest way I find.
Comment: @user88595 $f'' > 0$
Comment: @Galc127 Yes that's it. But you need to include it because if it's a maximum, then you just proved that $e^{x-1}\le x \quad \forall x\in \mathbb R$.
Comment: What is "MVT"? "IVT"?
Comment: @sidht: thank you, I know... Just saying it's missing.
Comment: @Shuchang: Mean Value Theorem and Intermediate Value Theorem.
Comment: @user88595, I wrote that it is minimum because I think it's obvious to show it after finding the critical point.
Answer: Your way is correct. Though I don't know another way, I think your way is easy to understand.
Comment: Proof of minimum missing. For what I see, his proof could also show the complete opposite!
Comment: I don't think his proof is for opposite.
Comment: "could". Not saying it is.
Answer: Your proof is correct, assuming you know the exponential function is increasing.  Maybe you should mention it.  Another way, not requiring derivatives, is to note that if $x \leq 1$, then $e^{x-1} \geq e^0 = 1 \geq x$ since $\exp$ is increasing, while if $x \geq 1$, since $\ln$ is increasing, the comparison property of the definite integral gives
$\ln(e^{x-1}) = x-1 =\int_1^x 1\,dt \geq \int_1^x 1/t\,dt = \ln x$, implying $e^{x-1}\geq x$.
Comment: Nice proof. Unfortunately, integrals are not allowed.
Comment: @Galc127 :     thx   %%%
Answer: Make sure that a global minimum is achieved at $x=1$. This is guaranteed if $f'<0$ for $x<1$ and $f'>0$, for $x>1$.
Answer: By Bernoulli's inequality
$$\left(1+\frac {x-1}n\right)^n\ge 1+n\frac{x-1}n=x$$
(as soon as $\frac {x-1}n\ge -1$, which is the case for almost all $n$).
In the limit the left hand side converges to $e^{x-1}$ (by defeinition, depending on the source)
Comment: @Galc127 If the goal is only $\ge x$ (not $>x$ for $x\ne1$) then convergence alone is enough.
Comment: Great! thank you. Do we need also to show that the left hand monotically increasing, thus smaller than the limit and therefore the inequality must hold or is it redundant?
Comment: Understood, thanks a lot.

Proof of an inequality involving $e^x$.


Algebraic Structure of $(\mathbb{N}, \max)$

Question:
Determine which algebraic structure $(\mathbb{N},\max)$ is, where $\max$ is the function that gets 2 elements and return the biggest element.

I have started with associativity:
We need to prove that for all $a,b,c\in \mathbb{N}$ $$\max(\max(a,b),c)=\max(a,\max(b,c))$$
I am trying to generalize the cases so I will not need to check every case.
 So W.L.G., $c$ is the biggest element that is $a<b<c$ therefore:
 $\max(\max(a,b),c)=c=\max(a,\max(b,c))$ but it seems that the argument is flawed but I can not put the finger on the problem
Comment: I think you are being misled by the word group in the question.  $(\Bbb N,\max)$ is not a group.  The operation is associative as you have shown, and $0$or $1$ is an identity (depending on whether you have $0 \in \Bbb N$), but there are no inverses.  You should have some other algebraic structures you have studied in the course and this should fit (at least) one of them.  Without the $\max$ this could be a total order, a partial order, a countable set, or some other things.  None of these have an operation required, so look through your structures for something that needs an operation.
Answer: You are right, your proof for associativity is flawed
The equality
$$\max(\max(a,b),c)=\max(a,\max(b,c))$$
is not symmetric in $a,b,c$ so you cannot use WLOG in the way you did.
Hint
Case 1: $a\leq b$, then $\max(\max(a,b),c)=\max(b,c)$.
Also, you have $a \leq b \Rightarrow a \leq \max(b,c)$.  Thus
$$\max(a,\max(b,c))$$
Case 2: $a >b$. Show the same way that both sides are equal to $\max(a,c)$.
**P.S.**Alternatelly, you can prove that both sides are equal to $\max\{ a, b, c \}$.
Comment: @Charter it is associative as N.S showed and $0\in \mathbb{N}$ is the unit element, but there is no inverse $max(a,0)\neq 0$  as $a>0; \forall a\in \mathbb{B}$ so it is a Monoid
Comment: So $(\mathbb{N},\max)$ is just a magma, right?
Comment: @gbox ups, you're right. I misread the answer and I thought $\max$ wasn't associative.


Unique permutations of list from 2 to N in Python

Question: I am trying to generate a list of permutations in a loop and print for each iteration with output more than two (or write to lines of a file).
Example input list:
['one', 'two', 'three', 'four']

Required output:
['one', 'two', 'three', 'four']
['two', 'three', 'four']
['one', 'three', 'four']
['one', 'two', 'four']
['one', 'two']
['one', 'three']
['one', 'four']
['two', 'three']
['two', 'four']
['three', 'four']

This is what I've managed so far (very early in my Python life, please excuse):
from itertools import permutations

input = ['one', 'two', 'three', 'four']

def convertTuple(tup):
    str =  ''.join(tup)
    return str

while (len(input) > 1):
    permlist = set(permutations(input))
    for i in permlist:
        print(i)
        i = convertTuple(i)
        outfile = open("out.txt", "w")
        outfile.write(i)
    input = input[:-1]
else:
    print("End of permutation cycle")

Which outputs:
('two', 'three', 'one', 'four')
('two', 'four', 'one', 'three')
('three', 'two', 'one', 'four')
('four', 'two', 'one', 'three')
('two', 'one', 'three', 'four')
('two', 'one', 'four', 'three')
('three', 'one', 'four', 'two')
('four', 'one', 'three', 'two')
('one', 'two', 'three', 'four')
('one', 'two', 'four', 'three')
('three', 'four', 'one', 'two')
('four', 'three', 'one', 'two')
('two', 'three', 'four', 'one')
('two', 'four', 'three', 'one')
('three', 'two', 'four', 'one')
('three', 'four', 'two', 'one')
('four', 'two', 'three', 'one')
('four', 'three', 'two', 'one')
('three', 'one', 'two', 'four')
('four', 'one', 'two', 'three')
('one', 'four', 'two', 'three')
('one', 'three', 'two', 'four')
('one', 'three', 'four', 'two')
('one', 'four', 'three', 'two')
('two', 'three', 'one')
('three', 'two', 'one')
('three', 'one', 'two')
('one', 'two', 'three')
('one', 'three', 'two')
('two', 'one', 'three')
('two', 'one')
('one', 'two')
End of permutation cycle

I understand I am going wrong with
input = input[:-1]

as it just removes the last value in the original list but I can't work out how to get only unique lists back with different numbers of values in each list...
Am I using the wrong part of itertools? Should I be using combinations or something else?
I am seriously stuck so any help is very much appreciated!
Thanks!
Answer: Assuming ('one','two','three') was an accidental omit from your wanted list, this should do the trick:
from itertools import combinations

l = ['one', 'two', 'three', 'four']

for size in range(2,len(l)+1):
    for i in combinations(l,size):
        print(i)

Answer:
I can't work out how to get only unique lists back with different numbers of values in each list... Should I be using combinations or something else?

Yes, use combinations to avoid getting the same set in a different order.

I understand I am going wrong with input = input[:-1]

Right; do not remove items from the candidate set yourself. The functionality you want is already built in.
>>> import itertools
>>> help(itertools.combinations)
Help on class combinations in module itertools:

class combinations(builtins.object)
 |  combinations(iterable, r) --> combinations object
 |
 |  Return successive r-length combinations of elements in the iterable.
 |
 |  combinations(range(4), 3) --> (0,1,2), (0,1,3), (0,2,3), (1,2,3)

All you need to do is provide the second parameter (it works for permutations too). Instead of repeatedly removing elements and finding combinations, just iterate over the possible values for output size.
(Incidentally, there is a slick example for this sort of thing in the "recipes" section of the documentation: the one labelled powerset. The power set of a set is basically the thing you're computing, except also including all the one-element results and the empty result.)


Git commit disappeared after merge

Question: I'm working on a project with some collaborators, each having a remote copy of the master branch, which may or may not be up-to-date with the origin master branch.
Suppose one of the collaborators has an out-of-date copy due to a commit X, commits some Y and tries to push it to the origin branch. It fails and the collaborator merges the changes and pushes again, this time successfully.
Now, when looking at the history of the repository, commit X cannot be found anymore, except the changes of it are now in another commit called Merge branch 'master' of https://.....
Is this normal behaviour of git? This makes tracking down changes hard. Is there something that can be done to avoid this issue?
Thanks in advance!
Answer: This is completely normal behavior for Git when using git merge.  Git merge replaces the commits in the source branch with a single merge commit in the destination branch.
One option to preserve history is to use git rebase instead.  In a rebase, your collaborator would first bring in the changes from the remote master branch, and then recommit his work on top.  This would leave his local master ahead of the remote, and he can simply push his changes in.  Consider the following diagram:
remote: A -- B -- C -- X
local:  A -- B -- C -- Y

After doing git rebase origin/master the diagram would look like this:
remote: A -- B -- C -- X
local:  A -- B -- C -- X -- Y'

where Y' is your collaborator's original Y commit slightly modified.  Now your collaborator is in the enviable position of being able to push his work directly onto the remote master branch.  After doing git push origin master from his master branch, the diagram would look like this:
remote: A -- B -- C -- X -- Y'
local:  A -- B -- C -- X -- Y'

Now your collaborator's commit appear intact in the remote master branch.

Git commit disappeared after merge


How to pass required arguments to a custom content type with services?

Question: I am trying to create a Room Reservations Reservation through a Service using the Node Creation resource, but looking at the Room Reservations module code, the module does not allow the creation of nodes, or the passing of the form data for that matters, unless specific parameters are passed as arguments in the URL.
So far I have tried using the node create resource with the following data formatted as JSON, to no success
{"title":"fulano de tal","type":"room_reservations_reservation","reservation_block_title":{"und":[{"value":"0"}]},"reservation_date":{"und":[{"value":{"year":"2016","month":"6","day":"1","hour":"10","minute":"0","second":"0"},"timezone":"America/Bogota","timezone_db":"America/Bogota"}]},"reservation_length":{"und":[{"value":"60"}]},"reservation_repeat_type":{"und":[{"value":"1"}]},"reservation_repeat_until":{"und":[{"value":{"year":"2016","month":"6","day":"1","hour":"10","minute":"0","second":"0"},"timezone":"America/Bogota","timezone_db":"America/Bogota"}]},"reservation_room":{"und":[{"value":"163"}]},"reservation_time":{"und":[{"value":"1000"}]},"reservation_series_id":"","field_codigo_reservation":{"und":[{"value":"654535184561"}]},"field_email_registro":{"und":[{"email":"firstname.lastname@example.com"}]}}

The response is the whole webpage and no node is created.
i think the solution here is to pass the required arguments so that Room Reservations creates the node, would something like the following work?
example.com/endpoint/node.json/add/room-reservations-reservation/6/1/1200/160
Along with the rest of the data in the JSON POST request.
Answer: 1) Firstly you will create your custom resource for POST request.
Add Below Code to your module file.
function Your_Module_Name_services_resources() {
     $resources = array();
     //$_SERVER['REQUEST_METHOD'] = 'POST';
     //Include the necessary inc files.
    module_load_include('inc', 'Your_Module_Name', 'includes/Your_Module_Name.services');

   //Service Apis for contents.
    $resources += Your_Module_Name_resource();
    return $resources;
}

2) Create Your_Module_Name.services.inc file and write below code for your custom callback as given below.
function Your_Module_Name_resource() {
  $api = array(
'applicant_validate' => array( // Resource Name
 'operations' => array(
    'create' => array(
    'help' => 'Validate Applicant',
    'file' => array(
        'type' => 'inc',
        'module' => 'your_module_name',
        'name' => 'includes/your_module_name.services',
    ),
    'callback' => 'your_callback_function_name',
    'access callback' => 'user_access',
    'access arguments' => array('access content'),
    'access arguments append' => FALSE,
    'args' => array(
        array(
        'name' => 'body',
        'type' => 'string',
        'description' => 'Function to perform',
        'source' => 'data',
        'optional' => FALSE,
        'default' => '0',
        ),
    ),
    ),
    ),
  ));
   return $api;
 }

// variable name $body as you mentioned above in args part with name      parameter
function your_callback_function_name($body)
{
  // It's called when you will hit to your web service url from your from submission.

}

Validate Web Service

 <form id="frm" method="post" action="http://example.com/api/endpoint/applicant_validate">
 <input type="text" placeholder="Application Id" name="appno" id="appno"/>
 <input type="text" placeholder="Email Id" name="emailid" id="emailid"/>
<input type="text" placeholder="Mobile Number" name="mobno" id="mobno"/>
<input type="text" name="userpwd" placeholder="Password" id="userpwd"/>
<input  type="submit" value="submit"/>

</form>


static_assert fails compilation even though template function is called nowhere

Question: I use g++ 4.6.3, (currently default package for ubuntu 12.04) with the flag c++0x, and I stumble across this:
template <typename T>
inline T getValue(AnObject&)
{
    static_assert(false , "this function has to be implemented for desired type");
}

with the compilation error:
static_assertion failed "this function has to be implemented for the desired type"

even though I don't call this function anywhere yet.
Is it a g++ bug ? Shouldn't this function be instanciated only if it is called somewhere in the code.
Answer: The standard says in [temp.res]/8

No diagnostic shall be issued for a template definition for which a valid specialization can be generated. If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required. ... [ Note: If a template is instantiated, errors will be diagnosed according to the other
  rules in this Standard. Exactly when these errors are diagnosed is a quality of implementation issue. — end note ]

There is no possible way to instantiate your function template that will compile, so the template definition is ill-formed and so the compiler is allowed (but not required) to reject it even if it isn't instantiated.
You could make it work like this:
template<typename T>
struct foobar : std::false_type
{ };

template <typename T>
inline T getValue(AnObject&)
{
    static_assert( foobar<T>::value , "this function has to be implemented for desired type");
}

Now the compiler cannot reject the function template immediately, because until it is instantiated it doesn't know whether there will be a specialization of foobar that has value == true.  When instantiated the relevant specialization of foobar<T> will be instantiated and the static assertion will still fail, as desired.
Comment: This answer is amazing. I can't believe all the ways people invent to make C++ bow to their will. The language is just vast.
Comment: C++17 alternative: `template  inline constexpr bool always_false = false; static_assert(always_false, "whatever");`.
Comment: Does a template alias do the same or the introduction of a struct is required?
Comment: @GuillaumeRacicot you can't specialize an alias template, so the compiler knows right away if it's always false, and so it wouldn't work.
Answer: That's because the condition does not depend in any way on the template parameters. Therefore, the compiler can evaluate it even before instantiating that template, and produces the associated compilation error message if it the evaluation yields false.
In other words, this is not a bug. Although many things can only be checked once a template is instantiated, there are other validity checks that a compiler can perform even before. This is why C++ has a two-phase name lookup, for instance. The compiler is just trying to help you finding errors that are 100% likely to occur.
Comment: @StephaneRolland: then use `static_assert()`, but make the condition *dependent* on some template argument. Make it such that it will always yield `false` for the arguments you are going to instantiate it with (e.g. `static_assert(typename is_same::value), "Error!")`, but do mention the template argument in the condition. This will tell the compiler: "Hey, this is a dependent name, so wait until instantiation before judging..."
Comment: @StephaneRolland: Note that `static_assert(sizeof(T) == 0, "Error!")` is usually brought up as another solution, but I *think* the concensus is that this can fail "early" as well, because the compiler can know the size is never less than 1 without even looking at `T`. Just to keep that out there.
Comment: @StephaneRolland: I do not remember how Alexandrescu defined it, but that's very likely. After all, this is a new language feature, not just a library. Similarly, if you write a function call to `foo()` in your template function `getValue()`, and no function `foo()` exist, your code won't compile, because the compiler knows that there is no way you could instantiate that template in a way that it would compile.
Comment: Can you confirm that this function is instanciated even though I call it nowhere in the code ? Is that what you said ?
Comment: @StephaneRolland: The function is not instantiated, only the `static_assert()` instruction is executed.
Comment: in which case it is also totally different from the static_assert that Andrei Alexandrescu had proposed in his book "Modern C++ Design".
Comment: So I am in the quest for a another way to static_assert like, but only at template instanciation time :-)
Comment: @StephaneRolland: That trick was actually wrong :-) Or... it did the work, in that it generated a compile-time error, but not the one you probably expected: it was an error evaluating the condition itself, so  the condition never got evaluated to `false`, which in turn means the associated message was not displayed. I updated my comment.
Comment: This is the problem - if you write something like static_assert( N!=N, "oops") ... where N is an 'int' template parm (or a static consexpr derived from template parms), then it works now, but maybe some compiler will notice that it can't ever be true. Seems like there should be a better solution (and, maybe a guarantee that any expression with template dependencies, regardless of how trivial, will be deferred until instantiation? so even `false && N` should work  ).
Comment: The same problem arises with `if constexpr (...) { ... } else if constexpr ( ... ) { ... } else { static_assert(false, "not supported); }`- the `false` needs to be changed to something with a template dependency, or it fails when the template is analyzed.
Answer: This is really a comment, but requiring a code example.
The holy standard's wording for static_assert does not limit its effect to instantiated code.
However, the code
template <typename T>
inline T getValue(int&)
{
    typedef char blah[-1];  // Common C++03 static assert, no special semantics.
}

int main()
{}

also fails to compile with MinGW g++ 4.7.2, which accentuates the question.
I think the answer is that g++ is right, and that Visual C++ 11.0, which does not produce a compilation error for this, is wrong, but I'd be hard pressed to provide the relevant analysis in terms of the Holy Standard's verses.
A practical consequence of the compiler difference is that presently you can not rely on the behavior.
Comment: @JonathanWakely: sorry, forgot to mention, Visual C++ also ignores a `static_assert` in there.
Comment: The template definition is ill-formed, but I think it's a "no diagnostic required" case, so both compilers are right.
Comment: See the quote in my answer, it's allowed to if the template is not instantiated
Answer: just -fdelayed-template-parsing
Comment: Please do not copy and paste the same answer everywhere!


Unsupervised learning examples in Matlab

Question: I am trying to classify ECG data into abnormal and normal using unsupervised learning methods in Matlab. The problem is that whilst I am used to supervised learning algorithms, I have never seen how unsupervised neural networks look like in Matlab. Anyone with advice on what first steps I should take, what are the best features to extract, a tutorial or even any example of such a network?
Answer: K-Means (week-8) from Andrew Ng's Machine Learning class. exp7.pdf explains the example exercise and exp7 folder has the matlab codes.
https://github.com/hrzafer/machine-learning-class
https://class.coursera.org/ml-005/lecture
if this is usefull for you please choose my answer bc i dont have any points : )


How to parse this with SwiftyJSON?

Question: I'm using SwiftyJSON with NSURLSession.sharedSession().dataTaskWithURL and openweathermap.org. I'm getting back data and can pull out values. But I'm not having any luck trying to get "main" in the sample below.
Here's the first part of the JSON that comes back:
{
  "cod" : 200,
  "weather" : [
    {
      "id" : 800,
      "description" : "clear sky",
      "main" : "Clear",
      "icon" : "01d"
    }
  ],

if let data = data {
   let json = JSON(data: data)
   main = json["weather"]["main"].stringValue

main doesn't contain anything.  json["weather"].stringValue also doesn't contain anything.  Any ideas what I'm doing wrong?
Answer: Try doing this to get the first item in the array:
json["weather"][0]["main"].stringValue

According to this line in SwiftyJSON documentation example:
json[1]["list"]

Is the way to get items in a JSON array
Comment: Correct! I think it's worth emphasizing that `json["weather"]` is an _array_, not an _object_, as OP seems to be expecting. If it _were_ an object, then OP's code snippet would work properly.

How to parse this with SwiftyJSON?


Proving $f\colon S \to S$; $f(x) = 1/x$ is bijective

Question: Hey I'm trying to figure out this proof. I don't know if anyone could help but I would really appreciate it!
Let $S = \mathbb{R} \setminus \{0\}$. Prove that the function  $f\colon S \to S$; $f(x) = 1/x$ is bijective.
I understand that to prove that it is bijective, it has to be proven injective and surjective. Injective being one to one and surjective being onto.
Comment: @TrickyTycoon: Not really. You've not indicated any understanding of what "onto" means.
Comment: Do you know what it means to be one-to-one or onto?
Comment: I think one to one is where only one value is assigned for one output, and for onto multiple values can be assigned to one output.
Comment: That isn't the most concrete answer... I would read over the definition of onto and one-to-one again
Comment: @graydad Updated my comment, is that more correct
Comment: @graydad I read the definition of Unto and I am still confused, do you think you could clarify what onto means.
Comment: For sure: Onto means for every element in the codomain, you can find an element in the domain that maps to it. In the context of your problem, that means for every $y \in S$, there exists $x \in S$ such that $f(x) = y$
Comment: In elementary terms, onto means every conceivable (in $S$) value is achieved.
Answer: To prove that $f$ is injective
Assume it isn't,  so $\exists$ two non equal elements $a, b\in S$ s.t $f(a)=f(b)$. So $1\over a$ $=$ $1\over b$ $\implies a=b$. Contradicting that $ a $ and $ b $ are unequal.
To prove that $ f$ is surjective. Choose any $r \in S$. Set $x$ to be s.t $1\over x$$=r$. Hence $x=$$1\over r$. Since $x$ is clearly in the domain of $f$, we have that $f$ is surjective.
Therefore $f$ is bijective.
Comment: Would the proof for injective be invalid because it assumes the conclusion?
Comment: In the proof for injection I let two values in the domain of f have the same value assigned to them by f. However, by simply rearranging f (a)=f (b), you get that this is only possible for when a=b. Sorry I should have mentioned that I rearranged the equation to get a=b
Answer: HINT: Two easy steps:

$f(0,+\infty)=(0,+\infty)$ and $f$ is bijective on $(0,+\infty)$;
$f(-\infty,0)=(-\infty,0)$ and $f$ is bijective on $(-\infty,0)$.
Answer: It is onto because if $r \in \Bbb R - \{0 \}$, then we want $r = 1/x$, so the points $x = 1/r$ precisely satisfies this property.
It is one-to-one because $f'(x) = \frac{-1}{x^2} < 0 \implies f$ is monotone.
Comment: I am a little confused on how it is one-to-one still, how does that imply that  *f*  is monotone?
Comment: No, I am saying it is one-to-one *because* it is monotone. You read it backwards.


How to add Search Dialog to an Android app

Question: Here is my issue. I have a requirement to add a search dialog to an Android app. I have found the way to add a search view component, but not the search dialog. I know from the Android Developers docs that I am supposed to use onSearchRequested() to make it appear, but I don't quite understand what that means. Is the search dialog supposed to be tied to my own UI component that will invoke this method or am I missing something? I haven't managed to read all the docs on searching, but I was hoping someone may point me where I missed something important.
I am not well versed in Android development, only a few small projects. I am more of a Flutter dev when creating a mobile app, but I don't have the option this time around.
Below is the screenshot of what the Android Developers docs show as search dialog and the comparison to search view.
Answer: Based on the documentation, the steps for invoking a Search Dialog are:

create an Xml file (which you already did for search widget)

Declaring a searchable activity: create an actiivty and notify the Manifest that it gets the search results (you did that too)

3. At the Manifest, decalre some other activity (MainActivity for examlpe) to be able to invoke the search dialog. Add this line inside <activity> tag:
<meta-data android:name="android.app.default_searchable"
            android:value=".SearchableActivity" />

(android:value is the name of the Activity that gets the searcg results, which you defined at step 2)
4. At that Activity, create a search button etc and invoke the Serach Dialog like this:
onSearchRequested();

Very simple actually :)
The search words stored in intent.getStringExtra(SearchManager.QUERY) as a String, you get it at onCreate() of SearchableActivity class. From this point you should handle the search query and results logic...
Comment: let me know if yo need more explanations about the first steps. I assumed you did them already
Comment: Ok once I get back to this and try it out I will tell you. Thanks in advance for the help.
Comment: Here is the thing. It seems that I missed the search dialogue in the docs and scrolled my way all the way down to the search view solution, going to try it tonight.
Comment: Done, it works as described. +1 for the docs link.


LINQ filter condition

Question: I have two tables:
People (~10000 Record)
PID Primary Key

PeopleCharater (~500000 Record)
OID Primary Key
PID Foreign Key
RecordDate(DateTime of the record is created)

Its a one to many relation, One PID have multiple OID.
I wan to select a list of OID that matched the Hashset(Of PID) and the RecordDate between a StartDate and EndDate
The tricky part is, The greatest RecordDate that <= StartDate should be included.
Example:
PeopleCharacter
OID PID RecordDate
a   1   2014-10-09
b   1   2015-12-10
c   1   2015-12-15
d   1   2016-02-25

The record retrieved should be a b and c if StartDate is 2015-01-01 to 2015-12-31.
How do I write a LINQ query to retrieve the record?
My Current Code:
Dim PIDHash as Hashset(Of PID)(~10000 PID)
db.Filter(Function(x) x.RecordDate >= StartDate andalso x.RecordDate <= EndDate andalso PIDHash.contains(x.PID)).
   Union(??? another filter to select the greatest RecordDate < StartDate)

The number of times retrieve from database must be limited due to performance issues. Stored Procedure solution is accepted.
Comment: You are retrieving the entire contents of the People table and then sending it back to the database to use as a filter?
Comment: I'm getting the entire People table from from User Input, and retrieve every people character that matched the criteria.
Answer: Finally Figure out with the help of my senior.
Dim query = db.PeopleCharacter.Where(Function(p) PIDHash.Contains(p.PID))
Dim RecordDateQuery = query.Where(Function(y) y.RecordDate <= StartDate).
                            GroupBy(Function(x) x.PID).
                            Select(Function(f) New With {.PID = f.key, .RecordDate = f.Max(Function(t) t.RecordDate)})
Dim results = query.Where(Function(p) p.RecordDate >= StartDate AndAlso p.RecordDate <= EndDate).
                  Union(query.Join(RecordDateQuery,
                                   Function(x) New With {.PID = x.PID, .RecordDate = x.RecordDate},
                                   Function(y) New With {.PID = y.PID, .RecordDate = y.RecordDate},
                                   Function(x, y) x))

Answer: In C# it would be:
var query=db.PeopleCharacter
  .Where(p=>PIDHash.Contains(p.PID));
var results=query
  .Where(p=>p.RecordDate>=StartDate && p.RecordDate<=EndDate)
  .Union(
    query.Where(p=>p.RecordDate<StartDate)
      .OrderByDescending(p=>p.RecordDate)
      .Take(1)
  );

Using an online converter to VB.NET gives:
Dim query = db.PeopleCharacter.Where(Function(p) PIDHash.Contains(p.PID))
Dim results = query.Where(Function(p) p.RecordDate >= StartDate AndAlso p.RecordDate <= EndDate).Union(query.Where(Function(p) p.RecordDate < StartDate).OrderByDescending(Function(p) p.RecordDate).Take(1))

Comment: The Union part only return one OID for the Top Most PID, i need to get OID for every PID.

LINQ filter condition


Can the Levene test be used to explicity test variance inequality instead of equality?

Question: The Levene test is a statistical test that can be used to test the equality of variance between two samples.
I have two paired samples (N = 751 per sample) that clearly have a very different variance, and I would like to statistically show that the two samples' variance is different.
Question: Is it valid to apply the Levene test in my case since my hypothesis is that the variance of both samples is not equal, while the Levene test is frequently used explicity test for equality of variance?
Let me provide an analogy to better clarify my question: One should use an equivalence test if the hypothesis is that the mean between both samples does not signficantly differ between sample one and sample two (instead of using a t-test where a potential p-value above the chosen alpha level does not allow the inference that both samples have identical means). Hence, if the Levene test turns out significant, is it safe to statistically infer that the variances between both samples diverges (or are there theoretical drawbacks that have to be considered)?
Answer: Re-read the rationale for basic hypothesis testing.  Every simple test starts with a null hypothesis which includes the case for equality, even the one-sided tests.  Inequality is always in the alternate (or research) hypothesis.  Tests like Levene's test or the chi-square test are abused by apostate statisticians and naive scientists and called tests of equality or fit, but are actually testing inequality or lack of fit.  Long story short, Levene's test is just what you want to demonstrate unequal variance.  Of course, since you only have two samples, you might want to confirm your result with a folded F-test.


How to analyze clinical outcomes after propensity matching

Question: Suppose I have two propensity matched groups whose covariates are balanced. How do I analyze clinical outcomes between the groups. I would be interested in overall survival, one year survival, and incidence of adverse events such as stroke.

Can I use standard generalized logistic regression models and kaplan meier curves with log rank test?
Do I need to treat the clinical subjects as paired and adjust my analysis appropriately (i.e stratified log rank and Mcnemar test)

Thank you!
Answer: This is debated. Peter Austin strongly recommends accounting for the paired nature of the matched groups. Other methodologies do not. I think it makes sense to do so. Generally, power will be increased if within-pair distances are low. The differences will not be stark, however.
Comment: I have two matched sets with n = 50. I've computed standard kaplan meier curves and a logrank test with p ~ 0.7. After, I tried to account for matches in 2 ways. In the first, I utilized the survdiff() function in the survival package with a cluster(ID) variable to account for pairing. In the second method, I ran the same thing with a coxph function and a cluster(ID) variable. survdiff gives me a value of p <0.0001 whereas coxph gives me a p value close to the baseline 0.7 with a slightly different robust standard error. Which should I trust. Does cluster(ID) not work with survdiff?


How do people measure which tuning freqency were used during Baroque period?

Question: In a website decribing the story of A=415Hz, it points out that

In the Baroque Era, pitch levels as high as A-465 (17th century
  Venice) and as low as A-392 (18th century France) are known to have
  existed. A few generalizations can be made:

However, how do people know what tuning freqency were they using? For example, if you found a harpsichord from 16th century, and you measured A=392Hz when trying to play the A4 note. How do you make sure this frequency is not due to out of tune of the instrument?
I am curious about how do people know the tuning in the past, specifically, before the invention of audio recording?
Comment: Is it the case that some instruments can't be tuned, so once they're manufactured, they'd retain their tuning forever?  I'm thinking of pipe organs.  Also the tuning fork was invented in 1711 (http://americanhistory.si.edu/science/tuningfork.htm), and there could be older fixed devices to set pitch standards.
Comment: Thanks for pointing out the concept of non-tunable instruments. But upon checking, I found that pipe organ can actually go out of tune under different temperatures. (https://www.organforum.com/forums/showthread.php?55319-Organ-Tuning-Is-it-really-necessary) (https://buzardorgans.com/organ-tuning-and-maintenance/)

I wonder how much is the frequency difference between summer and winter.
Comment: I bet acoustics people can reason about the likely climate and building interiors, and put a historical instrument in controlled conditions, and measure its pitch that way.  It's got to be an approximation, but maybe a good one.
Comment: The tuning of pipes doesn't change that much with temperature, and big churches don't change temperature much winter to summer.  Maybe twenty cents max difference.
Answer: Its mostly wind instruments:
HAYNES, Bruce. A history of performing pitch: the story of'A'. Scarecrow Press, 2002.
writes on page 4:

In terms of numbers, I was able to consult the pitches of many
  surviving original instruments, thanks to a grant from the Canadian
  Social Sciences and Humanities Research Council. The present book
  regularly refers to this information, which is included in summary
  form in the appendices; these list the pitches of some 127 cornetts,
  28 Renaissance flutes, 292 traversos, 317 recorders, 70 clarinets, 540
  organs, and 13 pitchpipes, for a total of 1,387 original instruments.
  The appendices include only instruments whose reliability I trust. Of
  these instruments, about 222 are Italian, 208 French, 544 German,
  192 English, 11O Dutch, 77 Belgian, and 31 Austrian.

A change in temperature from 20 to 24 °C will result in a pitch change of 11.7 cent, (PISANI, U. Effect of a Local Temperature Change in an Organ Pipe. Acta Acustica united with Acustica, 1976, 35. Jg., Nr. 2, S. 132-136.) which is not that much, but there are other factors, e.g. wood may change over ages.
Bruce Haynes gives a number for pitch pipes compensated for wood shrinkage:

To compensate for wood shrinkage, pitches given here are 5 Hz lower
  than pipes presently play.

But not only wind instruments play a role, e.g. the tuning forks of Handel and Mozart are preservered, but

The problem with tuning forks is to relate them with assurance to a
  particular place, time, or usage. Unlike pitchpipes (which are often
  stamped and which give note-names), forks offer few clues to their
  date of manufacture and use, or even where they originated.
  (page 31, HAYNES)

And even some stringed (e.g. not bowed) instruments can be taken into consideration because of the physical properties of the used strings:

Wraight found that the most common string-lengths for the note f2 of
  Venetian instruments made between 1523and 1594were 235, 246, 255, and
  265 mm, particularly 235 and 265 mm, which would notes a whole-tone
  apart from the same key of the keyboard. Since at that time there were
  two important Venetian pitches a whole-tone apart, mezzo punto and
  tuono corista, it is logical to associate the two string-lengths with
  the two frequencies (about 464 and 413 Hz, respectively).

(HAYNES, p 23)


Distributions using np.rand and np.randn

Question: I'm working through the Hands on Machine Learning book by Géron:
https://www.amazon.co.uk/Hands-Machine-Learning-Scikit-Learn-TensorFlow/dp/1491962291
As part of the learning material, there is a dplyr jupyter notebook:
https://github.com/ageron/handson-ml/blob/master/tools_numpy.ipynb
I was wondering whether anybody could please explain the difference between the distributions that is outputted in the 'np.rand and np.randn' section. For example, the graphs that are output are as follows:

One is clearly the normal distribution curve, but what is the blue line showing?
Grateful for any explanations.
Comment: A good place to start is the documentation. https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html
Answer: It's almost a convention that RAND() is uniform distribution, and RANDN() is normal. That's the case in your library, and in many libraries elsewhere. For instance, here's matlab's randn and rand functions. Similar in R: rand and randn functions
Answer: I am no Python user but the red line looks like a standard normal distribution and the blue one like a uniform distribution between 0 and 1.
A common way to tackle this kind of question is looking up documentation. I found this, which, if applicable, tells us, that 'rand' is a convenience wrapper for 'random_sample': https://numpy.org/doc/stable/reference/random/generated/numpy.random.rand.html
Documentation to 'random_sample': https://numpy.org/doc/stable/reference/random/generated/numpy.random.random_sample.html#numpy.random.random_sample
says

Return random floats in the half-open interval [0.0, 1.0).
Results are from the "continuous uniform" distribution over the stated interval. To sample Unif[a, b), b > a multiply the output of random_sample by (b-a) and add a:

So the documentation and the result are in line. But reading the documentation also told us, that it is an half-open interval and to which side it is open. If you plan to later take eg. a log, that can be very useful information.


JIRA approval to GITLAB pipeline

Question: We want to integrate JIRA with GITLAB for the approval. When CICD Pipeline runs it deploys to DEV/QA and when it deploys to PROD we want to check two things

One Merge request approval by a manager/code owner
It has be approved by a business user.

We dont want the business user to get access for gitlab and set up an approval there.When JIRA ticket is marked as an approved by him we want the pipeline to run .
Is this scenario possible by using like JIRA API or is there a better way to do this?
Thanks
Answer: As I'm aware there is no such built in functionality, but you can build a bot to do this.
Basically you write an application which listens to a JIRA webhook which is configured to trigger if an issue is updated.
The bot will then trigger the actions for you in Gitlab. Depending on your setup there it could set approvals ( incase you are using the deployment branch model ) or trigger the deployment pipeline after it is already merged by the code owner. This is done using the GitlabAPIs
Finally you can update the issue in JIRA using the API with the status of the deployment
Answer: There's also a way to implement similar functionality without middleware by GitLab checking JIRA issues statuses.
Check how TRIVE DEVOPS did their integration: https://medium.com/@trivedevops/automated-prod-deployments-over-jira-with-gitlab-ci-integration-9bca47a16a66

JIRA approval to GITLAB pipeline


Classification of isolated singularity by limit

Question: Let $z_0$ be an isolated singularity of $f$ so there exist a punctured ball $B'$ centered in the singularity where $f$ is holomorphic.
Let $f(z)=\sum_n a_n (z-z_0)^n$ be the Laurent series of $f$ in $B'$.
1. $z_0$ is a removable singularity iff $\lim_{z \to z_0}f(z)$ exists
2. $z_0$ is a pole iff $\lim_{z \to z_0}f(z)=\infty$
3. $z_0$ is an essential singularity iff $\lim_{z \to z_0}f(z)$ does not exists (either finite or infinite)
Proof of 1.
If $z_0$ is a removable singularity then $f(z)=\sum_{n=0}^{+\infty} a_n (z-z_0)^n$ so it follows $\lim_{z \to z_0}f(z)=a_0$. Conversely if the limit exists, putting $f(z_0):=\lim_{z \to z_0}f(z)$ gives an holomorphic extension of $f$.
Proof of 2.
If $z_0$ is a pole of order $m$ then $g(z)=f(z)(z-z_0)^m$ is holomorphic and so $\lim_{z \to z_0}f(z)=\lim_{z \to z_0}\frac{g(z)}{(z-z_0)^m}=\infty$.
Conversely by hypotesis one has that $\lim_{z \to z_0}g(z)=0$ where $g(z):=1/f(z)$; so $g$ can be extended holomorphic over all the ball and has a zero in $z_0$. It follows that $f$ has a pole in $z_0$.
Proof of 3.
Let $z_0$ be an essential singularity. By Casorati-Weierstrass, $f(B')$ is dense in $\mathbb C$ and so $\forall w \in \mathbb C, \forall \epsilon>0, \exists \zeta \in B'$ such that $|f(\zeta)-w|<\epsilon$. Take $w,w' \in \mathbb C$ with $w \neq w'$, then using the previous statement one can construct two sequences $\{z_n\}$ and $\{u_n\}$ such that $f(z_n) \to w$
 and $f(u_n) \to w'$. So $ \lim_{z \to z_0}f(z)$ does not exist.
Can I use exclusion for the converse? I mean, if the limit of $f$ in $z_0$ does not exist then $z_0$ is not a removable singularity or a pole and so it is an essential singularity.
Comment: Note that if a singularity is an essential singularity, then it's neither a pole nor a removable singularity. And you have proved the first two equivalent statements. So you can automatically get the third equivalent statement. But your proof for the statement $2$ is not complete. I will add some details you missed later.
Comment: u are correct...
Answer: I am writing to add some necessary details to the proof of the statement $\textbf{2}$.
The OP didn't provide his definition of $\textit{pole}$. By inspecting his proof, I guess his definition of $\textit{pole}$ is based on the singular part of Laurent series. To be more specific, his definition might be that, if the Laurent series at a singularity $z_0$ has at least one and at most finitely many terms of negative powers, then $z_0$ is called a $\textit{pole}$.
So in the proof of the converse direction of the statement $\textbf{2}$, we have to show that the Laurent series of $f$ at $z_0$ has at least one and at most finitely many terms of negative powers.
Proof:
First let's suppose that $\text{lim}_{z\to z_0}f(z)=\infty$.
Let $g(z):=\frac{1}{f(z)}$. Then $g(z)$ is well-defined in a ball centered at $z_0$ and $\text{lim}_{z\to z_0}g(z)=0.$ This implies that $g$ is holomorphic in the ball.
Since $g$ is holomorphic in the ball and has a zero at $z_0$, we can find some holomorphic function $h$ s.t. $g(z)=(z-z_0)^k h(z)$ with $h(z_0)\neq0$, where $k\ge 1.$
Since $h$ is holomorphic in the ball and $h(z_0)\neq0$, we may assume $h$ is nonzero in the ball. (Because we can always shrink the ball to a smaller ball, it doesn't really matter.) So $\frac{1}{h}$ is holomorphic in the ball and hence we can write $\frac{1}{h(z)}=\sum_{n=0}^\infty a_n(z-z_0)^n$ for some coefficients $\{a_n\}$ by the Taylor theorem.
So at every point $z$ in the punctual ball (i.e. the ball excluding $z_0$), we have $$f(z)=\frac{1}{g(z)}=\frac{1}{(z-z_0)^k h(z)}=\sum_{n=0}^\infty a_n(z-z_0)^{n-k}=\sum_{n=-k}^\infty a_{n+k}(z-z_0)^n.$$
By the definition, we conclude that $z_0$ is indeed a pole.

$$\tag*{$\blacksquare$}$$


In R how to calculate the difference between two columns dates by group (id) but whilst keeping the first available date as reference

Question: How to compute the time between two columns dates but keeping the first or earliest date as reference, by group.
For example the id N02, the reference date_1 should remains 2009-07-10 until the next id.
I think that I am close but I can't succeed in finding the right solution.
Please find below a minimal working example:
id <- c("N02", "N02", "N03", "N03", "N04", "N04", "N04", "N04", "N04", "N04")
date_1 <- c ("2008-03-15", "2008-04-15", "2008-06-15", "2008-07-15", "2009-07-10", "2009-07-13", "2009-07-15", "2009-07-16", "2009-07-17", "2009-07-20")
date_2 <- c ("2008-03-15", "2008-04-15", "2008-06-15", "2008-07-15", "2009-07-10", "2009-07-13", "2009-07-15", "2009-07-16", "2009-07-17", "2009-07-20")
df1 <- data.frame (id, date_1, date_2)
> df1
    id     date_1     date_2
1  N02 2008-03-15 2008-03-15
2  N02 2008-04-15 2008-04-15
3  N03 2008-06-15 2008-06-15
4  N03 2008-07-15 2008-07-15
5  N04 2009-07-10 2009-07-10
6  N04 2009-07-13 2009-07-13
7  N04 2009-07-15 2009-07-15
8  N04 2009-07-16 2009-07-16
9  N04 2009-07-17 2009-07-17
10 N04 2009-07-20 2009-07-20

My failed attempt:
df2 <- df1 %>% group_by (id) %>% mutate (diff = difftime (date_2, lag (date_1, default = date_1[1]), unit = "day"))
> df2
# A tibble: 10 × 4
# Groups:   id [3]
   id    date_1     date_2     diff
   <chr> <chr>      <chr>      <drtn>
 1 N02   2008-03-15 2008-03-15  0.00000 days
 2 N02   2008-04-15 2008-04-15 30.95833 days
 3 N03   2008-06-15 2008-06-15  0.00000 days
 4 N03   2008-07-15 2008-07-15 30.00000 days
 5 N04   2009-07-10 2009-07-10  0.00000 days
 6 N04   2009-07-13 2009-07-13  3.00000 days
 7 N04   2009-07-15 2009-07-15  2.00000 days
 8 N04   2009-07-16 2009-07-16  1.00000 days
 9 N04   2009-07-17 2009-07-17  1.00000 days
10 N04   2009-07-20 2009-07-20  3.00000 days

However I would like something like this:
id <- c("N02", "N02", "N03", "N03", "N04", "N04", "N04", "N04", "N04", "N04")
date_1 <- c ("2008-03-15", "2008-04-15", "2008-06-15", "2008-07-15", "2009-07-10", "2009-07-13", "2009-07-15", "2009-07-16", "2009-07-17", "2009-07-20")
date_2 <- c ("2008-03-15", "2008-04-15", "2008-06-15", "2008-07-15", "2009-07-10", "2009-07-13", "2009-07-15", "2009-07-16", "2009-07-17", "2009-07-20")
diff <- c("0.00000 days", "30.95833 days", "0.00000 days", "30.00000 days", "0.00000 days", "3.00000 days", "5.00000 days", "6.00000 days", "7.00000 days", "10.0000 days")
df2 <- data.frame (id, date_1, date_2, diff)
> df2
    id     date_1     date_2          diff
1  N02 2008-03-15 2008-03-15  0.00000 days
2  N02 2008-04-15 2008-04-15 30.95833 days
3  N03 2008-06-15 2008-06-15  0.00000 days
4  N03 2008-07-15 2008-07-15 30.00000 days
5  N04 2009-07-10 2009-07-10  0.00000 days
6  N04 2009-07-13 2009-07-13  3.00000 days
7  N04 2009-07-15 2009-07-15  5.00000 days
8  N04 2009-07-16 2009-07-16  6.00000 days
9  N04 2009-07-17 2009-07-17  7.00000 days
10 N04 2009-07-20 2009-07-20  10.0000 days

Thank you in advance for your help.
Charles
Answer: You were almost there - just use [[1]] (or dplyr::first()) instead of lag():
library(dplyr)

df1 %>%
  group_by(id) %>%
  mutate(diff = difftime(date_2, date_1[[1]], unit = "day")) %>%
  ungroup()

# A tibble: 10 × 4
   id    date_1     date_2     diff
   <chr> <chr>      <chr>      <drtn>
 1 N02   2008-03-15 2008-03-15  0 days
 2 N02   2008-04-15 2008-04-15 31 days
 3 N03   2008-06-15 2008-06-15  0 days
 4 N03   2008-07-15 2008-07-15 30 days
 5 N04   2009-07-10 2009-07-10  0 days
 6 N04   2009-07-13 2009-07-13  3 days
 7 N04   2009-07-15 2009-07-15  5 days
 8 N04   2009-07-16 2009-07-16  6 days
 9 N04   2009-07-17 2009-07-17  7 days
10 N04   2009-07-20 2009-07-20 10 days


First few coefficients of a power series

Question: The function $f(x) =\frac{5}{1+16x^2}$ is represented by the power series $$\sum_{n=0}^{\infty} c_nx^n$$
I'm supposed to find the first few coefficients of the power series, and these are the answers that I got:
$c_0 = 5$
$c_1 = -80$
$c_2 = 1280$
$c_3 = -20,480$
$c_4 = 327,680$
Yet the WebWork system is saying that $c_1$ through $c_4$ is incorrect.
I've been using this method to find the $n^{th}$ coeffecient:
$$\frac{f^{(n)}(c)}{n!}$$
where $c$ the center of the series is $0$.
What am I doing wrong?
Comment: See, for example, http://math.stackexchange.com/questions/355158/generating-functions-closed-form-of-a-sequence
Answer: Hint: What you wrote are the first five nonzero coefficients.
Comment: Never mind. After hours of figuring this out it finally came to me :) thank you for your help.
Comment: @Jordan:   Which you calculated correctly.  You did some extra work, though.
Comment: Ah I see. That's very helpful. How do I get all coefficients, including zeroes? I can't seem to figure it out.
Comment: @Jordan You're welcome -- the fact is that the several hours of thinking about it is what makes you remember how to do it in the future.


Spring boot with high concurrency

Question: We have a spring project with a huge of scheduled jobs.  We used @scheduler to perform each special job. But, we want all of the jobs were run in parallel. So in each job, we create a new Thread to perform every 20 tasks, and each task calls to the third-API to get data, process and save to the database. But when we deploy the app on the server, we have trouble with high CPU, it's higher than 100%. How can we solve this problem? Have any framework to manage and perform multiple concurrent scheduled tasks?
Answer: Think about switching to reactive approach with reactor, it has a great and extremely simple mechanism for managing threads and scheduled tasks, on the other hand, this solution imposes some limitations and you may have to rewrite some parts of the system.
Also you can try Quartz

Spring boot with high concurrency