Why are complex finite-dimensional irreducible representations of abelian groups one-dimensional?

Question: I'm supposed to show that each Complex finite-dimensional irreducible representation of an abelian group is one dimensional.
For any map $\phi: V \rightarrow V$ it holds that $\phi(\rho(g)v) = \rho(g) \phi(v)$. Also since the group $\rho(h) \rho(g) v = \rho(g) \rho(h) v$. From a previous exercise I know that $\phi = \lambda \cdot id_V$ for some $\lambda \in \mathbb{C}$. This transforms the previous equation into $\lambda \cdot id_V \cdot (\rho(g)v) = \rho(g) \lambda \cdot id_V \cdot v$ which implies that $\lambda \cdot id_V \cdot (\rho(g)v) =\lambda \cdot \rho(g) \cdot v$. Now I'm not quite sure how to bring into play that $G$ is abelian. Could someone give me a hint?
Cheers!
Comment: Why do we have $\phi(\rho_g(v)) = \rho_g(\phi(v))$ for an **arbitrary linear map** $\phi: V \to V$?
Comment: This is essentially the claim that commuting matrices have a common eigenvector.
Answer: Let us sort out things a bit.
Let $\rho: G \to \operatorname{GL}(V)$ be an irreducible representation of any group $G$.
You have seen that if $\varphi : V \to V$ commutes with all $\rho(g)$, for $g \in G$,
that is
$$
 \varphi (\rho(g) v) = \rho(g) \varphi (v)\tag{comm}
$$
for all $g \in G$ and $v \in V$,
then $\varphi = \lambda \operatorname{id}_{V}$ for some $\lambda \in \mathbf{C}$.
Now if $G$ is abelian we have $\rho(x) \rho(g) = \rho(g) \rho(x)$ for all $g, x \in G$, so that $\varphi = \rho(x)$ satisfies (comm).
It follows that for any $x \in G$ there is $\lambda \in \mathbf{C}$ such that
$$
\rho(x) = \lambda \operatorname{id}_{V},
$$
so all $\rho(x)$ are scalars, and then leave every subspace invariant.
Since the representation is irreducible, $V$ must have dimension $1$ then.
Comment: Why if $\varphi : V \to V$ commutes with all $\rho(g)$  then $\varphi = \lambda \operatorname{id}_{V}$? Can you please show it? Thanks!
Comment: @ Nikita: This is Schur's lemma.
Comment: It is better to evade Schur's lemma in this special, simple case
Answer: As an alternative approach:
Since all homomorphisms from a finite abelian group $G$ to $C^*$ are irreducible characters of $G$, and there are $|G|$ many of these, by dint of orthogonality relations, we conclude that they are all irreducible characters of $G$, and hence all irreducible representations of $G$ are of dimension $1$.
Inform me of any error. Thanks.

Edit:
I am not sure now what my argument was for the equation $\lvert\operatorname{Hom}(G,\mathbb C^*)\rvert=\lvert G\rvert$, but I think for a finite abelian group, we can prove this by writing $G$ as a direct product of cyclic groups, by means of the fundamental theorem of finitely generated abelian groups, and then showing that this equation holds for cyclic groups, and both sides of the equation are multiplicative with respect to the direct product.
Comment: I do not recall exactly my thoughts at that time, but I try to provide a possible argument in an edit.
Comment: How do you know there are $|G|$ of them?
Answer: For some variety, here is a ring-theoretic argument.
For a finite abelian group and an algebraically closed field $k$, $R:=kG\cong k^{|G|}$ as rings by Artin-Wedderburn.
[$R\cong\prod_iM_{n_i}(D_i)$ is a finite product of matrix rings over division rings, $D_i\cong\operatorname{End}_R(V_i)$ a division ring (finite dimensional over $k$), $V_i$ runs over the isomorphism classes of the simple left $R$-modules, $n_i$ their multiplicity in $R$ as a left module over itself.]
Hence the dimensions of the simple left $R$-modules must all be one [since $R$ is commutative, $n_i=1$, and $D_i$ is a field because $k$ is algebraically closed, $D_i=k$].
Comment: @Khoata The ring of $n\times n$ matrices (say with entries from ring with unit) is always noncommutative.
Comment: I'm not very familiar with commutative algebra, I just want to ask, why does R being commutative implies that $n_i =1$ in the decomposition?  Thank you very much.
Comment: thank you so much.  Silly me!! I don't know why I missed that, I kept thinking there is some deeper reasoning from commutative algebra about this.
Answer: We could go a bit heavier on linear algebra and lighter in representation theory.
Let $\rho: G\rightarrow GL(V)$ be a representation. Clearly $\rho(g)^{|G|}=I$ and so $\rho(g)$ is diagonalizable.
Furthermore, $\{\rho(g)| g\in G\}$ commute, so they are simultaneoulsy diagonalizable and there is a common eigenvector $v$.
Clearly $\text{span}(v)$ is $\rho(G)$ invariant, but  $\rho$ is irreducible, so we must have $ \{v\}$ a basis for $V$.


Qual é a melhor maneira de usar threading nesse exemplo?

Question: Esse é o desafio:

Crie uma função que mostre um item de uma lista a cada segundo. Utilizando threads, faça o programa mostrar todos itens o mais rápido possível

Meu codigo:
import threading
import time

threads = int(input('Quantas threads você deseja? '))
lista = list(range(0, 50000))

if threads > len(lista):
    threads = len(lista)

passo = len(lista) // threads
n = 0
n2 = passo

def thread(n, n2):
    for i in lista[n:n2]:
        print(i)
        time.sleep(1)

for i in range(threads - 1):
    threading.Thread(target=thread, args=(n, n2)).start()
    n = n2
    n2 += passo
threading.Thread(target=thread, args=(n, len(lista))).start()

Ele funciona, porém os prints não são em ordem, existe alguma maneira de deixa-los em ordem com a mesma eficiencia ou até mais?
Também percebi que o trabalho de cada thread é diferente (a ultima thread normalmente tem mais itens), então algumas terminam muito rápido, e outras muito devagar. Existe alguma maneira de deixar um numero de threads "fixo" até que o programa liste todos itens?
Answer: Uma solução é passar para cada Thread uma parte contínua da lista, como [..., n, n+1, n+2, ...] e fazer cada Thread montar um texto para ser impresso. Depois cada Thread imprime seu próprio texto.
num_threads = int(input('Quantas threads você deseja? '))
lista = list(range(0, 20000))

lock = threading.Lock()

# Cada Thread imprime uma parte contínua da lista
def imprime_lista(*lista):
    texto = '\n'.join(map(str, lista))
    with lock:
        print(texto)

total_valores = len(lista)
lista_threads = []

# Passa uma parte da lista para a cada Thread
for i in range(num_threads):
    lim_inf = int((i)*(total_valores/num_threads)) # Limite inferior para divisão da lista
    lim_sup = int((i+1)*(total_valores/num_threads)) # Limite superior para divisão da lista
    lista_threads.append(threading.Thread(target=imprime_lista, args=(lista[lim_inf:lim_sup])))

for t in lista_threads:
    t.start()

Com o Lock() é possível bloquear uma Thread de acessar parte do código. Como todas as Threads executam a mesma função, todas formam o texto a ser impresso paralelamente, mas são bloqueadas antes de imprimir o texto.
Com o comando with lock:, eu garanto que cada Thread vai imprimir seu texto e depois liberar para que outra Thread faça o mesmo. Assim as impressões não se sobressaem.
Nos testes que eu fiz, talvez por todas executarem a mesma função, a ordem em que as Threads imprimiram seus respectivos textos (ou seja, acessavam a parte bloqueada) seguiu a ordem em que foram inicializadas pelo t.start().
Comment: poderia me explicar o uso de threading.lock()?
Comment: Editei adicionando uma breve explicação com o que sei. Desculpe se não é o suficiente para seu problema, mas meu conhecimento de `Threads` com `Lock` não vai muito além disso.
Comment: gostei desse método de adicionar as threads em uma lista, com certeza vou usar
Comment: Como o `total_valores` e o `num_threads` são fixos, colocaria o cálculo deles `total_valores/num_threads` fora do looping pra evitar refazer-lo.


Compute the limit of $\frac{I [\left( 1-tR \right)^2+t^2I^2]^{n-1}}{\left( R-R^2t-I^2t \right)^n}$ as $t \rightarrow \pm \infty$

Question: Let $R$ and $I$ be two real numbers such that $I \neq 0$ and let $n \in \mathbb{N}$.
For each triple $(R,I,n) \in \mathbb{R} \times \mathbb{R}^* \times \mathbb{N}$, let $f_{(R,I,n)}$ be a function of a (single) real variable $t$ defined as follows: $$f_{(R,I,n)}(t)=\frac{I [\left( 1-tR \right)^2+t^2I^2]^{n-1}}{\left( R-R^2t-I^2t \right)^n}.$$
Regardless of the domain of the function $f_{(R,I,n)}$, we are actually only interested in computing the following limitations: $$\lim_{t \rightarrow - \infty}f_{(R,I,n)}(t),$$ and $$\lim_{t \rightarrow + \infty}f_{(R,I,n)}(t).$$
Thanks in Advance.
Comment: Please write the context of this problem and what you have tried.
Comment: Try factoring out as many copies of $t$ from the numerator and denominator as you can, and use the fact that $\lim_{t\to\pm\infty} 1/t^k = 0$ for any $k \in \mathbb{Z}^+$.
Answer: Let's expand the terms of the numerator and denominator which are in parentheses. Then we shall pull out as many copies of $t$, as I mentioned in my comment:
$$
\begin{align}
f_{(R,I,n)}(t) & = \frac{I[1 - 2Rt + (R^2+I^2)t^2]^{n-1}}{[R - (R^2+I^2)t]^n} = \frac{I(t^2)^{n-1}\left[\frac1{t^2} - \frac{2R}t + (R^2+I^2)\right]^{n-1}}{t^n\left[\frac{R}t - (R^2+I^2)\right]^n} \\
& = t^{n-2}\cdot\frac{I\left[\frac1{t^2} - \frac{2R}t + (R^2+I^2)\right]^{n-1}}{\left[\frac{R}t - (R^2+I^2)\right]^n}
\end{align}.
$$
Now, the big fraction in the second term has a finite limit as $t\to\pm\infty$ because $\lim_{t\to\pm\infty} 1/t^k = 0$ for any $k \in \mathbb{Z}^+$, and the fraction is a rational function of $1/t$. We see
$$
\lim_{t\to\pm\infty} \frac{I\left[\frac1{t^2} - \frac{2R}t + (R^2+I^2)\right]^{n-1}}{\left[\frac{R}t - (R^2+I^2)\right]^n} = \frac{I(R^2+I^2)^{n-1}}{[-(R^2+I^2)]^n} = \frac{(-1)^nI}{R^2+I^2}.
$$
Therefore, the limiting behavior of $f_{(R,I,n)}(t)$ depends solely on that of $t^{n-2}$. Hopefully it is now clear that your desired limits are
$$
\lim_{t\to\pm\infty} f_{(R,I,n)}(t) = \begin{cases}
0 & \text{if } n < 2\\
\frac{I}{R^2+I^2}& \text{if } n = 2\\
\pm\infty & \text{if } n > 2
\end{cases}.
$$
In particular, when $n > 2$, we have $t\to +\infty$ implies the limit is $+\infty$ if $n$ is even, and $-\infty$ if $n$ is odd. These cases are flipped when $t\to-\infty$.
Comment: Thank you so much


