Returning value from Parent-Child tasks

Question: In the snippet below, a task creates two child tasks using the TaskCreationOptions.AttachedToParent, which means the parent task will wait for the child tasks to finish.
Question is - why doesn't the parent task return correct value [102]? Does it first determine its return value and then wait for the child tasks to finish. If so, then what is the point of creating parent-child relationship?
void Main()
{
Console.WriteLine ("Main start.");
int i = 100;

Task<int> t1 = new Task<int>(()=>
{
    Console.WriteLine ("In parent start");
    Task c1 = Task.Factory.StartNew(() => {
        Thread.Sleep(1000);
        Interlocked.Increment(ref i);
        Console.WriteLine ("In child 1:" + i);
    }, TaskCreationOptions.AttachedToParent);

    Task c2 = Task.Factory.StartNew(() => {
        Thread.Sleep(2000);
        Interlocked.Increment(ref i);
        Console.WriteLine ("In child 2:" + i);
    }, TaskCreationOptions.AttachedToParent );

    Console.WriteLine ("In parent end");
    return i;
});

t1.Start();
Console.WriteLine ("Calling Result.");
Console.WriteLine (t1.Result);
Console.WriteLine ("Main end.");
}

The output:
Main start.
Calling Result.
In parent start
In parent end
In child 1:101
In child 2:102
100
Main end.

Answer: The problem is that you create c1 and c2 as separate tasks but then return i immediately from t1 before c1 and c2 have incremented i.
Thus, the return value from t1 is captured at that point, and is still 100.
As you noted, there isn't much point in a parent/child relationship for this arrangement; but there are plenty of cases where it does make sense.
A common use is just so that the parent task does not complete until its child tasks have completed, but if you require the parent task to wait for its children before returning a value, you will not be able to do it like this.
Of course, you can fix it by adding
Task.WaitAll(c1, c2);

just before the return i;. I know it's not what you're asking, but I just wanted to point that out anyway.
Comment: Yes waiting and returning value are different things, but why not wait before deciding upon the return value. Do you think this could be a bug in TPL?
Comment: @thewpfguy No, it's definitely not a bug. It's a natural consequence of the fact that the result of a task is cached if it exits before the task that started the first task accesses the first task's return value.
Answer: As already said the value of i is returned before it's incremented. changing your code in this way it return the expected value (102):
void Main()
{
    Console.WriteLine ("Main start.");
    int i = 100;

    Task<int> t1 = new Task<int>(()=>
    {

    Console.WriteLine ("In parent start");
    Task c1 = Task.Factory.StartNew(() => {
        Interlocked.Increment(ref i);
        Console.WriteLine ("In child 1:" + i);
    }, TaskCreationOptions.AttachedToParent);

    Thread.Sleep(1000);

    Task c2 = Task.Factory.StartNew(() => {
        Interlocked.Increment(ref i);
        Console.WriteLine ("In child 2:" + i);
    }, TaskCreationOptions.AttachedToParent );

    Thread.Sleep(1000);

    Console.WriteLine ("In parent end");
    return i;
});

t1.Start();
Console.WriteLine ("Calling Result.");
Console.WriteLine (t1.Result);
Console.WriteLine ("Main end.");
 }

what I did is simply take out the Thread.Sleep(1000) from the child task to the parent task. The result is returned now after the variable has been incremented.
Comment: This is terrible answer IMO. Sleeps were added into child tasks to simulate long work. Moving them to main task is poor man's synchronization mechanism. Sure it will work, unless child taks will take more than 1000ms to complete. Given that we have many, many ways to synchronize data in .NET - using sleep to do this is the worst possible solution you should never use
Comment: @Pako I agree with you but I just wanted to point out why the int variable wasn't being updated at the end of the two tasks.

Returning value from Parent-Child tasks


C++ int vs long long in 64 bit machine

Question: My computer has 64 bit processor and when I look for sizeof(int), sizeof(long), and sizeof(long long), it turns out that int and long are 32 bits, and long long is 64 bit. I researched the reason, and it appears that popular assumption telling that int in C++ fits machine's word size is wrong. As I understood it is up to compiler to define what will be the size, and mine is Mingw-w64. The reason for my research was understanding that if the usage of types smaller than word size is beneficial for speed(for instance, short vs int) or if it has negative effect. In 32 bit system, one popular opinion is: due to the fact that word size is int, short will be converted into int and it would cause additional bit shifts and etc, thus leading to worse performance. Opposing opinion is that there will be benefit in cache level(I didn't go deep into it), and using short would be usefull for virtual memory economy. So, in addition to confusion between this dilemma, I also face another problem. My system is 64 bit, and it doesn't matter if I use int or short , it still will be less than the word size, and I start thinking that wouldn't it be efficient to use 64 bit long long because it is at the level the system is designed to. Also I read that there is another constraint, which is library(ILP64, LP64) of OS that defines the type sizes. In ILP64 default int is 64 bit in contrast to LP64, would it speed up the program if I use OS with ILP64 support? Once I started to ask which type should I use for speeding up my C++ program,  I faced more deep topics in which I have no expertise and some explanations seems to contradict to each other. Can you please explain:
1) If it is best  practice to use long long in x64 for achieving maximum performance even for for 1-4 byte data?
2) Trade-off in using a type less than word size(memory win vs additional operations)
3) Does a x64 computer where word&int size is 64 bits, has possibility of processing a short, using 16 bit word size by using so called backward compatibility? Or it must put the 16bit file into 64 bit file, and the fact that it can be done defines the system as backward compatible.
4) Can we force the compiler to make the int 64 bit?
5) How to incorporate ILP64 into PC that uses LP64?
6) What are possible problems of using code adapted to above issues with other compilers, OS's, and architectures(32 bit processor)?
Comment: Don't ever rely on standard data types to have a specific size. C++11 has [fixed-width integer types](http://en.cppreference.com/w/cpp/types/integer) for this. (Before C++11 there were compiler specific types for that)
Comment: You could have a 512 bit CPU and a 16 bit `int` would still be 100% standard compliant. Why anyone would do this is beyond me, but it's still legal.
Answer: 1) If it is best practice to use long long in x64 for achieving maximum performance even for for 1-4 byte data?
No- and it will probably in fact make your performance worse. For example, if you use 64-bit integers where you could have gotten away with 32-bit integers then you have just doubled the amount of data that must be sent between the processor and memory and the memory is orders of magnitude slower. All of your caches and memory buses will crap out twice as fast.
2) Trade-off in using a type less than word size(memory win vs additional operations)
Generally, the dominant driver of performance in a modern machine is going to be how much data needs to be stored in order to run a program. You are going to see significant performance cliffs once the working set size of your program exceeds the capacity of your registers, L1 cache, L2 cache, L3 cache, and RAM, in that order.
In addition, using a smaller data type can be a win if your compiler is smart enough to figure out how to use your processor's vector instructions (aka SSE instructions). Modern vector processing units are smart enough to cram eight 16-bit short integers into the same space as two 64-bit long long integers, so you can do four times as many operations at once.
3) Does a x64 computer where word&int size is 64 bits, has possibility of processing a short, using 16 bit word size by using so called backward compatibility? Or it must put the 16bit file into 64 bit file, and the fact that it can be done defines the system as backward compatible.
I'm not sure what you're asking here. In general, 64-bit machines are capable of executing 32-bit and 16-bit executable files because those earlier executable files use a subset of the 64-bit machine's potential.
Hardware instruction sets are generally backwards compatible, meaning that processor designers tend to add capabilities, but rarely if ever remove capabilities.
4) Can we force the compiler to make the int 64 bit?
There are fairly standard extensions for all compilers that allow you to work with fixed-bit-size data. For example, the header file stdint.h declares types such as int64_t, uint64_t, etc.
5) How to incorporate ILP64 into PC that uses LP64?
https://software.intel.com/en-us/node/528682
6) What are possible problems of using code adapted to above issues with other compilers, OS's, and architectures(32 bit processor)?
Generally the compilers and systems are smart enough to figure out how to execute your code on any given system. However, 32-bit processors are going to have to do extra work to operate on 64-bit data. In other words, correctness should not be an issue, but performance will be.
But it's generally the case that if performance is really critical to you, then you need to program for a specific architecture and platform anyway.
Clarification Request: Thanks alot! I wanted to clarify question no:1. You say that it is bad for memory. Lets take an example of 32 bit int. When you send it to memory, because it is 64 bit system, for a desired integer 0xee ee ee ee, when we send it won't it become 0x ee ee ee ee+ 32 other bits? How can a processor send 32 bits when the word size is 64 bits? 32 bits are the desired values, but won't it be combined with 32 unused bits and sent this way? If my assumption is true, then there is no difference for memory.
There are two things to discuss here.
First, the situation you discuss does not occur. A processor does not need to "promote" a 32-bit value into a 64-bit value in order to use it appropriately. This is because modern processors have different accessing modes that are capable of dealing with different size data appropriately.
For example, a 64-bit Intel processor has a 64-bit register named RAX. However, this same register can be used in 32-bit mode by referring to it as EAX, and even in 16-bit and 8-bit modes. I stole a diagram from here:
x86_64 registers rax/eax/ax/al overwriting full register contents
1122334455667788
================ rax (64 bits)
        ======== eax (32 bits)
            ====  ax (16 bits)
            ==    ah (8 bits)
              ==  al (8 bits)

Between the compiler and assembler, the correct code is generated so that a 32-bit value is handled appropriately.
Second, when we're talking about memory overhead and performance we should be more specific. Modern memory systems are composed of a disk, then main memory (RAM) and typically two or three caches (e.g. L3, L2, and L1). The smallest quantity of data that can be addressed on the disk is called a page, and page sizes are usually 4096 bytes (though they don't have to be). Then, the smallest quantity of data that can be addressed in memory is called a cache line, which is usually much larger than 32 or 64 bits. On my computer the cache line size is 64 bytes. The processor is the only place where data is actually transferred and addressed at the word level and below.
So if you want to change one 64-bit word in a file that resides on disk, then, on my computer, this actually requires that you load 4096 bytes from the disk into memory, and then 64 bytes from memory into the L3, L2, and L1 caches, and then the processor takes a single 64-bit word from the L1 cache.
The result is that the word size means nothing for memory bandwidth. However, you can fit 16 of those 32-bit integers in the same space you can pack 8 of those 64-bit integers. Or you could even fit 32 16-bit values or 64 8-bit values in the same space. If your program uses a lot of different data values you can significantly improve performance by using the smallest data type necessary.
Comment: The effort that went into this answer far exceeds the effort that went into the question.  Bravo.
Comment: Perhaps it was a complicated question that required a lot of information to be complete.
Comment: What I still do not understand is why do people generally recommend using 4-byte integers over 2-byte short integers with a justification of 4-byte being the processor's natural word size and thus generally produces the most optimal performance. However you state above that in today's world of 64-bit processors you do not want to use an 8-byte integer everywhere. How do you reconcile these two notions? Note here I am talking purely about performance not memory footprint.
Comment: @UserRR You have a 64 bit register and a 64 bit bus, but the cache and RAM are still just rows of bits. If you specify 32 bits, you use 32 bits.
Comment: @SiddharthaGandhi I don't think it is generally recommended to use 4-byte integers. It just so happens to be the default integer size for many compilers. For programs with just a few variables the considerations in this answer are not relevant. However programmers who work with large amounts of data absolutely do care about and select the appropriate size numeric types for their application. I think 4-byte variables are common because 2-byte variables tend to be too small for many purposes, having a limit of 0-65,000 or -32,000-32,000 depending on signedness.
Comment: E.g. here: https://stackoverflow.com/a/5069643/5787296 or https://stackoverflow.com/a/24371078/5787296 . I'm asking from the vantage point of a developer whose applications are performance-sensitive first and foremost and where absolute memory consumption is generally immaterial (editorializing for a second, I don't think my perspective is unique in that regard).
Comment: Neither of those links say that you should use 4-byte integers, they say that the choice between 2-byte and 4-byte integers is mostly immaterial *except if it makes a difference in memory performance*. Absolute memory consumption is virtually never immaterial, as memory use is the dominating factor when it comes to performance on modern systems. There is virtually no difference in the time it takes a processor to operate on different size integers as long as the size is less than the word size. Any program using more than 64 bytes of data will incur some cache penalty on a modern system.
Comment: Thanks alot! I wanted to clarify question no:1. You say that it is bad for memory. Lets take an example of 32 bit int. When you send it to memory, because it is 64 bit system, for a desired integer 0xee ee ee ee, when we send it won't it become 0x ee ee ee ee+ 32 other bits? How can a processor send 32 bits when the word size is 64 bits? 32 bits are the desired values, but won't it be combined with 32 unused bits and sent this way? If my assumption is true, then there is no difference for memory.
Comment: > "...then you have just doubled the amount of data that must be sent between the processor and memory..."

