ValueError: Bin labels must be one fewer than the number of bin edges in Pandas 'Cut()'

Question: I'm using cut() in Pandas to categorize the values of a numerical column.
df['bmi_bin']=pd.cut(df['Body Mass Index'],bins=4,labels=[0,1,2,3])

Output:
0      2
1      2
2      1
3      2
4      1
      ..
683    1
690    0
694    1
696    0
699    1

I tried putting the same code in a loop:
num_of_bins=4

for i in range(0,num_of_bins):
            df['bmi_bin']=pd.cut(df['Body Mass Index'],bins=num_of_bins, labels=[i])

This is giving me the following error:
ValueError: Bin labels must be one fewer than the number of bin edges

Where did I go wrong? Doesn't labels[i] give label values [0,1,2,3] which are fewer than 4?
Comment: What is the desired output?
Comment: Same as the output mentioned under the first line of code, i.e., 0/1/2/3.
Comment: I think you need to give a bigger picture of what you are trying to do here.   Using a for loop here doesn't make much sense, at least to me.
Answer: Your code is trying to split dataframe into diff bins with lable. But bins and lables count not matching.
In for loop you are spliting dataframe into 0, 1,2,3 bins in different iteration but lable is only one at a time whether its 0, 1,2 or 3.
If you want to use loop then below can be used but not sure what output you are expecting -
num_of_bins=4
lt=[]
for i in range(0,num_of_bins):
    lt.append(i)
    df['bmi_bin']=pd.cut(df['Body Mass Index'],bins=i, labels=lt)

Comment: It was not about logic, it was for syntax match.. in loop... she wanted to have it in a loop.. thats why I put code like that...
Comment: Have you tried to run this? What is the point of doing it in the loop?


How to authenticate ssl client certificates with nginx front-end?

Question: I have some URL's that I like protecting with ssl client certificates using directives like these in my apache configs:
SSLVerifyClient require
SSLVerifyDepth  10
SSLRequireSSL
SSLOptions +StrictRequire
SSLRequire %{REMOTE_ADDR} =~ m/^127\.0\.0\.1$/ \
  or ( %{SSL_CIPHER} !~ m/^(EXP|NULL)/ \
  and %{SSL_CLIENT_I_DN_CN} eq "xxx" \
  and %{SSL_CLIENT_S_DN_Email} in {"firstname.lastname@example.com", "email@example.com",} )

There don't seem to be any nginx directives to handle this, so I assume I have to pass everything on to Apache backend.
Which brings me to my question, how to pass encrypted ssl?  All the proxying is decrypting the ssl packets at nginx level before passing them on to Apache.
Comment: You should ask a question about how to do what you want with Nginx. Downgrading back to Apache is not the solution. Using Django (you said mod_wsgi in another comment) can be done in Nginx easily. I do this for MANY sites. Look into uwsgi and Nginx. If you find Apache to be faster, your config is wrong. Ask those questions.
Answer: If you want Apache to handle the SSL verification, then you'll need to put it in front of nginx, or else do some crazy stuff with passing the client DN to Apache via a custom header or something -- an awful lot of work.
There are some facilities for doing DN checking in nginx, though; the Nginx HTTP SSL module docs give the possible variables (right down the bottom, and unfortunately the markup's stuffed so there's no header for it) but $ssl_client_s_dn should be what you're looking for, and you can use nginx's programming-like directives (if $ssl_client_s_dn ~= CN=something_or_other kind of thing) to redirect or return a 403 if you don't like the certificate presented.  It'll be very different to what you're used to with Apache, but ultimately Apache's way of doing this sort of thing isn't an oil painting either, so it all depends on which ugly you prefer...
I'm curious, though, as to why you'd put nginx in front of Apache... either of them should be capable of doing the whole job of serving web pages.
Comment: Ok, I'll look into the directives bit.  I'm running nginx in front of Apache because I need multiple "all ssl" virtual hosts and SNI seems pretty sketchy.  I still need Apache primarily because of mod_wsgi for Django apps as well as passenger for RoR and php-fpm.  I had a highly tuned Apache before needing multiple SSL sites so wouldn't have changed otherwise.  As it turns out nginx reduced my ssl page loads by around .3 secs or so and is fantastic on the static files so it was a great change.
Comment: Ok, got it working using something pretty close to the direction you pointed.
Comment: The link in your answer is broken, it appears https://nginx.org/en/docs/http/ngx_http_ssl_module.html#variables is the correct location. Also, you can pass these variables to your interpreter via `fastcgi_param DN $ssl_client_s_dn` if you're using fastcgi, If you're using proxy_pass instead, you can define arbitrary headers, e.g. `proxy_set_header nginx-ssl-client-dn $ssl_client_s_dn` and have the upstream server act on those headers (e.g. PHP behind an Apache upstream server would expose it as  `$_SERVER['NGINX_SSL_CLIENT_DN']`)


Android: Can an Android app have multiple FirebaseMessagingServices

Question: I am building a library project in which I have a FirebaseMessagingService.
I also have a FirebaseMesagingService in my app. What I see is, whenever an FCM is sent from the server, only one FirebaseMessagingService processes it. This was not the case when I was using GCM Receivers. Both the GCM Receivers used to receive the message and based on message content, they would/would not do anything about it.
How can I achieve the same in FCM.
Comment: Hey @Ripityom, I answered below. Let me know if the explanation is not clear.
Comment: what do you mean..? You want more than one FirebaseMessagingService?
Comment: Did you manage to find a solution so far? Can I ask what method you ended up using? Thanks!
Answer: Your library's manifest should not have a FirebaseMessagingService subclass. Adding the messa service to the app's manifest should be a part of the integration step, integrating the SDK. Also you should provide a hook in the SDK from where the app can pass the FCM message payload to the SDK.
Essentially if the app does not have its own FirebaseMessagingService subclass it would add your SDK's listener service in the manifest else it will add the hook in its own listener service which passes the payload to your SDK and SDK takes the required action
Comment: This question concerns `FirebaseMessagingService`, _not_ `FirebaseListenerService`. The documentation at https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService explicitly recommends subclassing that service, with sample manifest xml.
Comment: I meant the subclass itself, maybe it wasn't clear and sorry about the MessagingService and ListenerService typo mixed up the naming convention of fcm and gcm
Comment: right, that helps a little. I've removed my downvote. The answer is still confusing - perhaps you could add an explanation of your reasoning? Something like 1. in each APK, only *one* FMS subclass can receive push messages; 2. if you add two libraries to the APK, each with their own subclasses, only one would receive push messages; 3. therefore apps should receive push messages with their own subclass; 4. libraries should provide a hook (NOT a subclass) which the app subclass can call whenever it wants the library to do its thing; 5. this makes sure the app is always in control
Answer: The android app cannot have multiple FirebaseMessagingService as its a service not a receiver. What one can do, is to check two conditions:

If app has FCMMessagingService registered in its functionality, then provide a method in the library project which can take the message as parameter, received by app's FCMMessagingService.
If the app does not have FCM functionality integrated in it, then have FCMMessagingService in your library project which can handle the fcm sent by the server.
Answer: I've got the same issue a few days ago and in our team, we used a slightly different approach which involves reflection.
In general, we use delegate class and provide context via reflection.
public class OwnPushService extends FirebaseMessagingService {

    private List<FirebaseMessagingService> messagingServices = new ArrayList<>(2);

    public GCPushService() {
        messagingServices.add(new AirshipFirebaseMessagingService());
        messagingServices.add(new TLFirebaseMessagingService());
    }

    @Override
    public void onNewToken(String s) {
        delegate(service -> {
            injectContext(service);
            service.onNewToken(s);
        });
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        delegate(service -> {
            injectContext(service);
            service.onMessageReceived(remoteMessage);
        });
    }

    private void delegate(GCAction1<FirebaseMessagingService> action) {
        for (FirebaseMessagingService service : messagingServices) {
            action.run(service);
        }
    }

    private void injectContext(FirebaseMessagingService service) {
        setField(service, "mBase", this); // util method for recursive field search
    }
}

I've written an article on Medium about this approach if you are interested in the details: link
Comment: By the way, the context could be injected once during initialization as well, it doesn't need to inject every time for each messaging method. This is in Kotlin version: https://gist.github.com/yongjhih/cbd646f6740e166db6d64930cbdc7a8f
Comment: Thank you for the solution. By this, I was able to write down a solution for another bug https://stackoverflow.com/a/64804557/6117565
Answer: In such cases, you should make your LibraryFirebaseMessagingService as parent of your AppFirebaseMessagingService, and declare only AppFirebaseMessagingService in app's AndroidManifest.xml file.
For Example:
in your Library:
public class LibraryFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
         //Your library logic
    }
}

Now in your app:
public class AppFirebaseMessagingService extends LibraryFirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
         if(shouldLibraryHandlethis(remoteMessage)) {
            super.onMessageReceived(remoteMessage);
         } else {
            //Your app logic
         }
    }
}

Now in your app's AndroidManifest.xml file:
<service
    android:name=".AppFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

No need to declare LibraryFirebaseMessagingService in any AndroidManifest.xml
Comment: What happens if all libraries use this approach, how would you extend them all
Comment: it working for me ,please remember to add super.onMessageReceived(remoteMessage);
Answer: If you look at the source code of the Android Firebase Messaging, you will see that a broadcast receiver is used to listen for the "com.google.android.c2dm.intent.RECEIVE" event. The old event name from the GCM is also used in Firebase Cloud Messaging. It is possible to have multiple broadcast receivers, so your library can register its receiver in the android manifest:
<application>
    <receiver
        android:name="com.mylibrary.FirebaseMessagingReceiver"
        android:permission="com.google.android.c2dm.permission.SEND"
        android:exported="true">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </receiver>
</application>

A broadcast receiver in your library will receive Firebase cloud messages:
class FirebaseMessagingReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {

    }
}


Draw a smooth string

Question: I'm trying to create a simple "Avatar" user control in WinForms that draws a string in the middle of the control. However, I can't figure out a way to draw a smooth text. Here's the code:
protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
    e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
    e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
    e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
    e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
    e.Graphics.FillEllipse(_backgroundBrush, 0, 0, Width, Height);

    var fontSize = Width / 3 * 2;
    if(Font.Size != fontSize)
    {
        Font = new Font(_fontFamily, fontSize, _fontStyle, GraphicsUnit.Pixel);
    }

    var stringSize = e.Graphics.MeasureString(_initials, Font);
    var location = new Point((int)((Size.Width - stringSize.Width) / 2),
                             (int)((Size.Height - stringSize.Height) / 2));

    e.Graphics.DrawString(_initials, Font, _foregroundBrush, location);
}

I made the font size a little bit bigger to show what I mean. In the image below, as you can see the M is broken into several parts rather than a smooth line.

Update:
Based on the comment from @GyörgyKőszeg, I changed to TextRenderer and it's now looking like it should.
//e.Graphics.DrawString(_initials, Font, _foregroundBrush, location);
TextRenderer.DrawText(e.Graphics, _initials, Font, location, _foregroundColor);

