Path.GetRandomFileName vs Path.GetTempFileName

Question: Base on recommendation from https://msdn.microsoft.com/en-us/library/system.io.path.getrandomfilename(v=vs.110).aspx
I have replaced  GetTempFileName with GetRandomFileName to get a name for the temp file. And it cause a problem. Sometimes GetRandomFileName return not a file name but location in System32 folder. And of cause users with no admin rights are having an error that file is not found.
Do I missed anything?
Here is a code:
string tempFileName = Path.GetRandomFileName();
FileStream tempFileStream = null;
tempFileStream = File.Open(tempFileName, FileMode.Create, FileAccess.ReadWrite);
later on when I try to access that file by code:
FileInfo fileInfo = new FileInfo(tempFileName);

I have an error:

System.UnauthorizedAccessException: Access to the path 'C:\Windows\system32\25ddubwt.qsc' is denied.

I realised that when user initiate a program by using menu from Windows/Start button current directory for the application will be System32
Comment: Any example of a location being returned instead of a filename?
Comment: Here is a C# Fiddle demonstrating what Alex K is saying. Notice it only prints a random filename: https://dotnetfiddle.net/dqKbUy Also, GetRandomFileName() doesn't create a file while GetTempFileName creates a zero byte file and returns the full path to it.
Comment: I have to edit my question. If you initiate your exe by using sortcut on Windows Start button menu current directory became System32.
Answer: GetTempFileName() returns a full path, GetRandomFileName() does not.
If you assume GetRandomFileName() has a path and write to it the file may well end up in System32 if thats the current directory.
To fix create a full path:
string fname = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

Comment: It may be a good idea to explicitly mention that `GetRandomFileName()` does not create the file on disk while `GetTempFileName()` does it.
Comment: One will point to c:\something\temp\ the other will not, thats the only difference and its quite a big one.  Something else may be changing the directory - which is why you should always specify an absolute path where possible.
Comment: It's best to not assume the location of current directory, being there are any number of reasons why the current directory might change for example OpenFileDialog as discussed in this [SO question](http://stackoverflow.com/questions/930816/why-does-openfiledialog-change-my-working-directory)
Comment: It would be a reasonable explanation. But it is not a current directory. and it is happening only 10-25% of the time.
Comment: Does it mean that sometimes current directory can be System32 and sometimes not? Depends on what?


How can I communicate with a printer via USB using java in windows platform

Question: How can I communicate with a printer via USB using java in windows platform? I don't want to use JNI. I want to use jsr80 or libusb-win32 if any experienced have work with that please share his/her knowledge with some code. I think in libusb-win32 java wrapper must install some driver, correct?  I don't want to use any driver. I want to designing my application like plug and play type device by which I can send file easily. I don't want to buy any software.
Is there any other way to communicate with printer?
Comment: After searching a long time, i find a solution that if i write a driver for that using c and then call it from java it can be possible.I get help from http://blog.peter.skarpetis.com/archives/2005/04/07/getting-a-handle-on-usbprintsys/ this site but still now i have no idea upon wsdk and wddk if anyone have some idea please share.

How can i add it with java code?
Answer: To communicate with most usb devices a driver is required. In most cases though the driver should already be installed on the target pc unless this is a remote printing application. Also most printer drivers are free so you shouldn't need to buy anything.
Comment: but when a pendrive is attached with cpu is there any driver is needed to send file?????I think no except inbox driver which is operating system  have.....I want to this as same manner witout any pendriver driver we can send file....so why not i can send file to printer.....
Comment: Pen drives are not the same as printers. Printers have there own processors which communicate with the software on the computer. Pen drives are essentially sd cards or hard drives and can be directly managed by the os.
Comment: I want to bypass the application driver.I want to use windows driver and print file using it.usbprint.sys file in windows os manage the printing if i access this file then it is possible,but it is a system file so it is not possible to  access but we can send file to this usbprint.sys and then it will manage this sended file.we achieve it by using windows driver programming.Do you have any idea upon that? Is it possible using java?


correct application of Tannery's theorem on $(1+\dfrac{1}{n^2})^n$

Question: I know that $$\lim_{n\to\infty}\left(1+\dfrac{1}{n^2}\right)^n = 1$$ but by applyling Tannery's theorem, I am getting the answer as zero. Can somebody, help me find the error in the solution below?
$$S_n = \Bigg(1+\dfrac{1}{n^2}\Bigg)^n = \sum_{k=0}^n{n \choose k}\bigg(\frac{1}{n^2}\bigg)^k = \sum_{k=0}^n\bigg(\frac{n}{n}\bigg)\bigg(\frac{n-1}{n}\bigg)\cdots\bigg(\frac{n-k+1}{n}\bigg)\bigg(\frac{1}{n^kk!}\bigg)$$
$$\implies S_n = \sum_{k=0}^\infty a_k(n) \text{ , where } a_k(n) = \begin{cases}
      0 & \text{ if } k>n \\
       \bigg(\frac{n}{n}\bigg)\bigg(\frac{n-1}{n}\bigg)\cdots\bigg(\frac{n-k+1}{n}\bigg)\bigg(\frac{1}{n^kk!}\bigg) & \text{ if }k\le n
   \end{cases} $$
