Android CheckedTextView - Changing checked status on click

Question: I have a listview that needs to be multiple choice (i.e each list item has a checkbox which can be checked / unchecked)
The list view is in a tabhost and is the content for teh first tab.
My set up is like so:
My tab is set up with:
TabSpec tab = tabHost.newTabSpec("Services");

tabHost.addTab(tabHost.newTabSpec("tab_test1").setIndicator("Services").setContent(new Intent(this, ServiceList.class)));

When the tab is clicked new activity ServiceList is started
ServiceList is defined as such:
    public class ServiceList extends ListActivity{
        private EscarApplication application;
        ListView listView;
         @Override
           public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.service_list);
             ServiceList.this.application = (EscarApplication) this.getApplication();
             final ListView listView = getListView();
         }

         protected void onListItemClick(ListView l, View v, int position, long id) {
            String.valueOf(id);
            Long.toString(id);
            ((CheckedTextView) v).setChecked(true);
            super.onListItemClick(l, v, position, id);
        }

         @Override
         public void onStart() {
             super.onStart();
            //Generate and display the List of visits for this day by calling the AsyncTask
             GenerateServiceList services = new GenerateServiceList();
             int id = ServiceList.this.application.getVisitId();
             services.execute(id);
             listView  = getListView();
             listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
         }

        //The AsyncTask to generate a list
         private class GenerateServiceList extends AsyncTask<Integer, String, Cursor> {
              // can use UI thread here
              protected void onPreExecute() {
              }
              // automatically done on worker thread (separate from UI thread)
              protected Cursor doInBackground(Integer...params) {
                  int client_id = params[0];
                  ServiceList.this.application.getServicesHelper().open();
                  Cursor cur = ServiceList.this.application.getServicesHelper().getPotentialVisitServices(client_id);
                  return cur;

              }
              // can use UI thread here
              protected void onPostExecute(Cursor cur){
                  startManagingCursor(cur);
                  // the desired columns to be bound
                  String[] columns = new String[] {ServicesAdapter.KEY_SERVICE};
                  // the XML defined views which the data will be bound to
                  int[] to = new int[] {R.id.display_service};
                  SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(ServiceList.this, R.layout.service_list_element, cur, columns, to);
                  // set this adapter as your ListActivity's adapter
                  ServiceList.this.setListAdapter(mAdapter);
ServiceList.this.application.getServicesHelper().close();

              }
         }
    }

So, everything works ok until I click on my list item to change the checkbox state.
The part of teh code set to handle click events is causing me problems:
 protected void onListItemClick(ListView l, View v, int position, long id) {
                String.valueOf(id);
                Long.toString(id);
                ((CheckedTextView) v).setChecked(true);
                super.onListItemClick(l, v, position, id);
            }

My understanding is that the View v passed to the onListItemClick method reperesents my list element, so I am trying to cast v as a CheckedTextView and set teh checked value to true, however this just causes my app to crash. Am I missing something simple here, or is there an easier way to do this?
Thanks
Kevin
Comment: Can you show the exact stacktrace or logcat output from the crash?
Answer: Have you debugged it to check if 'v' is null? If so, you need to use a layoutInflator to retrieve the view.
if(v == null)
{
    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = vi.inflate(R.layout.service_list, null);
}


Link formatter to display link title, image and summary

Question: I know I've seen a module that does this at some point in the past but can't seem to find it right now. In Drupal 8, I have a link field. I want to fetch that link's metadata so I can display the link's title / summary / image via a formatter (think of how facebook displays links). I don't remember if this worked server-side, storing the info in the drupal db or client-side via javascript (and for the prototype I'm putting together, I doesn't matter).
I'd appreciate any help before I decide to go and write it myself.
Comment: Here's a great answer to acheive what you want, https://stackoverflow.com/a/3711554/7576961. You can maybe create a custom field formatter that implements that code.
Answer: You can do a lot with Custom Formatters and Tokens, also check out Field Formatter if you need to wrap the link around an image field.
For most control create one yourself, there are different tutorials for that, start here: Create a custom field formatter
Comment: Of course but the heavy lifting is fetching the page and extracting the title, summary and image url from the metadata.
Comment: Ah, I misunderstood when you said link metadata, I thought you were talking about the link field content, but now it seems you mean the whole page metadata. Are you using the module https://www.drupal.org/project/metatag? Are you trying to create something like social media card for your content?


Calculus: A given sequence $(a_n)_{n=1}^{\infty}$ with the property that $a_n\cdot a_{n+1}=1$ .

Question: Calculus: A given sequence $(a_n)_{n=1}^{\infty}$ with the property that  $a_n\cdot a_{n+1}=1$ .

Find all the partial limits of the series $(a_n)_{n=1}^{\infty}$.

Given $(a_n)_{n=1}^{\infty}$ a converges. Find the limit of the series.

Attempt:
Unfortunately, I have no idea
$a_n\cdot a_{n+1}$ need to be equal to $1$
Option 1: if all the sequence equal to $1$ ?
Comment: Try writing down some examples of such sequences. It should not be too difficult. (Whenever you "have no idea," try to play with examples. That's a good rule to follow all over mathematics.)
Comment: Try to answer the second part first.
Comment: No, edit again. You should say "with the property that" $a_na_{n+1}=1$ as part of the one sentence.
Comment: What does the phrase "The sustain for..."  mean?
Comment: @WillJagy Exists for I edited.
Answer: That's pretty close to trivial isn't it?  Let $a_1= a$.  Then $a_1\cdot a_2= a(a_2)= 1$ so $a_2= \frac{1}{a}$ $a_2\cdot a_3= \left(\frac{1}{a}\right)a_3= 1$ so $a_3= a$.
$a_3\cdot a_4= \left(a\right)a_4= 1$ so $a_4= \frac{1}{a}$.
See a pattern? You can conjecture that $a_n= a_1$ for n odd and $a_n= \frac{1}{a_1}$ for n even.  Can you prove that for all n?
Comment: thanks, if $a_n =n$ for $n$ odd and $a_n= \frac{1}{n}$ for $n$ even we can see that $n\cdot a_n= \frac{1}{n}=1$ for all n  right? , and the partial limits of the series is $0$ and $\infty$ right?


SSD + LUKS performance comparison?

Question: Our laptops and portable drives must be LUKS encrypted.  LUKS doesn't really seem to cause much performance loss on normal laptop hard drives (7200RPM) for us.  With the crazy data rates you get with SSDs, is that still true?
I searched around a little, but I didn't see any actual comparisons between LUKS vs. non-LUKS on SSDs.
Comment: Related: https://askubuntu.com/questions/1072773/luks-terrible-write-performance-on-ssd
Answer: I'm currently seeing 150MB/s performance with my SSD and LUKS.  That is a 50% loss since without LUKS my SSD will get 300MB/s read performance.
I'm using Fedora 17 with Linux kernel 3.4.
Comment: How did you benchmark it more precisely?
Answer: Yes the CPU usage of the encryption will go up, however unless the machine is otherwise CPU bound I'd expect IO to still be the bottlenect as most current machines should be able to exceed 250MB/sec of AES (that being the SATA-3g bandwidth)
Answer: AES-NI with SSD tests are in a link below
check this:
http://dentarg.it64.com/content/luks-and-intel-aes-ni-performance-part-2
Comment: Link-only answers are discouraged.
Answer: What about processors that has no hardware acceleration on encryption... or not using AES... etc.
Speed can go down to 15MiB/s (cascade algorithms like Serpenter and Twofish)... while two SSD in Linux Software Raid 0 can get more than 950MiB/s.
Speed loose is 'huge', very 'huge'.... on my test from 993MiB/s to 13.8MiB/s.
It is a Laptop where i put two Samsumng SSD 740 EVO, one in normal 2.5 slot, the other on a caddy where the dvd was... 8GIB ram and a processor quite old without AES and only 3GHz with Dual core.
Answer: I agree with Anonymous, i had done a test on a 64GiB RAM Ryzen 7 2700x (16 threads on 8 cores) and when Rendering (CPU is max used) the I/O on the LUKs SSD drops down a lot.
Most if using not AES, using cascade (LUKS over LUKS) with algorithms that must run on the CPU while no CPU is free to be used since it is been used on Rendering.
Identical situation for fast transcoding MPEG2 (DVD VOBs) to MP4, cpu is maxed out so there is no CPU free for LUKS.
Just a tip: If we would talk about fast NVMe (3000MiB/s write) the drop down is the same, you get a few megabytes per second writes when CPU is being used intensibly.
And that tested on a 4.35GHz Ryzen 7 2700X (8 cores / 16 threads) with 64GiB RAM 3200MHz.
Another Tip: AES is broken and has back-doors, worst if it is the hardware builtin "Intel" processors AES (or also worst disk drive internal AES), to be safe do not use AES for LUKS, neither use hard disk drives (HDD and/or SSD) with built-in hardware encryption, if you do not use ATA-passwords on them any one malicious can launch a fast command (in less than 0.1s) to it and activate a ATA-password change (from empty to non empty) and on next power down your data is hijacked, you can not access that disk without that ATA-password.
Software encryption is against speed, hardware encription is not even safe.

SSD + LUKS performance comparison?


Finding sum of the power series and the sum of the series

Question: (1) Find the sum of the power series
$$\sum_{n=1}^{\infty} nx^n$$
(2) Find the sum of the series
$$\sum_{n=1}^{\infty} \frac{n}{3^n}$$
Any tips on solving the sum of series/power series?
Comment: See [polylogarithm](http://en.wikipedia.org/wiki/Polylogarithm).
Answer: You can find the second sum using the first with $x = 1/3$. As for the first, for $|x| <1$ write
$$
  \sum_{n=1}^\infty n x^n
= x \sum_{n=1}^\infty n x^{n-1}
= x \sum_{n=1}^\infty \frac{d}{dx} x^n
= x \frac{d}{dx} \sum_{n=1}^\infty x^n
= x \frac{d}{dx} \frac{x}{1-x}
= \frac{x }{(1-x)^2}.
$$
Comment: How do you know about the x=1/3?
edit: nevermind!
Comment: Since $|1/3| < 1$ you can make the substitution $x=1/3$ in the formula for $\sum_{n=1}^\infty n x^n$ found above
Comment: wait actually how do we get to 1/3 first?
Comment: So, from the above deduction you have
$$
   \sum_{n=1}^\infty \frac{n}{3^n}
=  \sum_{n=1}^\infty n (1/3)^n
= \frac{1/3}{(1-1/3)^2}
$$
You just plug the value $x=1/3$ into the above formula! :)
Comment: I get that part but how do we know to use x=1/3?
Would it just be the first term?
Comment: Because the sum $\sum_{n=1}^\infty \frac{n}{3^n}$ *is* the sum $\sum_{n=1}^\infty n x^n$ when $x=1/3$. That was the point of the *first* equality in my above comment.
Comment: Hi, could you please tell me how to find the sum $\sum_{n=0}^\infty 3x^n$? It seems related, but I'm failing to do it.
Answer: Hint: If you take the power series $1/(1-x) = \sum_{n=0}^\infty x^n$ and take the derivative term by term, what do you get? Also the second series is a special case of the first where $x = 1/3$.


Big O notation preserved under convex functions?

Question: Suppose that the random variable $X_T$ is $O_p(1)$ as $T \rightarrow \infty$, i.e. $\forall \epsilon>0$, $\exists M_\epsilon>0$ such that $\mathbb{P}(X_T>M_\epsilon)<\epsilon$ $\forall T$.
Does this imply that the random variable $\max\{0,X_T \}$ is $O_p(1)$ as $T \rightarrow \infty$?
Comment: what does it mean for a rv to be $O(\cdot)$?
Comment: I have edited the question, thanks!
Answer: For a real number $x$, $\max\{0,x\}\leqslant |x|$, hence
$$\{\max\{0,X_T\}>M\}\subset |X_T|>M\}$$
and the conclusion follows.


