Model selection for $4\times 2\times 2$ factorial design in R

Question: I have read so many books, forums, and sites that I have completely confused myself.
Originally, my design was a simple 4x2 factorial with two categorical predictors, but it has gotten more complex with adding another categorical predictor, two continuous covariates, and an additional continuous dependent variable.  Does anyone have suggestions for selecting a statistical analysis for a 4x2x2 with 2 continuous covariates and 2 continuous dependent variables? I have since looked in to MANOVA and the mixed model, but I can't solidify a reasoning for choosing a specific approach.
Covariates are growth data on the individuals, and the dependent variables are not repeated measures.
Comment: Please edit the question to give more information about the details of your study. You seem to have two continuous outcome variables and two categorical predictors (one 4-level and one 2-level/binary, for the original 4x2 factorial design), but what type is the "another independent variable" and the "two covariates"? Are there repeated measurements on the same individuals? This seems like it could fit under the general form of a linear model, but it's hard to say without such details. Please provide that information by editing the question, as comments are easy to overlook and can be deleted.
Comment: @EdM Thank you for the feedback.  I hope I have provided some clarification.
Answer: What you describe seems to fit a two-outcome (multivariate) multiple regression model. Each of your categorical and continuous predictors/covariates is entered into the model, something like this in R:
<code>lm(cbind(outcome1,outcome2) ~ factor1*factor2 + extraCategorical + covariate1 + covariate2)
</code>
That includes the initial factorial design and additive terms for the other categorical predictor and the covariates. If you think some interactions among the other predictors are important and you have enough data, you would include those also.
These notes by Fox and Weisberg show how to perform such a multivariate analysis. Associations between the two outcomes for each individual are handled by the covariance structure; the point estimates of regression coefficients for each outcome are the same as you would get with separate regressions. I suppose this technically would be called MANCOVA (multivariate analysis of covariance), but such terminology often gets in the way more than it helps. Focus on what the outcomes are, what the predictors are, and how you think they might be related. Then specify a linear model to match. That way you can extend the rigid structures of those classic acronyms to give you just what you want.
Comment: Thank you! I think the will work, especially since I'm interested in the interactions of the predictors.  I've tried to figure out the technical classifications to make sure I meet all the assumptions before getting too far along.
Comment: @Dakota when you put all those classical classes of models into the single schema of linear models, then the critical assumptions end up being the same for all of them. See [this page](https://stats.stackexchange.com/q/16381/28500) for a good introduction.
Comment: I'm trying one more thing with the initial 4 treatment levels of one categorical predictor with 1 continuous response variable over 6 sampling dates.  Initially I thought I would used a repeated measures anova, but am I overlooking a reason to use a manova or mixed effects glm instead?
Comment: @Dakota if you must model a response variable in terms of counts, then the simple `lm()` approach to correlations of the bivariate outcomes within individuals might not work. If the counts are high enough you might have a work-around by treating the counts as effectively continuous, perhaps with a square-root or log transformation, and proceeding with `lm()`. If those solutions aren't appropriate for your data, I'd suggest searching the site or asking a new question about how to handle multivariate outcomes of different types (continuous, count, binary, ...). That's outside my expertise.
Comment: @Dakota percentages are often better handled by modeling the actual outcome and using things like the number of trials or observation duration as a predictor. For binary outcomes, a logistic regression with `glm()` handles that directly when you specify the outcome as a 2-column matrix of successes and failures for each case. For count or continuous outcomes, an `offset` term could be appropriate. When you ask another question, be sure to provide information about the experimental design and the original measurements that were made, rather than just jumping to their reduction into percentages.
Comment: I've worked through my analysis, and determined that I did not need both covariates, since they were correlated, and my response variables are not correlated, so I am running them through the model separately.   I've used an aov approach for an "ANCOVA"  aovout<aov(response~  covariate.centered + factor1 * factor2 * factor3, data), and the aov() and lm() function gives me the same output even though the SS should be different (Type 1 vs Type 3).  I've also read that glm() should be used instead of a 3-way ANCOVA with aov().  Do you have any insight?
Comment: @E10 the `glm()` function is for [generalized linear models](https://stats.stackexchange.com/tags/generalized-linear-model/info) that allow nonlinear mapping between the linear predictor and the outcome and other error distributions around predictions--like for binary outcomes. The `aov()` and `lm()` functions are identical in terms of modeling; they just present results in different ways. The `Anova()` function in the [`car` package](https://cran.r-project.org/package=car) applied to an `lm()` model might be a better choice, particularly if your design is unbalanced. Read its help page.
Comment: @E10 for the dual responses is not so much whether the responses themselves are correlated, it's whether the error terms are correlated. If you'd like to pursue a bivariate outcome model, the `Anova()` function in the `car` package allows for that, as explained by Fox and Weisberg in the reference I linked in the answer.


How to analyze a new music piece?

Question: I've been learning to play the violin for 4 years. I always struggle with reading new music piece, it takes me quite a while to fully understand it, by that I mean to actually know the rhythm and become acquainted with the techniques performed in that piece. How can I improve the sight-reading speed and how to correctly and quickly analyze a new music piece? I always find it hard to identify the beat and usually mess up with notes' length.
Comment: read by intervals
Comment: Please read some of the Related questions in the sidebar to the right. They may help.
Answer: Make sure you can sing it before you try to play it.   Or, if it's outside your vocal range, sing it internally.  Tap out the rhythm.  Then, when you're clear in your head what it SHOULD sound like, start practicing it, section by section, as slowly as is necessary to get it right.   This is the important part.  Play as slowly as is necessary to get it right.  If you fumble, you're trying to go too fast.
Answer: It,s always harder to play it slow ,but the rewards after are huge ,use your ears ,get it in your head first
Comment: Hi Paul, please read [about] and [ask] for guidance on the structure of posts that we need.

How to analyze a new music piece?


line segment intersection strange results

Question: I'm using this formula. I am getting very strange results with (1,3) to (29,17) and (6,19) to (7,8). I got an X* value of 7. When I plugged this into my intercept calculator it said they intercept at (7.173913, 6.086957). How is this possible with line segments?
The same thing happened with (1,3) to (29,17) , (6,19) to (9,15). X* is 8.793103. My intercept calculator said they intercept at (13.363636, 9.181818).
line segment intersection
$$x_*=\frac{{x_1} ({x_3} ({y_2}-{y_4})+{x_4}
   ({y_3}-{y_2}))+{x_2} ({x_3} ({y_4}-{y_1})+{x_4}
   ({y_1}-{y_3}))}{({x_1}-{x_2})
   ({y_3}-{y_4})+({x_4}-{x_3}) ({y_1}-{y_2})}$$ and this value must be such that $x_1 \leq x_* \leq x_2$ and $x_3 \leq x_* \leq x_4$ in order the segments intercept.
Answer: The two lines in your first case are $y=\frac12x+\frac52=(x+5)/2$ and $y=-11x+85$. Their intersection has $x^*=165/23$, which agrees with your intercept calculator. I don't know what method you used for finding the intersection, but there's something wrong with it. The fact that $x^*$ is greater than $7$ just means that the segments don't intersect.
Comment: I'm using the big x* formula in my post. I figured I could trust a guy that has 27.9K reputation score. That formula works in most cases, but I have found a few weird cases like what I just posted.
Comment: Right. Here's a case where it's more reliable to write down the two equations (point-point formula, for instance, for each) and solve the pair of simultaneous equations that you get.


Is $\inf_{[0,\:t]}f=\inf_{[0,\:t]\:\cap\:\mathbb Q}f$ even when $f$ is only left-continuous?

Question: Let $f:[0,\infty)\to\mathbb R$. If $f$ is continuous, then $$\inf_{[0,\:t]}f=\inf_{[0,\:t]\:\cap\:\mathbb Q}f\;\;\;\text{for all }t\ge0\tag1$$ by the intermediate value theorem. Can $(1)$ be shown in the case of left-continuity of $f$ too?
Answer: Yes. Suppose $L=\inf_{[0,t]} f$. If $f(0) = L$, we're done, since $0$ is rational.
Otherwise, for any $\epsilon>0$, there is some $c$ such that $c \in (0,t]$ and $|f(c)-L| < \frac{\epsilon}{2}$. In addition, by left-continuity, you can find some rational $d \in [0,c)$ such that $|f(d) - f(c)| < \frac{\epsilon}{2}$. Hence
$$\epsilon = \frac{\epsilon}{2} + \frac{\epsilon}{2}>|f(c)-L| + |f(d) - f(c)| \geq |f(d)-L|$$
In other words, there's rationals in $[0,t]$ which $f$ maps arbitrarily close to $L$.
Comment: We can also use contradiction: If $L(L+M)/2>f(x),$  a paradox.............+1
Comment: Did you implicitly assume $L>-\infty$?
Comment: @0xbadf00d Yes, I did. However, you can easily modify the argument to take care of that.

If $$\inf f(x)_{x \in [0, t]}= -\infty$$ then $$\inf f(x)_{x \in (0, t)}= -\infty$$ so given any $M>0$, there is a $c \in (0,t)$ such that $f(c) < -M$. By left continuity, there is a $\delta>0$ such that for all $q \in \mathbb{Q} \cap (c - \delta, c)$, we also have $f(q) < -M$.

Since $M$ is arbitrary, we conclude $$\inf f(x)_{x \in [0, t] \cap \mathbb{Q}}= -\infty$$


Copiar a data de um JTextfield para um JDateChooser

Question: Atualmente eu espelho do JDateChooser para um JTextField, mas agora estou precisando espelhar de um JTextField para um JDateChooser. Como faço para copiar e converter uma data de um campo de texto para o JDateChooser?
Utilizo dessa maneira para mandar para o JTextField
    String dia = Integer.toString(JDateChooser1.getCalendar().get(Calendar.DAY_OF_MONTH));
    String mes = Integer.toString(JDateChooser1.getCalendar().get(Calendar.MONTH) + 1);
    String year = Integer.toString(JDateChooser1.getCalendar().get(Calendar.YEAR));
    String fecha = (dia + "/" + mes + "/" + year);
    EntradadadosData.setText(fecha);

Comment: Você testou a solução abaixo?
Answer: Se o objetivo é converter a data do campo de texto, tente conforme abaixo:
Date date = new SimpleDateFormat("dd/MM/yyyy").parse(seutextfield.getText());

JDateChooser1.setDate(date);

Atente-se ao fato de que pegar entrada de usuário de um campo de texto e converter para data, sem nenhum tratamento ou restrição, vai ocasionar problemas que podem estourar em exceções de parse, se não puder ser garantido que os valores são realmente datas válidas. Caso o campo seja somente leitura, preenchido com data vindo de outro lugar onde seja garantido que a informação é realmente uma data, este problema pode ser descartado.
Comment: Desculpe Articuno, só pude ver agora. Deu certo sim, só tive q trocar o formato da data. Eu já havia pensado nessa possibilidade da pessoa enviar a data errada, mas fiz de uma maneira q isso não vai acontecer.
Comment: Estou com uma pequena duvida e gostaria de saber se vvc tem um post relacionado ao meu problema. Estou precisando agora de quando uma pessoa mudar um item de um combobox, ele limpar esse campo da data e mais alguns. para q a pessoa seja forçada a entrar com uma novada data.
Comment: @RafaelChaves sim, veja essa resposta: https://pt.stackoverflow.com/a/117903/28595
Comment: perfeito! dois coelhos em uma cajadada. Vlw!


Mod_rewrite: Different behavior on different systems

Question: I have the following .htaccess mod_rewrite rule:
RewriteEngine on
RewriteRule ^(abc1|abc2|abc3|abc-defg)/([^/]+)$  /redirect/category/$1/$2
RewriteRule ^(.+[^/])/$     /$1 [R=301,L]

Now I have the following url:
http://www.example.com/abc-defg/cat1/

which should be mapped to
http://www.example.com/redirect/category/abc-defg/cat1

This rule works on my local dev machine (Win7, Xampp), but not on production server (running Ubuntu 12.04 with Apache, mod_rewrite and mod_fcgid).
On Ubuntu, I get redirected to http://www.example.com/abc-defg/cat1 (removed "/", which is the second rule).
How can I find out why it's not working as expected?
Comment: Ok, I will solve the redirect on php side (since it is only for "old" urls).
Answer: You must place 301 redirect rule before first rule:
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ /$1 [R=301,L,NE]

RewriteRule ^(abc1|abc2|abc3|abc-defg)/([^/]+)$ /redirect/category/$1/$2 [L,NC]

Test this out in a new browser.
Comment: No, thats not the solution. The first rewrite is for old urls (we make a relaunch) and the second one is to prevent dublicate content. It doesn't matter, even if I comment out the seond one ('RewriteRule ^(.+[^/])/$ /$1') , it doesn't work as expected on Ubuntu.
Comment: Is `/abc-defg/cat1` a real directory on Ubuntu? First verify if this .htaccess is even read on Ubuntu by placing some random text in it and see if it generates 500 or not
Comment: As I said, this file is read and used, since the second regex is working.
Comment: There was 1 more question in my comment
Comment: What is the exact URL you enter in browser and what error do you get? (Examine in Chrome dev tool)
Answer: Your regex doesn't match.
Try this:
^(abc1|abc2|abc3|abc-defg)/([^/]+)/?$
You need to clarify that it can end with a slash. Or alternatively (but not recommended), not end it with a slash.
www.example.com/abc-defg/cat1 does match the regex - so I assume this is what you were using before.
That said it's entirely possible a user will put a slash at the end so it's best to account for it.
Comment: You are totally right! I don't know how it can work on my dev maschine, but the regex was wrong. But now, I can use the version with / and it works, but it doesn't work without a ending /, any ideas?
Comment: And the second weird thing is, if I use ^(abc1|abc2|abc3|abc-defg)/([^/]+)$, it doesn't work with /abc-defg/cat either :O
Comment: strange. the finishing ? should allow for a lack of / at the end. Testing in this site: http://martinmelin.se/rewrite-rule-tester/ shows it working.


Sametime Issue - User not available in group even when online

Question: We have two sametime servers(Replica) located in London and Singapore.
We also have a group (PL_ALL) in which singapore users and london users are added.
When user log in via London server as host, singapore users are not shown in group PL_ALL. But at the same time there is another group PL_SG_ALL which is working correctly. (EX: User XX is there in both group but only visible in one group)
Any idea what could be the problem with PL_ALL group alone?

Sametime Server Version: 8.5.2 &
Sametime Client Version: 8.5.2

Main Group - PL_ALL
Sub Groups - PL_LN_ALL and PL_SG_ALL
Subgroup under PL_LN_ALL  - PL_LN_Finance and more
Subgroup under PL_SG_ALL - PL_SG_Finance and more
User 1 - Is from Singapore and he is added in group PL_SG_Finance. He
always connect to sametime client using Singapore sametime server as
host
User 2 - Is from London. He always connect to sametime client using
London sametime server as host

Scenario 1: User 2 logs in to sametime and added PL_ALL and PL_SG_Finance group to his list. problem is he can see User1 listed under PL_SG_Finance but not under PL_ALL
Scenario 2:User 2 log in to sametine using Singapore sametime server as host. He case see User 1 in PL_ALL and also in PL_SG_Finance
I hope this is clear
Comment: Please provide the SW version of domino and Samtime used. Also, when you say "does not show in group", what do you mean exactly? Is this a group you added to your buddy list and the user is not showing up in there as available but it shows up as available in another group similar added? Does the user show up in the buddy list as available if you add him directly?
Comment: Sametime Server Version: 8.5.2 Sametime Client Version: 8.5.2
Comment: Are these servers in one ST Cluster? Which Server is User2 using in scenario 1 to log in?
Comment: Yes. Both servers are in cluster. User2 always uses London server and user1 always uses Singapore server. For verification purpose user2 logged in to Singapore server in scenario 2.
Answer: Have you tried the steps outlined in Technote #1113464 Troubleshooting issues with Sametime users not appearing online?
Especially, look at section 4 "If awareness involves clustered Sametime servers"

IV. If awareness involves clustered Sametime servers:
The communityConfig.txt file for the servers that are part of the cluster should look the same. If the clustered servers have different communityConfig.txt it is possible for awareness between the clustered servers (and additional Sametime servers) to intermittently stop working.
In order to ensure that the clustered servers build the same communityConfig.txt, make sure they are replicating the names.nsf and that they have the same configuration for Directory Assistance.
Comment: The user is not completely not visible. He is visible under PL_SG_Finance. He he is not visible only under PL_ALL even though PL_SG_Finance is listed under PL_ALL
Comment: Pls. verify communityConfig.txt according to "IV. If awareness involves clustered Sametime servers"
Answer: There are a lot of known issues with nested groups and awareness in Sametime. Just Google for "sametime nested groups" and you will find a bunch of suggestions for solutions. One is to get a special hotfix from IBM for this issue. Check this link for an issue, that sounds like yours.


Indexed System of Sets Proof Explanation

Question:
Proof:

I'm not sure what $f_i$ is. is $f_i$ an ordered pair or an element
of the codomain of f or what else?
Since f is a function on I, is I the codomain or domain of function
f?
What is I?

What does the exponentiation or multiplication of sets yield? Is the exponentiation or multiplication of sets the same thing as doing the infinite or n-ary cartesian product?

Ignore the background below if you know what we're talking about:
Comment: $f_i$ is the value that $f$ take on when applied to $i$, and can also be written as $f(i)$. $(i,s_i) \in f$ means $f(i) = s_i$.
Answer: Long story short: $f\subset I\times \cup_{i \in I}S_i$ means that $I$ is domain and $\cup_{i \in I}S_i$ is codomain. $f_i\in S_i$, so $f_i$ is an element of codomain. Moreover, $f(i)=f_i$ from definition. It is true, that $(i, f_i) \in f$, but in proof there is different notation, namely $(i, s_i) \in f$. I admit this notation is somewhat confusing.
It is helpful to start from beginning and carefully build things up. First of all $I$ is an arbitrary nonempty set. Then there is some auxiliary nonempty set of sets (aka family of sets) call it $X$. Now we need to fix some function $\Phi$ with domain $I$ and codomain $X$, thus $\Phi:I\to X$. In set language $\Phi=\{(i,x):i\in I ,x\in X \}$. Looks familiar? Observe, that if we take $X:=\{ S_i:i\in I\}$ and define $\Phi(i):=S_i$, then $\Phi=S$. It can be helpful to look at the case when $I=\{1,..,n\}$, and $|X|=m$ is also finite. Fix $\Phi: \{1,..,n\}\to X$, if $\Phi$ is surjection (so $n\geq m$), all elements of $X$ can be listed (probably many times) $X=\{\Phi(1),..,\Phi(n)\}$. Now take $X_i:=\Phi(i)$. For finitary product there is no need to introduce all that stuff, just simple induction:

$X_1\times X_2=\widehat\prod_{i=1}^2X_i=\{(x_1,x_2):x_1\in X_1,x_2\in X_2\}$
$\widehat\prod_{i=1}^nX_i=(\widehat\prod_{i=1}^{n-1}X_i)\times X_n$

(There should be big "$\times$" sign instead of $\widehat\prod$, like $\textbf{here}$.)
What is important, in finitary case induction based definition is equivalent to the "functional" one (there is cannonical bijection):

$\Pi_{i=1}^nX_i=\{f:f\ is\ a\ function\ on\ \{1..n\}\ and\ f(i)\in X_i\}$

It is helpful to see what is inside though:
$$\widehat\prod_{i=1}^nX_i=\{(x_1,..,x_n):x_i\in X_i\}$$
$$\prod_{i=1}^nX_i=\{\{(1,x_1),..,(n,x_n)\}:x_i\in X_i\}$$
If induction based product is so simple, couldn't it be extended to arbitrary indexing set? It cannot and $\underline{\textbf{here}}$ is why.
Exponent analogy to numbers (or more general cardinals) is that $|\{f:\ f\subset A\times B ,\ f\ is \ function\}|=|B|^{|A|}$, see $\underline{\textbf{here}}$.
Comment: I am confused, because I've already put an example of tuples and links to Wikipedia that discuss it in more detail. There was also a link to another SE question that should in my opinion shed some light ([link](https://math.stackexchange.com/questions/195517/definition-of-the-infinite-cartesian-product)). This topic is pretty tough at first sight (it was to me also), but after some time you'll hopefully find it meaningfull. Be sure you are familiar with cartesian-based definition of a function:
 https://en.wikipedia.org/wiki/Function_(mathematics)#Definition.
Comment: Is $$\widehat\prod_{i=1}^nX_i=\{(x_1,..,x_n):x_i\in X_i\}$$ a set that has only one element, a n-tuple whose coordinates are all the elements in the range of the function psi? The tuple ($x_1,...x_n$): $x_i \in X_i$ looks like a vector $\in R^{|X|}$
Comment: What's the difference between
$$\widehat\prod_{i=1}^nX_i$$ and
$$\prod_{i=1}^nX_i$$? What difference does the carrot's presence versus absence  above the product sign make? Why is the n on edge of the carot or roof of the product on the former and not on the latter? @Przemek
Comment: I've put carrot to distinguish between two definitions. It isn't standard notation. Placement of $n$ is typographical issue of $\LaTeX$. In finitary case $n$-ary cartesian product could be stated as set of $n$-tuples. Now it boils down to definition of tuple: in first example ($\widehat\prod$) tuple is defined as [$\underline{\textbf{nested ordered pair}}$](https://en.wikipedia.org/wiki/Tuple#Tuples_as_nested_ordered_pairs) $(a,b,c)=((a,b),c)$, in second ($\prod$) [$\underline{\textbf{as a function}}$](https://en.wikipedia.org/wiki/Tuple#Tuples_as_functions) $(a,b,c)=\{(1,a),(2,b),(3,c)\}$.
Comment: Your intuition about vector space is almost correct. For $X_1=..=X_n=\mathbb{R}$, $\ X_1^n=\mathbb{R}^n=\{(x_1,..,x_n):x_i\in \mathbb{R}\}$. But in this context $\mathbb{R}^n$ is simply set of $n$-tuples without vector space structure (e.g. adding vectors or multiplying by scalar).
Comment: Can you give me a finite example of the "nested ordered pair" and the infinite product (the pi looking thing)  "as a function (a,b,c)"? based on some concrete finite example of $S_i$ aka the "
indexed system of sets"? I'm having a hard time understanding the infinite general idea, so I was wondering whether you could give me a finite example or give me a specific indexed system of sets and then compute its infinite product and the "nested ordered pair" and "in second" pi-looking thing "as a function ..... (a,b,c).... @Przemek and show the proof is trivially true for that specific case.
Comment: HELLO @PRZEMEK....
Comment: There are examples:

$S_i=\{1..i\},\ i\in \mathbb{N}_+ $

$\widehat\prod_{i=1}^3S_i=\{((1,1),1),((1,1),2),((1,1),3),((1,2),1),((1,2),2),((1,2),3)\}$

$\prod_{i=1}^{\infty}S_i=\{\{(1,a_1),(2,a_2),..\}:a_j\in S_j,\ j=1,2,3...\}=\{\{(1,a_1),(2,a_2),..\}:a_j\leq j,\ j=1,2,3...\}$


Wicket - Relative Image Path without Mounting Page

Question: I have a little problem with image relative path and not-mounted page in Wicket.
Take this code inside a generic Page (ex MyPage.java):
final DatePicker<Date> date = new DatePicker<Date>("date",true);
date.setButtonImage("img/calendar.gif");

img directory is stored inside WebContent directory.
The image is not correctly showed unless I mount the page in WebApplication.
Without mounting the page, the relative url has to be considered starting from the java class path.
How Can I build the correct image url from WebContent without mounting the page?
Thanks
Answer: If you need the path you can use
date.setButtonImage(UrlUtils.rewriteToContextRelative("img/calendar.gif", getRequestCycle()));

That's how ContextImage works. If your DatePicker would accept another component as the image you could use this class directly.
Answer: After searching on google, I found a simple solution.
ServletContext servletContext = WebApplication.get().getServletContext();
String contextPath = servletContext.getContextPath();

and then
date.setButtonImage(contextPath+"/img/calendar.gif");


Markov chain and martingale: Taking one step to the right or going back to zero

Question:
Consider the Markov chain on nonnegative integers that does the
  following: from any site $x \geq 0$ it jumps to $x + 1$ with
  probability $p$ and to 0 with probability $1 − p$. Is this chain
  transient? Null recurrent? Positive recurrent? You need to prove
  whatever you claim.

Let $S_0=x$ and $S_n=x+X_1+X_2+\dots +X_n$.
$S_n$ is recurrent if $$P(\exists n\geq 1~|~ S_n=0)=1.$$ Furthermore, if the first return time is finite then we have positive recurrence and if it is infinite then we have null recurrence.
I believe that this depends on $p$.
I am interested in $$P(\text{return to x} ~|~ \text{started at x})$$
I am having a hard time determining what I should do next and I was hoping for a bit of help.
I want to try and use a martingale. I am wondering if I need to know the mean and the variance of the Markov chain? I am not really sure how to find either of these.
Thank you for any help you can give to assist me.
Comment: It _ought to be_ clear that the chain is irreducible and recurrent. As to positive or null recurrent, consider finding the expect value to its return to zero using "first step" analysis.
Answer: I'm not sure how to use martingales in this instance but here is one way:
For any $i,j \in \mathbb{Z}_{\geq 0},$ we have $P^{(j+1)}_{i,j}   \geq  P_{i,0} \cdot P_{0,1} \cdot P_{1,2} \cdots P_{j-1,j} =  (1-p)p^j > 0$. Similarly, $P^{(i+1)}_{j,i}>0$. Then all states $i,j$ communicate and so the MC is irreducible.
Next, note that $P^{(n)}_{0,0} = (1-p)\sum^{n-1}_{k=0} {n-1 \choose k}p^{n-1-k}(1-p)^k = (1-p)(1)^{n-1}=(1-p).$ Therefore,$\ \sum^{\infty}_{n=1}P^{(n)}_{0,0} = \sum^{\infty}_{n=1} (1-p) = \infty$ and so the MC is recurrent.
Finally, let $T_i$ be the waiting time for state $i$ and note that $P(T_0=k\ | \ X_0=0)=p^{k-1}(1-p).$ Then it follows that $E[T_0 \ | \ X=0] = \sum^{\infty}_{k=0}kp^{k-1}(1-p)=\frac{1-p}{p} \sum^{\infty}_{k=0}kp^k= \frac{1}{1-p}< \infty.$ So the MC is positive recurrent.
Comment: $P^{(j+1)}_{i,j}$ is the probability to go from $i$ to $j$ in $j+1$ steps.
Comment: No problem. There is actually a theorem that says that a state $i$ is recurrent if $\sum^{\infty}_{n=1}P^{(n)}_{0,0}= \infty$. Also, $P_{0,0} = 1 \Rightarrow i$ is recurrent, but  $P_{0,0} < 1 \not \Rightarrow i$ is not recurrent.
Comment: Thank you very  much. This is really great. I am going to go through this and make sure I understand it all.
Comment: NineteenNinetyNine is the notation $P_{i,j}^{(j+1)}$ the probability to go from i to j? What is the j+1 though? Sorry I am new to probability.
Comment: Oh thank you very much :)
Comment: Sorry one more question... Why does $\ \sum^{\infty}_{n=1}P^{(n)}_{0,0} = \sum^{\infty}_{n=1} (1-p) = \infty$ give recurrence? I thought that we needed to show the probability $P_{0,0}$ was 1 to prove recurrence.
Comment: Oh perfect. Thank you very much. I really want to understand this. I really appreciate your time :)


PHP file upload works with HTTP form but not with Python script

Question: I'm currently achieving a python project in which I need to have a text file uploaded through HTTP to my server.
At first I was using for convenience an HTTP form and the upload has been working fine.
But now, I want it so I don't have to manually select the file and just have my Python script sending the file for me.
But even if the Apache2 logs indicate that a POST Request have been received by the server while using the Python script, the file is not uploaded.
PHP
<?php
$uploaddir = './uploads/';
$uploadfile = $uploaddir . basename($_FILES['filer']['name']);

print_r($_FILES);
echo "
";
echo $uploaddir;
echo "
";
echo $uploadfile;

if (move_uploaded_file($_FILES['filer']['tmp_name'], $uploadfile)) {
    echo "
UPLOAD SUCCESS\n";
} else {
    echo "
ERROR :\n";
}
?>

WORKING HTML FORM
<form action='processing.php' method="post" enctype="multipart/form-data">

      <input type="file" name="filer" accept=".txt" id="file_loader">

      How to pay in bitcoin

      <input type="submit" name="send_file" value="Send File ">
    </form>

PYTHON
This script has been tested with httpbin.org/post and I have received only code 200.
import requests

with open("987654321.txt", "rb") as a_file:

    file_dict = {"987654321.txt": a_file}
    headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0','Origin':"http://www.myurl.com"}
    response = requests.post("http://www.myurl.com/processing.php", files=file_dict,headers=headers)
    print(response.text)
    print(response)

APACHE2 LOGS
Here are the server logs. The first line is a successful upload by the HTML form, the second is an unsuccessful attempt from the python script.
127.0.0.1 - - [04/Jul/2021:09:39:46 +0000] "POST /processing.php HTTP/1.1" 302 562 "http://www.myurl.com/" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"
127.0.0.1 - - [04/Jul/2021:09:40:55 +0000] "POST /processing.php HTTP/1.1" 302 523 "-" "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0"

Can someone indicates me if I need to change something about the script or the php side to have my file uploaded ?
Comment: If you have a redirect response (302 FOUND status), pythons request object may hide the first response and only show the response from the followed redirect which is counter-intuitive for your analysis. Set allow_redirects to false, it defaults to true. This should give you more insights.
Answer: You named your file 987654321.txt, not filer as expected:
import requests

with open("987654321.txt", "rb") as a_file:
    file_dict = {"filer": a_file}
    response = requests.post("http://www.myurl.com/processing.php", files=file_dict)
    print(response.text)
    print(response)


Aplicação sem acesso ao Banco de Dados após publicação no Azure

Question: Publiquei uma aplicação Asp.Net Core 2.1 no Azure, efetuei as Migrations, o banco e tabelas foram criadas normalmente, consigo ver toda a estrutura do banco pelo VS, mas a aplicação não acessa o banco.

Alguém consegue me dizer o que pode estar acontecendo?
Link da aplicação: https://grifo.azurewebsites.net/
OBS: A mesma aplicação em ambiente de desenvolvimento acessa o banco local e funciona perfeitamente
Answer: A solução foi simples. Apesar de subir os arquivos de configurações com a connectionString e as migrações, foi necessário efetuar uma update desse arquivos, via FTP (sem a publicação automática do VS) com a connectionString do servidor do Azure.


Send method as parameter

Question: I just can't find out the way to call methods of a class without using std::bind, because i need to call this method with parameter inside tryfoo.
//simple function
uint16_t getreg(const uint8_t& num)
{
    return 0;
}

Lets suppose that in class ClassItem we have public method
uint16_t ClassItem::getregI(const uint8_t &f)
{
    return 1;
}

function with callable function
void tryfoo (const uint8_t &param, std::function<uint16_t (const uint8_t&)> f)
{
// let's suppose that param= got some other value here
    uint16_t result = f(param);
}

void basefunction(ClassItem &A)
{
   tryfoo (0, getreg); // Why it's OK
   tryfoo (0, A.getregI) // And this's NOT  ?
}

Comment: It's not OK because C++ designers designed it this way.
Comment: What's wrong with `std::bind`?
Comment: Yeah, I don't understand what the issue is. OP, are you simply asking why the syntax `A.getregI` is not equivalent to a `std::bind`?
Comment: @Acorn i misunderstood the way std::bind is working, but now everything  has become clear.
Answer: This is because std::function holds space for one pointer*, and nothing else. Passing just a plain function is possible because it fits in the space of one pointer. This is not possible with a member function from an object because you'd have to store both the pointer to the function and the pointer to the object, and you just can't fit two pointers into the space of one pointer (at least, not safely).
So you have to build a separate object with std::bind and pass the pointer to that in. You could just pass the pointer to the member function without binding to an object like so tryfoo (0, ClassItem::getregI);, but that wouldn't have access to the A object.
*: the details on this is fuzzy, but the overall point still stands.
Comment: @VladSamara or, you can use a lambda instead: `tryfoo (0, [&](const uint8_t &param){ A.getregI(param); });`  `std::bind()` is pretty much deprecated in lieu of lambdas
Comment: `std::function` is not just a pointer. In fact, its size is pretty much unbounded (both at compile time for different implementations and at runtime if you consider the allocations as part of the effective size).
Comment: If `std::function` were just a pointer, why wouldn't we just use function pointers instead?
Comment: @randomusername On the contrary, it actually manages things for you, including allocating memory for whatever state is required/bound. `std::function` was actually designed to simplify that and provide a way to hold any callable. What do you mean?
Comment: @Acorn see my footnote, this is an explanation for a new programmer
Comment: @randomusername Thank you for your explanation, really, now i see how stupid my question is ( my cold makes me dummy)
well,
  auto f = std::bind (&ClassItem::getregI, &A, std::placeholders::_1)
 is a solution for me
Comment: @melpomene because it's more like 2 or 3 pointers conveniently rolled into one place that's much easier to use. It just doesn't promise to manage memory for you like `std::vector` does so you gotta be explicit and manually carve out the extra space with `std::bind` and friends.
Comment: I personally use @RemyLebeau suggestion of lambdas, so much nicer.

Send method as parameter


Ocultar Subitems de um ListView?

Question: Existe alguma forma de ocultar subitems e os seus dados de um listview?
Tentei começar o código com algo assim:
for I := 0 to Form1.LV1.Items.Count-1 do
      begin
            if Form1.LV1.Items[I].SubItems[5] = 'OK' then
      begin

Mais não sei como continuar para EXIBIR somente os SUBITEMS com o TEXTO "OK" ...
Comment: Obrigado pela resposta amigo, na verdade estou quebrando a cabeça aqui....não consegui bolar nada. Imagina o seguinte, eu tenho um LISTVIEW com 3 COLUNAS, nesse LISTVIEW insiro vários registros....quando o REGISTRO é CONCLUÍDO eu uso o BOTÃO para atualizar o mesmo e colocar um "OK" na frente. Preciso que eu tenha um outro BOTÃO, quando eu clique nele, ele "esconda" os REGISTROS sem o "OK" e mostre somente os com o "OK" deu pra entender?
Answer: Infelizmente não há como fazer isso, pelo menos não nativamente.
A ideia que tive para tentar contornar isso foi de guardar os dados em uma lista de strings(StringList) em uma variável global.
Atualização
Faça o seguinte:
{ Declare na cláusula Uses as units StrUtils e Types }
var
  Form1: TForm1;
  StringList: TStringList;   // Variável global

{
   Procedimento responsável por carregar os itens salvos da StringList em um
   Listview declarado por você.
}
procedure ListViewLoadItems(Listview: TListView);
var
I, J: Integer;
Fields: TStringDynArray;  { Na seção Uses declare System.StrUtils e System.Types }
Item: TListItem;
begin
try
ListView.Clear;
for i := 0 to StringList.Count-1 do begin
    Fields := SplitString(StringList[i], #9);
    Item := Listview.Items.Add;
    Item.Caption := Fields[0];
    for j := 1 to high(Fields) do Item.SubItems.Add(Fields[j]);
end;
finally
    StringList.Free;
end;
end;

{
   Procedimento responsável por salvar os itens na StringList.
   NOTA: Só será salvo os itens em que o SubItem[4] não conter a sequência
   ""OK""

}
procedure ListViewSaveItems;

 procedure AddTextToLine(var Line: string; const Text: string);
 begin
    Line := Line + Text + #9;
 end;

 procedure MoveCompletedLineToList(const Strings: TStringList; var Line: string);
 begin
    Strings.Add(Copy(Line, 1, Length(Line)-1));
    Line := '';
 end;

Var
  Tempstr: string;
  I, J: Integer;

begin
StringList := TStringList.Create;
Tempstr := '';
try
for i := 0 to ListView1.Items.Count -1 do
if (ListView1.Items.Item[i] <> nil) and
   (not ListView1.Items.Item[i].SubItems[4].Equals('OK')) then begin
AddTextToLine(Tempstr, ListView1.Items[i].Caption);

for j := 0 to ListView1.Items[i].SubItems.Count -1 do begin
    AddTextToLine(Tempstr, ListView1.Items[i].SubItems[j]);
end;
MoveCompletedLineToList(StringList, Tempstr);
end;
except
// ....
end;
end;

Esse código foi adaptado para a sua situação a partir dessa resposta no SOEn. Para utilizar basta colocar dois botões e chamar os procedimentos, veja um exemplo:
Salvar os itens:
procedure TForm1.BtnSaveitemsClick(Sender: TObject);
begin
ListViewSaveItems; // Salvará os itens
end;

Carregar os itens salvos em um outro Listview:
procedure TForm1.BtnLoadItemsClick(Sender: TObject);
begin
if StringList = nil then exit; // Verifica se StringList é válido
ListViewLoadItems(ListView2);  // Carrega os itens salvos na *Listview2*
end;

Veja essa ilustração:

Ao clicar no botão Save os itens que não conterem no seu SubItem 4 a palavra OK serão salvos.
Agora ao clicar no botão Load será mostrado algo parecido com isto:

Isso deve funcionar para você.
Comment: eu essa opção SplitString não existe no delphi7, o que posso colocar no lugar ?
Answer: Você por usar um TClientDataSet em memória.
Para esconder os registros que você deseja que não apareçam, basta você colocar um filtro no ClientDataSet e então na listView fazer .invalidate, e carregar na visualização somente os registros desejados através do ClientDataSet.
Nesta estratégia você não carrega todos os dados na TListView, todos os dados ficam carregados no CDs. Você transfere do CDs para a ListView somente aqueles que são para visualização naquele momento.
No lugar do CDs, você pode pensar em usar outros recursos, como por exemplo um TList de Objetos, através dele você também consegue fazer um sort se precisar e caminhar nos registros com eficiência de memória e velocidade. Mas te digo que o CDs é completo para você gerenciar os dados da forma que desejar.

Ocultar Subitems de um ListView?


Priors on Taylor Expansion series

Question: I'm wondering what priors can i choose for a Taylor series as follows:
$\theta_{1}+\theta_{2} (y-\alpha) + \theta_{3} (y-\alpha)^2$
What priors should I use for updating these parameters ($\theta_{1},\theta_{2},\theta_{3}$) in a Gibbs sampler to accommodate the conditions on Taylor expansion?
Any help would be appreciated!
Thank you so much in advance
Answer: Note that your problem is a linear model if you assume the noise is Gaussian. To specify the model you should specify this noise. I assume it is Gaussian. Then it sounds natural to use a Normal prior.
I would most simply use a Normal distribution with mean 0 and covariance matrix $I_3$ as a prior. It works like a basic $L_2$ regularization with coefficient 1 (the non-Bayesian equivalent of a prior). It says "all coefficients are around 0 with variance 1".
But before doing so, you must normalize your input ($y-\alpha$) and output (whatever you call it) to be between say [-1;1] (or have mean 0 variance 1) to avoid "scale issues". You may also normalise $(y-\alpha)$ and $(y-\alpha)^2$ separately but it's unnecessary imo.
"Scale issues" happen when for example $y$ has order of magnitude $10^{-6}$ because some choice of units (measuring a microscopic distance in meters for example). Then a natural order of magnitude for $\theta_2$ would be $10^6$ times $\theta_1$ for the same effect on the output. For $\theta_3$ it would be $10^{12}$ times... The covariance matrix of the prior would need to take this into account which I find cumbersome. Normalization hopefully bypass all these problems.
Comment: Thank you for your helpful answer dear @Benoit, how can i normalize $(y-\alpha)$ in a Gibbs sampler?
Comment: I would normalize the dataset before running the sampler. Write $y'=a(y-\alpha)+b$ with the appropriate constants Same for the output. Then run the sampler on $y'$, get samples $(\theta_1',\theta_2',\theta_3')$ and convert to them back to $(\theta_1,\theta_2,\theta_3)$.

Priors on Taylor Expansion series


How to apply phi-functions to matrices?

Question: I am implementing an explicit numerical integration technique, based on readings from Hochbruck and Ostermann (2010). They use the following formulation:
Excerpt from book
Here, the matrix A is a linear estimation of the Jacobian of the differential equation system, and g is the leftover nonlinear terms. It calls for the usage of a phi function, defined:
$$\phi_{1}(z)=\frac{e^{z}-1}{z}.$$
But it says to apply this to a square matrix? I understand the matrix exponential, but how do you divide by a square matrix? Should I multiply by the matrix inverse or divide term-by-term? Or is there another meaning?
Comment: Rather than implementing this yourself, take a look at [ExpoKit](https://www.maths.uq.edu.au/expokit/) for efficient Fortran/Matlab code. It has since been ported to a number of other languages, [e.g. Julia](https://github.com/acroy/Expokit.jl)
Answer: If you are interested in the action of matrix phi-functions to a vector,
then you can also consider to compute the exponential of an extended matrix as shown in Theorem 1 in the paper [Expokit: A Software Package for Computing, Sidje R., ACM Trans. Math. Softw., 24(1):130-156, 1998] and others.
The following Matlab code computes $\varphi_p(tA)c\in\mathbb{C}^n$, i.e., the $p$-th phi-function of $tA\in\mathbb{C}^{n\times n}$ applied to a $c\in\mathbb{C}^n$, where $t\in\mathbb{R}$ is a possible time step, by evaluating the matrix exponential of an extended matrix $X\in\mathbb{C}^{n+p\times n+p}$:
<code>X = A;
X(1:n,n+1) = c;
X(n+1:n+p-1,n+2:n+p)=eye(p-1);
X(n+p,n+p)=0;
expX=expm(dt*X);
phipAc=expX(1:n,end)*dt^(-p); % = phi_p(t*A)*c
</code>
For $p=1$, a small choice of $n$, and a diagonalizable matrix $A$ with nonzero eigenvalues you can compute a reference solution using the eigenvalue decomposition of $A$ as following
<code>[Q,lam] = eig(A,'vector');
philam = expm1(dt*lam)./(dt*lam);
phipAc_ref = Q*diag(philam)*inv(Q)*c;
</code>
(here the auxiliary $\texttt{expm1}$ computes the exponential minus 1 of the given vector and does improve the accuracy when eigenvalues of $A$ are close to zero.)
For further works on the computation of matrix phi-functions for applications in exponential integrators you can also have a look at [Computing the Action of the Matrix Exponential, with an Application to Exponential Integrators, Higham and Al-Mohy, 2011.] or [Algorithm 919: A Krylov Subspace Algorithm for evaluating the $\phi$-Functions Appearing in Exponential Integrators, Niesen and Wright, 2012.]
Answer: These helper functions are defined to avoid the matrix inverse. Thus the function $\phi_1$ can also be applied to singular matrices. Insert the power series of the matrix exponential to find
$$
\phi_1(A)=I+\tfrac12A+\tfrac16A^2+...+\tfrac1{n!}A^{n-1}+...
$$
For efficient evaluation you would use the eigen-decomposition $A=U^{-1}JU$ and then compute
$$
\phi_1(A)=U^{-1}\phi_1(J)U,
$$
where the middle splits according to the Jordan blocks of $J$ and the usual rules for the evaluation of matrix functions on Jordan blocks apply.
More elaborate tricks to reduce floating-point errors and/or computation costs exist.
Comment: Thanks so much for this explanation! I have a few quick follow-up questions if you have time:

1) Is there a general name for these phi-functions? Are there existing functions in matlab that make estimates of phi1?

2) Just to be clear, the best I could do to program such a function would be to estimate it using power series?
Comment: 1) I'm not aware of a name, could be helper or auxiliary functions, or exponential remainder functions, they routinely occur in Rosenbrock and exponential Runge-Kutta methods, there should be some slang for it. 2) Using the power series is inefficient but simple to implement, there should be readily available sources for the evaluation of analytical functions as matrix functions.
Comment: @Moo Take a look at `funm` Matlab function and the references there https://www.mathworks.com/help/matlab/ref/funm.html

How to apply phi-functions to matrices?


Is map $f:\mathbb{D}^2\to \mathbb{S}^2$ that fix $\partial \mathbb{D}^2$ surjective on a hemisphere?

Question: I have a map $f:\mathbb{D}^2\to \mathbb{S}^2$ that fix $\partial \mathbb{D}^2$. It seems intuitve believe that $f(\mathbb{D}^2)=\mathbb{S}^2_+$ or $f(\mathbb{D}^2)=\mathbb{S}^2_-$ (hemispheres north and south), but I could not to prove it.
Could you give me a clue?
Thank you
Comment: I guess that the boundary of the disk is mapped to the equator of $S^2$
Comment: I think the OP is identifying $\Bbb{R}^2$ with a subspace of $\Bbb{R}^3$ in the standard way.
Comment: Can you be a bit more precise about your assumption? What do you mean by "$f$ fixes the boundary of the disc"?
Answer: Suppose there are two points not in the image of $f$, one on the North emisphere and one on the South emisphere. Removing the two points, up to homeomorphism you get a map
$$\hat{f} : D^2 \to S^1 \times \mathbb{R}$$
That maps the boundary to a generator of the fundamental group on the right. This is impossible, because an element of the $\pi_1$ extends to a disk precisely when it is trivial.
On the other side, it is possible to have a surjective map with the constraints you prescribed. Firstly, let me notice there is a surjective function $g: D^2 \to S^2$ that maps the boundary to the South Pole: this is kind of "put a disk on a sphere and then collapse the boundary to the South Pole. A similar one can be done with North Pole, that we call $g_1$.
Now define $f: D^2 \to S^2 $ as:
$$ f(x) = g(2x) ,\quad \|x \| \le 1/2$$
$$ f(x) = g_1 \left ( \frac{x}{\| x \| } \left (\|x\| - \frac{1}{2} \right ) \right ) , \quad 1/2 \le  \|x\| \le 1 $$
You can verify that it satisfy your constraints. Use yous imagination!
Comment: Yes! You got the first argument, which shows that one of the two hemispheres will always be in the image. The boundary is mapped to a one clock tour around the cylinder, which is not homotopic to a constant. On the other hand, an extension to the disk can be thought as an homotopy to a constant: for a fixed radius $r$ you will find the image of the circle at time $r$: at 1 you have the original circle, while at zero you have a constant. The surjection is nicely explained by the other answer: it is the same "sock map" of Rob Arthas!
Comment: There was a mistake! It's on me. Now it should be more manifest how the formula acts: the direction is the same, but the norm is shifted by 1/2.
Comment: Thank you so much! I am reading doing other works (I mean, did not put down the formulas to study), but I imagine that is something like the explanation of Rob Arthan right down, right?
Comment: Please, could you explain me why $\hat{f} $ maps the boundary to a generator of the fundamental group on the right? Thank you so much (I understand the names, but is my first time on algebraic topology).
Comment: It was because the closed path on cilinder is not homotopic to a cnstant? Thank you
Comment: Thank you so much! Sorry by bothering. I have a doubt on second part of formula. When $||x||=1/2$, so $1-2/||x||=-3$ and you get $f(x)=g_1(-6x)$, but $-6x\not\in \mathbb{D}^2$...? Thank you and sorry if I mistaked
Comment: Thank you so much!
Answer: Think of $\Bbb{S}^2$ as the surface of a tennis ball and think of $\Bbb{D}^2$ as a sock of about the same diameter as the tennis ball. Put the tennis ball in the bottom of the sock with the south pole at the toes and tie a piece of string around the sock so that it neatly traps the whole tennis ball with the knotted string at the north pole. Now fold the rest of the sock into tucks so that the edge fits snugly around the equator of the tennis ball (if you choose a short enough sock you will only need one fold). This gives you a picture of a surjection of $\Bbb{D}^2$ onto $\Bbb{S}^2$ that identifies $\partial\Bbb{D}^2$ with $\Bbb{S}^1$ in the standard way.
Comment: It's a really nice manner to explain, thank you so much


Logarithm of matrix map?

Question: I am trying to calculate the following
$K^{-1}DK^{-1} * p$
Where $K$ is symmetric positive definite and $D$ is positive diagonal and $p$ is a vector. The problem is that p is very, very small so its rounded to zero by Matlab so I want to take the log of of the calculations of $p$ so that I can actually calculate the map above. However if I take the log I need to transform it back somehow.
Can this be done? $K^{-1}$ will naturally contain negative entries.
Comment: Why not considering a suitable scalar multiple of $p$ instead of the logarithm of the components?
Comment: Yea. That seems to be it. P in my case is a probability so I do negative log transform and then add the min value to exponential transform. At least I get some non-zeros.
Answer: Short answer: using logs won't really help.  Instead, you should multiply $p$ by something large so that matlab can deal with it, and then divide out in the end.
Long answer:There is a $\log$ for matrices, but it doesn't behave quite the same as $\log$ for numbers, and so it's not quite suitable for computations in the way you think it is.
From Taylor series, we have that $e^x=\sum x^n/n!$ and $\log(1+x)=\sum (-1)^n x^{n+1}/(n+1)$.  The fist formula holds for all $x$, the second for $x$ that are close to $0$, and they define inverse functions where they are defined.
Similarly, we can define a matrix $\log$ and matrix exponential by means of the exact same  power series.  $e^A$ will converge for all $A$, and $\ln(I+A)$ will converge for small matrices (take any linear norm on matrices such that $||AB||\leq ||A|| ||B||$, and the condition $||A||<1$ will work).
Unfortunately, these do not obey all the properties that you want.  For example, $e^{A+B}=e^A e^B$ only when $A$ and $B$ commute with each other.  When they don't commute, the Campbell-Baker-Hausdorff formula says what $\log(e^A e^B)$ equals.  Because of this, we do not in general have that $\log AB=\log A + \log B$, even when everything is defined.
Even ignoring this problem, we would still need to define the $\log$ of a vector.  I honestly don't know where one would begin, other than to take $\log$ of the individual entries, which wouldn't have any immediately useful properties as far as matrix actions are concerned.
If the problem is that Matlab has rounding errors, then you can exploit the fact that matrix multiplication is a linear operator and just multiply $p$ by some large constant $c$, do the calculation, and then divide by $c$ again.  Additionally, if you have problems with $D$ or $K$ being too small/large you can do the same to them.
Answer: It seems like it can't be done. If the $K^{-1}DK^{-1}$ is a m-by-n matrix then the map can be seen as just $m$ scalar products. However the log of a scalar product can't be tampered with since $\log(a \cdot b) = \log(a_{1} b_{1} + a_{2} b_{2} +...)$. Further on if we take the log of the individual elements we get $\log a_{1} \log b_{1} + \log a_{2} \log b_{2} +...$ which in this case is not feasible since $b$ in our case contains negative elements.
Even if $b$ just contained positive element there's no easy way of transforming it back since in general
$\log(a_{1} b_{1} + a_{2} b_{2} +...) \neq \log a_{1} \log b_{1} + \log a_{2} \log b_{2} +...$
and we thus can't take the exp to transform back.
Comment: Use $\backslash\!\!\log$.
Comment: When, where and how?
Comment: When: Each time you use log in a formula. How: Type backslash-l-o-g instead of l-o-g. I did it for you at the first occurrence in your answer.

Logarithm of matrix map?


Is my understanding about this dynamics correct?

Question:
Sorry if my question is too simple or obvious to the experienced piano players,
so the piece starts with the dynamics "f" and "marcato",
f - loud
marcato - stressed, pronounced
then towards the bottom of the image it transitions to a "p"
p - soft and quiet
so I know dynamics continues indefinitely until stated otherwise but I have the following questions

What exactly does it mean to play it "marcato"; stressed and pronounced here? You're already playing "f" loud and accenting many notes, do you give extra oomf to the non-accented notes as well?

Is the "f" and "marcato" tied together? When you reach the "p" you obviously stop playing "f", but there is nothing to negate the "marcato", I feel like it's implied "marcato" is discarded with the "p" because "marcato" doesn't exactly fit with soft and quite, but I just wanted to ask to be sure
Answer: Marcato is not a dynamic so much as a "feel", so marcato and p are not mutually exclusive.
A fair interpretation of marcato is to be particularly precise and pronounced rhythmically, and it also could be taken to mean that each note should be (over-)articulated.
For this exercise, I take marcato to apply to the accented octaves in the left and right hands, but the non-accented octaves and chords should be played in a less march-like way. The accent marks seem to be very specific. It seems as though Berens wants one to start out overdoing the pulse in both hands, then just in the "scale" hand, and then become more flowing in both hands. The process repeats itself when the hands switch roles. With the technical "practice" section out of the way, the legato p section focuses more on employing scale technique musically.


module import in loop python, value not updating

Question: I have a file that contains:

Hello There

    This is an example of a pml file

    <pml>
    def f():
        return "
First PML block
"
    pml = f()
    </pml>

Here is another paragraph

    <pml>
    def g():
        return "
Second PML block
"
    pml = g()
    </pml>

This is the last paragraph.

    <pml>
    def h():
        return "
Third PML block
"
    pml = h()
    </pml>

I'm writing a python script that will produce an output file that replaces the pml blocks with the result of the code in the block.  I want the output file to look like:

Hello There

This is an example of a pml file

First PML block

Here is another paragraph

Second PML block

This is the last paragraph.

Third PML block

Here is my script:
import sys

#define main

def main(argv):
    #make sure usage is proper
    if len(sys.argv) != 3:
        print 'Usage: pmlparser.py <input_file> <output_file>'
    else:
          #get the filenames
          inputfilename = sys.argv[1]
          outputfilename = sys.argv[2]
          #open the files
          with open(inputfilename,"r") as inputfile, open(outputfilename,"w") as outputfile:
                  for line in inputfile:
                         if not "<pml>" in line:
                               outputfile.write(line)
                         else:
                               pmlfile = open("pmlcode.py","a")
                               for line in inputfile:
                                       if not "</pml>" in line:
                                              pmlfile.write(line[1:])
                                       else:
                                              #I think the problem is somewhere in here
                                              pmlfile.close()
                                              import pmlcode
                                              outputfile.write(pmlcode.pml + "\n")
                                              break

if __name__ == "__main__":
   main(sys.argv[1:])

Unfortunately, I get the following as my result:

Hello There

This is an example of a pml file

First PML block

Here is another paragraph

First PML block

This is the last paragraph.

First PML block

I am iteratively importing pmlcode.py, and trying to write the new value for pml to the outputfile.  However, this script keeps writing the original value for pml to the output file.
I've tried the following as a test:
a.py:
def f():
    return 1
var = f()
def g():
    return 2
var = g()

b.py:
import a
print a.var

$> python b.py
2
I just started learning Python yesterday, so I apologize if my question sounds dumb or if I'm misunderstanding something.  I appreciate any help!
Comment: Not a bad code for someone who started to learn Python yesterday :)
Comment: Importing a module a second time doesn't do anything.  What is the nature of your `pmlcode` file?
Comment: Stephan: I've already given expected output.
Comment: do you have to do pml = f(), pml = g(), pml = h() every time? or do you care that the variable be named differently
Answer: import is not the tool you want here. As you discovered, once you've imported a module, you always get the same module; Python caches it in memory for performance. The problem, in other words, is not that pmlcode.py isn't getting updated, but rather that it is only actually read the first time import pmlcode is executed.
Since you already have a string, why write it to a file and then import it? Just use exec directly on the string.
Comment: Additionally, you can pass an explicit `globals` dictionary to the `exec` statement and read the value of `pml` out of the dict after the `exec`.
Comment: kindall,
Thanks for the response. I'll give the exec thing a try! Like I said, I just started using Python for the first time yesterday.  I haven't really seen the exec method/function/keyword/whateverit'scalled until now.
Comment: Just had to say: you can use imp.reload to get changes made to an already imported module.
Comment: Yes you can reload modules... still, writing a string to the file and then importing the file is the long way 'round.
Comment: If I import imp and pmlcode at the very top of my script, and replace the previous line that said "import pmlcode" with imp.reload(pmlcode), I still get the same incorrect output....:(
Comment: Part of the issue here is that the requirements of the project state that variables/functions in one pml block must be available for reuse in subsequent pml blocks.  Will exec() be able to use variables/functions that were referenced in a previous loop iteration?
Comment: Sure, if you use the same dictionary for globals each time.
Comment: So I can build a string up over the loop iterations that contains all of the code in the pml blocks, and then call exec on that string....
Comment: I got it to work by building the pml string up, calling exec on it, and writing the pml variable to the outputfile.
Thanks very much for all the help!
I would like to learn the globals() dictionary method, but I can't seem to understand the usage of exec() and globals() from the Python docs.


In an interrupted cricket match, how D/L Method is calculated?

Question: We all know that, for making result in cricket match both the umpair goes for the help of D/L method, but how this is been calculated,?  please anybody answer this
Comment: I'm voting to close this question as off-topic because this information is well-stated in the official documentartion, and any sufficient answer will necessarily refer to it, or be too long for Stack Exchange.
Comment: I agree the answers are in the official documentation - I think adding a link to that documentation might be helpful, even if the question is closed, so I've done so.
Answer: Have a look at the ICC regulations on Duckworth-Lewis - available as a PDF from here. There's also a FAQ, although I haven't read it, available from the same site, here.
Comment: Would be good to add an extract also rather than just having links. If they decide to change the URL then the links will become broken
Comment: That's a fair point, but it's tough to extract a pertinent section when the question is so broad. It really needs the user to read the entire link.


How can I skip the current password validation on the user form?

Question: My site requires that I turn off the current password validation on the Account Edit Form when changing / specifying a new password.
In D7 I accomplished this via a hook_form_alter implementation.  With Drupal 8's new form api I am unsure of how I can make this alteration.  hook_form_alter still exists, but the values in the form[#validation] seems to be used differently.
I see in the existing form code that a 'password reset' does exactly what I would like to do, but it's unclear how I can make this modification outside the actual AccountForm Class, or the ProtectedUserFieldConstraintValidator implementation of ConstraintValidator.
Thanks
Comment: Of course in general you wouldn't want to do this.  I am however writing a Federated login module that externally authenticates.  And without going into great detail, being able to change / create a password is part of the functionality required.
Comment: Why would your site require that? You know it's a security risk, right?
Answer: A working code example for this is the No Current Password module.
Here's the relevant code:

/**
 * Implements hook_form_FORM_ID_alter().
 * Remove the current password field from the user_profile_form form (user/%/edit).
 */
function nocurrent_pass_form_user_form_alter(&$form, &$form_state) {

  // Get the module's configuration
  $config = \Drupal::config('nocurrent_pass.settings');
  $nocurrent_pass_disabled = $config->get('nocurrent_pass_disabled');

  // If the Current password is disabled
  if ($nocurrent_pass_disabled) {

    $account = \Drupal::service('current_route_match')->getParameter('user');

    // If this is not the Superadmin's edit page
    if ( !empty($account) && ($account->id() != 1) ) {

      // Turn off Current password field's validation
      $form_state->set('user_pass_reset', 1);

      // Hide the Current password fields
      $form['account']['current_pass']['#access'] = FALSE;
    }
  }
}

Answer: $account->_skipProtectedUserFieldConstraint is a flag that is designed to prevent this validation.
The trick is setting it at the right place. Not exactly sure what works, since the code in AccountForm::buildEntity() always sets it to either TRUE or FALSE. Maybe we need to change that to only set it to TRUE if it's TRUE and not force it to FALSE. Then you could use an entity builder callback (see the last line in menu_ui_form_node_type_form_alter() for an example).
What you can try is setting $form_state->set('user_pass_reset', TRUE) altough that could possibly have side effects.
Comment: Thanks so much, let me investigate the info you provided and I'll let you know what happens.
Comment: I think setting user_pass_reset works well enough.  Maybe not ideal since it wasn't meant for this.  But good enough for now I believe. Like you said, making that flag in AccountForm more flexible would be a nice enhancement.  A lower priority one though.


YARN shell command to get number of containers and vcores used by running applications

Question: hadoop job -list only seems to show mapreduce jobs, but my resource manager UI shows things like "Allocated CPU VCores" and "Allocated Memory MB" for all jobs running on YARN (including things like Spark and Tez).
How can I get these results via the command line instead of going to the UI?
Answer: YARN supports different types of applications. "MapReduce" is one type of the application supported by YARN. If you use hadoop job (which is deprecated, you should use mapred job instead) or mapred job, you can only manipulate MapReduce jobs.
To view the status of the different types of applications (mapreduce, spark etc.), you should use YARN CLI.
For e.g. "yarn application -list -appStates ALL", lists the status of all the applications. The output of this command contains a column "Application-Type" which indicates the type of the application (for e.g. MAPREDUCE for MapReduce application).
You can get the details about the application, application attempt, containers etc. The commands for 2.7.1 version of Hadoop are given here: https://hadoop.apache.org/docs/current/hadoop-yarn/hadoop-yarn-site/YarnCommands.html.
But, these commands don't return the details like "Allocated CPU VCores" and "Allocated Memory MB", through CLI.
yarn application -status {Application ID} command returns "Aggregate Resource Allocation" in terms of "MB-seconds" and "vcore-seconds"
For e.g. -status for one of my applications returned:
Aggregate Resource Allocation : 12865641 MB-seconds, 1041 vcore-seconds
Comment: Then how does the resource manager UI get the VCore and Container info?   It looks like maybe my only method would be to scrape from the UI itself.
Comment: What I meant is, the information is available. But, in CLI only limited amount of information is being shown. In printApplicationReport() method, "ApplicationResourceUsageReport" is fetched. This contains information about vCores, Memory etc. But, only "MB-seconds" and "vcore-seconds" are printed. Maybe its because its difficult to show all the information on the CLI compared to GUI.


How many (parallel) harmonies can we have in a song without one harmony being an octave apart from another?

Question: I usually can only come up with two separate harmonies for a song, the ones I came up with after that are usually an octave higher or lower than those two harmonies.
Comment: Are you limiting yourself to triads, like major and minor chords, in your harmony? If so, then yes - there are only three notes in each octave for those chords, so your third added harmony note must be a repetition of an existing note in another octave. If you allow more complicated chords (e.g. 7ths and 9ths) then you can have more notes before you 'repeat yourself', though of course it jazzes up the sound of your harmony. (I hope I understood correctly!)
Comment: @topomorto I don't think the question is about chords at all.  I think it's about harmonizing a melody with a parallel melody.
Comment: @RockinCowboy I meant chords in the sense of what the resultant harmony of those parallel lines is.
Comment: @topomorto  ahhhh - I got ya.
Answer: 4 voices are common.  The classic "Glenn Miller" sound had the melody doubled on tenor sax and clarinet an octave apart, with the three other saxes harmonising within that octave.  So that's 4 independent parts.  Also look at the "locked hands" piano style associated with George Shearing.The Baroque composers regularly wrote 4-part counterpoint (that's even cleverer than just adding parallel harmony lines) and sometimes demonstrated they could manage more!  Even a simple hymn tune is normally harmonised in 4 parts - and the aim is to give each voice a coherent melodic line to sing.
Comment: "Hymn tune" can cover a multitude of styles and textures of course.  But I think you knew I was talking about Bach chorale-style SATB writing, where consecutive 5ths, let alone octaves, are banned!
Comment: It's difficult to understand exactly what is being asked here.  But you answered one of the possibilities.  Chords were not mentioned in the question.  Two voices (notes) can certainly harmonize with one another and it seems that may very well be what the question is about.  +1
Comment: If the 'harmonies' are doubled, an octave apart, that's not what the question is about. Hymn tunes often have a doubled harmony, an octave apart.
Answer: Taking that a nominal chord will have 3 notes - root, 3rd and 5th, before getting to the octave of the root, it's 3. Even with first or second inversion. However, 2nds (or 9ths), 6ths and some sort of 7ths will also fit with the original triad. Not all at the same time, but 1,2,3,5 works. As does 1,3,5,6, or 1,3,5,7. Going over the octave, but not duplicating notes, there's a 9th chord harmony, with 1,3,5,7 and 9. Of course, the 3s can be major or minor, the 5ths P, # or b, the 7ths major or minor, and the 9ths # or b. Then there's 11ths and 13ths. So, quite a few, and I expect I've missed some. Take a look at some guitar chords, with close voicing especially, for ideas.


