Loop through column names in multiple datasets to fix name discrepancy

Question: I have multiple data sets that are the same column structure but different row lengths. When I go to stack them, there is an occasional problem with rbind as one file may have the column names uppercase whereas another may have lowercase. I would like to run something at the beginning that converts everything to lowercase (as this is not case sensitive).
When I do the following one-off, the conversion works. However, I am messing something up trying to do this through a loop.
This works
names(fmli3) <- tolower(names(fmli3))

Does not work
df_qtr <- c(fmli1, fmli2, fmli3, fmli4, fmli5)

for (i in 1:length(df_qtr)){
  names(df_qtr[[i]]) <- tolower(names(df_qtr[[i]]))
}

Thanks in advance for the help.
EDIT:
Thanks to JWilliman answer in the cross post (and Shea for pointing out), the following works:
dfs <- c("fmli1", "fmli2", "fmli3", "fmli4", "fmli5")

for(df in dfs) {
  df.tmp <- get(df)
  names(df.tmp) <- tolower(names(df.tmp))
  assign(df, df.tmp)
}

Comment: Possible duplicate of [Rename columns in multiple dataframes, R](http://stackoverflow.com/questions/18375969/rename-columns-in-multiple-dataframes-r)
Answer: The problem with original code is that df_qtr is not a list of 5 data.frame. You can check length(df_qtr) to verify.
Use the following syntax to connect multiple data.frame into a list.
 df_qtr <- list( fmli1, fmli2, ...)


How to execute commands requiring root privileges inside a script?

Question: I have to change the frequency of CPU (through slide privileges) . I have written the command to change frequencies inside a script .However , when I run the script, I get the following error message
 #!/bin/bash
slide
        for i in 0 1 2 3
        do
                echo 1600000 > /sys/devices/system/cpu/cpu${i}/cpufreq/scaling_setspeed
        done

when I run the script , it gives following error
./change_freq.sh: line 4: /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed: Permission denied

Any suggestions ?
Comment: Where to learn more about slide privileges?
Comment: slide is a command that gives partial root access . And it does not require password authentication , if the system administrator has given you slide privilege.. For the current problem, you can substitute, slide with sudo and consider the problem
Comment: Can you give a URL where information about the 'slide' program can be found?  Searching for 'slide' is rather hard - you get Powerpoint instead.
Comment: Even I cannot find information about that. But that command works on our lab machines. I don't know about it's mechanism and how it functions . But when I "slide",  I get partial root access. That is what my system admin told me.
Answer: I don't know how slide works, but a possibly similar action using sudo that produces the same kind of error would be:
sudo echo foo > /path/to/file

The way to solve that is:
echo foo | sudo tee /path/to/file > /dev/null

I would be curious to know if any of the following provide further information on the origin of slide:
type -a slide
man slide
slide -v
slide --version
slide -h
slide -?

Comment: I will ask my system admin about this and let you know further info. I tried all the above commands , but I got this "/bin/bash: -c: option requires an argument"
Comment: @SharatChandra: Where did the `-c` come from? None of the commands in my answer include that. What does `file $(type -P slide)` give you?


Prove that $f(x) = \frac{1}{2}$ everywhere.

Question: Exercise:

Let $f(x)$ be continuous for $0 \leq x \leq 1$. Suppose further that $f(x)$ assumes rational values only and that $f(x) = \frac{1}{2}$ when $x = \frac{1}{2}$. Prove that $f(x) = \frac{1}{2}$ everywhere.

Attempt:
Suppose $f(\frac{1}{2} + \delta_1) = p \neq \frac{1}{2}$ is rational.
There is a irrational number $q$ that is between $\frac{1}{2}$ and $p$.
For $f$ to be continuous, there must be a $f(\frac{1}{2} + \delta_2) = q$ where $\delta_2<\delta_1$.
But, by $f$'s definition, it contains no irrational numbers.
So, the only other option is that $f(\frac{1}{2} + \delta_1) = \frac{1}{2}$.

Request:
Is my proof correct? If not, where and why'd I go wrong?
Comment: @Canardini -- No idea what that means (but I bet you're right ). I'm a beginner in Analysis.
Comment: You may want to spell out the name of the theorem you use (the Intermediate Value Theorem)
Comment: @HagenvonEitzen -- Regarding my second statement?
Comment: The shape of the proof looks good. How do you know that there is an irrational number between $\frac 12$ and $p$? For the proof to be tight you should justify each of the intermediate statements either by reference to previously established results, or by showing why they are true as part of the proof.
Comment: @MarkBennet -- Alright, thanks. I presume that's the same thing HagenvonEitzen pointed out, correct?
Comment: You should add that $\mathbb{R}$ is Archimedean to justify the existence of an irrational in your interval,  and that you used the intermediate value theorem
Answer: Why do you say that $\delta_1<\delta_2$.
here is an other approach.
$f$ is continuous at $[0,1]$ thus $f([0,1])$ is an interval.
but $f([0,1]) \subset \Bbb Q$ so the only possibility is $f([0,1])=\{\frac{1}{2}\}$.


Java Android Arraylist string to ArrayList custom class

Question: How can I convert values here:
List<String> values = new ArrayList<String>

to :
ArrayList<Custom>

EDIT:
public class Custom {

    public Custom Parse(String input) {
        // What should I do here?
    }
}

Comment: How do you convert from String to Custom?
Comment: refresh  your question, and put clear question
Comment: Can you tell me if I can do some parsing in my Custom class?
Comment: what about your custom class and your string data ?
Comment: custom class for parsing ...[see here ...](http://samir-mangroliya.blogspot.in/p/android-customized-listview.html)
Comment: What is the real content of your `Custom` class, we cannot know for you what is the String representation of a `Custom`, you're the only one who knows that... Your edit is insufficient.
Answer: You could use:
List<Custom> customList = new ArrayList<Custom>();
for (String value: values) {
   customList.add(new Custom(value));
}

Although it would be better just to add a constructor with a String argument:
class Custom {
   private final String input;

   public Custom(String input) {
      this.input = input;
   }

   // not needed but implemented for completeness
   public static Custom parse(String input) {
      return new Custom(input);
   }
}

Answer: Assuming your list of Custom objects has the same size with values list.
With one enhanced for-loop, set the appropriate fields of your objects like this:
int i=0;

for(String str:values)
   customList.get(i++).setSomeProperty(str);

Answer: you can find a solution using Google Collections libraries on this thread Converting a List<String> to a List<Integer> (or any class that extends Number)


Optimal Placement of Points inside a Set

Question: Say I have to place N points in $\mathbf{R}^2$ inside a circle of radius $R$.
I want to position them so as to maximize the sum of nearest distances i.e. solve the following problem
\begin{align}
\max_{x_1, ... x_n} &\sum_{i = 1}^{n} \min_{j \neq i} |x_i - x_j|^s \\
\text{s.t. } |x_i| &< R
\end{align}
 where $s$ is a constant larger than $2$
My aim is to characterize the behavior of this optimal value as the number of points $n$ becomes large. So even asymptotic order analysis will be helpful.
If it makes things easier, consider other constraint sets (eg. A square of side length $A$)
Comment: Related: http://www.math.ucsd.edu/~ronspubs/98_01_circles.pdf
Comment: If the question becomes minimize sum of distance inverse (repulsive coulomb potential) in $R^3$, it becomes how chemists predict the shape of molecules (VSEPR model). I've never seen rigorous math derivation of the cases of large $n$ in similar problems. I'm curious about this too.
Answer: Not an exact solution, but an approximation (that may or not not be good enough):
Assumption 1: Solving the problem for the infinite plane provides a solution that is a reasonable approximation for the circle problem.
Assumption 2: The solution is somewhat uniform; for all points $x$, the distance to its nearest neighbor is $\delta$.
This corresponds to the problem of packing $n$ circles with radius $\frac{1}{2}\delta$ and center $x_i$. The densest solution is placing the $x_i$ on the vertices of a triangular tiling with equilateral triangles.
One way to construct this tiling inside a circle is to chose $\delta$ such that $\delta$ divides $r$ and to place the first vertex $x_0$ in the center of the circle. Add two more vertices to create the first triangle (with arbitrary orientation), then add the remaining triangles and discard all vertices that are not on/in the circle. This creates a neat, symmetric result, but has the disadvantage that $n$ results from $r$ and $\delta$, and cannot be given.
This solution is not optimal (look at the vertices closest to the circle perimeter), but for large $n$ it should approach the optimum.
Answer: You can use Lloyd's relaxation:

Place the points randomly inside the circle.
Iterate until convergence:
a) Compute the clipped Voronoi diagram of the points and the circle
b) Move each point to the center of mass of its Voronoi cell

Comments:
In 2a), the clipped Voronoi diagram is the intersection of the circle and the "ordinary" Voronoi diagram of the points. However, the available methods that compute it, usually assume $L_2$ norm (you mentioned $s > 2$, so be careful).
In 2b), compute the center of mass with respect to the distance you are using (whatever $s$ you have).
This approach has the advantage that you only specify the number of points $n$. In the related circle packing and Poisson disk sampling problems, you provide the circle radius or the desired minimal inter-point distance.
Reference:
Here is a link to a paper for computing the clipped Voronoi diagram (there are planty more on Google).


WPF Data Grid Example

Question: I've been experimenting with WPF and just downloaded the toolkit which includes a DataGrid control. Does anyone know where I can find a fully worked through example of binding a database table to the WPF DataGrid?
Answer: Here is a blog that has some articles on DataGrid binding.

WPF Data Grid Example


Meteor.user() returns only its _id

Question: After logging a user in with Meteor.loginWithPassword() or creating a new one with Accounts.createUser (both client-side), I can confirm in their callbacks that Meteor.user() indeed contains all the set record's properties.
{ _id: "XXX",
  profile: {
     name: "Joe Shmoe",
     thumbnail: "http://www.YYY.com/ZZZ.jpg"
  },
  username: "joeshmoe" }

Furthermore, according to the official docs,

By default, the current user's username, emails and profile are published to the client.

So, would anyone be able to tell why when I try to access these fields in my Templates thusly
Template.login.user_name = function () {
    return (Meteor.userId() ? Meteor.user().profile.name : '')
};

it fails due to Meteor.user() only returning {_id: "XXX"} with none of its actual properties? I.e. the user is definitely logged in, but the user object suddenly lost/is hiding all of its properties.
Anyone know what the problem might be?
Many thanks.
EDIT: this happens with Meteor 0.5.4, the latest version at this time of writing. The accepted answer indeed fixes the issue; sometimes Meteor.userId() is already valid before the rest of the Object has arrived from the server. Thanks everyone.
Comment: I even tried to publish them on the server and autosubscribe them on the client, but no change. I shouldn't need to do that though.

`// Server
Meteor.publish("userData", function() {
    return Meteor.users.find({_id: this.userId}, {fields: {profile: 1, username: 1}});
});
...
// Client
Meteor.autosubscribe(function () {
    Meteor.subscribe("userData");
});`
Comment: Then I put the the whole call into a getter method into Meteor.methods, suspecting the Templates context to disallow certain DB accessors for security, but it's exactly the same problem.

`// Client
Meteor.methods({
userName: function() {
    return Meteor.user().profile.name;
});
...
Template.login.user_name = function () {
    return (Meteor.userId() ? Meteor.call("userName") : '')
};`
Answer: It's possible that the data has not yet arrived from the server. Instead of just checking for Meteor.userId, what happens if you check for the property?
Template.login.user_name = function() {
  return Meteor.userId() && Meteor.user() && Meteor.user().profile ? Meteor.user().profile.name : "";
}