Equational identities of the $(+,*,0,1)$ reducts of commutative rings

Question: Consider the class of algebras of type $<2,2,0,0>$ which are the $(+,*,0,1)$ reducts of commutative rings. What is a finite basis for the identities of that class of algebras? I conjecture that

$x+0=x$
$x+y=y+x$
$(x+y)+z=x+(y+z)$
$x*1=x$
$x*y=y*x$
$(x*y)*z=x*(y*z)$
$x*(y+z)=(x*y)+(x*z)$
$x*0=0$

is sufficient. Also, if this list is indeed sufficient, is the last axiom $x*0=0$ redundant? I know most ring theory books prove it from the remaining axioms, but that is with the help of the subtraction operator. I think, in this case, it is not redundant.
Comment: Axiom 8 is not a consequence of Axioms 1-7: Let $X$ be a nonempty set and let $A={\mathcal P}(X)$ be the power set of $X$. Equip $A$ with the operations $0=1:=\emptyset$ and $x+y=x\cdot y:=x\cup y$. Then $\langle A; +,\cdot,0,1\rangle$ satisfies Axioms 1-7 and not 8.
Comment: I don't think the class of $(+,\ast,0,1)$-reducts of commutative rings is an equational class. Do you mean the variety generated by this class?
Comment: @Eran Yes, the variety generated by the class.
Answer: Axioms 1-8 are a basis for this variety. This fact follows from 2 observations:
(1) Axioms 1-8 are sufficient to put any polynomial into normal form.
(2) If two polynomials $p, q$ written in normal form agree on $\mathbb Q$, then they are identical.
Let me include a few more details. Let $\mathcal A$ be the variety axiomatized by Axioms 1-8. Let $\mathcal C$ be the variety generated by the $(+,\cdot, 0, 1)$-reducts of commutative rings. Since commutative rings satisfy Axioms 1-8, we have ${\mathcal C}\subseteq {\mathcal A}$. The goal is to show equality. For this, it suffices to show that the $\omega$-generated free algebra $\mathbf F_{\mathcal A}(x_0,x_1,\ldots)$ lies in the subvariety $\mathcal C$. There is a natural surjective homomorphism $h\colon {\mathbf F}_{\mathcal A}(x_0,x_1,\ldots)\to {\mathbf F}_{\mathcal C}(x_0,x_1,\ldots)\colon x_i\mapsto x_i$, and I want to argue that $h$ is injective, and this will complete the argument.
Stage 1. Axioms 1-8 suffice to write each element $p\in {\mathbf F}_{\mathcal A}(x_0,x_1,\ldots)$ in normal form. Here, by the normal form of $p$, I mean ``$p = 0$'' or
 p is a left-associated sum of monomials.
 each monomial is either 1 or is a left-associated product of $x_i$'s.
 within a given monomial that is a product of variables, the variables are ordered by
increasing subscript.
 within the sum of monomials, the monomials are ordered by increasing degree, and monomials of the same degree are ordered lexicographically by subscript.
Stage 2. To obtain a contradiction, assume that $h(p)=h(q)$ where the normal forms of $p$ and $q$ are different. Choose such a pair $(p,q)$ of this type such that the sum of the lengths
of the normal forms, written $|p|+|q|$, is least. Argue that neither $p$ nor $q$ can be $0$, nor can they have a constant monomial $1$, nor can they share a monomial. (If they shared any monomial, then cancelling it from both yields a pair $(p',q')$ with the same properties and shorter total length.)
Stage 3. Now $p-q$ may be considered to be a polynomial with integer coefficients. It cannot be the zero polynomial, since $p$ and $q$ share no monomial, hence there is no cancellation when we form $p-q$. Thus, $p-q$ is a nonzero integer polynomial which vanishes
on every commutative ring. To obtain a contradiction, argue by induction on the number of variables appearing that any polynomial that vanishes on $\mathbb Q$ is the zero polynomial.


NGINX como proxy reverso e cache de um servidor compartilhado externo rodando apache