But the data is sent in parallel, what's the difference then? It still is done in one tact so there's no difference. Or I'm wrong?
Comment: @HeilProgrammierung If you're sending a lot of data then it matters. More than a cache line of data incurs multiple memory transfers to the CPU.

C++ int vs long long in 64 bit machine


blocked DNS traffic on GCE?

Question: I am trying to setup DNS server on an Ubuntu 14.04 VM on Google Compute Engine. It looks like queries to the DNS server are not reaching the VM even though  I have allowed TCP/UDP traffic on port 53 on the firewall.
To verify that it is not a DNS configuration error, I have also used the following to verify that the traffic is really blocked.

tcpdump
'sudo nc -ul 53' on the server

As DNS is not included in the officially blocked network traffic in GCE I am trying to understand what I am doing wrong.
Here is also an unanswered question with similar problem: Google Compute VM's as DNS servers not working.
Thanks,
Gobexe
Comment: It turns out that I made a mistake when writing the firewall rules, specifically, I was using a non-existing tag in the rule that was supposed to allow DNS traffic.
Answer: DNS primarily uses UDP on port number 53 to serve requests. Opening this port and protocol on the GCE firewall should let your DNS server respond to the external queries.
Make sure your DNS server is configured properly and try to use allow-query in options configuration file of the BIND (/etc/bind/named.conf.options) to accept the queries from your trusted source IP addresses.

blocked DNS traffic on GCE?


Elaborating some points in the proof of Proposition 3 on pg.100 "Royden and Fitzpatrick 4th edition"

Question: The proposition and its proof are given below:

My questions are:
1- why is $|f_{n} - f| < \eta$ and not $<\epsilon$?
2- why is $\{x \in E | |f_{n} - f(x)| > \eta \} \subseteq (E \setminus F)$?
Could anyone help me in finding answers to those questions, please?
Comment: 1. is just a definition. For 2. read the the line $|f_n-f|<\eta$ on $F$ for all $n\geq N$. So for $n\geq N$ and $y\in F$ we can't have $|f_n(y)-f(y)|\geq \eta$. So any $x\in E$ with $|f_n(x)-f(x)|<\eta$ implies $x$ is outside of $F$.
Comment: @S.D. 1. why $\eta$ and not $\epsilon$?
Comment: I do not know @S.D.
Comment: I think in your first comment in the last line the second inequality should be reversed
Comment: Yeah you are right.
Comment: No, you are not right, the first one should be $\eta$
Comment: So please state your definition of convergence in measure??
Comment: it is stated in the picture in $(4)$@S.D.
Answer: I think a confusing thing here is that, technically, there should be written

$|f_n - f| \color{blue}{\leq} \eta$ on $F$ for all $n \geq N$

if one takes the definition of convergence in measure as given in the book:
For all $\eta > 0$ we have

$\lim_{n\to\infty}m\left(\{x \in E\, | \, |f_n(x) - f(x)| \color{blue}{>} \eta\}\right) = 0$

Now, fix an arbitrary $\eta>0$. Hence, to show is that for any $\epsilon > 0$ we have $m\left(\{x \in E\, | \, |f_n(x) - f(x)| > \eta\}\right) < \epsilon$ for all $n \geq N$.
So, let $\epsilon >0$. Then Egorov's theorem assures that $f_n \Rightarrow f$ on an $\pmb{F \subseteq E}$ such that $m(E\setminus F) < \epsilon$. So, there is an $N$ such that $|f_n(x) - f(x)| \color{blue}{\leq} \eta$ for $n \geq N$ on $\pmb{F}$. This in turn means, that
$\{x \in E\, | \, |f_n(x) - f(x)| > \eta\} \subseteq E\setminus F$ for all $\pmb{n \geq N}$.
Hence $m\left(\{x \in E\, | \, |f_n(x) - f(x)| > \eta\}\right) \leq m(E \setminus F) < \epsilon$ for all $n \geq N$.


Regression model analysis

Question:
I have computed some regression data for the two variables I am investigating, I am trying to prove there is a positive relationship between the 2 variables. My first question is why is it that only the data for the independent variable is shown; what about the dependent variable?
My second question is why is the data 'Lower 95%' and 'Upper 95%' repeated twice?
Comment: You've got good advice. If you need more, show us the data. Regardless of that, you appear to be puzzled about the results, and plotting the data are the best way to understand them.
Answer: The regression you are doing aims to show the effect of the independent variable on the dependent variable.  Your dependent variable "depends" on the independent variable, and it shouldn't make sense to test the opposite effect.  For example, imagine your dependent variable was plant growth, and your independent variable was hours of sunshine per day.  It makes sense to test for an effect of the hours of sunshine on crop growth.  However, it would make no sense to test the opposite effect - crop growth couldn't possibly have an effect on the amount of sunshine.  If your two variables are completely unrelated you might be better doing a correlation rather than a regression.
I'm not sure why Excel shows the 95% upper and lower bounds twice. If you had chosen a different confidence interval earlier (e.g. 99%) when specifying the model, it would display the 95% and 99% intervals instead of showing 95% twice.
Comment: Thank you for the reply. I have just run my regression again and noticed that now both my upper and lower confidence intervals are negative. What can I deduce from this information? Isn't the higher the confidence interval, the stronger the relationship ? Does this mean a negative relationship is shown?
Comment: If your upper and lower confidence intervals are both negative, this suggests a significant effect of the independent variable on the dependent variable - is your p-value below 0.05?  Look at the coefficient to see the direction of the effect - a negative value would indicate a negative relationship.
Answer: Peter Flom's answer as above is on-point. On your follow up, the confidence interval (CI) gives you two important pieces of information: the estimate of effect and the precision of your estimate.
1) Effect estimate: Your CI has a lower bound of -1.356 and an upper bound of 0.234. A negative number indicates a negative relationship and a positive one indicates a positive one. Note that your CI includes the value 0, which would suggest no relationship.
2) Precision: how "wide" your estimate is, i.e. The distance between upper and lower bounds. If the distance is small or narrow, the estimate is quite precise. A wide estimate including 0 such as yours suggest the data are very scattered, and we can't get enough information from your sample to indicate that a true relationship exists.
A clue to interpretation exists in the p-value. When we run the statistical test on your data, we are calculating the likelihood we would see the pattern shown by your exact data in "infinite" imaginary samples if there were truly no association between the dependent and independent variables.
Unfortunately your p-value is fairly high, and together with your confidence interval suggest that there is no evidence for a relationship when just comparing these two variables with your data. If anything, there is (very very weak) evidence suggesting there could be a negative relationship, but really we don't know.
A CI and p-value go hand-in-hand. The narrower a CI and the farther it is from 0, the lower the p-value.
Your intercept tests give a very different set of stats, but unfortunately in a linear regression this isn't the main item of interest: the value of x1 is the key item, and if anything the data suggest your independent y variable may clustered around a certain value for reasons unrelated to the dependent variable.
Comment: Helpful (+1). In your last paragraph I think you mean "the value of **_the coefficient of_**  x1"
Comment: If @Peter Flom posted anything here, it's now disappeared.
Comment: Yes, thanks for noting linguistic sloppiness wrt algebra:  the coefficient of x1, ie -0.56, is the important bit of info for the OP.

Regression model analysis


An integral problem:

Question: I am working on a proof in lecture notes, but there are two steps that I couldn't understand
The first one: $$\int_1^n \frac{n-\lfloor x \rfloor}{x} dx=\int_1^n \frac{n+\frac{1}{2}+(\{x\}-\frac{1}{2})-x }{x} dx$$
Edit: Knowing that $\{x\}=x-\lfloor x\rfloor\quad$ , I am done with this part.
The second:
$$\int_1^n \frac{\{x\}-\frac{1}{2}}{x} dx=\int_1^\infty \frac{\{x\}-\frac{1}{2}}{x}dx+o(1),$$ since $$\int_1^\infty \frac{\{x\}-\frac{1}{2}}{x}dx \qquad (*) $$can be written as an alternating sum which is convergent so that its tail will converge to $0$.
I couldn't understand how they get from one equation to another and also the last part that says $(*)$ can be written as an alternating sum which is convergent so that its tail will converge to $0$
Actually in the second part, I don't know what is the meaning/use of $\{x\}$
Comment: just to be certain was $\{x\}$ defined as something specific?
Comment: This is what I want to know :)
Comment: given that the two are equal it makes sense to suggest that

$$\lfloor x \rfloor = x-\{x\}$$

on that note, (and i understand this is a shot in the dark so i apologise if this is unhelpful).

$\{x\}$ would make sense to be the difference between the floor of x and x itself.. as
$$\lfloor x \rfloor + \{x\} = x$$

 but again a stab in the dark and may be unhelpful; in which case sorry.