clearly, $a_k(n)\stackrel{n\to\infty}{\longrightarrow}0 = b_k \text{  ,} \forall k$
and $|a_k(n)| \le \bigg(\frac{n}{n}\bigg)\bigg(\frac{n-1}{n}\bigg)\cdots\bigg(\frac{n-k+1}{n}\bigg)\bigg(\frac{1}{k!}\bigg)\le \frac{1}{k!},\forall n$
and $\sum \frac{1}{k!} = e< \infty$
$$\implies \text{ by Tannery's theorem } \lim_{n\to\infty} S_n = \lim_{n\to\infty}\sum_{k=0}^\infty a_k(n) = \sum_{k=0}^\infty \lim_{n\to\infty} a_k(n) = \sum_{k=0}^\infty b_k = 0$$

But the correct answer is 1. So, where did I go wrong?
Comment: It should be $\sum_{k=0}^n.$ Starting with $k=1$ means you've already subtracted $1$ from your expression.
Comment: I think that $a_0(n) \to 1$ as $n \to \infty.$
Comment: @CalvinKhor we actually need $\forall k \space\space \exists \space\space M_k \space\space s.t. \space\space |a_k(n)| \le M_k ,\forall n$ with $\sum M_k<\infty$
Comment: and i show that in the last 3rd and last 4th line in the post above
Comment: But now $a_0(n)=1$ for all $n,$ so you get convergence to $1.$
Comment: @abhishek sorry, misread
Answer: The error is in the definition of $a_k(n)$, since for $k=0$ we have
$$
a_k(n) = {n \choose 0} \left(\frac{1}{n^2}\right)^0 = 1.
$$
This makes the following statement wrong:
$$
a_k(n) {\to} 0 = b_k, \forall k
$$
since is not true for $k=0$, as for $k=0$ we have $a_0(n) = 1$, $\forall n$.
When you substitute it with
$$
b_k = \begin{cases} 1 & \text{if } k=0 \\
0 & \text{if } k \geq 1
\end{cases},
$$
you will obtain $\lim_{n\to\infty}S_n = \sum_{k=0}^\infty b_k = 1$.


How do I use the Password Policy module?

Question: I just installed and configured the Password Policy module, but the constraints like password length should be minimum 8 characters is not reflecting when an administrator user adds a new user and creates an one time login password.
The constraints/plugins provided in Drupal 8 by the Password Policy module aren't working in any scenario. Can anyone suggest the possible cause or the right way of using this module?
This module also doesn't modify the default recommendations on /admin/people/create.
Recommendations to make your password stronger:

Make it at least 12 characters
Add lowercase letters
Add uppercase letters
Add punctuation
Comment: `not working in any scenario` So it doesn't work when anonymous users sign up?
Comment: I think these constraints should be applicable at user edit page, when system administrator creates a new user, and alots him password. Please correct me if I'm wrong.
Comment: Are you user 1? if yes, could be that since you're super user the constrains don't apply to you. Test with another admin that is not user/1. See [Enforce password policy for admin (user 1) account](https://www.drupal.org/node/2862906)
Answer: Please see the reference link below which has information about how to use this module for Drupal 8
https://opensenselabs.com/blog/tech/drupal-password-policy-module

There might be other modules like PRLP (Password reset landing
page) or other relevant password setting modules that might not be allowing the policy to be enforced.
You can check by uninstalling them and see if the created policy is
being enforced on the default user form.
Answer: Each type of constraint is a sub-module, so in order to add a constraint for character length, for example, you need to:

Enable the module password_policy_length
Add a policy at /admin/config/security/password-policy
In #2 you will add individual constraints like password length

The module's README.md file contains pretty good instructions.
Comment: At time of writing you also need to use the dev branch, the latest release will keep you locked in a password expiry loop
Answer: In the latest drupal 8 Password Policy recommendation and Password Strength is default functionality and it shows on every password field also it not force you to follow the recommended values.
You can overide the password policy recommendation and force password policy using this module
Password Policy Configuration Module


Intuition of orthogonal polynomials?

Question: Recently, while learning about the regularization methodologies(in machine learning), i came across orthogonal polynomials ( of Legendre's polynomials), I looked up on the Internet and there are formulas for the same, but i didn't get any intuition behind this. Can someone explain in simple terms what are these orthogonal polynomials and when should we use them?
Comment: How much linear algebra (vectors, inner products, orthonormal bases, ...) do you know?
Comment: @KundanKumar I think the $w(x)$ in this case turns into a matrix-valued function $W(x)$ and $\int_{-\infty}^\infty {P_1}(x)^T W(x) {P_2}(x)dx$ . So it is a scalar product with $W$ a gramian matrix. https://en.wikipedia.org/wiki/Gramian_matrix
Comment: Orthogonality of functions also benefit from some connection of linear algebra to analysis. Getting used to integrals being inner products. Usually a beginner course in transform theory or Fourier analysis contains that.
Comment: not much, i'm aware of vectors and inner products but don't know about orthonormal bases @Chappers
Comment: @mathreadler can you provide me some reference links to learn basics in transform theory and fourier analysis so i can understand this concept better
Comment: I think Wikipedia could give a first introduction to the ideas https://en.wikipedia.org/wiki/Fourier_transform there $\sin(k_1x)$ and $\cos(k_2x)$ are orthogonal to each other if $k_1$ and $k_2$ are integers. It basically means if you multiply them together and integrate the result becomes 0. $\int_{0}^{2\pi}\sin(k_1x)\cdot \cos(k_2x)dx = 0$, but some linear algebra will be necessary for the theory. If two polynomials are orthogonal to each other then that probably means $\int_{-\infty}^\infty w(x)P_1(x)P_2(x)dx = 0$ for some function $w(x)$.
Comment: @mathreadler If polynomial $P_1(x)$ and $P_2(x)$ are $n$-dimensional vector function, then how this inner product will look like?
Comment: @mathreadler Does it look like  $\int_{-\infty}^{\infty}w(x) P_1^T(x) P_2(x) = 0$, or $\int_{-\infty}^{\infty}w(x) P_1(X) P_2^T(x) = 0_{n\times n}.$ For multidimensional case can you suggest me some literature for more insight. Thanks!
Comment: @mathreadler Thanks for your response! So you mean to say for any orthogonal polynomial $\int_{-\infty}^{\infty} P_i(x)^T W(x) P_j(x) = \delta_{ij}$, where $\delta_{ij}$ is the Kronecker delta function. If weight function $w(x) = \mathcal{N}(x;\,0,\,I)$, then how $w(x)$ will be converted into $W(x)$. Actually I tried to understand Gramian matrix, but could not form a matrix valued function $W(x)$ by using $w(x)$.
Answer: Okay, here is a primer on the stuff you need to know to see why we like Orthogonal Polynomials.

Vectors
In physics, vectors are usually things that live in three-dimensional space. In computer science, they tend to be lists of numbers. In mathematics, a vector is a thing that lives in a vector space. A vector space is designed so that all the simple things you want to do to vectors work: you can add them, there's a zero and additive inverses, you can scale them by multiplying by numbers, which are things in the field of scalars (which we can assume is $\mathbb{R}$ for the purposes of this discussion, so we are talking about real vector spaces), and all the usual properties extend from the vector space $\mathbb{R}^3$ over the field $\mathbb{R}$ that physics uses (what does not extend at this stage is the notion of product).
A vector space has a positive integer called the dimension associated to it, which is the smallest number of fixed vectors $v_i$ you need to generate the whole thing by using linear combinations $\sum_i \alpha_i v_i$, where $\alpha_i$ are scalars (e.g. $\mathbb{R}^n$ has dimension $n$). This number is the most important quantity associated to a vector space, because one can prove that over the same field, if two vector spaces have the same dimension, they are isomorphic. A set of $v_i$ of minimal size that generates the whole space in this way is called a basis.
The sensible sort of map on vector spaces is one that keeps the addition and the scalar multiplication structure intact:
$$ L\left(\sum_i \alpha_i u_i\right) = \sum_i \alpha_i L(u_i) $$
for any set of vectors $\{u_i\}$. These are called linear maps.
The definition of vector space covers the spaces we expect, but includes many objects that we would not initially expect to be vectors:

$\mathbb{R}^n$ is a vector space of dimension $n$, as it should be.
Polynomials of degree $n$ or less are a vector space of dimension $n+1$ (addition is defined pointwise). An example of a linear map is sending a polynomial $p$ to its value at a point, $p(a)$.
Polynomials of all degrees are also a vector space.
Continuous functions on an interval $[a,b]$ (written $C[a,b]$) form a vector space using pointwise addition.
Infinite sequences of real numbers $(a_1,a_2,\dotsc)$ are also a vector space

The last three of these are infinite-dimensional, so the previous discussion does not apply completely, but we'll need infinite-dimensional spaces. Worth remembering is that your ordinary basis is defined to generate the space using finite linear combinations, so an infinite-dimensional space can have a very weird (and very big) basis in this sense. (Polynomials aren't so bad because they are finite linear combinations already, so $\{1,x,x^2,\dotsc\}$ is a perfectly reputable basis.)
Inner products and Orthogonality
The easiest product on $\mathbb{R}^3$ to generalise is the dot product $u \cdot v = \sum_{i=1}^3 u_i v_i$. Over a real vector space $V$, an inner product is a function $\langle \cdot,\cdot\rangle : V \times V \to \mathbb{R}$ that is

symmetric ($\langle u,v\rangle=\langle v,u\rangle$),
nondegenerate ($\langle u,u\rangle>0$ for all $u \neq 0$), and
bilinear. (Linear in each argument. Symmetry means we only need to check the first argument, $\langle \lambda u+\mu v,w\rangle = \lambda \langle u,w\rangle + \mu \langle v,w\rangle $ for any scalars $\lambda,\mu$).

The ordinary dot product is one of these. The example we shall need to worry about is
$$ \langle f,g \rangle = \int_a^b f(x) g(x) w(x) \, dx, $$
where $f,g \in C[a,b]$ and $w$ is a positive function.
A vector space equipped with one of these is called an inner-product space.
The inner product essentially allows us to generalise the idea of angles, so we borrow the terminology from geometry: two vectors $u,v$ are said to be orthogonal if
$$ \langle u,v\rangle = 0. $$
A vector is normalised if $\langle u,u\rangle=1$. A set of vectors where each vector is normalised and each pair of vectors is orthogonal is called orthonormal.
In a finite-dimensional inner product space, if I give you any old basis, there is an algorithm (called Gram–Schmidt) that will produce an orthonormal basis.
One final thing: given a vector $u$ and an orthonormal basis $\{v_i\}$, there is a unique way to express $u$ as a linear combination $u=\sum_i \alpha_i v_i$: taking inner products of both sides with $v_j$ gives
$$ \langle v_j,u \rangle =\sum_i \alpha_i \langle v_i,v_j \rangle = \alpha_j, $$
using linearity of the inner product and orthonormality of the $v_i$. So
$$ u = \sum_i \langle v_i,u \rangle v_i. $$
This is where we have to take a short deviation into infinite-dimensional spaces and analysis, where things become rather more complicated.
Hilbert space
A Hilbert space is a complete inner-product space. Complete technically means that Cauchy sequences converge, but you can think of it as a space where every convergent sequence converges to something still in the space. Given an inner-product space, we can make a Hilbert space by adding the appropriate limits in. Here, convergence means that $v_n \to v$ if $\langle v_n-v,v_n-v \rangle \to 0$ (this is either called convergence in mean square or strong convergence, but it doesn't really matter since it's the only sort we consider).
Any finite-dimensional inner-product space is a Hilbert space (no converging needs to happen), but this is not true in infinite dimensions. $C[a,b]$ is not a Hilbert space (one can construct continuous functions that converge to a function with a jump in it, for example), but we can turn it into a Hilbert space using the inner product given above; this new space is called $L^2[a,b]$, the space of square-integrable functions on $[a,b]$. Note that convergence is not pointwise: functions in $L^2[a,b]$ "look the same" if they differ on a finite set of points (more is true, but we shan't need the fully general statement).
A orthonormal basis in a Hilbert space $H$ is a bit different: we mean an orthonormal set of vectors so that given any element $u$ of $H$, we can approximate $u$ as closely as we like by a finite linear combination of elements of the basis (we say that these finite linear combinations are dense in $H$). In the case of $L^2[0,2\pi]$ with $w=1$, the classic example is Fourier series: any element of $L^2[0,2\pi]$ can be written as $\sum_{n=-\infty}^{\infty} a_n e^{inx}$, so $\{e^{inx}\}$ form an orthonormal basis of $L^2[a,b]$.

Orthogonal Polynomials
With the preliminaries finally out of the way, we can finally move on to what orthogonal polynomials are. Given $L^2[a,b]$ with a $w$ so that polynomials have finite integral (e.g. $1$ on [-1,1], $e^{-x}$ on $[0,\infty)$), polynomials form a dense subspace of $L^2[a,b]$ (this is a consequence of something called the Stone–Weierstrass theorem, but never mind about that). Hence, one might want to approximate functions in $L^2[a,b]$ by polynomials. To do so, one requires an orthonormal set of polynomials, and this is where orthogonal polynomials come in.

Definition: Let $\langle f,g \rangle = \int_a^b f(x) g(x) w(x) \, dx$ (i.e. the inner product on $L^2[a,b]$ with weight $w$). A sequence of polynomials $(P_n)_{n\geqslant 0}$ are called orthogonal polynomials for this space if

$P_n$ has degree $n$.
$\langle P_n,P_m \rangle = 0 $ when $n \neq m$.

One can show that each polynomial is unique up to an overall scaling. How? Apply Gram–Schmidt to the sequence $1,x,x^2,\dotsc$! For example, a simple computation of this kind gives the first few Legendre polynomials, $1,x,(3x^2-1)/2,\dotsc$. For various reasons, the scaling of orthogonal polynomials is not usually chosen so that $\langle P_n,P_n \rangle = 1$. The Legendre polynomials, for example, are normally defined to be scaled so that $P_n(1)=1$.
Gram–Schmidt is rather inefficient when you want a lot of terms. More commonly, orthogonal polynomials are defined by generating functions of one sort or another, which is often where the funny scalings come from.
Given a function in $L^2[a,b]$, we can expand it in terms of orthogonal polynomials in exactly the same way as one employs a Fourier series: it gives an expansion of the function in terms of simpler functions that are orthogonal to one another.
The usefulness of this expansion over the Fourier expansion often lies in the polynomials being solutions to certain differential equations. For example, Legendre polynomials appear entirely naturally when solving Laplace's equation in spherical coordinates, and one can use them to expand an axisymmetric solution to Laplace's equation in what is called the multipole expansion. This ability to expand functions in terms of solutions of a differential equation is a specific case of a more general phenomenon, which goes under the general title of Sturm–Liouville theory. Orthogonal polynomials are a particularly nice example of this.
Perhaps the best way to think of expansion in terms of orthogonal polynomials is that the coefficients $\alpha_i$ in $f(x) - \sum_{i=0}^n \alpha_i P_i(x) = R(x)$, where $R(x)$ is the error in the approximation, is that the $\alpha_i$ are chosen so that for a given $n$, $ \int_a^b (R(x))^2 w(x) \, dx $ is minimised: $\sum_{i=0}^n \alpha_i P_i$ is the best approximation of $f$ (in this sense) by a polynomial of degree at most $n$. The orthogonality makes it easy to derive what the coefficients must be, and it corresponds precisely with the $\alpha_i = \langle f,P_i \rangle / \langle P_i,P_i\rangle$ that we obtain by analogy with both the Fourier series idea and the calculation in finite-dimensional space.
One may also understand $\sum_{i=0}^n \alpha_i P_i$ as the projection of $f$ onto the finite-dimensional space spanned by $P_0,P_1,\dotsc,P_n$; we note the analogy with orthogonal projection of a point onto a plane in three dimensions, which also gives the closest point in the plane to the original point.

Intuition of orthogonal polynomials?


What is the probability that an arrangement of 'FACETIOUS' begins with A and ends with I?

Question: In how many ways can the letters of the word FACETIOUS be arranged in a line?
What is the probability that an arrangement begins with A and ends with I ?
I understand the first part which will be $9!= 270725$  since there are $9$ letters in the word.
I'm stuck with that last part, do I have to group the letters and do the combination formula ?
Comment: There  are $7$ letters left, which can be arranged in $7!$ ways.
Answer: How many possible ways can the letters be arranged? $9!$
This is all the possible arrangements of the letter.
To use the conditions, you have to put A at the beginning, and you have to put I at the end. Hence you have $7$ letters left to play with. How many ways can you arrange these? $7!$.
So, the probability of interest is
$$\frac{\text{Arrangements with condition}}{\text{All possible arrangements}} = \frac{7!}{9!} = \frac{1}{9(8)} = \frac{1}{72}.$$


How to combine 2 array to an Object?

Question: I have 2 arrays which are the same length like this:
$scope.access_name = ['group1','group2','group3','group4']

access_value=[1,2,1,3]

How can I combine to have result like this:
object_access = {'group1:1','group2:2','group3:1','group4:3'}

thankyou very much
Answer: var object_access={};
for(int i=0;i<access_value.length;i++)
{
  object_access[$scope.access_name[i]]=access_value[i];
}

How to combine 2 array to an Object?


Create multiple instances of the same function with different variables each time in a Socket.io event

Question: I need to create multiple instances of the same function with different variables passed from the front end in a socket.io event.  I also need these variables visible to another socket.io event so that I can stop the function from a front end click.  I have everything working great, but I can only use it for one instance with a set variable 'bigLoop' and need it to work for multiple instances. I need all of the loops to be able to run and independent of each other.
The socket event:

    //this is what I have so far and it works great, I can start and stop at any time. And I know bigLoop is global
    io.on('connection', function(connect) {
        //create new loop
       connect.on('createLoop', function(data) {
           bigLoop = new syncLoop(data.total, function(){}, function(){});
       });
        //end loop
       connect.on('endLoop', function(data) {
           bigLoop.break(true);
       });
    });

  //this is what I would like passing an array of multiple variables from the front end and creating a new syncLoop for each variable
        //create new loop
        connect.on('createLoop', function(data) {
           //save array to variable
           var newNames = data.array;
           //for each variable create new syncLoop
           newNames.forEach(function(newLoops) {
               newloops = new syncLoop(data.total, function(){}, function()  {});
           });
        });

        //end loop
       connect.on('endLoop', function(data) {
           //save array to variable
           var endNames = data.array;
           //for each variable end loop
           endNames.forEach(function(endLoops){
               endLoops.break(true);
           });

       });

//syncLoop function, for process and exit I have a lot more going on but did not want to write it
function syncLoop(iterations, process, exit){
 var index = 0,
     done = false,
     shouldExit = false;

    loop = {
        next:function(){
            if(done){
                if(shouldExit && exit){
                    return exit(); // Exit if we're done
                }
            }
            // If we're not finished
            if(index < iterations){
                index++; // Increment our index
                process(loop); // Run our process, pass in the loop
            // Otherwise we're done
            } else {
                done = true; // Make sure we say we're done
                if(exit) exit(); // Call the callback on exit
            }
        },
        iteration:function(){
            return index - 1; // Return the loop number we're on
        },
        break:function(end){
            done = true; // End the loop
            shouldExit = end; // Passing end as true means we still call the exit callback
        }
    };
    loop.next();
    return loop;
};

I have tried a lot of different things and I cannot get this to work.  I appreciate any help.  Thank you.
Answer: If I understand your question correctly:
Try creating a global map of all the running loops:
// global variable
var loops = {};

function createLoop(name, data) {
  stopLoop(name);
  loops[name] = new syncloop(/*...*/);
}

function stopLoop(name) {
  if (loops[name]) {
    loops[name].break(true);
    delete loops[name]; // remove entry from map
  }
}

The variable loops would contain all of the currently-running loops, by name.
Comment: THANK YOU!!!! I just adjusted the code, and it is starting and stopping loops and multiple loops.
Comment: I did, but I do not have a reputation of 15 yet.  It is recorded but not publicly displayed. I will try and up my reputation and come back and click again. Thank you.
Comment: Oh, ok. Glad I could help.


Total variation distance, $L^1$ norm

Question: Total variation distance is a measure for comparing two probability distributions (assuming that these are unit vectors in a finite space- where basis corresponds to the sample space ($\omega$)). I know a distance measure need to obey triangle inequality and it should satisfy that orthogonal vectors have maximum distance and the same distributions should have distance $0$. Others should like between these two. I completely don't understand why the $L^1$ norm is chosen for measuring the distance between these vectors (prob. distributions).
I also want to know why it is exactly defined the way it is. $TV(P_1,P_2) = \frac{1}{2}\sum_{x \in \omega} \mid {P_1(x)-P_2(x) \mid}$
Comment: Yes. That's what but here $f,g$ are probability distributions. I don't understand why $L^1$ is used and why 1/2 comes in the way?
Comment: The total variation distance of two probability measures is usually defined in terms of the sup norm: https://en.wikipedia.org/wiki/Total_variation_distance_of_probability_measures
Comment: "When the set($\omega$) is countable, the total variation distance is related to the $L^1$ norm by the identity." So, this follows by doing some math. But what is the intuitive explanation for this?
Comment: See Proposition 4.2 in https://pages.uoregon.edu/dlevin/MARKOV/markovmixing.pdf
Answer: The TV distance is measureing exactly what you want: The maximal difference between the assignments of probabilities of two probability distributions P and Q. And it is hence defined as
$$TV(P,Q)=sup_{A\subset\Omega}|P(A)-Q(A)|$$
Now, as is shown in Proposition 4.2 here your last equation
$$TV(P,Q)=\frac{1}{2}||P-Q||_1$$
is true (in countable prob. spaces). I will not redo the proof here, you can get a glimpse using the characterizations am_rf24 writes about in his answer. But I can give you some intuition: Although the definition of the TV distance seems closely resembling the definition of the infinity norm on vectors, it is actually subtly different. Note that the TV distance is defined over events (aka subsets) of $\Omega$ while the infinity norm is over elements of $\Omega$. So to conclude: other norms could be chosen. But because it is a norm we luckily do not need to show that the TV distance is a norm.
(Another remark: the orthogonality criterion you mention is not really a thing for a norm, as you can have norms without having a scalar product ;) )
Answer: The two observations for characterizing the total variation distance using the two definitions are $\\$:
 $ 1. \max{P_1(A)-P_2(A)} = \max{P_2(\omega-A)-P_1(\omega-A)} \\$
$2. P_1(\omega)= P_2(\omega) \implies P_1(A)- P_2(A) = P_2(B) -P_1(B) = \max_{A \subset \omega}( P_1(A)- P_2(A)) $
Now, using $ 1/2 \sum_{x}{\mid{P_1(x)-P_2(x)}\mid}= \frac{1}{2}\sum_{x:P_1(x)>P_2(x)}{{(P_1(x)-P_2(x))}}+  \frac{1}{2}\sum_{x:P_2(x)>P_1(x)}{{(P_2(x)-P_1(x))}}$ $ \qquad \qquad \qquad \qquad= \frac{1}{2}\max_{A \subset \omega}{(P_1(A)-P_2(A))} + \frac{1}{2} \max_{B \subset \omega}{(P_2(B)-P_1(B))}$
$ \qquad \qquad  \qquad \qquad=   \max_{A \subset \omega}{ \mid P_1(A)-P_2(A)\mid}$
Comment: Hi, could you suggest any reference about the proof you have provided? thanks! :-)
Indeed, I just asked a question about your proof https://math.stackexchange.com/questions/4771237/total-variation-distance-max-a-subseteq-mathcala-left-pa-qa-righ?noredirect=1#comment10135327_4771237
Answer: I can provide a little intuition as to why the relation between $TV$ distance and $L_1$ norm holds. For the sake of simplicity, assume $P_1$ and $P_2$ are probability mass functions with finite support. We can make the following claim:
In the notation of sani's answer, for every element $a$ in the optimal subset $A^\ast$, it must be that $P_1(a) \geq P_2(a)$. Otherwise, the difference in probabilities decreases the objective function, and hence cannot be a part of $A^\ast$.
Using the above claim, it follows that for all $b$ in $(A^\ast)'$, it must be that $P_1(b) \leq P_2(b)$. Let $p_1$ and $p_2$ denote $P_1(A^\ast)$ and $P_2(A^\ast)$, respectively. Then, $TV(P_1,P_2) = p_1 - p_2 \geq 0$. The $L_1$ norm can be written as
\begin{align}
 L_1(P_1,P_2) & = | P_1(A^\ast) - P_2(A^\ast) | + | P_1((A^\ast)') - P_2((A^\ast)') | \\
& = \big( P_1(A^\ast) - P_2(A^\ast) \big) -  \big( P_1((A^\ast)') - P_2((A^\ast)') \big) \\
&  = (p_1 - p_2) + (~(1-p_2) - (1-p_1)~) \\
& = 2(p_1 - p_2) = 2~TV(P_1,P_2).
\end{align}

Total variation distance, $L^1$ norm


Pandas DataFrame: How to calculate the difference by first row and last row in group?

Question: Here is my pandas DataFrame:
import pandas as pd
import numpy as np

data = {"column1": [338, 519, 871, 1731, 2693, 2963, 3379, 3789, 3910, 4109, 4307, 4800, 4912, 5111, 5341, 5820, 6003, ...],
         "column2": [NaN, 1, 1, 1, 1, NaN, NaN, 2, 2, NaN, NaN, 3, 3, 3, 3, 3, NaN, NaN], ...}

df = pd.DataFrame(data)
df
>>>      column1  column2
0        338      NaN
1        519      1.0
2        871      1.0
3       1731      1.0
4       2693      1.0
5       2963      NaN
6       3379      NaN
7       3789      2.0
8       3910      2.0
9       4109      NaN
10      4307      NaN
11      4800      3.0
12      4912      3.0
13      5111      3.0
14      5341      3.0
15      5820      3.0
16      6003      NaN
17      ....      ....

The integers in column2 denote "groups" in column1, e.g. rows 1-4 is group "1", rows 7-8 is group "2", rows 11-15 is group "3", etc.
I would like to calculate the difference between the first row and last row in each group. The resulting dataframe would look like this:
df
>>>      column1  column2  column3
0        338      NaN      NaN
1        519      1.0      2174
2        871      1.0      2174
3       1731      1.0      2174
4       2693      1.0      2174
5       2963      NaN      NaN
6       3379      NaN      NaN
7       3789      2.0      121
8       3910      2.0      121
9       4109      NaN      NaN
10      4307      NaN      NaN
11      4800      3.0      1020
12      4912      3.0      1020
13      5111      3.0      1020
14      5341      3.0      1020
15      5820      3.0      1020
16      6003      NaN      NaN
17      ....      ....     ...

because:
2693-519 = 2174
3910-3789 = 121
5820-4800 = 1020

What is the "pandas way" to calculate column3? Somehow, one must iterate through column3, looking for consecutive groups of values such that df.column2 != "NaN".
EDIT: I realized my example may lead readers to assume the values in column1 are only increasing. Actually, there are intervals, column intervals
df = pd.DataFrame(data)
df
>>>    interval      column1  column2
0      interval1     338      NaN
1      interval1     519      1.0
2      interval1     871      1.0
3      interval1     1731      1.0
4      interval1     2693      1.0
5      interval1     2963      NaN
6      interval1     3379      NaN
7      interval1     3789      2.0
8      interval1     3910      2.0
9      interval1     4109      NaN
10     interval1     4307      NaN
11     interval1     4800      3.0
12     interval1     4912      3.0
13     interval1     5111      3.0
14     interval1     5341      3.0
15     interval1     5820      3.0
16     interval1     6003      NaN
17      ....      ....
18     interval2     12        13
19     interval2     115       13
20     interval2     275       NaN
....

Answer: You can filter first and then get difference first and last value in transform:
df['col3'] = df[df.column2.notnull()]
                  .groupby('column2')['column1']
                  .transform(lambda x: x.iat[-1] - x.iat[0])
print (df)
    column1  column2    col3
0       338      NaN     NaN
1       519      1.0  2174.0
2       871      1.0  2174.0
3      1731      1.0  2174.0
4      2693      1.0  2174.0
5      2963      NaN     NaN
6      3379      NaN     NaN
7      3789      2.0   121.0
8      3910      2.0   121.0
9      4109      NaN     NaN
10     4307      NaN     NaN
11     4800      3.0  1020.0
12     4912      3.0  1020.0
13     5111      3.0  1020.0
14     5341      3.0  1020.0
15     5820      3.0  1020.0
16     6003      NaN     NaN

EDIT1 by your new data:
df['col3'] = df[df.column2.notnull()]
                  .groupby('column2')['column1']
                  .transform(lambda x: x.iat[-1] - x.iat[0])
print (df)
     interval  column1  column2    col3
0   interval1      338      NaN     NaN
1   interval1      519      1.0  2174.0
2   interval1      871      1.0  2174.0
3   interval1     1731      1.0  2174.0
4   interval1     2693      1.0  2174.0
5   interval1     2963      NaN     NaN
6   interval1     3379      NaN     NaN
7   interval1     3789      2.0   121.0
8   interval1     3910      2.0   121.0
9   interval1     4109      NaN     NaN
10  interval1     4307      NaN     NaN
11  interval1     4800      3.0  1020.0
12  interval1     4912      3.0  1020.0
13  interval1     5111      3.0  1020.0
14  interval1     5341      3.0  1020.0
15  interval1     5820      3.0  1020.0
16  interval1     6003      NaN     NaN
18  interval2       12     13.0   103.0
19  interval2      115     13.0   103.0
20  interval2      275      NaN     NaN

Comment: Will this only work if values in `column1` are increasing? I realized my example may be slightly misleading. See above.
Comment: I think it working too. But there is multiple groups `1` splitted by `NaNs` and need processing this groups separately?
Comment: I think e.g.in `column2` `2.0` is replace by `1.0`
Comment: There is only one unique group `1`, one group `2`, etc. The groups in `interval2` will never be the same as the groups in `interval1`
Comment: Ok, so what is problem? Need last value in group substract with first one? Can you explain more?
Comment: There is an interval, `interval1` with increasing values. Each value is labeled. There are unique groups labeled by integers. `interval1` may have groups 1,2,3,4,5. `interval2` may have groups 6,7,8,9,10,11,12, etc.
The task is the same though: For each group, there is a beginning value (first row) and ending value (last row). I need to know the difference between these two values. Is this clear?
Comment: so need change `.groupby('column2')` to `.groupby(['interval','column2'])` ?
Comment: Unfortunately, I'm still getting some errors. Does `df['col3'] = df[df.column2.notnull()] .groupby(['interval','column2'])['column1'] .transform(lambda x: x.iat[-1] - x.iat[0])` work if the `NaN` values are actually 0?
Comment: If `NaN` are replaced to `0` in `column2`, then need change `df[df.column2.notnull()]` to `df[df.column2 != 0]`
Comment: The solution shows a 'Stata way' outcome. Often one would only want to analyse Column 3 (col3) for each group. Is there a solution which only keeps columns 'interval', column1 and column2?
Comment: Unfortunately I do not understand. Can you create new question?
Comment: To clarify, col3 contains many repeated observations. If you are only interested in analysing col3 for 'interval' and column2, then the procedure should ideally produce a frame with 'interval', column2, and col3. (Apologies for the typo in the previous comment, it should not have mentioned column1).
Comment: So change variables names dont help like `df['col3'] = df[df.column2.notnull()]
                  .groupby('interval')['column2']
                  .transform(lambda x: x.iat[-1] - x.iat[0])` ? Because `transform` function return new column in original df, so it should work always. But I dont know what are your column(s) for grouping, so maybe need change `.groupby(['interval', 'column2'])['column2']` for grouping by interval with column2. Column after groupby, here column2 (`.groupby('interval')['column2']`) means column for applying lambda function. Need numeric column, because difference.


Page Arguments and Dynamic Pages

Question: I am trying to make dynamic pages using hook menu which is going fine but the problem is I want to add page arguments for the states. For example if someone goes to the url and there isnt a state in the url like california for example(site/listings/california) it would show a page not found error, and if found it would list the listings I have looped out in my function.
Any Suggestions?
/*
** Hook Menu
*/

function listings_menu() {
    $items = array();

    $items['site/listings/%'] = array(
        'title' => 'City Listings',
        'page callback' => 'business_listings',
        'access arguments' => array('access content'),
        'page arguments' => array('states', 'city_names'),
    );

    return $items;
}

/*
** State Listings
*/

function listing_states($state){
    $state = array(
        'alabama' => t('Alabama'),
        'alaska' => t('Alaska'),
        'arizona' => t('Arizona'),
    //ect.....

    );
    return $state;
}

/*
** Company Listings
*/

function business_listings(){

    $states = listing_states(arg(2));

    $dbquery = db_query("SELECT DISTINCT CityName FROM {zipcodes} WHERE StateName = '".ucfirst(arg(2))."' ORDER BY CityName ASC");

    $cityname = '';
    $cityname .= '
';
    foreach ($dbquery as $dbrow) {
        $cityname .='
';
        $cityname .= 'CityName)).'">'.$dbrow->CityName.'';
        $cityname .= '
';
    }
    $cityname .= '
';

    return array('states' => $states, 'city_names' => $cityname);
}

Answer: I believe you want to change your listings_menu()
function listings_menu() {
    $items = array();

    $items['site/listings/%'] = array(
        'title' => 'City Listings',
        'page callback' => 'business_listings',
        'access arguments' => array('access content'),
        'page arguments' => array(3),
    );

    return $items;
}

Then you need to change your listings to accept that argument
function business_listings($states){
  $states = listing_states($states);

  $dbquery = db_query("SELECT DISTINCT CityName FROM {zipcodes} WHERE StateName = '".ucfirst(arg(2))."' ORDER BY CityName ASC");

  $cityname = '';
  $cityname .= '
';
  foreach ($dbquery as $dbrow) {
      $cityname .='
';
      $cityname .= 'CityName)).'">'.$dbrow->CityName.'';
      $cityname .= '
';
  }
  $cityname .= '
';

  return array('states' => $states, 'city_names' => $cityname);
}

Answer: Update your menu callback to pass the third argument (state) to the page callback:
$items['site/listings/%'] = array(
  'title' => 'City Listings',
  'page callback' => 'business_listings',
  'access arguments' => array('access content'),
  'page arguments' => array(2),
);

Then update your page callback to use the new argument, validate it against the items returned by listing_states() (which doesn't look like it needs to accept an argument), and throw a 404 if validation fails:
/**
 * State Listings
 */
function listing_states() {
  $states = array(
    'alabama' => t('Alabama'),
    'alaska' => t('Alaska'),
    'arizona' => t('Arizona'),
    // ...
  );

  return $states;
}

/**
 * Company Listings
 */
function business_listings($state) {
  $all_states = listing_states();

  if (!isset($all_states[$state])) {
    drupal_not_found();
  }

  // your other code here
}

Additionally, you should really read up on Drupal's coding best practices, as there are numerous issues with your code. Other than formatting, your query is vulnerable to a SQL injection attack, your markup should be in a template file or a theme function, your CSS should be in an external CSS file, and your link should be using the l() function (or at least url() for the href attribute).
Comment: Thanks, yeah I know this is pretty sloppy, this is just a rough draft of the code, you know how it is when time is against you lol.

Page Arguments and Dynamic Pages


counting number of rows in mysql column

Question: I have a table which looks like,
 | user_id | url | is_bookmark | is_history    <== schema

sample data:
[2, "http://www.stackoverflow.com", 1 ,0]
[3, "http://www.stackoverflow.com", 1 ,0]
[4, "http://www.stackoverflow.com", 1 ,1]
[5, "http://www.php.net", 1 ,0]

and need the following table,
 |  url | total_bookmark | total_history    <== schema

sample data for this table
["http://www.stackoverflow.com",  3 , 1]
["http://www.php.net",  1 , 0]

I do this using 2 queries, but can it be done in 1?
Answer: try this:
 select url , count(*) total_bookmark, sum(is_history) total_history
 from yourtable
 group by url

DEMO
Comment: works, but out of curiousity, why did you choose `count(*) total_bookmark` instead of `sum(is_bookmark)`?
Comment: its the same , because you are grouping by url , so it will count all urls :) , you can even use count(1)
Comment: But it won't be the same when `is_bookmark` has a value of 0 or NULL; If that column is implementing a boolean the way we implement them here, we'd get a count of the `TRUE` values using an expression like this: `SUM(is_bookmark<>0)`, though `SUM(is_bookmark)` would also work, because we don't store anything but 0, 1 or NULL in the column.
Answer: Use aggregate function sum on both the columns bookmark and history.
If any of them are unset from book marked or history status, count will not work but sum works.
Try this:
select
    url,
    sum( is_bookmark ) total_bookmark,
    sum( is_history ) total_history
from table
group by url

Comment: NOTE: this assumes that `is_bookmark` and `is_history` are numeric, and will only be assigned values of 0, 1 or NULL (or character which will evaluate to numeric values of 0, 1 or NULL).
Answer: Did u mean this ?
select count(*) as total,is_bookmark,url,sum(is_history) total_history
from table
group by `url`

counting number of rows in mysql column


Probability of passing an exam .

Question: A student (mohan) appears in two exams . Probability of failing in these exams are (1/4) and (3/20) respectively . We have to find the probability of failing in both exams .
I tried it as
P = (1/4)(3/20)
But my answer is wrong why
In a book the answer is given as
Comment: I think your question should be "failing at least one exam" not what you wrote "failing both exams".
Comment: Are there $4$ questions on the first exam and $20$ questions on the second exam?
Comment: That formula is only valid if the event of failing the first exam and the event of failing the second exam are independent. Do you have any more information?
Comment: Does $1/4$ mean if you get a score lower than or equal to $1$ then you fail?
Comment: @JackyChong no (1/4) is the chance of failing in an examination
Comment: Then there must be something missing from your question. Otherwise, your answer looks correct.
Comment: The book looks wrong.
Comment: The probability calculated in the book is the complement of the event "Pass test 1 and test 2", but the complement of that event is "Fail at least one test", not "Fail both tests". Either the book is wrong, or you've misunderstood what the original question is asking.
Answer: Your book's solution tells you that the probability of failing both exams is one minus the probability of passing both exams.   No, that is the probability of failing in at least one exam.
You gave the correct answer for the probability of failing in both exams.    Is this the question that was asked?

Remark: Of course, both answers assume that the events of the student passing each of the exams is independent of the other, contrary to the purpose of testing.
Comment: But what is the wrong method done in the book
Comment: @koolman Nothing was wrong with the method; it was just the answer to a different question.

Probability of passing an exam .


how to join on the values returned from Json_array_elements in postgresql?

Question: Structure of "Order" table:
objectId text,
subOrders jsonb

structure of Jsonb: array of jsonb objects. eg.
[
   {
      "className":"SubOrder",
      "__type":"Pointer",
      "objectId":"tIENf1PCHO"
   },
   {
      "className":"SubOrder",
      "__type":"Pointer",
      "objectId":"yKVDFEuWx7"
   }
]

Structure of "SubOrder" table:
objectId text,
orderitems jsonb

structure of suborder.orderitems jsonb, eg:
[
   {
      "className":"SubOrderItem",
      "__type":"Pointer",
      "objectId":"6d1DLrmOTD"
   },
   {
      "className":"SubOrderItem",
      "__type":"Pointer",
      "objectId":"xK6mR2PDDU"
   }
]

I need to join Order.suborders.objectId with suborder.objectid
AS Order.suborders column is a jsonb array of objects, I am taking the array elements as below.
select jsonb_array_elements("subOrders")->>'objectId'
from "Order"

Then is the problem to join the returned objectId with the Suborder table.
Comment: @ a_horse_with_no_name thanks for editing the question.  Please help.
Answer: Put the function call into the FROM clause and de-reference the actual field of the JSON object later.
select o.object_id, so.*
from "Order" o
  cross join lateral jsonb_array_elements(sub_orders) j(suborder)
  join sub_order so on so.object_id = j.suborder ->> 'objectId'

Online example: http://rextester.com/GQBF88668
Comment: my query is like thisselect o.*, j.*, ot.*
from "Order" o
  cross join lateral select jsonb_array_elements("subOrders")->>'objectId' as j(object_id) from "Order"
  join "SubOrder" ot on ot."objectId" = j.object_id; and my error is syntax error at or near "->>"
LINE 3: ...oss join lateral jsonb_array_elements("subOrders")->>'object...  Please help
Comment: all my objectid are text fields only.
Comment: select o.*, j.*, ot.*
from "Order" o
  cross join lateral jsonb_array_elements(o."subOrders")->>'objectId' as j("object_id")
 join "SubOrder" ot on ot."objectId" = j."object_id";... sorry that one is not correct. even with this there is same error.'
Comment: Edited my question with the structure of the tables involved and the requirement. Thanks.
Comment: Done. Thank a lot.
Comment: Is the `lateral` required?
Answer: I would use WITH Queries
WITH suborders_object AS(
select jsonb_array_elements("subOrders")->>'objectId' AS obid from Order
)
SELECT * FROM suborders_object JOIN Order ON (suborders_object.obid = Order.id);

or any way you like to join suborders_object as like a table.
Comment: Thanks much Ashraful. can you also share the other method like joining with other table pls?
Comment: What about **[PostgreSql : Json Array to Rows using Lateral Join](https://stackoverflow.com/questions/61709474/postgresql-json-array-to-rows-using-lateral-join)**?


How does the performance of "rubato" differ in the classical and romantic styles?

Question: In the movie Amadeus, there's a scene of a chamber orchestra where the conductor uses a large pole and pounds the floor to indicate downbeats in a strict time. And in earlier music, such as Bach fugues, the complexity of the rhythmic subdivisions and independent lines seems to demand a very strict time.
But rubato wasn't invented just for the Romantic era, right? Classical-era music requires some small degree of "breathing", right?
For concrete examples, how should my rubato differ when playing Mozart's Piano Sonata, KV 331 vs. Beethoven's Piano Sonata No. 18, Op. 31/3?
Edit: This question was inspired by a memory of my (Russian) piano teacher telling me that I played Fur Elise too freely, and that I needed to be more strict with the time to be accurate for the period. While I didn't really heed the advice at the time, it has had a growing influence on me these past 20 years. So I'm more interested in the *scholarly" view, how to be period-appropriate.
Answer: The portion of Amadeus to which you refer is unfortunately a rather accurate depiction of a practice that has thankfully passed, that of using pounding large staff on stage to keep time. Jean-Baptiste Lully was literally an unfortunate casualty of this practice.
As for Rubato, the Harvard Dictionary of Music offers two related definitions. The main distinguishing factor is that an older sense of the term occurs at a micro-level, changing note durations in the melody without allowing the beat to move around, and that includes accompaniment that closely follows the beat, say in the singing of an aria. This sense of the word carries forward to every popular recording today. You could claim that every commercially popular singer makes use of this sense of rubato no matter how quantized the accompanying tracks are. In truth, here, you aren't robbing so much as constantly borrowing time, which you are also constantly repaying. By this standard, you would have to put Frank Sinatra at the very top of the list!
The second sense of rubato is the "modern" sense in which changes in tempo occur "in all parts at the same time without any compensation". We typically point to Liszt for this sense of the term. However, as a matter performance, the documentary records for this sense of the term go all the way back to Frescobaldi.
Important to your question of the difference in the sense of rubato in the case of Mozart v. Beethoven, my answer would be not too terribly much. Let me explain.
There is a spectrum of time manipulation between absolute metronomic rigidity and absolute improvisatory fluidity approaching chaos. Within that larger spectrum of what is possible there is a certain range of acceptability along the spectrum for any given passage of any given work. By means of physical comparison, a passage is kind of like chop sticks. For any given pair of chopsticks, there is a certain amount of bending possible before you snap and break them. You could make the same analogy with glass. So that range of flexibility can be generalized to given periods, or better, given composers. Bartok, for instance, gives us not only metronome markings, but precise timings for sections as a second check against grossly deforming his works. He believes his music to be more delicate with regards to timing, so one wrong move and snap. Mahler, on the other hand, gives loads of instruction with regard to tempo, some metronome markings, but a degree of sturdy flexibility to his music. It seems that his approach to harmony and orchestration further supports this.
As for Mozart and Beethoven, and my answer ("not much"): the two occupy a close relationship in time and practice. I don't want to suggest in any way that this means that their music sounds similar. This is not the case to all but the most basic unacquainted listeners. But they occupy a similar placement in history, and it is logical to see that their music, as performed by their contemporaries, would have received somewhat similar approaches to performance. By that I mean that words like Allegro and cantabile still meant roughly the same thing to Mozart's contemporaries in the 1780's as it did to Beethoven's in the 1820's.
The yawning chasm between the two is the abundance of additional performance instructions that Beethoven gives. Remember that not until Gabrieli did we even have the innovation of notated dynamics but even Mozart did not fuss too greatly with them, frequently limiting himself to simply p and f with fewer marked crescendi and diminuendi, while Beethoven added more of these, including the more frequent use of pp or ff.  (Caveat: Mozart uses almost all of these markings some times, but it is a question of frequency.) This process of specificity exploded further by the time of Tchaikovsky and still further with Mahler. But does that mean that we do not want to make a distinction between different forte sections in Mozart, or shape the phrase? I say no. It is simply that Mozart generally left it to the performer, familiar with the prevalent style, to know what would be appropriate, while Beethoven explicitly asks the performer, lest there be any doubt. All of these comments map almost perfectly onto their use of expressive tempo markings. Mozart tends toward the implicit, Beethoven toward the explicit. In fact, it is even more true in the case of tempo.
Take for instance, your examples. It's hard to make a direct comparison because of the number of variables. What would be easier would be to compare opening movements in the genre that begin in the same key, same time signature, and same tempo marking. But just to quickly compare, KV331 opens immediately with a stable tempo that is maintained until variation V, but with no indication as to how or when to change tempo. Beethoven, on the other hand, starts Op. 31/3 Allegro, but can't even make it three measures without giving directions for a change in tempo! So you see that there is a wide diversity between the musical style of the composers in how they manage time, but less diversity in how much we should additionally mold it beyond the directions of the composer. Not that we shouldn't do it, but that we shouldn't do it altogether differently. A comparison of the range of acceptable rubato usage between the performance of Mozart and of Beethoven would show that in both cases, one would be wise to tastefully but very judiciously shape time. The best way to see this is in careful study of many different recordings of the same work.
For the most thorough set of interpretive analyses of recordings that I've ever encountered, look into Gunther Schuller's The Compleat Conductor. It is a fascinating look, microscope in hand, at 8 masterworks. It contains dozens of tabular comparisons between the approaches of different conductors, each table to one very small phrase or interpretive decision, to get a sense of what the range is. Most often, given that tempo is the conductor's most essential domain, a trend emerges as to what consensus, if any, there may be between a range of interpreters. I would love to know if anyone has made a similar effort in covering another genre or other works.
I'll be interested in hearing your response or the response of others. I'd especially be interested in anyone who would like to compare two more similar movements in Mozart and Beethoven along the lines I suggested, to see what more specific truths can be added to this as far as performance approach.
Answer: Rubato is 'free of tempo'. So this means you can play the tempo in any way you like.
It think you are mixing up two things.
(1) Playing Rubato in a specific style, would make it sound different. (one could say: the intellectual way)
(2) The term rubato that just means 'freely' or 'stolen time' (one could say: the way you feel it should be)
Look for example how Glenn Gould plays Beethoven's Piano Sonata, Op. 31/3 versus somebody who is more a scholar. (Gould would be (2) and the scholar would be (1) )
So to give you a more concrete answer in the form of a question: How would you like to sound? Is it for study purposes? Or is it to create your own 'version' of the composition?
PS:
Actually you can study both :-)
Comment: I've edited to give some background. It's more the 'scholarly' view I'm looking for. Comparing Glenn Gould's performances of the two, he appears to play the Mozart *even more freely* than the Beethoven!


factor $3+\sqrt{3}$ into irreducibles in $\mathbb{Z}[\sqrt{3}]$

Question:
Factor $3+\sqrt{3}$ into irreducibles in $\mathbb{Z}[\sqrt{3}]$.

I have done the norm which is $6= 2 \cdot 3$. And i have tried two find $a$ and $b$ such that the norm of $a+b\sqrt{3}$ is $2$ and similarly with $3$, but then i get that there are no solutions (and that means that it is irreducible?) so I think I may be doing something wrong. I am not quite sure how to solve this.
Thank you
Sorry for the format i am not used to it-
Comment: No problem, the \$ should enclose the expressions, so you would use `$\sqrt{3}$` for $\sqrt{3}$ instead of just `$\sqrt{3}`
Answer: $$3 + \sqrt{3} = (1 - \sqrt{3})(-3 - 2\sqrt{3})$$
This is one possible factorization of $3 + \sqrt{3}$ into irreducibles. Noting that the first factor has norm $2$ and the second one $3$, proves that the factors on the RHS are indeed irreducibles.
Comment: @AngelaIg More or less yes. I tried to solve the equation $a^2 - 3b^2 = \pm 2$ and that's how I found the factorization.
Comment: The norms are $-2$ and $-3$.
Comment: @lhf I tend to use $N(a + b\sqrt{3}) = |a^2 - 3b^2|$. Considering the absolute value changes nothing, right?
Comment: thank you, but how do you find such numbers , for example, for a^2-3b^2=2 ,how do you get to the solution ? trying which one fits?
Answer: You need to consider factors with norm $\pm 2, \pm 3$.
Anyway, it is easy to find a factorization of $3+\sqrt{3}$ by inspection:
$$
3+\sqrt{3} = \sqrt{3}(\sqrt{3}+1)=(0+\sqrt{3})(1+\sqrt{3})
$$
The first factor has norm $-3$ and the second factor has norm $-2$. Since the norms are prime, the factors are irreducible.


Display batch operations messages

Question: I have defined a batch, everything works ok, but I want to understand how the messages are displayed. So I have this batch:
$batch = array(
  'title' => t('Generate reports'),
  'operations' => array(
    array('create_report_callback', array($time_min, $time_max)),
  ),
  'init_message' => t('Starting report generation...'),
  'progress_message' => t('Generating report. Operation @current out of @total.'),
  'error_message' => t('Error detected. Please try again!'),
  'finished' => 'create_report_finished',
);

What I want to know is how to change the progress_message to something else than Operation @current out of @total. I don't know where the @current and @total come from
Thank you.
Comment: You can just change it to whatever you like, those are placeholders the batch processor will inject automatically if necessary. They aren't mandatory, just available
Comment: Ok, clear enough. But is it possible to have a dynamic message, with custom placeholders? Basically to generate the message inside the batch process function.
Comment: The docs suggest it's not possible without altering core: https://api.drupal.org/api/drupal/includes!form.inc/function/batch_set/7 (search for "@current" in that page)
Answer: The placeholders @current, @total, etc are described in the file includes/form.inc in the description of the batch_set() function.

progress_message: Message displayed while processing the batch.
  Available placeholders are @current, @remaining, @total,
  @percentage, @estimate and @elapsed. Defaults to t('Completed
  @current of @total.').

You can add custom placeholders by using the t() function like this:
$my_custom_variable = 'contents of my custom variable';

$batch = array(
  'title' => t('Generate reports'),
  'operations' => array(
    array('create_report_callback', array($time_min, $time_max)),
  ),
  'init_message' => t('Starting report generation...'),
  'progress_message' => t('Generating @my_custom_placeholder report. Operation @current out of @total.',
     array(
       '@my_custom_placeholder' => $my_custom_variable,
     )
  ),
  'error_message' => t('Error detected. Please try again!'),
  'finished' => 'create_report_finished',
);

Display batch operations messages


Normalizing data based on row data

Question: I have got a pandas df with ID and Month and what I would like to do is to label the ID/Month with a number if it is first appearing in the dataset
ID     Month      Number
123    201501     1
123    201502     2
123    201503     3
234    201502     1
234    201503     2
455    201503     1

So as you can see the ID 123 is occurring multiple times in the dataset in every single month. Moreover ID 234 only occurs from Month 201502. What I would like to do is to label the rows with a number when they were activated.
Could you please advise?

Not sure what is going wrong but this is the output that I get from the formula that you have provided:
ID  Month   Num
12  201510  1
12  201511  17
12  201512  2
12  201601  3
12  201602  4
12  201603  5
12  201604  6
12  201605  7
12  201606  8
12  201607  9
12  201608  10
12  201609  11
12  201610  12
12  201611  13
12  201612  14
12  201701  15
12  201702  16

Answer: It seems you can use cumcount:
df = df.sort_values(['ID', 'Month']).reset_index()
df['num'] = df.groupby('ID').cumcount() + 1
print (df)
    ID   Month  Number  num
0  123  201501       1    1
1  123  201502       2    2
2  123  201503       3    3
3  234  201502       1    1
4  234  201503       2    2
5  455  201503       1    1

Comment: not sure what is going wrong but I'll share some output

Normalizing data based on row data


Probability distribution of binary time series

Question: I am unable to understand concepts related to the probability distribution of binary time series. This is from the book Binary time series by Benjamin Kedem, vol 52
Let $X_t$, t =0,1,... be a binary stationary Markov chain of $p^{th}$ order, such that
$Pr(X_t = 1) = p, Pr(X_t=0) = q,$
$p+q =1$
We define, $Pr(X_t = x_t|X_{t-1} = x_{t-1},\ldots,X_{t-k} = x_{t-k}) = p_{x_t x_{t-1}\ldots x_{t-k}}$
Consider a stationary AR(1) process $Z_t = \phi Z_{t-1} + u_t$, where $|\phi|  < 1$ and $u_t$ are independent $N(0,\sigma^2)$ variates. Then $Z_t$ is a zero mean stationary Gaussian process. Let $X_t$ be the clipped series at level zero. The joint distribution of the binary series of length (tuples) $n$ is given by
$Pr(X_1 = x_1,\ldots,X_n = x_n) = \frac{1}{2} \prod_{i=2}^{n} \prod p_{y_iy_{i-1}\ldots y_1}^{I_i I_{i-1}\ldots I_1}$  Eq(1)
such that the second product is over all $2^i$ 0-1 tuples $(y_i,y_{i-1},\ldots,y_1)$ and
$I_i = x_i$ if $y_i = 1$ otherwise
$I_i = 1- x_i$ if $y_i = 0$
This is the joint distribution of any 0-1 time series $X_1,\ldots,X_n$ provided $Pr(X_1 = x_1) = 1/2$
 and the products $I_tI_{t-1}\ldots I_1$ are the sufficient statistics for the chain.
My problem is that I have never seen any notation given in Eq(1) and I am unfamiliar with this kind of distribution where there is something on the power of probability. Can somebody please explain to me what is the meaning of the expression in Eq(1) and the notations $I$, power of probability? I remember reading about binomial distribution where the probability is raised to a power. But, this distribution and the meaning is complex to understand. Thank you for help.
Comment: There are several inconsistencies in the question.  First it says that the Markov chain has order $p$.  Then it uses $p$ as the probability of $X_t=1$.  Then it gives a conditional distribution that contradicts what was just said, and has order $k$ instead of $p$.  Note $p$ has $k+1$ subscripts here.  Then Eq(1) uses $p$ with a varying number of subscripts, ranging from 2 to $n$.  If these mistakes are all in the book, then I can understand your confusion.
Answer: It looks like Eq(1) is just a (very confusing) way of writing the chain rule of probability:
$$
Pr(X_1 = x_1, ..., X_n = x_n) = Pr(X_1 = x_1) Pr(X_2 = x_2 | X_1 = x_1) Pr(X_3 = x_3 | X_1 = x_1, X_2 = x_2) \cdots
\\
= \frac{1}{2} \prod_{i=2}^n Pr(X_i = x_i | X_{i-1} = x_{i-1}, ..., X_1 = x_1)
$$
This is because
$$
\prod_{y_i, ..., y_1} p_{y_i \cdots y_1}^{I_i \cdots I_1} = p_{x_i \cdots x_1}
$$
according to the definition of $I_i$.  Consider the case when all $y$'s are one.  Then each $I_i = x_i$, so
$$
p_{y_i \cdots y_1}^{I_i \cdots I_1} = p_{1 \cdots 1}^{x_i \cdots x_1}
$$
If any $x_j$ in the exponent is zero, the whole exponent is zero.  Since any probability raised to the zeroth power equals 1, this factor would contribute nothing to the product.  It only contributes when all $x_i, \cdots, x_1$ equal 1.  Applying the same argument for any pattern of $y$ values shows that the only factor that contributes to the product is the one where $y_i = x_i, ..., y_1 = x_1$.  This is how you get the equation above.
Comment: Thank you for your anser, however things are still unclear. Could you please clarify? (A) So there is no need for representing power of probability in Eq(1)?(B) Could you please elaborate why there are 2 products in Eq(1) and why there is only a single in your representation? (C)In your answer is there a typo $"<i"$ ?
Comment: I've updated the answer to clarify these things.
Comment: Thank you for the update and your effort. I still fail to understand what the $I's$ are and how come $\prod p_{yi..y1}^{I_1..I_1} = p_{xi..x1}$. This is because I don't understand the definition of $I$ and have fond no reference where I can clarify this concept. As you have followed, could you please share your knowledge in this limited space? Shall highly appreciate your time and effort.


unnest row into multiple json objects

Question: Currently I have a table my_table with columns (id,product_id,text) and transform data into a json object like so:
SELECT
  json_agg(
    json_build_object(
      'id', t.id,
      'parent_id', t.product_id,
      'text', t.text
    )
  )
FROM my_table AS t

Now that I am adding more columns to my_table, I need to return a JSON object per each row from the selected list of rows from this table.
Basically, talbe columns will be (id,product_id,text1,text2,text3)
And I want to return 3 identical objects with 1 different value of text (for text1,text2,text3)
How can I achieve this?
Answer: Use unnest() to yield a single row as three ones:
select id, product_id, unnest(array[text1, text2, text3]) as text
from my_table

Create the json array from the above query:
select
    json_agg (
        json_build_object(
            'id', id,
            'product_id', product_id,
            'text', text
        )
    )
from (
    select id, product_id, unnest(array[text1, text2, text3]) as text
    from my_table
    ) t

or
select
    json_agg (
        json_build_object(
            'id', id,
            'product_id', product_id,
            'text', text
        )
    )
from my_table
cross join unnest(array[text1, text2, text3]) as text

Comment: Thanks a lot! I was trying to use `unnest` in my `FROM (...)` but I did not realize how it worked in fact.
Comment: Using the function in the `from` clause with lateral join perhaps is more elegant, see the updated answer.
Comment: To me, the initial answer reads easier. Are there any reasons one should prefer the latter?
Comment: They are roughly equivalent in this case. Anyway, I would recommend taming with the second variant as it may be very helpful in more complex queries.
Comment: I see. I'll try to!
Comment: It seems there is no way to define a constraint which operates on rows unnested in the way used in the answer. But I'm not sure I understand well what you are asking. Maybe ask a new question with sample table structure and/or sample data.
Comment: How can I make this `unnest(array_remove(array[LOWER(text1), LOWER(text2), LOWER(text3)], NULL))` return only unique values based on LOWERed versions and removed NULLs?

unnest row into multiple json objects


Showing $8(\arctan(\sqrt2-1)) = \pi$

Question: My friend is saying  $$8(\arctan(\sqrt2-1)) = \pi$$
I know $4\arctan(1) = \pi$
I thought of $4\arctan(\sqrt2-1) + 4\arctan(\sqrt2-1)$
But I got stuck somewhere
How to solve?
Comment: I don't see any variables for which to solve.
Comment: This comes down to computing $\tan \left(\frac {\pi}8\right)$ which you can do via the half-angle formulas.
Comment: Actually I thought I may get answers by solving $arctan(\sqrt2-1)$ as $arctan(a) + arctan(b)$. But after seeing the answers from members it is so simple to solve. Thank you very much for the community
Comment: This is not an exact duplicate of https://math.stackexchange.com/questions/351592/show-that-tan-pi-over-8-sqrt-2-1, since that question asks for a proof using a specific approach.
Answer: So you have to prove $$\tan {\pi\over 8} = \sqrt{2}-1$$
Notice that $$1= \tan (2\cdot {\pi\over 8}) = {2\tan {\pi\over 8}\over 1-\tan ^2{\pi\over 8}}$$
and solve it on $\tan {\pi\over 8}$.
Answer: After taking the $ \tan $ on both sides you get:
$$ \tan\frac{\pi}{8} = \sqrt{2}-1 $$
Note these two double angle formulas $ \sin2x = 2\sin x \cos x $ and $ \cos 2x = 2\cos^2x-1 $. Now we have:
$$ \tan\frac{\pi}{8} = \frac{\sin\frac{\pi}{8}}{\cos\frac{\pi}{8}} =  \frac{\sin\frac{\pi}{8}}{\cos\frac{\pi}{8}}  \cdot \frac{\cos\frac{\pi}{8}}{\cos\frac{\pi}{8}} = \frac{\sin\frac{\pi}{4}}{1+\cos\frac{\pi}{4}} = \frac{\sqrt{2}/2}{1+\sqrt{2}/2} = \sqrt{2} - 1$$
Answer: Your friend is saying  $$8(\arctan(\sqrt2-1)) = \pi$$
in other words removing inverse function he is saying
$$  (\sqrt2-1)=\tan \frac{\pi}{8}=t $$
that you can say using double angle formula for tan
$$\tan \pi/4 = \frac{2t}{1-t^2} $$
which is same thing as saying
$$ 1=\frac{2 (\sqrt2-1)}{1-(3-2\sqrt2)}$$
thats same thing as saying ... after cancelling $2$ in numerator & denominator:
$$ 1=\frac{\sqrt2-1}{\sqrt2-1}$$
So he is right.

Showing $8(\arctan(\sqrt2-1)) = \pi$


Custom entity translation interface

Question: I created a new custom entity using drupal console, I enabled translation and all looks ok in code.
The problem is that I don't know how to translate an entity from the admin interface, I don't see any options to do this. I looked through the generated files and I don't see any translation related functionality there.
Am I missing something? Should I build my own admin interface and logic to manage translations?
Answer: I've missed configuration at admin/config/regional/content-language :|
Comment: Is there any way to make a custom entity fully translatable without the need to manually enable it at that page?

Custom entity translation interface


Como ter um resultado misturando checkbox'es?

Question: Como eu posso combinar resultados de checkbox?
Com 5 checkbox, por exemplo, como eu faço o resultado ser combinado para quando o usuário selecionar a 5 e a 3 aparece uma messenger box.
Ps: Se a pergunta estiver vaga avisar
Answer: Basta fazer isso:
if(meuCheckBox3.Checked && meuCheckBox5.Checked)
{
    //Combinação 1
}
if (meuCheckBox1.Checked && meuCheckBox4.Checked)
{
    //Combinação 2
}
if (meuCheckBox1.Checked && meuCheckBox2.Checked && meuCheckBox3.Checked)
{
    //Combinação 3
}

Você pode fazer isso dentro de um método e chamar esse método nos eventos de click dos checkbox.


How do you use forms to populate content type fields?

Question: Drupal 7. Custom module.
I have a content type 'RFI' with a handful of fields I created with Drupal UI. I want a user to: fill out a form & click submit while drupal: saves fields to database, creates a new node of 'RFI' and populates the fields of the node with the content provided by the form's corresponding fields.
From what I understand:
Build a form with fields (taking from the 'DBTNG examples' module just for clarity):
function rfi_build_form($form, &$form_state) {
$form = array();

$form['add'] = array(
'#type'  => 'fieldset',
'#title' => t('Add a person entry'),
);
$form['add']['name'] = array(
'#type'  => 'textfield',
'#title' => t('Name'),
'#size'  => 15,
);
$form['add']['surname'] = array(
'#type'  => 'textfield',
'#title' => t('Surname'),
'#size'  => 15,
);
$form['add']['age'] = array(
'#type'  => 'textfield',
'#title' => t('Age'),
'#size'  => 5,
'#description' => t("Values greater than 127 will cause an exception. Try it - it's a    great example why exception handling is needed with DTBNG."),
);
$form['add']['submit'] = array(
'#type'  => 'submit',
'#value' => t('Add'),
);
return $form;
}

Here the form fields are saved to database with database API.
function rfi_build_submit($form, &$form_state) {
global $user;

// Save the submitted entry.
$entry = array(
'name'    => $form_state['values']['name'],
'surname' => $form_state['values']['surname'],
'age'     => $form_state['values']['age'],
'uid'     => $user->uid,
);
$return = dbtng_example_entry_insert($entry);
if ($return) {
drupal_set_message(t("Created entry @entry", array('@entry' => print_r($entry, TRUE))));
}
}

Now here is how I know how to create a node of RFI type. How do I take the values of the fields above [name], [surname], [age] and have them related to corresponding fields in my content type?
function rfi_build_type($form, &$form_state){
$node = new stdClass();
$node->type = 'rfi';
$node->name = '';
$node->surname = '';
$node->age = '';
$node->language = 'en';
$node->uid = 1;
node_save($node);
}

I was thinking that since the fields have been saved already, that I could use a database query to get these values, but intuition tells me I'm overlooking something.
Thank you!
Answer: You seem to be going in a slightly wrong direction with this (until I'm missing or not understanding something). If your goal is to create a node based on the data users send via a custom form then it can be done in the form submit handler. Fields added via the GUI don't need separate handling. For example if you are using node_save() then adding a custom field value could look similar to this:
$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled.
$node->field_name[$node->language][0]['value'] = $form_state['values']['name'];

and with drupal_form_submit():
$new_form_state['values']['field_name'][$node->language][0]['value'] = $form_state['values']['name'];

There's a solid amount of information available about this on the web that you might want to check out for further assistance (like this, this and this).


The sequence of functions converge on a dense subset

Question:
Suppose that $f_n(x)$ is equicontinuous on $[a,b]$ and converges on a dense subset of $[a,b]$, prove: $f_n(x)$ converge uniformly on $[a,b]$.

My thought:Divide the interval into k parts by densely concentrated points so that the length of each part is not greater than $\delta$,and we can denote these points $a_1,\cdots,a_n$. Besides, suppose $f_n\to f$ on the dense subset. Hence,$\forall x\in [a,b]$,it must fall into one small part,which is to say $\exists a_i$,$|x-a_i|<\delta$. Therefore, $$|f_n(x)-f(x)|\leq |f_n(x)-f_n(a_i)|+|f_n(a_i)-f(a_i)|+|f(a_i)-f(x)|~.$$ So the biggest problem is that I don't know how to control $~|f(a_i)-f(x)|~$ for $~f(x)~$ may not be continuous.
Comment: What have you tried so far? Please tell us any ideas or attempts you have made
Comment: I'm very sorry.Thank you for your suggestion! and I have re-edited the problem
Comment: No problem, thanks for the edits!
Answer: Hint 1: the argument you sketched would work for any $\delta$.  This usually means that you need to apply it with a particular strategic choice of $\delta$.
Hint 2: you have not yet used the equicontinuity hypothesis.
Hint 3: Hints 1 and 2 are related.
Comment: I know how to solve it!Thank u!!


Microsoft Graph List activityStatistics

Question: I follow the web page(https://learn.microsoft.com/en-us/graph/api/activitystatistics-list?view=graph-rest-beta&tabs=http)
and use MS Graph Explorer (https://developer.microsoft.com/en-us/graph/graph-explorer/preview) as the image show
to List activityStatistics ,but always get the following information .
<code>{
"error": {
"code": "UnknownError",
"message": "{"Code":"Forbidden","Message":"Access to the requested resource is forbidden.","Target":"ReadUserAnalyticsV2","Details":[{"Code":"PolicyNotSatisfied","Message":"The following authorization requirements are not satisfied: UserRequireAdvancedMyALicense."}]}",
"innerError": {
"request-id": "0bf68858-17f4-498c-bb2b-c2c185e51e78",
"date": "2020-04-08T18:22:22"
}
}
}
</code>
And I have try license use get https://graph.microsoft.com/beta/me/analytics/settings
to check that I have the license like the images
<code>{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#users('5eed83b1-2949-4c9f-b42a-ee9055dfb4fd')/analytics/settings",
    "hasLicense": true,
    "hasOptedOut": false,
    "hasGraphMailbox": true
}
</code>
How can I solve the problem ? Thanks a lot.
Comment: Please post your code so we can look at it
Comment: Thanks, I have modified my post with the execute picture.
Answer: As per this document, for the analytics API to return results for a user, the current user analytics settings must show a valid MyAnalytics license, be opted in to using MyAnalytics, and have a cloud-hosted mailbox that's graph-enabled.
The signed user (/me) should have the following response for GET /me/analytics/settings
{
  "hasGraphMailbox": true,
  "hasLicense": true,
  "hasOptedOut": false
}
Then Analytics API will return results.
If the user doesn't have license, you can assign license via PowerShell
The user should also have "Microsoft MyAnalytics(Full)" license assigned as shown in the below picture. To assign a license, you can navigate to portal.office.com.
Comment: I have check GET /me/analytics/settings  ,  that I have the license , list bellow

{
    "@odata.context": "https://graph.microsoft.com/beta/$metadata#users('5eed83b1-2949-4c9f-b42a-ee9055dfb4fd')/analytics/settings",
    "hasLicense": true,
    "hasOptedOut": false,
    "hasGraphMailbox": true
}
Comment: Can you sign in into https://myanalytics.microsoft.com/ and confirm whether you can see the activity data for the user?
Comment: I have updated the answer with the additional license requirements(Microsoft MyAnalytics(Full)). Once you assign the license, it will take a few minutes to reflect the changes. I have attached a screen shot for your reference.

Microsoft Graph List activityStatistics


O que caracteriza um banco de dados?

Question: O que faz uma coleção de dados ser chamada de banco de dados?
Por exemplo, se eu tenho um arquivo pessoas.txt da seguinte maneira:
Vinicius
João
Maria
José

E passo a escrever, ler e atualizar os dados ali, ele é considerado um banco de dados?
Já vi também desenvolvedores armazenarem dados em XML:
<?xml version="1.0" standalone="yes" ?>
<Pessoa>
    <Codigo>1</Codigo>
    <Nome>Vinicius</Nome>
</Pessoa>

Ou até um arquivo CSV. De fato, existem diversas maneiras de armazenar dados, porém seria considerado banco de dados somente aqueles arquitetados em tecnologias como SQL, MariaDB, MySQL, etc?
Answer: Esta não é uma definição simples, qualquer uma pode dar margem a entender uma coisa diferente do que é. E sem uma definição clara não é fácil definir o que é. Darei minha definição.
Um banco de dados é uma coleção de dados diversos que pode ser facilmente organizada de diversas formas e ter sua recuperação de dados fácil e rápida. Deve haver um mecanismo que facilite isto, não pode ser só dados organizados.
Apenas um simples CSV ou XML permite isto? Me parece que não. Um mecanismo um pouco mais sofisticado talvez já possa ser considerado um banco de dados (uma planilha Excel por exemplo). Provavelmente seria um ruim, mas seria. Um formato assim poderia ser usado para o armazenamento básico, ainda que ineficiente e não atender todos cenários, mas não é porque tem um arquivo assim que já é um banco de dados.
Então o banco de dados não pode ser um único arquivo de dados simples (até pode ser algo mais sofisticado em apenas um arquivo), mas um mecanismo que obtenha organização e acesso rápido em dados diversos usando um arquivo texto simples já é um banco de dados. Note que sequer há a exigência da escrita ser rápida ou ser permanente, estas podem ser características de um modelo específico de banco de dados.
Um banco de dados não precisa usar SQL, tão pouco precisa ser relacional. Ser relacional é apenas um modelo. SQL é só uma opção da manipulação de dados relacionais.
Informalmente quando as pessoas falam de banco de dados quase sempre estão falando de algo relacional com SQL. Mais recentemente o NoSQL (péssimo nome) começou ser usado também, embora sua adoção seja maior do que a real necessidade.
Nem vou referenciar a Wikipedia porque tanto em português quanto em inglês a definição é ruim que dói, os artigos são uns frankensteins.
É muito comum encontrar material que define o que é banco de dados como sendo apenas o banco de dados relacional típico, ignoram que algo mais simples também é banco de dados. Por isso é muito complicado usar uma referência canônica. Há também quem ache que mesmo um arquivo texto simples é um banco de dados.
E não deveria causar espanto porque já vi muita gente falando besteira, e quando isso acontece é porque não tem uma forma definição mais formal. Por isso que bancos relacionais acabam sendo melhor, há definição mais formal, quando alguém falar besteira é fácil mostrar que ela está errada, e aí só resta ela continuar com teimosia ou aceitar o erro. Onde não tem uma definição formal cada um fala uma coisa e todo mundo pode, ou não, estar certo.
Coloquei no GitHub para referência futura.
Comment: Devo discordar de XML não ser considerado como formato válido para banco de dados. Lembro de estudar bancos de dados hierárquicos como projeto de pesquisa para um professor na faculdade. Como eu não lembro a referência original disso, achei um outro pouco disso na internet: [wiki c2](http://wiki.c2.com/?HierarchicalDatabase), sempre acho divertido a discussão nesse site; [sitepoint](https://www.sitepoint.com/hierarchical-data-database/). Sobre ser rápido e fácil, isso é um efeito desejável, não primário, no meu entendimento
Comment: @JeffersonQuesado acho que você não leu com atenção porque eu falei justamente que o XML pode ser um formato usado, não pode ser o banco de dados. Pela sua definição qualquer coisa que tenha dados é um banco de dados, não sei porque deveria existir um termo para isto, afinal "dados" seria suficiente. Se puder definir melhor o que é banco de dados para você, seria bom.
Comment: reli algumas vezes sua resposta nesse trecho, continuei no mesmo entendimento. Definição de banco de dados seria um conjunto não atômico de dados, e um sistema gerenciador de banco de dados é algo que forneça um mecanismo de leitura/escrita sobre o banco de dados. Assim, o banco de dados e o sistema gerenciador de banco de dados são indissociáveis. Eu sei que é um tanto vago, mas é essa a definição que tenho em mente, principalmente levando em consideração os diversos tipos de base de dados (hierárquico, chave-valor, tuplas, etc)
Comment: @JeffersonQuesado exatamente, isto não quer dizer nada e pode ser usado pra um monte coisa que ninguém ousaria chamar de banco de dados.
Comment: Aurélio Verde discorda sobre "ninguém ousar": https://s3.amazonaws.com/static.novatec.com.br/sumarios/sumario-9788575221525.pdf ; capítulo 10: bancos de dados textuais ;-)
Comment: a ideia era exemplificar que o Aurélio teve a ousadia de caracterizar bancos de dados textuais com shell script (tuplas em csv, se não me engano em múltiplos arquivos de tabelas). Infelizmente, não achei o livro dele na internet
Comment: Achei o código do capítulo no GitHub: https://github.com/aureliojargas/livro-shell/blob/master/10-banco/
Comment: Eu vou encerrar porque senti que vamos ficar em círculos, o que você mostrou não tem nada a ver com o início da conversa, eu estou falando de laranjas e você de alfaces. Tudo o que você falou e mostrou comprovam o que eu disse na resposta, suas conclusões são diferentes.
Comment: Só vi um índice, não algum texto que indique algo.
Answer: Banco de Dados, como o próprio nome sugere, é uma estrutura organizada, capaz de reter informações, e permitir que esta mesma informação seja recuperada, fornecendo mecanismos específicos capazes de dinamizar a tarefa, tornando eficiente, e seguro o armazenamento/recuperação dos dados/informação nele contidos - ou simplesmente colecção organizada de dados capazes de se relacionar, de modo a torna-los mais consistentes para determinadas tarefas?.
O que faz uma colecção de dados ser chamada de banco de dados, é a sua estrutura organizacional. O banco de dados agrupa os dados em blocos específicos, de modo que cheguem a ser possuidores de características próprias - já imaginaste o que seria tentar encontrar alguém sem nome, rosto, no meio de uma multidão com as mesmas características?
Um arquivo de texto simples (.txt, ou outro qualquer) pode ou não ser considerado um banco de dados, mas muito depende da forma como vais ordenar/agrupar a informação que lá estiver, ou mesmo propósito a que se destina a informação lá contida.
Bancos de dados, são normalmente caracterizados, por possuírem uma estrutura sólida, bem delineada e organizada, fácil acesso, recuperação e discriminação da informação neles contidos, e acima de tudo por serem seguros, senão todos nós estaríamos a usar ficheiros xml para armazenar senhas.
Alguns comentários/ideias vão obviamente discordar, e com razão digo, mas é o que é, banco (instituições financeiras mesmo) são assim denominados porque cumprem com algumas, senão grande parte, das características aqui mencionadas. Se não fossem seguros, e capazes de agilizar alguma operações com o dinheiro, melhor seria se estivéssemos a usar latas para guardar dinheiro, ou andar com milhões na carteira. Por esta razão quando se fala de banco de dados vem-nos a cabeça estes sistemas de armazenamento já muito conhecidos.
Edit: a segurança não é de todo uma característica fundamental para caracterizar um banco de dados.
Comment: De fato a segurança não é inerente ao banco de dados, inclusive a maioria deles são inseguros por padrão, nem consigo me lembrar de algum diferente disto.
Comment: @bigown pois é, não tanto, mas é de facto uma componente para a caracterização de banco de dados.
Comment: Galera, discutir conceito sem referência bibliográfica, puro empirismo. Mas não confunda os seus tipos de bancos que pode ser relacional SGDB, pode ser simples um Mapa de dados;  Ou simplesmente um txt; Um banco de dados é uma coleção de dados guardadas; A forma como vai ser gerenciado são seus tipos.

https://books.google.com.br/books?hl=pt-BR&lr=&id=xBeO9LSlK7UC&oi=fnd&pg=PP23&dq=banco+de+dados+defini%C3%A7%C3%A3o+&ots=xbMDj1Ba3M&sig=5mf2OpYhIdvZuUtsvORYHzLGR0E#v=onepage&q=banco%20de%20dados%20defini%C3%A7%C3%A3o&f=false

Esse livro é uma boa referência.
Comment: @alxwca valeu por não acrescentar nada novo.

O que caracteriza um banco de dados?


What is a "scoring position" in baseball?

Question: I heard the other day the commentator said a player is in "scoring position" after running to the second base.
What is a "scoring position" in baseball/MLB?
Comment: There is a wikipedia article on this term.  https://en.wikipedia.org/wiki/Scoring_position
Answer: It is exactly being at second or third base. The point is that it is (usually) enough to hit a single to score a run by that player, which is not true for players at first base, or home.
Answer: For a base hit it typically has to get out of the infield so a runner on second can score unless they had to hold for possible caught in the air.
On third runner can typically score on even a caught ball (sacrifice fly).
From first not going to score without error(s).


To check that under a constructed complete metric on $(0,1)$ the sequence $1/n$ is not cauchy

Question: I was asked to construct a a metric on $(0,1)$ that induces the usual topology on it and that turns it into a complete metric space. The obvious choice is to define the metric as follows:
We have the bijection,
$$ f:(0,1)\to\mathbb{R}, \ x\mapsto\tan\pi\left(x-\frac{1}{2}\right)$$
And we now define the metric as
$$ d(x,y)=|f(x)-f(y)| $$
which makes $((0,1),d)$ complete, because $\mathbb{R}$ is complete in its usual metric, and it induces the standard topology because $f$ is a homeomorphism.
Now I have to check that the sequence $(x_n)$ with $x_n = 1/n$ is not Cauchy in this metric. And I'm unable to do that. Can I get some assistance?
Answer: Just use the fact that $\lim_{n\to\infty}\tan\left(\pi\left(\frac1n-\frac12\right)\right)=-\infty$. In particular, the limit does not exist in $\mathbb R$. Therefore, your sequence does not converge. Since your space is complete, your sequence cannot be a Cauchy sequence.


MySQL 5.6 and SELinux are arguing

Question: CentOS 6.4(x64) / MySQL 5.6.10
Installed MySQL from source files and started it fine under it's original location (/var/lib/mysql). I moved everything to a different volume (/u0/mysql) and went through a few rounds of exempting it from SELinux (cat /var/log/audit/audit.log | audit2allow -M mysql-* followed by semodule -i mysql-*.pp). After every round of this I try to restart the service and see this error:
Starting MySQL..The server quit without updating PID file (/u0/mysql/server.pid).
 [FAILED]

After 3-4 rounds of adding exemptions I noticed that the files weren't changing anymore. IE everything that could be added this way had already been added. Out of curiosity I ran audit2why -a and got a slew of:
type=AVC msg=audit(1387207317.009:666): avc:  <some permission> for  pid=20640
  comm="mysqld" dev=sdc1 ino=36831373 scontext=unconfined_u:system_r:mysqld_t:s0
  tcontext=system_u:object_r:default_t:s0 tclass=file
 Was caused by:
     Unknown - would be allowed by active policy
     Possible mismatch between this policy and the one under which the audit message was generated.
     Possible mismatch between current in-memory boolean settings vs. permanent ones.

This leads me to believe that it should be working. If I disable SELinux: setenforce 0 then mysql will start just fine so it (SE) is still getting in the way.
Searching through SF turned this up - have tried it with no change in outcome.
SO: how do I find out where the blockage is?
EDIT:
[root@server u0]# ls -ldZ /var/lib/mysql
drwxr-xr-x. mysql mysql unconfined_u:object_r:mysqld_db_t:s0 /var/lib/mysql

[root@server u0]# ls -ldZ /u0/mysql
drwxr-xr-x. mysql mysql unconfined_u:object_r:mysqld_db_t:s0 /u0/mysql

Answer: You need to change the security context of the new directory to be the same as /var/lib/mysql.
For instance:
ls -ldZ /var/lib/mysql/
drwxr-x--x. mysql mysql system_u:object_r:mysqld_db_t:s0 /var/lib/mysql/

Then change the new directory to the same settings with chcon
chcon -R -u system_u -r object_r -t mysqld_db_t /u0/mysql

Comment: What is mysql error log? You can try to enable logging of the don't audits at semodule using: semodule -B -D. There should be a lot more things in the audit log, see if anything relates.
Comment: Just did this. No change in outcome.
Comment: What's the output of ls -ldZ /var/lib/mysql and /u0/mysql?
Comment: Try to reload the policy: semodule -R and then audit2why since last reload: audit2why -l to see if anything shows.
Comment: No change. `audit2why -l` has no output.
Comment: There isn't just a single mysql context to consider. Log files, configuration files and so on got their own contexts. Give a look at the output of "semanage fcontext --list |grep mysql"
Comment: Using `semodule -B -D` seems to have kicked something loose again. Now I'm able to use `audit2allow` to create exemptions again. This seems to be working. TY.
Answer: You can also try something like this:
chcon --reference=/path/to/existing/file /path/to/new/file

BTW, here is a great intro to SELinux: http://www.youtube.com/watch?v=MxjenQ31b70 from Redhat 2012 Summit. Also, look in your log files for error messages in /var/log/audit/audit.log or /var/log/messages.
Comment: There are no new entries in `/var/log/audit/audit.log` once I've added the pre-existing exemptions.

MySQL 5.6 and SELinux are arguing


Atualização de javascript

Question: Estou desenvolvendo um contador para a Black Week. Porém, estou com uma dúvida: o contador alterará mesmo a data quando chegar dia 21? Ou é melhor criar 2 arquivos e alterar via FTP dia 21 às 0:00? (sou iniciante, por isso a dúvida)

Comment: Por favor evitem longas discussões nos comentários; a conversa de vocês foi [movida para o chat](http://chat.stackexchange.com/rooms/67952/discussion-on-question-by-joao-pedro-atualizacao-de-javascript)
Answer: Sua pergunta O contador alterará mesmo a data quando chegar dia 21?
Minha resposta  Com certeza o contador vai alterar a data no dia 21.
Para verificar é muito simples, basta supor var hoje=21;, veja

//supondo hoje ser dia 21
    var hoje=21;
    var inicioBlackWeek = 20;

    // Definindo a data final
    if (hoje <= inicioBlackWeek){

        var contadorData = new Date("Nov 20, 2017 23:59:59").getTime();
        //supondo hoje ser antes do dia 20
        console.log("hoje dia 19 contadorData => Nov 20, 2017 23:59:59");

    }else{

        var contadorData = new Date("Nov 26, 2017 23:59:59").getTime();
        //supondo hoje ser dia 21
        console.log("hoje dia 21 contadorData => Nov 26, 2017 23:59:59");

    }

Utilizando o código inteiro
Par verificar é muito simples:
faça a var hoje igual a 21.
var hoje = 21;

altere essa linha var distance = contadorData - now;
para  var distance = contadorData - (now+(86400*(hoje-new Date().getDate())*1000));

Se a variável var hoje é igual a 21 e a diferença entre var hoje e a variável  var contadorData der 5 dias é que houve a tal mudança que você duvidou se iria mudar.
Com essa alteração você vai constatar que o texto Faltam apenas mudará para  Restam apenas que é a condição else da verificação if (hoje <= inicioBlackWeek) {

Veja funcionando.

var hoje = new Date().getDate();

 //para que após o dia 21 de novembro não seja necessário mudar para 22, 23 etc...
 var mes = (new Date().getMonth())+1;
 if (mes==10 || hoje<21){
  hoje = 21;
 }

 var inicioBlackWeek = 20;

 // Definindo a data final
 if (hoje <= inicioBlackWeek)
 var contadorData = new Date("Nov 20, 2017 23:59:59").getTime();
 else
 var contadorData = new Date("Nov 26, 2017 23:59:59").getTime();

 // Atualizando a contagem decrescente a cada 1 segundo
 var x = setInterval(function() {

 // Recebendo a data e hora atual
 var now = new Date().getTime();

 // Encontrando a distância entre agora e a data final
 var distance = contadorData - (now+(86400*(hoje-new Date().getDate())*1000));

 // Cálculos de tempo por dias, horas, minutos e segundos
 var dias = Math.floor(distance / (1000 * 60 * 60 * 24));
 var horas = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
 var minutos = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
 var segundos = Math.floor((distance % (1000 * 60)) / 1000);

 // Retornando resultados
 if (hoje <= inicioBlackWeek) {
     document.getElementById("dias").innerHTML = "Faltam apenas
" + dias + " dias";
     document.getElementById("segundos").innerHTML = +segundos + "s
para a Black Week!";
 } else {
     document.getElementById("dias").innerHTML = "Restam apenas
" + dias + " dias";
     document.getElementById("segundos").innerHTML = +segundos + "s
de Black Week!";
 }

 document.getElementById("horas").innerHTML = "e " + horas + "h ";
 document.getElementById("minutos").innerHTML = +minutos + "m ";

 // Se a contagem decrescente terminar, mudará a escrita das divs
 if (distance < 0) {
     clearInterval(x);
     document.getElementById("dias").innerHTML = "FINALIZADO";
     document.getElementById("tempo").innerHTML = "";
 }
 }, 1000);

Desnecessário explicar que até 21/11 sempre vão faltar 5 dias.

Atualização de javascript