Question: Estou tentando configurar uma instância brasileira na Amazon para ser proxy reverso com cache e compactação de um servidor (aceito sugestões de painéis, de preferência gratuitos, que aceitem essa configuração) nos EUA.
O objetivo é utilizar o máximo do HD brasileiro com cache do nginx e talvez usa-la como NS para diminuir a latência e tempo gasto baixando os sites.
Assim, reduzimos os custos com hospedagem sem perder em performance.
Resumindo: Eu quero fazer cache usando nginx de um servidor nos EUA que roda apache2. Todos tutoriais que encontrei estavam relacionados a rodar nginx e apache na mesma máquina e/ou funcionavam apenas para um site, sendo necessário configuração manual para cada cliente adicional.
Comment: Pessoal, essa pergunta aqui não é muito ampla, e não está tão mal explicada assim. Da sim pra manter ela aqui no SOpt.
Comment: Qual a sua dúvida?
Comment: @Yan quando a informação que vai adicionar no comentário é pertinente a pergunta, edite a pergunta e adicione nela o comentário.
Comment: @EmersonRochaLuiz, valeu pelo toque. Corrigi.
Answer: Basta encaminhar as requisições feitas para seu servidor rodando Nginx e dele passar para o servidor externo, da mesma forma que encaminharia para um apache local. Este é um exemplo de configuração (fica normalmente em /etc/nginx/conf.d/*.conf ou /etc/nginx/sites-enabled/*):
# definição do upstream: o servidor com apache que receberá as requisições
upstream apache {
    server xxx.xxx.xxx.xxx:80;
    # Onde xxx.xxx.xxx.xxx é o endereço IP público do servidor remoto
}

# definição de onde você armazenará o cache (zona)
proxy_cache_path  /var/lib/nginx/cache levels=1:2 keys_zone=apachecache:180m  max_size=500m;

# limites de tempo
proxy_connect_timeout 30;
proxy_read_timeout 120;
proxy_send_timeout 120;

# condição e tempo de armazenamento do cache
proxy_cache_valid 200 60m;

# Configuração do servidor local
server {
    listen      80;
    server_name _;

    location / {
        # zona de cache: deve ser o mesmo nome que keys_zone
        proxy_cache apachecache;

        # Esta diretriz fará com que você possa utilizar as configurações
        # de VirtualHost do apache
        proxy_set_header        Host            $host;

        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_redirect          off;
        proxy_buffering         off;

        # passa para o apache
        proxy_pass              http://apache;
   }
}

Você precisará configurar o DNS para encaminhar para o servidor do Nginx ao invés do servidor remoto com Apache. Note que há muitas outras diretrizes disponíveis para configurar o servidor. Para mais informações, consulte: http://wiki.nginx.org/HttpProxyModule
A compactação normalmente vem habilitada no bloco http do arquivo /etc/nginx/nginx.conf (diretrizes gzip*).


Understanding the output from coxph when used with pspline

Question: When I run:
res<-coxph(Surv(time_to,event)~pspline(covariate,df=3)
           ,data=a)

I get:
                              coef se(coef)      se2    Chisq   DF       p
pspline(covariate, df = 3), lin 5.42e-02 3.47e-03 3.47e-03 2.44e+02 1.00 < 2e-16
pspline(covariate, df = 3), non                            1.57e+01 2.04 0.00042

What exactly does the p-value on the second line (i.e. 0.00042) refer to (i.e. the name of the statistical test performed) and what are the implications of it being "significant"?
Comment: You can call it a chi-square test. The p-value is the probability a chi-square random variable with 2.04 d.f. is larger than 1.57e+01. The implication is the curve is significantly non-linear. This vignette may help [here](https://cran.r-project.org/web/packages/survival/vignettes/splines.pdf).
Answer: The vignettes of the R survival package are your friends here. With pspline() you are asking for a particular type of smoothing, with a potentially large number of regression coefficients whose magnitudes are reduced from what they would be otherwise (penalized) to provide the smoothing. Alternatives are natural splines or restricted cubic splines, which use fewer but unpenalized coefficients.
As the main survival vignette demonstrates near the end of Section 3.1, the 2 lines in the report about pspline provide a "simple check for linearity" with respect to the continuous predictor you are modeling with the spline. The first line is the linear ("lin")* part of the association in the spline model. That's clearly significant in your case.
The second line ("non") reports the significance of the nonlinear aspects of the fit. This provides a compact report on the multiple coefficients associated with the penalized spline fit. Technically, this is a Wald-type test, using the variance-covariance matrix of the coefficient estimates to determine if any of the nonlinear coefficient estimates differs significantly from 0. In your case, the low p-value indicates significant nonlinearity in the relationship between your covariate and log-hazard.
As a comment on the question indicates, the splines vignette in the survival package provides additional guidance for using this type of fit.
The details of how this is done are a bit tricky as, for example, you have non-integer degrees of freedom for the test and there are different estimates available for the variance-covariance matrix, leading to the two sets of coefficient standard errors reported, se(coef) and se2 (apparently identical in your case, but different in general). If you want to get into those details, see Therneau and Grambsch, which explains the basis for the survival package (then written for the S-Plus language, an older relative of R). Section 5.5 introduces penalized splines, and Section 5.8 shows how penalized models are fit in general, with details on penalized splines in Section 5.8.3.

If the name of the predictor had been shorter, you would see more of the full words "linear" and "nonlinear" displayed in the printout.


Does AWS SSM agent cloudwatch resource:memory event?

Question: Am picking ECS optimised instance(ami-05958d7635caa4d04) in data plane of  ECS in ca-central-1 region.
AWS Systems Manager Agent (SSM Agent) is Amazon software that can be installed and configured on an Amazon EC2 instance, an on-premises server, or a virtual machine (VM). SSM Agent makes it possible for Systems Manager to update, manage, and configure these resources.

In my scenario, Launching a ECS task in ECS optimised instance(ami-05958d7635caa4d04), causes resource:memory error. More on this error, here. Monitoring ECS->cluster->service->events will not work for me, because cloudformation roll back the cluster.

My existing ECS optimised instance is launched as shown below:
"EC2Instance":{
            "Type": "AWS::EC2::Instance",
            "Properties":{
                "ImageId": "ami-05958d7635caa4d04",
                "InstanceType": "t2.micro",
                "SubnetId": { "Ref": "SubnetId"},
                "KeyName": { "Ref": "KeyName"},
                "SecurityGroupIds": [ { "Ref": "EC2InstanceSecurityGroup"} ],
                "IamInstanceProfile": { "Ref" : "EC2InstanceProfile"},
                "UserData":{
                    "Fn::Base64": { "Fn::Join": ["", [
                        "#!/bin/bash\n",
                        "echo ECS_CLUSTER=", { "Ref": "EcsCluster" }, " >> /etc/ecs/ecs.config\n",
                        "groupadd -g 1000 jenkins\n",
                        "useradd -u 1000 -g jenkins jenkins\n",
                        "mkdir -p /ecs/jenkins_home\n",
                        "chown -R jenkins:jenkins /ecs/jenkins_home\n"
                    ] ] }
                },
                "Tags": [ { "Key": "Name", "Value": { "Fn::Join": ["", [ { "Ref": "AWS::StackName"}, "-instance" ] ]} }]
            }
        }

1) Does aws ssm agent installation required on ECS instance(ami-05958d7635caa4d04) to retrieve such cloudwatch events(resource:memory) with aws.ssm cloudwatch event rule filter? or Does aws.ec2 cloudwatch event rule filter suffice?
2) If yes, Do I need to explicitly install SSM agent on ECS instance(ami-05958d7635caa4d04)? through CloudFormation...
Answer: You don't need to install SSM agent to monitor something such as memory usage of your instance (whether container instance or not). This is domain of CloudWatch, not SSM.
All you need to install is unified cloud watch agent and configure it accordingly. This is where SSM can help but it is not necessary and you can install it manually (or via script if you want).
If you decide to use SSM then you will need to explicitly install it. It comes preinstalled on some OSes but not on Amazon ECS-Optimized AMI - more about this.
Comment: How [cloudwatch service](https://aws.amazon.com/cloudwatch/) different from "unified cloudwatch agent"?
Comment: If you imagine CloudWatch to be clinet/server model then CloudWatch service is the "server" and `unified cloud watch agent` is the client application that is sending data from instance to the server.


How to find the smallest number with just $0$ and $1$ which is divided by a given number?

Question: Every positive integer divide some number whose representation (base $10$) contains only zeroes and ones. One can easily prove that using pigeonhole principle.
<code>Some examples:
2 -> 10
3 -> 111
4 -> 100
...
</code>
But how to find the smallest answer? Is there any way to find it efficiently?
Comment: Essentially the same as http://math.stackexchange.com/questions/164986/smallest-multiple-whose-digits-are-only-ones-and-zeros
Comment: The sequence is tabulated at http://oeis.org/A004290 --- there is a link to http://www.mathpuzzle.com/Binary.html where a simple, efficient algorithm is given.
Comment: I'm adding the Project Euler tab for those who wish to avoid spoilers.
Comment: @ErickWong: Not the same question. Apparently algorithmshark's answer seems like a *partial* answer to this.
Comment: @GerryMyerson Thanks, but I'm looking for a way to solve n is around $10^9$
Comment: I think you may find that the algorithm at that link works fine at $10^9$. @Mike, are you saying this is a project-euler problem? Can you provide a link, or at least a number?
Comment: @GerryMyerson It will requires around 4G memory...It's too large for me..
Comment: @GerryMyerson I hadn't wished to advertise which problem, but if you check the edit history, I stated it there.
Comment: @Mike, thanks. There is a policy here (insofar as m.se can be said to have a policy about anything) not to allow project-euler problems. But the PE problem is not quite the problem here, as the PE problem allows the digit 2. The PE problem was discussed at http://stackoverflow.com/questions/7291031/how-to-solve-euler-project-prblem-303-faster
Comment: This link will probably answer your question
http://math.stackexchange.com/questions/388165/how-to-find-the-smallest-number-with-just-0-and-1-which-is-divided-by-a-give
Comment: @Sayakiss: related: http://math.stackexchange.com/questions/83932/
Answer: You are given $n$, and want to find a number with only $1$s and $0$s, which is divisible by $n$. Let the smallest such number have $k$ digits. There is an obvious $O(\min(n, 2^k))$ algorithm, and I don't think there is any algorithm asymptotically better than this in the worst case.
Suppose there is a number $k$ digits long, that has digits only $0$s and $1$s. If there are $m$ positions $i_1, i_2, \dots, i_m$ which have $1$s, and the rest of the $k-m$ positions have $0$s, then the number is $10^{i_1} + 10^{i_2} + \dots + 10^{i_m}$. This you want divisible by $n$, so modulo $n$, you want
$$10^{i_1} + 10^{i_2} + \dots + 10^{i_m} \equiv 0 \mod n.$$
In other words, you want to find a set of positions $i_j$ (a subset of $\{0, 1, 2, \dots, k-1\}$) such that the sum of the corresponding values is $0$ when working modulo $n$. This reduces to the subset-sum problem / knapsack problem, which are known to be hard. This does not prove that this problem is hard (the reduction is in the wrong direction), but one would guess that this problem is similarly hard: the fact that the values come as powers of $10$ probably does not have any useful structure to exploit.
Anyway, the method:

Evaluate powers of $10$ modulo $n$ one by one.
At each stage, keep track of all the possible subset-sums achievable out of the values seen so far. Algorithmically, this is: all the previously achievable sums, along with the new value by itself, and the latest value added to each of the previously achievable subset-sums.
When you have a subset-sum with value $0$ modulo $n$, stop. You have found the smallest such number.

Some concrete examples. Take $n = 7$. The powers of $10$ are:
$$\begin{align}
10^{0} \equiv &1 \mod 7 \quad \text{possible sums: }1\\
10^{1} \equiv 10 \equiv &3 \mod 7 \quad \text{possible sums: }1, 3, 4\\
10^{2} \equiv 10\times3 \equiv 30 \equiv &2 \mod 7 \quad \text{possible sums: }1, 2, 3, 4, 5, 6\\
10^{3} \equiv 10\times2 \equiv 20 \equiv &6 \mod 7 \quad \text{STOP.}\\
\end{align}$$
You have $1 + 6 \equiv 0 \mod 7$, so the smallest number is $10^0 + 10^3 = 1001$. Note that although for concreteness I listed all possible sums at every stage, in practice, if doing it by hand, your mind automatically has recourse to several heuristics that make it unnecessary to explore all sums systematically: for instance, until the last stage, the largest possible sum possible was $1 + 3 + 2 < 7$, so we could just skip to the next stage without (yet) evaluating all possible sums.

For some special values of $n$, there are some shortcuts we can use to speed it up (though it won't help with the worst-case analysis). For instance, if $n$ is divisible by either $2$ or $5$, let $r$ be the largest power of either of them dividing it. (That is, $r$ is the largest integer such that $2^r | n$ or $5^r | n$.) Then we immediately know that the last $r$ digits in the answer must be all $0$s. (Proof by induction: The last digit cannot be $1$, so it has to be $0$. After that, dividing the answer throughout by $10$ (removing the last digit basically), we want an answer corresponding to $r-1$, etc.) So we can set the last $r$ digits to $0$ (which gives divisibility by $10^r$ and therefore by both $2^r$ and $5^r$), and concentrate on the rest of the digits, for the "rest of" $n$. For example, when $n = 2 \times 5^2 \times 3^3 = 1350$, we can see that the last two digits must be $0$, and then it's a problem for just the $3^3 = 27$ part of $n$. The answer for $27$ is $1101111111$, so if we have precomputed the answer for $27$, we can directly say that the answer for $1350$ is $110111111100$, but just to show the work:
$$\begin{align}
10^2 \equiv 100 \equiv 19 &\mod 27 \quad \text{sums: }19\\
10^3 \equiv 10\times19 \equiv 190 \equiv \phantom{0}1 &\mod 27 \quad \text{sums: }19, \quad 1, 20\\
10^4 \equiv 10\times 1 \equiv 10 &\mod 27 \quad \text{sums: }1, 19, 20, \quad 10, 11, (29\equiv)2, (30\equiv)3 \\
10^5 \equiv 19 &\mod 27 \quad \text{sums: }1, 2, 3, 10, 11, 19, 20, \quad 21, 22, (39\equiv)12\\
10^6 \equiv 1 &\mod 27 \quad \text{sums: }1, 2, 3, 10, 11, 12, 19, 20, 21, 22, \quad 4, 13, 23\\
10^7 \equiv 10 &\mod 27 \quad \text{sums: }1..4, 10..13, 19..23, \quad 14, 5, 6 \\
10^8 \equiv 19 &\mod 27 \quad \text{sums: }1..6, 10..14, 19..23, \quad 24, 25, 15 \\
10^9 \equiv 1 &\mod 27 \quad \text{sums: }1..6, 10..15, 19..25, \quad 7, 16, 26
\\
10^{10}\equiv 10&\mod 27 \quad \text{sums: }1..7, 10..16, 19..26, \quad 17, 8, 9\\
10^{11}\equiv 19&\mod 27 \quad \text{sums: }1..9, 10..17, 19..26, \quad \color{red}{0}, 1, 18 \quad \text{STOP}.
\end{align}$$
So, modulo $27$, we have (retracing the path by which we reached $0$, which if we were doing by computer we would store while generating the sums):
$$\begin{align}
0
&\equiv 10^{11} + 8 \\
&\equiv 10^{11} + 10^{10} + 25 \\
&\equiv 10^{11} + 10^{10} + 10^{8} + 6\\
&\equiv 10^{11} + 10^{10} + 10^{8} + 10^7 + 23 \\
& \dots \\
&\equiv 10^{11} + 10^{10} + 10^{8} + 10^7 + 10^6 + 10^5 + 10^4 + 10^3 + 10^2\\
\end{align}$$
so the smallest number made of only $0$ and $1$s that is divisible by $1350$, is $$110111111100 = 1350 \times 81563786.$$
Comment: But someone can solve it with n<=10^9 in 3 seconds...See(It's a Chinese page):http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1111
Comment: I am sure, this algo can be optimized to $O(n\cdot\phi(n))$, because it isn't the real subset-sum problem. So if the input is good enough, this will work pretty fast.
Comment: Dang. this is a nice answer. +1
Comment: It might be worth pointing out that subset-sum is a number problem, and so is NP-complete in a possibly weaker way than some NP-complete problems.  The best algorithms for subset-sum are exponential in the length of the input, but the input is a list of numbers, and the algorithms are polynomial in the *size of the numbers*, so if you are not looking for multiples of ridiculously large numbers, they may perform quite adequately.
Comment: Probably this algorithm is amenable to a standard time-space tradeoff where one precomputes a lookup table of size $O(\sqrt{n})$ to reduce the effective search time by about the same factor.
Comment: I don't see how what you describe is `O(n)`. You describe: "Evaluate powers of 10 modulo n one by one. At each stage, keep track of all the possible subset-sums..." To be `O(n)`, the "each stage" part would have to be `O(1)` but clearly isn't. If you have an actual `O(n)` algorithm, I'd love to hear it.
Comment: @גלעדברקן Hmm, I think you're right that it's not clear (to me now, anyway). But it's also not clear that it's wrong. The number of stages is not $n$. There can be at most $n$ distinct remainders that you track. I'll try to do a precise analysis later, but I can't see any obvious reason why this algorithm definitely cannot be $O(n)$.
Comment: Typically complexity for algorithms are evaluated at worst case.
Comment: @גלעדברקן You're right of course. Here, for *most* $n$, powers of 10 behave as if "random" mod $n$: there are $\sim \lg n$ stages, giving a running time of $O(n)$. However, for the worst $n$, which seem to be usually ones that are multiples of more than one of $99$, $999$ etc., the running time seems to grow super-linearly: empirically, the worst I encountered after trying all $n$ up to ~2 million was something like $18n$. It is not obvious to me whether in general in the worst case this is still $O(n)$ with a large constant, or something like $O(n \log n)$, or $O(n^2)$ with a tiny constant...
Comment: @גלעדברקן I'm still trying to make sense of these numbers (maybe it can be a separate question on this or another site), but in any case, consider the claim of $O(n)$ in the answer retracted. Let me know if you have any further insights.
Comment: Cool. It may actually be O(n), it just doesn't seem easy for me to see.


mxKeyHandler not working when React component is remounted

Question: I'm using mxGraph in a React Single Page Application and instantiating a new mxKeyHandler in componentDidMount. I also invoke its destroy method in componentWillUnmount. It all works fine the first time you visit the page that has the mxGraph component, but if I navigate away to a different page and come back (without a page refresh), it no longer works.
componentDidMount() {
    this.editor = new mxEditor();
    this.editor.setGraphContainer(this.mxGraphRef.current);
    ......
    this.keyHandler = new mxKeyHandler(this.editor.graph);
    this.keyHandler.bindKey(46, evt => {
      this.editor.graph.removeCells();
    });
}

componentWillUnmount() {
    this.keyHandler.destroy();
    this.editor.destroy();
}

Any advice as to what I'm doing wrong?
Answer: Ok, the solution was to add the following 2 lines:
this.editor.graph.container.setAttribute('tabindex', '-1');
this.editor.graph.container.focus();

As described here: https://jgraph.github.io/mxgraph/docs/known-issues.html#Focus


Intuition behind unbiased OLS estimator derivation

Question: I was going through the derivation of unbiased OLS estimator
$$E(\hat{\beta_1}) = \beta_1 + (1/SST_x)  \sum_{i=1}^n d_i E(u_i) = \beta_1 + (1/SST_x) \sum_{i=1}^n d_i\cdot 0 = \beta_1$$
My doubt is if $$ d_i = x_i - \bar{x} $$ and assuming Gauss-Markov theorem is true that $x_i$ is independent of $u_i$.
Then how, $$ (1/SST_x) E(\sum_{i=1}^n d_i u_i) = (1/SST_x)  \sum_{i=1}^n d_i E(u_i)  $$
how can we treat $d_i$ as constant and we didn't take expectation of $d_i$. What is the intuition behind this?
Comment: Your equations seem to be for simple linear regression, instead of the more general ordinary least squares method.
Comment: Yes. I just want to understand from what intuition $d_i = x_i - \bar{x}$ is considered constant in this equation.
Answer: You can see this as the expectation being unbiased conditional on $x$, in which case the $d_i$ can be considered constants.
(and since the conditional expectation is zero, so will be the unconditional expectation)

The derivation in your question is only correct for constant $x$.
But it happens to be the same result for variable $x$, because the expectation conditional on $x$ is zero independent of it's value. (but for this extra step, from constant to conditional, the derivation is indeed not complete)

See here an intuitive plot how the marginal distribution of $y$ can be considered as a sum of the distributions of $y$ conditional on $x_1$.

The data here has been generated according to some silly/fancy model:
$$\begin{array}{}
 x_1 &\sim& N(\mu = 10, \sigma = 1) \\
y \vert x_1  &\sim & N(\mu = 0, \sigma = 2+ \sin(5x_1))
\end{array}$$
You can consider the distribution of $y$ as a sum of the distributions/components of $y$ conditional on $x$. When each of these components has a mean equal to zero, then the sum of them will also be zero.
<code>### settings for layout and computation
set.seed(1)
layout(matrix(1:2,1), widths = c(2,1))
par(mar= c(4,4,2,1), mgp = c(2.5,0.8,0))

### generate data
x <- rnorm(10^4,10,1)
y <- rnorm(10^4,0,2+sin(x*5))

### conditional colouring
col <- hsv(x/14,1,1,0.1)

### scatterplot
plot(x,y, col = col, bg = col, pch = 21, cex = 0.7, ylim = c(-10,10), main = "scatter plot \n", cex.main = 1,
     xlab = expression(x[1]))

### density plots for different conditions x
plot(-100,-100, ylim = c(-10,10), xlim = c(0,220),
     xlab = "", ylab = "", main = "conditional \n distribution", cex.main = 1)

for (xs in seq(7,13,0.4)) {
  sel = ((x>xs-0.2)*(x<xs+0.2))
  h <- hist(y[sel==1], breaks = seq(-15,15,0.4), plot = FALSE)
  lines(h$counts,h$mids, col = hsv(xs/14,1,1,0.5), lwd = 2)
}
</code>
Comment: how $d_i$ is constant for given x? I know that $\sum_{i=1}^nd_i$ is zero. But here it is $\sum_{i=1}^n d_i u_i$. So, how $E[\sum_{i=1}^n d_i u_i | X]$ makes d_i constant? Is there any proof or explanation that makes $d_i$ constant for given x?
Comment: *how $d_i$ is constant for given $x$?* Because $d_i$ is a function of $x$. If $x$ is constant, then $d_i$ is constant.
Comment: $d_i$ = $x_i - \bar{x} $ where $\bar{x}$ is mean of X. Thus, $\sum_{i=1}^n (x_i - \bar{x}) = 0$, all $x_i $ are independent and identically distributed.
Comment: When $x$ is constant, I mean that all $x_i$ are constant. It might be that you consider a case with varying $x_i$, but for a *conditional* expression you can consider the $x_i$ and $d_i$ to be constant. To go from constant $x_i$ to non constant $x_i$ you can use: $$E(a) = \sum_{\forall b} E(a | b) p(b)$$ So if the conditional expressions are zero (for constant x) then so will be the marginal expression (for varying x).
Comment: So the trick for varying x is to compute the expectation of beta for each individual (constant) x, and then get the overall expectation by summing/integrating over all the possible states of x (and weighing their probability). This sum is easy because each conditional term is zero.
Comment: Got it. For varying x, it is like law of iterated expectations. It's just that for a given x, considering $x_i$ and $d_i$ as constant seems less intuitive to me.
Comment: So you are right you can not go from $E(\sum d_i u_i)$ to $E(d_i \sum u_i)$ if the $d_i$ are not constant. Still for not constant $d_i$ or $x_i$ you may still get the same result, but that is only because the conditional expectations are zero. It is not true in general. **Thus, the derivation in your question is only correct for constant $x$, but because the expectation is zero it happens to give the same result as variable $x$ (but this requires an extra derivation step).**
Comment: @MayankMittal, It's just that for a given x, considering xi and di as constant seems less intuitive to me. Imagine a joint distribution of $\hat\beta_1-\beta_1$ and $x = \lbrace x_1, x_2, \dots , x_n$ as a multidimensional cloud. Getting the marginal distribution of $\hat\beta_1-\beta_1$ at once is maybe not so intuitive, because the varying $x$, but could you describe an intersection? For such intersection you describe the distribution/density of $\hat\beta_1-\beta$ while keeping other values *fixed*.
Comment: Interestingly, with the example that I made (in the edit) I realize now that the expectation of the marginal distribution might actually be undefined. For instance take $$\begin{array}{}
 x &\sim& N(\mu = 0, \sigma = 1) \\
y \vert x  &\sim & N(\mu = 0, \sigma = \vert 1/x \vert )
\end{array}$$

then $y$ is a Cauchy distributed variable, which has no defined mean/expectation. Yet, the distribution of $y \vert x$ is defined almost everywhere. So $E(y \vert x)$ needs to be defined everywhere or otherwise it can get tricky.


sample variance divided by variance is Chi Square

Question: To estimate the sample variance, the following relation is often used:
$$\frac{(n-1)s^2}{\sigma^2} \sim \chi^2(n-1) $$
With $(n-1)$ being the degrees of freedom.
Could someone provide me a formal proof and some intuition for this relation?
Comment: I think you mean $\large{\chi^2_{n-1}}$, with $n-1$ as a subscript.
Comment: Also I think $\sigma \to \sigma^2$. I believe this will help: https://stats.stackexchange.com/questions/100861/why-use-chi-squared-distribution-to-estimate-the-variance-of-normal-distribution
Comment: If $\mu$ is known, not estimated by $\bar X,$ then $\frac{nV}{\sigma^2} = \sim\mathsf{Chisq}(n),$ where $V = \frac{1}{n}\sum_{i=1}^n (X_i - \mu)^2.$ This follows directly from $\sum_{i=1}^n Z_i^2 \sim \mathsf{Chisq}(n),$ for $Z_i$ iid standard normals. For your expression, one says informally that a 'degree of freedom is lost' in the estimation of $\mu$ by $\bar X.$ // One formal proof uses an orthogonal transformation of a multivariate normal, so that one marginal 'respresents' $\bar X$ and the remaining $n-1$ represent $S^2.$
Answer: Simulation in R: Population mean estimated by sample mean.
set.seed(2021)
n = 5;  mu = 100;  sg = 15
q4 = replicate(n^5, 4*var(rnorm(n,mu,sg))/sg^2)
mean(q4)
[1] 4.000032  # aprx mean of CHISQ(4) = 4.

In the figure below the density function of $\mathsf{Chisq}(\nu=4)$ fits the simulated values, while the
density function of $\mathsf{Chisq}(\nu=5)$ does not.

hdr = "Mean estimated: CHISQ(4)"
hist(q4, prob=T, br=20, ylim=c(0,.25), col="skyblue2", main=hdr)
 curve(dchisq(x,4), add=T, lwd=2)
 curve(dchisq(x,5), add=T, lwd=2, col="red", lty="dotted")

Population mean known:
set.seed(320)
n = 5;  mu = 100;  sg = 15
q5 = replicate(n^5, sum((rnorm(n,mu,sg)-mu)^2)/sg^2)
mean(q5)
[1] 5.109602  # aprx mean of CHISQ(5) = 5

hdr = "Mean known: CHISQ(5)"
hist(q5, prob=T, br=20, ylim=c(0,.20), col="skyblue2", main=hdr)
 curve(dchisq(x,5), add=T, lwd=2, col="red", lty="dotted")


Proving the irrationality of the number $\cos\frac\pi9$

Question: The question is if there exists a standard way of proving that
$$ \cos{ π \over 9}$$ is an irrational number.
Comment: What do you mean with a "standard way" ? It is possible to determine the minimal polynomial of $\cos(\frac{\pi}{9})$, but this is not quite easy.
Comment: i know how to find the minimal polynomial of this. But he said i could do it much easier.(like the way you prove that sqrt(2)is irrational)
Answer: Hint: Use $\frac{1}{2}=\cos\frac{\pi}{3}=4\cos^3\frac{\pi}{9}-3\cos\frac{\pi}{9}$, then prove that $4x^3-3x=\frac{1}{2}$ does not have rational roots
Comment: I would add that the general result is called [Niven's theorem](https://en.wikipedia.org/wiki/Niven%27s_theorem) ([proof here](https://proofwiki.org/wiki/Niven%27s_Theorem))
Comment: Clear and concise.  +1
Answer: Observe that $\cos(\frac{\pi}{3}) = \dfrac{1}{2}$. Thus using the identity: $\cos (3x) = 4\cos^3 x - 3\cos x$, $\cos(\frac{\pi}{9})$ is a zero of the equation: $4x^3 - 3x = \dfrac{1}{2}$,and using the irrational root test, one concludes that this equation has no rational root, hence $\cos(\frac{\pi}{9})$ must be irrational.
Answer: Using either trig identities, or plug-and-simplify of $\frac{1}{2} (e^{\pi i/9}+e^{-\pi i /9})$, we verify that $\cos\frac{\pi}{9}$ is a root of $f(x)=8x^3-6x-1$.  By the Rational Root Theorem, the only possible rational roots of this polynomial are $\pm 1, \pm \frac{1}{2}, \pm \frac{1}{4},\pm \frac{1}{8}$, and none of these eight are correct.
Comment: I got it through a lucky guess, i.e. wolfram alpha.
Comment: And how do we get the minimal polynomial ? You should work this out.
Answer: Using Galois theory: Let $n\in\mathbb N$. If $2\cos(2\pi/n)=e^{2\pi i/n}+e^{-2\pi i/n}$ were rational, it would be fixed under the automorphism of $\mathbb Q[e^{2\pi i/n}]$ that sends $e^{2\pi i/n}$ to $e^{k2\pi i/n}$ for $\gcd(k,n)=1$, i.e., we would have
$$\cos(2\pi/n)=\cos(2k\pi/n)$$
if $\gcd(k,n)=1$. If $n>4$ there are such angles with different cosine (to be seen e.g. by drawing a unit circle, or take $k=2$ or $n/2-1$ according to as $n$ is odd or even).
We conclude that $\cos(2\pi/n)$ is irrational for $n>4$.


Criar .jar de um projeto com vários pacotes, sem IDE e na linha de comando

Question: Ao fazer uma pesquisa pelos sites do Stack Overflow, percebi que todos os tópicos só ensinam como criar o arquivo .jar de uma única classe ou então, até ensinavam a criar o .jar de um pacote inteiro, porém sem a finalidade de ser um .jar executável, mas sim uma biblioteca.
A minha necessidade aqui é diferente, eu preciso criar o .jar de um projeto com vários pacotes na linha de comando e esse .jar tem que ser diretamente executável. Criei uma imagem para ilustrar o meu problema, é só seguir a numeração na foto. Eu também descrevo cada etapa logo depois da imagem:

A pasta raiz do projeto tem o caminho C:\test
Etapas:
1 - Mostro o código do arquivo test.java que, em relação ao diretório raiz do projeto, tem caminho test\src\pack1\test.java
package pack1;

import pack2.*;

public class test
{
    public static void main(String[] args)
    {
        System.out.println(test2.s);
    }
}

2 - Mostro o código do arquivo test2.java que, em relação ao diretório raiz do projeto, tem caminho test\src\pack2\test2.java
package pack2;

public class test2
{
    public static final String s = "20/20";
}

3 - Mostro visualmente a estrutura do projeto pelo comando tree /f
4 - Compilo os arquivos .java jogando-os na pasta bin com o comando javac -d bin -cp src src\pack1\test.java
5 - Mostro visualmente a estrutura do projeto pelo comando tree /f
6 - Crio o arquivo .jar com o comando jar --verbose --create --file test.jar --main-class pack1.test bin
7 - Mostro visualmente a estrutura do projeto pelo comando tree /f
8 - Tento executar o .jar com o comando java -jar test.jar e recebo:
Erro: Não foi possível localizar nem carregar a classe principal pack1.test
Causada por: java.lang.ClassNotFoundException: pack1.test

Quando eu faço esse mesmo processo com apenas um .class, eu consigo executar o .jar sem nenhum problema.
Answer: Descobri o problema depois de analisar cuidadosamente a solução deste link. Pelo que vi, o grande problema foi ter adicionado a pasta bin dentro do .jar.
Há duas formas mais simples, na linha de comando, de chegar no resultado que eu queria:
Formas simples na linha de comando:
Substituir o comando da etapa 6 por:
Forma 1:
cd bin && jar --verbose --create --file ..\test.jar --main-class pack1.test . && cd ..
ou por:
Forma 2:
jar --verbose --create --file test.jar --main-class pack1.test -C bin .
Sendo assim, na hora de criar o .jar, o que vai para dentro dele são apenas as pastas que estão dentro da pasta bin, ou seja, as pastas dos pacotes pack1 e pack2. A opção -C do comando jar faz uma mudança temporária de diretório.

Outra forma na linha de comando:
Criar o arquivo manifest.mf com as seguintes linhas:
Main-Class: pack1.test
Class-Path: bin/

É importante ressaltar que o Class-Path não funciona se não tiver a barra sobrando depois do nome do diretório, observe que eu escrevi bin/ e não bin. Além disso, o Class-Path do manifest.mf é relativo a onde ele estiver, ou seja, nesse caso eu criei o arquivo manifest.mf na pasta raiz do projeto e por isso eu pude colocar bin/.
Além disso, o comando da etapa 6 passa a ser:
jar --verbose --create --file test.jar --manifest manifest.mf bin
Answer: jar cvfm nomedoarquivo.jar MANIFEST.MF -C pastaProjeto/ .

Para mim deu certo desta forma. baseada na forma explicada pelo axell-brendow

-c ou --create - Cria o arquivo compactado
-v ou --verbose - Gera saida detalhada no terminal.
-f ou --file - O nome do arquivo .jar
-m ou --manifest - Inclui o manifesto.


How to split a dataframe column into more columns, conditional to another column value?

Question: I am stuck because I can not split a dataframe column into more columns, conditional to another column value. I have a pandas dataframe which I generated straight from a '.csv' file with more than 100K rows.
Excerpt1:

I want to split column dca by ',' (comma) into more columns. The number of splits will be constrained by the values in n_mppts.
Edited on 2023-04-12:
I could succesfully perform the split column operation in the dataframe generated from this .csv file with the following code (thanks to @Abdulmajeed's solution):
def split_dca(row):
    values = row['dca'].split(',') if row['dca'] else []
    values += [float('NaN')] * (row['n_mppts'] - len(values))
    values = values[:row['n_mppts']]
    return pd.Series(values)

df_dca_dcv.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 418643 entries, 0 to 418642
Data columns (total 5 columns):
 #   Column       Non-Null Count   Dtype
---  ------       --------------   -----
 0   pipe_id      418643 non-null  int64
 1   date         418643 non-null  object
 2   inverter_id  418643 non-null  object
 3   n_mppts      418643 non-null  int64
 4   dca          418538 non-null  object
 5   dcv          418538 non-null  object
dtypes: int64(2), object(4)
memory usage: 19.2+ MB

df_dca_dcv['dca'] = df_dca_dcv['dca'].str.replace('{', '')
df_dca_dcv['dca'] = df_dca_dcv['dca'].str.replace('}', '')
df_dca_dcv['dca'] = df_dca_dcv['dca'].astype(str)

Excerpt2:

mppts_dca = df_dca_dcv.apply(split_dca, axis=1)
mppts_dca['dca_mppt_0'] = pd.to_numeric(mppts_dca[0], errors='coerce')
mppts_dca['dca_mppt_1'] = pd.to_numeric(mppts_dca[1], errors='coerce')
mppts_dca['dca_mppt_2'] = pd.to_numeric(mppts_dca[2], errors='coerce')
mppts_dca['dca_mppt_3'] = pd.to_numeric(mppts_dca[3], errors='coerce')
mppts_dca['dca_mppt_4'] = pd.to_numeric(mppts_dca[4], errors='coerce')
mppts_dca['dca_mppt_5'] = pd.to_numeric(mppts_dca[5], errors='coerce')
mppts_dca['dca_mppt_6'] = pd.to_numeric(mppts_dca[6], errors='coerce')
mppts_dca['dca_mppt_7'] = pd.to_numeric(mppts_dca[7], errors='coerce')
mppts_dca['dca_mppt_8'] = pd.to_numeric(mppts_dca[8], errors='coerce')
df_dca_dcv = pd.concat([df_dca_dcv, mppts_dca], axis=1)

Excerpt3:

However, I am facing a problem when I generate the dataframe from a pandas sql query specifying inverter_id=a2 and therefore the current solution wont succeed (the issue also persists with other inverter_id values):
df_dca_dcv = pd.read_sql_query("select pipe_id,created_at as date,inverter_id,n_mppts,dca,dcv from inverters where inverter_id = 'a2' order by pipe_id, inverter_id, date;", con=con) # connected to a postgreSQL db
df_dca_dcv.info()

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 16507 entries, 0 to 16506
Data columns (total 6 columns):
 #   Column       Non-Null Count  Dtype
---  ------       --------------  -----
 0   pipe_id      16507 non-null  object
 1   date         16507 non-null  datetime64[ns]
 2   inverter_id  16507 non-null  object
 3   n_mppts      16507 non-null  int64
 4   dca          16428 non-null  object
 5   dcv          16428 non-null  object
dtypes: datetime64[ns](1), int64(1), object(4)
memory usage: 773.9+ KB

Column dca Dtype is still object, but now it has values between "[ ]" instead of "{ }" (unlike in Excerpt 1), and when I perform this:
df_dca_dcv['dca'] = df_dca_dcv['dca'].str.replace('[', '')
df_dca_dcv['dca'] = df_dca_dcv['dca'].str.replace(']', '')
df_dca_dcv['dca'] = df_dca_dcv['dca'].astype(str)

I get the following error:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[6], line 2
      1 df_dca_dcv['dca'] = df_dca_dcv['dca'].str.replace("[", "")
----> 2 df_dca_dcv['dca'] = df_dca_dcv['dca'].str.replace("]", "")

File ~\Anaconda3\lib\site-packages\pandas\core\generic.py:5575, in NDFrame.__getattr__(self, name)
   5568 if (
   5569     name not in self._internal_names_set
   5570     and name not in self._metadata
   5571     and name not in self._accessors
   5572     and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5573 ):
   5574     return self[name]
-> 5575 return object.__getattribute__(self, name)

File ~\Anaconda3\lib\site-packages\pandas\core\accessor.py:182, in CachedAccessor.__get__(self, obj, cls)
    179 if obj is None:
    180     # we're accessing the attribute of the class, i.e., Dataset.geo
    181     return self._accessor
--> 182 accessor_obj = self._accessor(obj)
    183 # Replace the property with the accessor object. Inspired by:
    184 # https://www.pydanny.com/cached-property.html
    185 # We need to use object.__setattr__ because we overwrite __setattr__ on
    186 # NDFrame
    187 object.__setattr__(obj, self._name, accessor_obj)

File ~\Anaconda3\lib\site-packages\pandas\core\strings\accessor.py:177, in StringMethods.__init__(self, data)
    174 def __init__(self, data):
    175     from pandas.core.arrays.string_ import StringDtype
--> 177     self._inferred_dtype = self._validate(data)
    178     self._is_categorical = is_categorical_dtype(data.dtype)
    179     self._is_string = isinstance(data.dtype, StringDtype)

File ~\Anaconda3\lib\site-packages\pandas\core\strings\accessor.py:231, in StringMethods._validate(data)
    228 inferred_dtype = lib.infer_dtype(values, skipna=True)
    230 if inferred_dtype not in allowed_types:
--> 231     raise AttributeError("Can only use .str accessor with string values!")
    232 return inferred_dtype

AttributeError: Can only use .str accessor with string values!

I antecipated the ".astype(str)" operation, and then performed the ".str.replace(...)" operations. However, when I look at the dataframe now
Excerpt4:

column dca values are not in the same format as they are in Excerpt2 (e.g. "Decimal('2.2'),Decimal('2.2')..."). When I go ahead and execute
mppts_dca = df_dca_dcv.apply(split_dca, axis=1)
df_dca_dcv = pd.concat([df_dca_dcv, mppts_dca], axis=1)
df_dca_dcv['date'] = df_dca_dcv['date'].astype('datetime64[ns]')
df_dca_dcv['dca_mppt_0'] = pd.to_numeric(df_dca_dcv[0], errors='coerce')
df_dca_dcv['dca_mppt_1'] = pd.to_numeric(df_dca_dcv[1], errors='coerce')

the dca values are not passed to the newly splited columns, which (I suppose) is because "pd.to_numeric(" can't read "Decimal(...)":
Excerpt5:

I've tried all the following methods to convert dca column to string:
METHOD1: df_dca_dcv['dca'] = df_dca_dcv['dca'].map(str) #produced same output format as before
METHOD2: df_dca_dcv['dca'] = df_dca_dcv['dca'].apply(str) #produced same output format as before
METHOD3: df_dca_dcv['dca'] = df_dca_dcv['dca'].astype(str) #generated the following error:

ValueError                                Traceback (most recent call last)
Cell In[6], line 1
----> 1 df_dca_dcv['dca'] = df_dca_dcv['dca'].values.astype(str)
ValueError: setting an array element with a sequence

METHOD4: df_dca_dcv['dca'] = df_dca_dcv['dca'].values.astype(str) #generated same error as METHOD3

METHOD5: df_dca_dcv['dca'] = df_dca_dcv['dca'].applymap(str)  #generated the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 1
----> 1 df_dca_dcv['dca'] = df_dca_dcv['dca'].applymap(str)
File ~\Anaconda3\lib\site-packages\pandas\core\generic.py:5575, in NDFrame.__getattr__(self, name)
   5568 if (
   5569     name not in self._internal_names_set
   5570     and name not in self._metadata
   5571     and name not in self._accessors
   5572     and self._info_axis._can_hold_identifiers_and_holds_name(name)
   5573 ):
   5574     return self[name]
-> 5575 return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'applymap'

METHOD6:
def convert_float_string(row):
    float_list = row['dca']
    if len(float_list) > 0:
        string_list = ["%.2f" % i for i in float_list]
    else:
        string_list = float('NaN')
    return string_list
df_dca_dcv['dca'] = df_dca_dcv.apply(lambda row: convert_float_string(row), axis=1) #generated the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[8], line 1
----> 1 df_dca_dcv['dca'] = df_dca_dcv.apply(lambda row: convert_float_string(row), axis=1)
File ~\Anaconda3\lib\site-packages\pandas\core\frame.py:8839, in DataFrame.apply(self, func, axis, raw, result_type, args, **kwargs)
   8828 from pandas.core.apply import frame_apply
   8830 op = frame_apply(
   8831     self,
   8832     func=func,
   (...)
   8837     kwargs=kwargs,
   8838 )
-> 8839 return op.apply().__finalize__(self, method="apply")
File ~\Anaconda3\lib\site-packages\pandas\core\apply.py:727, in FrameApply.apply(self)
    724 elif self.raw:
    725     return self.apply_raw()
--> 727 return self.apply_standard()
File ~\Anaconda3\lib\site-packages\pandas\core\apply.py:851, in FrameApply.apply_standard(self)
    850 def apply_standard(self):
--> 851     results, res_index = self.apply_series_generator()
    853     # wrap results
    854     return self.wrap_results(results, res_index)
File ~\Anaconda3\lib\site-packages\pandas\core\apply.py:867, in FrameApply.apply_series_generator(self)
    864 with option_context("mode.chained_assignment", None):
    865     for i, v in enumerate(series_gen):
    866         # ignore SettingWithCopy here in case the user mutates
--> 867         results[i] = self.f(v)
    868         if isinstance(results[i], ABCSeries):
    869             # If we have a view on v, we need to make a copy because
    870             #  series_generator will swap out the underlying data
    871             results[i] = results[i].copy(deep=False)
Cell In[8], line 1, in <lambda>(row)
----> 1 df_dca_dcv['dca'] = df_dca_dcv.apply(lambda row: convert_float_string(row), axis=1)
Cell In[6], line 3, in convert_float_string(row)
      1 def convert_float_string(row):
      2     float_list = row['dca']
----> 3     if len(float_list) > 0:
      4         string_list = ["%.2f" % i for i in float_list]
      5     else:
TypeError: object of type 'NoneType' has no len()

...and if I simply skip converting dca to string and use
df_dca_dcv['dca'] = df_dca_dcv['dca'].replace("[", "")
df_dca_dcv['dca'] = df_dca_dcv['dca'].replace("]", "")

the replacement does not take place.
I would appreciate any suggestions on how to fix that issue.
Comment: After `pd.read_sql_query(...)` can you provide the exact output of `print(df_dca_dcv.head(5).to_dict())`, please?
Comment: Hi @Corralien, I've added the output you requested by the end of the main question as there isn't space to post it in the comments.
Comment: All `dca` and `dcv` are always a length of 11 elements?
Comment: @Corralien, no they are not. They can have 0 to 11 elements and the split operation should filter only the 'n' first elements from left to right, where 'n' = row['n_mppts']. Any ones beyond the 'n'th element should have value=float('NaN').
Comment: Two more question: when you have NaN in `dca` column, what is the value of `n_mppts`? Try: `df.loc[df['dca'].isna(), ['n_mppts', 'dca']]`. Are you sure the value of `n_mppts` is always lower than the length of `dca` array?
Comment: For `inverter_id`='a2', `df_dca_dcv.loc[df_dca_dcv['dca'].isna(), ['n_mppts', 'dca']]` outputs 4 rows with `n_mppts`=2 and `dca`='None'. This is because sometimes the datalogger wont send any data, so `dca` gets value 'None'. I am sure there are other cases like that for other `inverter_id`s. In those cases where `dca`='None', any column generated from the split operation should have value `float('NaN')`. Whenever the `dca` is not 'None', its length is >= `n_mppts`.
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253261/discussion-between-nigelblainey-and-corralien).
Answer: I am not sure if I understood your question correctly.
But you can use custom function with apply. Example below:
Hope it helps
def split_dca(row):
    values = row['dca'].split(',') if row['dca'] else []

    values += [float('NaN')] * (row['n_mppts'] - len(values))

    values = values[:row['n_mppts']]

    return pd.Series(values)

split_columns = df.apply(split_dca, axis=1)

Comment: Thank you @Abdulmajeed. After replacing "{" and "}" with "", and using ".astype(str)" on dca column, this function solved the issue with the .csv dataset column split, which is the main concern in my question! Would it be possible for you to provide some guidance on a secondary issue related to this split operation? When I generate the dataframe (not from a .csv file but) from a pandas sql query using "pd.read_sql_query(...)", and do the same as before, I get an error which I described in detail when I edited the main question.
Answer: Update

They can have 0 to 11 elements and the split operation should filter only the 'n' first elements from left to right, where 'n' = row['n_mppts']

Since dca has variable length, you can use this code:
# Part 0: fix special cases
mask = df['dca'].isna()
df.loc[mask, 'dca'] = df.loc[mask, 'dca'].apply(lambda x: [])
lens = df['dca'].str.len().values  # get the length of each array
n_mppts = df['n_mppts'].mask(df['n_mppts'].gt(lens), lens)

# Part 1: pad each array to be stacked
nrows, ncols = len(df), int(lens.max())
dca = np.zeros((nrows, ncols))  # create a 0s target array
mask = lens[:, None] > np.arange(ncols)
dca[mask] = np.concatenate(df['dca']).astype(float)  # copy data

# Part 2: keep values according n_mppts
mask = n_mppts.values[:, None] <= np.arange(ncols)
dca[mask] = np.nan
dca_df = pd.DataFrame(dca).add_prefix('dca_mppt_')
dca_df

Output:
   dca_mppt_0  dca_mppt_1  dca_mppt_2  dca_mppt_3  dca_mppt_4  dca_mppt_5  dca_mppt_6  dca_mppt_7  dca_mppt_8  dca_mppt_9  dca_mppt_10
0         2.3         2.3         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN
1         2.6         2.6         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN
2         2.9         2.9         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN
3         6.0         5.9         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN
4         3.9         3.9         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN

What you receive from read_sql_query is a list of Decimal instances.
It seems dca and dcv are a length of 11 items. You can use numpy to get your expected output in vectorized way:
dca = np.vstack(df['dca']).astype(float)
mask = df['n_mppts'].values[:, None] <= np.arange(12)
dca[mask] = np.nan
dca_df = pd.DataFrame(dca).add_prefix('dca_mppt_')

Output:
>>> dca_df
   dca_mppt_0  dca_mppt_1  dca_mppt_2  dca_mppt_3  dca_mppt_4  dca_mppt_5  dca_mppt_6  dca_mppt_7  dca_mppt_8  dca_mppt_9  dca_mppt_10
0         2.3         2.3         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN
1         2.6         2.6         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN
2         2.9         2.9         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN
3         6.0         5.9         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN
4         3.9         3.9         NaN         NaN         NaN         NaN         NaN         NaN         NaN         NaN          NaN

Comment: The solution is effective and solved the problem. Other users should be aware that during the implementation of 'Part 0', some `n_mppts` values may change, so if you need to summarize on that variable you should concatenate `dca_df` to a new `df`, and only then summarize it. Thank you @Corralien!
Comment: I got a ValueError: "ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 11 and the array at index 63 has size 1"
Comment: It's not a big deal to handle variable array length. I updated my answer, can you check it please?


Anonymous arrays in C++

Question: In C99+ I can use anonymous arrays and structs as compound literals, e.g.
void f(unsigned char *data);

f((char []){ 42, 15, 33 });

that is equivalently
{
    char tmp[] = { 42, 15, 33 };
    f(tmp);
}

Are there ways to do similar things in C++?
Answer: In C++11 you have std::initializer_list which allows something similar:
// Declare function
void f(std::initializer_list<int> data);

...

// Call function
f({ 1, 2, 3, 4 });

Most containers have been modified to take std::initializer_list, so you can have e.g. a std::vector instead:
// Function declaration, call as before
void f(const std::vector<int>& data);

Anonymous arrays in C++


How can I calculate whether I can form a group of 10 tournament matches from a limited set of matches/pairs?

Question: I'm trying to work out whether I can form 10 matches from a set of 98 possible player pairings (which includes 20 individuals).
Each player must play in one, and only one, of the matches.
A computational approach is fine, but there are too many possibilities to enumerate---14005614014756, to be precise, i.e. choose(98, 10).
The data for the allowable match pairs is:
<code>
Player01    Player13
Player01    Player14
Player02    Player03
Player02    Player04
Player02    Player05
Player02    Player06
Player02    Player07
Player02    Player08
Player02    Player09
Player02    Player10
Player02    Player11
Player02    Player12
Player02    Player13
Player02    Player14
Player02    Player15
Player02    Player16
Player02    Player17
Player02    Player18
Player02    Player19
Player02    Player20
Player03    Player04
Player03    Player05
Player03    Player06
Player03    Player07
Player03    Player08
Player03    Player09
Player03    Player10
Player03    Player11
Player03    Player12
Player03    Player13
Player03    Player14
Player03    Player15
Player03    Player16
Player03    Player17
Player03    Player18
Player03    Player19
Player03    Player20
Player04    Player05
Player04    Player06
Player04    Player07
Player04    Player08
Player04    Player09
Player04    Player10
Player04    Player11
Player04    Player12
Player04    Player13
Player04    Player14
Player04    Player15
Player04    Player16
Player04    Player17
Player04    Player18
Player04    Player19
Player04    Player20
Player06    Player07
Player06    Player08
Player06    Player09
Player06    Player10
Player06    Player11
Player06    Player12
Player06    Player13
Player06    Player14
Player06    Player15
Player06    Player16
Player06    Player17
Player06    Player18
Player06    Player19
Player06    Player20
Player07    Player09
Player07    Player15
Player08    Player09
Player08    Player10
Player08    Player11
Player08    Player12
Player08    Player13
Player08    Player14
Player08    Player15
Player08    Player16
Player08    Player17
Player08    Player18
Player08    Player19
Player08    Player20
Player09    Player10
Player09    Player13
Player09    Player14
Player09    Player15
Player09    Player20
Player10    Player13
Player13    Player14
Player13    Player15
Player13    Player17
Player14    Player20
Player16    Player17
Player16    Player18
Player16    Player19
Player16    Player20
Player18    Player19
Player18    Player20
Player19    Player20
</code>
Answer: I was able to identify the existence of one using the blossom algorithm.


Leibniz rule for expression containing derivative.

Question: I have found the following algebraic manipulation in a solution to a homework problem, and I don't understand it. The claim is (assume continuous, differentiable $f$):
$$ \frac{x^2}{2} f^\prime(x) = \int_0^x f^\prime(x)(x-t)\,dt $$
I find this confusing, because it seems to me that I can write
$$\int_0^x t f^\prime(x)\,dt = \left[\frac{t^2}{2} f^\prime(x)\right]_0^x = \frac{x^2}{2} f^\prime(x),$$
since $f^\prime(x)$ does not depend on $t$, which does not agree with the expression above (where does the $-x$ part come from)? What am I missing?
Comment: You are right. The conlusion is wrong - the sign minus is missed.
Comment: No, the solution was correct. I missed the minus sign and have edited the question to add it. However, the minus sign is not the source of my confusion-- it's the extra term $f^\prime(x) x$ under the integral that I'm unclear on.
Comment: @ user3482876. Now it's OK. Before correction, equality was wrong as I wrote because minus was missed
Answer: I think they are equivalent. Let $t'=x-t$, then
$$
\int _0^xtf'(x)dt=\int _x^0(x-t')f'(x)d(x-t')=-\int _x^0(x-t')f'(x)dt'=\int_0^x(x-t')f'(x)dt'
$$
Comment: I don't see a problem with your calculation, but I find this result very strange-looking. After trying it on a few examples of $f^\prime(x)$ I am convinced it is correct, though. Do you know a geometric interpretation?


Forcing Eloquent models to re resolve database connection

Question: Is there any way to force Eloquent models to re resolve the connection they were instantiated with?
Right now I have a method that changes the database connection in a loop :
foreach ($array as $key => $value) {

   Config::set('database.connections.mysql.database', $key . '_' . $database);

   $productRepo = new ProductRepository();

   $products = $productRepo->all();

}

Inside the product repository, i'm calling the Eloquent methods all() to get all the products in the database.
What i discover was that the connection was changed to the first value in the array but it does not change on further executions of the loop.
Is there anything I can do to force Eloquent to resolve the connection parameters at runtime?
Answer: Each database is really a separate connection, so I would setup all the connections to the database in the config/database.php file, and then you can use the setConnection() method on the model to switch between them.
database.php:
return [
    'default' => 'mysql-key1',
    'connections' => [
        'mysql-key1' => [
            'driver'    => 'mysql',
            'database'  => 'key1_dbname,
            // etc
        ],
        'mysql-key2' => [
            'driver'    => 'mysql',
            'database'  => 'key2_dbname',
            // etc
        ]
    ]
];

ProductRepository.php:
public function setConnection($name) {
    // assumes $this->product is your Product model
    $this->product->setConnection($name);
}

Code:
$productRepo = new ProductRepository();
foreach ($array as $key => $value) {
   $productRepo->setConnection($key . '_' . $database);
   $products = $productRepo->all();
}

Edit
additional information based on comments
Laravel is only going to instantiate a connection to the database the first time it is needed. Once the connection has been created, it is going to reuse it. It would be a huge performance issue if you had to reconnect to the database for every single query.
That being said, if you really want to do this the way you originally designed it, all you would need to do is call the DB::reconnect() method after updating the config.
foreach ($array as $key => $value) {
    Config::set('database.connections.mysql.database', $key . '_' . $database);
    DB::reconnect();

    $productRepo = new ProductRepository();
    $products = $productRepo->all();
}

However, I would strongly urge you to consider the originally described option so that you're not making a ton of unnecessary connections.
Comment: @yulun I have added some information to the answer based on your comment.
Comment: @yulun The problem is that `setConnection()` works on an instance of a class, but the `create()` method is a static method on the class itself. All the `create()` method does is instantiate a new instance with the given attributes, and call `save()`. So, instead of using `create()`, you'll just need to do this manually: `$productRepo = new ProductRepository($attributes); $productRepo->setConnection($key . '_' . $database); $productRepo->save();` You could also override the `create()` method on the ProductRepository class to accept a connection to do this, as well.
Comment: thanks! this could work. however I still do not understand why the model would still refer to the first connection even though it was changed dynamically. hope someone can explain !
Comment: hey @patricus, sorry to revisit the question but do you know if the method create() works differently from the query methods? because even after using setConnection() on the model, the create() method still points to the previous database.
Comment: just in case others are looking at the same problem as well .. you can refer to a better explained answer by @patricus here http://stackoverflow.com/questions/29069101/multi-tenancy-in-laravel-eloquent-orm
Answer: In my case, I did not have a database config for each of my databases, since they are created dynamically.  I used the following approach, just changing the table name to include the database name as well (This works for MySQL):
foreach ($array as $key => $value) {
   $productRepo = new ProductRepository();
   $productRepo->setTable($key . '_' . $database);
   $products = $productRepo->all();
}


Book with Spectral Sequence Calculation of (Co)Homology of Klein Bottle?

Question: Is there a book (A User's Guide to Spectral Sequences? Brown's Cohomology of Groups?) that goes through a calculation of the (co)homology of the Klein bottle using a spectral sequence, treating it as a fiber bundle with base $S^1$ and also fiber $S^1$?
What I really need to compute is the (co)homology of a fiber bundle with base $T^2$ (the standard 2-torus) and fiber the Brieskorn homology sphere $\Sigma(3,4,5)$ using trivial outer action but a non-trvial element of $H^2(Q; Z(K))$ in the total group of the fiber bundle $E$'s fundamental group, but I'd like to start with a problem where I know the answer, as I've never done a spectral sequence calculation before.
Answer: I am wholly ignorant of the literature, though I have heard Hatcher's notes are good to play with for practice. I will simply run through the computation for you. I apologize if I draw the SS differently than others, I forget sometimes how most people orient it --- you may have to swap the axes.
What you know is that there is a spectral sequence $$H^*(B;H^*(F;\Bbb Z)) \implies H^*(E;\Bbb Z).$$ This should be interpreted carefully: this is cohomology with local coefficients, where $H^*(F;\Bbb Z)$ is a $\pi_1 B$-module.
Remember that the Klein bottle may be defined as $K = S^1 \times [0,1]/(z,0) \sim (\overline z, 1)$, gluing the boundaries together by a reflection of the circle. This is in fact how the fiber bundle structure is defined; the projection to the base is the projection to the second factor $K \to [0,1]/0 \sim 1$, which is homeomorphic to a circle. Thus the monodromy action of $\pi_1 B = \Bbb Z$ is such that $1$ acts as reflection on the circle. The induced map of reflection on cohomology is the identity on $H^0(S^1)$ and $-1$ on $H^1(S^1)$.
Thus our spectral sequence starts with bottom line $H^*(S^1;\Bbb Z)$ (which is a copy of $\Bbb Z$ in degrees zero and one) and top line $H^*(S^1;\Bbb Z_-)$ (which is a copy of $\Bbb Z/2$ in degree one and nothing else). I encourage you to do the local coefficient computation on your own.
So our spectral sequence starts from $E_2$ page
$$0 \; \;\;\;\;\; 0 \; \; 0 \; \;\; \;$$
$$0 \; \; \Bbb Z/2 \; \; 0 \; \; \cdots $$
$$\Bbb Z \; \; \Bbb Z \; \; 0 \; \; \cdots$$
But all differentials from here on out have bidegree $(k, 1-k)$, and since we are at the $E_2$ page they will all have first degree $k \geq 2$ --- so that the differentials are all automatically zero. Thus the spectral sequence collapes at $E_2$.
We thus obtain $H^0(K; \Bbb Z) = \Bbb Z$, as well as $H^1(K;\Bbb Z) = \Bbb Z$ and $H^2(K;\Bbb Z) = \Bbb Z/2$. You may check this is the same thing you would get from, say, Mayer-Vietoris.
Comment: $\begin{tabular}{lll}
\mathbb{Z} & \mathbb{Z} \\
\mathbb{Z} & \mathbb{Z} \\
\end{tabular}$
Comment: Sorry, I got behind with classes starting up again! I hope to post more tomorrow or Sunday.

I read Hatcher, and did the homology calculation for the Klein bottle. Spectral sequences for the homology of fiber bundles are beginning to make sense to me. It appears the $E^1_{p,q}$ page is a $C_*$ page and the $E^2_{p,q}$ page is an $H_*$ page.

I hope to post more tomorrow. I think I should be able to do the calculation for $\Sigma(3,4,5) \hookrightarrow E \twoheadrightarrow T^2$ with what I know now; thank you so much
Comment: I do everything with handlebody homology (deRham cohomology when I can), so I think I can see everything that's going on on the $E^1_{p,q}$ page pretty accurately, even with local coefficients.


How to chose a ukulele chord tab?

Question: I found this website with common ukulele chord tabs and tried to typeset an E chord in Sibelius:

The first one was added by Sibelius (G# E B) and the second one by the mentioned website (B E G# B).
I understand that there are differences in voicing and numbers of notes (3/4).
In practice, when editing sheet music for ukulele, how to pick the chord tabs? Does the choice of one tab depend on the preceding/succeeding tab?
Comment: This question would seem to boil down to how to decide which voicing to use for a particular chord. That has been discussed here before. I'll try to hunt down some links. If your question is different from that, you might consider adding some clarification.
Comment: [Is there a reason an arranger/composer will choose a particular chord voicing when charting chords for guitar on a particular song?](https://music.stackexchange.com/questions/93484/is-there-a-reason-an-arranger-composer-will-choose-a-particular-chord-voicing-wh)
Answer: Some pretext:
In my answer, a tab, short for tablature, is a form of music notation for an entire song that plots the individual notes marked as fret numbers on each string. A chord diagram is the graphic that shows where to place the fingers on the fretboard in order to play a specific chord. Different forms of music notation, including sheet music, lead sheets, and tabs, are often accompanied by corresponding chord diagrams.
To show chord diagrams in text form, I will use the common practice of writing a string of 4 characters to represent each string from G to A with a number meaning a particular fret, a 0 meaning the open string, and an x meaning a muted string. As such, the common C major chord all ukulele players learn will be represented by 0003.
This is not consistent with the wording in the question; the question is currently written so as to mean that each chord diagram is a "tab" and thus a song has "tabs" to show one how to play the chords. To avoid confusion, I will write this answer in the more technically precise way - a tab is not the same as a chord diagram.

First of all, I can tell you why Sibelius came up with that chord symbol. I haven't used Sibelius in a long time, but my guess is that you had the notes [E G# B] written on the treble staff and then asked Sibelius to produce a chord diagram for that chord in the staff notation. My other guess is that you could also have asked Sibelius for a chord diagram for an E major chord. In either case, I think Sibelius interpreted this very literally. I suspect that Sibelius thought something along the lines of:

An E chord has the notes E, G#, and B in it. Let's map those notes onto an ukulele fretboard to create a chord diagram.

The lowest fret position that makes an E note is the open E
string.

The lowest fret position that makes a G# note is the 1st fret
on the G string.

The lowest fret position that makes a B note is the
2nd fret of the A string.

All the notes of the chord are accounted for. Let's mute all remaining strings.

Therefore, an E chord is fingered as 1x02.

I would guess that Sibelius has an algorithm to find chord diagrams for any notes or any chords on any string instrument tuned any possible way. However, there are always many alternative ways to play a chord, and what Sibelius finds to be the best diagram won't always align with what a human would decide upon.

All chords have alternate voicings and fingerings. When assigning chord diagrams to any form of music notation, one is recommending to the performer a particular way of playing the chord, and unfortunately the E major chord is one of the least agreed-upon of the common ukulele chords.  Context will matter a lot in determining what particular diagram you decide to endorse. I would recommend generally staying consistent, so one diagram should work for all the E chords. Let the player decide if there's a reason to switch it up.
The website linked in the question lists 4442 for the E major chord. This is a very common fingering for this chord. Some players "collapse the knuckle" to barre partially at the fourth fret, others use all four fingers on individual notes. I use and recommend this voicing because it is a moveable shape that can be easily applied in other places on the neck, as well as the fact that similar related chords are easily accessible from this position such as 4444 (E6), 4445 (E7), 4432 (Em), 5553 (F), and 4222 (Bm) for example.
On the other hand, this is one of the most complained-about chords I know of for the ukulele and can be difficult for beginners. Collapsing the knuckle to avoid touching the A string can be a real obstacle to the unexperienced, and cramming three fingers into the fourth fret area makes for a very tricky alternative to that barre.
The other main contender for the title of definitive E chord is 1402. This voicing has some serious advantages: with this shape, barring is unnecessary, and every fretted note is spaced far enough away to allow for cleaner fingering. It even has an open string in it, only requiring three fingers to play. I used this as my E chord for a long time when I first began playing, and I still recommend it to others who have trouble with 4442. I've even read some ukulele books that prescribe this 1402 shape, although a majority still list 4442. It does have its own set of problems, though.
This chord shape is not moveable, since it has an open string. Where 4442 can be slid up and down the neck to make more major triads, 1402 is much less versatile and only works in that position. Not only that, it doesn't lead as well into related chords around it. 1202 (E7) requires a repositioning of the entire hand, as do 2100 (A) and 4432 (B). While it is close to 2402 (Esus), it lacks an intuitive path to get to 4422 (Esus2) or 4402 (E5). Most of the chord transitions to and from this chord will end up being extra memorization; muscle memory for these movements won't apply as easily to other common chord changes since the 1402 shape is so specific. And for experienced players, it is much harder to add an extra finger somewhere or slide one around to add some flair with this 1402 shape than with 4442, which practically begs to be played around with.
Sibelius, though, came up with the ostensibly simpler 1x02 fingering. Although most ukulele chords are played with all four strings sounding regardless of how many unique notes appear in the chord, it is possible to learn to mute some strings while letting others ring out. This can be used to find easier alternatives to some tough shapes, and sometimes a mute can be avoided altogether with an extra-precise strum that avoids the string to be muted entirely. Many players do learn either muted-note chords or string-skipping strumming techniques, so for them this 1x02 chord may be a good alternative.
Personally, I have never adopted muting notes in chords or targeted strumming into my own playing, since my playing style diverged from the situations where those kinds of chords would be potentially viable (I do a lot of fingerstyle/fingerpicking). It is definitely possible to get away with neglecting these types of chords if one is willing and able to learn enough four-note voicings to cover all of one's bases. Moreover, these chord voicings will lack familiarity to most readers since all common chords have at least one standard way to play them that does not require muting.
In any case, I would argue that 1402 is just as easy if not more so, and one would have to use three fingers anyway - why not play that E on the fourth fret instead of having to lightly touch one string while pressing hard on the others? Additionally, this 1x02 is a bit of a "crutch chord". Like 1402, this is not a moveable shape so it lacks the versatility of 4442, and it doesn't lead into and from other related chords as well as 4442 does. I would not recommend 1x02 unless muting strings is already in the player's arsenal, and so for the most part I would not give the 1x02 chord diagram unless other muted-note chords will also appear in the music. For ease of performance, either categorically avoid all muting shapes, or prescribe them wherever it makes a chord easier.
There are lots of other options, especially if one looks higher on the fretboard. Here is one example source that lists some alternative ways of playing an E chord.

How does one decide what chord voicing to display for a song? In the general case, go with the most universal shape for the chord. Most chord diagrams written for songs are placed for the benefit of a less-experienced ukulele player who may need a quick reminder on what the desired chord looks like (or may even not yet know how to play a certain unfamiliar chord).
Chord symbols typically are located in one of two places: right next to every place where they occur in the music, or each chord in the song listed once at the top of the page. If they are at each specific location, it may be possible to suggest specific chord voicings for each instance, but beware that beginners will often either be confused by this or ignore it completely, and advanced players will already be aware of your suggestion and won't need the reminder. In a teaching context, it can make a lot of sense to show multiple diagrams for the same chord at different times (one potential example is the "pinky G" variation on the G chord that moves more smoothly to E7 but does require the pinky finger to be involved), but generally this is counter to the style of chord-diagrammed notation.
Chord diagrams are intended to demonstrate one definitive way to play the chords in question and are not intended to be absolute prescriptions for the only correct way to play the song. Experimentation and deviation from the exact chords given is to be expected and welcomed, and the more experienced players will come to their own conclusions about what exact chord voicing is best in each case. If explicit information on how to play each chord is deemed necessary, tablature or even fully-realized sheet music (gasp, shudder) would be a much more appropriate notation option!

How to chose a ukulele chord tab?


Change a virtual machine Notes attributes in esxi shell

Question: I have tried to change via ESXi shell the attribute field with the following command:
vmx -s attributes=new notes /vmfs/volumes/datastore1/VM/VM.vmx

but it doesn't change the attributes and doesn't give me any error.
Is there any command to do that?
I am using ESXi 5.0,
Answer: You can do this using PowerCLI.
Connect-VIServer esxihost.example.com
Set-VM name-of-vm -Notes "Here are some notes"

Comment: Can i set it without a power CLI ? In Esxi shell
Comment: You shouldn't be using that shell for routine tasks as a general rule. You should only be messing around with the shell if asked to do so by VMware support.


Let $X$ be a random variable with continuous CDF $F$. Find CDF for $|X|$ and (if $F\in C^1$) density $f_{|X|}$

Question: Let $X$ be a random variable with continuous distribution function $F$.

Find the distribution function $F_{|X|}$ for $|X|$.
Suppose that $F\in C^1$ . Find the density for $|X|$.

My approach was like this:
$$ F_{|X|}(x) = P(|X|\leq x) = P(-x \leq X \leq x) = F(x)-\lim_{y\uparrow-x}F(-x) = F(x)-F(-x) $$
In the last step I used the fact that $F$ is continuous.
Now the problem is the second part, I'm a little confused. Suppose that $F\in C^1$. Define $f:=F'$. It follows that $f_{|X|}(x):=F_{|X|}'(x) = F'(x) - F'(-x) = f(x) + f(-x)$. I know that $f$ is the density from $X$, but $f_{|X|}$ should be the density from |X| right? I'm probably making a mistake here somewhere but I think $f_{|X|}$ isn't a pdf because
$$
\int_{-\infty}^{\infty} f_{|X|}(x)\,dx = \int_{-\infty}^{\infty} f(x) + f(-x)\,dx =
$$
$$
=\underset{=1}{\underbrace{\int_{-\infty}^{\infty} f(x)\,dx}}
+ \underset{\neq 0}{\underbrace{\int_{-\infty}^{\infty} f(-x)\,dx}} \neq 1
$$
Where is my mistake? Thanks in advance!
Comment: But |X| takes values only in $[0,+\infty)$ not in $(-\infty, +\infty)$. So, the formula you got for $F$ is for $x>0$ and respectively you should define the integration limits in the second part.
Answer: Since $|X| \in [0, +\infty)$ you should be aware that the formula you got for $F$ is valid only for $x\ge0$. Thus in the second part you have that $$\begin{align*}\int_{-\infty}^{+\infty}f_{|X|}(x)dx&=\int_{-\infty}^{0}0dx+\int_{0}^{+\infty}\left(f(x)-f(-x)\right)dx\\&=0+\int_{0}^{+\infty}f(x)dx+\int_{0}^{+\infty}f(-x)dx\\&=\int_{0}^{+\infty}f(x)dx+\int_{-\infty}^{0}f(x)dx\\&=\int_{-\infty}^{+\infty}f(x)dx=1\end{align*}$$
Comment: Oh, I should have seen that. Thanks for your answer!


Do blocks replace custom content panes?

Question: In Drupal 7 I use the ctools_custom_content (Custom content panes) module to create exportable panel panes to drop into my panel pages. This module doesn't exist for Drupal 8, nor do I see any other way to create panes with the Panels, Page Manager and ctools modules installed.
Have blocks, which are now entities, effectively replaced custom content panes in Drupal 8? Or is there a different core feature or contributed module that I should look to for similar functionality?
Answer: There are 3 different things called "block" in Drupal 8. plugins, config entities and content blocks. See Adding custom block problems for an explanation.
But yes, to answer your question, content blocks are exposed as block plugins and can then be used not only for placing blocks in regions but also with page_manager/panels. You don't need anything else.
One limitation is that they can't be exported easily into config, since they are content. If you don't need extensible structure but only a text or a reference, then you could build your own block plugin or wait for a contrib module to do that (I recently saw a module that did basically that in 7.x with its own plugin system, would be very easy to make the same for 8.x as core provides 90% of what's needed. Can't remember the name right now.)
Comment: Thanks for the answer! It helped me realize that the "exportable" bit is key to my question. If neither you nor anyone else knows of a currently available contrib module that allows for easily exportable blocks of text, I may try building my own block plugin as you suggest.
Comment: Can't find the one that I remember but that was 7.x only anyway. I did find https://www.drupal.org/project/simple_block but that creates a separate config entity, which means you always have to create that first and can then place it. Unless you are specifically looking for reusable blocks, that seems overkill to me and you can achieve the same with a trivial block plugin.

Do blocks replace custom content panes?


R Shiny : Display tables as an output

Question: I am creating an R Shiny app. Everything is OK and the code runs (I have put cat() functions all other the code and I see everything works well in R Studio's console), but I don't really know how to display a table in my specific case.
My Shiny app takes user input and generate 2 datasets. It starts when the user push the button I have putted in interface (eventReactive).
The first one (dataset_1) is generated from API calls and several cleaning steps.
The second dataset (dataset_2) is generated from treatments in dataset_1.
How to structure the ui.R and server.R code to display the dataset_1 and dataset_2 (both are tables) as outputs in the user interface ?
Here are my current ui and server files:
ui.R
shinyUI(fluidPage(
   sidebarLayout(

     sidebarPanel(
        textInput("myfirstinput"),
        textInput("mysecondinput"),
        actionButton("button")
     ),
     mainPanel(
        ???????????
     )
   )
))

server.R
shinyServer(function(input, output) {
   ???????? <- eventReactive(input$button, {
       input1 <- input$myfirstinput
       input2 <- input$mysecondinput
       #function Make dataset_1 from api call (based on input1 & input2)
       dataset_1
       #function clean dataset_1
       #function Make dataset_2 from treatments in dataset_1
       dataset_2
       })
 })

In server.R file, I don't really know how to manage the eventReactive (see "????????") because I am generating 2 datasets...
Thanks!
Answer: If I'm understanding your question correctly, the problem is that you have one function that generates two datasets. While an eventReactive call can only return a single reactive function, that function can return a list containing any number of different objects that can be used independently. In your case, join your two datasets into a list and then return that list from your eventReactive. Then, when you call that reactive function (for example in a renderPlot or renderTable), you can choose which list item you want to work with:
shinyUI(fluidPage(
    sidebarLayout(

        sidebarPanel(
            textInput("myfirstinput"),
            textInput("mysecondinput"),
            actionButton("button")
        ),
        mainPanel(
            tableOutput("table1"),
            tableOutput("table2")
        )
    )
)

shinyServer(function(input, output) {
    plots.dfs <- eventReactive(input$button, {
        # Make dataset_1
        # Make dataset_2
        return(list(dataset_1, dataset_2)
    })

    output$table1 <- renderTable({ plots.dfs()[[1]] })
    output$table2 <- renderTable({ plots.dfs()[[2]] })
})

Comment: Thanks for your answer but it is not exactly what I need. I don't want to plot anything, I want to display a table (dataset_2) in user interface
Comment: If you want to show a table, just use `tableOutput` and `renderTable` functions. Or do you not care at all about `dataset_1`?

R Shiny : Display tables as an output