Como pegar o próximo atributo de uma array de objetos?

Question: Boa noite galera, então, to travado num código aqui. Eu tenho uma array de objetos, e irei receber um nome de entrada, que se encontra em um dos objetos "display" do array abaixo, por exemplo "Márcio Magalhães".
Após isso, eu preciso procurar o display e o nome do objeto seguinte a esse, por exemplo nesse caso seria "Alex Jones" e "F1207158".
const list_members = {
  "members": [{
    "value": "646d70b7f6974459b188174aa8a564fe",
    "type": "User",
    "display": "Márcio Magalhẽs",
    "name": "B133232",
    "$ref": "examplefortest.com"
  },
  {
    "value": "646d70b7f6974459b188174aa8a564fe",
    "type": "User",
    "display": "Alex Jones",
    "name": "F1207158",
    "$ref": "examplefortest.com"
  },
  {
    "value": "646d70b7f6974459b188174aa8a564fe",
    "type": "User",
    "display": "Bibiana Fonseca",
    "name": "G123454",
    "$ref": "examplefortest.com"
  }]
}

Minha dúvida é como pegar esses dados, tentei começar implementar algo assim mas não consegui progredir
const { members: [display, name]} = lista_membros.find(({members})) => members.includes("Márcio Magalhães"));

Answer: Podes usar o .findIndex para procurar no array o índice desse nome/display que tens. Depois é só acrescentar +1 e tens o índice do próximo.
Atenção que quando chegares ao fim dessa array (quando não houver próximo) a função vai retornar undefined.

