How can we adjust the loss to incorporate beliefs about the test set?

Question: Let's say we've some training data points (not a huge number), collectively referred to as $X$, and we're trying to fit a curve (a polynomial) to these points. Later, we check training and test set errors.
We know for a fact that higher degree polynomials are bound to overfit the data, and relatively lower degree polynomials might give smaller errors on the test set.
Mathematically, though, a (lower degree) polynomial is just a subset of any polynomial with comparatively greater degree. So, it should be possible (is there some way, rather?) for a higher degree polynomial that overfits the training set, and gives wild results on the test set, to perform at least as good as lower degree polynomial that does not overfit (since that's just a subset)
So, how can the loss term be adjusted to incorporate the test set? Ideally, we're not supposed to see the test set until we're done training, so I'm in bit of a confusion here!
Thanks :)
Comment: Regularization?
Comment: @user2974951 It's not obvious to me how regularisation brings us closer to the lower degree solution
Comment: By penalizing more complex models.
Answer: Why not just train your model on the test set? You won't, because this is a bad idea, but for exactly the same reason you shouldn't "correct" for test set. Test set is ought to be "unseen" data, that is used as a proxy to judge potential performance on future data. If you cheat on test set, you'd know nothing about performance of your model on future data. In such cases you cannot say if your data works well, or just overfits the test set.
Another case is when your test set is very different from train set. Both datasets should come from same population, and it should be same population as where you'd be making predictions for. If that's not the case, you should find better data, or prepare it better.


How to link Google AMP pages

Question: I want to link from 1 Google AMP page to another Google AMP page, to optimize the speed of the user's browsing experience.
I already read these articles: links to other AMP pages in an AMP article and https://www.ampproject.org/docs/fundamentals/spec and Link from AMP-Page to AMP-Page?. But these posts just cover whether one SHOULD link AMP to AMP, whereas I'm asking HOW to link.
Before I rewrite my links, what way should I link between AMP documents? Like this:
https://www.foo.com/amp_document.html
or like this:
https://foo-com.cdn.ampproject.org/c/s/foo.com/amp_document.html
I'm not sure if Google will treat an AMP page that is visited from another internal AMP page differently than when the user visits an AMP page from Google's SERPs.
Answer: You can link the AMP pages as

https://www.foo.com/amp_document.html

URLs like
https://foo-com.cdn.ampproject.org/c/s/foo.com/amp_document.html

are created when a user visits the link directly from the search result. You don't need to bother about that.

How to link Google AMP pages


Transparent HTTP tunnel to TCP

Question: I want to open any arbitrary TCP socket on a server, but it's behind a proxy and I can only use a port that is intended for HTTP hosting only. Simply put, what is the most transparent way to wrap such a socket into an HTTP connection? Preferably I would call a *nix program through a shell script in the server that would take care of translating the requests.
I apologize if this was answered before, but I am struggling to find and understand anything.
Answer: I ended up going with Chisel, which provides a single binary for both servers and clients, with features like authentication and reverse port forwarding. For instance:
chisel server

will run an HTTP server in $PORT or 8080, and
chisel client server.com 4567:123

connects to server.com and maps the remote port 123 into the local port 4567.
Other solutions are still welcome, particularly if they involve more transparency, frequently preinstalled tools like netcat, and if they also provide support for other protocols like UDP.

Transparent HTTP tunnel to TCP