Comment: `AntiAliasGridFit` has a very pixelated result when drawing text with GDI+ (`Graphics.DrawString`). Use pure GDI instead (`TextRenderer.DrawText`) with `ClearTypeGridFit` for the best quality.
Comment: @GyörgyKőszeg Thank you. That did it. Please add it as an answer so I can mark it as solved.
Comment: One of you two should write this up as an answer so that the next person looking to do this can find it.
Comment: You get the same effect if you set `TextRenderingHint = TextRenderingHint.AntiAliasGridFit` (or `TextRenderingHint.AntiAlias`) and add `StringFormat.GenericTypographic` to `e.Graphics.DrawString()` (you should always specify a StringFormat when drawing text with the Graphics object) -- `Graphics.DrawString()` uses Grid fitting as the default behavior.
Comment: As a note, `SmoothingMode.HighQuality` (it's AntiAlias) only applies to the shape (the ellipse), `PixelOffsetMode` and `InterpolationMode` are irrelevant.
Answer: Converting my comment to an answer upon request:
Use TextRenderingHint.ClearTypeGridFit for the desired result.
And if possible use TextRenderer.DrawText instead of Graphics.DrawString whenever possible. TextRenderer.DrawText uses GDI text rendering instead of GDI+ and it is the preferred way of rendering text since .NET Framework 2.0 (even by controls if UseCompatibleTextRendering is false).
And please also note that you should use TextRenderer.MeasureText instead of Graphics.MeasureString because they may return different sizes.
Comment: Thank you. I'll adjust the code as suggested.

Draw a smooth string


How do I analyze the treatment effect while controlling for covariates in a pretest–posttest design in R?

Question: I ran a repeated-measures ANOVA in R to look at the effects of treatment (3 different treatment groups), gender, age, and education level on a specific biomarker (continuous variable). The data is in long-form with two time points (baseline and post) corresponding to the id column.
model1 <- lmer(Measure1 ~ Treatment + Gender + Education_Level + Age + (1|id), data=dataset)
anova(model11_rma)

I've seen some examples of repeated measures ANOVA that include a time interaction. I just want to make sure that this is correct and is actually testing what I need to test. Can anyone verify that my code looks correct? Also, do I need to conduct Mauchly's Test of Sphericity to verify that the assumptions of the ANOVA have been met? If so, how do I do that in R with the lmer model?
I've also tried to run a repeated measures ANOVA in R using the car package and the ez package as shown below, however, I keep getting errors that tell me I am missing data like the following:
Error in ezANOVA_main(data = data, dv = dv, wid = wid, within = within, : One or more cells is missing data. Try using ezDesign() to check your data.

ezANOVA
ezANOVA(data=dataset_3_lfclean, dv=.(Measure1), wid=.(ID), within_covariates.(Age), within=.(Gender,Education_Level),
    between=.(Treatment), detailed=T, type=3)

Car
Measure1_Response <- with(dataset_3_lfclean,cbind(Measure1[Group==1], Measure1[Group==2], Measure1[Group==3]))

mlm1 <- lm(Measure1_Response ~ 1)

rfactor <- factor(c("g1", "g2", "g3"))

mlm1.aov <- Anova(mlm1, idata=data.frame(rfactor), idesign = ~rfactor, type="III")

summary(mlm1.aov, multivariate=FALSE)

Here's my data in wide-form where each dependent variable (measured at time 1 and time 2 has its own column and each participant has a single row:

Here's my data in long-form where each participant has multiple rows:
Comment: On the second point, there is this function - https://stat.ethz.ch/R-manual/R-devel/library/stats/html/mauchly.test.html Or do `?mauchly.test` from your R session
Comment: @thelatemail I think the mauchly.test function only works with mlm objects. I already tried that and can't seem to get it to work.
Comment: @AllisonGrossberg - can you not do something like `mauchly.test(lm(cbind(mpg, disp) ~ 1, data=mtcars))` to make a temporary linear model and test it? Obviously adapting to your data rather than the built-in `mtcars`
Comment: Is there any specific reason why you have to use a linear mixed model (fitted via `lmer()`) to model the data? This model is not exactly equivalent to a repeated measures ANOVA.
Comment: @statmerkur - I tried to use the Car package in R but was having a hard time getting it to work. I'm new to R and I don't entirely understand the differences between the different models. Can you tell me why a linear mixed model using lmer is not equivalent to a repeated measures ANOVA? What is the correct alternative?
Comment: @statmerkur - Just for a little extra context - I do have some missing data (only for some dependent variables) and the study design is a little complex. I also want to account for baseline differences in the dependent variables.
Comment: Regarding the difference between linear mixed models and rmANOVAs, I think [this Q](https://stats.stackexchange.com/q/122717) and Jake Westfall's answer are a good starting point. The model that you specified assumes compound symmetry which is not exactly the same as sphericitiy.
Comment: Also, please explain clearly which hypothesis you want to test. If you want to compare the effect of the `Treatment` at time point 1 and time point 2, you have to construct a different model. What do you mean by "two time points (baseline and post) corresponding to the id column"? Do you mean that data of the same subject (=`id`) are collected at time point 1 and at time point 2?
Comment: @statmerkur - Ok. I have 38 participants who each underwent one of three different 8-week interventions. Seven different dependent variables were measures once before the intervention and once after. So I'm interested in the effect of group (treatments 1,2, or 3) on the various dependent variables. There are major differences in the baseline scores of the different groups so I need to account for individual differences in each participant. I have my data in both long-form and wide-form - I'll post some examples.
Comment: I've edited the heading. Please check if you are OK with that and feel free to change it back if you aren't.
Answer: If you want to analyze your data with ANCOVAs and use the pre/baseline scores as a covariate, I think you should create separate columns for the pre and post scores (e.g. Measure1_pre and Measure1_post). Depending on whether you are interested only in the main effect of Treatment or also in the main effect of and interaction with Gender your ezANOVA()s should look like (1) or (2), respectively. Note that you should set orthogonal contrasts in order to get meaningful Type-III tests (see, e.g., John Fox' answer here).
# set orthogonal contrasts
options(contrasts = c("contr.sum", "contr.poly"))

# (1)
ezANOVA(data = dataset_3_lfclean,
        dv = Measure1_post,
        wid = ID,
        between = Treatment,
        between_covariates = .(Measure1_pre, Age, Gender, Education_Level),
        detailed = TRUE,
        type = 3)
# (2)
ezANOVA(data = dataset_3_lfclean,
        dv = Measure1_post,
        wid = ID,
        between = .(Treatment, Gender),
        between_covariates = .(Measure1_pre, Age, Education_Level),
        observed = Gender,
        detailed = TRUE,
        type = 3)

A repeated-measures-ANOVA-like approach, where your focus is on the Treatment by Timepoint interaction, is different, and tests different hypotheses.
Especially, the discussions under the heading Best practice when analysing pre-post treatment-control design seem to be a good starting point to decide how you want to analyze your data.
The corresponding ezANOVA() syntaxes to analyze this interaction would be
# (1)
ezANOVA(data = dataset_3_lfclean,
        dv = Measure1,
        wid = ID,
        between = Treatment,
        within = Timepoint,
        between_covariates = .(Age, Gender, Education_Level),
        detailed = TRUE,
        type = 3)

and
# (2)
ezANOVA(data = dataset_3_lfclean,
        dv = Measure1,
        wid = ID,
        between = .(Treatment, Gender),
        within = Timepoint,
        between_covariates = .(Age, Education_Level),
        observed = .(Gender, Timepoint),
        detailed = TRUE,
        type = 3)

Your lmer() model is more in line with the second approach, however, it assumes (positive) compound symmetry.
Comment: Thank you so much for this really detailed response! I really appreciate it. I tried to run both the ANCOVA and the Repeated Measures ANOVA and I am still getting the following error message: Warning: Data is unbalanced (unequal N per group). Make sure you specified a well-considered value for the type argument to ezANOVA(). Error in temp$ezCov - mean(temp$ezCov) : non-numeric argument to binary operator
In addition: Warning message: In mean.default(temp$ezCov): argument is not numeric or logical: returning NA
Comment: I guess you get the error only in the models where `Gender` is a covariate, right? Does it disappear if you put in `as.numeric(Gender)` (only for the covariates) instead? The warning is about a different issue: whether you should use (type I vs) type II vs type III Sums of Squares. Should I put in a link the addresses this issue?


Como escrever em um arquivo txt em Python?

Question: Como abrir um arquivo txt e adicionar o conteúdo de alguma variável nele?
Por exemplo, um conteúdo que sai do print no código abaixo.
for passw in senha_list:
    print(passw,end='')

Comment: É importante tentar entender o conceito de arquivo antes. https://docs.python.org/pt-br/3/tutorial/inputoutput.html#reading-and-writing-files Na documentação oficial do Python tem bem explicado como abrir, fechar e escrever em um arquivo.
Answer: Para gravar os dados em arquivo, basta usar o comando open do python, nele devemos passar como parâmetro o nome do arquivo que deseja salvar assim como o modo como deseja abrir o arquivo no sistema de arquivos, você pode ver a documentação aqui.
Segue um exemplo da utilização de acordo com o exemplo que passou:
with open('saida.txt','w') as f:
    for passw in senha_list:
        f.write('{}\n'.format(passw))

Neste exemplo estamos gravando um elemento passw por linha dentro do arquivo saida.txt, para isso eu usei o método string format, para mais informações como funciona o format, segue a documentação.
Answer: f = open('saida.txt','at')
for passw in senha_list:
    f.write(f'{}\n')
f.close()


Gstreamer is installed but not built with OpenCV

Question: I'm trying to stream a video from my PC to another
I started with a basic example, this one https://stackoverflow.com/a/51543002/9492711
I configured the udpsink host properly to map to my pc, but I'm getting this message constantly

VideoCapture or VideoWriter not opened
VideoCapture not opened

I have MacOS and I installed openCV 4.1.0 and gstreamer 1.16.0 with Python3.
Opencv 4.1.0 and gstreamer1.16.0 are installed using brew, and also I installed the the package file (1.16.0 runtime instller) from this link
https://gstreamer.freedesktop.org/download/
The problem is that gstreamer is not properly built with opencv and I'm new to this dev. env and I need some guidance.
Here is what I'm getting when I run print(cv2.getBuildInformation())
Gstreamer support in OpenCV
I tried to uninstall opencv and gstreamer and reinstalling them, but this did not work at all and still getting the same result

Gstreamer: NO

I also have the following configs. and info:
Platform:
Timestamp:                   2019-04-11T22:18:08Z
Host:                        Darwin 16.7.0 x86_64
CMake:                       3.9.4
CMake generator:             Unix Makefiles
CMake build tool:            /usr/bin/make
Configuration:               Release

I'm using Mac OS Mojave version 10.14.4 and trying to run the code using VSCode.
Any guidance or help how to make OpenCV recognize Gstreamer?
Comment: did you try sudo make uninstall in your build opencv directory and cmake with the parameters including -D GSTREAMER = ON ?
Answer: Previous answer is correct that you need to build from source, however, if you do decide to do that it only takes about 1-2hrs (and 1hr is just compile time during which you can walk away and do other stuff). Here is a guide for that: https://medium.com/@galaktyk01/how-to-build-opencv-with-gstreamer-b11668fa09c
These are steps for Ubuntu but they should work on Macs (also, even with Ubuntu you may still need to build from source if you want Python to be able to "see" gstreamer).
You can first try: https://medium.com/@satchitananda/setting-up-opencv-for-python-3-on-macos-sierra-with-5-easy-steps-647b64c5e0c9
Which looks less time consuming and easier, but I have not personally confirmed that it works.
Answer: I have the same issue. I ended up switching to ubuntu. I think brew won't install opencv plugin. So you might need to build from source, which I am not a fan of.


How many ways to arrange workpieces

Question: I'm wondering if there is a way to calculate arrangements for this problem without enumerating all possibilities.
There are 4 work-pieces that 4 guys A, B, C, D work on during a single day. At the end of the day each person transfers his assigned work-piece to another person. Each person works on only 1 work-piece during a day. How many arrangements of transfer are possible?
E.g. A can transfer his workpiece to C (AC), or A can transfer it to B, or A can transfer it to D, but A can't transfer it to A.
One arrangement is:
AC (A transfers to C)
BD (B transfers to D)
CA
DB

For the purposes of this question the order of transfer (e.g. whether AC is written in line above BD) doesn't matter, and the above arrangement is considered same as: CA, BD, DB, AC.
Answer: These are all the permutations of a set with 4 elements without fixed points. For a set with $n$ elements see: Fixed-point-free permutations or the many equations for it ($!n$) on wikipedia https://en.wikipedia.org/wiki/Derangement.
Answer: Alternative approach:
Person-A can transfer to anyone of three people.
So, there will be an initial factor of $(3)$.
Now, you can assume, without loss of generality, that Person-A transfers to Person-B.
Now, there are 3 people that Person-B might transfer to:

If Person-B transfers to Person-A, then the transfers are set, since Person-C and Person-D must then exchange.
If Person-B transfers to Person-C, then Person-C must transfer to Person-D (or else no one is transferring to Person-D).
Similarly, if Person-B transfers to Person-D, then Person-D must transfer to Person-C.

Therefore, if you know that Person-A transferred to Person-B, then there are $(3)$ possible ways that the transfers can be completed.
Therefore, the total number of satisfying groups of transfers is
$$3 \times 3.$$

How many ways to arrange workpieces


Book module versus Taxonomy module

Question: The Book and the Taxonomy modules are very similar for organising content in a hierarchy structure.
Both modules:

Allow Multi level hierarchies
Can be accessed by Views
All custom theming via node.tpl.php files
Offer the means for promoting or demoting content within a hierarchy, or moving it to another hierarchy

The Book module has the added advantage of creating navigation system (Although you could probably recreate this with taxonomy and Views).
My question is this: Which is the better to use for organising content in a hierarchical structure? Does one have any disadvantages or advantages over the other?
It seems odd to me that two similar modules exist in core. Why aren't they just combined into one?
Answer: The difference is that the Book module defines a content type ("Book page") that can be used to create books. The Book module allows also to create a hierarchical structure, and it shows:

the list of the child pages associated with the current book you are visualizing
a menu that allows to pass from a page to the next one, the previous one, and the top one
a link that allows you to create a new child page
a link that allows you to see a printable version of the current book page

The Taxonomy module is used to categorize the content of a site, but when you click on the link of a taxonomy term used by a node, the page you see shows all the nodes using that taxonomy term, without allowing you to pass from a node to the successive, or the one hierarchically on the top.
Said in sort, the Taxonomy module handles an attribute (in Drupal terminology is a field, handled with the field API) that can be associated with nodes, but also to users, while the Book module defines a content type that can be used to create books, and allows to handle that content type in a hierarchical way.

Book module versus Taxonomy module


AngularJS check if user is in the tab

Question: I'd like to check if the user is currently viewing the browser tab,
like if you are at google chrome and you have facebook, twitter and the application,
and you want to know if the application is selected between all 3.
Is there a way to do it with purely with angular or do I need to do something like this:
$(window).focus(function() {
  console.log('enter');
}).blur(function() {
  console.log('out');
});

Comment: what do you mean by "viewing the tab"? You can check which tab is selected thanks to your controller http://jsfiddle.net/j0wu52ar/
Comment: Looks like a duplicate of http://stackoverflow.com/questions/3479734/javascript-jquery-test-if-window-has-focus . AngularJS has no special tools for this.
Comment: is not like this. is the tab in the browser, like if you have facebook, twitter and youtube opened, and you want to know if you are inside facebook tab
Answer: AngularJS comes bundled with JQLite (subset of jQuery). You can inject the mock $window to keep it testable or just use window. Your call :
angular.element($window).bind('focus', function() {
  console.log('enter');
}).bind('blur', function() {
  console.log('out');
});

AngularJS check if user is in the tab


Interrupt while loop with user input (controlling neopixels via arduino and python 2.7)

Question: I'm trying to control some neopixels connected to an arduino via python and am running into a problem.  For the purposes of this demo, they light up when the arduino receives the "H" or "L" character via serial.
My original script was:
import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600)
#this is necessary because once it opens up the serial port arduino needs a second
time.sleep(3)

ser.write('H')

While it worked fine when I typed it into the python console, the lights turned off about 3 seconds in when I ran it as a script.  After doing some digging, it looked like one work around was to just turn the last bit into a while loop so the serial connection did not close:
import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600)
#this is necessary because once it opens up the serial port arduino needs a second
time.sleep(1)

while True:
    ser.write('H')
    time.sleep(3)

This kept the light on, but created a new problem.  If I want the lights to change according to user input I can do it once:
import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600)
#this is necessary because once it opens up the serial port arduino needs a second
time.sleep(1)

choice= raw_input("1 or 2?")

if choice == "1":

    while True:

        ser.write('H')
        time.sleep(3)

elif choice == "2":

    while True:

        ser.write('L')
        time.sleep(3)

But then the script is just stuck in the sub loop.  How do I keep the subloop running (i.e. keep the light on) but also waiting to respond to a new user input?
thank you!
Comment: These might help: http://stackoverflow.com/questions/32369495/how-to-wait-for-an-input-without-blocking-timer-in-python , http://stackoverflow.com/questions/16828387/how-to-accept-user-input-without-waiting-in-python , http://stackoverflow.com/questions/31340/how-do-threads-work-in-python-and-what-are-common-python-threading-specific-pit
Answer: This is the solution I found myself.
import serial
import time

ser = serial.Serial('/dev/ttyACM0', 9600)

#this is necessary because once it opens up the serial port arduino needs a second
time.sleep(2)

#ask for the initial choice
choice= raw_input("1 or 2?")

#keeps the loop running forever
while True:

    #if the initial choice is 1, do this
    while choice == "1":

        #send the H signal to the arduino
        ser.write('H')

        #give the user a chance to modify the chioce variable
        #if the variable is changed to 2, the while condition will no longer
        #be true and this loop will end, giving it an oppotunity to go
        #do the second while condition.
        #pending the decision the light will stay on
        choice= raw_input("1 or 2?")

    while choice == "2":

        ser.write('L')

        choice= raw_input("1 or 2?")


Countable subset of $A$ with cluster points in $A$

Question: Trying to prove:

If $A$ is any subset of $\Bbb R^p$, then there exists a countable subset $C$ of $A$ so that if $x \in A$ and $\epsilon > 0$, then there is an element $z$ of $C$ so that $\|x - z\| < \epsilon$. Hence every element of $A$ is either in $$ or is a cluster point of $C$.

Particularly the existence of $C$. I know that if $A$ is countable, all I have to do is let $C = A$. But I have no idea how to come up with a set like this for uncountable $A$. I'm not even convinced such a set exists if $A = (0, 1)$ in $\Bbb{R}$, since it seems like for any countable $C$ I choose, I can always come up with a small enough $\epsilon$ to make some of $A$ neither an element nor a cluster point of $C$.
Answer: HINT: Use the fact that there is a countable base $\mathscr{B}$ for the topology of $\Bbb R^n$. Let $$\mathscr{B}_0=\{B\in\mathscr{B}:B\cap A\ne\varnothing\}\;,$$ for each $B\in\mathscr{B}_0$ choose a point $x_B\in B\cap A$, and let $C=\{x_B:B\in\mathscr{B}_0\}$.


Custom Block Can't Invalid the Cache

Question: I wrote a block in Drupal 8 to render a menu based on the http headers of the request, which contains the users' information from the idp. This menu is supposed to be different from user to user, but currently it is cached once it is generated when the very first user login to the website.
I have tried setting the cache max-age to 0, but it didn't seem to work at all. Then I tried the session of the cache contexts, but still those pages visited by previous users are cached. Neither did header:custid.
Here is a sample code
class MyBlock extends BlockBase{
  public function build () {
    return array(
      '#cache'=>array('contexts'=>array('session')),
     '#markup' => ...,
    );
  }

 ...
}

Thanks in advance for any help!
Comment: This is because page_cache module caches complete pages for anonymous users. If you want to have this for them as well and not jsut for logged in users, where dynamic_page_cache comes to play, and still keep catching then you have to do it via JS. If this happens just for some anonymous users then you can implement page request/response policy tagged service that will stop page_cache from serving cached content and instead the dynamic_page_cache will be used.
Answer:
I wrote a block in Drupal 8 to render a menu based on the http headers
  of the request, which contains the users' information from the idp.

If you want to build a new page for every request then there is no point in using the page cache, so I would uninstall the core module Internal Page Cache.
Edit: Just read in drupal planet, BigPipe will be available in Drupal 8.1, then you have a perfect solution for this. But in the meantime running a site without page cache for anonymous users is fine, you still have the dynamic cache and can use the cache tags.
Answer: This is because page_cache module caches complete pages for anonymous users. If you want to have this for them as well and not jsut for logged in users, where dynamic_page_cache comes to play, and still keep catching then you have to do it via JS. If this happens just for some anonymous users then you can implement page request/response policy tagged service that will stop page_cache from serving cached content and instead the dynamic_page_cache will be used. See the headers on page request and look for x-drupal-cache and x-dynamic-page-cache headers(they will have hit, miss, uncacheable or they won't be present).
Answer: As you can read in this post you should implement the following methods to override the default cache configuration for custom blocks:

defaultConfiguration()
getCacheContexts()
getCacheTags()
getCacheMaxAge()

In your case, you need to add this code to your block:
 /**
  * {@inheritdoc}
  */
  public function getCacheContexts() {
    return ['session'];
  }

Custom Block Can't Invalid the Cache


Limit of an infinite product

Question: I want to evaluate this limit :
$$\lim_{n\to \infty}\frac {\left((2n+1)(2n+2).....(2n+n)\right)^{1/n}}n$$
What I did was that I bought the $\frac 1n$ inside and divided throughout by $n^n$ which left me with the answer as $2$. But the correct answer is $\frac {27}{4e}$. How?
PS : forgive me I dont know MathJax or LaTex so :P
Comment: Note:  I reformatted your post pretty heavily.  Please check to make sure I didn't accidentally change your meaning.
Comment: @lulu Thanks a lot!
Answer: I'm not going to tell you everything. But I'll guide you through the right steps. The mistake with your method is that the limit must be applied after multiplication not after Multiplication. so here you go.
Take the n inside. Take log of the the product. Now you have a summation of log((2n+r)/n) from r=1 to n, and whole divided by n. As n tends to infinity , this is a integral expressed as sum of limits. Find the integral and solve it using appropriate limits (what could it be ?)
Have fun solving !!
PS. nit giving entire answer so as to not deprive you of the satisfaction of solving it. And sorry for my formatting, i dont know latex or mathjax either.

Limit of an infinite product


Solve this system of equations using elimination for $x(t)$ and $y(t)$

Question: I'm taking an online Differential Equations class and don't understand how to solve this system of equations using elimination.
I tried the typical algebraic method but am running into trouble:

$x'+y'-x=5$
$x+y'+y=1$

So
$y=x'-2x-4$
$x=-5+x'+y'$
But I can't imagine this is how I leave the equation.  My guess is the equation should be in some form where I can use separation of variables or something, but I'm not entirely sure.
Any help is greatly appreciated.
Comment: What if you take the derivative of the second equation and now you have three equations to work with?
Comment: @Moo oh man I think you're right!  Thanks for that suggestion
Answer: Just write the system as

$$(D-1)x+Dy=5\\
x+(D+1)y=1$$

where $D=\frac{d} {dx} $. Applying the differential operator $- (D-1)$ to the second equation and adding both equations gives the equation

$$  -(D^2-1)y+Dy=6 \implies y''-y'-y=6. $$

I think you can finish it!
Comment: It is just ridiculous (and even more) to see, once more, a downvote for a good answer !
Comment: @Moo Yes it should be!  Thanks for the comment.
Comment: @downvoter it is corrected!
Comment: @Moo typos are expected!  The most important thing is the idea and the techniques!  Th OP has to take care of these things!!
Comment: @ClaudeLeibovici It is just an immature act!
Answer: Even more basic than the good solution from Mhenni Benghorbal to whom all credit must be given.
Your system being $$x'+y'-x=5\tag 1$$ $$x+y'+y=1\tag 2$$ From $(2)$, extract $x$ $$x=1-y-y'\tag 3$$ Differentiate to get $$x'=-y'-y''\tag 4$$ Plug $x$ from $(3)$ and $x'$ from $(4)$ into $(1)$ to get $$-y'-y''+y'-1+y+y'=5\tag 5$$ that is to say $$-y''+y'+y=6 \tag 6$$ Solve $(6)$ for $y$ and plug the result in $(3)$ to get $x$ directly.
Comment: So I did what you said and I got $x=1-y''+y-6$ and $y=y''-y'-y+6$ but I don't understand why these are 'solutions' to the question.  Would you mind elaborating for me?  Is it because they are linear solutions in terms of one variable?  I just want to understand.  Thanks!


How to setup dbaccess for informix?

Question: We have installed informix client in 64 bit machine. Informix client doesn't include "dbaccess" command. Our application uses "dbaccess" command in many shell scripts. As per the configuration we are not supposed to install server here. Is it possible to download or configure "dbaccess" alone?
Appreciate the support in advance.
Thanks,
Mathew Liju
Answer: There is a standalone 9.40 version here. If this version doesn't works well, you can try to browse the IBM download site or a Google search.

How to setup dbaccess for informix?


On the monadicity of Cat

Question: I heard that the category of small categories is monadic over the category of small graphs, the monad being the free-graph functor. I interpreted that statement as: the Eilenberg-Moore category of the free-graph monad is isomorphic to the category of small categories.
However, I can't figure out such isomorphism.
Are those categories really isomorphic? Or are they only equivalent?
I would welcome any hint on how to define this isomorphism or equivalence.
Answer: Let $C$ be a small category, $UC$ its underlying graph, and $FUC$ the free category on $UC$. All of these entities have the same set of objects, say $X$. Now, $F(UC)(x,y)$ is the set of all formal paths in $C$ from $x$ to $y$. In other words, a typical element in $FUC(x,y)$ is a finite composable sequence of morphisms in $C$ starting at $x$ and ending at $y$. Since $C$ is a category, such a sequence can be composed in $C$ to yield a single morphism $x\to y$. This defines a function $a\colon FUC(x,y)\to UC(x,y)$. All of these patch together to give a morphism $a\colon UFUC\to UC$ in $\mathbf{Grph}$. It is easily seen to be an algebra for the monad $UF$, so each category $C$ determines an algebra structure on $UC$. Conversely, if $G$ is a graph and $a\colon UFG\to G$ is an algebra structure, then deciphering the unit and multiplication compatibilities show that $a$ resolves each composable sequence of morphisms into a single morphism. Further, it does so associatively and it assigns identities as well. In other words, an algebra structure on $G$ is a category structure on $G$. These two processes are each other's inverse, so, yes, the category of small categories is monadic in the sense that it is isomorphic to the Eilenberg-Moore category of the monad induced by the free category functor on graphs.
As usual with monads, the way the algebraic structure is encoded is unbiased; while typically in a category we think of identities and composition of two morphisms as the structure, and then impose associativity axioms, the monad way defines the category by first creating the object encoding all composition problems to be solved, and then the algebra structure is an assignment of a solution for each such problem, in a coherent way.
More generally, and with virtually the same proof, the category of small $V$-enriched categories is monadic over $V$-graphs, provided $V$ is symmetric cocomplete closed monoidal (it suffices that $V$ has all small coproducts and that the tensor product distributes over them).
Comment: This looks very similar to a manual proof that the underlying set functor from monoids to sets is monadic -- which is probably unsurprising, given the interpretation of small categories as being essentially monoids with some "grading" structure.  In particular, for example given algebra structure $a : UFG \to G$ you define composition of compatible maps $x,y$ as being $a([x, y])$ and then associativity follows from $a([a([x,y]),z]) = a([a([x,y]),a([z])]) = a(\mu([[x,y],[z]])) = a([x,y,z]) = \cdots = a([x,a([y,z])])$.
Comment: @Bob That would follow from $a([x, a([]) ]) = a([a([x]), a([]) ]) = a(\mu([ [x], [] ])) = a([x])$.
Comment: Because the left hand side is $(a \circ UFa)([ [x], [] ])$ since in general $UF(g)([y_1, \ldots, y_n]) = [g(y_1), \ldots, g(y_n)]$; and by the condition that $a$ is a $UF$-action, we must have $a \circ UFa = a \circ \mu$.
Comment: By induction on the length of the list, using the identity $a([y_1, \ldots, y_n]) = a(\mu([ [y_1,  \ldots, y_{n-1}], [y_n] ])) = a(UFa([ [y_1, \ldots, y_{n-1}], [y_n] ])) = a([ a([y_1, \ldots, y_{n-1}]), a([y_n]) ]) = a([ a([y_1, \ldots, y_{n-1}]), y_n ])$, it follows that an algebra is uniquely determined by its actions on the empty list and on lists of two elements.  And pretty much directly from the definitions, if you start from an algebra $(G, a)$ and get a category and another algebra, that new algebra agrees with the old one on the empty list and on two-element lists.
Comment: @DanielSchepler Why is $a([a([x]),a([])])$ equal to $a(μ([[x],[]]))$?
Comment: @DanielSchepler Starting from an algebra $(G, a)$, one can obtain a category, and then again an algebra. But why this obtained algebra is equal to the original $(G, a)$?
Comment: OK, actually its actions on the empty lists (plural) which are indexed by vertices of the graph...
Comment: @Bob take instead a single object and all elements of $G$ as the morphisms.
Comment: @DanielSchepler How do you prove that $a([x,a([])]) = a([x])$?

On the monadicity of Cat


How to compare an array against a list of UI elements in Cypress?

Question: In my Cypress tests, I am trying to compare an array of objects (names) returned to me from a SQL query against a list of elements on my UI (the same names).
Here is my initial attempt at comparing the values:
import searchResultsPage from "../page_objects/searchResults.page"

cy.task('queryDb', `SELECT * FROM myTable where name like '%${queryInput}%';`).then(result => {
    for(var i=0;i<result.length;i++) {
        searchResultsPage.name(i+1).should('have.text', `${result[i].name}`)
    }
})

Here is my page object file - searchResults.page.js:
class searchResultsPage {

    name(index) { return cy.get(`#mat-option-${index+1} > .mat-option-text`) }

}

module.exports = new searchResultsPage()

The problem with the above code is that the lists contain the exact same values, however they are not in the same order.
The database can store the names like Adam, Bill, Charlie, but the UI may display them like Charlie, Adam Bill.
The test should pass if the same records are in both lists, regardless of their order.
I attempted to re-order both arrays alphabetically below, & compare the values. The array from the SQL query (result) is sorting as expected. However, the array of HTML elements (myArray) is not sorting alphabetically:
cy.task('queryDb', `SELECT * FROM myTable where name like '%${queryInput}%';`).then(result => {

        let myArray;

        cy.get('mat-option').then(elements => {

            myArray = Array.from(elements, element => element.innerText);

            cy.log(myArray)

            myArray.sort((a, b) => {
                if (a.name  b.name) {
                    return 1;
                }
                return 0;
            });

            cy.log('HTML array:')

            for (var i = 0; i < myArray.length; i++) {
                cy.log(myArray[i])
            }

            cy.log('*********')

            result.sort((a, b) => {
                if (a.name  b.name) {
                    return 1;
                }
                return 0;
            });

            cy.log('SQL array:')

            for (var i = 0; i < result.length; i++) {
                cy.log(result[i].name)
            }

            cy.log('*********')

        }).then(() => {

            for (var i = 0; i < result.length; i++) {
                cy.log(result[i].name)
                cy.log(myArray[i])
                expect(result[i].name).to.deep.eq(myArray[i])
            }

        })
    })

Can someone please explain why the result array isn't sorting alphabetically above?
Comment: You could gather the HTML elements `innerText` & sort them alphabetically before the assertion. There's many ways to solve this, this is just one
Comment: Hi @ManuelAbascal Thanks for your suggestion. I have attempted to sort alphabetically & updated my question with my effort. I'm able to sort one array, but am facing issues with the other.
Answer: Rather than trying to sort it yourself, you can use set membership
cy.task('queryDb', `SELECT * FROM myTable...`).then(result => {
  cy.get('mat-option')
    .should(elements => {
      const elementNames = Array.from(elements, element => element.innerText))
      const resultNames = result.map(r => r.name)

      expect(resultNames).to.have.members(elementNames)

    })
})

Using .should() will allow Cypress to retry to the assertion (in case the elements are still loading).
Ref: Testing Arrays and Objects with Chai.js
Answer: One way to do it is to split sort and join both the sql and the html texts

  it("does some checking", () => {
    // this would come from the sql task
    const myExpectation = "Charlie, Adam, Bill";

    // split, trim, sort, and join the string
    const cleanSql = str => str.split(/[\,]/gi).map(s => s.trim()).sort().join(",");

    // function uses `then` to get texts and sort and join it
    const getNames = (index) => {
      return cy
        .get(`#mat-option-${index + 1} > .mat-option-text`)
        .then((chainer) => {
          const values = [...chainer.map((el) => chainer[el].innerText)];
          return values.sort().join(",");
        });
    };

    // uses `equal` because the returned value is a string (have.text wouldn't work on it)
    getNames(1).should("equal", cleanSql(myExpectation));
  });

This assumes your html looks like this

Bill

Adam

Charlie

I'm using static myExpectation for local testability. You would, obviously, have to adapt that to your setup.


One command to npm install and bower in npm?

Question: A friend of mine said maven is king and npm and bower sucks because you have to run 2 commands. "npm install"... wait... then run "bower install". I heard node's package.json is able to support scripts. Is there a way for me to modify package.json so that I can do something like "npm setupmyproject" that will run both the npm install and bower install commands, then execute a grunt task? That way I can have one command to hit three birds?
Comment: Remember that just because a tool _can_ do everything doesn't mean it _should_ do everything. A shell script could easily take care of both use cases and then some (and it's not tied to a particular technology or tool).
Answer: Yes, you could add a single script to perform all those operations, e.g.
<code>{
  "scripts": {
    "setup": "npm install && bower install && grunt build"
  }
}
</code>
Then you'll be able to run it with
<code>npm run setup
</code>
Comment: An getting "missing script "setup""
Comment: @Rolando are you sure that you've added `"setup"` script to the `"scripts"` section of your `package.json` and saved all changes?