Username and password inputs filling with random information on page load

Question: I wrote register/login files using php. I have a index.php that contains two html forms a registration form with input boxes for username, password, reenter password, and email. Then I have a login form that has inputs for username and password. When someone completes the registration form and submits the form action calls reg_user.php this file sends an email with a "complete your registration" link.
When you click the link it redirects to complete_reg.php which checks DB credentials and if ok says "Thank you for registering, click the button below to login" This is where the problem occurs. When you click the button it redirects back to the index.php so you can then fill in the login form but when the page loads the username and password inputs on both forms are already filled in with a username and password BUT the username is an old admin password I setup for WordPress which I don't use anymore nor do I have it installed and the old password for the same.
I am not using WordPress at all on my computer and there is no WP database in phpMyAdmin/MySQL so its a complete mystery to me where this info is coming from. I did a search on my entire project just to be sure I didn't accidentally include them in any of my code or definitions and it didn't find a thing. I didn't post any code because I am not sure what would be helpful. Just hoping maybe someone would know where I could start to figure this out. Here is the end of the complete-reg.php which I know is probably not helpful but who knows...
    echo '<form class="congrats" action="index.php">';

    echo '
Your registration is complete, you can now login!
';

    echo '
Press the button below to go to the login page.
';

    echo '<input type="submit" name="logbutton" value="Login"/>';

    echo '</form>';

Comment: Clean the browser's cache, and Browsing Data (at least I know this option is available on Chrome, probably in all other browsers too).
Comment: Also try adding an autocomplete=off attribute to the input elements... ``
Comment: It sounds like something local to your browser. Try a different browser to confirm. If this is not the case, please include your index.php code as that is where the problem exists according to your description.
Comment: (Your code looks like it's from the '90s.) Rather than have a form post to index. why don't you attach Javascript to redirect to the login page. Without seeing your index.php code, it is guesswork, but I agree with others in suspecting your browser.
Comment: Yes you guys were right it was the browser, strange. So why would it fill out the inputs on ***my page*** when it loads but it doesn't do it when I go to a live website like my bank or something? And as for the autocomplete would you recommend turning that off, or is it just preference?
Comment: I normally do use listeners and javascript but when I am just in the testing phase I usually go as simple as possible. agreed I don't need the echos, just habit guess again just testing at the moment. I really don't want to turn off autocomplete because I use it to my advantage although I suppose I could turn it off and then back on after it loads?. You all may be right maybe it is my browser (just got home will give it a shot) If that's the case is there a way to programmatically clear browser datas?  How would I avoid this happening on someones browser when the site is live?
Comment: The only thing I can think of re browser is autocomplete. At some point in the past the url was used for something else that you filled in the data for. (Suspect this as it sounds like a test system you are using). As to turning off autocomplete, as a user I like having it so it would make me upset if I came to your page regularly and had to keep filling in the form.
Answer:
It seems to be a browser issue: try clearing your browser's data or using a different browser to validate that this is the case.
PHP doesn't need to echo HTML: you just include it outside of the PHP tags.
e.g echo '<form class="congrats" action="index.php">'; simply becomes <form class="congrats" action="index.php"> outside of the PHP tags.
As others have noted, it would be more modern to redirect the user instead of making another form which they need to submit.


Consultar se há dados na coluna

Question: Tem como fazer uma verificação para saber se existe dados dentro de uma coluna em uma tabela?
Quero fazer uma verificação e se existir dados na coluna ele exibe uma div.
Eu conheço o mysql_num_rows para somar os dados que foram buscados, mas queria saber se tem como eu verificar dados dentro de uma coluna.
Comment: Resolvido, echo "".$exibe_painel_usuarios_um [ "twitter" ]."";
Comment: `<?php if (!empty($exibe_painel_usuarios_um [ "twitter" ]))` basta negar
Comment: De boa veiow. \o
Comment: Thanks por a ajuda.
Answer: Se é para verificar apenas uma coluna, faça um SELECT contando os resultados, usando como atributo, se a coluna não é vazia (IS NOT NULL), na clausula WHERE. Assim terá como saber se existem resultados. No exemplo a seguir esta como validar se existem registros para a coluna, para fazer seu select deverá colocar todos itens que são necessários para a consulta que estava fazendo, para desta forma obter o resultado correto.
SELECT count(COLUNA) as TOTAL FROM TABLEA WHERE COLUNA IS NOT NULL;

Neste caso você pegara o resultado do select com o alias "TOTAL" e se for diferente de 0, então exiba os resultados, provenientes de outro SELECT preferêncialmente.
Fazendo da forma que esta acima irá reduzir consumo do MYSQL, tendo em vista que da outra forma irá retornar um linha da tabela toda, para no final de a coluna estar vazia ignora-la. Embora se usar o IS NOT NULL e colocar a coluna mesmo no mysql também não ira dar erro, porém neste caso será necessário usar o mysql_num_rows e isso consumiria mais recursos que efetuar um count em um campo.
Comment: Is not null não funciona se for um caso de campo vazio ' '.

Consultar se há dados na coluna


How to play a note with different length with both hands?

Question: As you can see I have 2 G notes to play, one in the right hand melody and other in the left hand chord, how am I supposed to play this?
Comment: Is this from an arrangement of the Game of Thrones theme? If so, the right-hand G pretty much has to be re-struck.
Comment: Yes, Actually it is Games of thrones theme, Arranged by Simon Csokan.
Answer: Sometimes you see the hands overlap like this in old keyboards music. In those cases it can be understood the music would have been played on an instrument with two manuals. Harpsichords and organs often have two manuals.
In modern times people sometimes stack up two electronic keyboards. You could play overlapping hands that way too.
I suspect this is just an arrangement someone made on a computer without any concern for what a real human has to do with their hands on a real piano.
As the other answers say, on a piano with one keyboard you can just release the G in the left hand and hit it again with the right hand for the second G. You have no other choice and it cannot be play exactly as written on a single keyboard.
Comment: Perfectly competent composers (Beethoven, Mozart...) do this sort of thing too.   They're indicating a musical intention, not a technical solution.  On a percussive instrument like piano it's no problem.  On (one manual of) an organ you'd want to be more careful.
Comment: Yeah, but I'm not ranking this piano music with those composers in terms of intention and real hands. I think it's fair to say this might not be a great piano arrangement.
Comment: It would work fine in a sequencer. That may be the intention. I mean, PLAYING a piece is SO old-fashioned!
Answer: You will play the G again in your RH as notated. In this instance, the pedal will sustain the LH G (see the pedal marks below the bass clef).  Therefore, you're good to lift your LH thumb from that G to allow the RH to play it.
Note that it is not usually recommended to rely solely on the pedal to sustain notes. Normally, even if there is a pedal marking (except in the case of staccato), you should keep the note depressed the full value. However, in this instance, you don't really have a choice. You've got to allow that RH G to be played, especially since it seems to be the melody of the song!
I would definitely recommend keeping the other two LH notes depressed for their full value - the G in question, played by the LH thumb, should be the only one you need to release.  Doing this will make it completely unnoticeable to the listener that you are "cheating" one note with the pedal (by necessity, of course).
Comment: Thank you so much for your complete explanation.


How to add list items in windows phone app using c#?

Question: I am beginner to c# and windows phone programming , I have a code where I load JSON data into the Message box which I was able to do it But I want to display those data in message box to the list items in the page , I was unable to understand the API in msdn,help me from this
Programm
XAML CODE
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
        <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,12,0" Style="{StaticResource PhoneTextTitle1Style}"/>
    </StackPanel>

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
           </Grid>
</Grid>

CS CODE
 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.Runtime.Serialization.Json;
    using System.IO;
    using System.Text;
    using System.Runtime.Serialization;
    namespace JsonArray
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                string jsonString = @"{
        'product': [
            {
                'CATEGORYNAME': [
                    'AshokLeyland',
                    'HindustanMotorsLtd',
                    'MahindraLtd',
                    'TataMotors',
                    'SwarajMazda'
                ],
                'CATEGORYID': [
                    '1',
                    '2',
                    '3',
                    '4',
                    '6'
                ],
                'SUBCATEGORYNAME': [
                    'MultiaxleVehicles',
                    'HippoHaulage',
                    'HippoTipper',
                    'Cargo',
                    'Pick-up'
                ],
                'SUBCATEGORYID': [
                    '1',
                    '2',
                    '3',
                    '4',
                    '5'
                ],
                'TYPENAME': [
                    'Haulage',
                    'Tippers',
                    'RigidTrucks',
                    'Cabs',
                    'DeliveryVan'
                ],
                'TYPEID': [
                    '1',
                    '2',
                    '3',
                    '4',
                    '5'
                ]
            }
        ],
        'success': 1,
        'message': 'Thetruckdetailsgettingsuccessfully'
    }
    "; ;
                RootObject TotalList = new RootObject();
                RootObject childlistonly = new RootObject();
                Product prdt = new Product();
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonString));
                DataContractJsonSerializer ser = new DataContractJsonSerializer(TotalList.GetType());
                TotalList = ser.ReadObject(ms) as RootObject;
                string category = "";
                string typename = "";
                int i = 0;
                int k = 0;
                foreach (var d in TotalList.product)
                {
                    foreach (var t in d.CATEGORYNAME)
                    {
                        category = category + "" + t.ToString() + "\n" + "\n" + "\n";
                        i++;
                    }

                    foreach (var t in d.TYPENAME)
                    {
                        typename = typename + "" + t.ToString() + "\n" + "\n" + "\n";
                        k++;
                    }
                    foreach (var t in d.SUBCATEGORYNAME)
                    {
                        // teststring = teststring + "" + t.ToString() + "\n" + "\n" + "\n";
                        i++;
                    }
                }
                MessageBox.Show("Your CATEGORYNAMES are:\n\n" + category);
                MessageBox.Show("Your TYPENAMES are:\n\n" + typename);
                ms.Close();

            }
        }
        public class Product
        {
            public List<string> CATEGORYNAME { get; set; }
            public List<string> CATEGORYID { get; set; }
            public List<string> SUBCATEGORYNAME { get; set; }
            public List<string> SUBCATEGORYID { get; set; }
            public List<string> TYPENAME { get; set; }
            public List<string> TYPEID { get; set; }
        }

        public class RootObject
        {
            public List<Product> product { get; set; }
            public int success { get; set; }
            public string message { get; set; }
        }
    }

Answer: You need to use LongListSelector or ListBox.
here are some tutorials
http://msdn.microsoft.com/en-us/library/windowsphone/design/hh202882%28v=vs.105%29.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/hh868196.aspx
http://developer.nokia.com/community/wiki/Listbox_handling_in_Windows_Phone
http://msdn.microsoft.com/en-us/library/windowsphone/design/jj735577%28v=vs.105%29.aspx
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj244365%28v=vs.105%29.aspx
http://blogs.windows.com/windows_phone/b/wpdev/archive/2013/05/23/windows-phone-8-xaml-longlistselector.aspx
Also, you can check out this series -
http://channel9.msdn.com/Series/Windows-Phone-8-Development-for-Absolute-Beginners
It gives a good overview of Windows Phone
EDIT:-
In the xaml page, you need to add a LongListSelector.
A simple example-
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<phone:LongListSelector Name="YourList" LayoutMode="List" IsGroupingEnabled="False">
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <Grid>
              //Add Textblocks here
            </Grid>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>