const list_members = {
  "members": [{
      "value": "646d70b7f6974459b188174aa8a564fe",
      "type": "User",
      "display": "Márcio Magalhẽs",
      "name": "B133232",
      "$ref": "examplefortest.com"
    },
    {
      "value": "646d70b7f6974459b188174aa8a564fe",
      "type": "User",
      "display": "Alex Jones",
      "name": "F1207158",
      "$ref": "examplefortest.com"
    },
    {
      "value": "646d70b7f6974459b188174aa8a564fe",
      "type": "User",
      "display": "Bibiana Fonseca",
      "name": "G123454",
      "$ref": "examplefortest.com"
    }
  ]
};

function getUserAfter(display, arr) {
  const startIndex = arr.findIndex(obj => obj.display === display);
  return arr[startIndex + 1];
}

console.log(getUserAfter("Márcio Magalhẽs", list_members.members));


Number of ways in which two elephants can be chosen out of $n$ elephants.

Question: Question:
In how many ways can two elephants be chosen out of $n$ elephants ?
Answer:
Clearly, it is  $^nC_2$.
My solution:
Let's choose an elephant . This can be done in $^nC_1$ ways. Now, to complete the selection , we have to take one more elephant out of left $(n-1)$ elephants. This can be done in $^{n-1}C_1$ ways. Hence, by multiplication rule of counting, total number of ways = $(^nC_1)  . (^{n-1}C_1)$ .
Problem:
However, $^nC_2≠ (^nC_1)  . (^{n-1}C_1)$ . So , where did I go wrong ?
Thanks. I am a beginner in permutations and combinations so forgive if I am doing a very basic mistake.
Comment: Divide by $2$, because the order in which the elephants were chosen is not important.
Comment: First taking elephant A and then elephant B is same as first taking elephant B and then elephant A. I would suggest you strongly embed this concept of whether 'order' matters or not in your mind. It is useful in further study of combinatorics.
Comment: also notice that your method works fine for permutations : $^nP_1 \times ^{n-1}P_1 = \frac{n!}{(n-1)!}\times\frac{(n-1)!}{(n-2)!}  = \frac{n!}{(n-2)!} =\space  ^{n}P_2$
Comment: @insipidintegrator Oh thanks. Now I get my mistake . According to my step, I counted Elephant-A and Elephant-B twice which is wrong. However, I am sorry that I couldn't figure it.
Comment: @kabenyuk  At first sight I didn't understood your comment but now I do. Thanks very much
Comment: Oh wow. Means it works on permutations but not on combinations. Thanks
Answer: Hello this is actually a good question if you are a beginner, the way I solved your question is, just assume 4 elephants (n=4).
$\\$No. of ways you can select 2 elephants = $^{4}C_{2} = 6$ ways.
Now let's solve this question by the way you told, acc. to you, first we select one elephant, that is $^{4}C_{1}$, now select another elephant out of remaining (3) elephants, so that'll be $^{3}C_{1}$, so total no. of ways we can select elephant = $^{4}C_{1}\cdot^{3}C_{1} = 12$ ways.
Let's analyze the mistake we did here, let's name the elephants E1, E2, E3 and E5.
Case-1: Let's select E2 first and than E3.
Case-2: Let's select E3 first and than E2.
See, in both the cases, we've actually selected 2 elephants but order differed there, but technically we don't see the order in selection process, and so both are same ways. and that's why 2 cases have been clubbed into 1, that's what the mistake we did in selection in two steps (the method that you told), what would have happened while selection is in one case we might have selected E2 first and than E3, and in another case we might have selected E3 first and than E2, and both are same things because we're just doing selections. so yeah this thing would have happened with all the cases formed, so we've to divide our answer by 2 to get those all same cases clubbed into one. Hope you might have got what I wanted to say.
Let's solve the mathematical equation now that you told.
$^{n}C_{2} = \frac{({n}C_{1}\cdot^{n-1}C_{1})}{2}
\\ \frac{n!}{2! \cdot(n-2)!} = \frac{(n)(n-1)}{2}
\\ \frac{(n)(n-1)}{2}=\frac{(n)(n-1)}{2}
\\ LHS = RHS$
I hope you might have got why'd we divided by 2 in your method.
Comment: Yes thanks . I got it !


