Weighted mean with aggregate function

Question: I want to get mean of V1 weighted by V2 and split by V3 and V4, so I tried aggregate, it doesn't work because weighted.mean would only accept one parameter from aggregate so it's the same as mean:
df <- data.frame(V1=seq(1, 1.5, 0.1),
                 V2=seq(6, 1, -1),
                 V3=c('A','A','A','B','B','B'),
                 V4=c('F','P','P','P','F','F'))
aggregate(cbind(V1, V2) ~ V3 + V4, data=df, weighted.mean)

  V3 V4   V1  V2
1  A  F 1.00 6.0
2  B  F 1.45 1.5
3  A  P 1.15 4.5
4  B  P 1.30 3.0

So I have to go back to split,lapply and deal with many other stuffs
df1 <- expand.grid(unique(df$V3), unique(df$V4))
colnames(df1) <- c('V3','V4')
df1$CrossTerm <- interaction(df1$V3, df1$V4)

temp <- sapply(split(df, interaction(df$V3, df$V4)), function(df) weighted.mean(df$V1, df$V2))
df2 <- data.frame(WeightedAvg=temp)
df2$CrossTerm <- rownames(df2)
df3 <- merge(df1, df2, by="CrossTerm")
df3$CrossTerm <- NULL
df3

  V3 V4 WeightedAvg
1  A  F    1.000000
2  A  P    1.144444
3  B  F    1.433333
4  B  P    1.300000

But it's a ugly and possibly buggy solution, is there any better solution to get a similar result like this? The format is important and should be as similar to my output as possible(sequence of rows can be different).
Answer: You can do this pretty easily with data.table aggregation:
library(data.table)
setDT(df)
##
R> df[
    ,list(
      wtdMean=weighted.mean(V1,V2)
    ),
    keyby="V3,V4"]
   V3 V4 wtdNMean
1:  A  F 1.000000
2:  A  P 1.144444
3:  B  F 1.433333
4:  B  P 1.300000

Comment: I wouldn't use `keyby` if the OP prefers to preserve the order
Comment: @DavidArenburg fair enough - I had originally just used `by=` but his desired output was presented as ordered by `V3,V4`

Weighted mean with aggregate function


Maximum consecutive multiples of coprimes

Question: Given a set of coprimes, can we calculate a value or a limit for the maximum number of consecutive multiples of these values that can possibly occur?
Now, given the same set of coprimes, if each value initiates two sequences of multiples with different starting points, can we calculate the maximum in that case?
For example, if our set is {5, 7, 11, 13}, and multiples of 5 start at 5 and 6, multiples of 7 start at 7 and 8, multiples of 11 start at 11 and 12, and multiples of 13 start at 13 and 14, what's the maximum unbroken stretch of multiples that can occur?
Comment: Please edit to include your efforts.   And for clarity:  what does, e.g., "multiples of $5$ start at $5$ and $6$" mean?
Comment: trivially until the next prime?
Comment: Please rephrase,not able to understand what you mean.
Comment: In this case, $0$ and $1$ mod $5,7,11,13$, the most is twelve, for example from $335$ to $346$.  Sorry, I don't have any ideas how to do it in general.
Comment: @lulu I have computed the result for some sets. I don't know where to start with this analytically.  A sequence of multiples of 5, so 5 10 15 20 etc.  A sequence with the same spacing starting at 6, so 6 11 16 21 etc.
Comment: Still not following.  Why not edit your post to include the solution for $\{5,7,11,13\}$?   I think that example is pretty straight forward and illustrates the general principle.
Comment: And, for clarity:  I am assuming that you are looking for a consecutive list in which *each* entry is divisibly by at least one of the given set.  Is that what you intended?
Comment: @lulu The idea is each element in the set produces a series of normal multiples and another identical series that is offset from the first one.  And yes, looking for a consecutive list where each entry is covered by any of the series of multiples including the offset series.
Comment: This is related to the "Jacobsthal function", if you want to look that up.
Comment: @GregMartin can you explain how it is related?
Comment: @RickyVesel "The idea is each element in the set produces ...." is not expressed in "multiples of these values". I read "multiples of $\{ 5, 7, 11, 13 \}$ as $ S = \{ 5, 7, 10, 11, 13, 14, 15, 20, 21, 22, 25, 26, \ldots \}$. $\quad$ It seems like you want both $5k$ and $5k+1$. Please express that clearly. You did subsequently mention "each value initiates two sequences of multiples ", so are we allowed to choose $ S + a, S+b$ (with obvious abuse of notation)?
Comment: Sequences $1$ and $4\pmod5$, $1$ and $3\pmod7$, $1$ and $2\pmod{11}$ and $5$ and $7\pmod{13}$ include all numbers from $1$ to $24$
Comment: plz clarify your post.
Answer: A simple inclusion-exclusion argument says that, for an interval of $x$ numbers, with two residue classes from each of $5,7,11,13$, there must be enough contributions to fill the interval.  So, note the positive terms involve the ceiling function, and the negative terms involve the floor function:
$$x\le 2(\lceil x/5\rceil+\lceil x/7 \rceil+\lceil x/11\rceil+\lceil x/13\rceil)\\
-4(\lfloor x/35\rfloor +\lfloor x/55\rfloor+\lfloor x/65\rfloor +\lfloor x/77\rfloor + \lfloor x/91\rfloor + \lfloor x/143\rfloor) \\
+8(\lceil x/385\rceil + \lceil x/455\rceil + \lceil x/715\rceil + \lceil x/1001\rceil \\
-16\lfloor x/5005\rfloor$$
If my graph is correct, that means $x\le139$ would be the widest possible interval for this set of coprime numbers $\{5,7,11,13\}$.
A simpler calculation, that uses $\lceil z\rceil \lt z+1$ and $\lfloor z\rfloor \gt z-1$, is $$x\lt\frac{3^4-1}{\frac35\frac57\frac9{11}\frac{11}{13}}=269.629...$$
However, as I hint at in comments, the actual widest possible interval covered is $24$ numbers.  That involved a computer search through all possible sets of the eight residue classes $\{5k+a,5k+b,7k+c,7k+d,11k+e,11k+f,13k+g,13k+h\}$. There are $10\times21\times55\times78$ sets.  My routine looks at the numbers from $1$ to $10010=2(5\times7\times11\times13)$. The pattern repeats every 5005 numbers, so two cycles will find all the intervals.


What makes graduate-level music theory different from undergraduate-level music theory?

Question: It's been years since I graduated with a music minor. My background consists of:

Traditional two-semester sequence in music theory (basic notation to borrowed chords), i.e., Kostka/Payne
Twentieth-century (Kostka). Also did research in Ravel and Prokofiev's music, which I feel is "quasi-tonalism" often neglected in a music curriculum.
Renaissance Counterpoint (species)
A seminar on Wagner's Ring Cycle.
Form and analysis (if memory serves, a lot of study into traditional structures, e.g., sonata form, binary, etc.).
Schenkerian analysis (using the Forte text)
Didn't take orchestration, but I learned it on my own in high school by reading through Rimsky-Korsakov's text and by studying the scores of Ravel after.

Sure, I could buy a copy of Counterpoint by Kennan (which, to my knowledge, covers Baroque counterpoint) once I have the money, but as far as I know, this is the only severe deficiency I have with respect to what is covered in an undergraduate program.
This is only at the undergraduate-level; people graduate with Ph.D.s in Theory, and this makes me think there has got to be more that I can learn. I'm not interested in getting published in a peer-reviewed journal, so I guess what I'm saying is I'm aiming for master's-level competency. Where do I go from here? Especially appreciated would be resources that people can refer me to.
Answer: My goodness. This is a large topic. Also, though perhaps unintentional, there appears to be some hubris underlying your question. Before I continue to my answer, I want to address three points in your question:
1.) Regarding "quasi-tonalism" - extended tertian harmonies and the evolution into the 2nd Viennese School, Neo-Classicism (Prokofiev) and Neo-Romanticism (Ravel) should all be adequately covered by any legitimately accredited organization for higher learning. Please remember that you speak only from the view point of your personal experience of your one particular program; a program in which you were a minor and therefore spent less time there than the students who majored.
2.) Regarding counterpoint, did you study out of Fux? If that name is unfamiliar to you, then you absolutely cannot count yourself proficient.
3.) Regarding orchestration, it has nothing to do with how you feel but rather how the music sounded when you stood in front of an orchestra and conducted your own music or an arrangement of something. Confidence is one thing, but if it sounds like garbage, then it is garbage.

I'll preface my answer and say that it will not be comprehensive, but it will be enough to satisfy your question, I believe.  Also, before I rattle off several avenues of music theory, I'd also like preface my answer by saying that even though a graduate student and an undergraduate student might be actually taking the same class, they aren't in fact taking the same class.  Someone who has no experience writing music will approach an orchestration class much differently than someone who has extensively written. Orchestration, like many other aspects of composition pedagogy, is an art form unto itself. So even though it might seem like a lame answer, sometimes the most pronounced difference between undergrad and grad level course is time and individual experience.
Just to save us all a lot of time and headache, I'll list several avenues of graduate level and beyond music theory and highlight a couple. I'm not going to bother with resources - you can figure it out on your own.
In No Particular Order

Serialism
Set-Theory
Lydian-Chromatic Concept
Schenkerian Analysis
Neo-Riemannian Theory
Compositional Modeling
Spectral Analysis
Instrument Synthesization
Assorted Seminar Classes on Analytical Histories of Specific Composers (In grad school I took a course on Beethoven that focused almost exclusively on his keyboard works. In seminar classes, you must also present your own analysis to the class in the same manner in which you might present a theory paper at a conference.)
Carnatic Rhythms
Electronic Composition
Small Form / Large Form Composition
Form & Analysis
Pedagogy of Theory
Various history classes
Score Study & Analysis

I'd like to highlight a couple of things: 1.) Even though you asked about theory, I included the bit about history classes because I find I learn so much more about music theory (application, nuance, and language) when I learn more about the composer's life and the circumstances in which they wrote. Learning about composers is tremendously helpful. 2.) In compositional modeling, you create music by mimicing styles of other composers. It is a hands-on approach to learning theory and style. 3.) Electronic Composition may feature a variety of focuses. I have friends who focus on MAX/MSP. I have other friends who focus on musique concréte, and I have others who focus on synthesizing various instruments from pure waveforms. 4.) Schenkarian Analysis is an old warhorse of graduate-level theory. If you do nothing else from the list but want to feel like grad student (i.e. hate yourself) then study Schenker religiously.
As I mentioned, this list is not comprehensive. If I think of more later, I'll jot them down. Each bullet represents one class for one semester. Obviously there is more than a masters degree worth of work here. If anyone else thinks of something, they are welcome to add it.
Comment: Anyway, thank you for the list. This is very helpful. I've also remembered a few more subjects that I forgot to include in the list.
Comment: Apologize for the hubris. I'll edit it out. I have read Fux. I had covered the entire theory curriculum for the majors minus the orchestration and Baroque counterpoint course before I graduated, so I don't think it was too much of a stretch for me to make that claim about quasi-tonalism in the curriculum. The only mention that I recall even remotely similar to that was a bit of talk about planing in the 20th century class, pertaining to Debussy. As for orchestration, I studied from Rimsky-Korsakov's text (continued...)
Comment: (cont...) which I realize is outdated in its treatment, and then self-studied the scores of Ravel.
Comment: @Clarinetist Happy to help. Regarding orchestration, one of the most popular contemporary texts on the subject is the book by Samuel Adler. There is also another excellent book by Alfred Blatter.
Answer: Also, algorithmic composition, machine learning, and such; David Cope has been active in this field, or see the text by Gerhard Nierhaus for a summary of areas of note.
Comment: Feel free to edit my answer and add "Algorithmic Composition" to the list.