How to find all field homomorphisms $\mathbb Q(\sqrt(5+ \sqrt 5 ) \rightarrow \mathbb C$

Question: After computing the minimal polynomial of $\sqrt{(5+\sqrt5}$ in $\mathbb C$, ie. $X^4 -10X^2 +20$. I know that $\mathbb Q \left(\sqrt{(5+\sqrt5} \right)$ = $ \left\{ a+b\alpha + c\alpha^2 + d\alpha^3 | a,b,c,d \in \mathbb Q, \alpha = \sqrt{(5+\sqrt5} \right\}$
Lets say $\phi : \mathbb Q \left(\sqrt{(5+\sqrt5} \right) \rightarrow \mathbb C$ is a homomorphism.
Then it satisfies that for $e$ and $\forall a,b \in Q(\sqrt{(5+\sqrt5})$ $$\phi(e) = e,$$
$$\phi(a+b) = \phi(a)+\phi(b),$$ $$\phi(ab) = \phi(a)\phi(b). $$
How do I find all $\phi?$
I know that $\phi (a+b\alpha + c\alpha^2 + d\alpha^3) = \phi (a) + \phi(b\alpha)  + \phi(c\alpha^2) + \phi (d\alpha^3)$. How should I proceed finding them?
Comment: Do you know Galois theory?
Comment: Another thing you know is that $\phi(1) = 1$, and so since $a,b,c,d \in \mathbb{Q}$, you know they commute with $\phi$ as well. Thus, all you have to do is figure out where $\alpha$ goes. But you have a degree $4$ polynomial in $\phi(\alpha)$ as a variable, so the four solutions in $\mathbb{C}$ will all satisfy the minimal polynomial. Then extend by linearity.
Comment: @KeeleyHoek, I am starting to get acquainted with it.
Answer: Taking your reasoning one step further: \begin{eqnarray*}\phi (a+b\alpha + c\alpha^2 + d\alpha^3) &=& \phi (a) + \phi(b\alpha)  + \phi(c\alpha^2) + \phi (d\alpha^3)\\&=&a + b\phi(\alpha)  + c\phi(\alpha)^2 + d\phi (\alpha)^3.\end{eqnarray*}
Here we have used the fact that any field homomorphism $\mathbb{Q}\to\mathbb{C}$ must be inclusion, as $\phi(1)=1$, so $\phi(n)=n$ for $n\in\mathbb{Z}$ and hence $\phi(\frac1n)n=\phi(\frac1n)\phi(n)=1$ for $n\in \mathbb{Z}$.
From this we see that the field homomorphisms are determined completely by $\phi(\alpha)$.  We know $\phi(\alpha)$ satisfies the minimal polynomial of $\alpha$:
\begin{eqnarray*}
\alpha^4-10\alpha^2+20&=&0
\\
\implies \phi(\alpha)^4-10\phi(\alpha)^2+20&=&0.
\end{eqnarray*}
Here we apply $\phi$ to both sides of the first line, to get the second line.
Thus there are four possibilities for $\phi(\alpha)$: $\pm\sqrt{5\pm \sqrt{5}}$, hence four possibile field homomorphisms.


Trill measure 29 3rd movement "Moonlight" sonata (Beethoven op. 27 no. 2)

Question: How do you play the trill in measure 29 of Beethoven's "Moonlight" sonata?
Answer: I would play it like this (the image came out kind of big...):

You can play the lower A# quite short to avoid tension (if it suits your interpretation, of course). For me the most natural fingering is 4-5-4-3-4-5 in the upper voice, although 3-5-3-2-3 works, too, and might be easier if your hand is big and/or 4th finger weak.
There's a similar trill a couple bars later, which I find much harder. The fingering 4-5-4-3-4-5 works there, too, or you may try 5-3-5-3-5-4 or something similar. In both cases it helps to play the first note of the trill a bit longer and stronger than the others, and it works musically, too.
Comment: Thanks! This sounds good to me.. I noted that there are 16th notes in the bass, so I got a 4 on 5 situation.. but I guess its played so fast (presto) that it does not matter..
Answer: Beethoven always started his trills on the upper note (documented here), unlike modern practice. This would come out more fluent coming onto the G#. Although Nonpop's example could work, it's not normal to start and end on the same note. A better interpretation would be B  A# B A# G# A#. As far as fingering, without context, it is hard to say what would work best, but 4-3-4-3-2-3 should work. As far as timing, whatever goes with your interpretation is fine. I've shown two alternate timings below.
Comment: The thing is, trills in Beethoven aren't so black and white. In fact, [here](http://www.appca.com.au/proceedings/2007/papers_part_1/herscovitch_ornamentation_beethoven.pdf) is a document which concludes that Beethoven most likely preferred to start on the main note (starting on page 5). I'll probably go to the library today, so I might as well see if I can find anything there, too.
Answer: There is a general consensus that the trill should start on the principle note (A#) with finger 4 and consist of 5 total notes, including the trill ending. Beyond that, there is little agreement. There are dissenters in all aspects, especially with regard to fingering.
Here is a table reflecting the choices of various editors.

                   Starting
    Editor         Note        Rhythm        Fingering
    --------------------------------------------------
    Lebert/v.B.    A#          3+2 or 2+3    ---
    Schultze       A#          5             4..34
    Casella        A#          5             4....
    Weiner         A#          5             45323
    Schnabel       A#          ---           45.24
    Schenker       A#          ---           35...
    Reimann        B           ---           ---
    Cooper         B           6             ---

Basis for the table
Lebert/von Bülow1

Schultze2

Casella3

Weiner4

Schenker5

Schnabel6

Reimann7

Riemann's [1885] editorial decision to begin his trills on the upper note upsets the sentiment that nineteenth century pianists would always start their trills on the main note. (p. 76)

Trills and turns had two primary functions during the nineteenth century. Those
located at the beginning of a strong beat served as accenting ornaments. Contrariwise,
those assigned to weak beats would act as connecting embellishments. The number of
notes within a trill, the rhythm of the shaking notes, and the trill endings depended on the taste of individual performers. Tracing back from the eighteenth-century convention,
pianists during the early nineteenth century usually began trills with the upper note on the strong beat. Later, an increasing number of pianists preferred to start on the main note. (p. 22)

Cooper8

Sources
1Sigmund Lebert and Hans von Bülow: https://imslp.org/wiki/Piano_Sonata_No.14%2C_Op.27_No.2_(Beethoven%2C_Ludwig_van)
2Clemens Schultze: Ibid.
3Alfredo Casella: Ibid.
4Leó Weiner: Ibid.
5Heinrich Schenker: Ibid.
6Artur Schnabel: Ibid.
7Hugo Reimann: King Yue Li, 2020, "Nineteenth-century Performance and Editorial Practice: A Study of Beethoven's Sonata in C-sharp minor, Op. 27 No. 2", Ph.D. diss, Arizona State University, https://repository.asu.edu/attachments/227327/content/Li_asu_0010E_19894.pdf
8Barry Cooper: Barry Cooper, 2007, Beethoven: The 35 Piano Sonatas Volume 2 (The Associated Board of the Royal Schools of Music).
Answer: I'm inclined to lean towards playing it as whatever sounds best. The quintuplet starting on the main note feels right to me - having been attempting to play bar 32 with a convincing trill starting on the upper note for a while and not really succeeding, it feels likely to me that it is supposed to start on the main note, which is much easier to execute accurately. The other way just seems a bit messy.


Is there a common name for the major II chord found in minor key progressions?

Question: The most common progression for this II chord to be found is the i-II-iv-V or variations as in the following examples:
Bossa Dorado
Music To Watch Girls By
Libertango
I could expect something like Phrygian II (Neapolitan II) or Lydian II (Beatles progression) but I cannot find something similar. (Maybe harmonic II?)
Answer: I've come across this chord quite often, but I've never heard any specific name for it, so I think that there is none. It is usually played as a dominant seventh chord, e.g. in A minor it would be a B7 chord. Some people would analyze it as V7/V (i.e. a secondary dominant for the dominant chord: B7 is the V of E7, which is the V of A minor). In your example progression the actual dominant chord (E7) is then delayed by the subdominant (Dm). So according to this analysis you could call the B7 chord a secondary dominant, even though this is no specific name for that exact chord because there can be many different secondary dominants. (Note that in Dutch there exists a special name for the V/V secondary dominant: wisseldominant, but I haven't come across any translation of this term into other languages.)
While I don't think that the above analysis is wrong, I believe that a better explanation is that this chord is actually a reharmonization of a diminished chord. So the original progression would be
Am(7) Ebdim7 Dm(7) E7
where the Ebdim7 is a descending non-dominant diminished chord, which works because of chromatic resolution. If you consider the progression Am7-Ebdim7-Dm then there is a chromatically descending two-part line:

Am7 - Ebdim7 - Dm
G   -  Gb    - F
E   -  Eb    - D

This is why the Ebdim7 resolves nicely to Dm, even though it has not dominant function.
What is happening in the progression you referred to is that the diminished seventh chord is reharmonized as a dominant seventh chord. This works because an Ebdim7 is enharmonically equivalent to a B7(b9) chord without the root:

Ebdim7: -  Eb Gb Bbb Dbb  (or Eb Gb A C)
B7:     B  D# F# A   C

I've come across what I call the 'original' progression (with the diminished seventh chord) mainly in Tango music.
Comment: @Chris: Right, the chord would be called Adim7, and it's enharmonically equivalent to Ebdim7 (as are Cdim7 and F#dim7). For me both progressions (with dominant or diminished) share the same character, because the only real difference is the root of the chord, which doesn't add that much color. But that's all subjective of course.
Comment: Your progression surely works well, but to my ears it lacks the unexpectancy of the major II. I think that one could write your progression -and please correct me if I am wrong- as such: Am7-Amb5dim7-Dm-E7
and that could work too, right?


Get minimum value of masked array

Question: I have a masked array, from which I'd like to return the index of the minimum value.  Further, I'd like to return the index of a randomly chosen minimum if there are multiple.  In the example below, this should randomly return
index 4 or 5:
import numpy as np
import numpy.ma as ma
import random

my_mask = [1, 0, 0, 1, 0, 0]
my_array = [ 0.018, 0.011, 0.004, 0.003, 0.0, 0.0]
masked_array = ma.masked_array(my_array,my_mask)

min_indices = np.where(masked_array.min() == masked_array)
min_index = np.random.choice(min_indices[0])

print masked_array
print min_index

My problem: The masked elements are treated as zero (?), and any element from  {0,3,4,5} can be returned.
My question: What is a good way to return the index of a (randomly chosen) minimum from an array (excluding masked values)?
Comment: As far as I can tell, `masked_array.min` does ignore the masked values. You actually have zeros in the valid portion of your array.
Answer: Use ma.where() instead of np.where()
min_indices = ma.where(masked_array == masked_array.min()))
print(min_indices)

Which gives:
(array([4, 5]),)

The ma module has a lot of functions that are designed to work with masked arrays.
Finally, grabbing a random element from this result would be something like:
min_index = np.random.choice(min_indices[0])

Get minimum value of masked array


UITableView is crashing when trying to edit cell

Question: I am having some form in that is consisting of 5 custom cells in a table view. Here is the code:
<code>    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;}

\- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

\- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"in here");
    static NSString *MyIdentifier = @"MyIdentifier";

    FreeSignupCell *cell = (FreeSignupCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        NSLog(@"shazam");
        cell = [[[FreeSignupCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    switch (indexPath.row) {
        case 0:
            NSLog(@"index 0");
            [cell setLabelContent:@"First Name"];
            //self.firstName = [cell getText];
            break;
        case 1:
            NSLog(@"index 1");
            [cell setLabelContent:@"Last Name"];
            //self.lastName = [cell getText];
            break;
        case 2:
            NSLog(@"index 2");
            [cell setLabelContent:@"Company (optional)"];
            //self.company = [cell getText];
            break;
        case 3:
            NSLog(@"index 3");
            [cell setLabelContent:@"Website (optional)"];
            [cell setText: @"http://"];
            //self.website = [cell getText];
            break;
        case 4:
            NSLog(@"index 4");
            [cell setLabelContent:@"E-mail"];
            //self.email = [cell getText];
            break;
        default:
            [cell setLabelContent:@"E-mail"];
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70.0;
}
</code>
The problem is that when I edit first field, that scroll quickly to the last one, app crashes with objc_mssend error.
Any ideas?
Comment: Looks like your problems must be somewhere inside FreeSignupCell class... Can't see any problems in the code you posted
Comment: Does the error occur if you scroll up and down without doing any editing? Can you post the code to FreeSignupCell? Try turning on NSZombie to see what is being freed.
Comment: No, just when I try to edit. It is interesting that it occurs in simulator, but not on the device. I am confused
Answer: Another issue... Here is how I am getting data from the page
<code>-(void) SignupAction {

//Fetching signup data from signup form
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
FreeSignupCell *cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.firstName = [cell getText];

path = [NSIndexPath indexPathForRow:1 inSection:0];
cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.lastName = [cell getText];

path = [NSIndexPath indexPathForRow:2 inSection:0];
cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.company = [cell getText];

path = [NSIndexPath indexPathForRow:3 inSection:0];
cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.website = [cell getText];

path = [NSIndexPath indexPathForRow:4 inSection:0];
cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.email = [cell getText];

//JSON Dictionaries require parameters that are not null
if (self.firstName == nil) {
    self.firstName = @"";
}
if (self.lastName == nil) {
    self.lastName = @"";
}
if (self.company == nil) {
    self.company = @"";
}
if (self.website == nil) {
    self.website = @"";
}
if (self.email == nil) {
    self.email = @"";
}
</code>
When I am pressing SignUp button on the page I only see website and email fields. What happens is that when I log variables, only email and website are there, other ones are @"". What is happening?
Answer: When the FreeSignupCell goes away (say because it scrolled off the top or bottom, or gets reused), and the getText from that destructed cell goes away and gets [text release];'d on you, what happens to your self.firstName, self.lastName, self.company, self.website, and self.email that now refers to a string object that was free()'d?
I think you should use [[cell getText] retain]; instead.


Expand Datastore Question

Question: I added storage to my LUN and want to expand the datastore in vCenter. Is it as easy as running the Expand wizard? Can it expand on the fly with VM's running? Do I need to backup my VM's before I do this? Will I lose and data?
This is a production array and I can't afford anything to happen to the data on this array. I would appreciate any help with this.
Thanks,
Answer: Yes it's that easy and yes it can safely be done 'live', though the VMs may momentarily pause for a very short period of time as you do the expand so you may wish to schedule it to happen at a time of low usage. You may also wish to backup your VMs first if you wish.
Comment: I'd still recommend having backups up to date and checked before doing it. I know it's supposed to work and probably will, but if it's important...I'd rather waste the time making backups and having them than leaving it to chance.
Comment: I second the suggestion to do it at a low I/O time. The applications that'll have problems with this kind of operation will be the ones that are sensitive to I/O lags. This operation will introduce one, and you don't want other normal traffic to make the lag worse.

Expand Datastore Question


CancellationTokens in parallel threads

Question: I am posting this partly out of intrest on how the Task Parallel Library works, and for spreading knowledge. And also for investigating whether my "Cancellation" updates is the reason to a new issue where the user is suddenly logged out.
The project I am working on have these components:

Web forms site. A website that acts as portal for administrating company vehicles. Further refered as "Web"
WCF web service. A backend service on a seperate machine. Further refered as "Service"
Third party service. Further refered as "3rd"

Note: I am using .NET 4.0. Therefore the newer updates to the Task Parallel Library are not available.

The issue that I was assigned to fix was that the login function was very slow and CPU intensive. This later was later admitted to be a problem in the Third party service. However I tried to optimize the login behavior as well as I could.
The login request and response doesn't contain perticularly much data. But for gathering the response data several API calls are made to the Third party service.
1. Pre changes
The Web invokes a WCF method on the Service for gathering "session data".
This method would sometimes take so long that it would timeout (I think the timeout was set to 1 minute).
A pseudo representation of the "GetSessionData" method:
var agreements = getAgreements(request);
foreach (var agreement in agreements)
{
    getAgreementDetails(agreement);
    var customers = getCustomersWithAgreement(agreement);
    foreach (var customer in customers)
    {
        getCustomerInfo(customer);
        getCustomerAddress(customer);
        getCustomerBranches(customer);
    }
}

var person = getPerson(request);
var accounts = getAccount(person.Id);
foreach (var account in accounts)
{
    var accountDetail = getAccountDetail(account.Id);
    foreach (var vehicle in accountDetail.Vehicles)
    {
        getCurrentMilageReport(vehicle.Id);
    }
}
return sessionData;

See gist for code snippet.
This method quickly becomes heavy the more agreements and accounts the user has.
2. Parallel.ForEach
I figured that I could replace foreach loops with a Parallel.ForEach(). This greatly improved the speed of the method for larger users.
See gist for code snippet.
3. Cancel
Another problem we had was that when the web services server is maxed on CPU usage, all method calls becomes much slower and could result in a timeout for the user. And a popular response to a timeout is to try again, so the user triggers another login attempt which is "queued"(?) due to the high CPU usage levels. This all while the first request has not returned yet.
We discovered that the request is still alive if the web site times out. So we decided to implement a similiar timeout on the Service side.
See gist for code snippet.
The idea is that GetSessionData(..) is to be invoked with a CancellationToken that will trigger Cancel after about the same time as the Web timeout. So that no work will be done if no one is there to show or use the results.
I also implemented the cancellation for the method calls to the Third party service.
Is it correct to share the same CancellationToken for all of the loops and service calls? Could there be an issue when all threads are "aborted" by throwing the cancel exception?
See gist for code snippet.
Comment: A couple of recommendations: profile your call to find the **real** perf bottlenecks. Use O(1) lookup collections - i.e. use `Dictionary` or `HashSet` to determine whether you've already seen a particular object instead of using LINQ. Limit the degree of parallelism of your `Parallel.ForEach` to avoid it creating new threads continuously when it encounters a blocking call (i.e. `lock`, `Task.Wait` or external service IO), as this will saturate your thread pool.
Comment: if getting data timeout over 1 minute you must be loading way too much informations than you need to. I load over 36 thousands records over 425 different tables and it doesn't take more than 7 seconds. Only load what's necessary it will help on that point. Your loops can very likely be replaced with an inner join each.
Comment: @Franck The thing is that I am dependent on the Third party service which currently (pre patch) is slow when under pressure. I could refactor the website to only ask for the info when needed, but that would require a large overhaul of the current implementation. Now we are looking for a quick solution for fixing the performace issues
Comment: @LazyTarget updated my answer
Answer:
Is it correct to share the same CancellationToken for all of the loops and service calls? Could there be an issue when all threads are "aborted" by throwing the cancel exception?

Yes, it is correct. And yes, there could be an issue with throwing a lot of exceptions at the same time, but only in specific situations and huge amount of parallel work.
Several hints:

Use one CancellationTokenSource per one complete action. For example, per request. Pass the same Cancellation Token from this source to every asynchronous method
You can avoid throwing an exception and just return from a method. Later, to check that work was done and nothing been cancelled, you you check IsCancellationRequested on cts
Check token for cancellation inside loops on each iteration and just return if cancelled
Use threads only when there is an IO work, for example, when you query something from database or requests to another services; don't use it for CPU-bound work

I was tired at the end of working day and suggested a bad thing. Mainly, you don't need threads for IO bound work, for example, for waiting for a response from database of third service. Use threads only for CPU computations.
Also, I reviewed your code again and found several bottlenecks:

You can call GetAgreementDetail, GetFuelCards, GetServiceLevels, GetCustomers in asynchronously; don't wait for each next, not running all four requests
You can call GetAddressByCustomer and GetBranches in parallel as well
I noticed that you use mutex. I guess it is for protecting agreementDto. Customers and response.Customers on addition. If so, you can reduce scope of the lock
You can start work with Vehicles earlier, as you know UserId at the beginning of the method; do it in parallel too

CancellationTokens in parallel threads


Condition for the existence of periodic solutions

Question:
Suppose the functions $a,f \in C(\mathbb{R} \rightarrow \mathbb{R})$ are both periodic with period $\omega.$ There are three parts for this question. I'm still trying to figure out the first part.
  Show that $x'+a(t)x=f(t),$ has a unique $\omega$-periodic solution if and only if $\displaystyle{\int_{0}^{\omega} a(t)~dt \neq 0}.$

My approach: Since both $a,f \in C(\mathbb{R} \rightarrow \mathbb{R})$ are periodic with period $\omega,$ we have $a(t+\omega)=a(t), f(t+\omega)=f(t)$ for all $t \in \mathbb{R}.$ \
$``\Rightarrow"$: Suppose that the given equation has a unique $\omega$-periodic solution.  Multiplying it throughout by the I.F. $\displaystyle{e^{\int_{0}^{\t} a(t)~dt}}$ yields
$$\frac{d}{dt}\left(  x(t) \cdot e^{\int_{0}^{t} a(s)~ds} \right)=f(t) e^{\int_{0}^{t} a(s)~ds}.$$
$$\Rightarrow x(t)=e^{-\int_{0}^{t} a(s)~ds} \cdot \int_{0}^{t} f(u) e^{\int_{0}^{u} a(s)~ds}~du.$$
From the periodicity, $x(t)=x(t+\omega),$
$$\int_{0}^{t} f(u) e^{\int_{0}^{u} a(s)~ds}~du = e^{-\int_{t}^{t+\omega} a(s)~ds} \cdot \bigg\{ \int_{0}^{t} f(u) e^{\int_{0}^{u} a(s)~ds}~du+\int_{t}^{t+\omega} f(u) e^{\int_{0}^{u} a(s)~ds}~du \bigg\}.$$
$$\Rightarrow \left( 1 - e^{-\int_{t}^{t+\omega} a(s)~ds} \right) \cdot  \int_{0}^{t} f(u) e^{\int_{0}^{u} a(s)~ds}~du  = e^{-\int_{t}^{t+\omega} a(s)~ds} \cdot \int_{t}^{t+\omega} f(u) e^{\int_{0}^{u} a(s)~ds}~du.$$
I've gotten only upto this far. I need help in solving this. Thank you.
Answer: You are missing the initial value in the solution formula. With that you get
$$
(e^{A(t)}x(t))'=e^{A(t)}f(t)\implies e^{A(t)}x(t)-x(0)=\int_0^t e^{A(s)}f(s)\,ds
$$
and using periodicity
$$
e^{A(ω)}x(ω)-x(0)=(e^{A(ω)}-1)x(0)
=\int_0^ω e^{A(s)}f(s)\,ds
$$
and you only get a value for $x(0)$ if $e^{A(ω)}-1\ne 0\iff 0\ne A(ω)=\int_0^ωa(s)\,ds$.

In the other direction, if $x(ω)=x(0)$ then by uniqueness $x(t+ω)=x(t)$ for all $t$ which implies periodicity.


How to pause UWP app when the user presses the back button on root page?

Question: I am trying to find a way how to pause UWP application (not exit). The idea is to show a warning dialog with two options (Yes or No) when the user is on the root page and presses the back button (physical one).
I have been trying to find a solution online and the closest thing I found was calling - Application.Current.Exit in the UWP project.
Comment: What do you mean by "pause" an app? This might be helpful - [How to handle back button press](https://stackoverflow.com/questions/33571836/how-to-handle-back-button-pressed-for-uwp)
Comment: I think that is the state that the app goes to when the user "exists" the app by pressing the back button on the root page of the stack. That causes the user to be taken to the OS - out of the app.
Answer: If you want to have a confirmation dialog when user presses the back button, you need to handle the SystemNavigationManager.BackRequested event. You will mark it as Handled and present the user with a confirmation dialog. If the user presses Yes, you can then force exit the application with Application.Current.Exit().
However this presents the problem that the app is not suspended but terminated, which means it is not paused. That's because presenting the dialog is an async task and event handler must return immediately. You can manually call your suspend logic to save state, but there is no way to programmatically trigger the system based suspend flow...
Comment: Unfortunately there is no way to do so programmatically as of now... Only user can trigger the system back navigation
Comment: Thank you for your answer. Calling Application.Current.Exit is not ideal for me. Do you know if the back button can be triggered programmatically? My idea is to invoke a back button after the user selects "Yes". That is basically the scenario that the user experiences without the confirmation dialog.
Comment: Thank you again for your help. I am accepting your answer although the solution is not ideal in my scenario (i.e. terminating the app) but I do understand that currently it is impossible to be done. :)


Allow user to control access to own content for other authenticated users

Question: I just want a field in a content type, so that when a user creates new content he/she can select whether that content can be viewed by other authenticated users or not. I prefer doing this with a selection field displaying 'hidden/visible' to other users and then set the actual access in a custom module using some hook.
This is what I tried so far in my custom module hook entity access with content type test_adgang and checking field_test_adgang, but no effect, meaning all authenticated users can still see, if field set to 'skjult' (hidden).
<code>
<?php

use Drupal\Core\Access\AccessResult;

function import_functions_entity_access(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Session\AccountInterface $account) {
    if ($entity->bundle() == 'test_adgang') {
        if ($entity->field_test_adgang->value == 'skjult' && $operation == 'view' && in_array('godkendt bruger', $account->getRoles())) {
            return AccessResult::forbidden();
        } elseif ($entity->field_test_adgang->value == 'synlig' && $operation == 'view' && in_array('godkendt bruger', $account->getRoles())) {
            return AccessResult::allowed();
        }
    }

}

?>

</code>
Comment: You aren't returning the result of your hook. That's one problem. Check the docs of the hook you are using.
Comment: Thanks, return was missing, works now :-)
Answer: The Rabbit Hole module allows you to specify if users can view the node. The first setting is for the content type, but there is a setting there to allow that to be set on a per node basis. You can look at using that module, or look at what they're doing to solve this.


sequential call using promise inside loop

Question: i want to perform synchronous operation of functions using promise. I have loop that passes the data to be inserted to insert function and after inserting one row i want to check the no. of rows exists in table hence i am performing select operation.
But the issue is if there are 3 records then it inserts all 3 records and after that my select function gets executed. what i want is after insertion of one record select function gets called.
here is my pseudo code as entire code involves lot of operations
for(var i=0; data.length ; i++){

 self.executeFeedbackTrack(data);

}

executeFeedbackTrack:function(callInfo){
    var self=this;
   return  self.insertFeedbackTrack(callInfo).then(function(data){

                console.log("insertFeedbackTrack status "+status);

        return self.getFeedbackTrack();

    });
},

getFeedbackTrack :function(){

    return new Promise(function(resolve,reject){
         var objDBFeedbackTrack = new DBFeedbackTrack();
        objDBFeedbackTrack.selectFeedbackTrack(function(arrayCallRegisters){
            if(arrayCallRegisters){

            console.log("notification.js no. of feedbacks "+arrayCallRegisters.length);

            resolve(arrayCallRegisters.length);

            }

        });
    });

},
insertFeedbackTrack :function(callInfo){

return new Promise(function(resolve,reject){
     var objDBFeedbackTrack = new DBFeedbackTrack();
     objDBFeedbackTrack.insertFeedbackTrack(callInfo.callNumber,callInfo.callServiceType,function(status){

             resolve(status);
            $('#loader').hide();

     });
});

}

Comment: Please see provided answer.
Answer: The previous answer is good, but if you are using nodejs, or babel, or you are using only modern browsers. You can use an async-await pair, it is es8 stuff.
let insertFeedbackTrack = function(){ return new Promise(/***/)};
let getFeedbackTrack = function(){ return new Promise(/***/)};
let processResult = async function(data){
   let feedbacks = [];
   for(let i=0;i<data.length;i++){
      let insertedResult = await insertFeedbackTrack(data[i]);//perhaps you will return an id;
      let feedbackTrack = await getFeedbackTrack(insertedResult.id);
      feedbacks.push(feedbackTrack);
   }
   return feedbacks;
}

processResult(data).then(/** do stuff */)

Answer: It looks to me like this is caused by executing a series of asynchronous inserts, and assuming that the get of insert n (inside of a .then()) is called before insert n+1 is executed. However, I'm not aware of any such guarantee, in JavaScript; all that I'm familiar with is that then n will be called after insert n, not that it would be called before insert n+1.
What I'd suggest is avoiding this mix of traditional and callback-based code, and instead put the iteration step inside getFeedbackTrack().then. Assuming this understanding of the issue is correct, then something like the following should work:
function iterate(i) {
    if (i < data.length) {
        obj.insertFeedbackTrack(data[i]).then(function(insertResult) {
            self.getFeedbackTrack().then(function(getResult) {
                // this line is the important one, replacing the `for` loop earlier
                iterate(i+1);
            });
        });
    }
}

iterate(0);

By doing that, you would guarantee that insert for the next element does not occur until the current select executes successfully.
Naturally, you may also want to restructure that to use chained .then instead of nested; I used nested rather than chained to emphasize the ordering of callbacks.
Comment: Do you have synchronous versions of your functions, and can you afford to block the (sole) execution thread while running DB operations?
Comment: you are trying to replace for loop with the recursive function ?
Comment: Yep, exactly so - replace the for loop with a recursive chain of `then` callbacks, in order to force a specific ordering of the events/callbacks.
Comment: well i have to do lot of changes just to change the for loop , is there any workaround
Answer: This can be solved by using a very handy JS library Ramda. Concept is to use two methods, one is R.partial and another is R.pipeP.
First create a promises array from your data array, like following.
var promises = data.map(function(i) {
  return R.partial(sample, [i])
});

Then you can pass this promise to R.pipeP, so that it can be executed one after another. like below.
var doOperation = R.pipeP.apply(this, promises)

Please execute following snippet attached.

// Sample promise returning function
function sample(d) {
  return new Promise(function(resolve, reject){
    setTimeout(function() {
      console.log('resolved for:' + d);
      resolve(d);
    }, 1000)
  })
}

// Sample data
var data = [1, 2, 3, 4, 5]

// Converting data array to promise array
var promises = data.map(function(i) {
  return R.partial(sample, [i])
});

var doOperation = R.pipeP.apply(this, promises)
doOperation();

So in your case, the code will look like this
var promises = data.map(function(i) {
  return R.partial(self.executeFeedbackTrack, [i])
});
var doOperation = R.pipeP.apply(this, promises)
doOperation();

Answer: Here's how I would sequentially call promises in a loop (I'm using ES7).
First, let's define some basic data:
const data = [0,1,2,3];

Then, let's simulate some long running process, so let's create a function that returns a Promise (you can think of this as a simulated network request, or whatever suits your needs)
const promiseExample = (item) =>
  new Promise((res) => {
    setTimeout(() => {
      console.log('resolved ', item);
    res(item);
  }, 1000);
});

Now, let's create an array of promises. What the next line of code does is: for every item in the array data, return a promise factory. A promise factory is a function that wraps a certain promise without running it.
const funcs = data.map(item => async () => await promiseExample(item));

Now, the actual code starts here. We need a function that does the actual serialization. Since it has to handle an array of promiseFactories, I split it in two functions, one for the serialization of a single promise, and one for handling an array of promiseFactories.
const serializePromise = promiseFactoryList =>
  promiseFactoryList.reduce(serialize, Promise.resolve([]));

const serialize = async (promise, promiseFactory) => {
  const promiseResult = await promise;
  const res = await promiseFactory();
  return [...promiseResult, res];
};

Now, you can simply call it like this:
serializePromise(funcs).then(res => {
    console.log('res', res);
});

As you can see, the code is pretty simple, elegant, functional, and doesn't need any external dependency. I hope this answers your question and helps you!

const serializePromise = promiseFactoryList =>
  promiseFactoryList.reduce(serialize, Promise.resolve([]));

const serialize = async (promise, promiseFactory) => {
  const promiseResult = await promise;
  const res = await promiseFactory();
  return [...promiseResult, res];
};

const data = [0,1,2,3];

const promiseExample = (item) =>
  new Promise((res) => {
 setTimeout(() => {
   console.log('resolved ', item);
   res(item);
    }, 1000);
  });

const funcs = data.map(item => async () => await promiseExample(item))

serializePromise(funcs).then(res => {
  console.log('res', res);
});
Comment: @Hunt please check this out :)
Answer: I use yield for such cases if using generator functions.
for(var i = 0; i < order.tasks.length; i++){
        if(order.tasks[i].customer_id === 0){
          var name = order.tasks[i].customer_name.split(" ")
          const customers = yield db.queryAsync(
            `INSERT INTO customers(
              business_id)
             VALUES(?)
            `,[order.business_id])
        }
      }

Or else I use self-calling functions in case of callbacks.
var i = 0;
(function loop() {
    if (i < data.length) {
        task_ids1.push([order.tasks[i].task_id])
        i++;
        loop();
    }
}());

Answer: I ran into this problem recently and solved it as shown below. This is very similar to the answer by @Ethan Kaminsky, but only uses callbacks. This may be useful for people avoiding promises for whatever reason.
In my application the asynchronous function may fail and can safely be retried; I included this logic because it's useful and doesn't overly complicate the routine, but it is not exercised in the example.

// Some callback when the task is complete
function cb(...rest) { window.alert( `${rest.join(', ')}` ) }

// The data and the function operating on the data
// The function calls "callback(err)" on completion
const data = [ 'dataset1', 'dataset2', 'dataset3' ]
const doTheThing = (thingDone) => setTimeout( thingDone, 1000 )

let i = -1                    // counter/interator for data[]
let retries = 20              // everything fails; total retry #

// The do-async-synchronously (with max retries) loop
function next( err ) {
    if( err ) {
        if( ! --retries ) return cb( 'too many retries' )
    } else if( ! data[++i] ) return cb( undefined, 'done-data' )

    console.log( 'i is', i, data[i] )
    doTheThing( next, data[i] )       // callback is first here
}

// start the process
next()


AndEngine how to make a sprite not jump if jump method is called

Question: So I have this method that when i touch the screen my sprite will jump. Now the problem is when I continuously touch the screen the sprite will jump again and again. What I want to do is if it jumps, the jump method cannot be called unless my sprite hits the ground.
Here is the code.

public class PhyiscsActivity extends BaseGameActivity implements IAccelerometerListener, IOnSceneTouchListener{
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private Camera mCamera;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mTextureRegion;
private Scene mScene;
private FixtureDef mFixtureDef = PhysicsFactory.createFixtureDef(1,-10f, 0.5f);
private PhysicsWorld mPhysicsWorld;
private Body body;
private AnimatedSprite facebox;
private FixtureDef wallfixture = PhysicsFactory.createFixtureDef(-1,0.5f, 0.5f);
@Override
public Engine onLoadEngine() {
    // TODO Auto-generated method stub
    this.mCamera = new Camera(0,0,CAMERA_WIDTH, CAMERA_HEIGHT);
    final EngineOptions mEngineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),this.mCamera);
    mEngineOptions.getTouchOptions().setRunOnUpdateThread(true);
    return new Engine(mEngineOptions);
}

@Override
public void onLoadResources() {
    // TODO Auto-generated method stub
    this.mBitmapTextureAtlas = new BitmapTextureAtlas(128,128, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    this.mTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(mBitmapTextureAtlas, this, "gfx/face_box_tiled.png", 0, 0, 2, 1);
    this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}

@Override
public Scene onLoadScene() {
    // TODO Auto-generated method stub
    this.mEngine.registerUpdateHandler(new FPSLogger());
    this.mScene = new Scene();
    this.mScene.setBackground(new ColorBackground(1,1,1));
    this.mPhysicsWorld = new PhysicsWorld(new Vector2(0,SensorManager.GRAVITY_EARTH),false);

    // This is the walls
    final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
    final Shape ground = new Rectangle(0,CAMERA_HEIGHT - 2,CAMERA_WIDTH,2);
    final Shape left = new Rectangle(0,0,2,CAMERA_HEIGHT);
    final Shape right = new Rectangle(CAMERA_WIDTH -2, 0,2,CAMERA_HEIGHT);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallfixture);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld,  roof, BodyType.StaticBody, wallfixture);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallfixture);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallfixture);

    // This is the Sprite
    facebox = new AnimatedSprite(30,(CAMERA_HEIGHT - 2) -  this.mTextureRegion.getHeight()  ,this.mTextureRegion);
    body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, facebox, BodyType.DynamicBody, mFixtureDef);
    this.mScene.attachChild(facebox);

    this.mScene.registerUpdateHandler(this.mPhysicsWorld);
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(facebox,body,true,true));
    this.mScene.setOnSceneTouchListener(this);
    return this.mScene;

}

@Override
public void onLoadComplete() {
    // TODO Auto-generated method stub

}

//Accelerometer
@Override
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
    // TODO Auto-generated method stub
    final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX()* 3, 10);
    this.mPhysicsWorld.setGravity(gravity);
    Vector2Pool.recycle(gravity);
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    this.enableAccelerometerSensor(this);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    this.disableAccelerometerSensor();
}