Comment: Indeed, that seems to have been the problem. I kinda suspected it was something benign, I just didn't make the cognitive leap to explicitly check the rest of the object as well, since I trusted `Meteor.userId()` to either be null or valid along with the rest of the user data. Thanks for that.
Answer: This has happened to me, too, using loginWithFacebook.  I use this function, which has worked without problems so far:
var reallyLoggedIn = function() {
  var user = Meteor.user();
  if (!user) return false;
  else if (!user.profile) return false;
  else return true;
};

Answer: I am not able to reproduce this problem, but if you have the UserID, you can get all the information from the full database, Meteor.users (even though it SHOULD be doing this already).
Template.login.user_name = function() {
    return (Meteor.userId() ? Meteor.users.findOne({_id:Meteor.userId()}).profile.name : '')
}

Comment: Yes, I had tried that too but this call was also returning undefined, as if the user had been there on creation and then simply disappeared. This could be explained by the fact that the client simulates the correct response (one of the many things I adore about Meteor), then the server rectifies it immediately, not having written the record yet, and is only valid again on subsequent, insistent tries.

Meteor.user() returns only its _id


Why does value not change inside thread?

Question: When i assign a new value to variable it doesn't change after start(), however after i use join() it does. Why does this happen and in this case should int a be volatile or not?
class SampleThread extends Thread {

    private static int a = 0;

    @Override
    public void run() {
        a = 3;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t2 = new Thread(new SampleThread());
        t2.start();
        System.out.println(a);
    }
}

Comment: Why would you expect the new thread to immediately get control before the main thread continues and executes `println`?  There's no guarantee on the order in which the threads get dispatched.
Comment: It does, but main thread continues with `System.out.println(a);` before second thread will change `a` (so change will happen *after* printing). To increase chances of second thread updating `a` before main thread will print it make that main thread wait for a bit before calling `println`. You can do it by invoking something like `Thread.sleep(1000);` before `System.out.println(a);`.
Comment: If you explicitly `.join()` on `t2`, you don't need volatile - as joining a thread establishes a happens-before edge.
Comment: If 2 threads should be executed then it could be that only 1 thread is executed (main thread) before the JVM quits; To prevent this we could use join() so the JVM waits until 2nd thread is finished before terminating. Am i right?
Comment: If even one non-daemon thread is still active then JVM will not exit. Since your `t2` thread was not set to be a daemon then your application will exit only when *main* thread will finish and when `t2` will finish. `.join` is used when you want to force thread which calls that method to wait till other thread will finish its work. In your case if you call `t2.join()` inside `main` then main thread will wait till `t2` will finish its work and only after that will continue (but in that scenario it defeats the purpose of even starting separate thread).
Answer: for seeing what is going on, try this:
    ...
    @Override
    public void run() {
        System.out.println("start of Thread");
        a = 3;
        System.out.println("end of Thread");
    }
    ...