</Grid>

in code-behind -
YourList.ItemSource = YourListObject;
Comment: I have mentioned  "I was unable to understand the API in msdn" in question
Comment: @vinod Thats why I linked you to tutorials also.
Comment: Can you brief it without the links


Causal impact: how can i determine the incremental impact of campaign 1 when campaign 2 was so live in same markets and dates?

Question: 0
I have a theoretical / stats related question. I've run the package in R and it's easy. Question: How can I know the incremental impact of Campaign #1 during a period when Campaign #2 was also active during the same time period and in the same markets as Campaign #1?
r
statistics
Comment: Do you have any data for a period with only Campaign #1 or only Campaign #2, or do the two campaigns overlap entirely?
Comment: @BryanKrause completely overlap...
Answer: You cannot do this with any statistical model or software. You do not have two interventions, you have one intervention "Campaign 1+2". They are perfectly correlated and not possible to separate.
Your only hope would be if you could apply some prior domain-specific information to the problem. For example, if Campaign 1 said "buy oranges" and Campaign 2 said "buy apples", you could perhaps separate them by looking separately at apple and orange sales, rather than a composite "fruit sales", and using an assumption that a specific campaign for apples doesn't impact other fruits.


Web API, authenticate using Azure AD

Question: We have an Angular SPA and Web API which is hosted in IIS - standalone server.
Our Web API uses user ID and password OAuth token authentication.
One of our client wants to use their Azure AD instead of our application's user id and password.
How to pass their AD token in our <code>/token</code> API call? Is there any easy way to implement this?
Answer: We can do this by two ways.
Approach 1# using ADAL.js in SPA
For SSO Clients
Get AD Token using ADAL, then pass it to /token with custom grant_type and  decrypt AD token and generate your own token
Apporach 2# using SAML approach
For SSO Clients
Get SAML response, and pass it to /token with custom grant_type and  decrypt SAML token with certificate you received from AD SSO then generate your own token

Web API, authenticate using Azure AD


Extracting dates from long filename

Question: I've read through some of the other questions on here about extracting dates (or different sections) from filenames, but I can't seem to get any of the other answers to work on my filenames.
I have a list of >15,000 filenames from a directory and I need to extract the dates from the filenames so I can then figure out which dates I am missing (I should have 15,706 in total, but in some dirs. I only have ~15,600)
Here's an example
maxTemps <- list.files("./Daily/Daily_TMax/", recursive = TRUE, pattern = ".asc$", full.names = FALSE)
length(maxTemps)
[1] 15697

head(maxTemps)
[1] "1970/eMAST_ANUClimate_day_tmax_v1m0_19700101.asc" "1970/eMAST_ANUClimate_day_tmax_v1m0_19700102.asc"
[3] "1970/eMAST_ANUClimate_day_tmax_v1m0_19700103.asc" "1970/eMAST_ANUClimate_day_tmax_v1m0_19700104.asc"
[5] "1970/eMAST_ANUClimate_day_tmax_v1m0_19700105.asc" "1970/eMAST_ANUClimate_day_tmax_v1m0_19700106.asc"

tail(maxTemps)
[1] "2012/eMAST_ANUClimate_day_tmax_v1m0_20121226.asc" "2012/eMAST_ANUClimate_day_tmax_v1m0_20121227.asc"
[3] "2012/eMAST_ANUClimate_day_tmax_v1m0_20121228.asc" "2012/eMAST_ANUClimate_day_tmax_v1m0_20121229.asc"
[5] "2012/eMAST_ANUClimate_day_tmax_v1m0_20121230.asc" "2012/eMAST_ANUClimate_day_tmax_v1m0_20121231.asc"

I've been able to use the following code to get the years (based on the folder)
regmatches(maxTemp, regexpr("[0-9]{4}", maxTemp))

I thought I could use this, with invert = TRUE to return the rest of the strings because if I try to include the constant part of the filename in the regexpr I get an error
maxTempsFiles <- regmatches(maxTemp, regexpr("[0-9]{4}\/(eMAST_ANUClimate_day_tmax_v1m0_)", maxTemp), invert = TRUE)
Error: '\/' is an unrecognized escape in character string starting ""[0-9]{4}\/"

So I thought I could use the code that works and then subset out the constant part of the filename leaving me with the date, and then I would just need to remove the .asc with sub, but this returns some messy text
maxTempsFiles <- regmatches(maxTemp, regexpr("[0-9]{4}", maxTemp), invert = TRUE)
maxTempsFiles <- sub(x = maxTempsFiles, pattern = "/eMAST_ANUClimate_day_tmax_v1m0_", replacement = "")
maxTempsFiles <- sub(x = maxTempsFiles, pattern = ".asc", replacement = "")
head(maxTempsFiles)
[1] "c(\"\", \"19700101\")" "c(\"\", \"19700102\")" "c(\"\", \"19700103\")" "c(\"\", \"19700104\")" "c(\"\", \"19700105\")"
[6] "c(\"\", \"19700106\")"

The files always have /eMAST_ANUClimate_day_prec_v1m0_ in them, it is just the first folder which changes, and the end of the filename 19700101.asc through to 20121231.asc
If someone could provide some code/advice on how best to do this, that'd be great.
Comment: How about `gsub("(^.*_)(\\d+)\\.asc", "\\2", x)`?
Comment: Thanks for the quick anwer @RomanLuštrik. I haven't had much experience with `gsub`, `sub`, `regexpr` etc before.
Could you place your comment in an answer @RomanLuštrik, to explain exactly what it does? I'm trying to understand how it works.
Answer: This is a simple searching for partial matches of the string using groups - and returning the desired match in a group.
gsub("(^.*_)(\\d+)\\.asc$", "\\2", x)

Regex explanation:
group 1:
  (^.*_) - match beginning of string (^) and then any character until _ is found
group 2:
  (\\d+) - find any digit, several times (+)
no group:
  \\.asc$ - at last, find .asc, which should be the end of the string ($)

replacement argument in gsub is there to replace matched part of the string, or, return a desired group. For group 2, you will want \\2. Difference between sub and gsub is that former will return only first matched pattern while gsub will work on the entire vector.

Extracting dates from long filename


Developing a good practice routine for piano improvisation with a band

Question: I have been playing the piano for 2 years now and I can play some of my favorite pieces through sheet music and a lot of time. I have also been singing in a band for quite some years and want to incorporate the piano into some songs.
What could be a good practice routine for practicing piano improvisation/jamming? We're in a band with a guitarist/bass player/drummer and myself as a singer.
I can play and identify major/minor scales, but not instantly. I practice for 2 hours a day.
I was thinking about 20 minutes of practicing scales, 20 minutes of practicing chords and chord progressions and then spend the remaining time playing along with random snippets of music. Would this be the most effective way of increasing this skill?
Also if it's of any help I'm a quite an experienced guitar and bass player myself and have been writing songs and producing music for a couple of years now so I have a good idea of how I need to compliment other instruments and take the lead myself.
Comment: This may be helpful if not exactly the same question: [http://music.stackexchange.com/q/1266/28](http://music.stackexchange.com/q/1266/28)
Comment: Thanks for your reply Matthew. However I'm looking to put together the most effective routine specifically for the piano in a 2 hour time-frame.
Comment: Jim, couple of questions before I write an answer. First, what type of band? What type of music does the band play? Second, do you mean piano or keyboard? Not just for practice, but when playing with the band. Piano is one sound, keyboard/synth offers different sounds/patches and can influence the style of play and thus how you might practice.
Comment: It's a style that kind of leans towards Coldplay only with a more experimental edge (longer songs, different structures). And I mean piano, it is played on a MIDI keyboard though.
Answer: You've set yourself a schedule, and that's fine. It's now a question of what you do in that time, and what skills you need to develop.
You're likely to need a backing track. There are lots of options -- have a friend accompany you, play over a CD, record yourself, use a loop pedal, computer software, etc.
Alternatively, since you're on piano you can provide your own backing with your left hand. If you do this, introduce a metronome at some point.
A few disciplines you could follow:
Practice playing tunes you know, by ear
Start with simple tunes like "Twinkle Twinkle Little Star" and move on to more complicated ones. You're judging yourself on how accurately you play the tune, and how seldom you hesitate. You could start by doing this unaccompanied (so you can pause to find a note), then move on to a metronome (so if you mess up, you can resume on any beat), then move on to a full accompaniment.
If you find that you're playing from memory, move on to a different tune.
Practice embellishing tunes you know, by ear
As above, but vary the rhythm, add embellishments and fills - jazz, man! What you've learned playing by ear, will help you here.
Practice full improvisation
Play whatever you like, over a set chord progression.

In all of those cases, it can be helpful to sing or hum the improvised melody as you play it. This forces you to differentiate between playing the note you intended to play, and merely playing a note that sounds OK.
Also, practice in as many keys as you can. You should aim to be able to play a melody by ear, starting on any note. However, since you're in a band with a guitarist and bassist, it wouldn't do any harm to give extra attention to the keys guitarists tend to prefer -- E, A, D, G, C; Em, Dm, Am -- go by what keys the songs in your band's repertoire use.
Comment: Thanks for this. I find that although I can figure out how to play a piece or how to play what I hear in my head, I have never really practiced doing so with a steady beat/metronome.
Answer: This may not be hugely applicable as it's somewhat hard to find (I also hesitate to recommend a video game that doesn't actually involve teaching theory), but I find that Rock Band 3 with the keyboard attachment has some good exercises for improvisation. It's heavily based around scales and chords.
You're still reading notes, sort of, but as it's not actually sheet music, I find that it encourages thinking differently. Also, you're reacting to the notes in a very limited time frame, a bit like sight reading.
Of course, the default keyboard has a limited range (I think it's four octaves?), but as I recall there was an interesting variety of styles.
Comment: There is also a software called Reaper including a plugin called "NinJam" that allows you to jam online with people. It works pretty well and at least you don't stick to the genre of music you are used to playing.
Answer: Jim,
I play a different style, but I think the principles are the same no matter what. Practice is great, and I commend practicing scales and chords. That is your good personal practice.
But for improv with a group, the best practice is to play with the band. If you don't have chord charts for the songs you play, then make some. Start with 4 or 8 note chord "pads" (this is why I love the synth sounds - they don't decay like a piano patch). After a while, you will get really good at this, and ... bored. Great! Now start start experimenting with 'fill notes' and such between the chords. Scales are great here, as is knowledge of stuff like "circle of 5ths" and other methods to bridge between primary chords. Keep playing and messing about at this point, because this is where you are really developing building blocks for improv.
If you can't do this with the band, then either a) play along with coldplay songs, or b) record your band at a gig or practice and play along with that.
That should get you going.
-R
Comment: Thanks for this, I should look into the circle of fifths more as I've been quietly avoiding it for a while
Answer: Your keyboard practice routine needs to:
1). improve your finger ability in both hands - 20%.
     - drills involving scales and arpeggios up and down in both hands