//This is where i make the sprite jump
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
    pSceneTouchEvent.isActionDown();{
        this.jump(facebox);
    }
    return false;

}
public void jump(AnimatedSprite sprite){
    boolean jumping = false;
    if(!jumping ){
        body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));
        }
    }

public void jump(AnimatedSprite sprite){
    if(!jumping){
       body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-10));
       jumping = true;
    }

    }

@Override
public void beginContact(Contact contact) {
    // TODO Auto-generated method stub
    jumping = true;
}

@Override
public void endContact(Contact contact) {
    // TODO Auto-generated method stub
jumping = false;
}

I don't know why the beginContact and endContact isn't initialzing. are there things that i should do to initialize this? example like updating a handler? contactlistener?
Answer: You need to have a method wich tell you if your player is jumping or not.
public void jump(AnimatedSprite sprite){

    if( isJumping(sprite)){
        body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));
        }

In this method you should put the way to compute if the player is jumping. Like if he is touching a wall (if player's body (x,y) is in a wall (rectangle))
Ok you are using box2d to make physics =)
You have to create a ContactListener, you have to implement this class and the function ou want to add.
http://code.google.com/p/andenginephysicsbox2dextension/source/browse/src/com/badlogic/gdx/physics/box2d/ContactListener.java?r=1605f6e82f710ef9ebbe07632d6b055239d3b520
public void beginContact (Contact contact);
public void endContact (Contact contact);