only run method changed, rest of code unchanged
Answer: Yes, it needs volatile.
Every thread has an evil coin. The thread flips the coin anytime it reads or writes a field: Heads, and the thread uses its own local (to the thread) copy of it; if writing, that update simply does not reflect to all the other threads, who will still see the 'old' value, and if reading, same deal: Reads whatever it had, even though other threads updated it already. Even if they did so an hour ago. Tails, and it does refresh other threads' views of this thing, and won't use the local copy.
The coin is evil: it is not a fair coin. It will work every time today, and every time tomorrow, and every time during the test suite, and all throughout that week you have it live for the early adopting customers. And then juuust as that big client comes in and you're giving the demo? It flips to break your app every time. That kind of evil.
So, you must eliminate all coin flips, or at least ensure that the result of a coin flip does not affect your app whatsoever.
The way to do this, is to establish comes-before relationships. Between any 2 lines of java code as executed by a VM, there is a set of rules to determine if these 2 lines have such a relationship: That one is guaranteed to be run after the other. It's not about whether they did (timestamps of when they ran is completely irrelevant), it's whether the Java Memory Model decrees that such a relationship exists.
If yes, no coin is flipped: Anything the 'line that came before as per the JMM' did, is definitely visible to the line that came after. But if the JMM does not explicitly spell out that this relationship exists, the coin is flipped, and you lose.
One trivial 'comes before' relation ship is within a single thread: x = 5; System.out.println(x); trivially has such a relationship; they ran in the same thread, one came after the other. That's the freebie.
But between threads, oh dear. You need synchronized, volatile, or call code that does these things internally or has other mechanisms to ensure it (tip: There is lots of great stuff in the java.util.concurrent package, and as the name suggests, it's generally thread safe in very efficient ways. For example, an AtomicInteger is almost always far better than a volatile int, and can do far more, such as CAS operations, which volatile ints cannot do.
Comment: no, the VM does not quit until all threads* are done. *) if you set the 'daemon' flag on a thread, it won't count. There is no need to join here. Note that join is one way to establish comes-before. all statements thread X executes come before the resolution of `X.join()`. In that way, after joining, the variable is guaranteed updated. You need to read the JMM and the full list of CBCA rules, or forego communicating between threads via fields.
Comment: If 2 threads should be executed then it could be that only 1 thread is executed (main thread) before the JVM quits; To prevent this we could use join() so the JVM waits until 2nd thread is finished before terminating. Am i right?
Answer: If you expect things in different threads to happen in a particular order -- in this case, that a = 2 is executed before `System.out.println(a)' -- then you have to write code to make that order happen.
In this trivial case, where no real work is being done, almost anything you can do makes the use of threads pointless.  The main thread could 'join' the thread that's setting a to 2, but then all you've achieved is an expensive way to execute code that could be executed in a single thread.
Comment: But this is obviously not for real applications, but for training purposes. So what you said really doesn't matter.
Answer: main thread should wait that the other was executed
One solution is to use join()
class SampleThread extends Thread {

    private static int a = 0;

    @Override
    public void run() {
        a = 3;
    }

    public static void main(String[] args) throws InterruptedException {
        Thread t2 = new Thread(new SampleThread());
        t2.start();

        // wait for completion of t2
        t2.join()

        System.out.println(a);
    }
}

Comment: But if value is not assigned inside t2 then why does the code in t2 immediatly fire? And does the JVM quit when a deamon thread is active?


create a new and unique file each time one function is compiled in java

Question: create a new and unique file each time the function is compiled.
that means if file exists already before execution of the program
then
it should delete the file and create a new file and append the new data.
or
add the content after just deleting the complete data which is already there in the same file.
I have created something like this....
Writer output = new BufferedWriter(new FileWriter("C:\\" + bankAccNo + ".txt", true));
output.append(myRow.getCell(0).toString()+ "\n");
output.close();

Comment: every time a program is executed i want to create a new file with same name.
Comment: It is appending into the file each time these lines of code are being called.

but what I want is each time when the PROGRAM executes It creates a new file with same name.
Comment: Just do `file.delete()` on startup? I don't see how that would be complicated.
Comment: Thats because you are calling the append. Simply write to the stream directly, without appending.
Comment: What has your problem to do with "unique file each time the function is compiled"?  Please fix your your title and problem description. If you use the FileWrite Constructor `append=true` "then bytes will be written to the end of the file rather than the beginning"
Comment: @Vikas: because I want to append the data till program completely executes...
but before starting execution of these lines I want to delete the file which contains same name as the file name in the same directory....
Comment: @Abhinav as long as our output stream is open the data will be appended to the file / output. 'append=true' in the FileWriter constructor controlls whether the next bytes written to the output are written to the beginning of the file or appended at the end of the file.
Comment: I need to append the data each time i am writing using these lines of code but before start writing/creating this file, I want to check if this file already exists, then create a new file with some other name......
Answer: If you want to overwrite the file contents every time you write to it, use false for the append switch.
new FileWriter("<file>", false);

Comment: U r right but i dont want that...

I need to append the data each time i am writing
but before start writing to this file i want to check if this file already exists then then create a new file with some other name......
Comment: @Abhinav if you want to overwrite your file each time you start your application / open the File `FileWriter("", false);` is the correct choice ortherwise fix your problem description. You may also have a look at [File API](http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html)


Grails Web App - Show current logged in users

Question: ive created a grails app and used Apache Shire plugin http://shiro.apache.org/ for security and logging in purposes.
I wanted to find out if anyone knows how I can get a list of the users who are currently logged in to the web application?
Where would this information be stored / on the servers side in a cookie or a session?
Basically i want a widget showing all the current users whom are logged in to my web application currently.
Answer: You would probably have to make a table in your app with currently logged in users (or alternatively a shared hashmap)
When your users log in you write the name to the table / hashmap. The tricky part is to figure out, when the user logs out. If the user logs out by clicking "logout" - no problem. If the users session expires you have to listen to the events fired by Shiro.
According to the Shiro website there are several events you can hook into.


Category-theoretic cross product and set-theoretic cross product

Question: I recently proved as an exercise the associativity of cross product as defined in category theory. But in set theory, cross product is not associative. It seems intuitive to me that cross should be associative, but it also seems that the set theoretic definition doesn't cause set theorists any problems. Are there examples in which the associativity of cross does matter, so that the category theoretic def. is preferable for more than just intuitive reasons?
Comment: The categorical product is associative only up to canonical isomorphism. But so is the set-theoretic cartesian product. This causes no problems in practice.
Comment: What do you mean by "cross product?" When you say category-theoretic cross product, I would guess you mean direct product (also sometimes known just as product). In this case, the set-theoretic product is the product in the category of sets, so it's certainly associative in the obvious sense.
Answer: The Cartesian product of sets is what is usually referred to as the product of two sets, and it is denoted with $\times$. It's certainly associative. The intuition is that when you form $(X\times Y)\times Z$ and $X\times (Y\times Z)$, they are both basically the same as the set of ordered triples $X\times Y\times Z$. While the first two are not equal as sets, they are "rearrangable" with a canonical isomorphism of sets so that they look alike, so there is not much reason to consider them different.

No longer believe the user has this problem:
You might possibly be confusing it with the 3-d vector algebra cross product (denoted by the same $\times$), which is not associative. This is a product between vectors (usually from $\Bbb R^3$), not sets.
Comment: What OP seems to hint at is that while there is a canonical isomorphism, there is _not_, strictly speaking, an identity.
Comment: Dear @user1093043, The sense in which the categorical product is associative, which you seem to accept, is "up to canonical isomorphism," not literal equality of objects, and this is the same sense in which the cartesian product of sets is associative. So why do you consider the cartesian products of sets not to be associative?
Comment: @Lord_Farin That could be... I'll try to add a bit more to try to promote the idea we should still think of it as being associative.
Comment: Excellent now, +1.
Comment: @Lord_Farin Thanks for helping me make a better solution :)
Comment: @rschwieb Thanks for the answer; the intuition you mention matches my own. It was that intuition that led me to think that the def. of product in set theory is /wrong/: by not characterizing product only up to canonical isomorphism, it has lost associativity -- a property product has. So I'm after some example where the failure of associativity of set theoretic product is used in a way that seems acceptable.


Why cannot I not access a worksheet by name?

Question: I'm using Excel 2013. I can access a worksheet by iterating through the worksheets and checking the Name property, but I cannot find it individually by using the worksheet name as a key.
Do I really have to iterate through all the worksheets and then all the pivot tables to find the one I need to refresh?
This works:
Dim oWorksheet As Worksheet
Dim oPivot As PivotTable

For Each oWorksheet In ActiveWorkbook.Worksheets
    If oWorksheet.Name = "FPSpivot" Then
        For Each oPivot In oWorksheet.PivotTables
            oPivot.PivotCache.Refresh
        Next oPivot
    End If
Next oWorksheet

This doesn't work:
Dim oWorksheet As Worksheet
oWorksheet = ActiveWorkbook.Worksheets("FPSpivot")

I get the old chestnut:

Object variable or With block variable not set.

Why?
Ideally, I just want the single line:
ActiveWorkbook.Worksheets("FPSpivot").PivotTables("FPSpivot").PivotCache.Refresh

Surely this must be possible?
Comment: Are you sure you mean `ActiveWorkbook` (the workbook which has focus / is on top) or did you mean `ThisWorkbook` (the workbook the code is written in)? Makes a huge difference and in most cases you need `ThisWorkbook`. • Also note that you need to use `Set` for objects like putting a worksheet into a variable use `Set oWorksheet = ActiveWorkbook.Worksheets("FPSpivot")`
Comment: Using `activeworkbook`, `activesheet` and `.select` [is generally considered bad practise.](https://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba) As it invites errors like the one @Pᴇʜ pointed out.
Comment: I'm still unclear as to why ```ActiveWorkbook.Worksheets("FPSpivot").PivotTables("FPSpivot").PivotCache.Refresh``` doesn't work. It complains with "Unable to get the PivotTables property of the Worksheet class".
Comment: @MarkRoworth That means your pivot table is not named "FPSpivot".
Answer: The error

Object variable or With block variable not set.

occurs because if you work with objects you need to use Set.
Dim oWorksheet As Worksheet
Set oWorksheet = ActiveWorkbook.Worksheets("FPSpivot")

Additionally:
Are you sure you mean ActiveWorkbook (the workbook which has focus / is on top) or did you mean ThisWorkbook (the workbook the code is written in)? Makes a huge difference and in most cases you need ThisWorkbook.
Comment: Ah, thank you. Firstly, yes, I mean ThisWorkbook. And secondly, I use .Net so much I forgot you need a Set in VBA. Strange how sometimes you don't see the wood for the tree. Thank you for your help. Spot on.


Interpreting matrices of SVD in practical applications

Question: I have a question regarding the interpretation of the different matrices produced by singular value decomposition.
Suppose a mxn matrix $A$ containing n images of m pixels. So each column of this matrix when reshaped is an image. The images are actually wavelets transformations but that bears no relevance to the question. Furthermore, the first k columns of $A$ are images of dogs and the remaining columns are images of cats. So I have something like this:
wavelet_dc = np.hstack((wavelet_dogs, wavelet_cats))
U, S, V = svd(wavelet_dc, full_matrices=False)

What is the interpretation for U, S and V? I know we can build the original image based on a sum such as $A = \sum_{i} u_{i}s_{i}v^{T}_{i}$ in which $u_{i}$ and $v_{i}$ are column vectors of U and V, respectively. I also know that $S$ determines the most important modes of the original matrix and that the columns of U are the features associated to those modes. However, what role does V play in this matrix? I'd like to know how to think about V in practical applications (that is, more than as a rotation in the geometric interpretation of SVD)
Furthermore, suppose I plot V in the following way:
plot(V[0:40, 0], 'o-', markersize=3)

Since V is a nxn matrix, I don't know how to interpret it, but I think this code is actually taking the first 40 rows and the first column of V. As I understand V as:
$$V = \begin{pmatrix}v_{1}^{T} \\v_{2}^{T} \\\vdots \\v_{n}^{T}\end{pmatrix}$$
I think the plot is referring to the first component of the 40 vectors $v_{1} ... v_{40}$ but what does it mean? In the Coursera class "Computational Methods for Data Analysis", this is described as the dogs for the first mode and V[0:40, 1] would be the dogs for the second mode, and so on. You can see the notes here, figure 159, page 388. This is assuming of course, that at least the first 40 columns of $A$ contained images of dogs. In the class, they also plot V[80:120, 0] because their matrix $A$ actually has the first 80 columns representing images of dogs and the last 80 columns representing images of cats, so V[80:120, 0] is plotting the first 40 images of cats.
UPDATE:
According to the video lectures, W8_L21_P3-Features: https://class.coursera.org/compmethods-002/lecture/76, they seem to interpret V[0:40, x] as the projection of the first 40 dogs onto the modes. Well, V has dimensions nxn which is the number of images, however, I wouldn't interpret it as "dogs". In any case, I think such projections shouldn't be V[0:40, 0] but V[0, 0:40] because these are the first 40 components of the vector $v_{1}^{T}$ that multiplies the first mode $s_{0}$. However, please let me know if I made a mistake.
Comment: If you can get your hands on the book/PDF `Understanding Complex Datasets:
Data Mining with Matrix Decompositions`, I would highly recommend that for interpreting SVD + other methods. I read it too long ago to recall the content though.
Comment: That book looks amazing. Thank you very much. I just glanced some relevant sections to this question, but I can't determine which interpretation fits the particular use given in the Coursera lecture. I will read more thoroughly tomorrow, though.
Answer: I think I have an answer. If you notice a mistake, let me know.
One interpretation explained in the book suggested by Cam.Davidson.Pilon is the factor interpretation. This tells you that a matrix $A$ with n rows and m columns  can be decomposed into a matrix $C$ with $n$ rows and $r$ columns, a matrix $W$ with r rows and r columns and a matrix $F$ with $r$ rows and $m$ columns. So suppose that your matrix $A$ contains $n$ objects and m attributes. As a consequence of this decomposition, you can think of $C$ as a different view of the $n$ objects, except that instead of using $m$ pieces of information, you use r pieces of information (usually, $r < n$). Similarly, $F$ can be thought as a different view of the attributes of $A$, again using $r$ pieces instead of $n$.
This description applies to general decomposition. The difference in SVD is that the matrices $W$ and $F$ have $r=n$, but the same interpretation is possible.
Therefore, in the example I described in the question, the matrix $A$ looks like this:
$$
  A = \begin{pmatrix}
  a_{1,1} & a_{1,2} & \cdots & a_{1,n} \\\
  a_{2,1} & a_{2,2} & \cdots & a_{2,n} \\\
  \vdots  & \vdots  & \ddots & \vdots  \\\
  a_{m,1} & a_{m,2} & \cdots & a_{m,n}  \end{pmatrix}
$$
in which the rows are attributes (in this case, pixels) and the columns are objects (dogs and cats). An SVD decomposition of this matrix would like this:
$$ A = U\Sigma V^{*} $$
$$
A = \begin{pmatrix}
  u_{1,1} & u_{1,2} & \cdots & u_{1,n} \\\
  u_{2,1} & u_{2,2} & \cdots & u_{2,n} \\\
  \vdots  & \vdots  & \ddots & \vdots  \\\
  u_{m,1} & u_{m,2} & \cdots & u_{m,n}
  \end{pmatrix}
\begin{pmatrix} \sigma_{1} & 0 & \cdots & 0 \\\
  0 & \sigma_{2} & \cdots & 0 \\  \vdots  & \vdots  & \ddots & \vdots  \\\
  0 & 0 & \cdots & \sigma_{n}  \end{pmatrix}
\begin{pmatrix}
  v_{1,1} & v_{1,2} & \cdots & v_{1,n} \\\
  v_{2,1} & v_{2,2} & \cdots & v_{2,n} \\\
  \vdots  & \vdots  & \ddots & \vdots  \\\
  v_{n,1} & v_{n,2} & \cdots & v_{n,n}  \end{pmatrix}
$$
Following this factor interpretation, every row of $V^{*}$ corresponds to a factor, the first columns correspond to dogs and the last ones to cats. So, let's take the first 40 columns of $V^{*}$ of the first factor. This should be:
fig = figure()
ax = fig.add_subplot(1,1,1)
ax.plot(V[0, 0:40], 'o-', markersize=3)
ax.set_xlabel('Dogs')
ax.set_title('Mode 1')

This plot describes the value of the first factor for the first 40 dogs. Since these values $v_{1,1}, ... v_{1,n}$ are going to be multiplied by $\sigma_{1}$, they are associated to the first mode:
$$\Sigma V^{*} =
\begin{pmatrix}
  \sigma_{1} v_{1,1} & \sigma_{1}v_{1,2} & \cdots & \sigma_{n} v_{1,n} \\\
  \sigma_{2} v_{2,1} & \sigma_{2}v_{2,2} & \cdots & \sigma_{2}v_{2,n} \\\
  \vdots  & \vdots  & \ddots & \vdots  \\\
  \sigma_{n}v_{n,1} & \sigma_{n}v_{n,2} & \cdots & \sigma_{n}v_{n,n}
\end{pmatrix}$$
Likewise, V[0, 40:80] produces the first factor for the first 40 cats.
Don't read too much into the values because I used a low quality dataset just to reproduce this part of the course.


Prove by induction on n that for any k ≥ n + 1 the sets Zn and Zk are disjoint.

Question: Suppose that $f : X \to X$ is an injective function.
Define by recursion a sequence $Z_0$, $Z_1$, $Z_2$, $\ldots$ of subsets of $X$ as follows.
$$Z_0 =X- ran(f),$$
$$Z_{n+1} = f(Z_n)$$ for any $n\geq 0$.
Prove by induction on $n$ that for any $k\geq n + 1$ the sets $Z_n$ and $Z_k$ are disjoint.
I am lost. Could anyone give me a hint, please? Thanks
Comment: *Base step* : $n=0$. Prove that for any $k \ge 1$ : $Z_0$ and $Z_k$ are disjoint.
Answer: Let us see that $Z_0\cap Z_k = \varnothing$, whenever $k>0$:
if $x \in Z_0\cap Z_k$ then $x \notin f(X)$ and $x = f(y)$, for some $y \in Z_{k-1}$, a contradiction.
Suppose now (induction hypothesis) that $Z_n\cap Z_k=\varnothing$ for $k>n$ is true whenever $n < n_0$.
Let $k >n_0$ and suppose that $x \in Z_{n_0}\cap Z_k$. Then
$$x=f(y), \;\text{with}\; y \in Z_{n_0-1}\quad\text{and} x=f(z),\;\text{with}\;z \in Z_{k-1}.$$
Since $f$ is injective, we get $y=z$, and so $Z_{n_0-1}\cap Z_{k-1}\neq\varnothing$, a contradiction with the induction hypothesis because $k-1>n_0-1$.


Get the value of nodes inside a node

Question: I've been trying to find a clear solution for my problem since every explanation I've found until now its confusing (for me at least).
I've tried DOM readers but I can't complement it with this file since its a more complex XML file.
My XML file:
<Order>
  <OrderID>
    <OrderConfirmationDate>
      <Date>
       <Year>2000</Year>
       <Month>10</Month>
       <Day>06</Day>
      </Date>
    </OrderConfirmationDate>
  <Adress>
    <Name>Papel do Porto</Name>
      <Address1>Rua da Alegria</Address1>
      <Address2>nº1</Address2>
      <City>Porto</City>
      <PostalCode>4000-032</PostalCode>
      <Country ISOCountryCode="PT">Portugal</Country>
    </NameAddress>

This is the code I've understood until now
dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(xmlFile);
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

So, I can easily get the root of document with getDocumentElement() . And I can easily get a node of the root with getNodeName().
But how do I get the "node.getNodeName(). (in this case I mean to get the <OrderConfirmationDate> out of <OrderID>)
Also, I've read that tags like <Year> and <Month> are Ids of the node <Date>.
Answer: To select a particular node, you can use a XPath expression.
XPath xpath = XPathFactory.newDefaultInstance().newXPath();
XPathExpression expr = xpath.compile("//OrderConfirmationDate/Date");
Element orderConfirmationDateElement = (Element) expr.evaluate(doc, XPathConstants.NODE);

Then to go through the node's children:
NodeList dateChildren = orderConfirmationDateElement.getChildNodes();
for(int i = 0; i < dateChildren.getLength(); i++) {
    if (dateChildren.item(i).getNodeType() == Node.ELEMENT_NODE) {
        System.out.println(dateChildren.item(i).getNodeName() + ": " + dateChildren.item(i).getTextContent());
    }
}

Output:
Year: 2000
Month: 10
Day: 06

Get the value of nodes inside a node


What am I doing wrong in this code?

Question: I am trying to develop a module to insert a lot of node at one time and I would like to use Batch-API to display the progress of operations.
I read the example into 'Example' module and I wrote this code.
But don't made anything. I can see the progress bar go head but it don't save any node.
Can anyone help me on this?
function custom_content_archive_import_contents_submit($form, &$form_state) {
    $file=$form_state['values']['file'];
    unset($form_state['values']['file']);
    $file->status = FILE_STATUS_PERMANENT;
    file_save($file);
    drupal_set_message(t('The file @filename was uploaded successfully.', array('@filename' => $file->filename)));

    ini_set('auto_detect_line_endings',TRUE);
    $path = drupal_realpath($file->uri);
    $importer = new CsvImporter($path, true, variable_get('custom_content_archive_file_delimiter'), variable_get('custom_content_archive_file_enclosure'));
    $data = $importer->get();
    ini_set('auto_detect_line_endings',FALSE);

    $_SESSION['http_request_count'] = 0; // reset counter for debug information.
    $all_data = array(
        'ctype' => $form_state['values']['list'],
        'data' => $data,
        'user' => $form_state['values']['user'],
        'lang' => $form_state['values']['languages'],
    );
    batch_set(start_batch_creation_nodes($all_data));
}

function start_batch_creation_nodes($all_data) {
    $num_operations = count($all_data['data']);

    $operations = array();
    $i = 0;
    // leggo il file uploadato e creo i nodi
    foreach ($all_data['data'] as $node) {
         $operations[] = array('create_node',
                          array($node,
                                $all_data['ctype'],
                                $all_data['user'],
                                $all_data['lang'],
                                t('(Operation @operation)', array('@operation' => $i))
                          )
         );
         $i++;
    }

    $batch = array(
        'operations' => $operations,
        'finished' => 'custom_content_archive_import_contents_finished',
    );
    return $batch;
}

function create_node($arrnode, $ctype, $user, $lang, $operation_details, &$context) {
    $node = new stdClass(); // Create a new node object
    $node->type = $ctype; // Or page, or whatever content type you like
    node_object_prepare($node); // Set some default values
    $node->uid = $user; // UID of the author of the node; or use $node->name
    $node->language = $lang; // Or e.g. 'en' if locale is enabled
    $node->promote = 0;

    foreach ($arrnode as $field => $value){
        switch ($field) {
            case 'title':
                $node->title = $arrnode['title'];
                break;
            case'body':
                $node->body[LANGUAGE_NONE][0]['value'] = nl2br($arrnode['body']);
                $node->body[LANGUAGE_NONE][0]['summary'] = text_summary($bodytext);
                $node->body[LANGUAGE_NONE][0]['format'] = 'filtered_html';
                break;
            default:
                $arrfield = field_info_field($field);
                switch ($arrfield['type']) {
                    case 'datetime':
                         $my_date = new DateTime($value);
                         $node->{$field}[LANGUAGE_NONE][0][value] = date_format($my_date, 'Y-m-d H:i:s');
                         break;
                    case 'text':
                         $node->{$field}[LANGUAGE_NONE][0][value] = $value;
                         break;
                    case 'email':
                         $node->{$field}[LANGUAGE_NONE][0][email] = $value;
                         break;
                    case 'link_field':
                         $node->{$field}[LANGUAGE_NONE][0][url] = $value;
                         break;
                }
        }
    }

    if($node = node_submit($node)) { // Prepare node for saving
        node_save($node);
        workflow_execute_transition($node, 3, $comment = NULL, $force = TRUE);
        // Store some results for post-processing in the 'finished' callback.
        // The contents of 'results' will be available as $results in the
        // 'finished' function (in this example, batch_example_finished()).
        $context['results'][] = $node->nid . ' : ' . check_plain($node->title);

        // Optional message displayed under the progressbar.
        $context['message'] = t('Saving node "@title"', array('@title' => $node->title)) . ' ' . $operation_details;

        _custom_content_archive_import_contents_update_http_requests();
    }

}

function custom_content_archive_import_contents_finished($success, $results, $operations) {
    if ($success) {
        // Here we could do something meaningful with the results.
        // We just display the number of nodes we processed...
        drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _custom_content_archive_import_contents_get_http_requests())));
        drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
    } else {
        // An error occurred.
        // $operations contains the operations that remained unprocessed.
        $error_operation = reset($operations);
        drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
    }
}

function _custom_content_archive_import_contents_update_http_requests() {
    $_SESSION['http_request_count']++;
}

function _custom_content_archive_import_contents_get_http_requests() {
    return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
}

Comment: In the "custom_content_archive_import_contents_submit" the last param is $form_stat and in your code you use $form_state, is a long shot but i think that could be the problem. Also, you do a "count($all_data['data'])" but in custom_content_archive_import_contents_submit  the var $data is defined no where
Comment: Thank's Gnuget, but was only a mistake. I've completed with all 'submit' code. Every comment is welcome.
Answer: I'll refrain from trying to debug your code - that is probably a bit too localized, but will instead give you some advice/help on how to use the batch API in general.
What you are doing should work, but you are still using the batch API the wrong way.
The intended use is, that you create a function that receive all the data, which can handle it in chunks and add that as a single operation. I'll try to illustrate this in code. The code is fictional but should give you an idea of how to do batch jobs. The main thing is that the batch job operation function should keep track of how far it is in the process and do a repeat job a certain number of times. The function will then be recalled with the same args, but the context variable will be updated from each repeated call.
function form_submit_handler_submit($form, &$form_state) {
  // $form_state['values']['nids'] is an array of nids to opperate on.
  $nids = $form_state['values']['nids'];
  batch_set(array(
    'operations' => array('clone_nodes', array($nids)),
    'finihsed' => 'finised_callback',

  );
}

function clone_nodes($nids, &$context) {
  if (empty($context['sandbox']['count'])) {
    $context['sandbox']['count'] = 0;
  }

  $nids_to_handle = range($context['sandbox']['count'], $context['sandbox']['count'] + 49);

  foreach ($nids_to_handle as $key) {
    $node = node_load(nids[$key]);
    clone_the_node($node);
  }

  $context['sandbox']['count'] += 50;

  $context['finished'] = 1.0 * $context['sandbox']['count'] / count($nids);
  $context['message'] = t('Cloning node @current/@total', array('@current' => $context['sandbox']['count'], '@total' => count($nids));
}

Comment: This answer seems to follow the Drupal Batch API documentation: [link](https://api.drupal.org/api/drupal/includes%21form.inc/group/batch/7.x).

The OP follows the pattern in the Examples Module - batch_example [link](https://api.drupal.org/api/examples/batch_example%21batch_example.module/function/batch_example_batch_1/7.x-1.x).

I do not believe that the Examples Module uses the 'wrong' approach.

What am I doing wrong in this code?


MySQL function alternative for me access datediff

Question: Datediff('d', date1, date2) gives an error in MySQL.
But MySQL has a datediff function, and yet it is giving error
Comment: So what did the documentation say about it?
Answer: The datediff in mysql has a different definition.
Try datediff(date2, date1), Also need to reverse the order of dates.
Next time, do read the docs before posting. Here


How write good round_double function in c++?

Question: I'm trying to write good round_double function which will round double in specified precision:
1.
double round_double(double num, int prec)
{
    for (int i = 0; i < abs(prec); ++i)
        if(prec > 0)
            num *= 10.0;
        else
            num /= 10.0;
    double result = (long long)floor(num + 0.5);
    for (int i = 0; i < abs(prec); ++i)
        if(prec > 0)
            result /= 10.0;
        else
            result *= 10.0;
    return result;
}

2.
double round_double(double num, int prec)
{
    double tmp = pow(10.0, prec);
    double result = (long long)floor(num * tmp + 0.5);
    result /= tmp;
    return result;
}

This functions do what I wan't but they are, in my opinion, not good enough. Because starting from precision = 13 - 14, they returning bad results.
The cause I'm sure that there is possible to write good double_round is that just printing the number via cout in specified precision (say 18) is prints better result than result of my function.
For example this part of code:
int prec = 18;
double num = 10.123456789987654321;
cout << setiosflags(ios::showpoint | ios::fixed)
<< setprecision(prec) << "round_double(" << num << ", "
<< prec << ") = " << round_double(num, prec) << endl;

Will print round_double(10.123456789987655000, 18) = -9.223372036854776500 for first round_double and round_double(10.123456789987655000, 18) = -9.223372036854776500for second one.
How write good round_double function in c++? Or there is already exists?
Comment: double has a decimal precision of 15 digits. you cannot get precise values beyond that
Answer: Don't cast to long long that is forcing a conversion to an integer with limited range, beyond what 10^13 requires (well 19 for 64-bit with no whole number part).  Just calling floor should be enough.
double round_double(double num, int prec)
{
    double tmp = pow(10.0, prec);
    double result = floor(num * tmp + 0.5);
    result /= tmp;
    return result;
}

Note that Mike is also correct, you have a limited range you can represent just in double itself. It isn't so great if you need clean decimal responses. But the long long is the cause of your totally wacky numbers.
Comment: Thank you for pointing on this. But in this case also starting from 18 - 19 results are different.
Comment: You have to consider the whole number part of the number you are rounding. If all 10^19 is used for the fractional rounding there is no space to store the whole number. So `10.123` probably starts failing at around 17 precision.
Comment: But again, as what Mike says, double doesn't even have this high of precision, so even fixing the `long long` won't help at this point.  If you have a `long double` it might help.
Answer: The problem is the floating-point representation. A binary representation cannot represent all decimal numbers exactly, and only has a finite precision.
double usually means a 64-bit binary representation as specified by IEEE754, with a 52-bit fractional part. This gives a precision of approximately 16 decimal digits.
If you need more precision than that, then the best option is probably to use an arbitrary-precision arithmetic library such as GMP. Your compiler may or may not offer a long double type with a higher precision than double.
EDIT: sorry, I didn't notice that you're getting completely incorrect results. As another answer says, this is due to the conversion to long long overflowing.
Comment: I don't wan't use GMP or other libraries that works with big decimals. I just wan't that result of precisioned cout and result of my function was the same.
Comment: Sorry, I misread the question and thought you were asking why you don't get 18 digits of precision.
Answer: Another approach is to round based on binary-digits of precision.  Sample implementation below - not sure if it's useful to you, but since you got me playing I thought I'd throw it out there.
Notes:

this uses the ieee754.h header common on Linux systems: it could easily be ported to Windows, but this is undeniably bit hackery and whether it's appropriate in any given production code is a case-by-case call.
you could approximate some decimal near-equivalent, e.g. multiply the desired decimal precision by 10 and divide by 3 (based on 2^10 ~= 10^3).

The input number (10.1234...) with 1 bit of precision is 8; with 2 it's 10 etc..
Separately, IMHO decimal rounding is best done at output time, or when using a decimal-capable representation (e.g. storing an int mantissa and power-of-10 exponent).
#include <ieee754.h>
#include <iostream>
#include <iomanip>

double round_double(double d, int precision)
{
    ieee754_double* p = reinterpret_cast<ieee754_double*>(&d);
    std::cout << "mantissa  0:" << std::hex <
ieee.mantissa0
        << ", 1:" <
ieee.mantissa1 << '\n';
    unsigned mask0 = precision < 20 ? 0x000FFFFF << (20 - precision) :
                                      0x000FFFFF;
    unsigned mask1 = precision <  20 ? 0 :
                     precision == 53 ? 0xFFFFFFFF :
                                       0xFFFFFFFE << (32 + 20 - precision);
    std::cout << "masks     0:" << mask0 << ", 1: " << mask1 << '\n';
    p->ieee.mantissa0 &= mask0;
    p->ieee.mantissa1 &= mask1;
    std::cout << "mantissa' 0:" <
ieee.mantissa0
        << ", 1:" <
ieee.mantissa1 << '\n';
    return d;
}

int main()
{
    double num = 10.123456789987654321;

    for (int prec = 1; prec <= 53; ++prec)
        std::cout << std::setiosflags(std::ios::showpoint | std::ios::fixed)
            << std::setprecision(60)
            << "round_double(" << num << ", "  << prec << ") = "
            << round_double(num, prec) << std::endl;
}

Output...
mantissa  0:43f35, 1:ba76eea7
masks     0:fff80000, 1: 0
mantissa' 0:0, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 1) = 8.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:fffc0000, 1: 0
mantissa' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 2) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:fffe0000, 1: 0
mantissa' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 3) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:ffff0000, 1: 0
mantissa' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 4) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:ffff8000, 1: 0
mantissa' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 5) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:ffffc000, 1: 0
mantissa' 0:40000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 6) = 10.000000000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:ffffe000, 1: 0
mantissa' 0:42000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 7) = 10.062500000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:fffff000, 1: 0
mantissa' 0:43000, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 8) = 10.093750000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:7ffff800, 1: 0
mantissa' 0:43800, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, 9) = 10.109375000000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:3ffffc00, 1: 0
mantissa' 0:43c00, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, a) = 10.117187500000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:1ffffe00, 1: 0
mantissa' 0:43e00, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, b) = 10.121093750000000000000000000000000000000000000000000000000000
mantissa  0:43f35, 1:ba76eea7
masks     0:fffff00, 1: 0
mantissa' 0:43f00, 1:0
round_double(10.123456789987654858009591407608240842819213867187500000000000, c) = 10.123046875000000000000000000000000000000000000000000000000000

etc....


Showing that a ring homomorphism induces a correspondence that maps free modules to free modules

Question: Let $f: \Lambda \rightarrow \Lambda'$ be a ring homomorphism. This induces a mapping $M \mapsto \Lambda' \otimes_{\Lambda} M$, where $M$ is a $\Lambda$-module and $\Lambda' \otimes_{\Lambda} M$ is a $\Lambda'$- module. I want to show that if $M$ is free, $\Lambda' \otimes_{\Lambda}M$ is free.
My attempt:
Let $\left\{x_i\right\}$ be a basis for $M$. I claim that $\left\{1_{\Lambda'} \otimes x_i\right\}$ form a basis for $\Lambda' \otimes_{\Lambda} M$. Given $\lambda' \otimes m \in \Lambda' \otimes_{\Lambda} M$,
\begin{align*}
\lambda' \otimes m &= \lambda' (1_{\Lambda'} \otimes m) \\
                   &= \lambda' (1_{\Lambda'} \otimes (\lambda_1 x_1 + \dots  +                                \lambda_n x_n)) \\
                   &= \lambda' \lambda_1 (1 \otimes x_1) + \dots + \lambda' \lambda_n (1 \otimes x_n)
\end{align*}
for some $\lambda_i \in \Lambda$.
I think I'm on the right track, but I don't know how to reconcile the products $\lambda' \lambda_i$ since at first glance this product has no reason why it would make sense. I feel like I have to use that $f$ is a homomorphism from $\Lambda$ to $\Lambda'$, but I"m not sure how. Can we just consider $\Lambda'$ a $\Lambda$-module and make these problems go away?
Comment: $M$ is free as $\Lambda$-module then $M$ is isomorphic to $\oplus \Lambda.$ Hence $\Lambda' \otimes_{\Lambda}M$ is isomorphic to $\oplus \Lambda '.$ So $\Lambda' \otimes_{\Lambda}M$ is free as $\Lambda '$-module
Answer: By Hungerford's Algebra book page 181:
If $M$ is free as $\Lambda$-module, then $M$ is isomorphic to $\oplus \Lambda.$ Hence $\Lambda' \otimes_{\Lambda}M$ is isomorphic to $\oplus \Lambda '.$ So $\Lambda' \otimes_{\Lambda}M$ is free as $\Lambda '$-module.
Comment: page 213 of book says tensor product  commutes with direct sum. so $\Lambda' \otimes_{\Lambda}M\cong \Lambda' \otimes_{\Lambda}(\oplus \Lambda)\cong $$ \oplus (\Lambda' \otimes_{\Lambda}\Lambda)\cong \oplus \Lambda '.$
Answer: Yes, $\Lambda'$ is a $\Lambda$-module via $f$: set $\lambda\lambda'=f(\lambda)\lambda'$.
But you still need to prove the linear independence of $(1_{\Lambda'} \otimes x_i)$ , and this is not obvious.
Answer: From the abstract nonsense point of view: The tensor product is left adjoint to the Hom-Functor, hence it commutes with arbitrary colimits, in particular with direct sums.


how to configure the landing page in React web application

Question: I created a ReactJS project with the create-react-app package and that worked well, also I'm starting the application using npm start command.
So the application is landing on http://localhost:3000 by default, but I wanted to configure my landing URL to something like this http://localhost:3000/google where /google will be useful to configure the apache web server to detect my application.
NOTE: There is a way to redirect using react-router-dom to <Redirect from="/" to="/google" /> option and this will work once the application is loaded. But I am expecting my application should be accessible only with this URL http://localhost:3000/google. That means all my webpack, node_modules, public URL should go via /google only.
How to configure the base URL of web application ?
Comment: did you refer routing and routing redirect
Comment: when I am executing the `npm start` to open immediate the `localhost:3000/google` without Router component or Redirect
Comment: do you need subfolder routing,
Comment: No. I have updated my question
Comment: hope you are looking  build app into sub folder. https://medium.com/@svinkle/how-to-deploy-a-react-app-to-a-subdirectory-f694d46427c1
take a look at the article
Answer: I think you have the answer to your question here: React Start Landing URL
The first one by @Bhojendra Rauniyar
Hope it helps!
Comment: when I am executing the `npm start` the application itself should start with `localhost:3000/google` without Router component or Redirect. That means all my `webpack, node_modules, public` URL should go via /google only.
Answer: If you are using react-router you can use the basename prop to set the sub directory
<BrowserRouter basename='/google'>
  <App />
</BrowserRouter>

OR you can redirect / to /content in your Switch block
<Switch>
   <Redirect exact from="/" to="/google" />
   <Route exact path="/google" component={ContentComponent} />
</Switch>

Comment: when I am executing the `npm start` the application itself should start with `localhost:3000/google` without Router component or Redirect.
Comment: Currently other than this, there is no other way. However, they are trying to add this feature of configuring the URL. You can check it here https://github.com/facebook/create-react-app/pull/7259


Determining a "spaghetti boundary" 0f formal $n$-squares

Question: Consider a formal $n$-square, i.e., the set $\{ 0, 1, .., n-1 \}^2$ of positions (column number, row number) of a square $n$-matrix. A cyclic rotation of the $j$-th row $\{ (i,j) : i = 0 .. n-1 \}$ by an integer amount $r$ maps $(i,j)$ to $(i + r \text{ mod } n, j)$ for each $i$. A horizontal move consists of a cyclic rotation of some rows by some amount (which may be different for each involved row).
Let $S$ be a set of $n$ positions, distributed over a number of rows. The more rows containing positions of $S$, the less elements on individual rows. The "spaghetti effect" occurs when it is possible to apply a horizontal move shifting the positions of $S$ into all different columns. Considering a horizontal move as "exercising pressure" on $S$ and moving positions row by row into distinct columns as "crumbling", some intuition gets explained: a set of $n$ positions involving many rows is like a string of spaghetti, having small (rows) sections. With the appropriate pressure, it should crumble completely.
The spaghetti boundary of a formal $n$-square is the least number $s(n)$ such that all sets of $n$ positions involving more than $s(n)$ rows can have their row sections moved apart. In my paper "shuffled equi-$n$-squares", available at
http://arxiv.org/abs/1701.02325
a method was developed to estimate spaghetti boundaries for various sizes $n$. The method relies on results about rotating sets apart in a regular $n$-gon. The results for $8 \leq n \leq 50$ can be found in table 5 of the cited paper.
Only a few values are proven sharp. They are listed here in the format $(n, s(n))$ for $n \geq 4$:
$$(4,2), (5,2), (6,3), (7,3), (8,4), (9,5), (12,7).$$
For $n = 2, 3$ the spaghetti effect occurs all the time whence $s(2) = s(3) = 0$. For a few other $n$ we have a value that is proven sharp up to one unit.
Question. Find sharp spaghetti boundaries for other $n$.
Added 04/29/2017: s(10)=5 is sharp too.
Added 19/05/2017: I finally managed to write a (rather lengthy) inductive proof showing that Hagen von Eitzen's proposal for $s(n)$ is correct for all $n$. I don't see a way to attach a pdf-file here.
Comment: I just finished my unpolished code and it produces the sequence $0,0,2,2,3,3,4,5,5,6,7,7,8,9,10,10,11,12$ (which is not in OEIS), and now begins to become really slow while computing $s(20)$ ... Is it clear that $n\ge4\implies s(n)le s(n+1)\le s(n)+1$?
Comment: So a different formulation would be: Given $m$ regular $n$-gons such that $n$ of the $mn$ vertices are marked (at least one for each $n$-gon), it may or may not be possible to rotate (but not reflect) and overlay the $n$-gons to obtain an $n$-gon with all vertices coloured. Let $s(n)$ be the maximal $m$ for which such a constellation exists for which one cannot achieve that all vertices are coloured?
Comment: ... So it seems that a search for $s(10)$ can be performed by throwing some (but not very much) CPU at it?
Comment: .. without reading the results in your paper, it is clear that $s(10)\ge 5$ (pick five rows with each having two even positions occupied), and $s(10)<7$ ($10=3+2+1+1+1+1+1$: the three-rwo can block at most $6$ of ten possible positions for the two-row; $10=2+2+2+1+1+1+1$: the first two-rows can block at most $8$ positions for the last two-rpw). So what about $6$? $10=4+2+1+1+1+1$ and $10=3+3+1+1+1+1$ can be solved by the same argument; $10=3+2+2+1+1+1$ requires a few more thoughts how the two-rows can interfere, but I think can be easily dismissed; only $10=2+2+2+1+1$ I'd test systematically
Comment: The table in my paper gives an estimated spaghetti boundary s(10) = 6. In my personal notes I see that I considered rotating four pairs apart, which is probably what you propose on the last line (with three 2's).  A few years ago I ran a C-program with n up to 30 something on the problem of rotating pairs apart in an $n$-gon (see my question on "how many clock hands".  If I can trust my short notes, four worked fine but five pairs can fail. This would mean that 5 rows of 2 can fail the spaghetti effect and s(10)=5 is sharp.
Comment: ... and this looks suspiciously like $s(n)\approx \frac{\sqrt5-1}{2}n$, doesn't it?
Comment: I withdraw my guess about the golden ratio - after all there is a simple bound $s(n)\ge n-2\lceil\sqrt n\rceil+2$.
Comment: @Hagen: your sequence improves some of my sp-values. What exactly have you been testing? When using number partitions, the ones can largely be neglected as they can always be rotated in the leftover columns. This can help to stretch your program to larger n.
Answer: Theorem. Let $n\ge 4$. Then
$$ s(n)=n+2-\min_{ab\ge n}(a+b)=n+2-\lceil\sqrt n\,\rceil-\left\lceil\frac{n}{\lceil\sqrt n\,\rceil}\right\rceil.$$
Proof.
First we show the second equality: If we want to minimize $a+b$ under the condition $ab\ge n$ (and wlog. $a\ge b$), we certainly need $a\ge\sqrt n$. And if $a=\lceil\sqrt n\,\rceil+r$ with $r\ge 0$, then we need $b\ge\frac na$, i.e., we need to choose $r$ to minimize $r+\left\lceil\frac{n}{\lceil\sqrt n\,\rceil+r}\right\rceil$, but increasing $r$ by one decreases the second summand by at most one, so that the minimum is attained already for $r=0.
Next we show $s(n)\ge m$ if $m=n-a-b+2$ with $ab\ge n\ge a+b$ (and so $m\ge2$). We may assume wlog. that $a(b-1)<n$.
Let $$\begin{align}A_1&=\{0,1,\ldots,a-1\},\\A_2&=\{0,a,2a,\ldots,(b-1)a\},\\ A_k&=\{0\}\qquad\text{for }3\le k\le m.\end{align}$$ (All viewed as subsets of $\Bbb Z/n\Bbb Z$). Then $\sum_{k=1}^m|A_k|=a+b+(m-2)=n$. Assume there are integers $c_1,\ldots,c_m$ such that $\bigcup(A_i+c_i)=\Bbb Z/n\Bbb Z$, or equivalently $(A_i+c_i)\cap (A_j+c_j)=\emptyset$ for $i\ne j$. We may assume wlog $c_1=a(b-1)$. Then $0\le c_2<a$ leads to a conflict because $(b-1)a\in A_2$, $a\le c_2<2a$ does the same because $(b-2)a\in A_2$, and so on, so that $0\le c_2<ab$ leads to a conflict. By assumption, this covers all residue classes $\bmod n$. Hence $(c_1,\ldots,c_m)$ as desired cannot exist, thus showing $s(n)\ge m$ and ultimately
$$s(n)\ge  n+2-\lceil\sqrt n\,\rceil-\left\lceil\frac{n}{\lceil\sqrt n\,\rceil}\right\rceil.$$
The other direction follows from the claim below, where we work with $d=n-m$ and thus without all one-element sets $A_i$, which never pose a problem. $\square$
Claim.
Let $n\ge 4$ and let $d$ be a non-negative integer $<\lceil\sqrt n\,\rceil-\left\lceil\frac{n}{\lceil\sqrt n\,\rceil}\right\rceil$.
Let $k\in\Bbb N$ and $a_1\ge a_2\ge \ldots\ge a_k\ge 2$ and $a_1+\ldots+a_k\le k+d$. Then for any choice of sets $A_1,\ldots,A_k\subseteq\Bbb Z/n\Bbb Z$ with $|A_i|=a_i$, there exist integers $c_1,\ldots,c_k$ with $A_i+c_i\cap A_j+c_j=\emptyset$.
Proof [by induction on $k$].
The case $k=1$ is trivial.
Assume $k\ge2$ and the claim is already correct for smaller $k$.
Let $A_1,\ldots,A_k$ as in the claim be given.
From $a_1+\ldots+a_{k-1}\le(k-1)+d$ and the induction hypothesis, we find suitable $c_1,\ldots,c_{k-1}$. Of the $n$ possible choices for $c_k\bmod n$, this prohibits at most
$$\tag1 a_k\cdot(a_1+\ldots+a_{k-1})\le a_k(k+d-a_k)\le\frac1k\cdot\frac{k-1}{k}\cdot (k+d)^2$$
(using $ka_k\le a_1+\ldots+a_k\le k+d$ and that$x\mapsto x(a-x)$ is increasing on $[0,\tfrac a2]$).
As $k\le (a_1-1)+\ldots+(a_k-1)\le d<2\sqrt n$, this is $<\frac{4n}k$, which already $<n$ and hence sufficient if $k\ge4$.
If $k=3$, our estimate $(1)$ is $< \frac29\cdot 4n<n$ and we are done again.
Remains the case $k=2$.
But then the conditions of the claim give us
$$a_1+a_2<\lceil\sqrt n\,\rceil-\left\lceil\frac{n}{\lceil\sqrt n\,\rceil}\right\rceil=\min_{ab\ge n}(a+b)$$
so that we conclude $a_1a_2<n$, i.e., again there are less than $n$ choices prohibited. $\square$
Comment: Beautiful. In the proof of the Claim I recognize many ingredients of my original proof of lemma 3.11. But you have dealt far more cleverly with numbers and formulae than I did. In this way, you avoided some restrictions and complications that I met with. I.'m going to check the consequences of  the strengthened result.  I'll inform you soon.
Comment: On second reading, it seems to me that you run into a similar problem as I did in the proof of my lemma 3.11. After you get k <= d < 2 sqrt(n) you want (1) < n. Now d127. The "4n" is wrong in your computation.


Is $\sqrt{-1}$ equal to $i$ or $\pm i$?

Question: In complex numbers is $\sqrt{-1}$ equal to $i$ or $\pm i$ ?
In both cases how do we explain it?
The question arose when I saw it in Lathi's book (Linear Systems and Signals).
Comment: Doesn't matter. Although $i$ and $-i$ are additive(and multiplicative) inverses of each other, there is no *algebraic* difference. You can choose anyone to be $\sqrt{-1}$
Comment: I think the most *usual* definition is $\;\sqrt{-1}:=i\;$ , and this helps only to make things standard. Just like by definition $\;\sqrt4=2\;$ and *not* $\;\sqrt4=-2\;$ : no "natural" reason to choose one over the other, just simplicity and international (more or less..) standard
Answer: $i^2=-1$ and $(-i)^2=-1$. In $\mathbb C$ these are the two distinct roots of equation $z^2+1=0$.
Based on that it might be tempting to say that $\sqrt{-1}=i$ and/or $\sqrt{-1}=-i$, but  we deal here with a function on $[0,\infty)\subset \mathbb R$ so $\sqrt{-1}$ is not defined properly here. Extension of the function to $\mathbb C$ leads to 'branches'.

Is $\sqrt{-1}$ equal to $i$ or $\pm i$?


Regressors vs. conditioning variables in glmtree

Question: I have a dataset with ~800K samples, ~300 features and I'm trying to predict a binary outcome. I've started with sklearn's SGDClassifier (using log loss and l1 penalty), and I got a nice 0.67 auc score on my validation set; now I'm trying to improve this.
I have a reason to believe that different subspaces of features space "behave" differently, so I thought fitting a glmtree would make sense (splitting the features space to different subspaces and fitting a logistic regression in each of them). However, when trying to write the formula, I don't have any prior knowledge regarding which features should be used as predictors for the glm, and which should be used as conditioning variables.
Is there any automated way to select which is which?
Answer: glmtree() is particularly strong in situations where you have a GLM that you would typically fit to the whole sample, e.g., a voter targeting model (Rusch et al. 2013), a treatment effect model (Seibold et al. 2016), or an economic growth model (Wagner & Zeileis 2017). Then you can detect heterogeneity in the model parameters depending on the partitioning variables.
If you have no such "base" model and have no idea which variables are important, then you can fit an intercept-only GLM in each node. However, then the resulting tree is often rather similar to other classification trees (CART, CTree, etc.).
I have also seen situations where all numeric variables have been used in the regression and the categorical variables in the partitioning part. Possibly the numeric variables can also appear in both parts. However, this is only likely to yield good results if the number of numeric variables is small to moderate.
In your case with ~800K observations and ~300 variables and no prior information I personally would not bother with single trees anyway. I would probably use a random forest. You have enough observations to approximate potentially linear (or partially linear) effects sufficiently well through the forest.
References

Thomas Rusch, Ilro Lee, Kurt Hornik, Wolfgang Jank, Achim Zeileis (2013). Influencing Elections with Statistics: Targeting Voters with Logistic Regression Trees. The Annals of Applied Statistics, 7(3), 1612–1639. doi:10.1214/13-AOAS648
Heidi Seibold, Achim Zeileis, Torsten Hothorn (2016). Model-Based Recursive Partitioning for Subgroup Analyses. The International Journal of Biostatistics, 12(1), 45–63. doi:10.1515/ijb-2015-0032
Martin Wagner, Achim Zeileis (2017). Heterogeneity and Spatial Dependence of Regional Growth in the EU: A Recursive Partitioning Approach. German Economic Review. Forthcoming. doi:10.1111/geer.12146
Comment: Thanks for the detailed response! The reason I'm not using a RF approach is that I'm specifically looking for the partitioning rules; the ability to interpret the rules is roughly as important to me as the prediction accuracy. I understand from your answer that there's no principled way to select which is which - is that correct?
Comment: I wouldn't know of a solution that will automatically and reliably separate linear, partially linear, nonlinear effects in the presence of high-order interactions and subgroup effects. There are search heuristics, however, e.g., performing AIC or BIC selection for the model in each node of the tree. But so far I have just tried these on a couple of data sets but never evaluated them properly. As for interpreting the rules: Note that these may change dramatically if you introduce linear effects in the subgroups! This model fits may still be similar but the structure could look very different.


Regex para validar o mime type

Question: Qual seria a regex para validar esse mime types somente

image/bmp
image/gif
image/x-icon
image/pjpeg
image/jpeg
image/png

Quero fazer um sistema de upload de imagens somente, no caso desse mime types:
$verifyimg = getimagesize($_FILES['image']['tmp_name']);

if(!preg_match($pattern, $verifyimg['mime']){
        die("Só imagens!");
    }

Answer: Você pode fazer desta forma, fica mais simples e mais fácil de ler:
if (strpos($verifyimg['mime'], 'image/') === false) {
    die("Só imagens!");
}

Utilizando regex:
if (!preg_match('/image\//',$verifyimg['mime'])) {
    die("Só imagens!");
}

Regex para validar o mime type


Indent placement?

Question: I've tried to design a small (3 rooms) "adventure-like" mini-game, and when i run it i get:
if answer == "restaurant":
IndentationError: unindent does not match any outer indentation level
# TRUMANIA v. 0.0.2

def Square():
    print "You exit from there, and you appear in Central Square. Do you want to walk to the Library or to the Restaurant?"
    answer = raw_input("Type 'Restaurant' or 'Library' and hit 'Enter'.").lower()
    if answer == "restaurant":
        Restaurant()
    elif answer == "library":
        Library()
    else:
        print "You didn't pick Restaurant or Library! You can't just stand there like a rock; you need to make a decision!"
        Square()

def Trumania():
    print "You've just entered Trumania!"
    print "Do you want to go to the Restaurant or visit the Library?"
    answer = raw_input("Type 'Restaurant' or 'Library' and hit 'Enter'.").lower()
    if answer == "restaurant":
        Restaurant()
    elif answer == "library":
        Library()
    else:
        print "You didn't pick Restaurant or Library! You can't just stand in the entrance like a scarecrow; you need to make a decision!"
        Trumania()

def Restaurant():
    print "You've just entered the Restaurant!"
    print "Do you want to eat Inside or Outside?"
    answer = raw_input("Type 'Inside' or 'Outside' and hit 'Enter'.").lower()
    if answer == "inside":
        print "You need to bribe the waiter, but you get a cozy table and have a wonderful meal!"
    elif answer == "outside":
        print "You get a cheap (and cold) table in the outside and manage to eat some apetizers"
    else:
        print "You didn't selected Inside or Outside. The Waiter euh... Waits... "
        Restaurant()

def Library():
    print "You arrive at the Library!"
    print "Do you want to get some books or you prefer to study?"
    answer = raw_input("Type 'Books' or 'Study' and hit 'Enter'.").lower()
    if answer == "books":
        print "You get some books that you can sell later. However, thats bad karma!"
    elif answer == "study":
        print "You learn a lot!However, you feel tired after so much effort"
    else:
        print "You didn't pick Books or Study! Try again."
        Library()

Trumania()
Square()

The idea is to start in Trumannia, then be able to choose Restaurant or Library, and then exit to Square and be able to choose again Restaurant or Library. So I'm not sure a) how to fix the error and b) how to format the different variables so the program can "load" all the info first and then "go" to each place when needed. I hope i explained myself. Thanks again!
Comment: Also include your goals with the game, a simple "my code is not working" is not enough to get good pieces of advice.
Comment: Please read http://stackoverflow.com/help/mcve
Comment: @MrE *"I'm unable to make it work"* suggests otherwise...
Comment: What is "fencing" a variable?!
Comment: @Top8 that's correct, or you can click the `{}` button at the top of the editor window.
Comment: Ok thanks everyone, error fixed and some "stackoverflownetiquette" learned ;-) Its ok if i delete this post completely and make a new one with the cleaned code, a short problem and i try not to make @jonrsharpe work again? (He deleted seventy something words form my first post lol)
OR I edit this post myself as well so i avoid more noise?
Comment: No worries, and no need to start over. Your attitude is great! Welcome to the site. Editing is a normal part of the asking process. Your question looks good now, and you got an answer. Hopefully the question will be reopened shortly now that it's been fixed up. If you want you could delete your older comments that have been addressed. Cheers!
Answer: Your code is broken because there is no colon after def Square().
Comment: Once you fix that, though, we'd be glad to have you ask a question on [codereview.se].

Indent placement?


Command Prompt not responding after running django server

Question: Windows command prompt gets stuck after i run my django server
seems working, and the server works in the browser but i cant type any thing in the command prompt

any ideas?
Thanks
Comment: If you press Ctrl+Break when in command line window, doesn't stop the dev server?
Comment: What is it you're trying to do? This sounds like an [xy problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)
Comment: Nothing unusual, webserver is running as console application, it's not a windows server as you may expected
Answer: It's exactly what's expected - the dev server is not meant to run as a service, it's only here for local development purpose. Use another command prompt if you have other things to do while the dev server is running.
Comment: Thank you, that has solved my problem
Comment: @MJerjawi glad I could help - and feel free to accept the answer then ;)


Suppose a random variable X has mean 0 and moment generating function as follows, find values of a and b

Question: $M_x(t)=a(1+e^{-2t}+e^{-t} +e^t+be^{2t}), -\infty<t<\infty$
Do I take the first derivative of this function? How do I solve for two variables given only one equation?
And as a followup question, how do I obtain the probability mass function of X from the moment generating function?
Edit: Taking Andre's hint, $M(0)=a(4+b)=1$ and $M'(0)=a(-2+2b)=0$. Solve these two equations and obtain $a=\frac{1}{5}, b=1$
Answer: Hint  We have $M_X(0)=1$  and $M_X'(0)=0$.
For your second problem, we can read off $\Pr(X=0)$, $\Pr(X=-2)$, $\Pr(X=-1)$, $\Pr(X=1)$ and $\Pr(X=2)$
from  the mgf.
Comment: In this case, yes. For a proof, note that a finite number of discrete values gives an mgf of this type, and then appral to thr uniquness theorem.
Comment: So the pmf will be a function of discrete values?


Looking for asymp exp as $N \rightarrow \infty$ for $\sum_{r=1}^{N}\sum_{t=1}^{N}\left[\sqrt{rt}\in Z\right]$ w/wo the constraint $GCD(r,t)=1$

Question: Where $[...]$ are Iverson brackets. These two problems arise from summing over the number of reducible quadratics using their coefficients where $r\, {x}^{2} + s\, x + t$ where in both cases we have $1 \le r \le N$ and $|s|, |t| \le N$ where $N$ is the naive height such that $N \ge 1$.
These two sums occur when $s = 0$.  In the first case the sum over all possible quadratics are unique, that is no quadratic appears more than once.  This constraint to the sum is $GCD(r,t)=1$.  The cases when $r, t \le 0$ are  already folded into the above expressions.
The second case is that the uniqueness condition is removed so that $GCD(r,t) \ge 1$.
I am looking for the asymptotic expansion to as many terms as possible as $N \rightarrow \infty$ and if possible a solution in terms of known functions.
Numerical solutions for large $N$ show that the case with constraints we get approximately $0.6 N$ and without constraints is $0.6 N Log(N)$.  I have noticed that $0.6 \sim 6/(\pi)^2 = 1/\zeta(2)$ which does show up in many number theory expansions.
I have encountered a related case where we have $\sum_{t=1}^{N}\left[\sqrt{rt}\in Z\right]$ for a fixed $r$.  Now if $r=1$ we have the number of perfect squares between $1$ and $\sqrt{t}$ which is $\left\lfloor\sqrt{N}\right\rfloor$.  If $r=2$, I see that $\left\lfloor\sqrt{2N}/2\right\rfloor$ holds.  This is not generalizing to $r>2$.  What is the expansion as $N \rightarrow \infty$ for arbitrary $r$.
Answer: If $\gcd(a,b)=1$ and $ab$ is a perfect square, then it follows that both $a$ and $b$ must be perfect squares. Thus, your sum is equal to the number of ordered pairs $(a,b)$ of coprime perfect squares less than $n$.
Let's write $a=x^2$ and $b=y^2$, so that $x,y$ must be coprime positive integers between $1$ and $\lfloor \sqrt{n}\rfloor$, inclusive. Suppose that $n$ is large. In this previous question, we see that the probability of two random integers being coprime, when selected uniformly and at random from the first $N$ positive integers for large $N$, converges to $6/\pi^2$. Thus, since there are $\lfloor \sqrt{n}\rfloor ^2 \sim n$ pairs $(x,y)$ with $1\le x,y\le \lfloor \sqrt n\rfloor$, we have that your sum is asymptotically
$$\sim \frac{6n}{\pi^2}$$
Thus, your suspicion was correct.
Comment: That is very nice solution to the constrained case.
Answer: For the case where we do not require $\gcd(r,t) = 1$, and for $\sum_{t = 1}^N [\sqrt{rt} \in \mathbb{Z}]$ it is useful to know that every positive integer $n$ can be written in a unique way as $n = a\cdot b^2$ with a squarefree $a$. Indeed, by the fundamental theorem of arithmetic $a$ must be the product of the primes whose exponent in the factorisation of $n$ are odd. Call $a$ the squarefree part of $n$, write it $\operatorname{sf}(n)$. Then it is easy to see that for positive integers $m,n$ we have $\sqrt{mn} \in \mathbb{Z}$, i.e. $mn$ is a square, if and only if $\operatorname{sf}(m) = \operatorname{sf}(n)$.
Also useful is the fact that $\lvert \mu(n)\rvert$ is the characteristic function of the squarefree positive integers, and that the number of squarefree integers not exceeding $x$ is
$$Q(x) = \sum_{n \leqslant x} \lvert \mu(n)\rvert = \frac{6}{\pi^2}x + O(\sqrt{x})\,.$$
(Using the prime number theorem one can show that the remainder term is $o(\sqrt{x})$, but that doesn't help us here.)
For $\sum_{t = 1}^N [\sqrt{rt} \in \mathbb{Z}]$ the matter is easy. Let $a = \operatorname{sf}(r)$. Then $\sqrt{rt} \in \mathbb{Z} \iff \operatorname{sf}(t) = a \iff t = a\cdot b^2$ for some positive integer $b$. And
$$t \leqslant N  \iff b^2 \leqslant \frac{N}{a} \iff b \leqslant \sqrt{\frac{N}{a}}\,,$$
i.e.
$$\sum_{t = 1}^N [\sqrt{rt} \in \mathbb{Z}] = \biggl\lfloor \sqrt{\frac{N}{\operatorname{sf}(r)}}\biggr\rfloor\,.$$
This is a nice exact formula, but it requires knowing the prime factorisation of $r$ almost completely, which is generally a hard problem for large $r$. (Not completely; if one knows that the unfactored part has at most two prime factors and is not a square, then one knows that it is squarefree without having to know whether it's a prime or a semiprime, and in the latter case which primes divide it.)
For the quadratic forms without the uniqueness condition we have to calculate a little more. It's a relatively simple calculation to obtain the leading term if we sum over the common squarefree part of $r$ and $t$. We need to know how many $r \leqslant N$ have a given squarefree part $a \leqslant N$. We just computed that above, the number is $\lfloor \sqrt{N/a}\rfloor$. By symmetry, the number of $t$ with squarefree part $a$ is the same, and since we can choose the square parts of $r$ and $t$ independently we have
$$\sum_{r = 1}^N\sum_{t = 1}^N [\sqrt{rt} \in \mathbb{Z}]
= \sum_{a = 1}^N \lvert\mu(a)\rvert\cdot \biggl\lfloor \sqrt{\frac{N}{a}}\biggr\rfloor^2\,.$$
Now for every real $x$ we have $\lfloor x\rfloor^2 = (x - \{x\})^2 = x^2 - 2x\{x\} + \{x\}^2$, where $\{x\} = x - \lfloor x\rfloor$ is the fractional part of $x$, so
$$\sum_{r = 1}^N\sum_{t = 1}^N [\sqrt{rt} \in \mathbb{Z}]
= \sum_{a = 1}^N \lvert\mu(a)\rvert\cdot \frac{N}{a} - 2\sum_{a = 1}^N \lvert\mu(a)\rvert\sqrt{\frac{N}{a}}\biggl\lbrace\sqrt{\frac{N}{a}}\biggr\rbrace + \sum_{a = 1}^N \lvert \mu(a)\rvert \biggl\lbrace\sqrt{\frac{N}{a}}\biggr\rbrace^2\,.$$
The first of the sums on the right can easily be evaluated with satisfactory precision using summation by parts:
\begin{align}
\sum_{a = 1}^N \frac{\lvert \mu(a)\rvert}{a} &= \frac{Q(N)}{N} + \int_1^N \frac{Q(u)}{u^2}\,du \\
&= \frac{\frac{6}{\pi^2}N + O(\sqrt{N})}{N} + \int_1^N \frac{\frac{6}{\pi^2}u + O(\sqrt{u})}{u^2}\,du \\
&= \frac{6}{\pi^2} + O(N^{-1/2}) + \frac{6}{\pi^2}\int_1^N\frac{du}{u} + \int_1^N O(u^{-3/2})\,du \\
&= \frac{6}{\pi^2}\log N + \frac{6}{\pi^2} + \int_1^{\infty} O(u^{-3/2})\,du + O(N^{-1/2}) - \int_{N}^{\infty} O(u^{-3/2})\,du \\
&= \frac{6}{\pi^2}\log N + K + O(N^{-1/2})\,.
\end{align}
Reintroducing the factor $N$ that we ignored for the computation, the first sum is $\frac{6}{\pi^2}N\log N + KN + O(\sqrt{N})$ with an (as yet) unknown constant $K$.
The value of the constant $K$ can be determined by other means, but unfortunately, as it turns out, that won't help us.
We can brutally estimate the second sum using $0 \leqslant \{x\} < 1$:
\begin{align}
\sum_{a = 1}^N \lvert\mu(a)\rvert\sqrt{\frac{N}{a}}\biggl\lbrace\sqrt{\frac{N}{a}}\biggr\rbrace
&\leqslant \sqrt{N} \sum_{a = 1}^N \frac{\lvert \mu(a)\rvert}{\sqrt{a}} \\
&\leqslant \sqrt{N}\sum_{a = 1}^N \frac{1}{\sqrt{a}} \\
&\leqslant 2N\,,
\end{align}
so this is an $O(N)$ term. In fact, with more work one can find that this sum is $C\cdot N + O(N^{\alpha})$ for an $\alpha < 1$ (off the top of my head I'm not sure which $\alpha$, I think $3/4$, or maybe $O(N^{3/4}\log N)$, so $3/4 + \varepsilon$). But again, that isn't worth the effort, because for the third sum I don't know how to do better than
$$\sum_{a = 1}^N \lvert \mu(a)\rvert \biggl\lbrace\sqrt{\frac{N}{a}}\biggr\rbrace^2 \leqslant \sum_{a = 1}^N \lvert \mu(a)\rvert = O(N)$$
(well, we can find an explicit constant factor easily, but it remains an $O(N)$).
Thus altogether
$$\sum_{r = 1}^N\sum_{t = 1}^N [\sqrt{rt} \in \mathbb{Z}] = \frac{6}{\pi^2}N\log N + O(N)\,.$$
So we have found the principal asymptotics, but with an unpleasantly large remainder term.
A different way to evaluate the sum gets us more, also the linear term and an $O(N^{2/3}\log N)$ remainder term. Thus also the third sum $\sum \lvert \mu(a)\rvert \{\sqrt{N/a}\}^2$ behaves well, and it should be possible to show that directly. I'll look at that when I have time. But now to the different evaluation. Let
$$S(N) := \sum_{r = 1}^N\sum_{t = 1}^N [\sqrt{rt} \in \mathbb{Z}]\,.$$
Then $S(N) - S(N-1)$ is the number of pairs $(r,t)$ such that $rt$ is a square where $r = N$ or $t = N$ (or both). By symmetry
$$S(N) - S(N-1) = 2\sum_{t = 1}^{N} [\sqrt{Nt}\in \mathbb{Z}] - 1\,.$$
($r = t = N$ was counted twice in the sum, so we must subtract $1$.) We found the value of the sum above,
$$\sum_{t = 1}^{N} [\sqrt{Nt}\in \mathbb{Z}] = \sqrt{\frac{N}{\operatorname{sf}(N)}}\,.$$
No $\lfloor\,\cdot\,\rfloor$ needed since $N/\operatorname{sf}(N)$ is a perfect square. Thus
$$S(N) = \sum_{k = 1}^{N} \bigl(S(k) - S(k-1)\bigr) = \sum_{k = 1}^{N} \biggl(2\sqrt{\frac{k}{\operatorname{sf}(k)}} - 1\biggr) = 2\sum_{k = 1}^{N}\sqrt{\frac{k}{\operatorname{sf}(k)}} - N\,.$$
If $k = a\cdot b^2$ with squarefree $a$, then $\sqrt{k/\operatorname{sf}(k)} = b$, and to evaluate the sum we can count how often each possible value $b$ occurs. Every $b \leqslant \sqrt{N}$ occurs $Q(N/b^2)$ times, hence
$$\sum_{k = 1}^{N} \sqrt{\frac{k}{\operatorname{sf}(k)}} = \sum_{b \leqslant \sqrt{N}} b\cdot Q\biggl(\frac{N}{b^2}\biggr)\,. \tag{$\ast$}$$
If we plug $Q(x) = \frac{6}{\pi^2} x + O(x^{\rho})$ into this sum, that leads to an $O(N)$ error term, regardless of $\rho \leqslant 1$. But if we use
$$Q(x) = \sum_{k \leqslant \sqrt{x}} \mu(k)\biggl\lfloor \frac{x}{k^2}\biggr\rfloor$$ we get something that helps us:
$$\sum_{b \leqslant \sqrt{N}} bQ\biggl(\frac{N}{b^2}\biggr) = \sum_{b \leqslant \sqrt{N}} \sum_{k \leqslant \sqrt{N}/b} b\mu(k)\biggl\lfloor\frac{N}{(bk)^2}\biggr\rfloor = \sum_{n \leqslant \sqrt{N}} \varphi(n)\biggl\lfloor \frac{N}{n^2}\biggr\rfloor = \sum_{n^2m \leqslant N} \varphi(n)\mathbb{1}(m)\,.$$
This sum can be handled with the hyperbola method (yes, the graph of $x \mapsto c/x^2$ isn't really a hyperbola, but let's not be too pedantic) to yield the above error term. Applying the hyperbola method to $(\ast)$ also gives an $O(N^{1-\delta})$ error term, but if I haven't miscalculated there's a hard lower bound of $N^{7/10}$ because $Q(x) - \frac{6}{\pi^2} \in \Omega_{\pm}(x^{1/4})$ (Liu's $\rho = \frac{11}{35} + \varepsilon$ gives $1 - \delta = \frac{59}{83} + \varepsilon$, while the elementary $\rho = 1/2$ gives $3/4$), so we gain something by the rewriting.
To evaluate the last sum, we need the asymptotics of some sums involving Euler's totient function. The first,
$$\sum_{n \leqslant y} \varphi(n) = \frac{3}{\pi^2}y^2 + O(y\log y)$$
is known well enough. The second is
\begin{align}
\sum_{n \leqslant y} \frac{\varphi(n)}{n^2}
&= \sum_{k\cdot m \leqslant y} \frac{\mu(k)m}{(km)^2} \\
&= \sum_{k \leqslant y} \frac{\mu(k)}{k^2}\sum_{m \leqslant y/k} \frac{1}{m} \\
&= \sum_{k \leqslant y} \frac{\mu(k)}{k^2}\bigl(\gamma + \log y - \log k + O(k/y)\bigr) \\
&= (\gamma + \log y)\biggl(\frac{6}{\pi^2} + O(y^{-1})\biggr) + \sum_{k \leqslant y} \frac{\mu(k)\cdot(-\log k)}{k^2} + O\biggl(\frac{\log y}{y}\biggr) \\
&= \frac{6}{\pi^2}\biggl(\log y + \gamma - \frac{\zeta'(2)}{\zeta(2)}\biggr) + O\biggl(\frac{\log y}{y}\biggr)
\end{align}
since
$$\sum_{k \leqslant y} \frac{\mu(k)(-\log k)}{k^s} = -\frac{\zeta'(s)}{\zeta(s)^2} + O\biggl(\frac{\log y}{y^{\operatorname{Re} s - 1}}\biggr)$$
for $\operatorname{Re} s > 1$.
Then we have
$$\sum_{n^2m\leqslant N} \varphi(n)\mathbb{1}(m) = \sum_{n \leqslant N^{1/3}} \varphi(n)\biggl\lfloor\frac{N}{n^2}\biggr\rfloor + \sum_{m \leqslant N^{1/3}} \sum_{n \leqslant \sqrt{N/m}} \varphi(n) - \bigl\lfloor N^{1/3}\bigr\rfloor \sum_{n \leqslant N^{1/3}} \varphi(n)\,.$$
The first of these sums yields
\begin{align}
\sum_{n \leqslant N^{1/3}} \varphi(n)\biggl(\frac{N}{n^2} + O(1)\biggr)
&= N\sum_{n \leqslant N^{1/3}} \frac{\varphi(n)}{n^2} + O\Biggl(\sum_{n \leqslant N^{1/3}} \varphi(n)\Biggr) \\
&= N\frac{6}{\pi^2}\biggl(\frac{1}{3}\log N + \gamma - \frac{\zeta'(2)}{\zeta(2)} + O\biggl(\frac{\log N}{N^{1/3}}\biggr)\biggr) + O(N^{2/3}) \\
&= \frac{2}{\pi^2}N\log N + \frac{6}{\pi^2}\biggl(\gamma - \frac{\zeta'(2)}{\zeta(2)}\biggr)N + O(N^{2/3}\log N)\,.
\end{align}
The second is
\begin{align}
\sum_{m \leqslant N^{1/3}} \biggl(\frac{3}{\pi^2} \frac{N}{m} + O\biggl(\sqrt{\frac{N}{m}\log \frac{N}{m}}\biggr)
&= \frac{3}{\pi^2}N\biggl(\frac{1}{3}\log N + \gamma + O(N^{-1/3})\biggr) + O\Biggl(\sqrt{N}\log N \sum_{m \leqslant N^{1/3}} \frac{1}{\sqrt{m}}\Biggr) \\
&= \frac{1}{\pi^2}N\log N + \frac{6}{\pi^2}\frac{\gamma}{2} + O(N^{2/3}\log N)
\end{align}
and the third
$$\bigl(N^{1/3} + O(1)\bigr)\biggl(\frac{3}{\pi^2}N^{2/3} + O(N^{1/3}\log N)\biggr)
= \frac{3}{\pi^2}N + O(N^{2/3}\log N)\,.$$
Adding up, we find
$$\sum_{n \leqslant \sqrt{N}} \varphi(n)\biggl\lfloor \frac{N}{n^2}\biggr\rfloor = \frac{3}{\pi^2}N\log N + \frac{6}{\pi^2}\biggl(\frac{3}{2}\gamma - \frac{\zeta'(2)}{\zeta(2)} - \frac{1}{2}\biggr)N + O(N^{2/3}\log N)$$
and therefore
$$S(N) = \frac{6}{\pi^2}N\log N + \frac{6}{\pi^2}\biggl(3\gamma - 2\frac{\zeta'(2)}{\zeta(2)} - 1 - \zeta(2)\biggr)N + O(N^{2/3}\log N)\,.$$
Comment: The case where $\sum_{r=1}^{N} \sum_{t=1}^{N} \left[\sqrt{rt} \in \mathbb{Z}\right]$ is listed as sequence A132188 in the OSIE with the above asymptotic results to the same order.
Comment: Using the improved error estimate from Liu, H. -Q. (2016) "On the distribution of squarefree numbers", Journal of Number Theory. 159: 202-222. doi:10.1016/j.jnt.2015.07.013

$$Q \left(x\right) = \frac{6}{{\pi}^{2}} x + O \left({x}^{11/35 + \epsilon}\right)$$

will still result in $O \left(x\right)$.
Comment: Yes, the big problem here is the sum $\sum \lvert \mu(a)\rvert \{\sqrt{N/a}\}^2$. Only after that can be evaluated (as $c\cdot N$ plus a lower order error term) we might be able to use sharper estimates for $Q(x)$ to get a better overall error term. But I don't know if we can get that sum under control at all.
Comment: @LorenzHMenke A different angle of attack yields the linear term too, with an $O(N^{2/3}\log N)$ error term. Getting the error term closer to $O(\sqrt{N})$ is probably difficult.


Keep entity object after getTitle() method in render() method in a custom controller

Question: I've setup my controller like this:
my_controller:
  path: '/controller/{id}'
  defaults:
    _controller: '\Drupal\Mymodule\Controller\MyController::Render'
    _title_callback: '\Drupal\Mymodule\Controller\MyController::getTitle'
  requirements:
    permission: 'access content'

Now, in my MyController.php I need to load an entity object (for which {id} is the id). Unfortunately, it seems I need to load my object twice.
First, in my getTitle function because this is called first. Then again in my Render() function, which is called afterwards.
When I try to save the entity to my class as $this->entity It gets lost when the Render() function gets called. I tried to load via a constructor or create function, but these do not have access to the {id} slug in a proper manner.
What's the good practice way to make sure I need to load my entity object once for both functions?
Answer:
When I try to save the entity to my class as $this->entity It gets
lost when the Render() function gets called.

Use a service. Services are singletons and when the class resolver finds an existing service it loads the instance from the container and doesn't create a new class instance:
defaults:
  _controller: 'my_service:render'
  _title_callback: 'my_service:getTitle'

For good practice this is not really necessary in this case because entities are cached in memory automatically and loading an entity twice doesn't have a performance impact.
Good practice, though, would be to load the entity via route parameter upcasting:
foobar.view:
  path: '/foobar/{foobar_placeholder}'
  defaults:
    _controller: '\Drupal\foobar\Controller\Foobar::content'
    _title: 'Oh yeah foobar'
  options:
    parameters:
      foobar_placeholder:
        type: entity:foobar

Example from https://www.drupal.org/docs/8/api/routing-system/parameters-in-routes/parameter-upcasting-in-routes
Comment: Thank you, route parameter upcasting seems a nice option because it already includes security on the slug.


pg_basebackup: could not get WAL end position from server

Question: I want to create a Base Backup with the pg_basebackup tool of postgres 9.1. After some seconds I receive:
``pg_basebackup: could not get WAL end position from server`
What can be the reason for this error message?
Comment: `pg_basebackup`is relatively new (just came into existence in January) and not particularly well-documented - you may get a faster answer asking on the Postgres mailing lists (and when you get an answer, post it here for free upvotes :)
Answer: The problem was that there were files in the data directory not owned by the postgres user.
Comment: how did you figure this out dude? This post saved me a ton of headaches but the error message gave no clue to the issue