How does using an inboud NAT help in terms of security?

Question: When creating a VM, I didn't give it a public ip address to RDP into it since that won't be secure. I made a public load balancer and added the VM to the backend pool and created an inbound nat rule with the VM as the target using port 3389.
How does using an Inbound NAT in Load balancer help in terms of security?, since it would forward all RDP's to our VM anyway making it feel as if the public ip of the load balancer is the public ip of the VM itself?
Answer:
How does using an Inbound NAT in Load balancer help in terms of security?, since it would forward all RDP's to our VM anyway making it feel as if the public ip of the load balancer is the public ip of the VM itself?

It doesn't. NAT is not about security; it's about network addressing.
For filtering traffic, we have firewalls.


WPF: bind property from MainWindow.xaml cause an error

Question: So I have this view model:
public class WiresharkFiles : INotifyPropertyChanged
{
    public ObservableCollection<WiresharkFile> List { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
    private bool _inUse;
    private int _packets;
    private bool _hasItems;

    public WiresharkFiles()
    {
        List = new ObservableCollection<WiresharkFile>();
        HasItems = false;
        List.CollectionChanged += List_CollectionChanged;
    }

    private void List_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        HasItems = List.Count > 0;
    }

    public bool InUse
    {
        get { return _inUse; }
        set
        {
            _inUse = value;
            NotifyPropertyChanged("InUse");
        }
    }