The contact object will countain your objects (Fixture) that are touching. You just have to check and set jumping to false when the body is touching and true when you call jump and you  can jump, set jumping to true;
Ok?
@Override
public void beginContact(Contact contact) {
    // TODO Auto-generated method stub
    jumping = true;
}

@Override
public void endContact(Contact contact) {
    // TODO Auto-generated method stub
jumping = false;
}

This is false.. I would think of it like that only:
@Override
public void beginContact(Contact contact) {
    jumping = false; //you touched ground so you aren't jumping anymore
}

@Override
public void endContact(Contact contact) {
jumping = true; //you leave ground so you're jumping
}

and one jump method (not two):
   public void jump(AnimatedSprite sprite){
        if(!jumping ){
            jumping = true;
            body.setLinearVelocity(new Vector2(body.getLinearVelocity().x,-8f));
            }
        }

You see?;
Comment: can you show me how to do that? the one to tell if my player is touching the wall.
Comment: You have to tell me how your wall are stored in memory.
Comment: Going to try it. I'll let you know!
Comment: Do I have to start the begincontact method? Or it will run as long as the 2 fixtures collide?
Comment: It will run when 2 fixture will contact
Comment: edited; Look at it and maybe write all the states on a paper to see it clearly.
Comment: I tried it but it's still the begin contact and end contact isn't running right. What happens is I can make it jump the first time, now when it hits the ground the begincontact isn't running. What is the code to make it run?
Comment: Sorry i don't have time to help you more: You should look at http://www.box2d.org/documentation.html for more informations.
Comment: after experimenting with codes i came up with this.   this.mPhysicsWorld.setContactListener(this);
it calls the begin contact and end contact now. Thank you so much you really helped me a lot.
Comment: Great! You can select my answer o close this topic ;)


API Facebook - Secure Canvas URL

Question: Estou tentando fazer com que um aplicativo funcione no Facebook e tenha interação com o site que estou desenvolvendo. O objetivo é que o usuário faça login pelo Facebook no site.
Acontece que toda vez que tento adicionar o site nas configurações de API do Facebook, acusa esse erro:
This must be derived from Secure Canvas URL, Mobile Site URL, Unity Binary URL, Site URL ou Secure Page Tab URL.

Vejam que na imagem tem o campo App Domains. É esse campo que não entra de jeito nenhum. É obrigatório ser HTTPS ? Não entendo.
Se sim, como faço para configurar isso no servidor ?
E no campo Secure Page Tab URL tem que por esse websiteseguro.com para funcionar. A questão é dá para enganar o Facebook e deixar assim. Mas estar assim o aplicativo não funciona no Facebook, mas funciona no site.
Enfim, Facebook até pra isso está muito chato de usar.
Sé Loco Cachorrera!
Answer: Consegui resolver essa questão.
O Facebook nesse campo aceita apenas o protocolo HTTPS.
Ou seja, se quisermos colocar alguma página do site em uma TAB (Aba) no Facebook precisamos contratar o serviço SSL da hospedagem, que faz com que o domínio também seja acessado pelo protocolo HTTPS.