2) improve your ability to locate harmonies (chords) - 40%
   - learning shell chords in the left hand
   - learning common chord voicings for your kind of music in both hands
3) improve your ability to create bass lines in the left hand -20%
   - outline the root progression
   - appropriate walking bass for different styles of music
4) improve your ability to ability to create lines in the right hand - 20%
   - improvised melodies
   - licks
ABANDON THE SHEET MUSIC!!
Comment: I wouldn't say abandon sheet music, but it definitely shouldn't be the majority of your practice period.
Answer: In my opinion, practicing seventh chords within 12 major (Ionian) scales are more effective in learning scales than playing scales with a single note; Practice your scales with a seventh chord in root position (the root starting on Do) for every Major key. That will get you to be able to "See" or "Feel" each key. It will also help your ear, as many jazz and pop progressions have predominantly movement in fourths and/or in the chromatic scale.
Comment: Hi Brian - I'm not sure this answers the question which is about practice for improvisation with a band.


Testing a Null Hypothesis Nested by the Alternative Hypothesis

Question: Consider a parameter of interest $\beta \in \mathcal{B}$, and two hypotheses
$$H_0:\;\beta\in\mathcal{B_0}\quad versus \quad H_1:\;\beta\in\mathcal{B}_1$$
where $\mathcal{B}_0\cup\mathcal{B_1}=\mathcal{B}$ and $\mathcal{B}_0\subset\mathcal{B_1}$.
That is, the null hypothesis is a subset of the alternative hypothesis.
To be specific, I am dealing with hypotheses
$$H_0:\;\beta\geq0\quad versus \quad H_1:\;\beta\geq c$$
where $c$ is an unknown negative constant (i.e. $c<0$ and $c$ is unknown).
Is there any method or approach related to this situation?
(this post is closely related to my past posting Testing a special inequality hypothesis)
Comment: Conventionally, the class of distributions $\mathcal P$ is classified into two mutually exclusive subclasses $\mathcal H_0\uplus \mathcal H_1= \mathcal P. $ That is the basis of hypothesis testing. The current situation is pretty unorthodox and it seems evident there would be cases where both would be true.
Comment: @User1865345 Not only mutually exclusive, but complementary.
Answer: By definition (see e.g. Jun Shao "Mathematical Statistics", 2nd edition, chapter 6, first paragraph) of statistical hypothesis tests, the null and the alternative hypothesis must be disjoint. Moreover, both hypotheses together must contain all possibilities.
If you are interested in the probability of $H_0$ compared to the probability of $H_1$, you might want to approximate the density and then integrate it over both $H_0$ and $H_1$.
Comment: I'm not sure that the requirement to contain all possibilities is necessarily absolute. e.g. This would appear to typically preclude the testing of simple-vs-simple hypotheses as in the Neyman-Pearson lemma. (That's not to say I don't think it's generally both an important concept and a good strategy.)
Comment: @Glen_b the simple-vs-simple hypotheses consider population families with just two members, so in this sense, all "possibilities" (i.e. all populations in the considered family) are contained in either $H_0$ or $H_1$. See also the first paragraph of chapter 6 in Jun Shao's "Mathematical Statistics".
Comment: @Glen_b I agree that in general we only need disjointness. E.g. [Young and Smith (2005)](https://doi.org/10.1017/CBO9780511755392) write on page 65 "Throughout we have a parameter space $\Theta$, and consider hypotheses of the form $H_0:\theta \in \Theta_0$ vs. $H_1:\theta \in \Theta_1$, where $\Theta_0$ and $\Theta_1$ are two disjoint subsets of $\Theta$, possibly, but not necessarily, satisfying $\Theta_0 \cup\Theta_1=\Theta$."
Comment: @User1865345 Yes, they also agree with Shao's definition.
Comment: @statmerkur But what is then $\Theta \setminus (\Theta_0 \cup \Theta_1)$ used for? Why not setting $\Theta = \Theta_0 \cup \Theta_1$ ?
Comment: @statmerkur My point is that it does.
Comment: It's perfectly possible I missed something, but by recollection this is not the case in Neyman and Pearson (1933)
Comment: Also @frank, Lehmann and Romano's Testing Statistical Hypotheses in _Stating the Problem_.
Comment: @frank Simply because $\Theta_0 \cup\Theta_1$ might not correspond to the parameter space at hand.
Comment: @frank We seem to disagree on the definition of a parameter space then.


Irreducible polynomials of degree 2 over $\mathbb{F}_4$

Question: I am trying to figure out a quick way to determine the 2nd degree polynomials of $\mathbb{F}_4$ that are irreducible. I already know that for degree 1, we have $x$, $x+1$, $x+a$, $x+b$. From here it is clear that the following 2nd degrees are reducible:
$x*x=x^2$
$x(x+1)=x^2+x$
$x(x+a)=x^2+ax$
$x(x+b)=x^2+bx$
$(x+1)(x+1)=x^2+ax+1$
$(x+1)(x+a)=x^2+bx+a$
$(x+1)(x+b)=x^2+(1+b)x+b=x^2+(1+a+1)x+b=x^2+ax+b$
$(x+a)(x+a)=x^2+(a+a)x+b=x^2+b$
$(x+a)(x+b)=x^2+(a+b)x+a(1+a)=x^2+(a+1+a)x+(a+b)=x^2+x+1$
$(x+b)(x+b)=x^2+(b+b)x+(a+1)b=x^2+ab+b=x^2+a(a+1)+b=x^2+b+a+b=x^2+a$
So the remaining 2nd degree polynomials are:
$x^2+1$, $x^2+ax+a$, $x^2+bx+b$, $x^2+x+a$, $x^2+x+b$, $x^2+bx+1$
Am I on the right track here? How would I test the remaining ones to see if they are irreducible?
Comment: Can you please elaborate what $2,3 \in \mathbb F_4$ are? I suspect you're mixing up $\mathbb F_4 \neq \mathbb Z / 4 \mathbb Z$!
Comment: @user3784030 And then what would $\;2\cdot3\;$ in your notation be? Not to mention that using the symbol $"2"$ in a field of characteristic $\;2\;$ seems to be an invitation to confusion.
Comment: Well, $\mathbb{F}_4$ has ${0,1,w,\bar{w}}$, so if you just let $w=2$ and $\bar{w}=3$, it is all the same, isn't it?
Comment: Ah, I see the issue. In my notation $2*3=6=2 mod 4$, when in normal notation $w*\bar{w}=1$. My bad, I will have to fix this up.
Comment: Then you know $3+3=0$ and $2+2=0$? Check here https://en.wikipedia.org/wiki/Finite_field
Comment: In any ring, numbers like $2$ or $7$ naturally make sense in a canonical way : they are $1+1+\dots +1$ ; so you can't just assign them to any element because it seems to make sense in the context. You can also state this by saying that there is a unique ring morphism from $\mathbb{Z}$ to any ring.
Answer: In any field, a polynomial of degree $2$ is reducible if and only if it is split. So your method of listing all possible split polynomials does give all the reducible ones (and then by taking the complement, the irreducible ones).
On the other hand, you need to fix your notation for the elements of $\mathbb{F}_4$ as pointed out in the comments, it gives false results (even though the method itself is valid).
Comment: I have edited my question, correcting the notation: after doing so, I was able to find that there are 16 in total (which makes sense, as $4^2$ combinations), and I found 5 that did not appear to be reducible: $x^2+1$, $x^2+ax+a$, $x^2+bx+b$, $x^2+x+a$, $x^2+x+b$, $x^2+bx+1$. Is this correct?
Comment: You corrected the notation, but not the calculations. You still write that $a=1+1$ when you write $(x+1)(x+1)=x^2 + ax +1$. The problem is not so much the notation itself as the fact that it gives wrong answers : in $\mathbb{F_4}$, $1+1=2=0$.
Answer: Any quadratic $\Bbb F_4$-irreducible polynomial has its roots in $\Bbb F_{16}$, say $\rho$ and $\bar\rho$, forming an $\Bbb F_4$-conjugate pair of elements of $\Bbb F_{16}$. And each such pair gives you a unique monic quadratic polynomial irreducible over $\Bbb F_4$.
There are sixteen elements of $\Bbb F_{16}$, and four of them are in $\Bbb F_4$ — each of those is its own conjugate. Since there are twelve elements of $\Bbb F_{16}\setminus\Bbb F_4$, these fall into six conjugate pairs. So there are just six monic irreducible quadratics over $\Bbb F_4$.
Comment: Do the solutions I listed up there look correct? I tried to test them, and they seem to be good, but I am not sure.
Comment: No, $x^2+1=(x+1)^2$, and I think you're missing $x^2+ax+1$. I suspect that the others are right. It's easy enough to check, by just substituting in the four elements of $\Bbb F_4$ and verifying that the result is never zero.


check checkbox with Javascript

Question: I'm trying to autocheck a checkbox (terms & condition) in a checkout page with javascript when my chrome extension is running but any code I have tried are not working. The website is https://www.supremenewyork.com/shop/all. You need to add an item in the basket to go to the checkout page and there you can find the checkbox.
I already fillup the rest of the information in the checkout and just missing the autocheck the checkbox.
This is one of the code I have tried:
document.getElementById("order_terms").checked  = true;

I have also tried get element by class, name and trying with all the different id, names that I have found in inspect (f12). I am trying all this code on the google chrome console for testing purpose.
Comment: That checkbox _does_ get checked, the box replacing it just doesn't display it. How about `if(!document.getElementById("order_terms").checked){ document.getElementById("order_terms").nextElementSibling.click(); }`?
Comment: What does the HTML look like?
Comment: @SebastianSimon thank you! that was what I was looking for! Didn't know about nextElementSibling. I'll do some research to understand it more.
Comment: @Faiyaz `nextElementSibling` is just part of the [DOM API](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model) for basic DOM traversal.
Answer: Looks like the website is using something called icheckbox. It's a replacement for the normal checkbox and needs an additional class, .checked to be added to it before it visually displays as checked.

  {...rest of your checkbox html}

check checkbox with Javascript


Uniform Boundedness Principle

Question: This is a homework question, I have no clue where to start!
Use Uniform Boundedness Principle to prove the following statement.
A subset $X$ of a normed space $E$ is bounded if and only if $f(X)$ is bounded in $\mathbb{K}$ for each $f\in E'$ (where $\mathbb{K}$ is $\mathbb{R}$ or $\mathbb{C}$)
Any hints appreciated!
Answer: Suppose that $X$ is unbounded. Hence there exists a sequence $(x_n )$ such that $||x_n ||\to\infty.$ Let us define an sequence $h_n : E' \to \mathbb{R}$ of linear functionals by $h_n (f) =f(x_n ).$ By assumptions for every $f\in E'$ the sequence $h_n (f) $ is bounded so by Uniform Boundedness Principle the sequence $||h_n ||$ is bounded but $||h_n || =||x_n ||$. Contradiction.

Uniform Boundedness Principle


htaccess 301 redirect - Remove query string (QSA)

Question: I've been struggling with some htaccess redirects. I just spent some time reading and searching on stack and couldn't get an anwser that works with my scenario.
I'm in the process of making the 301 redirect for an old client website to a new one. The old pages has parameters query which I want to remove from the url.
/menu.php?idCategorie=29&idDetail=172
to
/new-website-page/
I have multiple queries to do, here's a couple example:
/menu.php?idCategorie=29&idDetail=172
/menu.php?idCategorie=29&idDetail=182
/menu.php?idCategorie=29&idDetail=184
/menu.php?idCategorie=29&idDetail=256

Which all link to different new pages.
Here's what I tried:
RewriteCond %{QUERY_STRING} idDetail=172
RewriteRule ^menu.php(.*) /new-page/? [R=301,L]

I get redirected correctly, but the URL keeps the query string:
http://website.com/new-page/?idCategorie=29&idDetail=172

I also tried this:
RewriteRule ^menu.php?idCategorie=29&idDetail=172$ http://website.com/new-page/? [L,R=301]

And this:
RewriteCond %{QUERY_STRING} idDetail=172(.*)$
RewriteRule ^menu.php /new-page-name?$1 [L,R=301]

And it didn't work (Still have the query string at the end)
Thanks!
Answer: You can use this rule:
RewriteRule ^menu\.php$ /new-page-name? [L,R=301]

Take note of trailing ? in the end which is used for stripping off any existing query string in the original URI.
Answer: In addition to anubhava's answer you can alternatively use the QSD flag from Apache 2.4.0
RewriteRule ^menu\.php$ /new-page-name [L,R=301,QSD]

http://httpd.apache.org/docs/current/en/rewrite/flags.html#flag_qsd


Slow performance for rabbitmq amqp_login (PHP client)

Question: I'm using php-amqp to read and write from a local rabbitmq server.  This is for a high traffic website.  Following the example at http://code.google.com/p/php-amqp/, I haven't found a way to avoid calling amqp_login with every web request.  The call to amqp_login is, by far, the slowest in the sequence.  Is there an easy way to bypass the need to call this with every web request?  We're using Apache on SuSE linux.
<code>$time = microtime(true);
$connection = amqp_connection_popen('localhost', 5672);
print "connect: ".(microtime(true) - $time) . "\n";

$time = microtime(true);
amqp_login($connection, 'guest', 'guest', '/');
print "login: ".(microtime(true) - $time) . "\n";

$time = microtime(true);
$channel = 1;
amqp_channel_open($connection, $channel);
print "channel open: ".(microtime(true) - $time) . "\n";

$time = microtime(true);
$exchange = 'amq.fanout';
amqp_basic_publish($connection, $channel, $exchange, $routing_key, 'junk', $mandatory = false, $immediate = false, array());
print "publish: ".(microtime(true) - $time) . "\n";
</code>
Example Results:
<code>connect: 0.00019311904907227
login: 0.041217088699341
channel open: 0.00034213066101074
publish: 5.6028366088867E-5
</code>
Comment: Depending on what you're doing there may be, if you can, try publishing multiple entries at a time.
Comment: After discovering this, I am modifying the code so that both queue writes use the same connection + exchange.  That way, I can at least avoid logging in twice.
Answer:
Is there an easy way to bypass the need to call this with every web request?

Not specifically familiar with this package, however....
It's suprising that it allows persistent connection, but not persistent authentication - one way to fix this would be to key the connection pool based on server+port+username+password rather than just the server+port, which wouldn't require too much change to the code.
If you don't feel like modifying the C code / arguing with the maintainers, then you could run a proxy daemon which maintains an authenticated connection.
Comment: It is strange (that persistent connection requires re-authenticating).  We're not tied to this AMQP package.  It's worthwhile to switch to another PHP AMQP package if there is a better one.
Comment: I'll keep the proxy server idea in mind.  I've already used that in a different case where recreating an object with every web request was too expensive.  Things would be so much easier if there were a way to persist a PHP object between requests.
Comment: '...persist a PHP object...' there's lots of ways of doing that - the real problem though is that *resources* can't be serialized.
Comment: Sorry for never coming back to comment on this.  It's been 10 years.  If I remember correctly, the underlying issue was in our PHP library which managed HTTP connections.  They didn't provide a way to share memory or connections across separate requests.

After realizing that was the issue, we dropped the unnecessary rabbitmq and instead routed traffic through a newly created internal service.  The new service simply maintained a pool of connections to an external service which we frequently queried.
Comment: So, we never did find a performant solution with php-amqp.  But, we put no effort into that since we didn't need the queue after setting up the connection-pooling service.


Ordenar uma matriz segundo coluna

Question: Como ordenar a seguinte matriz segundo a coluna das coordenadas x? Eu quero que nao ordene apenas as coordenadas, mas que me troque as linhas de maneira a que as coordenadas das cidades estejam corretas.
O que tenho até agora, funciona para as 3 primeiras cidades mas a ultima é simplesmente uma cópia de uma linha anterior.
matriz_ficheiro=[['Nome' 'Coordenada X' 'Coordenada Y' 'Peso']
 ['Lisboa' '36.752' '204.319' '3']
 ['Copenhaga' '1971.323' '2086.417' '5']
 ['Berlin' '2046.659' '1735.743' '4']
 ['Madrid' '520.862' '392.972' '2']]

lista_x=[]
for i in range(1,matriz_ficheiro[:,0].size):
    lista_x.append(float(matriz_ficheiro[i][1]))
matriz_ordenada_x=matriz_ficheiro

 for i in range (1,matriz_ficheiro[:,0].size):
    minimo_x=min(lista_x)
    for j in range (1,matriz_ficheiro[:,0].size):
        if minimo_x==eval(matriz_ficheiro[j][1]):

            matriz_ordenada_x[i][0]=matriz_ficheiro[j][0]
            matriz_ordenada_x[i][1]=minimo_x
            matriz_ordenada_x[i][2]=matriz_ficheiro[j][2]
            matriz_ordenada_x[i][3]=matriz_ficheiro[j][3]
    lista_x.remove(minimo_x)

O resultado é o seguinte:
--------------------------------------------------------------------------------
|     Nome           |     Coordenada X   |     Coordenada Y   |     Peso      |
--------------------------------------------------------------------------------
|     Lisboa         |     36.752         |     204.319        |     3         |
--------------------------------------------------------------------------------
|     Madrid         |     520.862        |     392.972        |     2         |
--------------------------------------------------------------------------------
|     Berlin         |     2046.659       |     1735.743       |     4         |
--------------------------------------------------------------------------------
|     Berlin         |     2046.659       |     1735.743       |     4         |
--------------------------------------------------------------------------------

Answer: Você pode utilizar o itemgetter do módulo operator e simplificar bastante seu código:
from operator import itemgetter

matrix_ficheiro = [['Nome', 'Coordenada X', 'Coordenada Y', 'Peso'],
 ['Lisboa', 36.752, 204.319, 3],
 ['Copenhaga', 1971.323, 2086.417, 5],
 ['Berlin', 2046.659, 1735.743, 4],
 ['Madrid', 520.862, 392.972, 2]]

# Recebendo a primeira linha da matriz (Nome, Coordenada X, Coordenada Y, Peso)
# Salvando em uma nova matriz que estará ordenada
matrix_ordenada = matrix_ficheiro[0]

# Ordenando pela segunda coluna da matrix (desconsiderando a primeira linha)
sub_matrix_ordenada = sorted(matrix_ficheiro[1:], key=itemgetter(1))

# Percorrendo a sub matriz e adicionando cada elemento na nova matriz ordenada
for elem in sub_matrix_ordenada:
    matrix_ordenada.append(elem)

# Opcional
# Matriz original recebe a nova matriz ordenada
# É opcional, pois você pode simplesmente usar a matriz ordenada
matrix_ficheiro = matrix_ordenada

Ordenar uma matriz segundo coluna


Update CCK Field Every 90 Days

Question: I'm using Drupal 7. I have a CCK field called Placement Date (Placement_Date) that the end user will manually put in. I then have two text fields, Start_of_Period and End_of_Period. Start_of_Period initially is set to Field->Placement_Date and Field->End_of_Period is set to 90 days out. The setup is no problem, but after 90 days Field->Start_of_Period needs to be the old End_of_Period + 1, and Field->End_of_Period needs to be another 90 days out from the new Field->Start_of_Period. I looked to Rules to try to find a way to schedule this, but cannot figure out what I need to be doing.
Any help would be greatly appreciated. Thanks in advance.
Answer:

You need to set up cron tab on your server may be once a day
use hook_cron() to add your custom code write query to grab nids for which field values needs to be updated.
use node_load_multiple() to load all nodes with the query returned nids
update the field values and save the node again in a loop.
set the crontab to run at the time when your site receives least traffic, this way it will not be performance issue as well.
Comment: Thank you for your quick response.

I think this would be great if we weren't already running cron. We currently have cron running for reindexing and updating another portion of the site. It looks like hook_cron() will check/update the necessary fields every time cron runs, which is a few times a day already. I worry about the over head this will cause if we grab every node with these fields and process the check/update multiple times a day during potential peak hours.

Is hook_cron() our only option do you think?
Comment: very true, but you have better option to extend drupal core cron use [elysia cron](https://drupal.org/project/elysia_cron). With this module you can get hold of each tasks individually, also you can have control over scheduling each task separately.
I am using elysia cron module try it, if the above answer does not fit your requirement because of cron,elysia cron will be the solution
Comment: Perfect. Thank you very much. I think that should work.
Comment: Just remember drupal core cron works as per the site is accessed, if you need every thing right on time ;) set crontab on your server and so is elysia cron it can be configured easily to work both way.

Update CCK Field Every 90 Days


I'm on Unity 2017 LTS now. To upgrade, should I go right to 2019 LTS or should I go through 2018 LTS first?

Question: My end goal is to keep up with upgrades and stay on the latest LTS. I'm concerned about not having a lot of time left for 2018 LTS. If doing the upgrades incrementally is helpful, I'll do that regardless. If a 2018 upgrade introduces problems that I wouldn't have to address with a direct to 2019 upgrade, I may be interested in skipping 2018. I realize 2019 LTS isn't out at the moment, but it should be any time now.
Also, is it ever helpful to make a new project and move my existing assets over gradually in logical pieces? Or is it always best to just open the existing project with the new version (after a back up) and start tackling errors? TIA
Comment: You could always make a copy of an existing working project and go right for the latest version and see if any problems pop up and try fixing them from there.With this approach you don't need an intermediate version.
Comment: First, carefully read the release notes and be sure you can live with the things that have changed. 2019 drops support for older iOS and Android versions that you may have existing players on. When you do choose a version to upgrade to be sure to delete your Library directory before opening the project in the new editor. I've seen quite a few WTF type problems just go away because of that simple step where the old Library just confused Unity. Definitely use source control or make a backup (or more than one) before you open the project with a new editor.
Answer: Doing upgrades incrementally (keeping up with minor version changes) is only reccomended if you need a feature introduced in a specific minor version. Normally moving from LTS version to the next is fine for a long running project.
You won't really be saving yourself any work by going through 2018 first (although it's likely it doesn't hurt much either if you're already on 2017.4.x). If there isn't any particular reason you're thinking about upgrading you can probably stay on 2017 LTS version until 2019 LTS is out.
As for the upgrade process itself, opening the project in the new editor version and fixing any errors that pop up is the standard approach. The way you mention "backing up first" makes me feel the need to add: you should try using some form of source control. Git and Unity work quite well together, and if anything goes wrong with your upgrade, you can always roll back to a stable commit.
Comment: We're just trying to stay on the latest stable code base in general. Newer versions are appealing because of the added features and the extra length of Unity support. By the time I get to the 2018 upgrade we'd probably have something like 6 months of support left before I'm forced into another upgrade. So I was thinking of going for it all at once. We're using SVN for version control by the way.
Comment: Sounds like you're probably fine to wait until 2019.4.x then.