Real $3 \times 3$ matrix such that $A^{3} = Id$ and minimal polynomial $x^{2} + x + 1$

Question: Is there a $3 \times 3$ real matrix $A$ such that it has a minimal polynomial $x^{2} + x + 1$ and is such that $A^{3} = Id$? If so, what an example of such a matrix?
My thought is that the answer is no since the invariant factors of $A$ must divide $x^{2} + x + 1$ which does not split over $\mathbb{R}$. Thus $A$ does not have a rational canonical form over $\mathbb{R}$ and hence no such $A$ exists. However, I'm not sure if this is correct.
Comment: If $A^3=1$, then $A$ satisfies $x^3-1=0$. In fact, this must be its characteristic polynomial since it is degree $3$. Notice that $(x^2+x+1)(x-1)=x^3-1$. Can you conclude that such a matrix can exist?
Answer: The irreducible factors of the characteristic polynomial are precisely those of the minimal polynomial. Since the latter in this case has exactly one such factor, which is moreover of degree two, we see that the degree of the characteristic polynomial is even.
Since $3$ is not even, there can be no such matrix.


How do you theme an autocomplete dropdown

Question: I'm trying to theme an autocomplete dropdown from a textfield. In this case the autocomplete is provided from the finder module.
I need to theme it beyond just simply adding css. Ideally I'd like a theme function to override or a template file but I can't find one.
I have tried using the theme function which is provided by the finder module, however all that does is theme the resulting page, not the autocomplete drop down.
Can anyone advise?
Comment: Just to confirm, is it the autocomplete widget provided by core or one that the Finder module provides?
Comment: Ideally the one that finder provides, However if there's a way to do it with core then I'll just code it  with the normal autocomplete field. I don't need half of the gui features of finder anyway.
Comment: Can you be more specific about in what ways you want to theme the dropdown? Can you give an example of what you expect to see as the end result?
Answer: To theme the Drupal 7 finder autocomplete dropdown take a look at this video. Generally you write PHP into the 'Rewrite' settings on the advanced options of the finder. I would avoid using the theme() function, it might be obsolete.
To theme the Drupal 6 finder version, you may find this article helpful. It uses theme functions.