Interpreting coefficients for Poisson regression

Question: I don't understand how to interpret the coefficient from a Poisson regression relative to the coefficient from an OLS regression.
Suppose I have time series data, my left-hand side variable is number of games won per year, and my main right-hand side variable is NASDAQ value. If my preferred specification is to interpret the model as in percentage terms, I take the log transformation of games won.  I can also take the log of the NASDAQ to say how much a 1 percent increase in the NASDAQ would increase the percent of games won.  Now, I acknowledge that a Poisson model might make sense because the data for games won are counts and not continuous.  I run the regression with say many, many control variables.
Would I not do a log-transformation on games won and instead use only games?      When I get the coefficients, do I do some sort of marginal effects calculation (as can be done for probit)?
How do I interpret these coefficients?
How do I compare the interpretation of the Poisson to OLS--either the OLS that is log-transformed or the OLS that is not?
I know this sort of question has been asked before, but I still don't quite get it.
Comment: My answer here is relevant:  http://stats.stackexchange.com/questions/142338/goodness-of-fit-and-which-model-to-choose-linear-regression-or-poisson/142353#142353
Answer: Not to be critical, but this is kind of a strange example.  It's not clear that you're really doing time series analysis, nor what the NASDAQ would have to do with the number of games won by some team.  If you're interested in saying something about the number of games a team won, I think it would be best to use binary logistic regression, given that you presumably know how many games are played.  Poisson regression is most appropriate for talking about counts when the total possible is not constrained well, or at least not known.
How you would interpret your betas depends, in part, on the link used--it is possible to use the identity link, even though the log link is more common (and typically more appropriate).  If you are using the log link, you probably wouldn't take the log of your response variable--the link in essence is doing that for you.  Let's take an abstract case, you have a Poisson model using the log link as follows:
$$
\hat{y}=\text{exp}(\hat{\beta}_0)*\text{exp}(\hat{\beta}_1)^x
$$
alternatively,
$$
\hat{y}=\text{exp}(\hat{\beta}_0+\hat{\beta}_1x)
$$
(EDIT: I'm removing the "hats" from the betas in what follows, because they're ugly, but they should still be understood.)
With normal OLS regression, you are predicting the mean of a Gaussian distribution of the response variable conditional on the values of the covariates.  In this case, you are predicting the mean of a Poisson distribution of the response variable conditional on the values of the covariates.  For OLS, if a given case were 1 unit higher on your covariate, you expect, all things being equal, the mean of that conditional distribution to be ${\beta}_1$ units higher.  Here, if a given case were 1 unit higher, ceteris paribus, you expect the conditional mean to be $e^{{\beta}_1}$ times higher.  For instance, say ${\beta}_1=2$, then in normal regression it is 2 units higher (i.e., +2), and here it is 7.4 times higher (i.e., x 7.4).  In both cases, ${\beta}_0$ is your intercept; in our equation above, consider the situation when $x=0$, then exp$({\beta}_1)^x=1$, and the right hand side reduces to exp(${\beta}_0$), which gives you the mean of $y$ when all covariates equal 0.
There are a couple of things that can be confusing about this.  First, predicting the mean of a Poisson distribution isn't the same as predicting the mean of a Gaussian.  With a normal distribution, the mean is the single most likely value.  But with the Poisson, the mean is often an impossible value (e.g., if your predicted mean is 2.7, that's not a count that could exist).  In addition, normally the mean is unrelated to the level of dispersion (i.e., the SD), but with the Poisson distribution, the variance necessarily equals the mean (although, it often doesn't in practice, leading to additional complexities).  Finally, those exponentiations make it more complicated; if, instead of a relative change, you wanted to know the exact value, you would have to start at 0 (i.e., $e^{{\beta}_0}$) and multiply your way up $x$ times.  For predicting a specific value, it's easier to solve the expression inside the parentheses in the bottom equation and then exponentiate; this makes the meaning of the beta less clear, but the math easier and reduces the possibility of error.
Comment: I don't follow that. You don't compare OLS to Poisson; they are different types of models for dif types of situations / phenomena. They are *not* 2 dif models of the same thing where 1 model might be a better account than the other. You wouldn't compare a kitten & a Christmas tree to see if 1 were better. I don't quite get how you're using the phrase "marginal effect", if you mean the effect of a predictor *ignoring the effects of all other variables* (like the marginal effect of a factor in ANOVA), then exp($\beta_1$) is the marginal multiplicative effect of $x_1$.
Comment: I, like @gung , am not sure what you are trying to do. But if you want to compare the *results* of the two models, you  can plot the predicted values of each against each other in a scatterplot.

Comparing the coefficients doesn't make sense.
Comment: Thank you for your help!  Yes, I agree the example is terrible.  Thank you for the abstraction.  I understand how to interpret OLS.  1 unit increase in x lead to a beta_1 increase in y.  If I do a log-transformation to y, then a 1 unit increase in x leads to a 100*beta_1% increase in y.  I don't understand what to do with Poisson.  If I know beta_1, a 1 unit increase in x leads to a what increase in y?
Comment: It's in the answer, in the 3rd paragraph. A 1 unit increase in x leads to an exp($\beta_1$) *times* increase in y.  Say your 'old' y was 10, and $\beta_1=2$, then exp($\beta_1$)=7.4, and y would be 10 *times* 7.4, ie 74. If there were another observation that was 1 unit higher still, that would be 74 * 7.4, etc.
Comment: I'm not understanding because it seems to depends on the values of x and y?  Is there a "marginal effects" that people tend to go by?  For example, don't people use mfx in Stata to report probit estimates?
Comment: The relationship in Poisson reg is *multiplicative* instead of *additive* like in OLS reg. Given that, the role of x isn't that dissimilar. Eg, w/ OLS reg, the value of x tells you how many times you *add* $\beta_1$, w/ Poisson, the value of x tells you how many times you *multiply by* exp($\beta_1$) (both times when going up from 0). OTOH, Poisson certainly does depend on the value of y. B/c the relationship in Poisson is multiplicative, the size of the *jump* at a given point depends on the current value of y, but the size of the *multiplicative factor* remains constant @ exp($\beta_1$).
Comment: How do I compare OLS to Poisson.  I want to say that the results are not driven by the distributional assumption.  Is there a way to estimate the mean effects of Poisson estimates, using marginal effects like in Poisson?
Comment: I have often seen probit coefficients compared to OLS.  Why would Poisson v.s. OLS be a different comparison than probit v.s. OLS?
Comment: That's interesting, I've never seen probit actually used outside of a textbook--I've always seen / used logistic regression. At any rate, probit assumes a latent normal distribution which has been converted into a binary variable, so there is a natural connection between probit & OLS that doesn't exist b/t logistic & OLS, Poisson & OLS, etc.
Comment: If the dependent variable is binary, either a linear probability model or a probit can be used.  Isn't the difference just the distributional assumptions made on the error?
Comment: I'm not sure what you mean. If the DV is binary, you could use logistic regression, or probit, or some other possibilities like complementary-log-log, but you couldn't use OLS regression. Probit models do have a natural connection to OLS regression, but they are not the same thing, and that connection doesn't exist for logit or Poisson, for example.


Algebraic Link/Knot not of the Torus Type

Question: I'm studying Milnor's Singularities of Complex Hypersurfaces, and a small, perhaps moot, point in Chapter 10 has me thinking in circles. (I asked a related but different question here).
Here is some relevant background material, most of which can be found in the book.
Let $f \colon (\mathbb{C}^{n},\mathbf{0}) \to (\mathbb{C},0)$ be a complex analytic function with an isolated critical point at the origin. Define the singular hypersurface $V_{f, \kappa} = f^{-1}(\kappa)$ for small $\kappa > 0$. Milnor proves that the map $\varphi_{f} = f/\| f \| \colon S_{\epsilon}^{2n-1} \setminus V_{f, \kappa} \to S^{1}$ is a fibration, where $\epsilon > 0$ is sufficiently small. The intersection of $V_{\kappa}$ with a small sphere $S_{\epsilon}^{2n-1}$ is an algebraic link which we denote by $K_{f}$. (It is known that all algebraic links are fibered links (by definition) as well as iterated torus links.)
In 1928, Brauner proved that for $f = z_{1}^{p} + z_{2}^{q}$, $K_{f}$ is a torus link $T_{p,q}$, which is a knot if $p$ and $q$ are coprime. In 1968, Milnor conjectured that the unknotting number $u(T_{p,q})$ is equal to the delta invariant of the corresponding complex algebraic plane curve. By a theorem of Kronheimer and Mrowka in 1992, the conjecture is true.
In the same book, Milnor proves the relation $\mu = 2 \delta - r + 1$, where $\delta$ is the delta invariant, $r$ is the number analytically irreducible branches of $V_{f,0}$ passing through the origin and $\mu = \dim_{\mathbb{C}} \mathbb{C}\{ z_1, \dots, z_n \} / \langle \partial_1 f, \dots, \partial_n f \rangle$.
Questions: Is there an algebraic link that is not of the torus-type $T_{p,q}$; one whose crossing number, unknotting number, etc. is well-known? For example, what is the link of $f = z_1^{a} z_2^{b} + z_1^{c} z_{2}^{d}$, where not both $ac = 0$ and $bd = 0$ (provided that $a,b,c,d$ are non-negative integers such that $f$ has an isolated critical point at the origin)?
Can the delta invariant be defined for any non-degenerate weighted homogeneous polynomial of two variables? If so, how does one compute it? My understanding is that the delta invariant can be defined if and only if the polynomial is square-free, which leads me to yet another question: What is a square-free complex polynomial? If so, how does one reconcile the fact that the corresponding unknotting number exists but the delta invariant may not be defined, if these are suppose to be equal?
Edit: The question about square-free complex polynomials has been answered in the comments. Since the ring of convergent power series $\mathbb{C}\{ z_1, \dots, z_n \}$ is a UFD, then a series $f$ is square-free if and only if factored into irreducibles, $f = f_1^{r_1} \cdots f_{n}^{r_{n}}$, it is the case $r_{i} = 1$. Note, here $r = \sum_i r_i$. This is true (though not too simple to prove) if and only if $\mu$ is non-negative and finite.
Thanks!
Comment: I'm not sure I understand your question about square-free complex polynomials. $\mathbb{C}[x_1, ... x_n]$ is a UFD, so like all UFDs it has a natural notion of square-freeness (an element with no repeated irreducible factors).
Comment: @Qiaochu Yuan: Thanks.
Answer: Check out the Knot Atlas, which has a tabulation of known invariants for various knots. For example, the figure-8 knot $4_1$ has crossing number $4$ and unknotting number $1$ but is not a torus knot. A good way to rule out that a knot is a torus knot is to check if it has hyperbolic volume. All torus knots are of Seifert-fibered type, which means their complement can't be hyperbolic. In fact, any randomly chosen prime knot is highly likely to be hyperbolic, as the table shows.
By the way, unknotting number is often difficult to compute, so many of the entries in the table don't have this invariant, but maybe one of them which does will be what you're looking for.
Edit: It is known that any algebraic link, in this sense, is an iterated torus link. None of these examples is hyperbolic. An iterated torus link is an iterated satellite of a torus link by other torus links. I'm not sure what the status is on the crossing or unknotting numbers of such knots. Even computing the crossing number of a generic satellite is a difficult problem. This paper of Lackenby shows that how far we are from realistic results in that direction, but it may be that for the special case of iterated torus knots the answer is known.
Comment: This seems more like a list of links up to crossing. Are algebraic links listed?
Comment: Oops, sorry. I was thinking that most small links are algebraic, but that's using a different definition than this one. (I was thinking algebraic meant closure of an algebraic tangle, which is not the usage here.) According to one reference I just found, algebraic in your sense means "Iterated torus knot," but I don't know the precise definition of what that means.


Prove that this graph is k-connected

Question: Let $G$ be a graph with $n$ vertices and minimum degree $\delta \ge (n+k-2)/2$, with $k\ge 2$. Prove that $G$ is $k$-connected.
Comment: Suppose there are $k - 1$ vertices $v_1, v_2, \ldots, v_{k - 1}$ such that after deleting these vertices we get graph $H$ with $m \ge 2$ components. What can you say about minimum vertex degree in $H$? What can you say about the number of components in $H$?
Comment: Suppose the graph were not connected.  What can you say about the sizes of the components?
Comment: Well, if we assume there are $m$ components $Q_1,...,Q_m$ then each must have at least $(n+k)/2$ vertices. So $|V(G)| \ge m(n+k)/2$ which for $m \ge 2$ gives $k\le 0$. So $m=1$ therefore G is connected, and also $n\ge k\ge 2$.
Comment: Yes, you can also reason in a dual manner and claim that at least one component has at most $n/2$ vertices, which leads to a contradiction.
Comment: But do you know how we would go about proving that it is k-connected?
Answer: First remove any $k-1$ nodes. We get a new graph $G'$ with  $|V(G')|=n-k+1$ and a minimum of $\delta(G') = (n-k)/2$. Assume that $G'$ is not connected, then
there exist two distinct nodes with distinct neighbors. The neighbors are at
least $\delta(G')*2 \ge n -k$. Add the aforementioned nodes and we have a minimum
number of $n-k+2 > |V(G')|$ nodes. Therefore $G'$ is connected and thus $G$ is $k$-connected.

Prove that this graph is k-connected


How to check iPhone network type GSM or CDMA

Question: In my application I need to check network type GSM or CDMA (for Verizon). I found CTCarrier class which contains property mobileNetworkCode, but I don't wanna hardcode value for each providers. Are there other ways to check it?
Please help.
Comment: You make me curious, why do you need this information?
Comment: If CoreTelephony.framework doesn't provide such an API, I doubt if there's an AppStore-compatible way of doing this.
Comment: I have specific server side logic, also I need it for statistic.
Answer: You could get that through the Model#, but then again, you have to hardcode a list of Model# and compare them.
Answer: Maybe you could do that by checking the IP Address of the iPhone if it is wireless connected. If once detected it would´t change unless you change the SIM ;-)
At http://www.tcpiputils.com/browse/ip-address/126.96.36.199 you get information on verizon IP space


How to add code logic to Display Suite fields without PHP in Drupal 8? Twig?

Question: Using Display Suite in Drupal 7 I can easily add PHP logic to any custom Display Suite Field. In Drupal 8 PHP is not allowed. How can I program logic in Display Suite fields in Drupal 8?
As an example, I'd like to use a simple if/then statement to show/hide a node field. I have tried embedding Twig into the field but it does not execute. It just renders as plain text.
This is my sample code {# This is a test comment. #}
Answer: Showing hiding fields in twig is pretty simple. You can check if a field has a value like so:
{% if node.field_foo is not empty %}
  {{ content.field_foo }}
{% endif %}

You cannot execute raw PHP from a twig file, nor can you inject Twig and parse it out through field formatters (probably not without a lot of work, anyway).
Note the difference between checking the node object and rendering the content array.
You want to do checks against the entity object, and never (in my opinion) the render array. In the above code, if the field has a value, then the condition is true. If you want to check the value for a specific string, it is likely better suited for a preprocess hook.
What if the twig template being rendered is a Paragraph, and not a Node template? Not much difference:
{% if paragraph.field_foo is not empty %}
  {{ content.field_foo }}
{% endif %}

If you need more muscle, I would advise moving your logic to a preprocess hook in mytheme.theme where you can use whatever PHP you want, and inject new variables into your Twig file.
function mytheme_preprocess_node(&$variables) {
  $variables['show_foo_field'] = FALSE;

  // if field ...
  // and maybe some more conditions?
  $variables['show_foo_field'] = TRUE;
  // end if...
}

Then in the twig template...
{% if show_foo_field %}
  {{ content.field_foo }}
{% endif %}

Now, you say you are using Display Suite - I personally don't inject condition or code through the UI like what you are asking. But one thing I do know about Display Suite is it (when I last looked) tends to hijack a majority of the preprocess calls - so check its docs or API for the right preprocess hook to call for nodes.
The current hooks for Display suite can be viewed in its .api.php file.
Definitely enable twig debug to be able to check what the Twig template naming pattern should be (suggestions) in the HTML source, and be aware of mytheme_preprocess_node potentially not firing. You will need to locate the correct DS hook.
Comment: I think the answer I needed  (although not the answer I wanted to hear) is that I cannot parse Twig through a field formatter.  Thanks for your additional, and comprehensive, advice.  I will check out the Display Suite API.
Comment: If you get a little lost in Display Suite, a good starting point is to setup a vanilla site without it and get a feel for theming under normal circumstances - then come back to Display Suite. When I first used it in 8.x, a lot of my preprocess hooks quit firing, and it threw me for a loop. Maybe you don't even need Display Suite - I find D8 theming easier than before.
Comment: I think you're right.  I may not even need DS in Drupal 8.  At least, not for theme logic.  It is still useful for overriding default field code and creating quick stacked layouts.
Comment: Yeah, it can be used for RAD/prototyping - just be aware that normal theme calls (namely preprocess) and template suggestions that are in the Drupal documentation _may not work_ when Display Suite is enabled. Just so you don't get confused later - it's not immediately clear at first without diving through the call stack and DS docs/code/api.
Answer: Just an update: starting from its version 8.3.4, Display Suite supports "twig fields" as a new custom field type.
https://www.drupal.org/project/ds/issues/3072095


How to interpret the code: module_invoke_all('user', 'logout', NULL, $user);?

Question: How to interpret this line of code?
module_invoke_all('user', 'logout', NULL, $user);

Obviously, it logs out the current user, but how do the four arguments make it happen?
I read through the doc, but I can't find information about the four parameters.
Can anyone explain?

From Documentation:
module_invoke_all()

Invoke a hook in all enabled modules that implement it.

Parameters
$hook The name of the hook to invoke.
... Arguments to pass to the hook.
Answer: No, they don't log the user out. That is done by the call to session_destroy() in user_logout().
That line only invokes the hook to tell other modules that the user was just logged out so they can react based on that.
The passed arguments are simply those that match the executed hook. Note that the Drupal 7 version is different from what you posted above, which is the Drupal 6 version.


Win32: Python to get all Column Names from Excel

Question: How can I get all defined names from a Excel Worksheet using the COM-interface, i.e. Win32 API in Python?
I want to obtain some sort of a dict, e.g. in the example below it would be:
cols['Stamm_ISIN'] = 2

so that I could access the column later using it's ID 2 to set some values: excel.Cells(row, cols['Stamm_ISIN']).Value = 'x'
I'm somewhat stuck at the API documentation https://msdn.microsoft.com/en-us/library/office/ff841280.aspx so it seems to somehow be possible... Any moreover I couldn't find any result when googeling :(
Comment: Can't you use openpyxl? Check it out: https://openpyxl.readthedocs.io/en/default/
Comment: Only for this task? Yeah would be possible. Apart from that I'm using COM Api because it is for me easy to modify existing cells in a workbook which is already open within Excel. I do not want to open and re-save the workbook from scratch as OpenPyxl does not maintain VBA and format. Hence, I'm using COM Api to modify some cells
Answer: The following script shows you how to display all of the column headings for a given WorkSheet. First it calculates the number of columns used and then enumerates each column. Next it displays all of the named ranges for a given WorkBook, for each it stores the instance into the cols dictionary. This can then be used in conjunction with Range() to set all cells in a given named range to a given value:
import win32com.client as win32
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(r"test file.xlsx")
ws = wb.Worksheets("Sheet1")

cols = {}       # Dictionary holding named range objects

# Determine the number of columns used in the top row
xlToLeft = -4159
col_count = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column

# Print each column heading
for col in range(1, col_count + 1):
    print "Col {}, {}".format(col, ws.Cells(1, col).Value)

# Enumerate all named ranges in the Workbook
for index in range(1, wb.Names.Count + 1):
    print "Workbook: ", wb.Names(index).Name, wb.Names(index).Value
    cols[wb.Names(index).Name] = wb.Names(index)

# Enumerate any named ranges in the current Worksheet
for index in range(1, ws.Names.Count + 1):
    print "Sheet: ", ws.Names(index).Name, ws.Names(index).Value

# Change all cells for a named range called "TestRange" to "TEST"
ws.Range(cols["TestRange"]).Value = "TEST"

# Save the changes
wb.Save()
excel.Application.Quit()

In this example, your Excel file would need to have a named range called TestRange.


relative sequential compactness in L1 along with convergence in measure implies convergence in L1

Question: At the beginning of section 6.2 in "Probability Theory: A comprehensive course" Klenke states: "From the preceding section, we can conclude that convergence in measure plus existence of L1 limit points implies L1-convergence. Hence convergence in measure plus relative sequential compactness in L1 yields convergence in L1. In this section, we study a criterion for relative sequential compactness in L1, the so-called uniform integrability".
I have gone through the preceding sections and I cannot find what Klenke states. What are the theorems or facts that would lead me to Klenke's conclusion?
L1 convergence is defined as $\int |f_n-f|d\mu \xrightarrow{} 0$ as n grows to infinity. Convergence in measure is defined as $\mu(\{d(f_n,f)>\epsilon\}\cap A)\xrightarrow{} 0$ as
$n\xrightarrow{} \infty$ for all $\epsilon>0$ and all $A\in\mathcal{F}$ with $\mu(A)<\infty$
I already know from Athreya about Scheffe's Theorem which requires convergence almost everywhere, convergence in integral and that the limit be integrable, for L1 convergence to result. It seems that Klenke's statement is more general and less restrictive. I suspect that fast convergence in measure may be involved.
Answer: The result, as stated, is fasle. Suppose $f_n \to f$ measure but not in $L^{1}$. Then $(f_1,f,f_2,f,f_3,f,\dots)$ is  a counter-example.
What you need is the fact that every subsequence of $(f_n)$ also has limit point in $L^{1}$. Under this assumption here is a proof:
Let $f$ be a limit point of $(f_n)$ in $L^{1}$. There is a subsequence $(f_{m_k})$ of $(f_n)$ converging to $f$ in $L^{1}$, hence also in measure.  Let $f_n \to g$ in measure. Since $(f_{m_k}) \to g$ in measure we conclude that $f=g$ a.e. Thus, $\int |f_{m_k}-g| \to 0$.
This argument can be applied to any subsequence of $(f_n)$ instead of the whole sequence. Conclusion: Every subsequence of $(f_n)$ has  further subsequence converging to $g$ in $L^{1}$. This implies that $\int |f_n-g| \to 0$.
Comment: @UniformIntegrability Yes, it is correct.
Comment: Thank you for the response. I am a little surprised that there is a simple counterexample to the assertion in the book. Can you point me to a source for the theorem that if every subsequence of $(f_n)$ has a further subsequence converging to $g$ in $L^1$ then $f_n$ converges to $g$ in $L^1$?. I know that an analogous result holds for a regular sequence of numbers converging but it does not hold for convergence almost everywhere of sub-subsequence.
Comment: It is true in any metric space. You can  give a proof by contradiction.  Suppose $d(x_n,x)$ does not tend to $0$. Then $d(x_{n_k},x) \geq \epsilon$ for some $\epsilon$ and some $(n_k)$. Now take a subsequence of $x_{n_k}$ which converges to $x$. Do you see the contradiction now?  @UniformIntegrability
Comment: I think I do. Why does not a similar argument prove the analogous version with almost everywhere convergence?
Comment: @UniformIntegrability Almost everywhere convergence is not given by any metric.
Comment: Thanks again. In your counterexample, you show one sequence that has a convergent subsequence, but I think the statement says that every sequence must have a convergent subsequence.
Comment: @UniformIntegrability The statement says 'existence of limit points'. This is vague. I interpret this as saying that the whole sequence has at least one limit point. If every subsequence of $(f_n)$ has  a limit point then the statement in the book is correct , as I have proved. My counter-example is only to pint out this vagueness in the statement.
Comment: I see.  But if it said that every sequence has a limit point, then  the statement is correct?
Comment: The term "existence of L1 limit points" is unclear, but the next sentence says "relative sequential compactness in L1," and I am wondering if it implies every sequence in L1 has a convergent subsequence. (I was also confused by Klenke and came across this post).
Comment: In an infinite dimensional normed space there are always bounded  sequences which have no convergent subsequence. @keepfrog
Comment: But I thought the definition of sequential compactness is that every sequence (bounded or not) has a convergent subsequence. "Relative" is when the limit is in the closure of the space according to Klenke. I thought your counterexample can be excluded by assuming relative sequential compactness?


Quadratic equation 4

Question: need some guidance with a quadratic equation.
Suppose $x^2+20x-4000=0$
Here is what I have done so far;
Using the Quadratic Equation $x=\frac{-b\pm\sqrt{b^2-4ac}}{2a}$ where from the above equation, $a=1$, $b=20$, $c=-4000$, we find
$$\begin{align*}x&=\frac{-20+\sqrt{(20)^2-(4)(1)(-4000)}}{2\times 1}\\
             &=\frac{-20+\sqrt{16400}}{1}\\
              &=108.0624847 \end{align*}$$
and
$$\begin{align*} x&=\frac{-20-\sqrt{20^2-(4)(1)(-4000)}}{2\times1}\\
             &=\frac{-20-\sqrt{16400}}{1}    \\
              &=-148.0624847 \end{align*}$$
Neither of these values for $x$ prove correct when applied to $x^2+20x-4000=0$.
I know something is wrong but I can't figure out what. Any guidance would be appreciated.
Comment: Can you please use [Mathjax](http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference)?
Comment: The denominator is correct, but the nominator should be $10(\pm \sqrt{41}-1)$.
Answer: $2\times 1=2$, not $1$.  In the denominator.
Comment: Thanks Tilper, I can't believe I didn't spot that. I feel silly.
Answer: Did you figure it out?? I think everyone pointed out where you went wrong, but just to be sure:
$x=\frac{-20+\sqrt{20^2-(4)(1) (-4000)}}{2*\ 1}$
$\frac{1}{2} (-20-20 \sqrt{41})$
= $54.03124237$
And similarly
$x=\frac{-20-\sqrt{20^2-(4)(1) (-4000)}}{2*\ 1}$
=$\frac{1}{2} (-20+20 \sqrt{41})$
$=-74.03124237$
I used Mathematica to check my work:
Letting $answer\text{=}-74.03124237$
$=answer^2+20 answer-4000$
= 0
Similarly,
$answer\text {=} 54.03124237$
$=answer^2+20 answer-4000$
=0

Quadratic equation 4


What is the probability that a leap year has $53$ Sundays or $53$ Mondays

Question: Following question is given in my book:

What is the probability that a leap year has $53$ Sundays or $53$ Mondays?

My understanding:
I understand that in a leap year there are $366$ days i.e. $52$ complete weeks and $2$ extra days.
All the possibilities of these extra days are:
<code>Sunday Monday
Monday Tuesday
Tuesday Wednesday
Wednesday Thursday
Thursday Friday
Friday Saturday
Saturday Sunday
</code>
The question asks us to find the probability of $53$ Sundays or $53$ Mondays which includes the following cases.

Saturday Sunday
Sunday Monday
Monday Tuesday

Here Sunday and Monday occur in two cases each and they occur simultaneously in one case.
Let occurrence of Sunday be denotes by $A$ and that of Monday be $B$, the probability is given by,
$$\begin{aligned}P(A \cup B) &= P(A) + P(B) - P(A\cap B)\\& = \frac27 + \frac 27 - \frac17 \\& = \frac 37\end{aligned}$$

I'm not sure about my work and logic. I wish if someone could check my work and tell me the mistakes if any.
Comment: You don't even need the last bit of computation. You identified that three cases are favourable out of seven total. That is sufficient assuming all those seven cases are equally likely to occur.
Comment: Aah! My bad. I'm really very bad at probability. Thanks for the guidance.
Comment: This is not _entirely_ accurate, because the number of days in the 400-year cycles is a multiple of 7; so the cycle of weekdays repeats eactly, and the true answer is some multiple of 1/97 (97 is the number of leap years in the cycle). Otherwise your result would be correct.
Comment: @TonyK that has to deal with the fact that leap years don't come every 4 years?
Comment: @SBF Yes, see my answer below.
Answer: The answer depends on what you have assumed about the calendar you are using.
If your book has not explained how the calendar works, I would say the question is not well posed, because the calendar has features that cannot be predicted by pure mathematics; they depend on detailed knowledge about the way people have agreed to adjust the civil calendar to the Earth's rotation and its orbit around the Sun.
In the Julian calendar, in which there is a leap year every four years without exception, the calendar will repeat the exact same sequence of days of the week on January $1$ every $28$ years, and in this sequence each weekday occurs on January $1$ of a leap year exactly once. According to that calendar, if we were to check the number of Sundays and the number of Mondays in the upcoming $n$ leap years, starting from any given date, the frequency of years with either $53$ Sundays or $53$ Mondays would approach $3/7$ as $n$ increased, which is a reasonable model of "probability" for a calendar problem like this.
But the civil calendar used almost everywhere in the world today is the Gregorian calendar, in which not every year whose year number is divisible by $4$ is a leap year.
A year number divisible by $100$ is a leap year only if it is divisible by $400$.
In the next $400$ years, $2100$, $2200$, and $2300$ will not be leap years.
There are therefore just $97$ leap years every $400$ years on average, for a total of $400\times 365 + 97 = 146097$ days every $400$ years.
Since $146097 = 20871 \times 7$, the $400$-year cycle of leap years is also a $400$-year cycle of weeks, that is, the same sequence of month/day-of-month/day-of-week triplets repeats exactly every $400$ years. January 1, 2000 was a Saturday and January 1, 2400 will be a Saturday.
Within this cycle of $400$ years, every year the year starts one day later in the week, except that if the year is a leap year then the next year starts two days later in the week. By starting at Saturday, January 1, 2000 and tabulating the day of the week on January 1 for each year of the cycle, you can develop a table from which you can count you many times a leap year starts on each day of the week. (You can also use various techniques to count these days more efficiently, but that doesn't change the result.)
The result is that in every $400$ years,
$15$ leap years start on Sunday,
$13$ leap years start on Monday,
$14$ leap years start on Tuesday,
$14$ leap years start on Wednesday,
$13$ leap years start on Thursday,
$15$ leap years start on Friday, and
$13$ leap years start on Saturday.
That's a total of $97$ leap years, and you want the years that start on Saturday, Sunday, or Monday. The probability according to the Gregorian calendar therefore is
$$ \frac{13+15+13}{97} = \frac{41}{97}, $$
which is slightly less than $3/7$.
If we wanted to be even more pedantic, we might note that even though the Gregorian calendar does a much better job of synchronizing with the orbit of the Earth than the Julian calendar did, it is not perfect and the civil calendar might be modified again in the far future.
But that's beyond anything we can assign an exact probability to.

The final conclusion is that your work is fine under certain simple assumptions (namely, a leap year every $4$ years without exception, which is what most of us alive today can expect to see in their lifetimes).
Whether that is a good or bad answer according to the author of the book depends on what assumptions the author made, although if they were assuming $97$ leap years every $400$ years they really should have said something about that.
Comment: Great answer on second order effects. I'm afraid though that if OPs TA only checks answers and not solutions, they may be expecting to see 3/7. The likelihood of them waiting for the 41/97 I'd say is lower than 41/97
Comment: Thanks for this, David! I didn't have the energy to work all that out for myself.
Comment: @SBF Good point, as I was originally writing this up I wrote that the answer expected by the author was likely $3/7$, but every way I phrased it seemed too snarky. It does seem unlikely that the student would be expected to analyze a complete $400$-year cycle for a "simple" problem like this.


i got "Invalid attempt to call Read when reader is closed" when using sqldatareader how to solve it in a three tier architecture

Question: when i click on linkbutton it gives error that is "Invalid attempt to call Read when reader is closed" my DAL method which returns dr is
private SqlDataReader getDownload(string sql)
{
SqlDataReader dr;
using (SqlConnection con = ConnectionManager.GetDatabaseConnection())
{
SqlCommand cmd = new SqlCommand("getInfo", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@query", SqlDbType.VarChar).Value = sql;
cmd.Connection = con;
dr = cmd.ExecuteReader();
}
return dr;
}

another DAL method is
public SqlDataReader getDownload(int auto_id)
{
string sql = "select mfile_name,file_data from Viva_Notice where auto_id=" + auto_id;
SqlDataReader dr = getDownload(sql) ;
return dr;
}

my BLL method is
public SqlDataReader getDownload(int field)
{
GetPostAssign mGetPostAssign = new GetPostAssign();
SqlDataReader dr = mGetPostAssign.getDownload(field);
return dr;
}

when i am calling it then getting "Invalid attempt to call Read when reader is closed"
protected void lnkDownload_Click(object sender, EventArgs e)
{
try
{
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
if (gvrow.RowIndex < 0)
return;
int field = Convert.ToInt32(lnkbtn.Attributes["RowIndex"]);
SqlDataReader dr = MclsAssignment.getDownload(field);
if (dr.Read())
{
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["mfile_name"] + "\"");
Response.BinaryWrite((byte[])dr["file_data"]);
Response.End();
}
}
catch (Exception)
{
throw;
}
}

Comment: Once you close your connection, you also close the reader. You need to extract the data from the reader before closing it.
Comment: Please indent your code properly and *always* use the previous when you're posting a question - you should have been able to tell that it was basically unreadable long before you posted.
Comment: Possible duplicate - http://stackoverflow.com/questions/6021207/invalid-attempt-to-call-read-when-reader-is-closed
Answer: This is really a design issue - you can't really return a "live" data reader if you intend on destroying the connection, the SqlDataReader is dependant on it i.e.
SqlDataReader dr;
using (SqlConnection con = ConnectionManager.GetDatabaseConnection())
{
    SqlCommand cmd = new SqlCommand("getInfo", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@query", SqlDbType.VarChar).Value = sql;
    cmd.Connection = con;
    dr = cmd.ExecuteReader();
} // the SqlConnection is disposed here
return dr; // dr is now invalid

On top of that, you are leaking your implementation detail through to your BLL/UI layers by returning SqlDataReader. You should read the data whilst the connection is active and return the actual data instead to keep things nice & clean e.g.
public class Download
{
     public string Name { get; set; }
     public byte[] Data { get; set; }
}
...
private Download getDownload(string sql)
{
    using (SqlConnection con = ConnectionManager.GetDatabaseConnection())
    using (SqlCommand cmd = new SqlCommand("getInfo", con))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@query", SqlDbType.VarChar).Value = sql;
        con.Open();
        Using (SqlDataReader dr = cmd.ExecuteReader())
        {
            while (dr.Read())
            {
                return new Download
                {
                    Name = (string)dr["mfile_name"],
                    Data = (byte[])dr["file_data"]
                };
            }
        }
    }
}

Answer: It's a design issue with you data access layer. It's not decoupling the application from the database as it's supposed to. it's just a different interface to the database than using SQL. You need to decouple them completely. That is you need to return data objects to the application. These data objects should then be populated in the DAL using the reader
So your method would be something like:
private MyFile getDownload(string sql)
{
    SqlDataReader dr;
    using (SqlConnection con = ConnectionManager.GetDatabaseConnection())
    {
        SqlCommand cmd = new SqlCommand("getInfo", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@query", SqlDbType.VarChar).Value = sql;
        cmd.Connection = con;
        dr = cmd.ExecuteReader();

        return new MyFile {
                  file_name = dr["mfile_name],
                  file_data = dr["file_data]
        }
    }
}

Comment: +1 for "It's a design issue with you data access layer. It's not decoupling the application from the database as it's supposed to. "
Answer: The using statement disposes the object in context (which is SqlConnection in this case) immediately after the last statement executes in the using block. This is the reason why you are getting the error "reader is closed ..." when you access it outside the DAL function.
Either you should remove the using block and handle connection open/close at the higher layer or better yet, you should convert SqlDataReader to an object or DataTable inside the DAL method and return that instead.
Example with DataTable:
private DataTable getDownload(string sql)
{
    SqlDataReader dr;
    DataTable dt;
    using (SqlConnection con = ConnectionManager.GetDatabaseConnection())
    {
        SqlCommand cmd = new SqlCommand("getInfo", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@query", SqlDbType.VarChar).Value = sql;
        cmd.Connection = con;
        dr = cmd.ExecuteReader();
        dt = new DataTable();
        dt.Load(dr);
    }
    return dt;
}

Comment: bt if i explicitely open it by using cmd.Connection.Open() then also gets error
Comment: Try out the data table solution I just wrote; it will help you. Btw, you'd need to change your caller method(s) as well to work on data table rather than data reader.
Answer: you should change/add a method
public DataTable getDownload(string sql)
{
    using (SqlConnection con = new SqlConnection(yourconstring))
    {
        SqlDataAdapter dap = new SqlDataAdapter(sql,con);
        DataTable dt = new DataTable();
        dap.Fill(dt);
        return dt;
    }
}

then in your download click method
DataTable dt = MclsAssignment.getDownload(field);
        if (dt.Rows.Count> 0)
        {
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + (string)dt.Rows[0]["mfile_name"] + "\"");
            Response.BinaryWrite((byte[])dt.Rows[0]["file_data"]);
            Response.End();
        }

Comment: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

Details: Error parsing near '����JFIF��'.
Comment: what is the line number?
Comment: it shows the alertbox and not showing line number.After executing all try code it directly goes to corresponding catch block
Comment: Dear after Response.End() it directly goes to catch block and shows error


Testing with vsfStream and PHP Unit

Question: So I'm currently picking up Unit testing and on my travels I've discovered I also might need to mock the file system as well as database queries and the like.
I've read a recommendation from the PHPUnit documentation that vfsStream is a good library for mocking the file system.
I'm having issues with using it, though.
I don't really understand how to set it up despite following documentation provided in the wiki hosted on GitHub.
I've abstracted file system interaction into it's own class with some aptly named methods. That doesn't appear to be the issue at hand, as when I wasn't mocking the file system it worked as intended.
Here is how I setup the test:
public function setUp()
{
    // Setup here
    $this->filesystem   =   new Filesystem;
    vfsStreamWrapper::register();
    $this->root         =   vfsStream::setup('test-dir');
}

Here is an example test I written to test for the creation of a directory:
public function testCanCreateDirectory()
{
    $dir        =   vfsStream::url($this->root->path() . '/sub-dir');
    $filesystem =   $this->filesystem;
    $filesystem->makeDirectory($dir);
    $this->assertTrue($filesystem->isDirectory($dir));
}

That appears to work, so far so good. However, this next test fails:
public function testCanPutFile()
{
    $file       =   $this->root->url() . '/sub-dir/test.txt';
    $contents   =   "test text";
    $filesystem =   $this->filesystem;
    $filesystem->put($file, $contents);
    $this->assertTrue($filesystem->isFile($file));
    $this->assertEquals($contents, $filesystem->get($file));
}

As far as I can tell from the documentation this should work, but obviously my reading of it isn't correct and I've gone awry somewhere along the lines.
Any hints as to how I can resolve this issue, as I've been changing lines over and over and cannot figure it out for the life of me.
Thank you in advance!
Answer: I appear to have solved this. The manner of the fix is... somewhat unsatisfactory, maybe someone could help explain the reasoning behind this I'd be happy.
public function testCanPutFile()
{
    $dir        =   vfsStream::url($this->root->path()) . '/sub-dir';
    $file       =   $dir . '/test.txt';
    $contents   =   "test text";
    $filesystem =   $this->filesystem;

    $this->assertTrue($filesystem->makeDirectory($dir));
    $this->assertTrue($filesystem->isDirectory($dir));

    $filesystem->put($file, $contents);

    $this->assertTrue($filesystem->isFile($file));
    $this->assertEquals($contents, $filesystem->get($file));
}

Is how I fixed the issue. It appears I have to recreate the structure per test method. I don't really understand why I have to do it this way. Maybe I'm missing something here but I can't see what I'm doing wrong and there are a paltry number of decent examples on the internet, with most of them found on the authors GitHub wiki. I do believe I've probably misread the documentation though, so a correction on this issue would also be much appreciated.
Comment: It may be that you have to re-create the structure for each test because the structure is created locally in your test function ("$dir" is a local variable, right?) You'll need to move the structure creation into the setup code for the test class, then declare $dir as an instance variable of your test class so all test functions can use "$dir".

Testing with vsfStream and PHP Unit


Traversing MS Word docx with Apache POI and identity the sequence of Tables, images and other elements

Question: Im trying to traverse the docx file with Apache POI, See the following code
<code>         FileInputStream fis = new FileInputStream("sample1.docx");
         XWPFDocument xdoc=new XWPFDocument(OPCPackage.open(fis));
         Iterator<IBodyElement> iter = xdoc.getBodyElementsIterator();
         int count=0;
         while(iter.hasNext()) {
             IBodyElement element=iter.next();
             if(element instanceof XWPFParagraph) {
                 System.out.println("Paragraph\t");
             }
             if(element instanceof XWPFPictureData) { // if(element instanceof XWPFPicture) {
                 System.out.println("Picture");
             }if(element instanceof XWPFTable) {
                 System.out.println("Table");
             }
             count++;
         }
         System.out.println("Items Found " + count);
</code>
Below is the input docx i used

And my result is:
Paragraph
Paragraph
Paragraph
Paragraph
Paragraph
Table
Paragraph
Paragraph
Paragraph
Items Found 9
The problem is
I m not able to retrieve the position of the picture still able to manage to get the position of the table in the given sample file.
(element instanceof XWPFPictureData) or (element instanceof XWPFPicture)
both of these are not getting me there.
Somehow my mistake is I m not using the proper type for the picture I think, then which type I have to use to get the picture position.
Thanks in Advance
Answer: I found the solution with the following code
<code>         FileInputStream fis = new FileInputStream("sample1.docx");
         XWPFDocument xdoc=new XWPFDocument(OPCPackage.open(fis));
         List<XWPFParagraph> paragraphs=xdoc.getParagraphs();
         for(XWPFParagraph para : paragraphs) {
             for(XWPFRun run : para.getRuns()) {
                 System.out.println(run.getEmbeddedPictures().size());
             }
         }
</code>
The above code returns the number of images in the paragraph. Actually the thing is, in the XML encoding the XWPFPicture itself is encoded in XWPFParagraph, where in the case the tables are not. So we need this indirect method to find whether the paragraph contains the image or not and by doing so we are able to identify if the paragraph contains a picture/image and identify the position of the picture or the paragraph in which the picture is found.


Service Worker (sw.js) should always return offline.html document if there is no network connection

Question: I'm having an issue with a service worker that's working partially. The manifest defines the start_url correctly (https://example.com/start.html) for users that add the website to the Homescreen, and both the start.html and offline.html are cached correctly as well, and both are available while the browser has no internet connection.
If the user gets offline (no network connection), the service worker successfully serves both https://example.com/start.html and https://example.com/offline.html -- but if the user tries opening anything else (e.g. https://example.com/something.html) the browser throws a "site can't be reached" error message.
What I actually need, is that, if there is no network connection, the service worker always returns the offline.html cached document, no matter which url the user is trying to reach.
In other words, the problem is that the Service Worker is not properly serving offline.html for the user's requests when there's no network connection (whatever solution is found, it also needs to cache the start.html for the manifest's start_url).
This is my current code:
manifest.json
{
    "name": "My Basic Example",
    "short_name": "Example",
    "icons": [
        {
            "src": "https://example.com/static/ico/manifest-192x192.png",
            "sizes": "192x192",
            "type": "image/png"
        },
        {
            "src": "https://example.com/static/ico/manifest-512x512.png",
            "sizes": "512x512",
            "type": "image/png",
            "purpose": "any maskable"
        }
    ],
    "start_url": "https://example.com/start.html",
    "scope": "/",
    "display": "standalone",
    "orientation": "portrait",
    "background_color": "#2196f3",
    "theme_color": "#2196f3"
}

core.js
if('serviceWorker' in navigator) {
    navigator.serviceWorker.register('sw.js', {
        scope: '/'
    }).then(function(registration) {
    }).catch(function(err) {
    });
    navigator.serviceWorker.ready.then(function(registration) {
    });
}

sw.js
const PRECACHE = 'cache-v1';
const RUNTIME = 'runtime';
const PRECACHE_URLS = [
    '/offline.html',
    '/start.html'
];
self.addEventListener('install', event => {
    event.waitUntil(
        caches.open(PRECACHE)
        .then(cache => cache.addAll(PRECACHE_URLS))
        .then(self.skipWaiting())
    );
});
self.addEventListener('activate', event => {
    const currentCaches = [PRECACHE, RUNTIME];
    event.waitUntil(
        caches.keys().then(cacheNames => {
            return cacheNames.filter(cacheName => !currentCaches.includes(cacheName));
        })
        .then(cachesToDelete => {
            return Promise.all(cachesToDelete.map(cacheToDelete => {
                return caches.delete(cacheToDelete);
            }));
        })
        .then(() => self.clients.claim())
    );
});
self.addEventListener('fetch', event => {
    if(event.request.url.startsWith(self.location.origin)) {
        event.respondWith(
            caches.match(event.request).then(cachedResponse => {
                if(cachedResponse) {
                    return cachedResponse;
                }
                return caches.open(RUNTIME).then(cache => {
                    return fetch(event.request).then(response => {
                        return cache.put(event.request, response.clone()).then(() => {
                            return response;
                        });
                    });
                });
            })
        );
    }
});

Any ideas? Thanks!
Comment: Check out this:- https://stackoverflow.com/a/64504700/1289713 Let me know if this works for you or not.
Comment: @VimalPatel thanks for your help, a solution with code that fixes the issue was posted a few hours ago.
Comment: Where is your code for showing offline page in case of no connection? Yet you said offline page is showing. Are you manually navigating to that page?
Comment: @VimalPatel i meant that if you go to offline.html while offline, it will be cached, but there is no code for showing the offline.html when trying to access any other resource. That's what i need help with :)
Comment: @VimalPatel partially. It solves the offline issue for any resources while there is no network connection, but now there's no access (via fetch) to the start.html if the user accesses via Homescreen while being offline.
Comment: Did you add the "start.html" in your cache list? if there is default page configure then try adding "/" in your cache list and it will work.
Comment: @VimalPatel yes, i added start.html to the cache list as well. The only issue remaining with that solution, is that if the user is offline, and tries to access start.html, offline.html will be displayed. But in *that* specific case, since start.html is cached, that same file should be displayed instead.
Comment: As I said you might try to add '/' in cache list if default document is set in webserver. Can you share your application url?
Comment: @VimalPatel the app is still in a development environment, so I can't share a url yet :( -- i also added '/' to the cache list but the issue still persists. While being offline if I try opening the root '/' or '/start.html', the cached resource won't be displayed, and offline.html cached content will be displayed instead.
Comment: It should work, without debugging difficult to say the root cause. Can you verify when you are online , start,html page is served from service worker cached instead of server?
Answer: Most of your code worked as expected, but you needed a check to see if the user was requesting start.html. I took the code from Create an offline fallback page and modified it to suit your request.
// Incrementing OFFLINE_VERSION will kick off the install event and force
// previously cached resources to be updated from the network.
const OFFLINE_VERSION = 1;
const CACHE_NAME = "offline";
// Customize this with a different URL if needed.
const START_URL = "start.html";
const OFFLINE_URL = "offline.html";

self.addEventListener("install", (event) => {
  event.waitUntil(
    (async () => {
      const cache = await caches.open(CACHE_NAME);
      // Setting {cache: 'reload'} in the new request will ensure that the
      // response isn't fulfilled from the HTTP cache; i.e., it will be from
      // the network.
      await Promise.all([
        cache.add(new Request(OFFLINE_URL, { cache: "reload" })),
        cache.add(new Request(START_URL, { cache: "reload" })),
      ]);
    })()
  );
  // Force the waiting service worker to become the active service worker.
  self.skipWaiting();
});

self.addEventListener("activate", (event) => {
  event.waitUntil(
    (async () => {
      // Enable navigation preload if it's supported.
      // See https://developers.google.com/web/updates/2017/02/navigation-preload
      if ("navigationPreload" in self.registration) {
        await self.registration.navigationPreload.enable();
      }
    })()
  );

  // Tell the active service worker to take control of the page immediately.
  self.clients.claim();
});

self.addEventListener("fetch", (event) => {
  // We only want to call event.respondWith() if this is a navigation request
  // for an HTML page.
  if (event.request.mode === "navigate") {
    event.respondWith(
      (async () => {
        try {

          // First, try to use the navigation preload response if it's supported.
          const preloadResponse = await event.preloadResponse;
          if (preloadResponse) {
            return preloadResponse;
          }

          // Always try the network first.
          const networkResponse = await fetch(event.request);
          return networkResponse;
        } catch (error) {
          // catch is only triggered if an exception is thrown, which is likely
          // due to a network error.
          // If fetch() returns a valid HTTP response with a response code in
          // the 4xx or 5xx range, the catch() will NOT be called.
          console.log("Fetch failed; returning cached page instead.", error);

          const cache = await caches.open(CACHE_NAME);
          if (event.request.url.includes(START_URL)) {
            return await cache.match(START_URL);
          }
          return await cache.match(OFFLINE_URL);
        }
      })()
    );
  }

  // If our if() condition is false, then this fetch handler won't intercept the
  // request. If there are any other fetch handlers registered, they will get a
  // chance to call event.respondWith(). If no fetch handlers call
  // event.respondWith(), the request will be handled by the browser as if there
  // were no service worker involvement.
});

One thing to note with this, once start.html has been cached when the service worker is first installed, it will not be updated again until the service worker is updated. That means your users may see an old/outdated start.html any time they're offline and load your app. You probably want to use a network first strategy for start.html.
You can try the working demo and source
Comment: Hi @PeteLe thanks for the reply, it seems to solve the issue but I have a question before accepting the answer: If the browser is set to offline, i'm getting this error (through sw.js): `The service worker navigation preload request failed with network error: net::ERR_INTERNET_DISCONNECTED.` [screenshot](https://imgur.com/a/BpDpbLr) --- in that specific example, i reloaded the site 20 times. As I understand it, the service worker is trying to update the cached resources on each load. Shouldn't that happen only once, and then again only if `OFFLINE_VERSION` is incremented?
Comment: The only time the service worker will update the cached resource (in this implementation) is during the `install` event. The error you're seeing happens each time the page loads. The service worker intercepts the HTTP requests, tries to hit the network for the file, but it fails because it's offline, so it throws that error.
Comment: Thanks for the explanation. The bounty has been awarded to your account :) -- btw, do you have any idea what could be causing this related issue in Google's Lighthouse PWA checklist? Basically it returns a "start_url does not respond with a 200 when offline" error [screenshot](https://imgur.com/a/gljFxF3). If `start.html` is already cached, why is the Lighthouse not recognizing the resource as available while offline? I posted this question in [here](https://stackoverflow.com/questions/66268076/lighthouse-pwa-audit-returns-a-start-url-does-not-respond-with-a-200-when-offli).


print() em loop com time.sleep() só exibe as mensagens depois

Question: Estou fazendo um código como se fosse uma máquina, então eu decidi colocar isso para tentar imitar um som:
 while(aux>0):
    print("Bzz..",end="")
    aux-=1
    time.sleep(1)

A ideia era de que printrasse "Bzz.. Bzz.. Bzz" na mesma linha com um intervalo de 1 seg para cada um aparecer, porem todos aparecem juntos no fim dos 3 segs, por que isso acontece e como eu posso deixar do jeito que eu pensei?
Comment: `print("Bzz..", end="", flush=True)`  seria dessa forma  o **flush=True**   força o esvaziamento do buffer
Comment: O buffer sera determinado pelo parâmetro file= do print(), na ausencia desse parametro o sys.stdout sera usado, então a fora que o buffer trabalha poderá variar de terminal para terminal, em um terminal que testei funcionou perfeitamente, no cmd do Windows ja tive o mesmo problema que o seu, usar o flush=True resolvera de forma forcada, como ja foi dito.
Comment: Então @Danizavtz, a parada é : quando   coloca o "end=" " depois do "Bzz..",   tá dando uma folguinha pro "Bzz.." antes de imprimir a próxima string. É tipo dar um tempinho pro "Bzz.." respirar, sabe? Mas aí, se   não limpa o buffer de saída antes de imprimir a próxima string, as strings vão ficar todas entupidas lá, como se fossem geladeira cheia de cerveja da pra imaginar? É por isso que o uso o flush=True, pra dar uma desobstruída nesse negócio. É como se fosse uma limpeza.
Comment: @stack.cardoso entendi, deu pra pegar a ideia dps que executei
Answer: O buffer no print() geralmente é determinado pelo parâmetro file=, conforme:
print(*objects, sep=' ', end='\n', file=None, flush=False)

Se for file=None ou se não for definido (o caso do seu código) será usado sys.stdout
Dependendo do terminal você pode obter a saída desejada de imediato ou então só ao final mesmo, por exemplo, em um terminal que usei não precisei usar o flush=True, já no cmd do Windows foi usado o buffer (ocasionando o problema que citou na pergunta).
Note que você pode usar flush= como True, então fluxo será liberado à força, o que provavelmente resolve a sua necessidade mas isso é só a partir do Python 3.3, antes disso tinha que controlar de outras formas, coisas como reabrir o sys.stdout controlando:
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

Ou setar a variável de ambiente PYTHONUNBUFFERED ou então executar o python com a flag -u, assim:
python -u exemplo.py

De forma semelhante ao print(..., flush=True), você pode controlar o flush do sys.stdout com o método .flush()
Exemplo com sys.stdout.write
import time, sys

x = 0

while x < 5:
    sys.stdout.write('Bzz.. ')
    sys.stdout.flush()
    time.sleep(1)
    x += 1

Exemplo com print (já que o file= implícito é o próprio sys.stdout):
import time, sys

x = 0

while x < 5:
    print('Bzz.. ', end='')
    sys.stdout.flush()
    time.sleep(1)
    x += 1


Selecting all values above a threshold and then a random sample of the values below the threshold

Question: Using the cars dataset for example (shown below), I want to create a new data.frame by selecting all of the rows where cars$speed >= 12 and a random sample of 5 from the rows where cars$speed < 12.
> head(cars)
  speed dist
1     4    2
2     4   10
3     7    4
4     7   22
5     8   16
6     9   10

I can do this in multiple steps by first selecting the rows where speed >= 12 using
one <- cars[cars$speed >=12 , ]

then select a random sample of 5 where cars$speed < 12
two <- cars[sample( which( cars$speed < 12) , 5 ) , ]

and then rbind() the two data.frames.
However, I want to add the code to a larger loop and a single step with the same outcome would be greatly preferred.
Thanks in advance for any suggestions.
Answer: I'm not sure how much more this can be condensed, but here's one approach:
library(data.table)
cdt <- data.table(cars)
set.seed(1)
##
rbind(
  cdt[speed>11,],
  cdt[speed<12,][sample(1:.N,5),])

and just to show that the 5 rows were correctly sampled,
R> rbind(
    cdt[speed>11,],
    cdt[speed<12,][sample(1:.N,5),]
    )[order(speed)][1:8,]
   speed dist
1:     4    2
2:     7   22
3:     7    4
4:     8   16
5:    10   18
6:    12   14
7:    12   20
8:    12   24

This isn't necessarily a data.table specific approach, I just find the syntax convenient to use. Also, if your actual data is very large, you might see a performance benefit from using a data.table and replacing rbind(<first subset>, <second subset>) with rbindlist(list(<first subset>,<second subset>)).
Edit:
Thanks to @Arun, another data.table approach:
cdt[
  ,if (grp) .SD else .SD[sample(.N, 5L)],
  .(grp=speed>11),
  .SDcols=1:2]

Comment: @Arun Thank you! `data.table` provides an unusually rich syntax for a single package, so it's always nice to learn some more of its grammar. I wasn't aware that you could define inline groups like that.
Answer: I think you still need to rbind the results but you can do it in one line of code. I've used the sample_n function from dplyr for the sampling:
library(dplyr)

rbind(sample_n(cars[cars$speed<12,], 5), cars[cars$speed>=12,])

dplyr also has the rbind_list function if you need something faster than rbind.
Answer: Perhaps a less elegant solution, but one that just uses plain vanilla R:
cars[c(which(cars$speed >=12),sample(which(cars$speed < 12) , 5 )) ,]

This might not speed things up but it avoids the additional call of rbind.


Setting up for linprog in Matlab

Question: I am having trouble setting up the follow type of constraints for linprog in Matlab
Max 9x1 + 8x2 + 7x3
Subject to: 2 <= x1 + x2 <= 3
            4 <= x2 + x3 <= 5
            x1 >= 0, x2 >= 0, x3 >= 0

Following mathwork's doc
A = [ 1 1
      1 1
      1
      1
      1 ]
f = [ 9 8 7 ]
b = ???
lb = [ 2 4 ]
ub = [ 3 5 ]

I don't know how to set up b since the example given on the website isn't bounded.
Also how does (if needed) the lb and ub take into account of x1 >= 0, x2 >=0, and x3 >=3?
Answer: According to the linprog function, there are 3 forms of specifying constraints:

Inequality constraints. Those like x1+x2 <= 3. For these constraints you must write all of them as a linear combination between the variables less than a constant. So finally you write them like Ax <= b.
Equality constrains. Those like x1+x2 = 3. This case is like the above but instead of the less than sign, you use an equal sign. So your equality constraints will be represented as Aeq x = beq.
Nature of variables. In these constraints you bound your variables. So here you write 0 <= x1 <= Inf as lb <= x <= ub.

So you should write your problem as:
Max 9 x1 + 8 x2 + 7 x3
Subject to:
        - x1 - x2 <= -2
        x1 + x2   <= 3
        -x2 - x3  <= -4
        x2 + x3   <= 5

        0x1 + 0x2 + 0x2 = 0

        0 <= x1 <= Inf
        0 <= x2 <= Inf
        0 <= x3 <= Inf

Now that you rewritten it you define your Matlab variables:
A = [-1 -1 0; 1 1 0; 0 -1 -1; 0 1 1]
b = [-2; 3; -4; 5]
Aeq = zeros(1,3)
beq = 0
lb = [0; 0; 0]
ub = [Inf; Inf; Inf]
f = [9 8 7]

Hope it helps you. Greetings
EDIT:
linprog minimize so f must be [-9 -8 -7]
Comment: Fine detail : The `linprog` function does a minimization so for maximizing you should define `f = [-9 -8 -7]`
Comment: Absolutely. My mistake.
Answer: The MathWorks doc that you link to shows several example of setting upper and lower bounds, and the code you show doesn't follow the doc.  Your A isn't a valid matrix and your lb and ub don't have enough elements (and contain the wrong numbers).
You want
A = [1 1 0;-1 -1 0;0 1 1;0 -1 -1];
b = [3;-2;5;-4];
lb = [0;0;0];
ub = [inf;inf;inf];

Setting up for linprog in Matlab


Finding the value of an unknown from a summation of an exponential

Question:
Find the value of $c$ such that $$\sum_{n=0}^\infty e^{nc}=16$$

I am not too confident with the direction to pursue, but so far, I have let $$a(n)=e^{nc}$$
I then calculated $\dfrac{a(n+1)}{a(n)}$, to which I got $e^c$
I tried to find the radius of convergence by solving $-1<e^c<1$, but when solving the first inequality, I received an imaginary number $(i\pi)$...
I appreciate any help. Thank you very much
Answer: $$\sum_{n=0}^{+\infty} r^n = \frac{1}{1 - r}$$
if $\vert r \vert < 1$.
Let $r = e^c$, so
$$\frac{1}{1 - e^c} = 16$$
which gives
$$c = \ln \frac{15}{16} $$
Answer: Well, if $c$ has to be real, then it is always the case that $e^c>0$, so you only have to check for which $c\in\mathbb R$ you have $e^c<1$. The answer is $c<0$.
In any case, if you want $c$ such that
$$\sum_{n=0}^\infty e^{nc}=\sum_{n=0}^\infty (e^c)^n=16,$$
think that the aforementioned sum equals
$$\frac1{1-e^c},$$
so you need
$$\frac1{1-e^c}=16\quad \iff\quad 1=16-16e^c\quad \iff\quad 16e^c=15$$
which is equivalent to
$$c=\ln\left(\frac{15}{16}\right).$$
Answer: You do not have to worry about the negative side because $e^c$ is a positive number.
All you have to find is the solution to $e^c <1$ which could be done by logarithms.


Why does the definition of tensor as multilinear map always use the same vector space and its dual space?

Question: The definition of a Tensor as a multilinear map is usually given as
$$T:V^* \times V^*  \times\ ... \times\ V \times V \to R$$
Where $V$ is the vector space and $V^*$ is its dual space and $R$ is the real field. Why can't the tensor be a map from different vector spaces to different vector space? like
$$T:V^* \times W^*  \times\ ... \times\ V \times W \to Z$$
Can the mapping:
$$V^* \times W^*  \times\ ... \times\ V \times W \to Z$$
Be represented as a tensor?
For ex:
I know that a tensor product can be defined as a bilinear map:
$$\otimes:V \times W \rightarrow V {\otimes} W$$
and by the universal property any bilinear map:
 $$\alpha:V \times W \rightarrow Z$$
can be given uniquely as linear map
$$u:V {\otimes} W \rightarrow Z$$
but here, is the linear map $u$ that is acting on memebers of $V \otimes W$ (which are type (2,0) tensors i guess?) a tensor too?
How is the definition of tensor as a multilinear map from differnt vector spaces, like
$$T: V \times W \rightarrow R$$
(a type (0,2) tensor I guess?)
or
$$T: W^* \times V \times W \rightarrow R$$
(a type (1,2) tensor i guess?)
given? Is it possible to give the define tensor as a multilinear map that takes inputs form different vector spaces and their dual spaces?
I am asking because, the definition of tensor as a multilinear map is always  given as map from same,let it be V, and its dual, V*.
$$T: V^{*^p} \times V^q \rightarrow R$$
and never using differnt vector spaces and their duals like $V$, $W$, $V^*$,$W^*$. Can tensor take in elements from different vector spaces?(I know generally multilinear maps can be defined like this and also that tensor can be defined as an element of the tensor product of different vector spaces.)
Comment: @Thomas I have gone through that page and also https://en.wikipedia.org/wiki/Tensor & https://en.wikipedia.org/wiki/Tensor_(intrinsic_definition) but failed get an answer. Maybe I am missing something minor in my understanding. But I don't know what it is. I am a confused about the definition tensor as a multilinear map.
Comment: Wikipedia is your friend: https://en.wikipedia.org/wiki/Tensor_product
Answer: This is from the bottom of the Using Tensor Products section.

Tensor products can be defined in great generality – for example, involving arbitrary modules over a ring. In principle, one could define a "tensor" simply to be an element of any tensor product. However, the mathematics literature usually reserves the term tensor for an element of a tensor product of any number of copies of a single vector space V and its dual, as above.

From this, it sounds like it's simply convention with Mathematics.
Note that in the finite dimensional case, it "doesn't matter" a certain amount as for any $V$, a $k$-vector space, we have that $V\cong k^n$ for some $n$
This means when we look at $V\times W\times W^*$, we can say that $V\cong\mathbb{R}^n$, and $W\cong\mathbb{R}^m$, so this is just $\mathbb{R}^{n+m}\times (\mathbb{R}^m)^*$.
I get the impression one reason to define them over the same vector space is so that you can refer to a tensor solely by the $(p,q)$ notation.
I believe this stops working in the cases you mention.
One way to see that is that it's not really "consistent" anymore.
By this, I mean that if:
$$T:V\times W\times W^*\to\mathbb{R}$$
is a $(1,2)$ tensor, this remains true if $V\cong W$.
But what if $V\cong\mathbb{R}^2$, and $W\cong\mathbb{R}^3$?
Then, this becomes a $(3,5)$ tensor, which is undesirable. This is because it means that the 2 covariant indices somehow became 5 covariant indices, without changing any of the underlying mathematical structure, only our "perspective" on it.
This in turn means that we were unjustified in treating the two covariant indices as being the "same" before, so the $(p,q)$ notation was flawed.
Comment: How does it become a $(3,5)$ tensor? The number of _indices_ (input vectors) is not the same as the number of _dimensions_ for one index.
Comment: This answer is a bit misleading. The place for the convention is in the *differential geometry literature*, not within all of mathematics.  In diff. geom., tensors at a point on a manifold are in practice always tensor products of a number of copies of the tangent and cotangent spaces at that point. That is why differential geometers speak about $(p,q)$-tensors. In other parts of mathematics (like commutative algebra and number theory) tensor products need not involve only the same vector space (or module) and its dual, and there is *not* a convention of the kind the OP writes about.


Change the site slogan on certain pages

Question: I have a Drupal 8 site with a content type called Landing Page.  I'm using the bootstrap theme and I have the block showing the brand info (including site slogan) in the navbar.
I want some of the landing pages to have a different site slogan (shown in the navbar).  How can I do that?
What I tried
I thought of adding a text field custom site slogan to my Landing Page content type and then using a twig template (content type node template) to show the slogan, but the slogan is printed in the header, and the template comes after that.
I could also make a different branding block for each page I need to change the slogan on and manually change the text in a twig template, but that seems like a very horrible, labor intensive way to do it.
Comment: You could use `HOOK_preprocess_block` and in there check for node pages with `$node = \Drupal::routeMatch()->getParameter('node');`
Comment: Agree having a field on the content type to override the slogan text, then use `preprocess_node()` to check if the field has value and replace the slogan from the site block. Or if the alternate slogan is the same for all changed pages, use a `boolean` field on the content type? Off the top of my head, seems like you'll need some flag somewhere on the content type level...
Comment: Further, if you do make a field on the content type, I would suggest a select field that can be reused and updated. Have a default slogan, then a list of alternatives. This field could then be reused with its logic on any node. Just to future proof if you're going to do all that work.
Comment: Could `\Drupal::routeMatch()->getParameter('node');` be translated in OOP to 50 out of 500 nodes?
Answer: Ok, thanks to the hint from @Hudri, here's what I did.
In mycustomtheme.theme:
<code>function MYCUSTOMTHEME_preprocess_block(&$vars) {
  if ($vars['configuration']['id'] === 'MYMODULE_site_branding_nolinks_block') {
    $site_config = \Drupal::config('system.site');
    $vars['site_slogan'] = $site_config->get('slogan');
    $vars['site_name'] = $site_config->get('name');
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node instanceof \Drupal\node\NodeInterface) {
      $type = $node->getType();
      // For landing pages, set the site slogan to the value of the slogan field.
      if ($type === 'landingpage') {
        $node = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
        $slogan_field = $node->get('field_slogan')->getValue();
        $slogan_field = $slogan_field[0]['value'];
        $vars['site_slogan'] = $slogan_field;
      }
    }
  }
}
</code>
But, we're not done yet, because the block (provided by my custom module) is cached the same for every page; it needs to be cached per URL, since it might be different for each landing page.
To fix that, in mymodule/src/Plugins/Blocks/myBlock.php, add this code:
<code>  // Caching must be added here:
  // https://drupal.stackexchange.com/questions/223376/what-is-the-correct-way-to-set-cache-contexts-on-custom-blocks
  public function getCacheContexts() {
    return Cache::mergeContexts(parent::getCacheContexts(), ['url.path', 'url.query_args']);
  }
</code>
Comment: I'd bet you can simply set the caching in the block prepo function, too. `$vars['#cache']['contexts'][] = 'url';`.
Comment: @leymannx That did not work for me, see this issue: https://drupal.stackexchange.com/questions/223376/what-is-the-correct-way-to-set-cache-contexts-on-custom-blocks

Change the site slogan on certain pages


$\mathbb{Q}[\sqrt[3]{2}]/\mathbb{Q}$ is separable but not normal, $\mathbb{F}_p(T)/\mathbb{F}_p(T^p)$ is normal but not separable

Question: I just begun to learn algebraic extension. In some notes, I see two examples.

Example 1. The polynomial $X^3-2$ has one real root $\sqrt[3]2$ and two non real roots in $\mathbb{C}$. Therefore, the extension $\mathbb{Q}[\sqrt[3]2]/\mathbb{Q}$ (which is separable) is not normal.

How can I show it is separable and not normal?

Example 2. The extension $\mathbb{F}_p(T)/\mathbb{F}_p(T^p)$ (which is normal) is not separable because the minimal polynomial of $T$ is not separable.

How can I show it is normal but not separable?
Answer: An extension $E/F$ is separable if for every element $\alpha\in E$, the minimal polynomial of $\alpha$ over $F$ has no multiple roots. A test for multiplicities of roots can be carried out by putting in the roots in the formal derivative of the polynomial. More concretely, if $\{\alpha_1,...,\alpha_n\}$ are all the roots of $f(x)$, then $f(x)$ has no multiple roots if and only if $f'(\alpha_i)\neq 0$ for all $i$. In the case of an extension over $\mathbb Q$, or more generally over a field of charactersitic $0$, any extension is separable. This follows from the fact that any irreducible polynomial over $\mathbb Q$ is separable. If $f(x)$ is irreducible and contains a multiple root $\alpha$, then $\alpha$ is also a root of $f'(x)$, hence $f$ divides $f'$ by irreducibility. This is a contradiction since $f'$ is not identically $0$, so it has positive but strictly smaller degree than $f$.
The field extension $\mathbb Q(2^{\frac13})/\mathbb Q$ is not normal because it does not contain all the roots to the polynomial $x^3-2$. As $\mathbb Q(2^{\frac 13})$ is a real field, so it cannot contain the non-real roots.
For the second extension, the minimal polynomial of $T$ is given by $x^p-T^p\in\mathbb F_p(T^p)[x]$. It has multiple roots because in characteristic $p$, $x^p-T^p=(x-T)^p$ in $\mathbb F_p(T)$. (You can prove that this polynomial is irreducible by showing that $(x-T)^k\notin\mathbb F_p(T^p)[x]$ for any $k<p$.) Since this polynomial is non-separable, the field extension is not separable. To see that it is normal, simply notice that $\mathbb F_p(T)=\mathbb F_p(T^p)(T)$, i.e. the field extension is obtained by adjoining the single root $T$. But by the above discussion, $T$ is the only root of the minimal polynomial of $T$. So it is in fact a splitting field, which is normal.


How to show language specific search result in Solr search

Question: We have configured solr search in one of our site.The site is multilingual and having 5 languages content in it.
The problem we are facing is, when user language is selected as french and any searches are performed then the search result is not showing french content at top.
We want the users current language content to be populated at top.
Any module or solr settings which we missed.
Answer: use entity translation ...Regarding Entity Translation introductions:

Enable language fallback
Display shared labels
Select which entities can be translated: Node;Commerce product
Details for Commerce product: Selected Author's language and no
checkboxes at all

Languages selection/detection (admin/config/regional/language/configure):

Selected/added all necessary languages, still leaving English as
default, manually added en prefix for English default, to make clear
separation, as I am using prefix for language separation
User interface text language detection: URL; Default
Content language detection: Interface (moved to top manually); URL;
Default

Commerce product type settings (admin/commerce/products/types/product/edit):

Enabled via Entity Translation
admin/commerce/products/types/product/fields
Replaced regular Drupal Title field with Title module, and in this
field settings checked, that this is the only field in Product type
I'm going to allow translation.
Product display node content type settings from admin/structure/types/manage/{your-display-type}
Published options > Multilingual support - selected: Enabled, with
translation (NOT Enabled, with field translation, as I'm going to use
i18n for language and node separation).
My title field in this display node, which is overridden by Title
module is left for field translation, as these are per field
settings, not per content type.
There are no any other fields let for entity translation in product
display node, even product reference field (used with inline entity
form).

Basically that's all. Taxonomy term translation is handled by i18n modules and Taxonomy vocabulary has Translate... (NOT Localize...) settings - different name for each language tied into translation sets.
Answer: I haven't done this in a while but this should help Apache Solr Multilingual

The goal of this project is to cleanly extend Apache Solr Search Integration to provide:

better support for non-English languages
support for multilingual search
  cross-language information retrieval (CLIR)
an easy-to-use administration interface

Apache Solr Multilingual does not replace Apache Solr Search Integration but is based on it.


Resolve local subdomain on apache for paths within user dir

Question: On Apache 2.2.x I've activated mod_userdir. I used the default setup, so that http://localhost/~name/ will be connect with ~name/public_html/ and a path within public_html, e.g. ~name/public_html/mySite can be reached through http://localhost/~name/mySite.
How can I achieve, that the same path can be reached through http://mySite.name.localhost/? I don't want a manual approach like it is suggested in other SF questions (such as Apache: Setting up local test server with subdomains), but rather want an automatic mapping of all available paths to the corresponding URL.
I think, several steps will need to be taken: Change mod_userdir configuration, so that the subdomain of localhost will be connected with all available user names on the machine. The second step would maybe include the usage of mod_rewrite, so that the subsubdomain could be matched to the path within ~name/public_html...
What would be your prefered way?
EDIT:
The solution is flagged. I some small modifications to meet special requirements and added comments:
RewriteEngine on
# check, if not redirected yet
RewriteCond %{ENV:REDIRECT_STATUS} ^$
# look for URLS like 'subdir.username.localhost'
RewriteCond %{HTTP_HOST} (.+)\.([^.]+)\.localhost$
# check if /home/username/public_html/subdir is a directory or symbolic link
RewriteCond /home/%2/public_html/%1 -d [OR]
RewriteCond /home/%2/public_html/%1 -l
# rewrite to default target
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
# rewrite to /home/username/public_html/subdir
RewriteRule (.+)\.([^.]+)\.localhost(?::\d+)?(.*) /home/$2/public_html/$1$3 [L]

Answer: I had a similar need a few weeks ago.  I solved it using mod_rewrite, by putting this in my default vhost definition:
RewriteEngine on
 # look for URLS like 'subdir.username.localhost'
 RewriteCond %{ENV:REDIRECT_STATUS} ^$
 RewriteCond %{HTTP_HOST} ([^.]+)\.([^.]+)\.localhost$
 RewriteCond /home/%2/public_html/%1 -d
 RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
 RewriteRule ([^.]+)\.([^.]+)\.localhost(?::\d+)?(.*) /home/$2/public_html/$1$3 [L]

There is some more info at https://gist.github.com/744971, including getting username.localhost to resolve to /home/username/public_html
Comment: Since some of my 'subdirs' contain dots, I rewrote parts of the regex. Also some paths are symbolic links, so I added another RewriteCondition. Since your solution does exactly what I asked for, I flaged it as the solution but will add my changes to the initial question.
Great work!