Tangent plane equation of an implicit equation of a surface

Question: How am I supposed to find the equation of a tangent plane on a surface that its equation is not explicit defined in terms of z?
The equation of the surface is:
$$
x^{2} -y^{2} -z^{2} = 1
$$
And I need to find all the points where the tangent plane is parallel to the plane:
$$z = x + y
$$
How can I do that?
My approach at the moment was to think about the gradient vector of the function as being the normal vector of the plane... But the gradient vector of that surface has only two components (partial x and y), and the vector the normal vector of the plane has three components (1,1,-1)...
Answer:
My approach at the moment was to think about the gradient vector of the function as being the normal vector of the plane... But the gradient vector of that surface has only two components (partial x and y), and the vector the normal vector of the plane has three components (1,1,-1)...

Don't consider the surface as the graph of one (or more) function(s) of the form $z=g(x,y)$, but as a level set of a function of three variables:
$$f(x,y,z) = c$$
where in your case $f(x,y,z)=x^2-y^2-z^2$ and $c=1$. Then the gradient of $f$ is:
$$\nabla f = \left( \tfrac{\partial f}{\partial x}, \tfrac{\partial f}{\partial y}, \tfrac{\partial f}{\partial z} \right) = (2x,-2y,-2z)$$and this gradient is perpendicular to the surface, so parallel to a tangent plane at that point.
Comment: Perpendicular to the surface, so parallel to the tangent plane? Wouldn't it be Parallel to the surface at that point, so parallel to the tangent plane?  perpendicular gradient makes no sense to me
Comment: Now it makes sense! So I just need to check when my gradient is going to be a multiple of the normal vector of the plane, right?
Comment: Right, because if those are multiples, they're parallel. In a point _on the surface_, of course.
Comment: *In a point on the surface* : i.e. you have to find the points $(x,y,z)$ such that $\nabla f$ is parallel to $(1,1,-1)$ AND $f(x,y,z)=c$
Answer: One approach would be to calculate the normal to the surface and check when it is parallel to the normal $(1,1,-1)$ of the plane.
The normal of the surface is just the gradient of the implicit function which defines it, i.e. $(2x, -2y, -2z)$.
Comment: Wait... I agree that the normal of the surface is the gradient of my implicit function. But how am I able to get $-2z$? 2x is partial x in respect to z, and the same for $-2y$. But  $-2z$ is the partial of z in respect to?! I'm not getting it.