API Facebook - Secure Canvas URL


OpenModelica Interactive Simulation with OPCUA and Python

Question: EDIT: See the linked forum discussion (here) for more info.
I'm a new user of OpenModelica. I'm using a Raspberry Pi as a (opc-ua) test server, which sets some of my server nodes automatically. I am also connected to the server via UaExpert to read/write nodes from my PC. I would like to connect an OM model to this server so I can pull server node values as inputs for the model's simulation.
I took a dive into the OM forum and found a topic similar to this. In that discussion, the OM model looked like this:
model SimulationTest
  input Real RPM;
  Real Torque;
equation
  RPM = Torque;
end SimulationTest;

They also had a python script that connected to the server and modified some node values. Here is a similar test script:
from opcua import Client
from opcua import ua
import time
import logging
from opcua.ua.uatypes import VariantType

# Define the URL on which the server is broadcasting
url = "opc.tcp://my.pi.ip.address:4840/"

if __name__ == "__main__":
    client = Client(url)
    logging.basicConfig(level=logging.WARN)

    try:
        client.set_user("username")
        client.set_password("password")
        client.connect()
        print("Client connected!")

        # Set default node values
        engineRPM_command = client.get_node("ns=4;s=engineRPM_command")
        engineRPM_command.set_value(800, VariantType.Int32)
        RPM = engineRPM_command.get_value()
        print("Current state of engineRPM_command : {}".format(RPM))

        engineTORQUE_current = client.get_node("ns=4;s=engineTORQUE_current")
        engineTORQUE_current.set_value(1.0, VariantType.Float)
        Torque = engineTORQUE_current.get_value()
        print("Current state of engineTORQUE_current : {}".format(engineTORQUE_current.get_value()))

        # Change node values over time
        while (True):
            engineRPM_command.set_value(int(1.1*RPM), VariantType.Int32)
            RPM = engineRPM_command.get_value()

            engineTORQUE_current.set_value(1.5*Torque, VariantType.Float)
            Torque = engineTORQUE_current.get_value()

            print("RPM value is: ", RPM)
            print("Torque value is: ", Torque)
            time.sleep(2)
            print("="*40)

    except KeyboardInterrupt:
        print("Stopping sequence!")

    finally:
        print("Done!")
        client.disconnect()

To get the interactive simulation environment setup, I configured the Additional Simulation Flags (Optional): -rt 1.0 -embeddedServer opc-ua in the Simulation Flags tab under Simulation Setup. When I simulate the model, some files are produced (.exe, .log, .json, .xml, and .mat) but I can't see any plots in OMEdit.
The big questions are (1) how do I properly connect to my pi server from OMEdit, (2) how do I pull node values for the simulation, and (3) can I access this opcua node data from within OMEdit at Simulation time?
Thanks
Answer: OMEdit implements its own client and uses that for reading data and plotting.
If you want to use your own client then I suggest you should not use OMEdit and instead run the simulation from the command line using the simulate command. See https://build.openmodelica.org/Documentation/OpenModelica.Scripting.simulate.html. Basically you call something like simulate(SimulationTest, simflags="-rt=1.0 -embeddedServer=opc-ua -embeddedServerPort=4841") and then use the data you receive in python client to plot results.


centering a navbar when screen resizes

Question: I am trying to centre my navbar, after my screen resizes to a smaller size.  I have made sure that my class containing my navigation: .menu has the following
display:
inline-block;
margin: 0px auto;

It still doesn't centre...
However, my logo contained in the div above, has the same properties and it is able to centre when the screen resizes.
My menu HTML

    <button type="button" class="navbar-toggle" data-toggle="offcanvas" data-target=".sidebar-nav">

    </button>

A

B

C

D

E

Can anybody give me some insight?
Comment: Give Us the code from the parent to the last child of this navbar..
Comment: please make a fiddle: https://jsfiddle.net/
Comment:

A
B
C
D
E
Comment: I think my nesting is ok... I can send you the code for the relevant classes to if that will help.
Answer: display: inline-block; needs text-align: center; on the parent element for it to center like you're thinking because it's an inline element.
Instead, use display: block; margin: 0px auto;. You'll also need to set the width if it's not all the way across.
Examples
Comment: Thanks so much DACrosby, it didnt centre either... I am gonna have another look at the code and make sure there is not some float attached to one of the the classes in my css that prevents it from centering.. When I inspect element, nothing like that jumps out to me..
Comment: What's the menu doing now? Can you send a link to your website, or better yet create a jsFiddle that replicates the issue? [Here's a starting point.](http://jsfiddle.net/daCrosby/szz0zjqk/).
Comment: I will figure out shortly how to to do jsFiddle in order for you to see it... the website is www.gothrive.io   When you change the screen size to anything below 1200px and above 768px you will notice that the menu covers the top-logo. This is because I recently made a change to the website by adding a section "the Thrive process". My current solution is to try have the logo move above the menu when the screen resizes. Thanks for you insights in advance.
Comment: Your logo is `display: block` with a width set, so add the auto margin to center it: `.logo-top { margin: 25px auto 0; }`. It still overlaps your nav bar because the nav is floated so remove the float on `.nav-sidebar`. Then your logo is below the nav, if you want to the best fix is to simply put it before the nav in your HTML instead of after.

centering a navbar when screen resizes


Bluesnap Card Holder First Name and Last Name

Question: I would like to use one input field for card holder name instead of two inputs for first name and last name. How should I split the input from one card holder name into first and last name?
Is there an alternative solution to this problem?
Comment: Are you using API or BuyNow page?
Comment: I'm using the API
Answer: If you're using the API, it's up to you to decide how you want to split it. Matching the card holder name exactly is not crucial for the approval of the transaction. You can split it on the first whitespace character or split it on the last space. Just make sure you have validations to enforce having at least 2 names in the input field.


Estimando Pi no JavaScript

Question: Estou tentando estimar Pi, porém recebo um NaN.

var x = 0
var y = 0
var fora = 0
var dentro = 0
var f = document.querySelector('input#t')
var t = Number(f.value)
var pi = document.querySelector('div#pi')
var atual = 0
var distance = 0

function start() {
  while (atual < t) {
    x = Math.random()
    y = Math.random()
    distance = x ** 2 + y ** 2
    if (distance <= 1) {
      dentro += 1
      atual += 1
    } else {
      fora += 1
      atual += 1

    }
  }
  pi.innerHTML = 4 * dentro / fora
}

Calculador de pi

Insira um número de tentavicas
<input type="number" name="t" id="t"><input type="button" value="start" onclick="start()">

asdas

Comment: `4*dentro/fora` se nunca incrementar o valor de "fora" será uma divisão por zero, pq a variável é iniciada assim `var fora = 0`, deveria iniciar com 1, que é um valor "neutro" na divisão
Answer: Alguns erros presentes no seu código:

iniciar a variável t fora da função start. Nesse caso, a variável será executada quando o HTML for criado, e nessa etapa, o valor da caixa de texto está vazia, fazendo com que f.value seja '' e Number('') seja NaN.

dividindo dentro por fora ao invés de atual.

Você pode corrigi-los dessa forma:

var f = document.querySelector('input#t')
var pi = document.querySelector('div#pi')

function start() {
  var x = 0
  var y = 0
  var fora = 0
  var dentro = 0
  var atual = 0
  var distance = 0
  var t = parseInt(f.value)

  while (atual < t) {
    x = Math.random()
    y = Math.random()
    distance = x ** 2 + y ** 2
    if (distance <= 1) {
      dentro += 1
      atual += 1
    } else {
      fora += 1
      atual += 1
    }
  }
  pi.innerHTML = 4 * dentro / atual
}

Calculador de pi

Insira um número de tentavicas
<input type="number" name="t" id="t"><input type="button" value="start" onclick="start()">

asdas

Comment: Realmente, eu tinha colocado o valor de F como o input quando na verdade era pra ser o atual
E sobre o valor fora da função realmente era o que estava dando o NaN. Muito obrigado pela ajuda, agora posso continuar os estudos ! (:

Estimando Pi no JavaScript


Android SQLiteDatabase insert() method throws Null Pointer Exception

Question: I have written a method which allows you to add a "word" to a database. It throws a Null pointer exception when it attempts database.insert() once I call the addWord() method.
public class DatabaseControl {

// columns for table
public static final String KEY_ROWID = "_id";
public static final String KEY_SYNID = "synsetID";
public static final String KEY_WORD = "synsetID";
public static final String KEY_DESCRIPTION = "description";
public static final String KEY_POPULARITY = "popularity";

// table name
public static final String DATABASE_TABLE = "wordnet"; // Name of the Database

private Context context;
private SQLiteDatabase database;
private DatabaseHelper dbHelper;

public DatabaseControl(Context context){
    this.context = context;
}

public DatabaseControl open() throws SQLiteException {
    dbHelper = new DatabaseHelper(context);
    database = dbHelper.getWritableDatabase();
    return this;
}

public void close(){
    dbHelper.close();
}

public long addWord (String synId, String word, String description, int popularity) {
    ContentValues setUpVals = createContentValues(synId, word, description, popularity);
    // SEEMS TO THROW NULL POINTER EXCEPTION
    return database.insert(DATABASE_TABLE, null , setUpVals);
}

I call this method here:
private void populateDatabase(){
    try {
         long rowId = 0;
         rowId = dbControl.addWord("testSyn","","",1); // Add a row to the database for test
    }
    catch (SQLiteException e){
      e.printStackTrace();
    }

Don't really understand why it's throwing this error, and I don't fully understand the second paramater within insert() so it could be that for all I know :/
Comment: do you ever call `open()` ?
Comment: Can you please post the full stack trace?
Comment: Ahh, it would probably help if I did call open() afterall :/
Answer: Try out this way:
private void populateDatabase()
{
    try {
        long rowId = 0;
        rowId = dbControl.addWord("testSyn",null,null,1);
    }
    catch (SQLiteException e){
        e.printStackTrace();
    }


Delete .csv columns based on partial header pattern

Question: Does anyone know of an awk or linux command that removes .csv file columns based on a partial pattern match within header? I tried many examples, but it either did nothing or removed all columns.
Example: I want the columns removed that have the header name "(RAW)" within it.

G_TP01_OPER_ID,G_TP01_OPER_ID(RAW),G_TP02_PROC_NO,G_TP02_PROC_NO(RAW)
4008636142,4008636142,4008636142,4008636142,4008636142

Desired output:

G_TP01_OPER_ID,G_TP02_PROC_NO
4008636142,4008636142

Thank you!
Comment: You have 4 headers and 5 data columns
Comment: Yes hek2mgl, I meant to post just a snippet of a very large file and accidentally didn't match the headers and data columns correctly. Per my comment below, there are many fields that have the "(RAW)" pattern (about 200 or so) throughout the header that I need to remove via partial pattern match.
Answer: awk -F, '{print $1,$3}' OFS="," file

G_TP01_OPER_ID,G_TP02_PROC_NO
4008636142,4008636142

Comment: I apologize,  this appears to only remove columns 1 & 3. I was in a hurry when I posted this, but I meant for it to remove all the columns with "(RAW)" in the headers, which is about 70 or so throughout a very long header row. My fault for not explaining that.
Comment: This is what I meant in the previous comment:  "I apologize,  this appears to only retain columns 1 & 3. I was in a hurry when I posted this, but I meant for it to remove all the columns with "(RAW)" in the headers, which is about 200 or so throughout a very long header row. My fault for not explaining that. So I need it to remove the columns based on a partial pattern "(RAW)" . It would also be nice if there was an opposite command to retain many columns based on a partial pattern (for a different file of mine).  Thanks!"
Comment: Hi Claes : your answer would be better with some context. If you include an explanation of how/why this is intended to solve the questioner's problem, it is easier to understand - both for the questioner themselves and for other readers looking for solutions to similar problems.
Comment: In my post I just showed to print the columns with now RAW.
Evidently there are many more columns and I have tried to
elaborate with !/RAW/ not print columns with RAW and have
not succeeded at all. Sorry I cant produce any better!
Answer: Here's a shell version, to be fed via standard input:
read
fields=-f
sep=
i=0
IFS=,
for field in $REPLY
do  # collect fields without "(RAW)"
    ((i=i+1))
    if [[ $field != *"(RAW)"* ]]
    then    # output header name, append field number to $fields
            echo -n "$sep$field"
            fields+="$sep$i" sep=","
    fi
done
echo
cut -d"," "$fields"

Comment: Armali, where do I place my argument (input file) with this script?
Comment: @Adrian Chavez: By _to be fed via standard input_ I meant input redirection such as `script <input_file`.
Comment: Okay, let me try this. Thank you!


How to determine intervals where $f$ is greater than $g$?

Question: I have two functions, $f(x) = 2x$ and $g(x) = \frac{x^3}{3}$.  I solved for $x$ where $f = g$, finding $x = \pm 6^{1/2}$, then solved for $x$ where $f > g$, $x > \pm 6^{1/2}$, and where $f < g$, $x < \pm 6^{1/2}$. However it is clear there is overlap for where I have stated $f > g$ and $f < g$. How do I show that $f > g$ only when $x \in (0, 6^{1/2})$ and $f < g$ only when $x \in (- 6^{1/2}, 0)$ without graphing?
Completely different approaches from mine are welcomed!
Answer: The roots, $x = 0, -\sqrt{6}, +\sqrt{6}$ show merely the crossover points.  You must test the four regions defined by those three points:

$-\infty < x \leq -\sqrt{6}$
$-\sqrt{6} \leq x \leq 0$
$0 \leq x \leq +\sqrt{6}$
$\sqrt{6} \leq x < \infty$
Answer: You forgot the solution $x=0$.
By the Intermediate Value theorem the relative positions of the curves can change only at intersection points. Now, for $x\gg 1$, $f(x)<g(x)$, hence we conclude

if $x>\sqrt 6$, $f(x)<g(x)$,
if $0<x<\sqrt 6$, $f(x)>g(x)$,
if $-\sqrt 6<x< $, $f(x)<g(x)$,
if $x<-\sqrt 6$, $f(x)>g(x)$.
Comment: By "x ≫ 1", do you mean as x approaches 1? How did you come to the conclusion that x = 1 was a significant point to analyze?
Comment: No, it means $x$ tends to $+\infty$ ('much greater than $1$).
Answer: Solve the inequality
$$
2x>\frac{x^3}{3}
$$
that can be rewritten as
$$
x(x-\sqrt{6})(x+\sqrt{6})<0
$$
The expression on the left changes sign only at $-\sqrt{6}$, $0$ and $\sqrt{6}$ (where it vanishes). Since its value at $1$ is $-5$ you have the diagram
$$
\begin{array}{ccccccc}
\_\!\_\!\_\!\_\!\_\!\_\!\_  & -\sqrt{6} &
\_\!\_\!\_\!\_\!\_\!\_\!\_ & 0 &
\_\!\_\!\_\!\_\!\_\!\_\!\_ & \sqrt{6} &
\_\!\_\!\_\!\_\!\_\!\_\!\_ \\
& & & & <0 &
\end{array}
$$
and you can complete it by changing sign at the stated points
$$
\begin{array}{ccccccc}
\_\!\_\!\_\!\_\!\_\!\_\!\_  & -\sqrt{6} &
\_\!\_\!\_\!\_\!\_\!\_\!\_ & 0 &
\_\!\_\!\_\!\_\!\_\!\_\!\_ & \sqrt{6} &
\_\!\_\!\_\!\_\!\_\!\_\!\_ \\
<0 & =0 & >0 & =0 & <0 & =0 & >0
\end{array}
$$
So the intervals where the original inequality holds are $(-\infty,-\sqrt{6})$ and $(0,\sqrt{6})$.
Comment: Why is it necessary to know what the value of the 2nd function is at x = 1?
Comment: @galacti-cat97 You just need *one* value besides those where the expression vanishes. I chose $1$, it could be $3$; just choose an "easy" one.


Back to views search result

Question: View search results gives node list based on exposed filters applied when making the search.
After user picks a result (node) and reads it there is no easy way to get him back to search results.
Exposed filters are "remembered", but the option to click back in browser does not seem user friendly, so i am looking for a module to do this. Is there any ?
Answer: Maybe you could check the selected answer here How to provide back link to page? just to confirm if you really want this kind of functionality and it also suggests some options that you can do and it's "non-recommended" level.
Hope that helps.
Comment: Yep, i only want to get back to views result page without using back button. Solutions on your link are close but i am not sure how to implement it. Also all answers there from 2013, so maybe some solution came up in meantime.

Back to views search result


Stack Based Column Sum in a data frame using R

Question: My existing data frame looks line given below
NAV_Date    NAV    Year  Day       Units   Amount  Balance_Units
2013-06-01  282.5  2013  Saturday  3.540   1000    3.540
2013-06-08  279.3  2013  Saturday  3.581   1000    3.581
2013-06-15  276.0  2013  Saturday  3.623   1000    3.623
2013-06-22  261.6  2013  Saturday  3.822   1000    3.822
2013-06-29  273.3  2013  Saturday  3.659   1000    3.659

I want my new data frame to have Balance_Units column have entries like given below using R Language
i.e Balance unit column should be the sum of previous value and present value
This is need to be done on the list of dataframe
NAV_Date    NAV    Year  Day       Units   Amount  Balance_Units
2013-06-01  282.5  2013  Saturday  3.540   1000    3.540
2013-06-08  279.3  2013  Saturday  3.581   1000    7.121
2013-06-15  276.0  2013  Saturday  3.623   1000    10.744
2013-06-22  261.6  2013  Saturday  3.822   1000    14.566
2013-06-29  273.3  2013  Saturday  3.659   1000    18.225

I tried this but , this is not working
 for( i in 1:length(W)) {
  W[[i]]$Units  = 1000/W[[i]]$NAV
  W[[i]]$Amount = 1000
  W[[i]]$Balance_Units = 0
  W[[i]]$Balance_Units = W[[i]]$Units + W[[i]]$Balance_Units
   }

Comment: Do you want cumulative sum (shown in your current post) or sum of current row + previous row?
Answer: This can be done with a convenient function cumsum in base R
df1$Balance_Units <- cumsum(df1$Balance_Units)

With dplyr, it can be created within mutate
library(dplyr)
df1 %>%
    mutate(Balance_Units = cumsum(Balance_Units))

If 'W' is a list of data.frames, we can use lapply
W <- lapply(W, transform, Balance_Units = cumsum(Balance_Units))

data
df1 <- structure(list(NAV_Date = c("2013-06-01", "2013-06-08", "2013-06-15",
"2013-06-22", "2013-06-29"), NAV = c(282.5, 279.3, 276, 261.6,
273.3), Year = c(2013L, 2013L, 2013L, 2013L, 2013L), Day = c("Saturday",
"Saturday", "Saturday", "Saturday", "Saturday"), Units = c(3.54,
3.581, 3.623, 3.822, 3.659), Amount = c(1000L, 1000L, 1000L,
1000L, 1000L), Balance_Units = c(3.54, 3.581, 3.623, 3.822, 3.659
)), class = "data.frame", row.names = c(NA, -5L))

Comment: @Alexis Just use `soread()` from [overflow](https://github.com/mrdwab/overflow-mrdwab) after copy/paste in clipboard
Comment: Hello @akrun, just a question: how do you take the table posted by the OP, in order to avoid typing one by one to test the code?
Answer: Here's a data.table solution. I added something for sequential sum as well..
library(data.table)
> dat
     NAV_Date   NAV Year      Day Units Amount Balance_Units
1: 2013-06-01 282.5 2013 Saturday 3.540   1000         3.540
2: 2013-06-08 279.3 2013 Saturday 3.581   1000         3.581
3: 2013-06-15 276.0 2013 Saturday 3.623   1000         3.623
4: 2013-06-22 261.6 2013 Saturday 3.822   1000         3.822
5: 2013-06-29 273.3 2013 Saturday 3.659   1000         3.659

# Cumulative sum
> dat[, cumulative_sum := cumsum(Balance_Units)]
> dat
     NAV_Date   NAV Year      Day Units Amount Balance_Units cumulative_sum
1: 2013-06-01 282.5 2013 Saturday 3.540   1000         3.540          3.540
2: 2013-06-08 279.3 2013 Saturday 3.581   1000         3.581          7.121
3: 2013-06-15 276.0 2013 Saturday 3.623   1000         3.623         10.744
4: 2013-06-22 261.6 2013 Saturday 3.822   1000         3.822         14.566
5: 2013-06-29 273.3 2013 Saturday 3.659   1000         3.659         18.225

# Sequential sum
> dat[, sequential_sum := Balance_Units + shift(x = Balance_Units, fill = 0)]
> dat
     NAV_Date   NAV Year      Day Units Amount Balance_Units cumulative_sum sequential_sum
1: 2013-06-01 282.5 2013 Saturday 3.540   1000         3.540          3.540          3.540
2: 2013-06-08 279.3 2013 Saturday 3.581   1000         3.581          7.121          7.121
3: 2013-06-15 276.0 2013 Saturday 3.623   1000         3.623         10.744          7.204
4: 2013-06-22 261.6 2013 Saturday 3.822   1000         3.822         14.566          7.445
5: 2013-06-29 273.3 2013 Saturday 3.659   1000         3.659         18.225          7.481


Is it common among hobby-snooker-players to play for money?

Question: Snooker is a very popular game in Great Britain and Ireland. Most Top-Players come from these Isles. Nearly 90% probably. I don't know the history of this sport, on TV it looks a bit elitist, fine dressed players :) I'm asking if it is common to play snooker for money in Britain, because it could somehow explain to me why most Top-Players come from there, more competition, incentive for good players. In Germany it's not really popular and we play mostly for fun and not those several hours lasting matches.
So do many non-Top but good hobby-Players in Great Britain play for substantial money amounts in clubs and pubs? It looks similar to Poker like a Luck&Skill game, where betting money would seem normal when playing longer matches
Answer: I cannot really comment on whether or not it's common to play for money, however I presume it would happen occasionally as with pretty much all other pub environment games.
Relating the success of players from the UK and Ireland to gambling however, is rather inaccurate, in my opinion. I think it's more of a culture and audience thing; as in the more people who know, follow and play the game the more "experts" will emerge. Allow me to illustrate this with an example from football. I believe the early dominance of Latin American nations in football is essentially a direct consequence of the central value football has in these countries. The recent shift in dominance towards the European nations came after the establishment of youth academies and years of extensive outreach programs (especially in Germany :)).
Similarly the over-representation of African Americans in NBA or NFL, or Caucasian Americans in NHL (ice hockey) can be similarly attributed to the culture of these sports being deeply rooted in the respective communities. Long story short, think like this: how many kids grow up dreaming to become a snooker champion in Germany vs in the UK. ;)
As for a history of snooker here's a useful link.


Probability calculation from Normal Distribution

Question: Assume that we want to perform a one-sample t-test with null hypothesis $\mathbb{E}X=0$ and an alternative $\mathbb{E}X > 0$. We have an i.i.d. sample $x=(x_1, \ldots, x_{n})$ from X. Let n=100. To simplify calculations, let us assume that we have a large enough sample to reliably estimate the variance of X from the data and apply the central limit theorem. In other words, we will assume that sample mean $\bar{x}$ is distributed under normal law with expected value \mathbb{E}X and variance that is equal to $\mathrm{\mathop{Var}}_{+}(x)/n$, where $\mathrm{\mathop{Var}}_{+}(x)$ is unbiased sample variance of x. Assume that $\mathrm{\mathop{Var}}_{+}(x)=5$.
Find value $x_{crit}$ such that probability to observe $\bar{x}\ge x_{crit}$ is equal to 0.05. (Use scipy.stats.norm.ppf.).
Solution:
$P(\bar{x} \ge x_{crit}) = P(N(0,5/100) > Xcrit = 0.05\\P(Z > \frac{x_{crit}}{5/10})\\where Z \sim N(0,1)$
$\frac{x_{crit}}{5/10} = 1.645 ~~(using~ scipy.stats.norm.ppf(0.95)$
$x_{crit} = 1.645 \times \frac{5}{10} = 0.3678 \approx 0.37$
Assume that correct $\mathbb{E}X=0.5$. Find probability to observe $\bar{x} < x_{crit}$ in this case. (Use $x_{crit}$ found previously. Take into account the correct distribution of $\bar{x}$.)
What I used is as follows:
I created a Normal distribution with $\mathbb{E}X=0.5$ and $\sigma^2 = 5/100$ and then calculated the cumulative probability distribution to calculate the required probability.
$probability = scipy.stats.norm(0.5, 5/100).cdf(0.37) \approx 0.004$
But, as you may see it is extremely small for such a large region. May someone please suggest a correction?
Comment: I have by mistake written 5/10 but it is sqrt(5)/10.
Answer: I was using the wrong sigma.
$sigma = \sqrt{5} / 10$
Using the value I got the $probability \approx 0.28$.
For the same I used:
$scipy.stats.norm(loc=0.5, scale=numpy.sqrt(5)/10).cdf(0.37) \approx 0.28$


Intuitively interpreting the F-statistics in ANOVA

Question: I am trying to get the intuition of the definition of F-statistics in ANOVA. F is defined as MST(T for treatments)/MSE(E for error). So I thought that if there is no treatment effect then F should be 0, not 1. However, the answer seems to be 1.
Please help me out with this and let me know if there is anything I am missing.
Answer: The F statistic is computed as a ratio of two quantities, so it makes sense that 1 would be the reference point used to judge its magnitude.
If the F-statistic is much larger than 1, then MST is much larger than MSE, meaning there is more variability in the data due to the treatments than due to the other (unobserved or unaccounted for) factors. In other words, the signal (treatments) dominates the noise (unobserved or unaccounted for factors). This would provide evidence in favour of a treatment effect.
If the F-statistic is hovering around 1, then MST is hovering around MSE. This would suggest no evidence in favour of a treatment effect. The signal (if it exists) is drowned by the noise OR there is no signal to begin with.
Answer: Well, for it to be zero, the MST(treatment) should be zero, implying that the means are exactly equal. You do not require that much. You would declare the treatment effect non significant if its size "can be expected" given the dispersion of the data values. The F statistic is thus kind of a signal-to-noise ratio, with high values leading to the rejection of the hypothesis $H_0$ .
Answer: No ratio can be 0 unless the numerator is 0 (a 0 denominator is undefined). Furthermore, note that F ratios are a ratio of variances, and variances also can't be 0.
Now the intuition is this: the F distribution quantifies the distribution of variance ratios. In ANOVA this is between groups (experimental variance) / within groups (error variance). If you have no treatment effect (or a weak one) you are left with only error in the numerator and denominator, yielding 1.0, or sometimes < 1.  Only if your treatment is effective will you get a ratio > 1.0
Comment: Variance *can* be 0, but cannot be negative.


Testing for differential ranking between lists relative to a reference list

Question: I am interested in comparing ranked lists in order to find elements that are differently ranked. Suppose we have 10 lists, each ranked (and for simplicity assuming the elements overlap and only the ranking was different). What method could I apply to find elements with high ranks in list1 that consistently have low(er) ranks in all the other lists?
The background here is that each of these lists represent outputs from differential testings from experiments that cannot be directly compared to each other as confounders exist between them that are nested with the effect we want to compare. Hence, I would like to at least prioritize elements that in the given reference list ranked highly (and therefore were very significant or "important" in experiment1), but have low(er) ranks in all other experiments, suggesting that this element was "unique" to experiment 1, meriting downstream experiments with these elements.
For elements consistently ranked highly I would use RobustRankAggregation or the Rank Product calculation, but what about actually finding differences in ranked lists? Obviously one could simply take the topX elements from list 1 and calculate something like the variance for their ranks across the other lists, but I am hoping for a method that is more sophisticated. If possible links to respective implementations/packages in R would be appreciated.
Answer: One possible way to approach your problem is as follows: For each element, collect all the ranks that it has in all your lists, i.e., if you have $n$ lists, $m$ elements, and the rank of an element $e_i$ in list $l_k$ is $r_k(e_i)$, then the rank collection $c_i$ of an element $e_i$ is:
$$
c_i := (r_1(e_i), r_2(e_i), \ldots, r_n(e_i)), \quad i=1,\ldots, m.
$$
Now you are trying to find the elements $e_i$ for which there is a list $l_k$ such that $r_k(e_i)$ is consistently different from the ranks $r_s(e_i)$ of $e_i$ in the other lists $l_k, s\ne k$. This can be understood as finding an outlier in the collection $c_i$.
In general, there are many methods for finding outliers $x$ in a set $X$ and most of them assign an outlier score $os(x)$ to each $x\in X$ which is supposed to describe the amount of "outlier-ness". And the most outlying $x\in X$ is then the $x$ with the maximum outlier score. So you could assign to each element $e_i$ the maximum outlier score $mos(e_i)$ of all the ranks in $c_i$:
$$
mos(e_i) := max_k\{os(r_k(e_i))\}.
$$
Then, all that is left to do is to find the elements $e_i$ with the largest value of $mos(e_i)$.
You mentioned that you want to find the elements $e_i$ which are not only differently ranked in one list compared to the other lists, but also that this outlying rank is higher than that in the other lists. If this is the case, you could simply append a filter to the outlier detection which would make sure that this is indeed the case.
Back to the outlier detection and available algorithms: Many methods are possible. E.g. you could take as outlier score the z-score, the distance of a rank $r_k(e_i)$ to the mean $\mu_i := mean(c_i)$ divided by the standard deviation $\sigma_i := sd(c_i)$:
$$
os_z(r_k(e_i)) := \frac{|r_k(e_i) - \mu_i|}{\sigma_i}.
$$
Other outlier detection methods with implementations in R are e.g. kNN or isolation forest. Of course, if you are willing to consider python libraries, too, there is a whole collection here. Most of those are, however, overkill, given that you have only about ten lists.
Comment: This is a very clearly presented algorithm for outlier detection among the ranks for any given item in the ranked list.  My only concern is that this assumes that the rankings themselves would be normally distributed (thus justifying use of a z-score outlier detection method).  But, on the face, I'm not sure this is justifiable.  If one item is ranked highest by most, this means you might have a set like {1, 1, ..., 1, 2}.  In this case, that one 2nd ranking would have a z score of 2.8...but ¿is it an outlier?
Comment: @GreggH I don't have to assume Gaussian distribution for any of the methods I have suggested. And measuring the distance from the mean in units of the standard deviation is a very commonly used method independent of the type of the distribution. Also, the advantage of outlier scores is that this gives the user a numeric measure and the user can then decide, using domain knowledge, what thresholds to use. Scores are always better than binary decisions.
Answer: My suggestion is to first confirm that there is indeed a difference in the ranking between two lists.  This could be accomplished using something like Cohen's kappa-coefficient (though I would suggest a weighted kappa).  This is essentially asking to build a contingency table with the items and their paired rankings.  Then, this contingency table is assessed for something like an inter-rater reliability.
If you confirm that the rankings are substantively different, then you can asked which items are truly being ranked differently simply by looking at how far the items are from the diagonal of the contingency table.
Next, if you wish to do a pairwise comparison for each of your 10 rankings or so (this is probably reasonable for a relatively small number like this), you can build a list of how often and how far away from the diagonal any one item is for each of the possible pairings...but I would only use those that indicated reasonable differences for the rankings.
For packages to help with this, the psych package in R has a function for kappa calculations.
Happy to clarify as needed.


HP (QC) ALM POST or PUT is forcing me to provide "required fields" which are not required in current workflow state

Question: I'm trying to update a HP QC Defect via REST API using PUT VERB.  However, it will tell me it can't update the defect because of a "Missing required field".
I would understand if the field was actually required, but the thing it that it is not.  I can go into QC, update that same defect with the value I am trying to update it with in script, and it works just fine.
If the Status field is set to deferred then "User-03" which is labelled "Deferred Reason" is a mandatory field and you can't save the defect without a deferred reason when status is deferred.
In this case, status is set to "Closed", so this required field does not apply in this work flow state.  However, HP REST API 11 will still make me provide a value for every single "potential" required field regardless of the current workflow state.
This is a very bad design.  Is there a way around this?  I don't want to put dummy data in these fields just to satisfy an issue with HP's API.
<code><?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<QCRestException><Id>qccore.required-field-missing</Id>
<Title>Missing required field: user-03 for entity defect</Title>
<ExceptionProperties/></QCRestException>
</code>
Answer: HP's rest api is bypassing the workflow/vbs configured for all these purpose. It is highly depended upon the customization metadata which we have per project.
To overcome what you should do is, go to customization -> Project entities -> entity -> custom fileds -> user-03. Mark it non required field from customization. In the workflow script still you can set it mandatory for specific conditions so that it works with same behavior from the user's point of view and hopefully rest will also allow you to update without specifying this field.
Thanks,
Gaurav


How can one add inline JavaScript in the validation function for the Form API?

Question: I found that there is a way to add javascript files in the form validation function as stated in "drupal_add_js not work in form validation failed".
However, I would like to know how to add javascript using the drupal_add_js function with the inline option. I would just like to execute some javascript code while I am in the validation function of the Drupal Form API.
Answer: See the docs @https://www.drupal.org/docs/7/api/javascript-api/managing-javascript-in-drupal-7

drupal_add_js() example:
drupal_add_js(
  'jQuery(document).ready(function () { alert("Hello!"); });',
  array(
    'type' => 'inline',
    'scope' => 'footer',
    'group' => JS_THEME,
    'weight' => 5,
  )
);

#attached example:
$element['#attached']['js'][] = array(
  'type' => 'inline',
  'data' => 'jQuery(document).ready(function () { alert("Hello!"); });',
  'scope' => 'footer',
  'group' => JS_THEME,
  'weight' => 5,
);

Just replace $element by $form.

Original source: https://web.archive.org/web/20160502205421/http://www.failover.co/snippets/drupal-7-attaching-css-and-javascript-forms-or-form-elements
Answer: Use the #attached property to add your JS file instead of drupal_add_js function
More info:
FAPI #attached
Comment: I am thinking that this is just what I will do, but I meant to ask if it is possible to use the inline option of the drupal_add_js function in this situation.


Reading a remote text file line by line and comparing with inputbox entry

Question: I wrote an application with an inputbox where I want people to input  password which will be compared from a list of passwords stored in my webserver in a text file which are one entry in each line then given access to my application
So in a few words i want the inputbox password to be compared line by line to my text file but i have failed to do that so far
Here is my code:
string input =
    Microsoft.VisualBasic.Interaction.InputBox("Please enter your password for access to this software", "pass:");

if (input=="")
{
    appexit();
}

WebClient client = new WebClient();
Stream stream = client.OpenRead("http://haha.com/access.txt");
StreamReader reader = new StreamReader(stream);
//String content = reader.ReadToEnd();

int counter = 0;
string line;

while ((line = reader.ReadLine()) != null)
{
    if (line!=input)
    {
        MessageBox.Show("This software has been deactivated because of wrong pass", "YOUR ACCESS HAS BEEN LIMITED");
        appexit();
    }

    counter++;
}

reader.Close();

The password file contains lines such as:
hahdfdsf
ha22334rdf
ha2233gg
charlysv-es

Where is the error? The code compiles but even if the correct password is entered, the check fails.
Comment: hahdfdsf
ha22334rdf
ha2233gg
charlysv-es                                  password file contains such entries one entry in each line
Answer: According to your loop, when you get a line that doesn't equal input, then you stop everything - what is logically incorrect.
You have to compare lines, until one of them equals input or end of file.
 ...
 bool valid = false;

 using (WebClient client = new WebClient())
 {
     using (Stream stream = client.OpenRead("http://haha.com/access.txt"))
     {
         using (StreamReader reader = new StreamReader(stream))
         {
             string line;

             while ((line = reader.ReadLine()) != null)
             {
                 if (line.Equals(input))
                 {
                     valid = true;
                     break;
                 }
             }
         }
     }
 }

 if (valid)
 {
     // password is correct
     ...
 }
 else
 {
    MessageBox.Show("This software has been deactivated because of wrong pass", "YOUR ACCESS HAS BEEN LIMITED");
    appexit();
 }
 ...


How to Reference using non-ID fields in Fluent NHibernate?

Question: Bear with me, this is a legacy system, so the table relations are not idea.
Dog has field ID, Owner, and Name
DogHouse has ID, Owner and Name.  It does not have a foreign key reference to Dog.  Rather, it is related by Owner and Name.
In my DogMap, I need to be able to tell it how to reference its Doghouse.  How do I do that?  When I try
  Reference(x=>x.DogHouse).

What do I put after the dot?  It's not ID, and should be Owner and Name.
Answer: non id fields must be one property
Reference(x=> x.DogHouse, "Owner").PropertyRef(house => house.Owner).Readonly();

since both are needed we have to use some tricks
DogHouse FakeReferenceProp { get; set; }
public DogHouseMap()
{
    Component(x => FakeReferenceProp, c =>
    {
        c.Map(x => x.Owner, "Owner").Readonly();
        c.Map(x => x.Name, "Name").Readonly();
    });
}

// in DogMap
Reference(x=> x.DogHouse).Columns.Add("Owner", "Name").PropertyRef(house => FakeReferenceProp).Readonly();


How to add numbers in a listBox

Question: My program is supposed to generate 24 random numbers then add them all together and display them.
I've gotten them to do everything, except I can't get the first 24 numbers to add.
I tried moving the statement that collects the numbers but it didn't work.
Im not sure how to go forward.
int x = 0;
int number = 0;
int i = 0;

while (i < listBox1.Items.Count)
{
    number += Convert.ToInt32(listBox1.Items[i++]);
}

totaltextBox.Text = Convert.ToString(number);

Random ran = new Random();

for(x = 0;x <= 23; x++)
{
    listBox1.Items.Add(Convert.ToString(ran.Next(0,100)));
}
fileNumbers.Text = listBox1.Items.Count.ToString();

Comment: What errors you get?
Comment: From what you have posted it should work. Maybe there is something else in your code that overrides this behavior
Comment: I dont get an errors, it just displays 0 in my textbox. This is all of my code besides the exit button.
Comment: Only one 0 or 24 zeroes?
Comment: you have 2 textboxes, which one do you mean?
totaltextbox will be 0 because at the time you call the while loop, the listbox is empty.
Comment: Only a single 0. Sorry, for my total textbox
Comment: do you use `listBox1` anywhere else in the code?
Comment: The problem is obvious here. You try to add the numbers from the list box items before creating the items.
Comment: I moved the entire while statement below where i created my random numbers and it works.
Comment: @Niomi than it means you explained the issue wrong since the listbox is not affected by that while. Only the TextBoxes are affected by that change.
Answer: As mentioned in the comments, you set your totaltextbox text too early. That way the listbox is empty when you try to accumulate the values.
Try this:
Random ran = new Random();

for (var x = 0; x <= 23; x++)
{
     listBox1.Items.Add(Convert.ToString(ran.Next(0, 100)));
}

var number = listBox1.Items.Cast<string>().Select(Int32.Parse).Sum();
var count = listBox1.Items.Count;

I also replaced your while loop with a LINQ-Expression. Also note, that in c# you can declare for-variables like in my example code. No need to declare them for the whole method (unless you want to use them after the for loop for whatever reason).
Answer: Just replace for and while loop. if not, it cant take listBox1.Items.Count.
int x = 0;
int number = 0;
int i = 0;
Random ran = new Random();
for (x = 0; x <= 23; x++)
{
    listBox1.Items.Add(Convert.ToString(ran.Next(0, 100)));
}
while (i < listBox1.Items.Count)
{
    number += Convert.ToInt32(listBox1.Items[i++]);
}

totaltextBox.Text = Convert.ToString(number);
fileNumbers.Text = listBox1.Items.Count.ToString();

Answer: The other answers are correct. However I will add my solution which avoids some back and forth conversions and reduces iterations:
Random ran = new Random();
int total = 0;
for (int x = 0; x <= 23; x++)
{
 int rn = ran.Next(0, 100);
 total += rn;
 listBox1.Items.Add(rn.ToString());
}

totaltextBox.Text = total.ToString();

How to add numbers in a listBox


On Continuity and Discreteness

Question: The Point has many definitions but when dealing with it purely geometrically, it seems to be defined as that which has no part.

When we put a point, for example, on a sheet of paper using a pencil, wouldn't the point have some amount radius to it on zooming?
I have thought about the problem and perhaps one way of thinking about it is that as we zoom in, the point changes itself so that it decreases in size by the same factor as that of magnification, and thus, its definition holds. But this still doesn't explain how the point can have no part, i.e., no radius.

Now, let's move to another topic, that of differential calculus. It is said that, geometrically, the derivative at a point is the slope of the tangent at that point and that the tangent only touches the curve at that point. But, if we once again zoom in to the point of contact, wouldn't it show an infinite number of points touching the tangent?

In general, how do we use a discrete quantity - the point - in something that is continuous - the curve?
P.S. - I am a high schooler who just started learning calculus and would appreciate it if the solution, if possible, can be given in terms that can be understood by a high schooler
Comment: In real life it's impossible to construct the mathematical idea we have of a point. A point is a $0$ dimensional object, so when you "draw" it onto a sheet of paper you shouldn't be able to see anything (no matter how much you zoom in). This, of course, is quite inconvenient for anyone reading your graph, so we approximate it with a $2$-dimensional circle for convenience.
Comment: Well, how tangent lines work, you take the limit as $h$ approaches 0, where $h$ is the difference between some point you're trying to find the slope of (call it point $x_1$), and another arbitrary point $x_1+h$. As $h \to 0$, this shouldn't matter, it's approaching a single spot on the curve. For probably a better explanation, see [this](https://en.m.wikipedia.org/wiki/Difference_quotient)
Comment: Just a heads up: the oft-repeated characterisation of tangents as the line that touches the curve once is straight up false. Tangents often do intersect curves multiple times.
Comment: @Theo, wouldn't that be a secant line?
Comment: @CottonHeadedNinnymuggins I guess technically they are? That doesn't mean they're not tangents. For example, take the tangent of $2x^3 - 3x^2$ at $x = 0$, which is the $x$-axis, and it intersects the curve at both $(0, 0)$ and at $(3/2, 0)$.
Comment: @Theo, I get you, yes
Comment: If you are beginning calculus you MUST learn the logical foundations of $\Bbb R$ and how that leads to its basic properties or you will become completely confused. BTW the name "real" numbers is merely a historical relic.
Comment: @DanielWainfleet Is there any good resource to learn that?
Comment: The definition, etc.,of $\Bbb R$ is in many textbooks. Also https://en.wikipedia.org/wiki/Real_number. Or google Real Number System.
Answer: In the physical world, the components of matter are not infinitely small. And measurements are  only  approximations,  ranges of numbers.
The use of $\Bbb R$ and other math can be justified as a model, a simulation.  Suppose we have an object $A$ and some process, denoted by $\to$, after which $A$ has changed into $B$. So we write $A\to B$. We may take  mathematical models $A^*$ and $\to^* $ and obtain a mathematical result $A^*\to^* X^*$. And $X^*$ should be a model of some $X$. Now we also have a model $B^*$ of $B$. How useful the model is, depends on how "close" $X$ is to $B$ and how "close" $X^*$ is to $B^*$.This of course depends on the context & the details.
Mathematical entities are  not defined by physics & chemistry.
Comment: @JoseCarlosSantos .I had a computer glitch and when I returned, my A was edited but it looks exactly how I remembered it. No problem. I'm just curious.

On Continuity and Discreteness


how to fix "django-admin not recognizable error in pycharm"

Question: I am trying to create the python project in the same environment as previous project but i'm having an error message that it does not recognizes the "django-admin" command please help me to solve this issue.whenever i try to run my  admin project i usually get the message (is not recognized as an internal or external command,
operable program or batch file).please click on this link to view my screenshot of the above error message i usually encounter when i try to run the project
Comment: Is django installed in your virtual environment?
Comment: it worked thank you..how i do i star your answer?
Comment: There is two way to create virutal environment  using ```virtualenv --system-site-packages your_env_name``` and  ```virtualenv your_env_name```. Using first command will install all package in your venv , but not when using second.
So if you are using second command then yes you have to install again.
Answer: Probably you have to install Django in your enviroment
<code>pip install Django
</code>
in your command line

how to fix “django-admin not recognizable error in pycharm”