Confusion around Bellman (update) operator

Question: I've seen at least two versions from the CS229, wondering if there is a comprehensive resource around this topic
The first version:
$$
B(V)(s) = V'(s) = R(s) + \gamma \max_{a \in A} \sum_{s' \in S} P_{sa}(s') V(s')
$$
from problem 5 in http://cs229.stanford.edu/ps/ps4/ps4.pdf. It's called Bellman update operator in the problem description.
The second version:
$$
B(V)(s) = V'(s) = R(s) + \gamma \sum_{s' \in S} P_{s\pi(s)}(s') V(s')
$$
from problem 4 in from https://see.stanford.edu/materials/aimlcs229/problemset4.pdf, it's called Bellman operator in the problem description.
Given $a$ is equivalent to $\pi(s)$, the major difference is one has a $\max$ operator which the other doesn't, so which is which, then?
Answer: The precise terminology might differ across sources, but according to the source I learned from, the first one is called the Bellman optimality equation or simply the Bellman equation. The second one is the Bellman expectation equation. The expectation equation is linear in $V$, so you can solve for $V$ using simple linear algebra. The optimality equation, on the other hand, is nonlinear (due to the max operation) so there is no closed-form solution... which is why there are many algorithms to find the optimal solution (Q-learning, value iteration, policy iteration etc.).
The expectation equation is usually used to evaluate a known policy $\pi$, whereas the optimality equation is used to learn the optimal policy $\pi^*$.
Comment: Cool, comparing to the comparison in https://stackoverflow.com/questions/37370015/what-is-the-difference-between-value-iteration-and-policy-iteration, **Bellman optimality equation** correspond to **Value iteration**, while **Bellman expectation equation** correspond to **Policy evalution**, insightful.
Comment: Sounds the term "Bellman operator" is overloaded, which confused me.
Answer: A good resource will be the classical textbook on reinforcement learning Reinforcement Learning: An Introduction, mostly the part about dynamic programming. Another good resource will be Berkeley's opencourse on Artificial Intelligence on EdX.
The difference in their name (Bellman operator vs Bellman update operator) does not matter here. You will see other names like Bellman backup operator. They are all the same thing. Basically it refers to the operation of updating the value of state $s$ from the value of other states that could be potentially reached from state $s$.
The definition of Bellman operator requires also a policy $\pi(x)$ indicating the probability of possible actions to take at state $s$. The second definition of Bellman operator in your example means updating the value of state $s$ by taking actions with probability from any predefined policy $\pi$. On the other hand, the first definition of Bellman operator is only specific to one policy, which is to take action on state $s$ that gives the highest value of state $s$ with 100% probability.
Therefore, the first definition can be regarded as a specific example of the second definition: the second definition works for any policy, while the first definition only works for the optimal policy that gives the highest value.