Best way to visualize data with two keys and many rows in R (heatmap, mosaic plot, treemap, ggplot)

Question: I have been wrestling with the problem of creating some type of visualization for my data.
I have two keys (used in data table to create the data) that are intervals. I constructed the intervals from what used to be continuous variables in my data set. It is important for me to be able to construct these manually because I do not want the same breaks in the buckets. This has been completed to my satisfaction.
Specifics about the data: The data is in the form of a data frame with rownames that are 187 unique intervals. (I also have a data table that has these values in separate columns if that would help in visualization.) I have 4 categories which have corresponding counts for how many observations fall within the interval.
for example it looks like this:
<code> (interval1_1, interval2_1) :   number_category1 ... number_category4
 .
 .
 .
 (interval1_187, interval2_187): number_category1 ... number_category4
</code>
I have attempted to create a heat map; however, there are too many rows for my heat map to be able to read the intervals. I have also looked into mosaic plots and treemaps. I would be happy to restructure the data in a way that is better suitable for visualization.
Right now, the best I can do is a visualization using ggplot.
<code>ggplot(df, aes(x=interval_1, y=interval_2)) + geom_jitter(aes(size=N, col=four_types),position=position_jitter(.2,.2)) + scale_size(range=c(2,9))
</code>
The above code creates a plot with points of a size corresponding to how big the count is for that particular interval. While not all the intervals contain each of the four_types, effectively there are 4 points of different color based on their type that are slightly jittered around each corresponding interval that are on one axis each.
In addition to my question of whether anyone can think of a better way to visualize this data, I have not been able to create as many splits in the size variable as I want to for the plot formed from the ggplot code above. Right now, it uses only four different sized points. When I try to use scale_size_continuous, the first three-four points are not different enough in size from each other, so effectively, I still get four points even when I supply numbers for the breaks. I have played around with the range inside of scale and scale_size_continuous, but I haven't quite gotten anything to work.
Thanks!
Comment: What are the `number_category`s? What kind of data are they? Why not jost use a dotplot?
Comment: @gung - the numbers are counts of data that fall within the two intervals; they range from 0 to 1.7 million; most fall within the realm of [0, 200k]. The counts were generated using the data.table command .N using the keys. So, it counts the data for each of the four types that fall within the specified interval.
Comment: @gung I have two questions about using dot plot: is the scale of the counts not too large? how would I incorporate all four categories into one dot plot?
Answer: With that much data (187 x 187 x 4 categories), I think the main options are 4 heat maps/scatterplots using color for the count (or log count if skewed). Here's a heat map sized so each square is 3x3 pixels.

Another option is two-levels of graphs:

A coarser view with fewer interval groups (10x10 instead of 187x187)
Zoomed in views of those blocks which are of interest of are selected on demand

Here's an example of the coarser view, which allows all four category counts to be summarized for that block. I've used a line plus background color for the summary, but it could be a treemap or other view instead.


Android Studio java: listview em uma activity, mandar informações para ela de outra activity

Question: O código da lista:
public class Main2Activity extends Activity {
    //Atributos
    private SQLiteDatabase bancoDados;
    private ArrayAdapter<String> itensAdaptador;
    private ArrayList<String> itens;
    private ArrayList<Integer>ids;
    private Button adicionar;
    private EditText txt;
    private ListView lista;
    private String Name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lista= (ListView)findViewById(R.id.list);
        adicionar=(Button)findViewById(R.id.button);
        txt=(EditText)findViewById(R.id.txt);

        // Criando Banco dados
        bancoDados = openOrCreateDatabase("apptarefas", MODE_PRIVATE, null);

        //Criando Tabela do banco
        bancoDados.execSQL("CREATE TABLE IF NOT EXISTS tarefas(id INTEGER PRIMARY KEY AUTOINCREMENT,tarefa VARCHAR)");

        recuperarTarefas();
        adicionar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String texto = txt.getText().toString();
                salvarTarefa(texto);
                txt.setText("");
            }
        });
        //Clicar e remover tarefa
        lista.setLongClickable(true);
        lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                removertarefa(ids.get(position));
                return true;
            }
        });
    }
    //Metodos
    public void salvarTarefa(String texto){
        try {
bancoDados.execSQL("INSERT INTO tarefas (tarefa)VALUES('" +texto+ "')");
recuperarTarefas();
}catch (Exception e ){
            e.printStackTrace();
        }
    }
    public void recuperarTarefas() {
        try {
            //Recupera as Tarefas
            Cursor cursor = bancoDados.rawQuery("SELECT * FROM tarefas", null);

            //Recuperar os ids das colunas
            int indiceColunaId = cursor.getColumnIndex("id");
            int indiceColunaTarefa = cursor.getColumnIndex("tarefa");

            //criar adaptador
            ids=new ArrayList<Integer>();
            itens= new ArrayList<String>();
            itensAdaptador = new ArrayAdapter<String>(
                    getApplicationContext(),
                    android.R.layout.simple_list_item_2,
                    android.R.id.text2,
                    itens);
            lista.setAdapter(itensAdaptador);

            //listar as tarefas
            cursor.moveToFirst();
            while (cursor != null) {

                //Log.i("Resultado", "Tarefa: " + cursor.getString(indiceColunaId));
                Log.i("Resultado", "Tarefa: " + cursor.getString(indiceColunaTarefa));
                itens.add(cursor.getString(indiceColunaTarefa));
                ids.add(Integer.parseInt(cursor.getString(indiceColunaId)));
                cursor.moveToNext();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void removertarefa(Integer id){
        bancoDados.execSQL("DELETE FROM tarefas WHERE id="+id);
        recuperarTarefas();

    }

}

Answer: //Enviando valores
lista.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long id) {
            String descricao = itens.get(position);
            Integer id = ids.get(position);

            Intent intent= new Intent(Main2Activity .this, SUA_OUTRA_ACTIVITY.class);
            intent.putExtra("descricao", descricao);
            intent.putExtra("id ", id);
            startActivity(in1);
        }
    });

//recebendo valores
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String descricao = getIntent().getStringExtra("descricao");
        Integer id= getIntent().getIntegerExtra("id", 0);

    }

Espero que ajude!


How to programatically add attachments to a user (securely)

Question: To any Drupal 7 gurus out there... I have hundreds of PDFs that need to be uploaded to users as their own private attachment. I've created the PDFs in the folder:-
../website-files/userfiles-{uid}/filename.pdf  (There's one for each uid)
My field_data_field_attachments table has the "field_attachments_fid" column with the id relating to the file_managed table.
This is all pretty self explanatory, until... I find the file_managed table has two mysterious columns:-

__pk
   __unique_uri

Both seem to be MD5 checksums, e.g:-

4A448458-860C-4E16-B901-F8841136C17D

So my question is; how do I correctly generate these two values for each attachment I import to the DB?
Thanks,
Darren
Comment: Those should be indexes, not fields...one for primary key (pk) and a unique index for the uri field (unique_uri)
Comment: Thanks Clive - So they should be generated automatically  by SQL when I import the data?
Comment: I would guess so, where exactly are you seeing these extra fields? Is it an SQL dump file?
Comment: Yeah, It's a SQL dump where I see the two keys. The __unique_uri field is generated by SQL, but the __pk value must be generated in PHP. I've tried an MD5 of the URI but not sure if that's correct.
Answer: You want to try and use the drupal api to move these files into drupal. Since you talk about private files, I assume you've enabled a private filesystem ... or else this discussion is pointless as all your files with be public :).
Once you've done that you could take your hundreds of files and put them in your server someplace and use file_move to place them into the drupal private filesystem using a small php script.
A module tutorial with a private file upload widget can be seen at this blog post and uses the drupal api to store a private file. Private files typically use hook_file_download which that blog post also shows to allow access to the file.
Comment: Yeah I got private files enabled :) Ah - So there's no way for me to easily generate the keys so I simply import the data at the SQL level? **EDIT** just seen Clive's comment above - these two values should hopefully be generated automatically when I import the rest of the table data...
Answer: OK, I've given up trying to do it in raw SQL due to the intricate referential integrity of the Drupal datamodel (OK, I admit defeat!! :)
The following code bootstraps drupal, then moves files from an input directory to the private-files folder on the local drive and uses the Drupal file_save() API to create the Drupal file registers, then updates the user profile custom field (in my case called "field_attachments").
hope it helps anyone trying to do the same. (I created a standalone PHP file called "importuserfiles.php" in the root of the drupal folder using the code below)
<?php
define('DRUPAL_ROOT', getcwd());
$_SERVER['REMOTE_ADDR'] = "localhost"; // Necessary if running from command line
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