Comment: @Vaas Indeed, $\{ x\}$ is a common notation for the fractional part of $x$, i.e. $\{ x\} := x - \lfloor x\rfloor$. And that's how it is used here.
Comment: ahhh i see so the first set of equations is then a rewriting by adding zero and expressing the floor as the difference between x and the fractional part of x..
Answer: That
\begin{equation*}
\int_1^n \frac{\{x\} - \frac{1}{2}}{x} \,dx = \int_1^\infty \frac{\{x\} - \frac{1}{2}}{x} \, dx + o(1)
\end{equation*}
will follow from
\begin{equation*}
\int_n^\infty \frac{\{x\} - \frac{1}{2}}{x} \, dx = o(1).
\end{equation*}
This is equivalent to
\begin{equation*}
\lim_{n \to \infty} \int_n^\infty \frac{\{x\} - \frac{1}{2}}{x} \, dx = 0
\end{equation*}
which will follow from the fact that
\begin{equation*}
\int_1^\infty \frac{\{x\} - \frac{1}{2}}{x} \, dx
\end{equation*}
is convergent.
The idea is that we should partition the integral into an alternating sum. Recall that an alternating sum $\sum_n (-1)^n a_n$ is convergent if and only if $a_n \to 0$ as $n \to \infty$. After some thinking one realises that the partition to use is $1, 1 + \frac{1}{2}, 2, 2 + \frac{1}{2}, \ldots$. Indeed, for a natural $k$,
\begin{equation*}
\int_k^{k + \frac{1}{2}} \frac{\{x\} - \frac{1}{2}}{x} \, dx
= \int_k^{k + \frac{1}{2}} \frac{x - k - \frac{1}{2}}{x} \, dx < 0
\end{equation*}
and
\begin{equation*}
\int_{k + \frac{1}{2}}^{k + 1} \frac{\{x\} - \frac{1}{2}}{x} \, dx
= \int_{k + \frac{1}{2}}^{k + 1} \frac{x - k - \frac{1}{2}}{x} \, dx > 0.
\end{equation*}
What is left is to see that the terms of the sum converges to 0. I am lazy and will only do the positive ones. Well,
\begin{align*}
\int_{k + \frac{1}{2}}^{k + 1} \frac{x - k - \frac{1}{2}}{x} \, dx
&= \int_{k + \frac{1}{2}}^{k + 1} 1 - (k + \frac{1}{2}) \frac{1}{x} \, dx \\
&= \frac{1}{2} - (k + \frac{1}{2}) (\log(k + 1) - \log(k + \frac{1}{2})
\end{align*}
which converges to 0 as $k \to \infty$. This feels a bit cumbersome but it involved alternative sums so it was probably what was intended.

An integral problem:


Does app server instance require restart, if we update DB

Question: DB content/rows are updated. Does any appserver need a restart to reflect the rows in appserver instances.
Answer: From my experience, the answer is - it depends.
If you have 2 servers (cloned) and 2 DBs (cloned) and a gateway to somehow maintain exactly the same data without any discrepancies in both the DBs (refer to this https://docs.oracle.com/goldengate/c1230/gg-winux/GGCON/introduction-oracle-goldengate.htm#GGCON-GUID-EF513E68-4237-4CB3-98B3-2E203A68CBD4), then you don't need to restart the application server when DBs are being updated one at a time. But when one of the DBs is being restarted, we need to make sure that all the DB traffic goes to the other one. And vice versa.
Now, coming to whether the DB changes will get reflected in the application server, they will. But the schemas and all must suit. Also, you may have to fetch the cache from DB again (if you have some scheduled (or manual) cache fetching mechanism, it might be fine).


borel-measurable function is pointwise limit of a sequence of continuous functions, which is uniformly bounded

Question: Let $H$ be a Hilbert space over $\mathbb{C}$, $A\in L(H)$ ( $A:H\to H$ is linear and continuous) and let $A$ be self-adjoint. Consider the spectrum of A, $\sigma(A)$ and $f:K\to \mathbb{K}$ a bounded, borel-measurable function on a compact subset $K\subseteq \mathbb{R}$. Is $f$ a pointwise limit of a sequence $(f_n)\subseteq C(\sigma(A))$, which satisfies $\sup_{n\in\mathbb{N}}\|f_n\|<\infty$?
We used this many times in lecture but I never had measure theory, this is not a homework. I would like to know a sketch of the proof. Could you give me a reference or a name, if this is a Theorem with a name?
Comment: This is not correct. $\chi_{\Bbb{Q} \cap [0,1]}$ for $\sigma(A) = [0,1]$ is a counterexample. But what is correct is that if $\mathcal{F}$ is a class of measurable functions which contains the continuous functions and is closed under the form of convergence you describe (i.e. if $f_n \in \mathcal{F}$ with $f_n \to f$ pointwise and $\Vert f_n \Vert \leq C$, then $f \in \mathcal{F}$), then $\mathcal{F}$ is the class of all Borel measurable bounded functions.
Comment: This might also be interesting to you http://math.stackexchange.com/questions/549135/construction-of-a-function-which-is-not-the-pointwise-limit-of-a-sequence-of-con.
Comment: @Alex: What I have written above is a consequence of a fact that Nate Eldredge calls "Dynkins multiplicative system theorem", see http://math.stackexchange.com/questions/47507/if-int-0-infty-e-lambda-tft-rm-dt-0-for-all-lambda-0-then-f. Using that fact, it will follow that $\mathcal {F} $ contains all bounded functions that are measurable with respect to $\sigma (C_b) $ (the sigma algebra generated by all bounded continuous functions). In many cases (e.g. for separable metrizable spaces), this will be the Borel sigma algebra, but not always.
Comment: @Jan: Yes, almost everywhere convergence is not a problem. Just note that if $f$ is bounded on a compact set, then it is integrable. It is well-known that the set of continuous functions is dense in the set of integrable functions, so that $\|f-f_n\|_{L^1} \to 0$ for some sequence $(f_n)_n$ of continuous functions. It is well-known that this implies $f_{n_k} \to f$ almost everywhere for some subsequence. The issue here is really the *pointwise* convergence *everywhere*, which does not hold in general.
Comment: oh ok thank you. Then it is a mistake made in lecture but I know this statement what you mention which is correct.
Comment: yes, it is helpful, thank you
Comment: Meanwhile I have understand it. Additional to your link, helpful is http://mathoverflow.net/questions/31271/approximation-with-continuous-functions and http://en.wikipedia.org/wiki/Nowhere_continuous_function
Comment: @PhoemueX: Your statement about the class $\mathcal F$ seems to be exactly what I was looking for (for my own work). Do you know whether local-compactness of the underlying space is needed? Could you please provide a source for a general statement?
Comment: @PhoemueX We are discussing your statement about the class $\mathcal{F}$ in our course and really think that we don't get it right. Why does this result does not imply the result which OP asks for, at least almost everywhere? I understand your counterexample, but would the statement of OP become true if you add "almost everywhere"? Adding "almost everywhere" seems to kill the counterexample. It would be great if you could help us.
Answer: As PhoemueX in the comments said, the Dirichlet function is a counterexample which is in Baire class 2, but not in Baire class 1 because the function is nowhere continuous and class 1 functions can only be discontinuous on a meagre set.


Showing a structure is minimal

Question: Underlying set of structure is $\mathbb{N}$ , our language only have (<) interpreted as usual order.  Why is this minimal? (Every definable subset finite or cofinite)
I try to show with contradiction that we have set not of this form (so definable set which is infinite but not cofinite) but I cannot see how to describe this with only this order relation <. I first try to see without quantifiers, which is not hard, but when quantifiers come I begin to have troubles. Please give advise.
Answer: First, remark that $\omega \equiv \omega +\zeta$ where $\zeta$ is the order type of the integers $\mathbb{Z}$. The language is always $<$, I omit it in notion. Next since the automorphism group of $\omega+\zeta$ is transitive on $\zeta$ any definable subset of $\omega+\zeta$ must either contain or be disjoint from $\zeta$.
Now assume that $\varphi(x)$ defines an infinite, coinfinite subset of $\omega$ then
$$\omega \vDash \forall x\exists y (x<y\wedge \varphi(y))$$
$$\omega \vDash \forall x\exists y (x<y\wedge \neg\varphi(y))$$ and thus $\omega+\zeta$ must satisfy the same formulas. This contradicts the earlier remark about the subset of $\omega+\zeta$ defined by $\varphi$.
Comment: $n\mapsto n+k$ is an automorphism of the $\zeta$ part. I dont understand the second question.
Comment: The formulas say that $\varphi$ and $\neg \varphi$ are cofinal. This cannot happen in $\omega+\zeta$ since the auto group is transitive on $\zeta$, thus it cannot happen on $\omega$ since the structures are elementarily equivalent.
Comment: Just a few things? How do you know that automorphism group will be transitive, and also please explain the formulas you defined for the ordinal ω? Thanks.
Comment: I understand use of $\phi $  but not of the conditions in the quantifier scope. Why is it necessary to  intersect with the condition that $x<y$? They are stating that , given $x$, we have bigger $y$ that satisfies $\phi$, and then that we have a bigger $y$ that does not satisfy $\phi$. But if $\omega + \zeta $ satisfying these formulas, then we can not have both infinite and coinfinite, but I do not see how to reach conclusion?
Comment: I probably should not have done that so soon, and asked you first. I thought I understand the process of what you did, but now I analyse it myself and try to do it in detail, and see I don't know completely. Sorry.

Showing a structure is minimal


"Target KMS key" error when trying to enable backup replication on an AWS RDS instance

Question: I created an RDS database instance using the AWS user interface, and I made sure that the "Backup replication" feature was enabled and I specified the ARN for a symmetric multi-region KMS key that I created for "encrypt and decrypt" usage. The database creation seemed to go successfully.
To my surprise, when I tried to modify the RDS instance, the "Enable replication in another AWS Region" box was still unchecked. So I checked the box, and entered the same ARN for the KMS key again. When I tried to submit the modification, I got the following error:

We're sorry, your request to modify DB instance rds-mysql-dev-1 has failed.
The target KMS key [arn:aws:kms:us-west-2:REDACTED:key/mrk-REDACTED] does not exist, is not enabled or you do not have permissions to access it.

I double checked the KMS key and it is enabled. I thought perhaps I needed to add a role to the "Key users" section, so I added the AWSServiceRoleForRDS role. That seems like the only one that makes sense to be added. However, when I tried to modify the RDS instance again to enable backup replication with the same ARN, I got the same error as above.
Any idea what may be causing the error?
Comment: If the key exists, and is enabled, maybe you don't have access to the key in the other region. Check its key policy carefully, and check your IAM / SCP policies to see if they allow that use as well.
Comment: This AWS guide to RDS cross-region backups doesn't mention multi-region keys. Perhaps you should try the simpler solution. https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_ReplicateBackups.html
Comment: Further reading, out of interest, finds that "You can use multi-Region keys with client-side encryption libraries, such as the AWS Encryption SDK, the DynamoDB Encryption Client, and Amazon S3 client-side encryption.". Suggest you follow the link above to AWS recommended process for RDS multi-region backups.  https://docs.aws.amazon.com/kms/latest/developerguide/multi-region-keys-overview.html
Comment: I found out that the key must be created in the region to which the backups are replicated. You cannot use a key that was created in the originating region.
Comment: Yep, KMS keys only exist in one region. Even the multi-region keys only really exist in one region. Suggest you answer your own question, so people don't come in to try to help, and to help others who have a similar problem in future :)
Answer: So the problem is that I had created my symmetric multi-region KMS key in the same region in which I was creating the RDS database.
The KMS key needs to be created in the region where the backups will be replicated to. After I created a symmetric single-region KMS key in that region, I was able to modify the RDS instance to enable backup replication, and it also allowed me to select the new KMS key in the destination region. (Previously it only allowed me to enter an ARN because the destination region did not have any KMS keys defined.)


Can't access local apache virtualhost using Chrome

Question: I have a few virtualhosts on my localhost apache, here's the config of one (the others just change the letters before .localhost)
<VirtualHost *:80>
    ServerAdmin email@example.com
    ServerName dev.localhost
    ServerAlias dev.localhost
    DocumentRoot /var/www/dev
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

It doesn't work on Chrome. Works on other browsers and works when I type this on terminal
sudo wget dev.localhost

When I access the virtualhost on Chrome, I get the error ERR_NAME_NOT_RESOLVED and if I'm not connected to the internet, I get ERR_INTERNET_DISCONNECTED (I think that's not supposed to happen once it's localhost)
I also tried to access the virtualhost with Chrome Private Mode (CTRL+SHIFT+N) and accessing like this I get the DNS_PROBE_FINISHED_NXDOMAIN error.
Tried to disable all my extensions too.
Everything I just said, it's only for virtualhosts because when I access localhost or any folder inside that, works fine.
This is my /etc/hosts file
127.0.0.1   localhost
127.0.1.1   Henrique-Note
127.0.0.1   bobbabr.localhost
127.0.0.1   art.localhost
127.0.0.1   id.localhost
127.0.0.1   artweb.localhost
127.0.0.1   pontoagua.localhost
127.0.0.1   ibobba.localhost
127.0.0.1   fnxhenry.localhost
127.0.0.1   pipoca.localhost
127.0.0.1   popbr.localhost
127.0.0.1   gentil.localhost
127.0.0.1   structure.localhost
127.0.0.1   thehits.localhost
127.0.0.1   dev.localhost

# The following lines are desirable for IPv6 capable hosts
::1             localhost
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

How can I fix this and access virtualhost on Chrome?
Chrome version: 43.0.2357.18 beta (64-bit)
OS: Elementary OS Freya, which is based on Ubuntu 14.04
Apache/2.4.7
Answer: Do you have a non-beta Chrome version you could try just in case it's a bug with Chrome?
Another thing you could try is to code those hostnames and configure the VirtualHosts to access the IP address of your machine instead of 127.0.0.1, even though in theory using 127.0.0.1 should be legitimate.
Comment: I installed Chrome stable and it's working now. But why virtualhosts can't be accessed on this beta? I tried to search Chromium for something like that but found nothing.


How to append a div inside a another div?

Question: I am a beginner to jQuery and I need to append the below div to this.element
var div = '

' +  this.getNeedlePointerValue() + this.model.properties.rightlabel + '

';
div.appendTo(this.element);

But exception thrown as

sourcefile.js:71 Uncaught TypeError: div.appendTo is not a function

How to add that my div effectively into the this.element?
Comment: `div` is a string and not a jQuery object -> [jQuery Learning Center](https://learn.jquery.com)
Comment: `$(div).appendTo(this.element);`
Comment: Wrapping a string etc in $() will make it a jQuery object.
Comment: Thanks @Zenoo and Steve
Answer: In your case div is a string variable so it doesn't contain any DOM or jQuery related functions. What you need is to convert that string to jQuery element first.
To create the jQuery object you need to use
$() function with a htmlString param:
$(div).appendTo(this.element);


In what way is the existential and universal quantifiers treated differently by the rules of $\forall$-introduction and $\exists$-introduction?

Question: In Natural Deduction in what way is the existential and universal quantifiers treated differently by the rules of $\forall$-introduction and $\exists$-introduction?
They look really similar in their definitions?:
$\forall$-introduction: $\frac{\frac{}{A(c)}}{\forall x A(x)}$ Provided that c is a new constant symbol
$\exists$-introduction: $\frac{A(t)}{\exists x A(x)}$
Comment: I think the major difference is, in $\forall$-introduction, you need to provide a subproof of $A(c)$ where $c$ is an introduced atomic symbol.  Whereas in $\exists$-introduction, $t$ can be any term in the language, including a complex term, for which the substitution $A(x)[x := t]$ can be proved.
Comment: $\exists$-intro: if we have proved (or assumed) that $A(t)$ holds, i.e. that some "object" named $t$ has "property" $A$, we can safely deduce that "there is something that is $A$". Please, note: proved or assumed: if we have assumed $A(t)$, obviously the conclusion: $\exists x \ A(x)$ holds under the same assumption.
Comment: $\forall$-intro: we can use also a variable $y$ in place of $c$, but the proviso still holds: it must be "new". This mean that there are no assumptions about $c$ or $y$ in the context of the proof. This fact formalize the intuitive understanding that $c$ or $y$ being "new" means that they are "generic". If so, we have that, if $A$ holds for a "generic object", then it holds for all.
Answer: Well, say $c$ is  a constant symbol. Then the rule tells you that you can conclude $\exists{x}A(x)$, but you certainly can't conclude $\forall{x}A(x)$.
For an example: Suppose you are working with real numbers and $A(0)$ is stating that $\forall{y}$ $0.y=0$ (i.e. multiply any number by $0$ and you get back $0$). So it is correct to conclude $\exists{x}\forall{y}$ $x.y=x$ (i.e. there is some number s.t. if you multiply any number with that number you get the initial number back). It is certainly not correct to conclude $\forall{x}\forall{y}$ $x.y=x$ (i.e. the product of any two numbers is equal to the first number.)
Edit 1: I'm not really clear on what you mean by "new", but I think the point that is being attempted is the following (suppose that the only constant symbols you have are $0,1$): Suppose that you assume $c$ is some arbitrary element and you manage to prove some thing about $c$, then you can conclude for all $x$. For example, suppose $c$ is non-zero number. Then you can prove $\exists{y}$ s.t. $c.y=1$. So you can conclude for all $x$, if $x$ is non-zero, then there is some $y$ s.t. $x.y=1$. However you have $1.1=1$. You can't conclude for all $x$, $x.x=x$. But you can conclude there is some $x$ for which $x.x=x$
Comment: If you parameterize the entailment judgement with a signature that specifies the constants (and their arities if necessary), then having a "new" or "fresh" constant can be simply described as extending the signature. I briefly describe this in the "The Signature" section of [this article](http://www.hedonisticlearning.com/posts/understanding-typing-judgments.html). With this perspective the rules for universal/existential introduction look like: $$\frac{\Gamma\vdash_{\Sigma,c}P(c)}{\Gamma\vdash_\Sigma\forall x.P(x)}\qquad\frac{\Gamma\vdash_\Sigma P(t)}{\Gamma\vdash_\Sigma\exists x.P(x)}$$
Comment: Sorry I updated my definition ( it was wrong )
Answer: The $\exists Intro$ has no restrictions, because as soon as something ($t$) has property $A$, then of course there is something that has property $A$. So, from $A(t)$ you can infer $\exists x A(x)$, no matter what $t$ is, or how $t$ came about.
The $\forall Intro$ works quite differently. You want to show that $\forall x A(x)$.  How can you show that?  Because $A(t)$ for some $t$? Clearly that is not enough ... You need to know that $A(t)$ is true for all $t$'s!  OK, so how do we show that? Well, if we assume that $c$ is some completely arbitrary object from our domain (and we do this by introducing a new symbol $c$, so we can ensure that we don't know anything about $c$ other than that it is some object from our domain), and we can show that $c$ has property $A$, then we can conclude that all objects have property $A$.
In short, if any object $c$ has property $A$ (i.e. If $A(c)$ for a newly introduced $c$), then all objects have property $A$ (I.e. then  $\forall x A(x)$)


Sum of $\{n\sqrt{2}\}$

Question: I would like to prove (rigorously, not intuitively) that
$$\sum_{n=1}^N \{n\sqrt{2}\}=\frac{N}{2}+\mathcal{O}(\sqrt{N})$$
where $\{\}$ is the "fractional part" function. I understand intuitively why this is true, and that's how I came up with this claim - $\{n\sqrt{2}\}$ behaves like a random variable uniformly distributed in $(0,1)$, and treating it as a random variable makes the sum analogous to a random walk, and the $\mathcal{O}(\sqrt{N})$ bound can be shown using expected values.
However, just saying that $\{n\sqrt{2}\}$ behaves "like a random variable" is highly non-rigorous. Can someone show me how to justify this in a more airtight way (ideally without using any theorems that require specialized background knowledge)?
Thanks!
Comment: I don't believe this is even true.  Certainly the integer part of $n\sqrt{2}$ is uniformly distributed, but the terms are not independent; I believe the error bound is more like $O(N/\sqrt{\log N})$.
Comment: @mjqxxxx Really? I see what you mean about non-independence thwarting my prediction... but if anything, it seems like it should be *tighter* than $\sqrt{N}$, since if $\{n\sqrt{2}\}$ is less than $1/2$, then $\{(n+1)\sqrt{2}\}$ is more likely to be greater than $1/2$, and vice versa, suggesting that it should "even out" to $1/2$ even more reliably than independent random variables.
Comment: @mjqxxxx Also, can you offer any (rigorous) methods for finding/proving a bound?
Comment: @frpzzd a bit of quick numerical work backs you up. I wouldn't be surprised if the error term is $O(1)$, although that may be overly optimistic.
Comment: I don't think that this can be done without specialized knowledge. You need to know about the nature of continued fraction expansion of $\sqrt 2$.
Comment: @i707107 Oh, I actually do happen to know a bit about continued fractions. Can you suggest a method?
Comment: I just wonder with it is possible to make progress on this using the fact that $\sqrt2$ and $2+\sqrt2$ yield a pair of complementary Beatty sequences? Basically the wish is to be able to estimate $\sum_{n=1}^N\lfloor n\sqrt2\rfloor$ accurately enough.
Comment: See [here](https://math.stackexchange.com/q/2052179/11619) for more.
Answer: Let $f$ be the function defined for a positive integer $N$ as follows:
$$
f(N)
=
\sum_{1\le n\le N}\{n\sqrt 2\}\ .
$$
We need a control of $f(N)-N/2$.
Here, i try to give arguments for the idea extracted from the following experimental computation:
Experiment:
We approximate $\sqrt 2$ by using continued fractions, for such a fixed approximation,
$P/Q$ say, we compute $f(Q)$ and $f(Q)-Q/2$. For the first few values of $Q$...
def f(N):
    return sum( [ RR(k*sqrt(2)).frac() for k in xrange(1, N+1) ] )

cf = continued_fraction( sqrt(2) )

# consider the first 15 "convergents" P/Q and compute f(Q) - Q/2 for them
for frac in cf.convergents()[:15]:
    P, Q = frac.numerator(), frac.denominator()
    print "sqrt(2) ~ %s :: Q = %s :: f(Q)-Q/2 ~ %s" % ( frac, Q, f(Q)-Q/2 )

Results:
sqrt(2) ~ 1 :: Q = 1 :: f(Q)-Q/2 ~ -0.0857864376269049
sqrt(2) ~ 3/2 :: Q = 2 :: f(Q)-Q/2 ~ 0.242640687119285
sqrt(2) ~ 7/5 :: Q = 5 :: f(Q)-Q/2 ~ -0.286796564403573
sqrt(2) ~ 17/12 :: Q = 12 :: f(Q)-Q/2 ~ 0.308657865101423
sqrt(2) ~ 41/29 :: Q = 29 :: f(Q)-Q/2 ~ -0.317100367703610
sqrt(2) ~ 99/70 :: Q = 70 :: f(Q)-Q/2 ~ 0.320702497141440
sqrt(2) ~ 239/169 :: Q = 169 :: f(Q)-Q/2 ~ -0.322176510488248
sqrt(2) ~ 577/408 :: Q = 408 :: f(Q)-Q/2 ~ 0.322790161566445
sqrt(2) ~ 1393/985 :: Q = 985 :: f(Q)-Q/2 ~ -0.323043813132131
sqrt(2) ~ 3363/2378 :: Q = 2378 :: f(Q)-Q/2 ~ 0.323148970494003
sqrt(2) ~ 8119/5741 :: Q = 5741 :: f(Q)-Q/2 ~ -0.323192510470562
sqrt(2) ~ 19601/13860 :: Q = 13860 :: f(Q)-Q/2 ~ 0.323210559656218
sqrt(2) ~ 47321/33461 :: Q = 33461 :: f(Q)-Q/2 ~ -0.323217967510573
sqrt(2) ~ 114243/80782 :: Q = 80782 :: f(Q)-Q/2 ~ 0.323221431812271
sqrt(2) ~ 275807/195025 :: Q = 195025 :: f(Q)-Q/2 ~ -0.323220559774200

So the difference is in these cases "surprisingly" in the interval $[-1,1]$.
The estimation:
Let us try to convert these observation into a proof.
Let $a$ be $\sqrt 2-1$, so $a\in(0,1/2)$.
(I need $a<1$ below.) We have $\{n\sqrt 2\}=\{na\}$.
Let $P/Q$ be a rational approximation of $a$, an irreducible fraction, namely one provided by the
continued fraction convergents of $a$, which form an "alternated sequence around $a$" $\dots P/Q, P'/Q', P''/Q'',\dots$,
and the difference between two consecutive convergents $P/Q$ and $P'/Q'$ is $\pm 1/(QQ')$.
In particular, $Q,Q'$ are relatively prime.
We will also use in the following the "next convergent" $P'/Q'$.
Fix some natural $n$ with $0<n<Q$. So $nP/Q<n$ is not an integer. (If $Q|(nP)$, then $Q|n$.)
Assume now there is an integer between $na$ and $nP/Q$.
Then the same integer is in the bigger interval between $nP'/Q'$ and $nP/Q$, which is of length
$1/(QQ')<1/Q$. But from $nPQ\not\in\Bbb N$ to the next integer is a distance of at least $1/Q$.
Contradiction.
Our assumption is false.
So $na$ and $nP/Q$ have the same floor.
Then
$$
\left\{na\right\}
=
\left\{n\frac PQ+n\left(a-\frac PQ\right)\right\}
$$
lies between the numbers
$$
\left\{n\frac PQ\right\}\pm \underbrace{\left\{n\left(a-\frac PQ\right)\right\}}_{<n/(QQ')}\ .
$$
Now let $n$ run in a set $S=S(Q)$ of $Q$ consecutive elements. Then
$$
\begin{aligned}
\sum_{\substack{n\in S(Q)\\Q\not|n}} \{na\}
\text{ lies between }
&\sum_{\substack{n\in S(Q)\\Q\not|n}} \left\{n\frac PQ\right\}
\pm \sum_{\substack{n\in S(Q)\\Q\not|n}}\left\{n\left(a-\frac PQ\right)\right\}
\\
\text{ thus between }
&\sum_{0<n<Q} \frac nQ
\pm \frac 1{QQ'}\sum_{n\in S(Q)}{\color{red}{n}}
\\
=
&\frac 12(Q-1)
\pm \frac 1{QQ'}\sum_{n\in S(Q)}{\color{red}{n}}
\ .
\end{aligned}
$$
(Later edit in red.)
This can now be converted to a proof for the stated result as follows.
I try to explain my feeling of a procedure using an example, $N=100000$ say.
Recall the following denominators "$Q$" of the convergents of $a=\sqrt 2-1$:
$1$, $2$, $5$, $12$, $29$, $70$, $169$, $408$, $985$, $2378$, $5741$, $13860$, $33461$, $80782$, $195025$.
Using $Q=80782$ and $S(Q)=(N-Q,N]\cap\Bbb Z$ we obtain a deviation from $\frac 12|S(Q)|$ which is less than (one plus)
$\frac {N(N+1)/2}{QQ'}<\frac{Q'Q'/2}{QQ'}<2$. (Here, $Q'/Q\approx \sqrt 2+1$, which is the limit of the ration of two
consecutive convergents.)
Then for the "new N", which is $N-Q=19281$ we use the "new Q" $13860$. And so on.
Using this we get a deviation of $f(N)$ from $N/2$ which is of the shape $2\log_{\sqrt 2+1} N$.
(Have to submit, hope that the idea is clear, this was more important for me than to write things rigurous,
and possibly deflect from the idea.)
Comment: This approach is indeed the required arguments to prove Theorem 3.4 p125 of Kuiper Niederrater in my answer.
Comment: @dan_fulea Wow, this is great. I tried to do something initially with continued fractions, but I couldn't make it all the way to your final result of $2\log_{\sqrt{2}+1} N$. Awesome!
Comment: @dan_fulea Where did $N(N+1)/2$ come from? Shouldn't the deviation from $\frac{1}{2}|S(Q)|$ be under $1/Q'$, since $$\frac{1}{QQ'}\sum_{n\in S(Q)} 1 =\frac{1}{Q'}$$
Comment: Sorry, the initial message was typed in a hard days night. The sum is over $n$, not over one. I missed to insert it in the first post, now edited in red.
Answer: Writing
\begin{align}
&\quad \sum_{n=1}^N \left( n\sqrt{2} - \lfloor n\sqrt{2} \rfloor \right) \\
&=\int_1^N \left( n\sqrt{2} - \lfloor n\sqrt{2} \rfloor \right) {\rm d}n + {\cal O}(1) \tag{1} \\
&=\frac{1}{\sqrt{2}} \int_\sqrt{2}^{\sqrt{2}N} \left(x- \lfloor x \rfloor \right) {\rm d}x + {\cal O}(1)  \\
&=\frac{1}{\sqrt{2}} \Bigg( N^2 - 1 - \Bigg[ \frac{\sqrt{2}N \left(\sqrt{2}N-1\right)}{2} + \frac{\left\{\sqrt{2}N\right\} \left(1-\left\{\sqrt{2}N\right\}\right)}{2} \\
&\quad - \frac{\sqrt{2} \left(\sqrt{2}-1\right)}{2} - \frac{\left\{\sqrt{2}\right\} \left(1-\left\{\sqrt{2}\right\}\right)}{2} \Bigg] \Bigg) + {\cal O}(1) \\
&=\frac{N}{2} + {\cal O}(1)
\end{align}
where we used
$$
\int_0^x \lfloor t \rfloor \, {\rm d}t = \frac{x(x-1)}{2} + \frac{\left\{x\right\}\left(1-\left\{x\right\}\right)}{2} \, .
$$
The order follows from the fact that
$$
\frac{\left( \sqrt{2}N - \lfloor \sqrt{2}N \rfloor \right) + \left( \sqrt{2} - \lfloor \sqrt{2} \rfloor \right)}{2} = {\cal O}(1) \tag{2}
$$
and
$$\int_1^{N} \left( n\sqrt{2} - \lfloor n\sqrt{2} \rfloor \right)'' {\rm d}n \tag{3} \\
= \left( \sqrt{2}n - \lfloor \sqrt{2}n \rfloor \right)'\big|_{n=N} - \left( \sqrt{2}n - \lfloor \sqrt{2}n \rfloor \right)'\big|_{n=1} \\
=\sqrt{2} \sum_{k=-\infty}^{\infty}\left[ \delta\left( \sqrt{2} - k   \right) - \delta\left( \sqrt{2}N - k  \right) \right]
$$
but this requires Euler-Maclaurin.

Due to the harsh critic about my somewhat heuristic argument I want to correct my approach as far as possible.
Set $$f(x)=x-\lfloor x \rfloor$$ and $$f_n(x)=\frac{1}{2} - \frac{1}{\pi} \sum_{k=1}^n \frac{\sin(2\pi k x)}{k} \, ,$$such that
$$
\lim_{n\rightarrow \infty} f_n(x) = f(x) \, .
$$
Since $f_n$ is differentiable we can use Euler-Maclaurin to calculate the sum
$$
\sum_{k=1}^N f_n(ak)
$$
with some $a$. The integral in (1) does not create much of an issue in the limit $n \rightarrow \infty$, since the limit is piecewise continuous and the integral can be splitted accordingly and then integrated. Also the limit of (2) is of ${\cal O}(1)$. So the problematic term which needs to be examined is the remainder $R_2$ ((3) was very heuristic) which can be written as
$$
R_2=\int_1^N B_2\left(t-\lfloor t \rfloor\right) \frac{\rm d}{{\rm d}t} f_n'(at) \, {\rm d}t
$$
neglecting unnecessary constants and $B_2$ is the second Bernoulli polynomial. We can express
\begin{align}
f_n'(at) &= 1-\sum_{k=-n}^{n} {\rm e}^{i2\pi k at} = 1 - \frac{\sin\left((2n+1)\pi at\right)}{\sin\left(\pi at\right)} \\
B_2\left(t-\lfloor t \rfloor\right) &= \left(t-\lfloor t \rfloor\right)(\left(t-\lfloor t \rfloor - 1\right) + \frac{1}{6} = \lim_{M \rightarrow \infty} \sum_{k=1}^M \frac{\cos(2\pi kt)}{\pi^2 k^2}
\end{align}
and integrate by parts
$$
R_2=-B_2\left(t-\lfloor t \rfloor\right) \frac{\sin\left((2n+1)\pi at\right)}{\sin\left(\pi at\right)} \Bigg|_{1}^{N} - 2 \sum_{k=1}^{M} \int_1^N   \frac{\sin(2\pi kt)}{\pi k}  \frac{\sin\left((2n+1)\pi at\right)}{\sin\left(\pi at\right)} \, {\rm d}t \tag{4} \, .
$$
For $a$ not an integer, the first term is bounded and ${\cal O}(1)$ as $n \rightarrow \infty$. The integral can be viewed as a functional for $n \rightarrow \infty$ in which case the Dirichlet kernel acts as a periodic delta-distribution $\sum_{m=-\infty}^{\infty} \delta(at-m)$
$$
\lim_{n \rightarrow \infty} \int_1^N   \frac{\sin(2\pi kt)}{\pi k}  \frac{\sin\left((2n+1)\pi at\right)}{\sin\left(\pi at\right)} \, {\rm d}t = \sum_{m=\lceil a \rceil}^{\lfloor Na \rfloor} \frac{\sin\left(\frac{2\pi km}{a}\right)}{\pi k a} \, .
$$
Evaluating
$$
\sum_{m=\lceil a \rceil}^{\lfloor Na \rfloor} \lim_{M\rightarrow\infty} -2\sum_{k=1}^{M} \frac{\sin\left(\frac{2\pi km}{a}\right)}{\pi k a} = \sum_{m=\lceil a \rceil}^{\lfloor Na \rfloor} \frac{2\{m/a\}-1}{a} \tag{5}
$$
and using $\sum_{n=1}^{N}\{an\} = \frac{N}{2} + {\cal O}(?)$
this becomes ${\cal o}(N)$. So it is actually true ${\cal O}(1)$ does not follow.

We continue with the integral in (4) for $N$ integer
\begin{align}
&\quad -2\sum_{k=1}^\infty \int_1^N   \frac{\sin(2\pi kt)}{\pi k}  \frac{\sin\left((2n+1)\pi at\right)}{\sin\left(\pi at\right)} \, {\rm d}t \\
&=-4\sum_{m=1}^n \sum_{k=1}^\infty \int_1^N \frac{\sin(2\pi kt)\cos(2\pi m a t)}{\pi k} \, {\rm d}t \\
&=\frac{4}{\pi^2} \sum_{m=1}^n \sum_{k=1}^\infty \frac{\cos^2(N\pi m a)-\cos^2(\pi ma)}{k^2-m^2 a^2} \\
&=2\sum_{m=1}^n \left[ \frac{\cos^2(N\pi m a)-\cos^2(\pi ma)}{\pi^2 m^2 a^2} - \frac{\cot(\pi ma)\left(\cos^2(N\pi ma) - \cos^2(\pi ma)\right)}{\pi ma} \right]
\end{align}
where $a$ must be an irrational number now. The first term is ${\cal O}(1)$ for $n\rightarrow \infty$.
Any idea for the second?
It can be rewritten as
\begin{align}
&\quad \, \, \sum_{m=1}^n \frac{\cot(\pi ma)\left(\cos^2(N\pi m a) - \cos^2(\pi ma)\right)}{\pi ma} \\
&= \sum_{m=1}^n \frac{\cot(\pi ma)\left(\cos(N2\pi ma) - \cos(2\pi ma)\right)}{2\pi ma} \\
&= - \sum_{m=1}^{n}\cos(m\pi a) \, \frac{\sin\left((N+1)m\pi a\right)}{m\pi a} \, \frac{\sin\left((N-1)m\pi a\right)}{\sin(m\pi a)} \\
&= - \sum_{m=1}^{n} \frac{\sin\left((N+2)m\pi a\right)}{m\pi a} \, \frac{\sin\left((N-1)m\pi a\right)}{\sin(m\pi a)} + \sum_{m=1}^n \frac{ \sin\left(N2\pi ma\right) - \sin\left(2\pi ma\right) }{2\pi ma}
\end{align}
so the second sum is bounded again $\forall N$ and $n \rightarrow \infty$.
Not sure if it helps, but I have the following two identities for the sines
$$
\frac{\sin\left((N-1)nx\right)}{\sin(nx)} = \sum_{l=2}^N \cos\left((N-l)nx\right) \cos^{l-2}(nx)
$$
and
$$
\frac{\sin\left((N-1)nx\right)}{\sin(nx)} = 1+2\sum_{l=1}^{\frac{N}{2}-1} \cos\left(l2nx\right) \, ,
$$
but evaluating this feels as if I'm running in circles.
I added a Figure of the RHS of (5) up to $N=10^6$ which doesn't look anything like $\log$, so either the numbers are just too small or I dont why it has to be $\log$.
Comment: After the first equality sign things are relatively simple. Why do we have this control with $O(1)$ of the difference between the sum and the integral?
Comment: It is quite unclear that using Euler-Maclaurin formula giving any useful information. The part with $O(1)$ needs much more information and it needs to be done very carefully.
Comment: Fourier series approach seems promising. Maybe you can try starting  with the error bound for $f_n -f$.
Answer: This is along the same line as another problem and my answer there: Determine whether $\sum_{n=1}^\infty \frac {(-1)^n|\sin(n)|}{n}$ converges
We need Koksma's inequality p. 143, Theorem 5.1 of 'Uniform Distribution of Sequences' by Kuipers and Niederreiter.

Theorem [Koksma]
Let $f$ be a function on $I=[0,1]$ of bounded variation $V(f)$, and suppose we are given $N$ points $x_1, \ldots , x_N$ in $I$ with discrepancy
$$
D_N:=\sup_{0\leq a\leq b\leq 1} \left|\frac1N \#\{1\leq n\leq N: x_n  \in (a,b) \} -(b-a)\right|.
$$
Then
$$
\left|\frac1N \sum_{n\leq N} f(x_n) - \int_I f(x)dx \right|\leq V(f)D_N.
$$

To control the discrepancy, We apply the following theorem, for the sequence $x_n = n\alpha - \lfloor n \alpha \rfloor$. Note that with $\alpha = \sqrt 2$, it has a bounded partial quotients in its continued fraction.
Theorem 3.4 of p125 in the Kuipers and Niederreiter's book, states that

If an irrational real $\alpha$ has a bounded partial quotients, then the discrepancy $D_N$ satisfies
$$
N D_N\ll \log N.
$$

Then applying these to your problem with $f(x)=x$, we obtain
$$
\bigg\vert\frac1N \sum_{n\leq N} \{ n\sqrt 2\} - \int_0^1 x \ dx\bigg\vert \ll \frac{\log N}N.
$$
Therefore, we have an estimate of
$$
\sum_{n\leq N} \{n\sqrt 2\}=\frac N2 + O(\log N).
$$

To obtain a more precise estimate, we have $V(f)=1$ for $f(x)=x$. Also, Theorem 3.4 describes how $O(\log N)$ term behaves in a more precise way. Using those, we have
$$
\Bigg\vert \sum_{n\leq N} \{n\sqrt 2\}-\frac N2 \Bigg\vert \leq  3+\left(\frac1{\log \xi}+\frac{2}{\log 3}\right)\log N.
$$
Here we use $\xi=\frac{1+\sqrt 5}2$ and the continued fraction partial quotients are bounded by $2$ (It is in fact $[1;2,2,2,\ldots]$).
Comment: Do you know if the bound of Koksma is the best? It seems rather poor,or?
Comment: If we calculate continued fraction of $\sqrt 2$ and use it in the proof of Theorem 3.4, then some constants may be reduced, but it does not improve $O(\log N)$.

Sum of $\{n\sqrt{2}\}$


Assignment to indexed images in SimpleITK

Question: What is the best way to do assign values or slices to indexed arrays in SimpleITK?
Example (1): Assigning a 2D slice to an indexed 2D slice in a 3D volume
In NumPy, we can do the following assignment to the index arrays:
import numpy as np
nda = np.ones((64, 256,256))
nda[0,:,:] = 2*nda[0,:,:]

The same operation in SimpleITK,
 import SimpleITK as sitk
 image = sitk.GetImageFromArray(nda)
 image[:,:,0] = 2*image[:,:,0]

gives the following error:
IndexError                                Traceback (most recent call last)
<ipython-input-18-4649e90a4ea5> in <module>
----> 1 image[:,:,0] = 2*image[:,:,0]

~/anaconda3/lib/python3.7/site-packages/SimpleITK/SimpleITK.py in __setitem__(self, idx, value)
 4690
 4691     # the index parameter was an invalid set of objects
-> 4692         raise IndexError("invalid index")
 4693
 4694

IndexError: invalid index

Example (2): Assigning a value an indexed 2D slice in a 3D volume
The following operation in NumPy,
  nda[0,:,:] = 2

n SimpleITK,
 image[:,:,0] = 2

gives the same Index error as in Example (1).
Answer: Unfortunately you can't do that type of operation in SimpleITK.  The left hand side of the the assignment operator has to be a single pixel.  It can't be a slice.
To do those types of operations, you'd have to extract out a slice from the volume, operate on the slice and then use the Paste function to paste the slice back into the volume.
You can see an example of the Paste function in an earlier answer I posted:
https://stackoverflow.com/a/32612215/3712577


Shapley values- airport problem

Question: can anyone help me on this?
I am trying to understand how can I find the Shapley values for the following airport problem.
There are 3 types of airplanes, type 1, type 2 and type 3 and they require the following length of the runway to land:
Type 1: 100
Type 2: 160
Type 3: 200
I would like to know how much each airplane should pay to land in the airport.
I know that the Shapley formula is:
$\phi= \sum{\frac{|S|! (|N|-|S|-1)!}{|N|!}}* (v(SU{i})-v(S)) $
where in this case:
N={1,2,3}=3, so for the first part of the formula I have that:
$\phi= \frac{|S|! (|3|-|S|-1)!}{|N|!}$=
$\phi= \frac{|S|! (|2|-|S|)!}{|3|!}$
To find the Shapley value for type 1:
I did:
for the first part of the formula  and if S= $\emptyset$
$ \frac{|S|! (|2|-|S|)!}{|3|!}$ =   $\frac{| \emptyset| ! (|2|-|0|)!}{|3|!}$= $\frac{0! * 2!}{3!}= \frac{1}{3}$
And the same for S={2}
$ \frac{|S|! (|2|-|S|)!}{|3|!}$ =   $\frac{|1| ! (|2|-|1|)!}{|3|!}$= $\frac{1! * 1!}{3!}= \frac{1}{6}$
The same if S={3}
The result is the same as S=2
For S={2,3}
$ \frac{|S|! (|2|-|S|)!}{|3|!}$ =   $\frac{|2| ! (|2|-|2|)!}{|3|!}$= $\frac{2! * 0!}{3!}= \frac{1}{3}$
Now I need to apply the second part of the formula which is:
$(V(SUi)-V(S))$
I believe that for S=$\emptyset$
the formula will be: $v(\emptyset U 1) $- $V(\emptyset)$= v(1)-v($\emptyset$)=100-0=100
But then for
S={2}, S={3} and S={2,3} I cannot understand how can I apply the formula. Should it be:
For S={2}
$v(1 U 1) $- $V(1)$= v(1)-v($\emptyset$)=(100+100)-100=200-100=100 ?
And how can I know what is the value of S={2,3} ?
Can anyone give a hint how to calculate these values?
Thanks
Answer: Your airport TU-game is not completely defined. For instance, if we define the airport game from the cost vector $c=(100,160,200)$ through
$$C(S)=\max_{i \in S}\, c_{i} \qquad \forall S \subseteq N,$$
then the associated cost game is given by
$$cv=(100,160,200,160,200,200,200)$$
Computing the Shapley value, and we obtain
$$sh_{cv}=(100,190,310)/3. $$
Comment: is this for type 1 airplanes? Can you clarify what you did please? thanks
Comment: I don't get your point. For each composition of airplanes I compute the cost charged by the above formula. For instance, for S=$\{1,2\}$, the cost incurred to S is $max(100,160) = 160$. Having this accomplished, apply the formula of the Shapley value.  Then the fee imposed by the Shapley value for airplane 1 is $100/3$, for airplane 2 it is $190/3$, and finally for the third, a fee of $310/3$ is charged.
Comment: I got it. I was asking for the cost of airplane 1 because the others will follow the same logic, So for S={2,3} the cost will be 200 right? Thank you for your help.
Comment: is this the cost or the runaway in meters they need to land? because additionally, I know that the price to repair the damage done by one landing to 1m of the runaway is 10pences
Comment: Yes, this is simplest way one can approach the construction of an airport game. In the literature, one can find different forms to get the associated airport game.
Comment: are you replaying to my first comment? Sorry, I did not understand if this is the length of land they need or the cost, I believe that is just the length if not can you explain? thanks
Comment: These are the costs. If airplane $1$ needs a runway of $1600$ meters, then the cost imposed to the airport to maintain the runway are $160$ currency units (dollar, pounds, etc.).
Comment: so it means that if I multiply the shapely values=(100/3,190/3,310/3)*0.10 I will know the payoff division?
Comment: No. Airplane $3$ incurred the hightest cost to the airport of $200$ currency units. Hence, the airport needs a runway of max $2000$ meters, which incurs costs of maintaining the whole runway of $200$. Hence, one has total costs of $200$. Now, the Shapley value answers the question of how these total costs should be divided among the airplanes, since it is considered as unfair to incur the fee only to airplane $3$ to cover the maintaining costs of the runway. Adding up these fees one gets the runway maintaining costs of $200$.
Comment: so airplane 3 instead of paying 200, it should pay 310/3 to be a fair price?
Comment: Yes, that is the point.

Shapley values- airport problem


Como faço para exibir o que está dentro da lista?

Question: Tenho esse método que adiciona coisas na lista:
 public Pedido adicionarPedido(int quantidade, String nome){

        lista.add(new Item(quantidade, nome ));
            return this;
 }

E instanciei nessa classe nos atributos a lista :
List<Item> lista = new ArrayList<Item>();

Eu estou pedindo para exibir no método toString os dados da lista mais o nome da pessoa:
     public Pedido ToString() {
        mensagem = "Pedido [lista=" + lista.get(0) +  lista.get(1) + ", cliente=" + cliente.getNome() + "]";
          return this;
     }

O problema é que no console está sendo impresso assim:
Pedido [lista=teste.Item@15db9742 teste.Item@6d06d69c, cliente=Aline Gonzaga]

Mas eu quero que seja impresso os itens da lista.
Como posso fazer isso?
Comment: Como está sua classe Pedido?
Comment: O que deveria imprimir? e reforço o pedido para postar a classe `Pedido`.
Answer: Pra mim é um abuso usar o ToString() para isso, esse método não foi criado para esse tipo de coisa.
Você deve pegar o elemento e o membro da classe, e deve fazer isso varrendo toa a lista, algo assim:
public Pedido ToString() {
     mensagem = "Pedido do Cliente " + cliente.getNome() + "\n";
     for (Item item : lista) mensagem += "Quantidade: " + item.quantidade + " Produto: " + item.nomeProduto + "\n";
     return this;
}

Coloquei no GitHub para referência futura.
Só consegui responder olhando sua pergunta anterior. Todo esse código ainda tem vários outros problemas.
Comment: @gonz só porque ela é professora não significa que seja dona da razão, já vi professores ensinarem do jeito que eles acham certo, mas depois vi que não era bem assim. Temos que ter a mente aberta para o conhecimento, e não nos engessar nas ideias que nos impoem, dessa forma, nós conseguimos obter um leque maior de recursos, independente de que linguagem ou área que se trate.
Comment: @gonz manda ela vir aqui, eu acho pouco provável que ela esteja ensinando errado, mas se estiver ela pode justificar porque está fazendo isso, ou aprender fazer direito.
Comment: @diegofm de fato tem isso também, mas já falei em outra resposta.
Comment: Eu também  acho um abuso encadear métodos dessa forma, aparentemente sem necessidade, fazendo retornarem a propria classe, mas é a escolha do OP, só nos resta alertar mesmo.
Comment: aprendi isso com minha professora. Bringuem com ela. rs
Answer: Sua classe Item precisa sobrescrever o método toString. Por padrão o método mostra um hashing único gerado pelo JDK. Então ficaria algo do tipo:
@Override
public String toString() {
  return String.valueOf(this.getQuantidade()) + " " + this.getNome();
}

Sempre que um método que recebe uma String como parâmetro e um objeto é passado, o método toString é invocado. No caso de uma lista, o método de seus itens é invocado, causando o efeito que você descreveu (Item@...).
Logo após você pode sobrescrever também o método da classe pedido:
@Override
public String toString() {
  return "Pedido: lista=" + this.getLista() + ", cliente=" + this.getCliente().getNome();
}

Assim a seguinte chamada:
System.out.println(pedido);

Irá produzir:
Pedido: lista=[1 Produto 1, 2 Produto 2], cliente=Aline Gonzaga

Notei também que você usou o método ToString com letra maiúscula. Pela convenção de código Java os métodos devem iniciar com letra minúscula, com exceção dos construtores. Então você deveria sobrescrever o método toString.
Quanto a utilização e sugestão sobre o uso abusivo do toString nesta resposta, temos esse tópico no stackoverflow que indica que a utilização é interessante para fins de depuração, então talvez para mostrar uma mensagem final na sua aplicação seja melhor outro método para definir a mensagem.
Comment: Acessar propriedades diretamente? E não é endereço de memória, é um hashing unico gerado pelo JDK.
Comment: Este é o problema de responder perguntas antes do OP adicionar maiores esclarecimentos. Falta informação na resposta.
Comment: Agora vc poderia me explicar o motivo de ter dado certo, tipo só adicionei o ToString() na classe Item e funcionou...
Comment: Funcionou obrigada amigo
Comment: Ótimo, obrigada. entendi.


Unable to copy default register user action in MVC

Question: I'm trying to copy the default register user action from the MVC template, when I copy the method to another class I get the following errors.
Does anyone know what I could be doing wrong? I'm stumped.

incorrect number of type parameters
cannot resolve symbol Succeeded
Cannot resolve symbol add errors
Answer: You are missing 2 other methods from the template. First one to get the UserManager object:
private ApplicationUserManager _userManager;

public ApplicationUserManager UserManager
{
    get
    {
        return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
    }
    private set
    {
        _userManager = value;
    }
}

Secondly, AddErrors:
private void AddErrors(IdentityResult result)
{
    foreach (var error in result.Errors)
    {
        ModelState.AddModelError("", error);
    }
}


R need to replace text between slashes in URL

Question: I'm trying to replace a number in a URL to improve image quality and can't figure out how to properly escape the / to get it to work.
I need to change://contentinfo.autozone.com/znetcs/product-info/en/US/nip/3289/image/1/
to://contentinfo.autozone.com/znetcs/product-info/en/US/nip/3289/image/10/
of course, https is ahead of both of those URLs, but the forum converts that to an image if I include it.
Comment: `sub("\\d+/?$", "10/", myurl)`
Comment: Thanks!  That  worked :D
Answer: I tried the code below on an online snippet and it worked well. Used the hint of Aurèle too. Maybe it will give you a hint:
url <- "//contentinfo.autozone.com/znetcs/product-info/en/US/nip/3289/image/1/"

cat("Simple way\n")
simple <- gsub("/1/","/10/",url)
cat("Before: ", url, "\n")
cat("After: ", simple, "\n")

cat("Regex \n") As sugested by Aurèle
regex <- sub("\\d+/?$", "10/", url)
cat("Before: ", url, "\n")
cat("After: ", regex, "\n")

Answer: Working solution by Aurèle:
sub("\d+/?$", "10/", myurl)


How To get Data from Parse database In Android Array?

Question: I have a database in Parse, I succeed to connect and receive user ID from Parse DB.
But didn't get inner field of that User ID (i.e. user ID's detail which is stored under that User ID).
Comment: FYI, Parse is going to close. Its better you first switch to other solution. They will be still operating till Jan,17. Read more at http://techcrunch.com/2016/01/28/facebook-shutters-its-parse-developer-platform/#.2udf7j:J7hg
Comment: Thanks, I know that but My client is not want to switch on any other solution.
Answer: <code>ParseQuery<ParseUser> query = ParseUser.getQuery();
// for current user details
 // query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId();

           query.findInBackground(new FindCallback<ParseUser>() {
            @Override
            public void done(List<ParseUser> objects, com.parse.ParseException e) {
                if (e == null) {

                   //objects = list of all user data
                   //for getting user details

                   for (int index = 0; index < objects.size(); index++) {

                                    String email=objects.get(index).getEmail();
                                    String username=objects.get(index).getUsername();

                                    //if you created custom field in user table i.e "details"
                                    //String details=objects.get(index).get("details").toString());

        }

                }

            }
        });
</code>


How to get an index in a list without calling index function or ANY built in functions?

Question: I am currently trying to build a function that finds the second divisor of a number (n) and returns the index of the second divisor WITHOUT calling the build-in index function.
xs is a list and n is the number to be divided
example would be: locate_second_divisor([20,3,4,2],12) yields 2
My current code
count=0
def locate_second_divisor(xs,n):
     count=0
     for num in xs:
        if n % num==0:
          count+=1
        if count==2:
         return

At return, I need to write the index of the second divisor but I can't think of how to do it without calling index.
This is not a duplicate question as I am not allowed to use ANY built in functions. I can only use append, int, float, str, and loops with booleans and operators. I cannot use enumerate like similar questions can. I need some sort of way around the built in functions.
Updated code (only fails because I have to include None if there is no second divisor)
def locate_second_divisor(xs,n):
    count=0
    index=0
    for num in xs:
        if (n % num)==0:
            count+=1
        if count ==2:
            return index
        else:
            return None
Comment: `for i, num in enumerate(xs):  return i`
Comment: @juanpa.arrivillaga I cannot use any built in functions, so enumerate will not work.
Answer: If I understand correctly, you can just track the index the same way you're tacking the count of the times your condition is met.
def locate_second_divisor(xs, n):
     count = 0
     index = 0
     for num in xs:
        count += (n % num) == 0

        if count == 2:
            return index
        else:
            index += 1

print(locate_second_divisor([20,3,5,3,4], 12))
# 3

Fun note:
Since (n % num) == 0 evaluates to True or False, which safely cast to 1
and 0, you can simplify your first if block to: count += (n % num) == 0
Comment: Okay so I think that is a good set up, but I am still failing tests. For instance,  locate_second_divisor([20,3,5,3,4],12) should return 2, but instead is giving none. @Paul H
Comment: @ocean.1234 check your indentation. it works as expected for me. Also note that your example is wrong. the second divisor is the second occurrence of 3, which is the 4th element, which is located at index 3,
Comment: you are correct that my example is wrong. I see that. I still can't get the function to past the tests though.
Comment: Also @Paul H, it needs to include an else statement that returns none. Here is my updated code:def locate_second_divisor(xs,n):
 count=0
 index=0
 for num in xs:
  if (n % num)==0:
   count+=1
  if count ==2:
   return index
  else:
   return None
Comment: @ocean.1234 you don't need that last `else` since `None` is returned by default. See for yourself with: `print(locate_second_divisor([20,3,5,3,4], 7))`
Comment: you were right. I didn't need the last else statement. I removed it but my function is just returning 0 each time. I don't think it is updating count or something.
Comment: restart your interpreter, check your indentation. my implementation is working as expected for me.


Simulation power analysis

Question: How do you perform a power analysis for linear mixed model using R? I measure area of five groups of plants across time. Each group of plants grow under two treatments. I have 12 replicates for each plant. I have fitted a lme with plant group and treatment as fix effects and the replicates as random effect. How can I go from that to the actual calculation? I have found tools for ANOVAs and MANOVAs but not for lme. Thanks.
Comment: possible duplicate of [Simulation of Logistic Regression Power Analysis - Designed Experiments](http://stats.stackexchange.com/questions/35940/simulation-of-logistic-regression-power-analysis-designed-experiments)
Answer: This is one way to perform power analysis using lmer. I have created a simulated data set using different functions. Here I report how I could perform the actual simulation.
library(lme4)
library(lmerTest)

simulateFixedEffect <- function(model, ecotype, time, id, B){
  Tvalue=rep(0, B)
  set.seed(781)
  for(b in 1:B){
    s1 <- drop(simulate(model))
    ms <- lmer(s1$sim_1 ~ ecotype * time + (time | id))
    Tvalue[b] <- anova(ms)[[6]][3]
  }
  return(Tvalue)
}

plotValue <- function(Fvalue, alpha=0.05){
  mean(Fvalue > alpha)
  hist(Fvalue, 30, prob=T, xlab="", ylab="", main="")
  x <- seq(0, 15, 0.1)
  lines(x, df(x,1,80), col="red")
  grid()
}

time <- (subdata$time)
    gclass <- as.factor(subdata$gclass)
id <- subdata$id
    growth <- subdata$growth

m1 <- lmer(growth ~ gclass * time + (time | id))
Tvalue <- simulateFixedEffect(m1,gclass, time, id, 10)

Simulation power analysis


Real life applications of general vector spaces

Question: Students familiar with Euclidean space find the introduction of general vectors spaces pretty boring and abstract particularly when describing vector spaces such as set of polynomials or set of continuous functions. Is there a tangible way to introduce this? Are there examples which will have a real impact? I would like to introduce this in an engaging manner to introductory students. Are there any real life applications of general vector spaces?
Comment: What is real life?
Comment: Solutions to linear differential equations?
Comment: Well, it explains why you can add matrices/polynomials/functions and multiply them by a scalar. They behave the same way, although they are different objects, and the reason why is because they form a vector space.
Comment: Related: [What are some applications of elementary linear algebra outside of math?](http://math.stackexchange.com/questions/1072459/what-are-some-applications-of-elementary-linear-algebra-outside-of-math). Maybe some of the other posts tagged [linear-algebra+applications](http://math.stackexchange.com/questions/tagged/linear-algebra+applications) might be interesting for you.
Answer: Depending on how much depth you want to introduce, I think you should mention Fourier analysis. Even if they haven't taken differential equations courses before, showing that functions form a vector space is quite trivial. Once you have this, you know you can introduce the idea of a basis for this space, which lets you reliably decompose certain functions as being made up from other ones. Applications obviously abound. Probably not "obviously" for your students, but take your pick to wow them, since it probably has a use in whatever they're studying.
The point is that this concept of "representing a function as a sum of others" would be difficult to define as a concrete method with only the tools they've seen so far. But it's the same ideas as vector projection and finding a basis, but on things that don't look like vectors at all. The abstract idea of vector spaces lets you carry all these neat and powerful tools over to other problems.
Comment: You can get way more upvotes, if you'd include those "obvious" applications to your answer. Because in the end peoples reading this page are mostly those mentioned students.
Answer: What's boring about polynomials and real-valued functions ?
Polynomials have a great use in science, mainly in approximations
using interpolations.
Since the set of polynomials with degree smaller than $n$ is a vector
space, we can take an orthonormal basis for it and easily find approximation
for any real value function (depending on the inner product of course).
note that the reason we can do this is that the real valued functions
are also a vector space!
Answer: I don't know if this is what you are looking for, but...
The functioning of the 4G-smartphones depends on the phones ability to quickly carry out certain transformations (DFT/IDFT) in certain (for example) 1024-dimensional subspaces of the space of (periodic) functions.
Look up OFDM for more details.
Answer: Is the stock exchange real enough? OK, you'll have to abstract from the fact that you only can buy or sell complete stocks, not arbitrary fractions of stocks (although given the modern way of trading stocks, I'm not even completely sure if you really can't ;-)). But it is clear that if you e.g. put an order to sell two Microsoft stocks and buy one Apple stock, and then put another order to buy two Apple stock and five IBM stock, you ultimately sell two Microsoft stock and buy three Apple and five IBM stock. So you've got vector addition (-2,1,0,0,....) + (0,2,5,0...) = (-2,3,5,0...). This also has the advantage that there's no natural order of your basis (there's no reason why you should list Microsoft stock first, or even why you should put all stock in a linear order). Multiplication by a scalar is also clear: You buy/sell e.g. twice as many stocks.
You also get a natural dual space: The price. It is a mapping from a vector (sell/buy order) to a scalar (money to pay/earn), and it is obviously linear (if you buy one Apple and two Microsoft stocks, you pay the price of an Apple stock plus twice the price for a Microsoft stock. Stock exchange covectors are regularly listed in certain newspapers and on certain web sites.
Also it has the advantage of not having an intrinsic scalar product (you can't meaningfully ask about the product of the order "selling two Microsoft stocks" and the order "buying one Microsoft stock and selling three Apple stocks"). When coming from Euclidean vector spaces, you are inclined to take a scalar product for granted, so having a space where a scalar product simply doesn't make sense is probably a good idea.
Comment: What is the zero vector in your stock market model, which by the way, is a finite set?
Answer: Well you could talk about the word vectors? Or even thought vectors, really any time you want a categorical piece of data to be represented in a unique relational database, you can give that piece of data numerical "features", or in other words, dimensions that allow one to classify people, places, words, things, literally anything you want.
For example Netflix vectorizes movies, and they actually then insert the user as a vector into the same vector space as the movies to get an idea of what other movies to suggest to the user. Vectors are heavily used in machine learning and have so many cool use cases.
What's really awesome about those kinds of examples is you don't really have to understand the linear algebra to even really understand what is going on, at least from a conceptual point of view. Geoff Hinton (one of the inventors of back-propagation) does work with thought vectors, I really suggest reading up on that because the applications are literally endless. Like Google is developing an algorithm that can flirt with thought vectors. They already have a program that can answer email for you based on the same technology.
Hope this helps!


.htaccess Allow owner of home directory only?

Question: Suppose I have a home directory "~someuser/www" which Apache (maintained by someone else than me) exposes as http://www.myserver.com/~someuser . If I want to limit this directory to certain users, I can create a .htaccess file in the www folder containing the line "Require user username1 username2" etc.
The question is: Is there a way to specify in .htaccess that only the owner of the home directory, whoever that might be, is supposed to be able to access it from the web?
If I knew that the owner was "someuser", then I would just specify "Require user someuser", but in this case I'm trying to put together a template that will work for a lot of users, and I'd like to avoid having to tell them each to insert their own username if possible.
(More background: Specifically I'm trying to get it to work on the MIT site described here: http://scripts.mit.edu/faq/15 . We'll have students doing little web projects in their own www folders, but don't want other students to be able to view their JavaScript source code.)
Answer: This is a cop-out, but make the students do it:
Make your first-day-getting-familiar-with-the-lab exercise something that gets this kind of housekeeping set up, explaining what the <code>.htaccess</code> file is, and linking the students to the .htaccess tutorial.  Then as one of the exercises have them restrict web access on some directory to themselves, the TAs and the professor.
This is what I've traditionally done for the first-year programming classes I occasionally run at my alma mater ("Set the permissions on your home directory so people can't snoop.", "Configure CVS/git/whatever for your account." , etc...) - It saves me the work of having to do it, and makes the students get comfortable with the environment they'll be working in.
Comment: You can probably also do some hackery with SetEnv and URL parsing (though I don't know that you can do it in .htaccess - it may need to be done in the main apache config file) -- I would actually have to sit down and play to figure that out though.  It strikes me as "Probably possible, but definitely work."
Comment: Not unreasonable. In the end I created the file with the right user name as part of a script that they had to run to set up their system in the first place. Thanks!


When will PCA be equivalent to ICA?

Question: $X = AS$ where $A$ is my mixing matrix and each column of $S$ represents my sources.  $X$ is the data I observe.
If the columns of $S$ are independent and Gaussian, will the components of PCA be extremely similar to that of ICA?  Is this the only requirement for the two methods to coincide?
Can someone provide an example of this being true when the $cov(X)$ isn't diagonal?
Comment: I'd love to see a mathematical answer here -- something that starts from a derivation and demonstrates concretely where the two diverge and in what general case they are equivalent.
Answer: PCA will be equivalent to ICA if all the correlations in the data are limited to second-order correlations and no higher-order correlations are found. Said another way, when the covariance matrix of the data can explain all the redundancies present in the data, ICA and PCA should return same components.
Answer: My answer here may help you.
I believe that PCA will be equivalent to ICA, only when the independent components are orthogonal to begin with. PCA gives the independent components of your data, only in so far as an orthogonal transformation might.
Answer: Here is my understanding, gathered largely from the paper https://www.sciencedirect.com/science/article/abs/pii/0165168494900299
First, some non-rigorous reasoning. PCA produces an orthogonal matrix defined up to a +-1 scaling of the columns, while ICA (when applicable) yields a generic matrix defined up to any scaling and reordering of the columns. If ICA and PCA give the same matrix, then the true matrix $A$ can be obtained from it by column scaling and reordering, and thus has orthogonal columns, and this is the condition of PCA and ICA coincidence. When the sources are Gaussian, ICA simply does not work.
Now for an attempt at a more thorough argument. I am describing this in terms of random variables, not samples from them - the latter will behave similarly, but with an effect of sampling noise. (So, a 'known random vector $x$' below means that the joint distribution of its components is fully known).
ICA
Suppose $x$ and $s$ are column vectors of random variables with zero means, and $x = As$, where A is a full-rank constant matrix (possibly non-square with more rows than columns), and the components of $s$ are independent. ICA is defined as finding the matrix $A$ for a known vector $x$ (and then $s$ can also be found from the equation). It is not possible to find exactly $A$ as any matrix from the set obtained from $A$ by an arbitrary scaling of each column and an arbitrary permutation of the columns will also yield an $s$ whose components are independent. Thus, the best that can be done is finding this set of matrices. And this is possible if the components of true vector $s$ have finite, nonzero variances, and at most one of them is Gaussian (this is the theorem of ICA identifiability, see COROLLARY 13 in the paper).
PCA
Let $x$ be a column vector of random variables with zero mean and a finite covariance matrix. PCA is defined as finding an orthogonal matrix $Q$ such that for $\tilde{s}$ defined by $x = Q \tilde{s}$, the components of $\tilde{s}$ are uncorrelated and their variances form a (weakly) decreasing sequence. Again, there is an indetermination: for any such $Q$, a matrix obtained by multiplying any subset of the columns by -1 will also satisfy the conditions. If the variances of the components of $\tilde{s}$ are all different, this -1 scaling is the only indetermination, otherwise, for any set of components of $s$ with the same variance, $Q$ can be multiplied by an arbitrary rotation matrix in the space of the corresponding columns.
Now let us consider when ICA and PCA can give the same result. Let $x = As$, with $A$ being a square matrix and $s$ satisfying the conditions of ICA identifiability. Let $x = Q \tilde{s}$ be the PCA of $x$, and suppose for now that the variances of the components of $\tilde{s}$ are all different. If $Q$ satisfies the conditions of ICA then it must coincide with the true matrix $A$ up to a scaling and reordering of columns. Thus, $A$ must have orthogonal columns (which is equivalent to $A^T A$ being diagonal). Conversely, if $A$ has orthogonal columns, then by scaling them to unit norm and ordering by strictly decreasing variance of $s_i$, we obtain an orthogonal matrix and uncorrelated sources in the proper order, and thus, one of the possible PCA decompositions.
To sum up, the orthogonality of the columns of $A$ is a necessary and sufficient condition of ICA and PCA 'coincidence' under the above assumptions. The 'coincidence' should be understood as the fact that the sets of matrices obtained by both methods intersect, and each has minimal indetermination.
Concerning the question about an example where the covariance of $x$ is not diagonal, we can take $s_1$ uniform on [-1,1], $s_2$ uniform on [-2,2], and $A$ a rotation in 2D by an angle that is not an integer multiple of $\pi / 2$ (otherwise $cov(x)$ is diagonal). Here is an example for a rotation by $\pi / 6$, with 1000 samples:

The lines are: black - true axes, blue - principal component directions, red - independent component directions. PCA was computed by the MATLAB function pca, while ICA was obtained using the EEGLAB package with a call: "runica(y, 'extended', 1);" The extended algorithm is needed if some components have distributions with negative excess kurtosis (called subgaussian, or platykurtic, distributions), as is the case here for uniform distributions.
If we remove the assumption of different variances of $\tilde{s}_i$, keeping the ICA identifiability and the orthogonality of the columns of $A$, PCA will be unable to select the 'true' basis within any subspace corresponding to the same variance, while ICA, as always, will find this basis up to a reordering and rescaling. Thus, the set of matrices satisfying the PCA conditions will include the set of ICA matrices with normalized columns, but the PCA set will be substantially larger (and thus give less information about the true sources).
Now let us try to remove the assumption of ICA identifiability. As an example, consider the situation when $A$ is square and $s$ has a multivariate Gaussian distribution with a diagonal covariance matrix having different values on the diagonal. In this case, the $\tilde{s}$ obtained by PCA will also be multivariate Gaussian, and its components will be uncorrelated and thus independent. So, the set of matrices produced by PCA will be included in the set provided by ICA. However, the indetermination in ICA will be larger than usual (i.e., than in the case of ICA identifiability). Indeed, if $A$ is the true matrix, and $D$ is the (diagonal) covariance matrix of $s$ then $A D^{1/2} R$ will be an ICA matrix for any rotation R. This can be seen from the fact that $A D^{1/2}$ is an ICA matrix with such a scaling that the corresponding sources have unit covariance (and are multivariate Gaussian). Any rotation of these sources will conserve this property and thus keep them independent. So, under these assumptions, if $A$ does not have orthogonal columns, the PCA matrix set will not include the true $A$, while the ICA set will, but will have a larger than usual indetermination. Conversely, if $A$ has orthogonal columns, PCA will find $A$ with its minimal indetermination, while the ICA matrix set will contain A together with many other matrices (in practice, a random one of them will be output by software).
As a conclusion for applications, if the columns of $A$ are known to be orthogonal and $s_i$ have different variances, go with PCA (regardless of the distribution of $s_i$, but if more than one of them are Gaussian, then, out of the two methods, only PCA will recover $A$ and $s$ under these assumptions). In all other cases, ICA is the way to go.
Answer: Putting aside the issue whether 'extremely similar' (please define what you mean by this; do you mean within the sampling error?) is the same as 'coincide', I believe the answer is no. To be equivalent, they must imply each other in both directions. If outputs are independent as in ICA (but not Gaussian, because ICA does not work for (circular) Gaussian data), then they are uncorrelated as in PCA. The converse is not always true. So one can only compare both methods for non-Gaussian data. For these, since both methods operate on different moments, they are not equivalent. ICA gives generally oblique components, while PCA components are always orthogonal. Therefore, orthongality (i.e., E(X'.X)=0) is a necessary condition for equivalence. But E(Xi'.Xj)=0 does not imply E(Xi'^2.Xj)=0, E(Xi'. Xj^2)=0, etc. for all higher-order moments and n-tuples to vanish as in ICA, so it is not a sufficient condition.

When will PCA be equivalent to ICA?


Looking up and inserting values from a table

Question: I have a huge dataset of reaction times from a psycholinguistic experiment, and in separate tables, I have information that I'd like to add in about each word's frequency and things like that.
The main dataset looks something like this:
maindataset <-structure(list(item = c(101, 103, 102, 104, 104, 102, 103,
102, 101, 103, 104, 101), react = c(512, 510, 506, 499, 515, 516,
517, 518, 509, 599, 520, 523), prime = c(1, 2, 3, 1, 3, 1, 3,
2, 2, 1, 2, 3)), .Names = c("item", "react", "prime"), row.names = c(NA,
-12L), class = "data.frame")

And the frequency information looks like this:
frequencyinfo <-structure(list(item = c(101, 102, 103, 104),
frequency = c(10,30, 40, 50)), .Names = c("item", "frequency"),
row.names = c(NA,-4L), class = "data.frame")

So basically, for each value of item in maindataset, I want to look up the frequency from the frequency table, and store it in a new column in maindataset.
I've been able to do it in the past using for loops, but it's horribly inefficient (it takes minutes to go through the whole 10,000 row dataset). Anyone know the best way to go about this?
Answer: If I interpreted your question properly, then you want to use merge():
merge(maindataset, frequencyinfo)
   item react prime frequency
1   101   512     1        10
2   101   523     3        10
3   101   509     2        10
4   102   506     3        30
....
....

This question on stack overflow provides a very nice overview of the different options for merge. If you are familiar with SQL, you can draw some very nice parallels to the different join operators.