Exiting gracefully when running an invalid command c++

Question: I'm using boost for running commandline commands from my application. I am using the following piece of code that I've encapsulated into a helper function:
tuple<string, int> Utility::RunCommand(const string& arguments) const
{
    string response;
    int exitCode;
    try
    {
        ipstream iStream;
        auto childProcess = child(arguments, std_out > iStream);
        string line;

        while (getline(iStream, line) && !line.empty())
        {
            response += line;
        }

        childProcess.wait();
        exitCode = childProcess.exit_code();
    }
    catch (...)
    {
        // log error
        throw;
    }

    return make_tuple(response, exitCode);
}

Now I have a command which only runs on machines with certain properties. This method returns the expected response and error code on those machines. On other machines, it throws an exception.
I've tried running the command manually on a machine where it's supposed to fail and it returns the following output:
POWERSHELL
PS C:\Users\xyz> dummy-cmd
dummy-cmd : The term 'dummy-cmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ dummy-cmd
+ ~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (dummy-cmd:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

COMMAND PROMPT
C:\Users\xyz>dummy-cmd
'dummy-cmd' is not recognized as an internal or external command,
operable program or batch file.

How can I make it run such that it returns a non-zero error code rather than throwing an exception?
Comment: The problem is your executable does not exist. That is what Powershell is complaining about.
Comment: Are you compiling your code to get a `dummy-cmd.exe`?
Comment: @doron True, I'm aware of that part. I'm wondering if there's a way to call the command so it returns back an error code instead of throwing the exception?
Comment: @FredLarson No. dummy-cmd.exe exists on some machines and absent on others.
Answer: That's what your catch clause is for, to handle exceptions. It doesn't have to rethrow them:
tuple<string, int> Utility::RunCommand(const string& arguments) const
{
    string response;
    int exitCode;
    try
    {
        ipstream iStream;
        auto childProcess = child(arguments, std_out > iStream);
        string line;

        while (getline(iStream, line) && !line.empty())
        {
            response += line;
        }

        childProcess.wait();
        exitCode = childProcess.exit_code();
    }
    catch (PlatformNotSupportedException& e)
    {
        std::cerr << "That operation is not supported on this platform." << std::endl;
        exit(1);
    }
    catch (...)
    {
        std::cerr << "Unspecified error occurred." << std::endl;
        exit(1); // give nonzero exit code
        //throw; // take out this
    }

    return make_tuple(response, exitCode);
}

Comment: I can do that. But is there a way to avoid going into the catch? Basically a way to execute the command such that it returns from the method. Also, does exit(1) exit from the application? I would like to return back to the caller.
Comment: @commandocaddy So wait, do you want the exception to be thrown and caught in this method, which then returns? Or do you want no exception at all?
Comment: Ideally no exception at all. Because a huge number of machines will not have this executable. I would want to prevent exception handling as it could be expensive.
Comment: @commandocaddy Then why not modify your method to not throw an exception, and instead just emit the corresponding exit code? Then you won't need exception handling.


Submitting a form more than once

Question: I am try to have a forum submits more then once when a user clicks the submit button. Why? I am try to add more then one idem to a shopping cart, The shopping cart software I am using doesn't support adding more then one product at a time and I don't want to edit there core code. The hidden forum would have the product ids like '1,2,3' I'd then need the JavaScript to separate the values and post each one using AJAX to the cart. I am not great a JavaScript but I coded what I think should work but its just giving me a alert: 'There was a problem with the request.' twice. I can't see whats wrong with it, any and all help and suggestions are welcomed! Here the code:
JS

HTML
<form action="javascript:testResults(document.getElementById('myform'));" name="myform" id="myform">
<input type="text" name="product_id" id="product_id" />
<input type="hidden" name="quantity" id="quantity" value="1" />

<input type="submit" name="button" value="Submit" />
</form>

post.php
<?php
print_r($_POST);
?>

Comment: lots and lots of spelling errors making comprehension difficult. You mean form not forum right ?
Comment: idem -> item.  there -> their. Here -> Here's.
Comment: Sorry! I have been up 12+ hours, my bad spelling get a lot worse the less sleep I have.
Answer: If you want to add two items to the cart shouldnt you be doing two posts with the same item? I can just see one post per item there. You are not taking the quantity into account. But this is not the problem. In this case this is only a logic error.
For the javascript side I would recommend you to use jQuery to treat the ajax stuff because it will make your life WAY easier than regular javascript that might event not work with all browsers.
This is the link related to the POST method of jQuery: http://docs.jquery.com/Post
Hope it helps
Comment: I don't want to take quantity into account it will always be 1 but its required by that shopping cart. I don't know why I would do two posts with the same item that would put that idem it the cart twice and I want each one them once.
Answer: It is against all the programming logics to post a form several times instead of having a more complex form. From what I can see or understand from your code you are trying to loop through your splitted (brokenstring) string. Your loop is not constructed where and how it should be. Anyway, if I were you, I would consider migraton to another free cart o the possibility to write one myself. From what I see you will be able to do so with a little bit of help from here.
Comment: is it possible to post more than once? i need to pass json in params so using post instead of get but it limits me to just one submit...https://stackoverflow.com/questions/44773514/pass-kendo-grid-model-in-ajax-call

Submitting a form more than once


Variance of quadratic form for multivariate normal distribution

Question: This is a homework problem I'm trying to solve but I can't seem to solve Q1b without using the theorem.

I am also given the fact that
$$E(y'Ay)=tr(A\Sigma)+\mu'A\mu$$
I've tried using the trace-expectation trick but to no avail. Assuming $y\sim N(0,I)$,
$$\begin{align*}
var(y'Ay)
&=E[(y'Ay-tr(A))^2]\\
&=E(y'Ayy'Ay)-tr(A)^2\\
&=E(tr[y'Ayy'Ay])-tr(A)^2\\
&=E(tr[Ayy'Ayy'])-tr(A)^2\\
&=tr(E[Ayy'Ayy'])-tr(A)^2\\
&=tr(AE[yy'Ayy'])-tr(A)^2
\end{align*}
$$
Then I'm stuck.
Comment: Another duplicate (albeit slightly disguised): https://stats.stackexchange.com/questions/413431.
Comment: @Stubborn Sorry: the last digit "1" got cut off during the cut and paste.  I have fixed the link--and thanks for checking.
Comment: https://math.stackexchange.com/questions/442472/sum-of-squares-of-dependent-gaussian-random-variables/442916#442916
Comment: https://stats.stackexchange.com/q/303466/119261
Answer: Suppose $A=((a_{ij}))$ and $y=(y_1,y_2,\ldots,y_n)'\sim N(0,I_n)$, so that $y_i$s are i.i.d standard normal.
You already have $\operatorname E[y'Ay]=\operatorname{tr}(A)$ by the result you quoted (discussed here).
For the variance, you can simply use $$\operatorname{Var}(y'Ay)=\operatorname E[(y'Ay)^2]-(\operatorname E[y'Ay])^2\tag{1}$$
To compute the first expectation, we write the quadratic form as $y'Ay=\sum_{i,j}a_{ij}y_iy_j$ to get $$(y'Ay)^2=\sum_{i,j,k,l}a_{ij}a_{kl}y_iy_jy_ky_l \tag{2}$$
Now observe that $$\operatorname E[y_iy_jy_ky_l]=\begin{cases}3&,\text{ if }i=j=k=l \\ 1&,\text{ if }i=j,k=l;i=k,j=l;i=l;j=k \\ 0&,\text{ otherwise }\end{cases}$$
Therefore taking expectation on both sides of $(2)$,
$$\operatorname E[(y'Ay)^2]=3\sum_i a_{ii}^2+\sum_i\left(\sum_{k\ne i}a_{ii}a_{kk}+\sum_{j\ne i}a_{ij}^2+\sum_{j\ne i}a_{ij}a_{ji}\right)\tag{3}$$
Keeping in mind that $A$ is symmetric, you have $\operatorname{tr}(A^2)=\sum_{i,j}a_{ij}^2$.
It is now straightforward to see that $(3)$ reduces to $$\operatorname E[(y'Ay)^2]=(\operatorname{tr}(A))^2+2\operatorname{tr}(A^2)$$
From $(1)$ you get the desired result $$\boxed{\operatorname{Var}(y'Ay)=2\operatorname{tr}(A^2)}$$
You can now try to generalize this to the case $y\sim N(0,\Sigma)$ and hence to $y\sim N(\mu,\Sigma)$.
Reference:

Linear Regression Analysis by Seber and Lee.
Comment: Since $A$ is symmetric, the easier way to find the variance directly for the $N(0,I_n)$ case is to use spectral decomposition to write $A=P'\Lambda P$ for some orthogonal $P$ and $\Lambda=\operatorname{diag}(\lambda_1,\ldots,\lambda_n)$ where $\lambda_i$ are eigenvalues of $A$. Then $y'Ay=x'\Lambda x=\sum_{i=1}^n \lambda_i x_i^2$ with $x=Py\sim N(0,I_n)$.
Answer: As the passage from step (c) to step (d) is non-trivial, let me add a new answer to this interesting question here.
(b) Let $A = O'\Lambda O$ be the spectral decomposition of $A$, where $O$ is an order $n$ orthogonal matrix and $\Lambda = \operatorname{diag}(\lambda_1, \ldots, \lambda_n)$. Let $z = Oy$, then $z \sim N(0, I_n)$ as $y \sim N(0, I_n)$ and $O$ is orthogonal.  It then follows $y'Ay = y'O'\Lambda Oy = z'\Lambda z = \sum\limits_{i = 1}^n\lambda_i z_i^2$ and the independence of $z_1^2, \ldots, z_n^2$ that
\begin{align}
\operatorname{Var}(y'Ay) = \sum_{i = 1}^n\lambda_i^2\operatorname{Var}(z_i^2) =
\sum_{i = 1}^n\lambda_i^2(E[z_i^4] - (E[z_i^2])^2) = 2\sum_{i = 1}^n\lambda_i^2 =
2\operatorname{tr}(A^2).  \tag{1}
\end{align}
In $(1)$, we used that $E[z_i^4] = 3$ and $E[z_i^2] = 1$ if $z_i \sim N(0, 1)$.
(c) If $y \sim N(0, \Sigma)$, we can write $y = \Sigma^{1/2}z$, where $z \sim N(0, I_n)$, it then follows by $(1)$ and the symmetry of $(\Sigma^{1/2})'A\Sigma^{1/2}$ that
\begin{align}
\operatorname{Var}(y'Ay) &= \operatorname{Var}(z'(\Sigma^{1/2})'A\Sigma^{1/2}z) =
2\operatorname{tr}((\Sigma^{1/2}A\Sigma^{1/2})^2) \\
&= 2\operatorname{tr}(\Sigma^{1/2}A\Sigma A\Sigma^{1/2})
= 2\operatorname{tr}(A\Sigma A\Sigma) = 2\operatorname{tr}((A\Sigma)^2). \tag{2}
\end{align}
In $(2)$, we used that $(\Sigma^{1/2})' = \Sigma^{1/2}$ and $\operatorname{tr}(M_1M_2) = \operatorname{tr}(M_2M_1)$ for order $n$ matrices $M_1, M_2$.
(d) If $y \sim N(\mu, \Sigma)$, we can write $y = \mu + z$, where $z \sim N(0, \Sigma)$, whence
\begin{align}
y'Ay = (\mu + z)'A(\mu + z) = \mu'A\mu + 2\mu'Az + z'Az.
\end{align}
Therefore,
\begin{align}
\operatorname{Var}(y'Ay) &= \operatorname{Var}(z'Az + 2\mu'Az + \mu'A\mu) = \operatorname{Var}(z'Az + 2\mu'Az) \\
&=\operatorname{Var}(z'Az) + 4\operatorname{Var}(\mu'Az) + 2\operatorname{Cov}(z'Az,
2\mu'Az). \tag{3}
\end{align}
By $(2)$,
\begin{align}
\operatorname{Var}(z'Az) = 2\operatorname{tr}((A\Sigma)^2). \tag{4}
\end{align}
In addition,
\begin{align}
\operatorname{Var}(\mu'Az) = \mu'A\operatorname{Var}(z)A\mu = \mu'A\Sigma A\mu.
\tag{5}
\end{align}
To evaluate $\operatorname{Cov}(z'Az, 2\mu'Az) = 2E[z'Az\mu'Az]$, note that the expansion of $z'Az\mu'Az$ consists of only terms of forms (regardless constant coefficients) $z_i^3, 1 \leq i \leq n$ and $z_i^2z_j, 1 \leq i \neq j \leq n$, while for $z \sim N(0, \Sigma)$, it is easy to verify that
\begin{align}
& E[z_i^3] = 0, \quad 1 \leq i \leq n, \\
& E[z_i^2z_j] = 0, \quad 1 \leq i \neq j \leq n.
\end{align}
Therefore,
\begin{align}
\operatorname{Cov}(z'Az, 2\mu'Az) = 0. \tag{6}
\end{align}
Substituting $(4), (5), (6)$ into $(3)$, we obtain
\begin{align}
\operatorname{Var}(y'Ay) = 2\operatorname{tr}((A\Sigma)^2) + 4\mu'A\Sigma A\mu.
\end{align}
This completes the proof.


Como mudar posição de todos os elementos da matriz, trocando o número da linha pela coluna?

Question: Eu tentei este algoritmo mas o resultado fica o mesmo apos esta troca
  int[,] array = new int[10,10];

  for (int l = 0; l <  10; l++)
    for (int c = 0; c < 10; c++)
     {
       int temp = array[l, c];
       array[l, c] = array[c, l];
       array[c, l] = temp;
    }

Output antes da troca

 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

depois da troca

 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |

Comment: Se o código é só isso, todos valem o mesmo, então não tem como notar diferença. Se colocar um código real, um [mcve], fica mais fácil ajudar.
Comment: Mas não entendo bem porque não resulta
Comment: Gostei das duas soluções nao dá para marcar as duas? é que funcionam as duas xD
Comment: Só pode votar em todas, aceitar só uma, tem que escolher a que é a que mais aproxima do que deseja. Funcionar, funciona, uma gera um objeto novo. Eu fiz uma que economiza memória, é mais rápida e vai na linha do que você postou, eu achei uma solução melhor, por isso postei, mas a decisão é sua.
Answer: Seu algoritmo não parece fazer nenhum sentido pra mim.
Transpor uma matriz, irá inverter o tamanho dela (linhas viram colunas e colunas viram linhas).
O certo seria fazer algo como:
public int[,] Transpor(int[,] matriz)
{
    int w = matriz.GetLength(0);
    int h = matriz.GetLength(1);

    int[,] novaMatriz = new int[h, w];

    for (int i = 0; i < w; i++)
    {
        for (int j = 0; j < h; j++)
        {
            novaMatriz[j, i] = matriz[i, j];
        }
    }

    return novaMatriz;
}

Veja funcionando no .NET Fiddle.
Answer: Se quiser fazer inline basta começar a verificação da coluna a partir da linha que parou. O problema é que se você começa do 0 ele inverte o que já foi invertido, então volta ao lugar original. Assim:
using static System.Console;

public class Program {
    public static void Main() {
        int[,] array = new int[10,10];
        for (int l = 0; l < 10; l++) {
            for (int c = 0; c < 10; c++) array[l, c] = c;
        }
        for (int l = 0; l < 10; l++) {
            for (int c = 0; c < 10; c++) Write($"{array[l, c]} ");
            WriteLine();
        }
        WriteLine();
        for (int l = 0; l < 10; l++) {
            for (int c = l; c < 10; c++) { // <========== mudei aqui, veja o l
                int temp = array[l, c];
                array[l, c] = array[c, l];
                array[c, l] = temp;
            }
        }
        for (int l = 0; l < 10; l++) {
            for (int c = 0; c < 10; c++) Write($"{array[l, c]} ");
            WriteLine();
        }
    }
}

Veja funcionando no ideone. E no .NET Fiddle. Também coloquei no GitHub para referência futura.


The "sum" of all fear

Question: I have come across an identity problem:

Prove $\sin(\theta+k\pi)=-1^{k}\sin(\theta)$.

I can get to the point where $\sin(\theta)\cos({k}\pi)+\cos(\theta)\sin(k\pi)$ and I assume evaluating $\sin(k\pi)$ to where $\sin(\pi)$ equals zero would eliminate $\sin(k)$ and $\cos(\theta)$.  However, $\cos(k\pi)$  evaluating to $-1^k$ is beyond me.  I understand that $\cos(\pi)$ is $-1$, the part where $k$ becomes the exponent is tripping me up at the moment. Any thoughts or explanations would be greatly appreciated!
Answer: What is the value of $cos(k\pi)$ when $k$ is 0, 1, 2,...
What is the value of $(-1)^k$ when $k$ is 0, 1, 2,...
You will see that only two possible values are involved.
Then look at the parity of $k$.
Answer: Hint:
The correct formula is $$\sin(\theta+k\pi)=(-1)^{k}\sin\theta$$ ( note the parenthesis)
where $(-1)^{k}$ is $+1$ for $k$ is even and $-1$ if $k$ is odd. I think that you can see how this is linked to $\cos (k \pi)$.
Answer: Sine is periodic with period $2\pi$. Hence $\sin k\pi=0$ follows by induction on $k$ using $k=0$ and $k=1$ as base cases.
Cosine is periodic with period $2\pi$. Hence $\cos k\pi=(-1)^k$ follows by induction on $k$ using $k=0$ and $k=1$ as base cases.
Actualy, the target equation follows directly from the $2\pi$ periodicity of $\sin$ and $\sin(\theta+\pi)=-\sin\theta$.

The "sum" of all fear


batch script to check the size of file and send mail if greater

Question: I have been working on a batch script which checks for the size of a file and send an email  in case the size is greater than 0 byte:
set V_TMP_FILE="D:\pmserver\Tgtfiles\XLI_stage\f_cdc_flag1.out"
set minbytesize=0
if exist %V_TMP_FILE% (
FOR %%A IN ('%V_TMP_FILE%') DO set size=%%~zA
if %size% GTR %minbytesize% (
    %V_EMAIL_PATH%\postie -host:internalmail.usa.xl -to:"%V_TO_EMAIL_ID%" -from:%firstname.lastname@example.com -s:"%V_SRC_TABLE_NM% %V_SUBJ%" -msg:"%V_MSG%"
) else (
    echo %V_TMP_FILE% is EMPTY
)

I am getting the below error -
Standard output and error:
h file. 0 was unexpected at this time.

D:\Informatica91\server\bin>if  GTR 0 (

Kindly help me solve this issue , its really taking me time to dig in the issue.
Answer: Batch parses an entire command, even if it's spread across multiple lines, before executing it. As part of that procedure, it substitutes ANY %var% variable for the value VAR has at the time it was PARSED - that is, BEFORE it is executed.
SIZE was NOT set before the IF... was parsed, so its value is replaced by (nothing) and hence the command is interpreted as IF (nothing) GTR 0
Further, the single-quotes around the filename would change the filename dince ' is a valid filename character.
Number of cures: easiest seems to be
if exist %V_TMP_FILE% (
FOR %%A IN (%V_TMP_FILE%) DO (
if %%~zA GTR %minbytesize% (
    %V_EMAIL_PATH%\postie -host:internalmail.usa.xl -to:"%V_TO_EMAIL_ID%" -from:%email@example.com -s:"%V_SRC_TABLE_NM% %V_SUBJ%" -msg:"%V_MSG%"
) else (
    echo %V_TMP_FILE% is EMPTY
)

Comment: thanks for the help ,i tried using the above, but it stil doesn't work, i am getting no mails even when the file is greater than 0 byte, plesae help .
Comment: earlier i was using this script- for %%A in (%V_TMP_FILE%) do ( if %%~zA==0 ( echo. "%%A" is empty ) else ( %V_EMAIL_PATH%\postie -host:internalmail.usa.xl -to:"%V_TO_EMAIL_ID%" -from:%firstname.lastname@example.com -s:"%V_SRC_TABLE_NM% %V_SUBJ%" -msg:"%V_MSG%" -a:%V_TMP_FILE% ) ) - it was working in the else part , but not in the true part ie when file size =0;   can you help me with this one .
Comment: I believe you'll find your parentheses are unbalanced. There appears to be 3 open but only 2 close-parentheses in your original post, so I assumed you were only posting a part-statement. Adding the DO added one more. Try temporarily replacing the `%V_EMAIL_PATH...` with a simple `ECHO %V_TMP_FILE% is NOT EMPTY` and possibly complete the list of possibilities by adding `)) else (echo %V_TMP_FILE% is missing)` to the code I posted in my response. the first `)` closes the `DO (` and the second the `IF` on the first line. Test against `V_TMP_FILE` absent, zero-length & non-zero length.
Answer: How many times have we had to answer this...
Ok, check out this answer that I wrote maybe a week ago. It explains everything...


Set of continuous functions from $\mathbb{R}$ to $\mathbb{R}$ is equinumerous to $\mathbb{R}$

Question: I am trying to prove that the set $\mathcal{C}:=\{f\mid f\in\mathbb{R}^\mathbb{R}\mid f \text{ is continuous}\}$ is equinumerous to $\mathbb{R}$.
To achieve this, I note that an $f\in\mathcal C$ is ''pinned down'' by $\mathbb{Q}$, i.e. if I know how $f$ behaves on $\mathbb{Q}$, I know all of $f$, since $\mathbb{Q}$ is dense in $\mathbb{R}$ and $f$ is continuous. So there is an easy injection from $\mathcal{C}$ to $\{f\mid f\in\mathbb{R}^\mathbb{Q}\mid f\text{ is continuous}\}$.
The latter set can be embedded into $\mathbb{R}^\mathbb{N}$, but I'm afraid that fact won't help me, since (as I believe): not $\mathbb{R}^\mathbb{N}\preccurlyeq\mathbb{R}$.
Could someone hint me into the right direction on proving $\mathcal{C}\sim\mathbb{R}$ (via $\mathcal{C}\preccurlyeq\mathbb{R}$)?
Comment: Actually your "easy bijection" is not a bijection, because there are continuous functions $\mathbb Q\to\mathbb R$ that don't extend to any continuous function $\mathbb R\to\mathbb R$, such as $$f(q)=\begin{cases}1 & \text{when }q^2>2 \\ 0 & \text{when }q^2\le 2\end{cases}$$ But it _is_ an injection into $\mathbb R^{\mathbb Q}$, and you can then use Cantor-Schröder-Bernstein.
Comment: You are absolutely right, I will fix this in my question
Answer: You have $$\vert \mathbb R^{\mathbb N} \vert = \vert (2^{\mathbb N})^{\mathbb N} \vert = \vert 2^{\mathbb N \times \mathbb N} \vert  = \vert 2^{\mathbb N} \vert = \vert \mathbb R \vert$$
Comment: Ah alright, so I was wrong when saying not $\mathbb{R}^\mathbb{N}\sim\mathbb{R}$. Would it however be doable to construct a specific bijection for each of above steps
Comment: @konewka Yes, it is possible to construct a specific bijection (injective functions are easy, and then the constructive proof oc Cantor-Schreder-Berenestein "seals the deal"). And anyways, diving into cardinality arithmetics, the above equalities are easy to prove.
Comment: Indeed, after one minute of trying, I found those bijections aren't hard to find
Comment: @FD_bfa Because as $\mathbb Q$ is dense in $\mathbb R$, a continuous map from $\mathbb R$ to $\mathbb R$ is defined by the values it takes on $\mathbb Q$.
Comment: $\lvert \mathcal C \rvert = \lvert \mathbb R^{\mathbb Q} \rvert = \lvert \mathbb R^{\mathbb N} \rvert$.


Send local traffic from host A:3306 to host B:3306

Question: I'm having trouble making sense of all the iptable examples out there.
I simple want all requests from my machine to host A on port 3306 to actually go to host B on port 3306. How can I do this?
Answer: Assuming you only want to redirect the connections originating from your machine (as opposed to forwarded by your machine), the following rules should suffice:
<code>iptables -t nat -A OUTPUT -d hostA -p tcp --dport 3306 -j DNAT --to hostB
iptables -t nat -A INPUT  -s hostB -p tcp --sport 3306 -j SNAT --to hostA
</code>
The first one consists in changing the destination address of the outgoing packets from hostA to hostB.
When hostB sends packets back, the second rule rewrites the source address from hostB to hostA so as not to confuse the client.
Comment: Perfect, thank you! As a side question, is it possible to use hostnames instead of IPs?
Comment: @MatthewHerbst It is possible to use hostnames instead, but there are pitfalls. From what I know, iptables resolves the hostname and stores an IP address in memory only once, when it loads the rule. And it will most likely fail if for some reason you cannot reach a DNS server (unless the hostname is hardcoded in /etc/hosts, for instance). I've always steered clear from using hostnames in iptables. If you want to do something involving hostnames, you'll probably have to do something more complicated like marking the packets with a user-level application such as squid.
Comment: Makes sense. I'll probably just stick with the IPs for now. Thanks again


Scala Hash Maps

Question: How can we select a random key value pair from a hash map in scala? I have a map of following description
var map = scala.collection.mutable.Map[(Int,Int),ActorRef]()
val random = new Random();
var keys:List[Set(Int,Int)] = map.keySet;
var randomKey:(Int,Int) = keys.get( random.nextInt(keys.size()));
var value= map.get(randomKey);

Answer: val pair = map.toIndexedSeq(Random.nextInt(map.size))

Answer: Here is an example that compiles and runs:
import akka.actor.ActorRef
import scala.util.Random

import scala.collection.mutable.{Map => MutableMap}

object RandomMap {

  def main(args: Array[String]): Unit = {
    val map:       MutableMap[(Int, Int), ActorRef] = MutableMap[(Int, Int), ActorRef]((9, 5) -> null, ((15, 1), null))
    val random:    Random                           = new Random()
    val keys:      List[(Int, Int)]                 = map.keySet.toList
    val randomInt: Int                              = random.nextInt(keys.size)
    val randomKey: (Int, Int)                       = keys(randomInt)
    val value:     Option[ActorRef]                 = map.get(randomKey)

    println(s"Random key: $randomKey -> $value")
  }
}

Scala Hash Maps


Ubuntu 16.04 32 or 64 bit which is best for MEAN + NGINX?

Question: Okay, I'm kind of lost here guys. I'm working on a web app and I'm expecting to have quite a bit of web traffic. I am planning to use Node.JS with express and MongoDB for a database all hosted on the same server.
I also plan to setup NGINX with reverse proxying to the daemonized version of the NodeJS app.
Now, I'm just trying to figure out what will perform better for this particular stack, Ubuntu 16.04 64-bit or 32-bit?
What are your suggestions, and why?
Thank you!!
Edit: Also, I'm using 1GB of RAM and a 1-core Virtual Machine hosted on Digital Ocean. I may have to scale up early on depending on performance. Basically, my question is for future-proofing which version of 16.04 is best for this particular stack regardless of how much memory and CPU cores I'm using?
Answer: Plain and simple, there is absolutely no reason to deploy anything but 64-bit systems today.
Comment: Suggest you read some basic articles on 32 vs 64 bit. One key reason is 32 bit systems can't address more than 4GB RAM, and if your application becomes popular you'll probably want to add RAM. http://www.digitaltrends.com/computing/32-bit-64-bit-operating-systems/
Comment: There are precious few reasons to *not* use a 64-bit OS. Some OS vendors have even started deprecating their 32-bit offerings entirely. It won't hurt anything to use 64-bit, and it may likely harm you going forward if you choose to use 32-bit now.
Comment: Hey, thanks for responding. Is there any particular reason why there's no reason to deploy anything but 64-bit systems? Would you happen to know why 64-bit systems are totally necessary for this stack?
Comment: @Tim I read that, but with the way nodeJS works, it seems highly possible to migrate the application to a more resourceful server if necessary.  With a ubuntu, it looks like 16.04 uses nearly 500 Mib on startup on the 64 bit version, while 32 bit only uses about 388.
Comment: @EEAA that seems like a pretty good reason to use 64-bit to me. Thank you!


Histogram for a sample in Python easily?

Question: Instead of this simplified-version of this file:
#!/usr/bin/env python
import numpy as np
import pylab as P

data = [x.rstrip().split("\t") for x in file(".data", "r").readlines()]
sdData = [float(x[1]) for x in data]
retData =[float(x[0]) for x in data]

# Calculating medians for estimating mean and sd
sdMed = sorted(sdData)[len(sdData)/2]
retMed= sorted(retData)[len(sdData)/2]

mu, sigma = retMed, sdMed
x = mu + sigma*P.randn(10000)

# the histogram of the data with histtype='step'
n, bins, patches = P.hist(x, 20, normed=1, histtype='stepfilled')
P.setp(patches, 'facecolor', 'g', 'alpha', 0.75)

# add a line showing the expected distribution
y = P.normpdf( bins, mu, sigma)
l = P.plot(bins, y, 'k--', linewidth=1.5)

#
# create a histogram by providing the bin edges (unequally spaced)
#
P.figure()
P.show()

is there something like this hist(SampleDataPoints)?
Answer: These demos look simpler.


As you drop variables, can AIC or BIC go up and then down?

Question: I have some potential spline models and I'm trying to use AIC or BIC to choose variables. I'm seeing that AIC is lower when I use all variables than if I exclude any one or two. However, if I exclude three variables, then AIC is lower than if I include all variables. Is this theoretically possible or am I potentially doing something wrong? My concern is that if I do backwards selection with AIC, the optimal model chosen will be all variables since incrementally dropping one will not reduce AIC.
Sorry I can't post a reproducible example as I have a quite complex spline model and the data is proprietary to my company.
Comment: [Relevant](https://stats.stackexchange.com/a/232494/72352)
Answer: Yes, this is definitely possible.
Remember that
$$
\mathrm{AIC} = 2k - 2 \log L
$$
where $k$ is the number of parameters and $L$ is the likelihood. As you remove features, both $k$ and $L$ decrease, so the $2k$ term decreases and the $-2 \log L$ term increases. Each of those terms is monotonic as you remove features, but their sum need not be, as they change at different rates.
If you can, it's usually better to use held-out or cross-validated likelihoods rather than AIC. That doesn't necessarily avoid the problem you're worried about, though; model selection is just hard.


Dependency Property does not work after view model was initialized

Question: I would like to write a custom asynchronous image container custom control. I have created a list from this control:
<ItemsControl ItemsSource="{Binding Items}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid>
                <custom:CustomImage Width="64" Height="64" BaseUri="{Binding Uri}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

The Items property is a list of object A what I initialize in the MainWindowViewModel:
public List Items { get; set; } = new List();

and
foreach (XmlNode item in doc.LastChild.FirstChild.SelectNodes(".//item"))
{
    Items.Add(
        new A
        {
            Title = item.FirstChild.InnerText,
            Uri = new Uri(item.SelectNodes(".//enclosure")[0].Attributes["url"].Value)
        }
    );
}

I want to set a Dependency Property on the custom control (you can see above: BaseUri="{Binding Uri}". Uri is a property of the class A.
This is the DP implementation:
public Uri BaseUri
{
    get { return (Uri)GetValue(BaseUriProperty); }
    set { SetValue(BaseUriProperty, value); }
}

public static readonly DependencyProperty BaseUriProperty =
 DependencyProperty.Register("BaseUri", typeof(Uri),
 typeof(CustomImage), new FrameworkPropertyMetadata(default(Uri), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

It works only if the CustomImage custom control doesn't have any view model. If I do this in the CustomImage's constructor:
DataContext = new CustomImageViewModel();

it doesn't work anymore.
Any idea?
Comment: Besides what's said in the answer, you should use `ObservableCollection` instead of `List` as type of the `Items` property, to enable built-in change notification when items are added to or removed from the collection.
Comment: It also doesn't seem to make sense that the `BaseUri` property binds two-way by default (or at all).
Comment: Finally "asynchronous image container" seems odd, when you could just use an Image control and set its Source property to a BitmapImage, which already implements asynchronous loading.
Answer: You should never explicitly set the DataContext of a UserControl. Doing so effectively prevents that the DataContext is inherited from the control's parent, as it is required by a binding like
BaseUri="{Binding Uri}"

So, remove the line
DataContext = new CustomImageViewModel();

from the constructor of your control.

It is not true that the "control doesn't have any view model" when you don't set it explicitly. In fact, the view model (or the inherited DataContext) is set - via the ItemsControl's item container - to the appropriate item from the ItemsSource collection. So the DataContext of your control is automatically set to an instance of your class A.
Comment: But I want a separate view model for the custom control which contains all async operation (and other bindable properties) I need. Is it possible?
Comment: "Is it possible?" Yes, but it doesn't make sense. You want to bind your control's properties like `BaseUri="{Binding Uri}"`, so a DataContext must be inherited.
Comment: Ohh I see. Thanks!


Attempt URL alias for spanish version of page - "The path */* is either invalid or you do not have access to it."

Question: Drupal 7.59. We have the page whatever.tld/thanks and whatever.tld/es/thanks.
I'm trying to setup whatver.tlp/gracias as a URL alias for es/thanks and it says "The path 'es/thanks' is either invalid or you do not have access to it."
The page itself is a tpl.php file referred to by a view and has individual blocks on it that are translated.
This translation stuff is beyond me. I have a dev site to practice on so I'm happy to try out any ideas. The following (apparently) applicable modules are enabled:

GTranslate
Block languages
Internationalization
Menu translation
Multilingual content
String translation
Translation sets
Comment: PHP Templates are there to dress up the output, but you're supposed to rely on Drupal engine to provide content for it. You should enable Languages you intend to use (you probably have) and translate your English Thanks page into Spanish. The Spanish node should contain the Spanish gracias content. You can use Pathauto to define special paths for languages if you need to. Blocks can also be language sensitive so when you translate block content through Drupal you don't have to place each language instance of a block separately by language...
Answer: As @prkos mentioned enable the languages module. In the Configuration/Regional and language/Languages settings you select the code you wish to use for your languages. The choice is obviously yours but when I have a multilingual site I prefer to use the ISO language code to help define the languages on the site.
Comment: I'm not sure if I'm not understanding part of your answer. I have Pathauto enabled but when I got to add a new alias from "es/thanks" and try to call it "gracias" (not a pattern -- and I'm not sure how those work) it gives me the above error.


Two definitions of vectors

Question: Abstractly, are vectors better defined as elements of a vector space or rank 1 tensors. I've heard of vectors in the context of tensors as generalizations of scalars, vectors, and matrices. However, since you could have a vector space of matrices, matrices can be considered vectors sometimes. These definitions don't seem to compile since one separates the ideas.
Comment: The word "vector" is overloaded and means different things in different contexts. Hopefully the context will make it clear which definition is being used. Whenever we are working with some vector space $V$, the elements of $V$ might be called "vectors" in that context.
Comment: To define a vector as a rank 1 tensor leads to the question...what is a tensor?
Comment: I think now looking back that while any tensor can be considered a vector, it is not necessarily and only a tensor of rank one is guaranteed to be a vector or element of a module more generally.
Comment: So I'd say a vector is an element of a vector space in the most abstract sense, as I asked
Comment: Strictly speaking, matrices are not rank 2 tensors. Rank 2 tensors can be *represented* by matrices.
Answer: Maybe type (1,0) tensor is better than rank 1 tensor for describing vectors. Co-vectors are also rank 1 tensors. By definition, a type (m,n) tensor is an element of $\underbrace{V\otimes\cdots\otimes V}_m\otimes \underbrace{V^\ast\otimes\cdots V^\ast}_n.$ Saying something is a type (1,0) tensor is equivalent to saying it is a member of a vector space. By tradition, a rank 0 tensor just represents a member of the field that the vector space $V$ is over. For clarity sake, it might be "better" to just say that vectors are members are vector spaces and tensors are a type of equivalence class over vector and dual spaces. $V\otimes W=F\!\left(V\times W\right)/\sim$ where $F$ is the free vector space and $\sim$ is the equivalence relation that defines the tensor product. When combined with scaling and addition rules, it has a basis of $\left[v,w\right]$ for $v\in V, w\in W$ usually denoted $v\otimes w.$

Two definitions of vectors


Converter String em Float

Question: Estou tentando converter string em float para calcular á vantagem entre gasolina e etanol, porém não está retornando resultado em Tela.
Abaixo o código HTML e JS:

var etanol, gasolina;

function Vantagem(){
 etanol = parseFloat(txtEtanol.value.replace(",","."));
 gasolina = parseFloat(txtGasolina.value.replace(",","."));

 if (etanol < 0.7 * gasolina) {
  document.getElementById('status').src="img/medidorEtanol.jpg";

 } else{
  document.getElementById('status').src="img/medidorGasolina.jpg";
 }
}

function Limpar(){
 document.getElementById('status').src="img/medidorNeutro.jpg";
}
<!DOCTYPE html>

  <meta charset = 'UTF-8'>

  <title> Calculadora de Combustível </title>

 Calculadora de Combustível

  <form name="frmFlex">

   Etanol: <input type="text" name="txtEtanol" placeholder="Etanol">

   Gasolina: <input type="text" name="txtGasolina" placeholder="Gasolina">

   <button onclick="Vantagem()"> Vantagem </button>

   <button onclick="Limpar()"> Limpar </button>

  </form>

Comment: Use o método `etanol = +txtEtanol.value.replace(",",".");
 gasolina = +txtGasolina.value.replace(",",".");

` em vez do parseFloat
Answer: Seu código tem alguns problemas:

na linha etanol = parseFloat(txtEtanol.value.replace(",","."));, onde foi definida a variável txtEtanol?
no seu input, não seria melhor definir "txtEtanol" como id no lugar de name?
por que declarar as variáveis globais var etanol, gasolina; se só está usando na function?
para comparar e garantir a operação com a gasolina, adicione um parênteses aqui: if (etanol < 0.7 * gasolina)

Para simplificar um poco o código, criei esse exemplo:

function Vantagem() {
    var etanol = parseFloat(document.getElementById('txtEtanol').value.replace(",", "."));
    var gasolina = parseFloat(document.getElementById('txtGasolina').value.replace(",", "."));

    if (etanol < (0.7 * gasolina)) {
        document.getElementById('status').src = "img/medidorEtanol.jpg";
        alert('medidorEtanol'); // apenas para testar

    } else {
        document.getElementById('status').src = "img/medidorGasolina.jpg";
        alert('medidorGasolina'); // apenas para testar
    }
}

function Limpar() {
    document.getElementById('status').src = "img/medidorNeutro.jpg";
}

 Calculadora de Combustível

   Etanol: <input type="text" id="txtEtanol" placeholder="Etanol">

   Gasolina: <input type="text" id="txtGasolina" placeholder="Gasolina">

   <button onclick="Vantagem()"> Vantagem </button>

   <button onclick="Limpar()"> Limpar </button>

Comment: interessante, nunca fiz dessa maneira, mas de qq forma acho o `parseFloat` mais claro, assim como `Number` :)
Comment: Se ele trocasse ParseFloat para `+` seria mesma coisa?
Comment: como o que colocou no comentário? `etanol = +txtEtanol.value.replace(",",".")`? desconheço essa sintaxe por isso não sei se funciona, mas não me parece claro, mas de qualquer forma vai dar erro por `txtEtanol` não está definido
Comment: Dar certo. Essa sintaxe transforma em número.
Comment: @MauryDeveloper Com o `+` seria a mesma coisa pois `+` seguido de uma `string` o `Javascript` tenta converter para numero como se tivesse feito `+ Number(texto)` que tanto funcionará quer o texto tenha um numero inteiro ou um `float`
Comment: Sim isso mesmo. Eu queria ajuda,mas ele não consegui entende.

Converter String em Float


How to get total weekend days counts grouped by month for a given month range in SQL Server?

Question: How to get total weekend(sunday) counts grouped by month for a given month range in SQL Server?
For example: dates selected : 'Oct-2020' to 'Dec-2020'

Month Name
Count

October
4

November
4

December
5
Comment: More detail. What are your tables, what query have you tried so far?
Comment: I would use a [Calendar table](https://www.mssqltips.com/sqlservertip/4054/creating-a-date-dimension-or-calendar-table-in-sql-server/) to get an answer to this type of queries. If you have a Calendar table in your database, then it becomes a trivial query.
Comment: @Charlieface, no table, i just need counts based on date range
Answer: You can use DATEDIFF() and DATENAME() functions along with a recursive CTE such as
DECLARE @date1 AS DATE, @date2 AS DATE;
SET @date1 = '2020-10-01';
SET @date2 = '2020-12-31';

WITH cte AS
(
  SELECT 0 AS n
  UNION ALL
  SELECT n + 1 AS value
    FROM cte
   WHERE cte.n < DATEDIFF(day,@date1,@date2)
)
SELECT DATENAME(MONTH,DATEADD(day, n, @date1 ) ) AS [Month Name], COUNT(*) AS [Count]
  FROM cte
 WHERE DATENAME(WEEKDAY, DATEADD(day, n, @date1 ))= 'Sunday'
 GROUP BY DATENAME(MONTH,DATEADD(day, n, @date1 ))

Demo
If the date interval covers more than one year, then use the following code which includes the ordering also.
DECLARE @date1 AS DATE, @date2 AS DATE;
SET @date1 = '2020-10-01';
SET @date2 = '2021-03-31';

WITH cte AS
(
  SELECT 0 AS n
  UNION ALL
  SELECT n + 1 AS value
    FROM cte
   WHERE cte.n < DATEDIFF(day,@date1,@date2)
)
SELECT DATEPART(yy, DATEADD(day, n, @date1)) AS [Year],
       DATENAME(MONTH,DATEADD(day, n, @date1)) AS [Month Name],
       COUNT(*) AS [Count]
  FROM cte
 WHERE DATENAME(WEEKDAY, DATEADD(day, n, @date1))= 'Sunday'
 GROUP BY DATENAME(MONTH,DATEADD(day, n, @date1)),
          DATEPART(yy, DATEADD(day, n, @date1)),
          DATEPART(m, DATEADD(day, n, @date1))
 ORDER BY [Year], DATEPART(m, DATEADD(day, n, @date1))
 OPTION (MAXRECURSION 0)

Demo
Comment: Recursive cte is really slow for this kind of thing. Use a tally or calendar table, or Itzik Ben-Gan's tally function instead