$local_from_path = 'c:\\inputfiles'; // root folder of your input folders.
$drupal_private_base = 'c:\\website-files'; // root location of Drupal's private website files.

$di = new RecursiveDirectoryIterator($local_from_path);
foreach (new RecursiveIteratorIterator($di) as $filename => $file) {

    $user_id = explode('-', $di);   // the userid is held in the folder name (userfiles-uid)
    $uid = $user_id[1];             // so pull the second value from the array (after userfiles-)

    echo $filename . ' - ' . $file->getSize() . ' bytes. Attaching to user: ' . $uid . '
';

    $local_to_path = $drupal_private_base . '\\userfiles-' . $uid . '\\' . basename($filename); // the destination folder on the local file system for this user.

    $drupal_to_path = "private://userfiles-" . $uid.'/'.basename($filename); // the private files folder URI for this user.

    echo "Moving file: " . $filename . " To: " . $local_to_path . '
';

    mkdir($drupal_private_base . '\\userfiles-' . $uid); // make the destination folder in case it doesn't exist.

    //move the file to the new location
    if (!rename($filename, $local_to_path)) {
    echo "failed to move $file..
";
        break;
    }

    $user = user_load($uid); // load the user object

// create the file object for Drupal...
$filetosave = new stdClass();
$filetosave->filename = basename($filename);
$filetosave->filepath = $filename;
$filetosave->uri = $drupal_to_path;
$filetosave->filemime = file_get_mimetype($filename);
$filetosave->filesize = filesize($filename);
$filetosave->display = 1;
$filetosave->description = basename($filename); // just use the filename as the description
$filetosave->uid = $uid;
$filetosave->status = FILE_STATUS_PERMANENT;
file_save($filetosave);  // save the file so Drupal registers it.

$user->field_attachments['und'][] = (array)$filetosave; // add the file to the file field called "field_attachments"
user_save($user);
?>


PHP output to HTML from database

Question: I cant output the values of the row value to the html content any suggestions on how to do that? i tried using different methods so that i will print on the page but it's always blank is there any way  to do it?
<?php

//connect
$dbh=mysql_connect ("localhost", "xxxx_admin", "xxxx")
   or die ('I cannot connect to the database.');
 mysql_select_db ("xxxx_Client");

$term = $_POST['term'];
echo $term;

$sql = mysql_query("select * from ClientTable where FName like '$term'");

if ($row['FName'] == $term){

$ID = $row['ID'];
$FName = $row['FName'];
$LName = $row['LName'];
$PHON = $row['PHON'];

}

else
echo "invalid input";

?>

<title></title>

asdadasdad

<?php echo $FName; ?>
a

<?php echo $_POST["$LName"]; ?>
a

$FName

$LName

$ID

$PHON

sadasdasda
</bod>

Comment: Where are you setting `$row`?
Comment: you need some sort of `$row = mysql_result($sql,0);` call  Incidentally, you should also use the mysqli_* functions as the mysql_* flavor are deprecated.
Comment: Take a look to these examples: http://www.php.net/manual/en/function.mysql-fetch-array.php
Answer: First, you are probably not getting any results from your query.  Typically when using LIKE  you use some form of wildcard in the query like this:
select * from ClientTable where FName like '%$term%'

Second, you are not actually working with the result set.
You need to use some sort of mysql_fetch_array or similar to get the values into $row.
And of course, you really should not be using the mysql_* functions anyway as they are being deprecated in favor of mysqli_* or PDO.
Finally, your need to learn how to prevent SQL injection.  Your code is vulnerable now.
Answer: Try adding error_reporting(E_ALL); near the beginning of your code. There is a good chance that a notice message will tell you what you're doing wrong.

PHP output to HTML from database


Accessing an element that is within a symbolic matrix

Question: I created a 3x3 matrix in MATLAB, where each element of the matrix is an column vector. I am facing some difficulties when I try to access the element of one of the column vectors within the matrix.
syms a b c real

Cube = sym('K', [3,3])

K1_1 = [a; b; c];
K1_2 = [a; b; c];
K1_3 = [a; b; c];
K2_1 = [a; b; c];
K2_2 = [a; b; c];
K2_3 = [a; b; c];
K3_1 = [a; b; c];
K3_2 = [a; b; c];
K3_3 = [a; b; c];

for i = 1:3
    for j = 1:3
        x = Cube(i,j);
        K1_1(3,1) % this does not give an error
        x
        x(3,1) % this gives an error
    end
end

Here is the output of the code above:
Cube =

[ K1_1, K1_2, K1_3]
[ K2_1, K2_2, K2_3]
[ K3_1, K3_2, K3_3]

ans =

c

x =

K1_1

Error using sub2ind (line 52)
Out of range subscript.

Error in sym/subsref (line 766)
                R_tilde = sub2ind(size(L), Idx.subs{:});

Error in MainSimFile2 (line 22)
        x(3,1)

I successfully set x equal to an element of the matrix which is a column vector. However, I get an error when I try to access an element of the column vector.
How can I set up this matrix?
Answer: As written in the document

Cube = sym('K', [3,3]) creates an 3-by-3 symbolic matrix filled with automatically generated elements. The generated elements do not appear in the MATLAB workspace.

For example,
>> clear all
>> Cube = sym('K', [3,3])

Cube =

[K1_1, K1_2, K1_3]
[K2_1, K2_2, K2_3]
[K3_1, K3_2, K3_3]

>> whos
  Name      Size            Bytes  Class    Attributes

  Cube      3x3                 8  sym

In other words, K1_1 assigned by
K1_1 = [a; b; c];

is not the same K1_1 as either in
Cube =

[ K1_1, K1_2, K1_3]
[ K2_1, K2_2, K2_3]
[ K3_1, K3_2, K3_3]

or the one assigned to x by
x = Cube(i,j);

Therefore, this line is actually declaring a new variable K1_1.
K1_1 = [a; b; c];

Also see this answer.
Comment: It depends on what you are going to use the matrix. You have two sets of symbols here, namely `K` and `a, b, c`. Do you really need all of them?
Comment: ok thanks! Yes I understand that x is assigned to a new value. In that case what is the best way to create a matrix of vectors?


Idea to grow the adoption of Expatriates: expand the scope

Question: Consider a rename of the site to Transnationals. We could thereby expand the scope a little bit:

Questions about the transnational nature of the lifestyle of a person who has lived in (or lives in) more than one country;
Questions concerning multiple citizenship;
Questions relating to unions (marriages, etc.) between citizens of different countries;
Questions concerning the life with no citizenship, unclear citizenship, or a change of citizenship;

These categories are a little more broad than the idea of an "expatriate", who is someone who has simply left their original country. But the theme would remain the same. How to navigate life without the often assumed 1:1 relationship between a person and a country?
Discuss?
Comment: +1 for this question and [Expatriates sucks because most questions are about Immigration](https://expatriates.meta.stackexchange.com/q/284/164)
Comment: To be honest, it sounds like you want to circumvent the problem of "recruiting" active users and attracting askers by expanding the scope. But the scope already covers most of what you've suggested, like @SztupY suggested.
Answer: I'm not sure about the renaming of the site, but it might be worth discussing what we could add to our "what's on-topic" examples to explicitly cover the points mentioned by you. Let's do a breakdown though:

Questions about the transnational nature of the lifestyle of a person who has lived in (or lives in) more than one country - This is probably part of "Psychological and physical effects of the immigration to a different country", although it could be modified to say country/countries.
Questions concerning multiple citizenship - This is definitely on-topic and we already have a handful questions about this
Questions relating to unions (marriages, etc.) between citizens of different countries - This is one of the examples which is not fully on-topic based on our examples (as questions about short term marriage visas are more aimed for Travel than Expats), but most questions in this regard are definitely on-topic already.
Questions concerning the life with no citizenship, unclear citizenship, or a change of citizenship - This is also something we already have a few questions, and is generally on-topic already, as it's part of "living in a country as a non-citizen", but could make it more explicitly on-topic with some additional examples in our help section.
Comment: Thank you for your thoughts. I guess specifically I wanted to address cases where a person hasn't necessarily left their home country, but some transnational aspect of life has appeared anyway. The point of my question is the inverse of https://expatriates.meta.stackexchange.com/questions/284/expatriates-sucks-because-most-questions-are-about-immigration - that is, embracing specifically the topics of immigration
Answer: I'd support this proposal. My (very limited) experience of expatriates.SE so far is that nearly every question is about visas and immigration, and out of the two non-visa-related questions that I tried to ask, on one I was told "go ask on law.SE" and on the other there was total silence until I answered it myself.
At the moment this does not seem to be a useful resource for anything but immigration questions. Despite being a new expat, I've pretty much given up looking here.
Comment: @Douglas Held Do questions automatically get migrated or is the OP expected to do it? I think the former would be more helpful if it's not already the norm.


Probit: Stata log likelihood iteration 0

Question: When working with probit models in stata the first line of the output is (for a sample of 583 with 3 variables):
Iteration 0:   log likelihood = -400.01203
If I understand this correctly the iteration 0 is the log likelihood when the parameter for my 3 variables = 0.
The log likelihood function I'm working from is:
\begin{equation}
\ln L(\boldsymbol{\alpha}, \beta|\boldsymbol{y,z,t}) = \sum\limits_{j=1}^T I_j ln[\Phi(\frac{\boldsymbol{\alpha}\boldsymbol{z_j}}{\sigma} - \frac{\beta t_j}\sigma)] + (1 - i_j) ln[1 - \Phi(\frac{\boldsymbol{\alpha}\boldsymbol{z_j}}{\sigma} - \frac{\beta t_j}\sigma)]
\end{equation}
where $T =$ total # of observations and $\boldsymbol{\alpha}$ and $\beta$ are my parameters
I assumed that by setting my parameters from $\boldsymbol{\alpha}$ and $\beta$ to 0 would be the same as setting my $\Phi$ to 0 and hence the value per observation to 0.5. this would then be multiplied by my number of observations, $\ T$:
ln L["# of observations" * ln(0.5)]
I.e. 583 * ln(0.5) = -404.1048
However, as we see, I was wrong. I assume this has to do with a constant being estimated for each parameter that changes the probability slightly (roughly to 50.35%). Where is this constant coming from? what kind of estimation is being done with my parameters?
Answer: By running the example below, you can see that the constant is based on the mean of the dependent variable transformed to the probit metric.
<code>// get some example data
sysuse auto, clear

// estimate a probit model, use the trace
// option to see the estimates at each iteration
probit foreign price mpg, trace

// compute the mean on the estimation sample
sum foreign if e(sample)

// transform that mean to the probit metric
di invnormal(r(mean))

// compute the log likelihood
gen double ll = cond(foreign,         ///
                     ln(r(mean)),     /// ll for foreing cars
                     ln((1-r(mean)))) /// ll for domestic cars
                     if e(sample)

// compute the sum
sum ll, meanonly
di r(sum)
</code>
Comment: thanks a lot, that helped to understand part of the puzzle. however, what I don't fully understand is how to go from the iteration 0 constant of -.5321897 to the log-likelihood value for iteration 0 of -45.03321. I thought that would be # of observations * ln(cdfnormal(constant))?
Comment: I edited the answer


View Pager tabs in middle of fragment

Question: How do i implement these tabs, the same as in Google plus profile page?
I think I can't use ActionBar tabs as they are clinged to ActionBar. I want the tabs to be below an image like in this image. Also i want these tabs to be fixed. I want three tabs and all three tabs should be visible on screen - as the android fixed tabs plus swipe layout which uses Action Bar tabs though.
Answer: Have a look at Jake Wharton's excellent Android ViewPagerIndicator library.
This sample gives you what you are after.
Comment: dont know why this was downvoted because it is exactly what the asker was looking for
Answer: i have been looking for something like that, but the closest solution i have found is this:
Google+ profile like scrolling Effect
Hope it helps.

View Pager tabs in middle of fragment


Need to open and read a .bin file in Python. Getting error: utf-8' codec can't decode byte 0x81 in position 11: invalid start byte

Question: I am trying to read and convert binary into text that anyone could read. I am having trouble with the error message:
'utf-8' codec can't decode byte 0x81 in position 11: invalid start byte
I have gone throughout: Reading binary file and looping over each byte
trying multiple versions of trying to open and read the binary file in some way. After reading about this error message, most people either had trouble with .cvs files, or had to change the utf-8 to -16. But reading up on https://en.wikipedia.org/wiki/UTF-16#Byte_order_encoding_schemes , Python does not use -16 anymore.
Also, if I add encoding = utf-16/32, the error states: binary mode doesn't take an encoding argument
Here is my code:

with open(b"P:\Projects\2018\1809-0068-R\Bin_Files\snap-pac-eb1-R10.0d.bin", "rb") as f:
    byte = f.read(1)
    while byte != b"":
        byte = f.read(1)
print(f)

I am expecting to be able to read and write to the binary file. I would like to translate it to Hex and then to text (or to legible text somehow), but I think I have to go through this step before. If anyone could help with what I am missing, that would be greatly appreciated! Any way to open and read a binary file would be accepted. Thank you for your time!
Comment: I don't know how to attach a file, but if you go to:

https://www.opto22.com/support/resources-tools/downloads/snap_pac_firmware-zip

you can download the .zip and unzip and it is the .bin file:
```
snap-pac-eb1-R10.0d.bin
```
Comment: Would you be able to share with us the binary file you are trying to read?
Comment: Thx. Do you know what is encoded in that file? (ie what we are supposed to get.)
Answer: I am not sure but this might help:
import binascii

with open('snap-pac-eb1-R10.0d.bin', 'rb') as f:
    header = f.read(6)

b = bytearray(header)

binary=[bin(i)[2:].zfill(8) for i in b]

n = int('0b'+''.join(binary), 2)
nn = binascii.unhexlify('%x' % n)

nnn=nn.decode("ascii")[0:-1]

result='.'.join(str(ord(c)) for c in nnn[0:-1])

print(result)

Output:

188.8.131.52


Tool to shows server workload status (ASP.NET + MS SQL)

Question: I want to provide our testers with tool to see live server load, like memory usage, processor usage, database etc via webpage.
There are a lot of tools which are installed on server for admin, But definitely I do not want to provide access to live remote desktop.
I am using Windows 2003, IIS, MS SQL Server, ASP.NET
Answer: SCOM (System Center Operations Manager), htough not really LIVE (live does not scale= - point.
Otherwise the normal performancecounters work - you can connect to them from a remote computer.
Comment: Thanks for answer. When I type server name in Connect to another computer dialog, I am having error "computer \\www.domain.com cannot be managed. the network path was not found". Is it firewall? I do not like how it says \\www.domain.com


Complex Analysis: Picard's theorem applying to cot(z)

Question: Why doesn't Picard's Theorem apply to $cot(z)$? Under my understanding, $cot(z)$ has an essential singularity at $\infty$, because there is no punctured neighborhood of $0$ in which $cot(1/z)$ is bounded. Since $cot(z)$ has this singularity, Picard's Thm implies it assumes all but possibly 1 complex value. However, there is no $z$ for which $cot(z) = i$, and no $z$ for which $cot(z) = -i$. This is seemingly a contradiction. Where did I go wrong?
Comment: $\cot$ is meromorphic (has poles), not entire and $\infty$ is a limit of poles so the singularity is not isolated; whether you call such essential or not is a matter of taste/opinion etc but Picard applies to isolated singularities not to such; incidentally (little) Picard still applies as it states that a meromorphic function in the plane cannot omit three values - for entire fucntions, $\infty$ is automatically omitted hence the usual statement there, but again $\cot$ being meromorphic takes $\infty$ so can (and does) omit two finite values
Comment: see the complete comment - little Picard still applies, just that now $\cot$ takes infinity so can omit two finite values; an entire function omits infinity so can omit only one finite value
Comment: Oh I see, thank you so much. I didn't realize it had to be isolated.
Comment: Oh wow that's an incredible insight into the problem, I never knew Picard acted like that. Thank you so much, that's amazing!
Answer: The answer is in a comment by Conrad, the singularity at $\infty$ is essential but not isolated, and Picard only applies to isolated.


Oil Patterns in Bowling

Question: I am aware that different oil patterns are used in the sport of bowling. Common oil patterns are the cheetah, chameleon, scorpion, shark, and viper.
What is the significance of oil patterns in bowling? How are strategies changed to accomodate for specific oil patterns?
Answer: Different oil patterns cause the ball to react differently.
Parts of the lane where there is more oil, the ball will hook or catch less. Parts of the lane  with less oil will cause the ball to hook more. The different patterns involve different concentrations of oil on different parts of the lane.
Strategies must change due to the way that the ball reacts. A bowler who relies a lot on his hook must change his strategy when the outsides of the lane have lots of oil, because his ball will not hook that much.
Typical bowling alleys have very consistent oil patterns, where the oil is a uniform rectangle for most of the lane's length. However at PBA events, the oil patterns get much more intricate, such as the cheetah or scorpion, to make it more challenge for the pros.

Oil Patterns in Bowling


Is $(a \bmod N) \equiv (a \bmod x) \times (a \bmod y) $ where $x,y$ prime?

Question: Can we say that for an $N$ where $N = xy$ and $x,y$ are prime
$$(a \bmod N) \equiv (a \bmod x) \times (a \bmod y) ?$$
Comment: No. $10 \mod 35 \ne (10 \mod 5) \times (10 \mod 7)$
Comment: counter example where $a$ is not congruent to 0 mod either $x$ or $y$  : use vvgiri's example, with $a = 2$ instead of $a = 10$.
Answer: $\begin{align} {\rm Hint\!:}\,\ \ \overbrace{(a+xj)}^{\large a_x}\overbrace{(a+yk)}^{\large a_y} &\,=\, a(a+\color{#c00}{xj+yk}) + xyjk\\[.2em]
&\,\equiv\, a\,\color{#0a0}{\Bbb Z}\!\!\!\pmod{\!xy},\ \ {\rm by}\ \ \color{#c00}{x\Bbb Z+y\Bbb Z} = \color{#0a0}{\Bbb Z},\ \ {\rm by\ Bezout} \end{align}$
thus $\ \begin{align}a_x&\equiv a\!\!\!\pmod{\!x}\\ a_y&\equiv a\!\!\!\pmod{\!y}\end{align}\,\Rightarrow\ a_x a_y\,$ can be $\equiv \rm\color{#0a0}{any\ multiple}$ of $\,a,\,$ mod $xy$
Answer: By the Chinese remainder theorem, the mapping $\Bbb Z_N\rightarrow\Bbb Z_x\times \Bbb Z_y$ given by $a\mod N\mapsto (a\bmod x,a\bmod y)$ is a ring isomorphism.
But your claim goes too far: $4\bmod 6 = (4\bmod 2)\cdot (4\bmod 3)$ amounts to $4 = 0\cdot 1$.


Compute the characteristic of $R = Z[j]/(2-5j),$ where $j^3 = 1$ and $j^2 \ne 1.$

Question: Compute the characteristic of $R = Z[j]/(2-5j),$ where $j^3 = 1$ and $j^2 \ne 1.$
Here i provide proof found in book. But i don't understand a lot from this proof.

$\overline x$ denotes an element in the quotient group involved.
Here we have $(2 − 5j)(2 − 5j^2) = 4 − 10(j + j^2) + 25j^3 = 4 + 10 + 25 = 39.$ Hence $39\cdot \overline 1 = \overline {39} = \overline{(2 − 5j) · (2 − 5j^2)} = \overline 0.$ Then the characteristic of $R$ is finite and divides
  $39.$ Therefore the characteristic of $R$ is $1, 3, 13$ or $39.$ Now
  let $c = \mbox{char}(R) > 0.$ Since $c \cdot 1_R$ lies in the ideal
  $(2 − 5j),$ then $c = (2 − 5j)(a + bj)$ for some $a, b, \in Z.$ Hence
  $|c|^2 = |2 − 5j|^2|a + bj|^2,$ so $$c^2 = 39(a^2 + b^2 − ab)$$ and
  therefore $39|c^2.$ The only value (among $1, 3, 13$ and $39$) for
  which it is possible is $c = 39.$ Thus $\mbox{char}(R) = 39.$

Why $j+j^2 = -1?$
Why $|2 − 5j|^2|a + bj|^2= 39(a^2 + b^2 − ab)?$
Answer: $j^3 = 1$ impies $0=j^3-1=(j-1)(j^2+j+1)$.
$j^2 \ne 1$ implies $j \ne 1$ and so $j^2+j+1=0$.
$|\cdot|^2$ here is just the complex number absolute value, $|z|^2 = z \bar z$, which implies $|zw| = |z| \, |w|$.
Using that $\bar j = j^2 = -1-j$, it is easy to compute $|a + bj|^2 = a^2 + b^2 − ab$.


What do the clavinova model numbers tell me?

Question: When shopping for a used clavinova I tried to figure out which models are a good choice for me. Doing that, I quickly wondered what the CLP-model numbers are trying to tell me.
For the most recent years the model numbers are:

2006: CLP-220, CLP-230, CLP-240, CLP-270, CLP-280, CLP-265GP, CLP-295GP
2008: CLP-320, CLP-330, CLP-340, CLP-370, CLP-380, CLP-S306, CLP-S308
2011: CLP-430, CLP-440, CLP-465GP, CLP-470, CLP-480, CLP-S406, CLP-S408
2014: CLP-525, CLP-535, CLP-545, CLP-575, CLP-585

What do these numbers stand for?
Answer: Here's how you read the model numbers for the last ten years
first digit -> year
The first digit tells you from which year the instument is.
2.. means 2005: CLP-220, CLP-230, CLP-240, CLP-270, CLP-280
3.. means 2007: CLP-320, CLP-330,...
second digit -> quality/features
The second digit tells you how high its quality is compared to the other models of that year
CLP-220 < CLP-230 < CLP-240 < CLP-270 < CLP-280
CLP-320 < CLP-330 < ...

exceptions from the rule (of course)
Careful though, some older models ar using the same first digits:
CLP-200 (1986), CLP-250 (1988), CLP-260 (1990)
CLP-300 (1986), CLP-311 (1996), CLP-360 (1990)
CLP-411 (1996)
CLP-500 (1987), CLP-511 (1996), CLP-550 (1988), CLP-555 (1997), CLP-560 (1990), CLP-570 (1989)