    public int Packets
    {
        get { return _packets; }
        set
        {
            _packets = value;
            NotifyPropertyChanged("Packets");
        }
    }

    public bool HasItems
    {
        get { return _hasItems; }
        set
        {
            _hasItems = value;
            NotifyPropertyChanged("HasItems");
        }
    }

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

MainWindow.xaml
private WiresharkFiles caps;

public MainWindow()
{
    InitializeComponent();
    caps = new WiresharkFiles();
}

Window.Resources
<Window.Resources>
   <Convertors:CollectionHasItemsConverter x:Key="CollectionHasItemsConverter"/>
</Window.Resources>

Converter
public class CollectionHasItemsConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return (bool)value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

And base of my collection item (empty or not) i want to enable/disable my Button:
<Button Name="btnDeleteAll"
        Click="btnDeleteAll_Click"
        IsEnabled="{Binding Path=(caps.HasItems),Converter={StaticResource CollectionHasItemsConverter}}">

And i got this error:

XamlParseException: Type reference cannot find type named
  '{http://schemas.microsoft.com/winfx/2006/xaml/presentation}caps'.
Comment: In addition to the caps being a private variable and data context not set, the round brackets in the binding is the problem.
Answer: I don't see where you're associating your DataContext with the caps property.
Make sure you have a public property because the WPF engine isn't running from within your class and won't be able to access the private WiresharkFiles caps; variable. Try the following:
private WiresharkFiles caps;
public WiresharkFiles Files { get { return caps; } }

with a corresponding
public MainWindow()
{
    InitializeComponent();
    caps = new WiresharkFiles();
    DataContext = Files;
}

Your XAML will then bind to Files as follows
IsEnabled="{Binding Path=HasItems}"

Update You'll need to have a look at implementing and binding to commands for the button which will make it a lot better. Look at this article for info on implementing and dealing with commands.
Comment: @ShivaniKatukota is on to something, try running it without the converter to see. In this case it isn't necessary to use a value converter if the source and target type are both `bool`
Comment: Where i need to put this: public WiresharkFiles Files { get { return caps; } } ?
Comment: Next to the definition of caps would be fine. Let me update my answer to show it
Comment: No error now but the button is enable although the list empty and i can see that my converter not even executed
Comment: "HasItems" is already a bool value.. why do you need a converter?
Comment: OK i have change it and the button still enable
Comment: I tried your code with the changes @Fabulous mentioned and removed the converter, it works fine!
Comment: What i need to do with the link you gave me ? why is good for my purposes ?
Comment: If it's working as you said then you can do without it, but otherwise you can bind buttons to commands using MVVM as they are designed to handle the enabling and disabling of buttons/menus etc. and other command related issues such as execution when the command is invoked
Answer: caps is a private variable:
private WiresharkFiles caps;

In order to bind, it would have to be a public property:
public WiresharkFiles caps {get;set;}

You would also have to set the datacontext of the window to itself. Something like:
this.DataContext = this;

or
In your window tag put:
 DataContext="{Binding RelativeSource={RelativeSource Self}}"

I don't see how this relates to your initial question but you can use dot notation in binding.
You can bind:
{Binding AnObservableCollection.Count}

And you can compare that to 0 in a datatrigger. With a button and a bound command if you want to disable it then I'd use the canexecute of icommand and return false if you have no entries or whatever your logic is.
Comment: No error now but the button is enable although the list empty and i can see that my converter not even executed
Comment: `Path=(caps.HasItems)` is also invalid here (the parentheses have a different meaning than what OP thinks), it should be `Path=caps.HasItems`,
Comment: This is what i have: IsEnabled="{Binding Path=HasItems}"
Comment: Can i move this.DataContext = this to XAML ?
Comment: You can bind the datacontext of the window to itself. I added that to my post above.
Comment: The thing is that i have several collections that i want to bind (each of them is WiresharkFiles  object) to different Buttons so i can user this DataContext 3 times ? or maybe i need to use another approach ?
Comment: I don't follow why you'd be binding a collection to a button. That doesn't make any sense.
Comment: In my application i have several ListView, each of them hold MyData collections that inside specific class, and one of the properties of this is HasItems
Comment: Maybe i need to try another approach ?
Comment: Still don't really see how your observablecollections relate to a button and this description. I don't understand what you're trying to do.
Comment: if my collection is empty i want my button will be disable, so i try to find a way how to hold 3 instances of my  view model that in each view model there is a collection with MyData


Rounding - best way how to do it?

Question: I have the situation that I have floats (like 1.122, 1.3232, 1.22222) I and need to round it always in line with math rules to 2 decimal places: up if third is 5 or higher, down is third is lower than 5.
Like this (before round -> after round):
1.123 -> 1.12
1.1250 -> 1.13
1.0050 -> 1.01
1.0000 -> 1.00
1.00001 -> 1.00

(always two after ".")
Which way in Python is the best for this?
I tried with round function, formatting, numpy, decimal and always sometimes it failed. Hint appreciated !
Comment: "I tried with round function, formatting, numpy, decimal and always sometimes it failed." How did it fail? When did it fail?
Comment: I downvoted this question because OP refuses to clarify whether they want to round or format the numbers.
Comment: Does this answer your question? [How to round numbers](https://stackoverflow.com/questions/27576691/how-to-round-numbers)
Comment: What do you mean sometimes it failed? Why does none of the functions you mentioned work for you?
Comment: A ``float`` has no concept of "decimal places". The numbers ``1.0000``, ``1.00`` and ``1.`` are exactly the same value. Are you trying to *format* the number, e.g. for printing or writing to a file?
Comment: Unfortanelley not - I tried this ones
Answer: There is a function "round" that would be useful:
round(1.123, 2) -> 1.12
round(1.12345, 3) -> 1.123
UPDATE
If you want to get arithmetic rounding, you can use math package and your own function:
import math
def arithmetic_round(val):
    x = 0
    if (float(val) * 100 % 1) >= 0.5:
        x = math.ceil(val*100)/100
    else:
        x = math.floor(val*100)/100
    return format(x, '.2f')

But timgeb is right - 1.0050 is 1.004999 in Python terms.
That is why I would suggest to change 0.5 to 0.49:
import math
def arithmetic_round(val):
    x = 0
    if (float(val) * 100 % 1) >= 0.49:
        x = math.ceil(val*100)/100
    else:
        x = math.floor(val*100)/100
    return format(x, '.2f')

arithmetic_round(1.123) -> 1.12
arithmetic_round(1.1250) -> 1.13
arithmetic_round(1.0050) -> 1.01
arithmetic_round(1.0000) -> 1.00
arithmetic_round(1.00001) -> 1.00

Comment: round(1.125, 2) -> 1.12
And I need to:
1.125 -> 1.13

Rounding - best way how to do it?


passed parameter not working on xpath, although working in unit test

Question: class ad_xml_model extends \ITico_core\common_class {

    public function get_info_from_ad($input_id) {
        $xml_file = parent::get_set('xml');
        $ad_info = $xml_file->xpath("//ad[@id='" . $input_id . "']");
        parent::get_set('ad_info', $ad_info);
        return $ad_info;
    }

}

The above code works when unit testing it, i've tried passing both a string and a int into the function and it works both times.

get_info_from_ad

test data: 1

info from ad:-

<?php
$ad_xml_model->get_info_from_ad(1);
$ad_info = $ad_xml_model->get_set('ad_info');
print_r($ad_info);

but in the code below when being called from the controller its not working
class main_controller(){
    $ad_top_limit = count($ads_from_category);
    $key = rand(0, $ad_top_limit - 1);
    $chosen_ad = $ads_from_category[$key];
    parent::get_set('chosen_ad', $chosen_ad);
    $ad_info = $ad_xml_model->get_info_from_ad($chosen_ad);
    parent::get_set('ad_info', $ad_info);
    if ($ad_info != null) {
        switch ($ad_type) {
            case NUll:
                break;
            case 'long':
                $long_view = new long_view($ad_info);
                $long_view->show_ad();
        }
    }
}

and the debug page testing
<?php
echo $main_controller->get_set('chosen_ad') . "
";
?>
ad information:-:

<?php
print_r($main_controller->get_set('ad_info'));

screenshot of the debug page
I've went every step of the way echoing all the variables to check that they aren't null but for some reason the xpath just doesnt work when being called from the main controller but it works fine from the unit test even though the exact same parameters are being passed.
Answer: I found solution was to implicitly tell the xpath it was an int
public function get_info_from_ad($input_id) {
    $xml_file = parent::get_set('xml');
    $ad_info = $xml_file->xpath("//ad[@id='" . (int)$input_id . "']");
    parent::get_set('ad_info', $ad_info);
    return $ad_info;
}

for some reason if I set it to a string it still doesn't work, perhaps someone else could share light on this.


Erro de syntax sql

Question: Estou tentando fazer um insert no banco de dados mais estou com um problema de syntax, ainda não consigui identificar o erro.
$sql='INSERT INTO tabela
    (
        nome,
        nit,
        rg,
        cpf,
    )
    VALUES';

foreach($dados as $linha_bd)
{
$sql=$sql."('"
'".$linha_bd['nome']."',
'".$linha_bd['nit']."',
'".$linha_bd['rg']."',
'".$linha_bd['cpf']."')";

}
Comment: Remova a virgula em `cpf,`, para concatenar strings use `.=` no lugar de `=` que faz uma nova atribuição a cada volta do foreach.
Comment: Meu caro, estou com outro problema agora, o erro mudou desta vez, (SQL inválido: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(
'00000439020154036304',
'0006660000',
'MARCOS DE ALMEIDA',
'08782952833',
' at line 44)
Answer: Remova  a virgula após o cpf
$sql = 'INSERT INTO tabela(nome, nit, rg, cpf,) VALUES ';
 --------------------------------------------^

A concatenação aqui está invertida
$sql=$sql."('"'".$linha_bd['nome']."','".$linha_bd['nit']."','".$linha_bd['rg']."','".$linha_bd['cpf']."')";

errado:
$sql."('"'".$linha_bd['nome']."'
-----^   |
abriu    |
         |essa aspa simples ficou da concatenação

Para eliminar o erro de sintaxe do php pode fazer assim:
$sql .= "('{$linha_bd['nome']}'"

Outra forma de corrigir o erro de sintaxe com sprintf, use os parâmentros adequados para cada tipo de dados %s é para strings.
foreach($arr_entrada as $linha_bd){
    $sql .= sprintf("('%s', '%s', '%s', '%s'),", $linha_bd['nome'],$linha_bd['nit'], $linha_bd['rg'], $linha_bd['cpf']);
}
$sql = substr($sql, 0, -1); //remove a ultima virgula se o objetivo é gerar um insert com varios VALUES

phpfiddle - exemplo
Comment: Obrigado meu caro, agora está funcionando.

Erro de syntax sql


Azure - Fault / Update Domain

Question: Im struggling to understand the concept of Fault / Update Domain in Azure, any help would be appreciated.
I have provisioned 2 VMs under the same Availability Set with 2 Fault Domain and 2 Update Domain respectively :
<code>                             Fault Domain           Update Domain
server_iis1                        0                        0
server_iis2                        1                        1
</code>
As i know ,Fault Domain in reality is a rack, however the following points confuses me :

If I loose entirely Fault Domain 0 ,then server_iis1 is gone ?
Where the 2nd instance of server_iis1 is located that should be up and running in case of a rack failure(Fault Domain 0) that will provide me with 99.95% SLA ?

Likewise, for server_iis2 , if i loose Fault Domain 1 ,how server_iis2 is supposed to be up and running ?

Server_iis1 & Server_iis2 are completely 2 different VMs with diferrent data and customers to serve.
Answer: Fault domains are designed to protect your workload (applications running on a vm) in case of unexpected hardware failures. In your example above iis1 and iis2 are two separate VMs running in the same physical datacenter (availability zone)but are in different fault domains. What this means is that if for some reason some physical component of the vm's support infrastructure fails (rack power supply, switch, physical host, etc) the other iis vm will remain up because it's sitting on a different cluster of hardware (fault domain) in the same datacenter.
One thing that is not as clear for many individuals is that your application must be smart enough to run on both machines simultaneously. If I have one app (excel as a very basic example (just humor me)) on iis1 and a different app (powerpoint) on iis2, if iis1 goes down you lose access to the app (excel) until the vm fails over to another host which may take a few minutes. And vice verse for iis2, if it went down you would lose (powerpoint). To truly make fault domains work you would need excel and powerpoint running on both iis1 & iis2 so if one vm fails the other can still server the application and of course your application would need to be smart enough to handle the failure/failover.
Soooo fault domains protect against unexpected unpredicted failures in the infrastructure.
Update domains are designed to ensure that during maintenance windows (either customer or Microsoft initiated) that we don't reboot/update all machines at once.
As an example, If I had 5 front end web servers connected to 5 backend servers I would put the 5 front end servers in the same update domain so that they will never be rebooted at the same time ensuring I can always serve web traffic. That also goes for my backend servers. I'd put them in an update domain together to ensure that they all never get rebooted at the same time. Doing this ensures that I keep the server(s) running though degraded during maintenance windows.
Availability Sets do provide an SLA of 99.95%.
Availability Zones provide an SLA of 99.99%
Deploying your vm into an Availability zone will replicate your vm to another physical datacenter (zone). This failover happens automatically without the customer having to do anything if something breaks.
Avail set is cheaper to deploy availability zones cost more because not only do you have the vm costs you have costs associated with data replication between zones but we're talking pennies on the dollar.
Hope that helps

Azure - Fault / Update Domain


Linear Activation Function in Neural Network

Question: Graph of output vs. weighted sum
The linear activation function $$ z = b+\sum_{i=0}^k x_iw_i $$
Is used in simple linear neurons in a neural network. Graphs of the z v. the weighted sum of the inputs plus the bias show the line passing through (0, 0), the origin. Is this a strict requirement? Can the line not pass through the origin?
Answer: This is not a strict requirement for activation functions in general, but for the simple linear neuron it is. For instance, with the sigmoid activation function, we typically have a $y$-intercept of $1/2$, but for a linear activation $0$ is typical.
Comment: So it's mostly a matter of convention?


How to store a value from database query result into a Variable in Robot Framework

Question: I've written Robot Framework Code to run database query and log the result of the query
Connect To Database pymysql ${Database_name}    ${UserName} ${Password} ${DatabaseHost} ${Port}

Check If Exists In Database SELECT cic.comboMenuItemId, mi1.zomatoName AS zomatoComboMenuItemName, cic.quantity, \ mi1.isVirtualCombo, cic.isItemVisible, cic.menuItemId, mi2.zomatoName AS zomatoMenuItemName, mi2.isVirtualCombo,micq.currentInventory FROM CombinationItemsComposition AS cic INNER JOIN MenuItem AS mi1 ON mi1.id = cic.comboMenuItemId INNER JOIN MenuItem AS mi2 ON mi2.id = cic.menuItemId INNER JOIN MenuItemCurrentQuantity AS micq ON micq.itemId = mi2.id WHERE mi1.isVirtualCombo NOT IN (0) AND micq.distributionId IN (7) AND mi1.isActive IN (1) AND mi2.isActive IN (1) AND micq.isAvailableOnZomato IN (1) ORDER BY cic.comboMenuItemId

@{QueryResult}  Query   SELECT cic.comboMenuItemId, mi1.zomatoName AS zomatoComboMenuItemName, cic.quantity, \ mi1.isVirtualCombo, cic.isItemVisible, cic.menuItemId, mi2.zomatoName AS zomatoMenuItemName, mi2.isVirtualCombo,micq.currentInventory FROM CombinationItemsComposition AS cic INNER JOIN MenuItem AS mi1 ON mi1.id = cic.comboMenuItemId INNER JOIN MenuItem AS mi2 ON mi2.id = cic.menuItemId INNER JOIN MenuItemCurrentQuantity AS micq ON micq.itemId = mi2.id WHERE mi1.isVirtualCombo NOT IN (0) AND micq.distributionId IN (7) AND mi1.isActive IN (1) AND mi2.isActive IN (1) AND micq.isAvailableOnZomato IN (1) ORDER BY cic.comboMenuItemId

Log Many    @{QueryResult}

Result of Query
(56, 'Party Pack (Serves 6-8)', 6, 1, 1, 1, 'Afghani Chicken Tikka Biryani (Heavy Eater)', 0, 11)
(58, 'Party Pack (Serves 6-8)', 6, 1, 1, 3, 'Chicken Tikka Biryani (Heavy Eater)', 0, 4)
(61, 'Party Pack (Serves 6-8)', 6, 1, 1, 5, 'Paneer Makhani Biryani (Heavy Eater)', 0, 18)
(79, 'Party Pack (Serves 6-8)', 6, 1, 1, 74, 'Afghani Veg Biryani (Heavy Eater)', 0, 10)
(90, 'Party Pack (Serves 6-8)', 6, 1, 1, 89, 'Butter Chicken Biryani (Heavy Eater)', 0, 0)
(253, 'Party Pack (Serves 6-8)', 6, 1, 1, 250, 'Classic Hyderabadi Chicken Biryani (Heavy Eater)', 0, 0)
(255, 'Party Pack (Serves 6-8)', 6, 1, 1, 252, 'Classic Hyderabadi Veg Biryani (Heavy Eater)', 0, 15)
(339, 'Party Pack (Serves 6-8)', 6, 1, 1, 325, 'Awadhi Veg Biryani (Heavy Eater)', 0, 26)
(340, 'Party Pack (Serves 6-8)', 6, 1, 1, 326, 'Awadhi Chicken Biryani (Heavy Eater)', 0, 0)
(381, 'Classic Chicken Tikka Roll ', 1, 1, 0, 408, 'Malabari Paratha', 0, 191)
(383, 'Signature Chicken Seekh Kebab Roll ', 1, 1, 0, 408, 'Malabari Paratha', 0, 191)

Please can anyone suggest how can I store the values of one particular row in variables
Thank You
Comment: Supposedly those rows are just list items, so treat the result variable as a list-like object.
Comment: I'm particularly new to Robot, so please can you be precise as to how should I go ahead with this, If i want to store suppose values of 3 columns of the first row from @{QueryResult} at run-time. As these values displayed are not fixed.
Comment: Yes, Thanks for the help Todor
Answer: Each row from the DB is basically returned as a tuple. To access a particular row use indexing.
Connect To Database pymysql ${Database_name}    ${UserName} ${Password} ${DatabaseHost} ${Port}

Check If Exists In Database SELECT cic.comboMenuItemId, mi1.zomatoName AS zomatoComboMenuItemName, cic.quantity, \ mi1.isVirtualCombo, cic.isItemVisible, cic.menuItemId, mi2.zomatoName AS zomatoMenuItemName, mi2.isVirtualCombo,micq.currentInventory FROM CombinationItemsComposition AS cic INNER JOIN MenuItem AS mi1 ON mi1.id = cic.comboMenuItemId INNER JOIN MenuItem AS mi2 ON mi2.id = cic.menuItemId INNER JOIN MenuItemCurrentQuantity AS micq ON micq.itemId = mi2.id WHERE mi1.isVirtualCombo NOT IN (0) AND micq.distributionId IN (7) AND mi1.isActive IN (1) AND mi2.isActive IN (1) AND micq.isAvailableOnZomato IN (1) ORDER BY cic.comboMenuItemId

@{QueryResult}  Query   SELECT cic.comboMenuItemId, mi1.zomatoName AS zomatoComboMenuItemName, cic.quantity, \ mi1.isVirtualCombo, cic.isItemVisible, cic.menuItemId, mi2.zomatoName AS zomatoMenuItemName, mi2.isVirtualCombo,micq.currentInventory FROM CombinationItemsComposition AS cic INNER JOIN MenuItem AS mi1 ON mi1.id = cic.comboMenuItemId INNER JOIN MenuItem AS mi2 ON mi2.id = cic.menuItemId INNER JOIN MenuItemCurrentQuantity AS micq ON micq.itemId = mi2.id WHERE mi1.isVirtualCombo NOT IN (0) AND micq.distributionId IN (7) AND mi1.isActive IN (1) AND mi2.isActive IN (1) AND micq.isAvailableOnZomato IN (1) ORDER BY cic.comboMenuItemId

${firstRow}     Set Variable   ${QueryResult[0]}
${secondRow}    Set Variable   ${QueryResult[1]}

In the above example ${firstRow} will contain the value  (56, 'Party Pack (Serves 6-8)', 6, 1, 1, 1, 'Afghani Chicken Tikka Biryani (Heavy Eater)', 0, 11) and ${secondRow} will contain the value
(58, 'Party Pack (Serves 6-8)', 6, 1, 1, 3, 'Chicken Tikka Biryani (Heavy Eater)', 0, 4)
Comment: Thanks a lot, It worked for me, I still have to figure out how to pick one one column now from this row value.
Comment: I suppose picking a single row and column would be like -

```${firstRow}     Set Variable   ${QueryResult[0] [0]}```
Thanks again for the help.
Comment: Yes, @TanyaGrover. `${QueryResult[0][0]}` will give you the first row and column.