Is this $\mathbb P^1$-fibration over $\mathbb P^1$ a flat family?

Question: This example is a test of several quantities invariant in a flat family.
Let $X$ be a projective variety, let $D$ be a divisor on $X$ with $\dim|D|\ge 1$. Choose a pencil $\{X_t\}_{t\in\mathbb P^1}$ inside $|D|$. This is in particular an algebraical family, therefore a flat family. We know Hilbert polynomial is invariant in a flat family, therefore all quantities arising from Hilbert polynomial, including dimension, degree and arithmetic genus, should be invariant in a such a family.
Here is an example seems contradicting to me: Let $p:X\to \mathbb P^1\times \mathbb P^1$ be the surface blowing up at a point, and let $\pi:X\to \mathbb P^1$ be the composite $p_1\circ p$ with $p_1$ the projection to the first coordinate. ($X$ is indeed a Hirzebruch surface $\Sigma_1$, but we are considering its $\mathbb P^1$-fibration in a different way.)
Then the map $\pi$ gives us a pencil family of divisors on $X$, with general fiber $X_t=\mathbb P^1$, and special fiber $X_0=\mathbb P^1\cup \mathbb P^1$, two $\mathbb P^1$ intersecting at a point. Now, let's check the those quantities mentioned above.
Obviously, the dimension is always 1 which doesn't change. The arithmetic genus is also the same by the answer of this post. However, the degree of $X_t$ is $1$, while the degree of $X_0$ is $2$ (The degree is additive on irreducible components, I believe.)
So my question is:
(1) Does $\pi: X\to \mathbb P^1$ gives a flat family of divisors?
(2) Is it true that $\deg(X_0)=2$?
Answer: The map is flat. In fact, a map from a variety to a smooth curve is flat if and only if it is dominant (see Hartshorne).
The degree is computed with respect to some (ample) line bundle. For instance, if $h_1$ and $h_2$ are the hyperplane classes of factors of $\mathbb{P}^1 \times \mathbb{P}^1$ and $e$ is the exceptional divisor class of the blowup $p \colon X \to \mathbb{P}^1 \times \mathbb{P}^1$, then $2h_2 - e$ is ample on fibers of $p_1$ and with respect to it both general and special fibers have degree 2.
Finally, $X$ is NOT a Hirzebruch surface, since its Picard number is 3. It is the del Pezzo surface of (anticanonical) degree 7.
Comment: Dear Sasha, thanks for your correction and explanation. Indeed, the Hirzebruch surface is blowing down a $(-1)$ curve on $X$. And I can now see that the map $\pi:X\to \mathbb P^1$ gives a family of conics degenerating to two lines.  But here is a question in your answer: $2h_2-e$ is divisor on $X$ whose restriction on each fiber of $\pi$ is ample, but itself is not ample on $X$ (as its paring with $h_2$ is zero). It seems to be a notion "being ample with respect to a morphism" if I interpret correctly. It is new to me. Do you have a reference to this notion?
Comment: For instance, see https://stacks.math.columbia.edu/tag/01VG.


Convergence of an infinite series problem

Question: I am having trouble with the series
$$\sum_{n=1}^\infty (-1)^n\frac n {n+1}$$
I want to know if it converges or not, and I´ve tried with the comparision test, the ratio test, the Leibniz test...
Any ideas?
Answer: Try the divergence test.  If the limit
$$
\lim_{n\to\infty}(-1)^n\frac{n}{n+1}
$$
isn't $0$, then it diverges.
Comment: How did you get $log$ term in limit?
Comment: It may sound silly, but I was already doing it and I´m completely stuck with the limit. I mean, with $(-1)^n$, I just don´t know how to work with that, as $log(-1)$ does not exist. Am I making sense?
Comment: I replaced $(-1)^n=e^{nlog(-1)}$
Comment: You need to show that $(−1)^n$ does not converge. Argue by contradiction suppose that $(−1)^n\rightarrow L$. Then given $\varepsilon$, there is some $N$ such that for all $n\ge N$ we have $|(−1)^n−L|\le \varepsilon$. If $n$ is even so $|1−L|\le \varepsilon$, if $n$ is odd $|1+L|\le \varepsilon$. Then $2=|1+1|≤|1+L|+|1−L|\le 2\varepsilon$. Setting $ \varepsilon<1$, we have a contradiction. Then the limit doesn't exist as desired.
Comment: Got it, but even if $(−1)^n$ does not converge, that does not mean that $(-1)^n \frac n {n+1}$ doesn´t.
Comment: But I´ve realized now that I didn´t have to use log, taking the absolute value of the sequence was enough. Thank you all for your help!
Comment: Yes the problem is that $n/(n+1) \not \rightarrow 0$
Answer: For series $\sum_{i=1}^{i=\infty}a_n$.if $\lim_{n\to\infty}a_n\neq0$,then series diverges
Answer: First let $a_n$ be a real valued sequence then we have the equivalence
$$\lim_{n\rightarrow \infty}a_n=0 \Leftrightarrow \lim_{n\rightarrow \infty}|a_n|=0$$
Here $\lim_{n\rightarrow \infty}|(-1)^n\dfrac{n}{n+1}|\not = 0$ then $\lim_{n\rightarrow \infty}(-1)^n\dfrac{n}{n+1}\not = 0$
hence the series $\sum_{n\ge 0} (-1)^n\dfrac{n}{n+1}$ can't be convergent. It is then a divergent series.


Problem with Quotas and File Screening on Mount Points in Windows 2008

Question: I have a Windows 2008 Server running the File Server Role and I would like to use mount points for my volumes instead of drive letters. However, I need to use the quota and file screening features of File Server Resource Manager, and it seems that they do not apply correctly to mount point folders. I am able to upload oversized files and excluded file types without any warnings.
Could someone help me with a fix or workaround for this issue?
Thanks,
Jamie
Comment: I have isolated this to mount points of iSCSI-attached storage volumes. Mount points on local storage, and even an iSCSI-attached volume with a drive letter assigned both work correctly! I'm puzzled as to why this is causing a problem. Any ideas?
Answer: I believe I have found the problem: I was applying an auto-Quota to the folder which was hosting my mount points, which is why they were not propagating to files and folders in the mount points themselves. I found that putting a quota directly on the mount point works as expected. Not as convenient as setting these policies at a higher level, but works nonetheless.


Effects of altitude on athletic performance

Question: It is well known that the performance of athletes who suddenly move to a location at a higher altitude than they are used to will suffer; when sports events such as the Olympics are held in a high altitude location, athletes often move to the area much earlier in order to adapt.
The question I haven't been able to find an answer to is: How big of an altitude difference is significant? E.g. Will a sprinter who lives and trains at sea level feel any different competing at 200 - 300 m above sea level?
Answer: As outlined in Tim Noakes book "Lore of Runnning".(1)

Performance in races of
  60 to 200 m is always superior at altitude, with the benefit improving with increasing
  altitude. Performance in races of 400 m improve progressively up to altitudes
  of between 2000 and 2500 m but then begins to return to sea-level values. However,
  even at altitudes of 4000 m, performance is still slightly better than at sea level.
  Performance in races of 800 m or longer is always inferior at altitude; this deterioration
  in performance falls most steeply at altitudes above 2500 m.

REFERENCE
(1) Tim Noakes (2001). Lore of Running. 4th ed. South Africa: Oxford University Press. 931.


If $A\oplus B\cong A\oplus C$ then $B\cong C$

Question: Let $A,B,C$ be finitely generated abelian groups, and $A\oplus B\cong A\oplus C$. Prove that $B\cong C$.
I know that it follows from the fundamental theorem of finite abelian groups, but I have difficulty with formalizing a proof
Answer: Hints:
Fix decompositions for $A,B,C$ and note that they "stack" to become decompositions of $A\oplus B$ and $A\oplus C$. The components of the decompositions must line up exactly, and without loss of generality, we can line up the components of $A$ on both sides because they're identical. What does this say about the remaining components?


JSON:API fundamental permissions and access restriction question

Question: Intro
JSON:API is declared as a must-have for decoupled Drupal websites.
While I completely (well, I hope so) understand the built-in Drupal permissions/access restriction system, I have a specific use-case and I would like to understand how can I implement what I need with so much advertised JSON:API approach taking into account potential security issues described below.
Current implementation
So far I was only able to accomplish the task using Views REST display, disabling default JSON:API endpoints and creating my own JSON:API resources, because Views allow me to have hard server-side restrictions while filtering the content and custom JSON:API resource gives me control over the content I deliver.
Disposition
Well, so there are 2 sections on my website:

There is a public section with Articles. Any logged-in user can see all the published articles. I can deliver this page using Views as a separate page, Views REST or JSON:API to some PWA, doesn't matter. So far so good.
There is a restricted section on the website (kind of dashboard) with the list of the articles created by the currently logged in user. Not only the User is able to see the publicly available fields, but some private fields too. User should not be able to see the other users' articles. For the purpose of this question, let's say that seeing all the articles in general via [1] route is not a problem from security standpoint, but seeing all the articles per user is a problem. In general, I do not what the users to know who is the author of each article.

Problem
We need to somehow separate an access restrictions for (1) and (2), even though both operations are going as 'view entity'. But (1) delivers all the articles regardless of the author, (2) delivers articles owned by the currently logged in user only.
I can build a view for a public section, but I like to use a PWA for my dashboard, so the articles are delivered using JSON:API there.
Problem from permissions standpoint
Now I can not use default permissions settings (i.e. adding/removing 'View Article' permission) because this breaks route (1) [means: users without 'View Article' permission can not see the list anymore]
At the same time, I can not just rely on the query when returning the articles for the dashboard request (2) of some specific user, because let's say some advanced user sees the parameters I include in the request, he changes those parameters and - boom - sees the articles created by the other user, which is a security breach.
Problem with different fields visibility
Same question applies to the fields. When I show the (1) route (all articles), I might need to output public fields only, while when I deliver the content to (2) [per-user dashboard], I need to deliver all the fields.
Final question number one
So my question is: how can I solve this? How can I deliver same content using JSON:API with checking the owner server-side, not passing the owner as a parameter, and without restricting an access using built-in permissions system?
[EDIT] Final question number two
Is there a way for me to declare a security context (read: declare needed permission to be checked against each entity) before actually outputting/processing the data using JSON:API? Good example would be custom route with custom permissions check, while JSONAPI, as far as I am concerned, only consider CRUD entity-related permissions based on GET/POST/PUT/DELETE method being used.
Comment: `Not only the User is able to see the publicly available fields, but some private fields too.` If you set custom field permissions so that users can only see the values of private fields on articles for which they are the author, JSON:API will respect this, and you can do everything in JSON:API.  For an example of setting up field permissions, check out the Examples module.
Answer:
JSON:API is declared as a must-have for decoupled Drupal websites.

This is not true.  Decoupled Drupal can take many forms, including

REST
JSON:API
GraphQL

JSON:API is pretty great, but if it doesn't fit your use case, you can build a complete decoupled website and never touch it.
For a comparison of these solutions, see the book Decoupled Drupal by Preston So.
How JSON:API handles access control
In Drupal, JSON:API respects whatever permissions/access control is set for users.  The system was designed this way so that if you already have a functioning Drupal website with access control properly set up, then you can enable JSON:API and it will have the same access control with no additional work required.

So my question is: How can I deliver same content using JSON:API with
checking the owner server-side, not passing the owner as a parameter,
and without restricting an access using built-in permissions system?

You need to use the built-in permissions system to restrict content with JSON:API.  That's the way it is designed in Drupal.  So the answer to "without restricting access using the built-in permission system" is "you can't."
If you want to make arbitrary rules for access, consider REST or GraphQL.
You could alternately use custom JSON:API Resources, but then whatever data you want to hide is probably exposed automatically by JSON:API, so you need to lock it down with JSON:API Extras, and then the question is why are you using JSON:API instead of REST for your particular use case?
Comment: >>This is not true
Well, at least Dries says so https://dri.es/headless-cms-rest-vs-jsonapi-vs-graphql

Okay, if I may rephrase the question, how can I declare a security context or declare needed permission to be checked against each entity before actually outputting the data using JSON:API? Good example would be custom route with custom permissions check, while JSONAPI, as far as I am concerned, only consider CRUD permissions based on GET/POST/PUT/DELETE method being used. (also updated the original question).
Comment: @AlexSmirnoff One question per post.  This is not a discussion site.  Since there is a concept of "accepted answer" here, multiple questions make it impossible to determine the accepted answer.  That said, to answer your second question, as you note JSON:API considers the CRUD (entity-level) permissions only.  So if you want to do a custom route, you have to block everything that you don't want shown in JSON:API (using JSON:API Extras or custom code) and then create a custom resource, as I said in the last paragraph of my original answer.  This is a limitation of JSON:API, but it is by design.


Add bytes to byte array C#

Question: How could I add the following bytes to a c# byte array?
00 00 00 00 00 00 00 00 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00

Does this make sense?
public void updateBytes(string exeName, int value)
{
    long baseaddress = GetBaseAddress(exeName, exeName + ".exe");
    long pointer = GetPointerAddress(baseaddress + 0x04105320, new int[] { value });

    byte[] intBytes = "00 00 00 00 00 00 00 00 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00";

    WriteBytes(pointer, intBytes);
}

I would appreciate any kind of help
Comment: This doesn't even compile as of yet. `byte[]` != `string`
Comment: Possible duplicate of [c# how to add byte to byte array](http://stackoverflow.com/questions/5591329/c-sharp-how-to-add-byte-to-byte-array)
Comment: It is better to use a list object which has an add method than to use an array.
Answer: It seems to me that you don't really want to add items to an array but create/initialize an array with given values.
You can use the following syntax to initialize an array:
byte[] intBytes = {0,0,0,4,5,1,1 /* ... */ };

Answer: if you want parse string into array of byte (byte[]) you can use Linq:
string source = "00 00 00 00 00 00 00 00 04 05...";

byte[] intBytes = source
  .Split(' ')
  .Select(item => Convert.ToByte(item, 16))
  .ToArray();

Answer: Okay, so first of all you have a string and you want to assign this to byte[] which is a big nono. If you cannot change this to byte[] by hand ( because of some weird protocol or something ) you can do this like that :
// assign bytes to string
string meBytes = "00 00 00 00 00 00 00 00 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00";
// split them by spaces
string[] hexBytes = meBytes.Split(new char[] { (char)0x20 });
// extract bytes
byte[] bytes = meBytes.Select(x => Convert.ToByte(x, 16)).ToArray();
// now you can write them into stream
WriteBytes(pointer, bytes);

You can even  do this in two lines :
string meBytes = "00 00 00 00 00 00 00 00 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 04 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00";
WriteBytes(pointer, meBytes.Split(new char[] { (char)0x20 }).Select(x => Convert.ToByte(x, 16)).ToArray());

Comment: `meBytes.Split(0x20)` *doesn't compile*: `0x20` is of type `int`, when `char` is expected; `0x20` is *magic number antipattern* (what does it stand for?) when `' '` (space) is evident
Comment: @DmitryBychenko Thank you for pointing that. Fixed it. And `0x20` ( as everyone knows ) is just a space character and it is more convenient ( for me and people i know ) to write bytes/chars in hex format.

Add bytes to byte array C#


GWT grails plugin ERROR resource.ResourceMeta - Resource not found: %nocache.js

Question: I'm having trouble using the GWT-Grails plugin. I tried to follow this example exactly, but I end up having this error:
| Error 2012-10-28 00:25:49,567 [http-bio-8080-exec-2] ERROR resource.ResourceMeta  -  Resource not found: /gwt/g3wt.Application/g3wt.Application.nocache.js

I get this error after I enter these commands: ( I am simply following this guide here )
>grails create-app g3wt
>cd g3wt
>grails install-plugin gwt
>grails create-gwt-module g3wt.Application
>grails create-gwt-page main/index.gsp g3wt.Application
>grails run-app
>grails run-gwt-client

this is the index.gsp code (which is autogenerated anyway):

  <meta name="layout" content="main" />

  <title>Example title (change this!)</title>

I understand that the nocache.js cannot be found, so I thought I could try to see where exactly it is. However, using the eclipse search function, it shows that the file is not created at all. Although there is a file named application.js.
Why is the nocache.js not created, and how do I fix this? I've been stumped for a while now, and it's frustrating.
I'm using gwt plugin 0.7.1 and grails 2.1.1, with eclipse 4.2 and a Linux machine.
Answer: Okay, so I got the error to disappear by first running
grails> compile-gwt-modules

Now a different problem occurs. The front-end gwt codes I made, aren't displayed.
Answer: I was having the same problem and found the answer here.
buildconfig.groovy is in the conf folder.  Just comment out the plugin where the runtime is ":resouces..."
Answer: You can add in you Confing.groovy:
grails.resources.adhoc.excludes = ['**/gwt/**']

This will solve the problem. But if you use plugin version 0.8 this issue should be resolved.


Is multicollinearity the issue here?

Question: How is that I get a low $R^2$ if I use $X1$ against $Y$ and $X2$ against $Y$, but a $R^2 =1$ if I use both together?
How does the combination blow out the $R^2$?
Could you please show me that calculation?
The data:
<code>Y   12.37   12.66   12.00   11.93   11.06   13.03   13.13   11.44   12.86      10.84    11.20   11.56   10.83   12.63   12.46

X1  2.23    2.57    3.87    3.10    3.39    2.83    3.02    2.14    3.04    3.26    3.39    2.35    2.76    3.90    3.16

X2  9.66    8.94    4.40    6.64    4.91    8.52    8.04    9.05    7.71    5.11    5.05    8.51    6.59    4.90    6.96
</code>
Answer: In multiple regression, you are trying to model observations, $y$ that are supposed to be noisy versions of a prediction, $X\beta$. That is, we should have:
$$y=X\beta+\epsilon$$
In your case, $\epsilon$ is almost zero. That is, both explanatory variables almost perfectly explain your observations. You have:
$$\beta = (-4.515413640943276 \text{ (constant offset)}, \\ 3.097007886142981, \\ 1.031859030944453)$$
This gives you a nearly perfect fit.

Regarding multicolinearity: By definition, multicolinearity occurs when your predictor variables, $X1$ and $X2$, are linearly related.
Now, in your case, $X1$ and $X2$ do, in fact, have a high correlation coefficient of about -$0.9$ (I had missed this in a previous edit).

However, this is not the cause of the $R2$ jump that you observe by including both $X1$ and $X2$.
For, let multicolinearity have been present. Then, we could write for a small error $\eta$:
$$X2 = a + bX1 + \eta$$
In that case, we would be able to write, for each data point $i$:
$$y^{(i)} = \beta_0 + \beta_1 X1^{(i)} + \beta_2 (a + bX1^{(i)} + \eta^{(i)}) + \epsilon^{(i)}$$
$$\Rightarrow y^{(i)} = (\beta_0 + a \beta_2) + X1^{(i)} (\beta_1 + b \beta_2) + \epsilon^{(i)} + \beta_2 \eta^{(i)}$$
Now that we know that $\epsilon^{(i)}$ are nearly zero, then, unless $
\beta_2$ were large, had multicolinearity been an issue we would have seen $R2 = 1$ for even the $Y-X1$ regression. Instead, we get $R2 \approx 0$. This means that while $X1$ and $X2$ together are good predictors, they aren't linearly related enough to be good predictors individually.
Having said that, multicolinearity might play a role in other aspects of your problem, given the $X1-X2$ correlation coefficient of $-0.9$.
Comment: Thanks but as both variables are highly correlated, why do you say there's no collinearity? Why could each variable have so small predictive power alone but perfectly fit together?
Comment: I am having trouble reconciling the 3D plot with the scatterplots.  The former *appears* to show linear relations among all three variables whereas the latter show something entirely different.
Comment: @whuber, the plots are right.
Comment: "both variables are highly correlated": which two do you have in mind? $X1$ and $X2$ are not correlated. The reason I said there is no multi-colinearity is because of the definition of the term: "Multicollinearity exists when two or more of the *predictors* in a regression model are moderately or highly correlated." The 3 variables are related by a linear equation but it doesn't qualify as multicolinearity as per the definition.
Comment: @whuber: the 2D scatters are just projections of the 3D one on each plane of the planes. What I was trying to show in the 3D plot was that all the $(X1, X2, Y)$ scatter points lie in a plane (linearly related), yet pairwise, they are not correlated (esp. the $X2-X1$ plot)
Comment: @Salmonstrikes, the correlation between X1 and X2 is -0.9. That´s what I got using SPSS and R. I think that can be observed from your first 2D scatterplot.
Comment: I had suspected as much, but your graphic makes it impossible to see what's going on.  At least consider describing what these graphics represent, for otherwise readers may misinterpret and misunderstand them.
Comment: @whuber: Thanks again for the feedback. I have revised my answer.

Is multicollinearity the issue here?


Automatically generate a short description of a page when a user posts a link

Question: On facebook, google plus and similar websites, when someone posts a link, the thumbnail and short description of the page is automatically generated. See the screenshot below.
Is it possible to this in Drupal 7 ? If yes, how? Is there any module for it?
Comment: Is node teaser what you need?
Comment: @ninjascorner: No. When someone pastes url of any website, the thumbnail and description should come automatically,
Answer: Look at the source for that page, you'll see this
<code><meta name='description' content='In honor of Wolfenstein 3D's 20th anniversary, the game will be released for free to play in browser today.'/>
</code>
Should be fairly straightforward to grab that!
Comment: Sorry for late reply, but you can grab some meta tags with get_meta_tags http://php.net/manual/en/function.get-meta-tags.php - or to grab opengraph data (which if available is ideal for this kind of preview but won't be included in get_meta_tags because the tags don't have a name attribute) I've found this library to be very helpful https://github.com/scottmac/opengraph
Comment: Straightforward?Exactly how?
Answer: It all depends on the meta tags you have set. The easiest way to accomplish this is by installing the Meta tags module.
Once enabled, you can navigate to the configuration page and set configuration for auto-generated tags. If you need to have specific tags for a node, you can edit that node and set the meta tags there.
You should enable the Open Graph meta tags submodule, as Facebook looks for those tags when you want to share on Facebook. Whenever you update the Open Graph tags, run it through the Facebook debugger so you can see what Facebook will grab. Most of the time, Facebook should grab whatever images are available on the page and use those as thumbnails.
I use a combination of Tweet Button, Facebook Share and Google Plus One but i'm sure there are "all-in-one" shares that you can use. The most important thing remember is that if the meta tags aren't correct in the HTML, then they probably won't work correctly when you try to share your page.
Comment: I think the OP was after the other side of this - grabbing previews from external sites, not creating his own meta tags.


MySql Dynamic Period Columns from Rows Conversion

Question: I am having issue about converting rows data to columns. Let's say I have this kind of data on table the_table
YEAR | MONTH | ASPECT | VALUE |
-------------------------------
2015 |   1   | Target |  90   |
2015 |   1   | Score  |  75   |
2015 |   2   | Target |  78   |
2015 |   2   | Score  |  80   |
2015 |   3   | Target |  90   |
2015 |   3   | Score  |  100  |
2015 |   4   | Target |  60   |
2015 |   4   | Score  |  90   |

Then I would like to change it into something like this. The number of columns is dynamically chosen by user (by range). For example, if user chooses range between period 1 and 3, the result will be
ASPECT | PERIOD1 | PERIOD2 |
----------------------------
Target |   90    |   78    |
Score  |   75    |   80    |

for now, I am using query such as
select PERIOD1.ASPECT, PERIOD1.VALUE, PERIOD2.VALUE
from
(
select ASPECT, VALUE
from the_table
where year = 2015
and period = 1
) PERIOD1,
(
select ASPECT, VALUE
from the_table
where year = 2015
and period = 2
) PERIOD2
where PERIOD1.ASPECT = PERIOD2.ASPECT

Do you have any idea how to deal with this thing?
Thanks in advance
Comment: Number of columns chosen? Period or month?
Comment: Basically user can only choose month (year is alway same)
Answer: You are effectively trying to create a pivot table. You can accomplish this by chaining together quite a few joins, here's one that works for your sample data:
select q1.aspect, q1.value 'period 1', q2.value 'period 2', q3.value 'period 3'
  from
    (select aspect, value
       from the_table
         where month = 1
    ) q1
    inner join
    (select aspect, value
       from the_table
         where month = 2
    ) q2
    on q1.aspect = q2.aspect
    inner join
    (select aspect, value
       from the_table
         where month = 3
    ) q3
    on q1.aspect = q3.aspect;

The general form there is tedious, but simple to expand.
Here's a demo fiddle: http://sqlfiddle.com/#!9/6eb5d/9
And here's a slightly more specific version that uses a little bit of voodoo math to work:
select aspect,
  sum(value * (1 - abs(sign(month - 1)))) as 'period 1',
  sum(value * (1 - abs(sign(month - 2)))) as 'period 2',
  sum(value * (1 - abs(sign(month - 3)))) as 'period 3'
from the_table
group by aspect

Which can be seen compared to the first query here:
http://sqlfiddle.com/#!9/6eb5d/13


JPA anotação @Autowired

Question: O @Autowired é o mesmo que extends?
Vejamos um pequeno exemplo:
package com.dendetech.services;
import com.dendetech.entity.Participante;
import com.dendetech.repositories.ParticipanteRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ParticipanteService {
@Autowired
private ParticipanteRepository participanteRepository;

/*
 * Crud Operations
 */

<(Restante omitido).
>
Answer: Não, cada coisa é uma coisa.
Esta anotação está injetando automaticamente uma instancia de ParticipanteRepository.
Para que você não precise usar new ParticipanteRepository() ou outro método de criação sempre.
Extends é Herança, neste caso o Service herdaria do Repository.
Comment: Na linha:

>"@Autowired
private ParticipanteRepository participanteRepository;"

A variável participanteRepository é quem recebe a instância do objeto da classe ParticipanteRepository?

Outra pergunta, isso significa que sendo o objeto instanciado na classe ParticipanteRepository, posso usá-lo no service através do @Autowired, pois não é possível usar um objeto instanciado em outra parte do código?
Comment: 1 - Sim, neste linha e variável recebe a instância da classe autamticamente. 2 - O objeto será instanciado na classe que for declarado. Neste caso ele está declarado dentro da classe ParticipanteService. Se precisar desta instância em outro local, pode inserir métodos getter públicos para então chamar parrticipanteService.getParticipanteRepository(). Não faz muito sentido usar desta segunda maneira. Em caso de dúvidas poste em outra pergunta.

JPA anotação @Autowired


Tomcat logs web interface

Question: I have several production machines running Java servlets on Tomcat infrastructure.
When I want to look at the logs, I have to remote access the machine with ssh, and manually grep the logs for errors.
Is there a recommended open-source, monitoring-oriented web interface to Tomcat logs?
Answer: Take a look at logstash. Its like Google for logs with loads of input and output adapters.

Tomcat logs web interface


Is the the derivation of the following motion equation correct?

Question: Suppose I have an undirected graph $G = (V,E)$, suppose $|V| = m$ and for each such vertex we associate a point mass $Q_1,\ldots Q_m$. Now we connect $Q_i$ and $Q_j$ with a spring if and only if $(Q_i,Q_j) \in E$.
Now imagine you have such system (call it a mesh) in space, no movement at first. So basically the kinetic energy of the system is given by
$$
T = \sum_{Q_i} \frac{1}{2}\lVert Q_i' \rVert^2
$$
Assume we are in a vector field defined as
$$
U(Q_i) = k_r \sqrt{x_i^2 + y_i^2} + k_g z_i
$$
the total amount of potential energy is given by
$$
V = \sum_i U(Q_i) + k_e \sum_i \sum_{j \in \mathcal{N}(i)} \left(\lVert Q_i - Q_j \rVert - l_{ij} \right)^2
$$
The motion equation can be derived by setting
$$
0 = \frac{\partial V}{\partial Q_i} + \frac{d}{dt}\frac{\partial T}{\partial Q_i'}
$$
And such equation leads me to
$$
0 = k_g \hat{e}_3 + k_r \frac{\text{Proj}_{span(e_1,e_2)} Q_i}{\lVert \text{Proj}_{span(e_1,e_2)} Q_i \rVert} + k_e \sum_{j \in \mathcal{N}{i}} \left(\lVert Q_i - Q_j \rVert - l_{ij} \right) \frac{Q_i - Q_j}{\lVert Q_i - Q_j \rVert} + Q_i''
$$
And this lead me to the motion equation
$$
Q_i'' = -k_g \hat{e}_3 - k_r \frac{\text{Proj}_{span(e_1,e_2)} Q_i}{\lVert \text{Proj}_{span(e_1,e_2)} Q_i \rVert} - k_e \sum_{j \in \mathcal{N}{i}} \left(\lVert Q_i - Q_j \rVert - l_{ij} \right) \frac{Q_i - Q_j}{\lVert Q_i - Q_j \rVert}
$$
Is the equation correct?
(I'm currently simulating it and I'm getting weird results, like instabilities).
What I'm trying to simulate is essentially like stretching some elastic material, assuming it is modelled with a triangular mesh where each triangle's edge has a spring connecting the two adjacent vertices.
The assumptions are uniform mass, I think even considering it it would cancel out.
Answer: Yes, a few things. First off, you used the potential energy of a deflected linear spring to be $k \Delta x^2$, but it's actually $\frac{1}{2} k \Delta x^2$. Secondly, I think deriving the equation of motion for an individual vertex using $F=ma$ is much easier here. You can then just write the equation of motion for an individual particle as:
$$-\nabla U(Q_i) + \sum_{j \in \mathcal{N}(i)} k_{ij} (||Q_i-Q_j||-l_{ij}) \frac{Q_j-Q_i}{||Q_i-Q_j||} = m_i Q''_i
$$
Then, if you're encountering stability issues, I would recommend checking that you're using a stable integrator. For equations like this, the Leapfrog or Verlet integrator is common. Leapfrog is easy to implement (explicit), accurate, and stable.
Comment: What do you mean with "equations like this"? (Just for reference). I also believe I might have stability issues (since I'm using a first order integrator).
Comment: By "like this", I mean large systems of second order equations usually associated with a many body dynamics simulation.
Comment: Just a side question, a large $k_{ij}$ compared to the other quantities should imply the body isn't very elastic right? If my force is constant the more I increase $k$ the harder should be to deform the spring right?
Comment: Indeed! It's the amount of force per unit deflection that the spring exerts. Larger $k$ makes more resistance to compression for smaller compressions. This is only a linear approximation though, and breaks down at large deflections. Check this out. https://en.wikipedia.org/wiki/Hooke%27s_law


Carregar table via AJAX

Question: Estou tentando carregar os dados de uma tabela, via ajax, desta forma:
function buscaFornecedores(id) {
    var url = "/Produto/BuscaFornecedor";
    $.ajax({
        url: url,
        type: 'GET',
        data: { id: id},
        success: function (data) {
            $("#tabelaf").html(data);
        }
    });
}

E aqui é como está no controller:
  [HttpGet]
    public ActionResult BuscaFornecedor(int id)
    {
        var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == id).ToList();

        return Json(new { Resultado = fornecedor_produto });
    }

Porém ele aparece em branco, o que está faltando no código ?
Edit:
O console log aparece desta forma:

Esse é o código da table:

                            Fornecedores

                        @foreach (var item in Model.ProdutosFornecedores)
                        {

                                @item.FornecedorProduto.Nome

                        }

Tentei utilizar, fazendo desta forma:
 public JsonResult BuscaFornecedor(int id)
{
    var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == id).ToList();

    return Json(new { Resultado = fornecedor_produto });
}

Porém também não deu certo. No MVC comum utilizaria JsonRequestBehavior.AllowGet, porém no MVC Core não é possível.
EDIT:
Tentei passando desta forma também, porém continua o mesmo problema:
public ActionResult BuscaFornecedor(int id)
    {
        var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == id).ToList();

        return new JsonResult(new { Resultado = fornecedor_produto });
    }

Comment: `$("#tabelaf").html(data.resultado);`?
Comment: Continua vindo em branco
Comment: O seu (fornecedor_produto) está sendo populado? Caso sim use:  return Json(fornecedor_produto , JsonRequestBehavior.AllowGet);
Comment: coloque um `console.log(data)` dentro da função `success` para ver no console do navegador o dado retornado e como está a estrutura das informações para você poder acessar.
Comment: @NetinhoSantos não deu certo.
Comment: @ThiagoMagalhães fiz o console.log, e coloquei a imagem com os dados retornado na pergunta.
Comment: Qual é exatamente o seu problema.? Ao vê o Edit da sua pergunta os dados estão sendo retornado no ajax.
Comment: nessa `#tabelaf` você quer retornar uma table?
Comment: Coloquei o código da tabela para que vocês possam entender. Isso @Barbetta quero retornar para ela os dados do ajax.
Comment: Primeiro, se você olhar seu JSON, vai ver que está vindo nulo em vários campos.
Segundo, ou você usa Razor, ou Ajax na sua tabela, as duas coisas vai ter overhead.
Terceiro, você está recebendo json e não html, use um for no javascript para que você printe uma linha da tabela para cada item do json recebido.
Comment: Eu só preciso dos dados fornecedor id e produto id. os outros campos irão ser retirados, creio que o problema não é este
Comment: @GustavoSantos você tem algum exemplo para me auxiliar ?
Answer: Acontece que você está retornando um Json e não o html da tabela.
Mude seu código da seguinte forma:
function buscaFornecedores(id) {
    var url = "/Produto/BuscaFornecedor";
    $.ajax({
        url: url,
        type: 'GET',
        data: { id: id},
        success: function (data) {
            $("#tabelaf").html(data).promise().done(function () {
                $(".modal").modal('show');
            });
        }
    });
}

[HttpGet]
public ActionResult BuscaFornecedor(int id)
{
    var fornecedor_produto = db.ProdutosFornecedores.Where(p => p.ProdutoID == id).ToList();

    return PartialView("_SuaPartialView", fornecedor_produto);
}

Agora irá retornar o html com as informações
Uma alternativa com menos código para o carregar as informações é fazer assim:
function buscaFornecedores(id) {
    $("#tabelaf").load("/Produto/BuscaFornecedor/" + id, function(){
        $(".modal").modal('show');
    });
}

Comment: Eu só não entendi a parte do PartialView @Barbetta, como vou vincular a tabela a partialview ?
Comment: Na parte que você adicionou o código da tabela aquilo deveria ser uma `PartialView`, ela não tem nada de diferente de uma `View`, o que muda é que ao retornar ela sua aplicação não irá passar pelo `ViewStart`. Nessa sua `View` ela tem um model, na parte do `PartialView("_SuaPartialView", fornecedor_produto);` você está indicando o nome dessa `View` e do lado o model do retorno
Comment: No `_SuaPartialView` mude o nome para o nome do arquivo `cshtml` que você deseja carregar, é importante que o modelo passado ao lado seja o `mesmo` que a View recebe
Comment: este código está dentro de um modal, não estou sabendo lidar com PartialView, estou utilizando mvc core.
Comment: Editei a resposta, coloquei para após carregar as informações ele abrir o modal
Comment: no mvc comum, funciona, eu fiz o teste, porém não consigo fazer funcionar no mvc core. :(

Carregar table via AJAX


For a non-surjective function, $f : \Bbb{R} \to \Bbb{R}^+$ is better than $f : \Bbb{R} \to \Bbb{R}$?

Question: Example:

From this we can tell no negative real number can be the image of any element of the domain. Thus not surjective because the range is not equal to the codomain, which means the function associates a any real number to a positive real number only. Is it better to write it this way $f : \Bbb{R} \to \Bbb{R}^+$ then?
Comment: In fact, we can easily define functions with co-domain $\{0,1\}$, but where the range is yet unknown!
Comment: The choice of codomain is often somewhat arbitrary. As I see it, a key point of the concept is simply to fix a context without necessarily identifying the range a function. It can be difficult to identify the range of a function (e.g. for a something like an even-degree polynomial $\mathbb{R} \to \mathbb{R}$ it can be clear that it maps real numbers to real numbers but can be difficult, and perhaps pointless for some purposes, to identify its minimal or maximum value, which you are kind of doing in your example but might not be able to do absolutely every time you want to define a function).
Comment: Consider for example if you'd want to use the quadratic formula absolutely every time you decide to consider a quadratic polynomial as a function. These polynomials are never going to map $\mathbb{R}$ surjectively onto $\mathbb{R}$. You certainly could adjust their codomains so that they are surjective, but why would you always want to?
Comment: $0$ is not a member of $\Bbb R^+$ but $f(0)=0$.
Comment: @DanielWainfleet Different conventions whether $\mathbb{R}^+$ means positive reals or nonnegative reals.
Answer: It depends on the context, but you're right that writing $f : \Bbb{R} \to \Bbb{R}^+$ contains superior information about the rule $x \mapsto |x|$. There are a few reasons why you might choose to write a more general codomain than what is called for, for example:

Sometimes a definition will tie you to a codomain. For example, the definition of function composition takes a function $f : A \to B$ and $g : B \to C$ and produces a function $g \circ f : A \to C$, defined by $(g \circ f)(x) := g(f(x))$. Now, if we take, for example, $g : \Bbb{R} \to \Bbb{R} : x \mapsto x^3 - x$, then we can only really compose with $f$ it is has codomain $\Bbb{R}$, to match the domain of $g$. Of course, it's a simple thing to do to extend a codomain (or even limit the domain of $g$ to $\Bbb{R}^+$), but one or the other should be done in order to make the composition kosher.
Sometimes the codomain is easy, but the range is unknown. As per Hagen von Eitzen's suggestion, we can take any object conjectured to exist (but whose existence is unknown) and turn it into a function to $\{0, 1\}$ with unknown range. For example:
$$f : \Bbb{N} \to \{0, 1\} : n \mapsto \begin{cases} 1 & \text{if $n$ is an odd perfect number} \\ 0 & \text{otherwise.} \end{cases}$$
Nobody knows the range of this function, but we do know the codomain is $\{0, 1\}$.
Sometimes, when you're writing mathematics, you choose a more general codomain, because writing the range as the codomain becomes an unproven assertion that demands proof, and writing a proof will interrupt the flow of the writing.
And sometimes there are pedagogical reasons. You might want to show your students that not every function needs to be surjective, to give the term "surjective" some meaning!
Answer: It depends on how you define what a function is (i.e., whether you take the set of ordered-pairs alone a la Bourbaki, or use something like the source-target predicate).
If you use the Bourbaki definition (i.e., $f\colon A\to B$ is $\{(a,f(a))\mid a\in A\}\subseteq A\times B$ and the Kuratowski ordered pair $(a,b)=\{\{a\},\{a,b\}\}$), then the codomain $B$ cannot be recovered from $f$.
On the other hand, if you build the codomain into the function as in source-target predicate, then you get the absolute value $\mathbb{R}\to\mathbb{R}$ and $\mathbb{R}\to\mathbb{R}^+$ are two different functions (which are related by the inclusion $\mathbb{R}^+\to\mathbb{R}$), which may or may not be what you want.


ClassNotFoundException: class JSONObject

Question: I want a servlet to return json on ajax request. For that I imported org.json.jar. But I am getting ClassNotFoundException for the class 'JSONObject' when I hit the url of the servlet.
I have included the jar in classpath too.
Comment: This will help us, Please show some of your code here.
Comment: I hope this http://stackoverflow.com/questions/7161846/getting-json-data-using-jquery-ajax-on-jsp-page?rq=1 may help you
Comment: I didn't add the jar into the servlet container's lib directory... on adding now its working fine...Thanks
Answer: Did you copy the jar file to your servlet container's lib directory? (like WEB-INF/lib)


ISPSS: Cross-sectional time series analysis

Question: I am trying to run a multiple regression in SPSS.
I am using panel data with 4 independent variables. Of these, one is a dummy variable.
Can someone please guide me through the process or give me some hints since I'm a beginner in this.
I have 350 companies. These companies have a "score" (dependent variable) for 4 different years. There are 4 different independent variables (1 dummy) that are affecting the dependent variable.
When i go to Analyze - Mixed model - Linear, i don't know how to proceed. I don't know what to put as "subjects" and "repeated", and i don't know which variables should be used fixed.
Comment: Did you try anything at all? Like Analyze > Regression > Linear??
Comment: Googling for "panel data SPSS" gives you lots of hits, for instance this one: http://www.ats.ucla.edu/stat/spss/library/gee.htm I suggest you work through some introductory material. If you have *specific* questions, we will be happy to try and help you. (But StackOverflow would be better for SPSS-related questions, CrossValidated aims at statistics, not programming.)
Comment: I made some edits to the text!
Answer: It really depends of what you want to know regarding your data. Are you interested in growth of your dependent? changes over time? or just your baseline? Can you reshape your data, and analyse in a different framework from mixed models? I'm assuming you may need to stick to mixed models by some reason.
Lets say your dependent is y, your independents are i1-i4, and your cases are id and your time variable is year, you could fit a model with following syntax:
<code>mixed y with year i1 i2 i3 i4
/print=solution
/method=ml
/fixed=intercept year i4 year*i4
/random=intercept year i1 i2 i3 | subject(id) covtype(un).
</code>
However, the parameters would only make sense if you know what your fitting to the data in each model. You could check http://www.ats.ucla.edu/stat/spss/examples/alda/ for different types of models for longitudinal data. That website has many examples from the Singer & Willet (2003) book on longitudinal analysis in different stat software (including r, stata, sas, spss, mplus, mlwin & hlm).
A main point to resolve is if you need to centred your covariates or not, and if you need to transformed your dependent or not. Most of the times, parameters are not directly interpretable and may required a variable transformation (e.g. log transformation, centering covariates). Centering variables is more tricky, because it really depends on what the covariate is. When is age, instead of mean centred, you can use a meaningful starting point (e.g age of entry, 14 years). In your case, I can't make any suggestions on that aspect, given I have no knowledge on what your covariates are.
Singer & Willet (2003) 'Applied Longitudinal Data Analysis' and the UCLA site with examples, are good reference to start.


React-testing-library - avoid rerender of child component

Question: I am trying to migrate a test from enzyme to React Testing Library
It's goal in enzyme was "should not force re-render of child if child is unaffected"
    const wasRendered = jest.fn();

    const Component = (props) => (

    );

    const ChildComponent = () => {
        wasRendered();
        return
Hello
;
    };

    const SkinnableParent = Skinnable(config, Component); //Skinnable is a higher order component applied from our code.
    const SkinnableChild = Skinnable(config, ChildComponent);

    const subject = mount(
        <SkinnableParent>
            <SkinnableChild />
        </SkinnableParent>
    );

    // Sending props to force parent to re-render
    subject.setProps({className: 'foo'});
    const expected = 1;
    const actual = wasRendered.mock.calls.length;

    expect(actual).toEqual(expected);

With testing library I am trying to use the rerender functionality.
    const wasRendered = jest.fn();

    const Component = (props) => {
        return

    };

    const ChildComponent = () => {
        wasRendered();
        return
Hello
;
    };

    const SkinnableParent = Skinnable(config, Component); //Skinnable is a higher order component applied from our code.
    const SkinnableChild = Skinnable(config, ChildComponent);

    const RootComponent = ({className}) => {
        return (
        <SkinnableParent className={className}>
            <SkinnableChild />
        </SkinnableParent>
        )
    };

    const {rerender} = render(<RootComponent />);

    rerender(<RootComponent className='foo' />);

    const expected = 1;
    const actual = wasRendered.mock.calls.length;

    expect(actual).toEqual(expected);

The problem is that the actual result is 2 with RTL. The Child Component is being rerendered as well.
Is there a way to achieve the same result as in the previous enzyme implementation?
Answer: There's nothing stopping the rerender of ChildComponent in that test. Your enzyme test is not testing what you thought it was. The only way to stop the rerender from the child component in this case would be to wrap it with React.memo
When a parent component is rendered, all the child components will re-render, unless stopped by memo or a shouldComponentUpdate implementation returning false.
React has good performance because even when a component is re-render, it may not need conciliation in the DOM (which is the heavy computational burden. See https://reactjs.org/docs/optimizing-performance.html#shouldcomponentupdate-in-action
You should't worry about avoiding re-renders unless the JS in the component is expensive
See this sandbox, replicating your test case, and you'll see it renders twice https://codesandbox.io/s/romantic-river-hcos6
in this case, using memo, it'll only render once https://codesandbox.io/s/condescending-lovelace-6z2j4
Comment: Thank you for your answer. I actually did not put the most relevant code. I updated the sample. The unit test is for the "Skinnable" component. Skinnable's context would generate a new context object every time it was rendered, which would be sent to all child components through context. To prevent this, the object is memoized so that it is not recreated unless the skin or variant actually changes.
Comment: There's nothing wrong with your RTL test, it is catching a bug in your Skinnable component


Django - Add products to favorite list

Question: I am building a supermarket store, using Django. I have to give user the possibility to add/remove items to/from the Favourite list.
So far i have done the following
models.py
class Product(models.Model):
    category = models.ForeignKey('Category', null=True, blank=True,
                                 on_delete=models.SET_NULL)

    sku = models.CharField(max_length=254, null=True, blank=True)
    name = models.CharField(max_length=254)
    description = models.TextField()
    price = models.DecimalField(max_digits=6, decimal_places=2)
    rating = models.DecimalField(max_digits=6, decimal_places=1, null=True,
                                 blank=True)
    image_url = models.URLField(max_length=1024, null=True, blank=True)
    image = models.ImageField(null=True, blank=True)
    favourites = models.ManyToManyField(User, related_name='favourites', blank=True)

    def __str__(self):
        return self.name

i created favourites folder and in the folder i have:
views.py
def favourites(request, product_id):

    product = get_object_or_404(Product, pk=product_id)
    if product.favourites.filter(id=request.user.ide).exist():
        product.favourites.remove(request.user)
    else:
        product.favourites.add(request.user)

    return render(request, 'favourites/product_favourite_list.html')

def product_favourite_list(request):
    user=request.user
    favourite_products = user.favourites.all()

    context = {
        'favourite_products': favourite_products
    }

    return render(request, 'favourites/product_favourite_list.html', context)

urls.py
urlpatterns = [
path('', views.favourites, name='favourites'),
path('favourites/', views.product_favourite_list, name='product_favourite_list'),

]
in product_details i have
def product_detail(request, product_id):
    """ A view to show individual product details """

    product = get_object_or_404(Product, pk=product_id)
    is_favourite = False

    if product.favourites.filter(id=request.user.id).exists():
        is_favourite = True

    context = {
        'product': product,
        'is_favourite': is_favourite,
    }

return render(request, 'products/product_detail.html', context)

product_details HTML has the following links
{% if is_favourite%}

      {% else %}

      {% endif %}

i can see the favourites page. when I click on individual products. I get the error as per the attached snapshot
Hope somebody can help. thanks in advance
Answer: You provide product.id to url product_favourite_list

So you should do:

You can also adjust urlpatterns, but I guess it doesn't make sense because you are showing all favourite products of user.
If you should have id in product_favourite_list add it to the path:
path('favourites/<int:id>/', views.product_favourite_list, name='product_favourite_list'),

Also add id as argument to your view:
def product_favourite_list(request, id):
    ...

Django - Add products to favorite list


Simple bash backup solution

Question: I have a very limited experience with shell scripting and would like to put in place some sort of script that backs up my blog (Wordpress) on a weekly basis.
Here is what I have so far
#!/bin/bash

# Determine current date
setenv CURDATE date +%Y%m%d

# Backup DB & email it to me
mysqldump dbname -u user -ppassword | gzip | uuencode ${CURDATE}dbname.sql.gz | mail -s "backup for dbname ${CURDATE}" firstname.lastname@example.com

cd /home/myhome

# Zip blog
tar cf - blog.mysite.me | gzip - > ~/backups/${CURDATE}blog.mysite.me.tar.gz

And this is where I am a bit stuck... I was thinking about emailing myself the blog directory but what happens when this becomes bigger then 10MB or so? how would I script this to split this and email me the chunks?
Another suggestion I have which I feel is better is to FTP the backups to another VPS that I own. But for the sake of space I would like to only keep the last 10 backups. How would I implement the part of the script script that:

Upload backup
Get list of files
Gets count of files in the current dir (e.g. /home/myhome/backups/blog )
If count > 10 delete oldest

Any help/advice or pointers with solving this problem would be much appreciated :)
Answer: Rather then zipping it and ftping, use rsync over the network.  If you make a new directory for each date, and  use the --link-dest option to link it back to the previous directory, it will only store the changed files, and files that don't change will be a hard link to the previous one.
I posted some code at What backup solution you use for linux servers
Comment: This is what I'm using for backups. It's really nice, because I have over 330GB of data backed up with a week's worth of daily snapshots (i.e. full, not incremental, backups), and yet the filesystem hosting those 7 days of backups uses the same amount of space! Expanding it to more days, or by adding weekly and monthly snapshots, would be the same story.
Answer: First, to set a variable from a command use
CURDATE=$(date +%Y%m%d)

instead of
setenv CURDATE date +%Y%m%d

If you want to leave only 10 days of backup, you can do that deleting the backup that was made 10 days ago. Use date relative commands to find it and save to another variable:
PAST_DATE=$(date +"%m-%d-%Y" --date="10 days ago")

Second, you can use lftp to execute ftp commands on a single line, like this:
lftp -u user,pass server -e "mrm *${PAST_DATE}.sql.gz; exit;

If you use the date on the file names, and each day remove the files that are 10 day old, you can do exactly what you are thinking. You can use lftp to put the files too, it works like a regular client, just that it works on a single line.
Comment: If you have shell access of some kind to the remote host (likely?) you can also use find to delete files older than 10 days: `find /backup/dir -type f -mtime -10 -exec rm -f '{}' \;`. This is probably simpler and more robust than figuring out the filename of the right file to delete.

Simple bash backup solution


How do I do a file upload with other form data in Drupal 6?

Question: I have this in my form function:
<code>$form['#attributes'] = array('enctype' => 'multipart/form-data');
... other form fields
$form['form']['form_file'] =  array (
    '#type' => 'file',
    '#name' => 'form_file',
    '#attributes' => array ('class' => 'textarea_crmform'),
 );
</code>
and when I call file_save_upload in my submit function, I am doing this:
<code>if ($file = file_save_upload('form_file'))
</code>
But it never gets into the above if statement.
Answer: My guess is that there's an error. Have you looked at your watchdog log? Is the temporary directory setup and writable by the web server?
Comment: The temporary directory is set up to be writeable. Also, I made a small form on the side as shown here: https://www.computerminds.co.uk/file-uploads-drupal-6-part-1 and it worked fine. I don't have any errors listed from the module I am making. The difference between my form and the example is that I have a 2D array and the example as a 1D array. I am wondering if this is the issue and how I would deal with using a 2D array.
Comment: What about checking the watchdog log ... anything there? Also, try naming the first dimension something else than `form` and remove the `#name` parameter.
Comment: Thank you. I changed the name from form, but this didn't seem to fix it. However, Removing the #name parameter did it. Any idea as to why that is?
Comment: Not sure - the input name usually comes from the array key, so maybe since they were the same value, something kooky happened. Glad it works now! Back away slowly... ;)


Two contradictory statement in the book  by Stephen Willard

Question: There is a statement In 8D

If $X_\alpha$ is a nonempty topological space and $A_\alpha \subset X_\alpha$, for each $\alpha \in A$, then $\prod A_\alpha$ is dense in $\prod X_\alpha$ if and only if $A_\alpha$ is dense in $X_{\alpha}$, for each $\alpha$.

There is a statement In 8E:

Let $X_\alpha$ be a nonempty topological space for each $\alpha \in A$, and let $X = \prod X_{\alpha}$. If $b_\alpha$ is a fixed point in $X_\alpha$, for each $\alpha \in A$. Then $B = \{x \in X | x_\alpha = b_\alpha \text{except for finitely many}   \alpha \in A\}$ is a dense set in $X$, i.e. $Cl_XB =X$

If 8D is true, How can a single point in $X_\alpha$ be dense ?
Comment: $B$ is not of the form $\prod A_\alpha$, so your first statement says nothing about it.
Comment: The set $B$ is not a cube (it cannot be written as the product of subspaces of the $X_\alpha$)
Comment: @RobertIsrael I think B is of the form: $\prod {b_\alpha} \times \prod X_\alpha(finitely)$
Comment: No, that's wrong. Actually the right conclusion is that 8D and 8E together imply that $B$ therein is not of the form $\prod_\alphaA_\alpha$ (unless perhaps in some pathological situations).
Answer: $B$ is not of the form $\prod_\alpha A_\alpha$ so that theorem does not apply.
It can be written we a union
$$B = \bigcup_{F \subseteq A\text{ finite }} \{\prod_{\alpha} A_\alpha : \forall \alpha \in F: A_\alpha = X_\alpha; \forall \alpha \notin F: A_\alpha = \{b_\alpha\}\}$$
but that's a lot bigger than just a single such product... And each subset we take a union of is not dense, but their union is dense, sort of like no $\{q\}, q \in \Bbb Q$ is dense, but their union is.


How to expand mobile app to app for tv (android)?

Question: I'm trying to expand my application to make it available on TV. I did as they say in this guide Getting Started with TV Apps
Here is my Manifest:
<uses-feature
    android:name="android.software.leanback"
    android:required="false" />
<uses-feature
    android:name="android.hardware.touchscreen"
    android:required="false" />

<application
    android:name=".NetworkAccess.MyApplication"
    android:allowBackup="true"
    android:banner="@drawable/banner"
    android:icon="@mipmap/ic_launcher"
    android:label="Amazon Codes"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".StartActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".TvActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|navigation"
        android:theme="@style/Theme.Leanback"
        android:logo="@drawable/banner" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LEANBACK_LAUNCHER" />
        </intent-filter>
    </activity>
</application>

I also added a folder layout-w960dp in res in which i created layout for TvActivity. But when I update my application in the store, it is still not available for TVs.
What's my mistake?
Comment: Did you check [TV app quality](https://developer.android.com/develop/quality-guidelines/tv-app-quality.html) guide by Google? I don't know if it's necessary but "Only apps that meet the following quality criteria will qualify as an Android TV app on Google Play."
Answer: You have to create a new module in the project for Android TV.
Same is for Wear.
Answer: You have to create a new module my friend.
Your project structure should look like this:

As you see I have a core module too. In the core you can put all the code that is shared between the two modules (usually utils, constants, networking stuff and so on).
After that, your settings.gradle should have the new modules specified like this:
include ':app', ':core', "tv"

Comment: what about the package name? How will it look in the store? Will there be 2 different applications?
Comment: [Distribute on TV](https://developer.android.com/distribute/best-practices/launch/distribute-tv.html)

Switch to advanced mode in the Developer Console to see the tv options.

You can use the same package name.

Basically you will have 2 apks submitted to the store, each one available to the devices supported. The tv one will be visible to Android TV devices and the other one to mobile devices.
Comment: It can be done so that the store would see one apk, but this apk could be used for both TV and phone?
Comment: I really don't know if it is possible honestly. If you have a multimodule project in Android Studio you can see that when you try to export a signed apk it will ask you if you want to do that for the tv or for the app. My guess is that you have to do different apks.


ReactJS + Redux: How to use Material-UI's RaisedButton as ?

Question: I want to use Material-UI's RaisedButton ( http://www.material-ui.com/#/components/raised-button ) as <input/> so I tried the following:
      <RaisedButton
        containerElement={<input type="file" onChange={this._handleImageChange}/>}
        label="Upload Image"
        labelColor='#88898C'
        labelStyle={{textTransform:'intial'}}
        backgroundColor='#1C1C1F'
      />

but I get an error Invariant Violation: input is a void element tag and must not have "children" or use "props.dangerouslySetInnerHTML". Check the render method.
Is there a way to do so? I would like the RaisedButton to act like that of <input type="file" onChange={this._handleImageChange}/>
Thank you in advance!
Comment: What do you think the `containerElement` is supposed to do?  I can't find it in the documentation.
Comment: OK, it seems `containerElement` is the element that will contain the button.  What are you attempting by putting the `input` there?  Are you trying to do a file upload with a `RaisedButton`?
Comment: @DavinTryon Yup that's exactly what I want to do!
Answer: The value of "containerElement" will be the element that contains your button (in other words, the button's parent or wrapper). An HTML input is not allowed to contain any other elements, hence the error.
Put your input as the button's child:
<RaisedButton label="Upload Image"
              labelColor='#88898C'
              labelStyle={{textTransform:'intial'}}
              backgroundColor='#1C1C1F'>
  <input type="file" onChange={this._handleImageChange}/>
</RaisedButton>


Stuck in a probability question (revolves around expectancy)

Question: I am stuck in a probability question that revolves around expectancy:
Alice and Bob play a game- Bob chooses randomally a number between 0 and 100 (included) and Alice guesses it. Then Alice pays Bob the square of the gap between her guess and what he chose.
A. What guess Alice should make to minimize the expectancy of the payment
B. How much money does Bob have to pay alice to make the game fair (i.e: to make the expectancy of Alice and Bob gain $0$) if Alice uses the technique from A.
This is what I did:
Random variable $X$- the number that bob chooses.
$X\sim \text{unif}\{0,...,100\}$)
a- Alices guess
$Y=(X-a)^2$
then I proved $E(Y)$ is minimized when $a=E(x)=50$
(hopefully this is correct).
But for B I am stuck-
I said he should pay her the value of $E[Y]$ when $a=50$.
but here I am stuck-
$E((x-a)^2)=E(x^2)-2aE(x)+E(x)^2=E(x^2)-E(x)^2$
(because from A we know $a=E(x)$)
I read online it's var(x) which turns out $100^2/12=833.3333$. Unfortunately we are yet to learn Variance so is there a way to continue from where I stopped here without using Variance?
Thank you!
Comment: Basically you just need to calculate $\mathbb E[X^2]$. Can you see why this is equal to $(1^2+2^2+\dots+100^2)/100$ ?
Comment: @jlammy - there are $101$ possible values including $0$
Comment: [Here's a MathJax tutorial](https://math.meta.stackexchange.com/q/5020/104041) :)
Comment: Thank you very much!
The think is that when I do that I get that E[x^2]=3350 and E[x]^2=50^2=2500 so I get that he has to pay her 850$. However when I used the var equation I got 833.333
Why is this difference? whichone is the correct answer?
Comment: @DR_2001 I believe your answer of $\ \$850\ $ is correct.  *That* is the variance of $\ X\ $. Why do you think that $\ \text{var}(X)\ $ should be $\ \frac{100^2}{12}\ $ instead?
Comment: @lonzaleggiera that's a formula for varaince of uniformly distributed random variable that I found online, but I guess I should stick to definitions.
Thank you very much!
Comment: I'm guessing the formula you found is [this one](https://math.stackexchange.com/questions/728059/prove-variance-in-uniform-distribution-continuous).  That's the formula for the variance of a *continuous* uniform distribution—that is, the random variable associated with the distribution can assume *any real* value between $\ a\ $ and $\ b\ $.  The random variable you're dealing with has a *discrete* distribution, in that it can only take on *integer* values between $\ 0\ $ and $\ 100\ $.
Answer: You're not stuck, since you've already got the answer. You've already determined that Alice's guess $\ a=50\ $, and therefore the amount she has to pay Bob is $\ (X-50)^2\ $.  To make the game fair Bob must therefore pay Alice
\begin{align}
E((X-50)^2)&=E(X^2)-E(X)^2\\
&=E(X^2)-2500
\end{align}
Forget about the fact that you haven't yet learned the (essentially trivial) fact that this is called "the variance" of $\ X\ $. What you need to do is calculate $\ E(X^2)\ $, which is, by definition,
\begin{align}
E(X^2)&=\sum_{x=0}^{100}x^2P(X=x)\\
&=\sum_{x=1}^{100}\frac{x^2}{101}\ .
\end{align}
I see from one of your comments that you've already obtained the correct value of $\ 3350\ $ for this sum, and therefore the correct value of $\ 850\ $ for the variance of $\ X\ $.
Comment: Thank you very much!


Martingale property of an stochastic integral, stopping time, stopping theorem

Question: before asking my question regarding of the martingale property of the stochastic integral I want to define some of the notations in the book I'm following:

$\mathcal{M}^2$ is the set of continuous martingales $(M_t, t\in \mathbb{R}^{+} )$ in $L^2$ such that $M_0=0$.
For $M \in \mathcal{M}^2$, let $L^2(M)$ denote the set of progressively measurable processes $(H_s, s \in \mathbb{R}^{+} )$ such that:
$$ \mathbb{E} \left[ \int_{0}^{T} H_s^2 d\langle M \rangle_t \right] < \infty $$

I would like to prove that, if $M, N \in \mathcal{M}^2$, $H \in L^2(M) $ and $K \in L^2(N)$, then for every finite stopping time $T$, we have:
$$ \mathbb{E} \left[ \left( \int_0^{T} H_s dM_s \right) \left( \int_0^{T} K_s dN_s \right)\right] = \mathbb{E} \left[ \int_0^{T} H_sK_s d\langle M,N \rangle_s \right]$$
My first approach was to prove that $L_t := (H \cdot M)_{t}(K \cdot N)_{t} - \langle H\cdot M,K \cdot N \rangle_t$ is a martingale $L^2$ (ie $L_t \in \mathcal{M}^2$) and so we can apply the stopping theorem and knowing that $L_0=0$ we would have proven the equality. It is not very clear that it is a martingale, but by definition of the quadratic variation, it is a local martingale. I don't know how to prove that $\mathbb{E} [L_t^2] < \infty$. I've tried with Kunita-Watanabe inequality but I don't find the right conditions that $L_t$ should verify for being able to apply the stopping theorem.
On the other hand, I've been thinking that given the stopping time $T$ is finite, then if $L_t$ is a martingale, the stopped martingale $L_t^T$ is bounded by the value of the martingale in $T$ so, $L_t^T \in \mathcal{M}^2$
and we would have the desired equality.
I'm not sure if my reasoning make sense, and if it possible to prove my first idea. Every help is much appreciated. Thank you!
Answer: I do not believe this result holds, consider for example two independent one dimensional Brownian motions $B_s$ and $B'_s$. Their quadratic covariation is equal to zero at every timepoint, whilst $⟨B'⟩_s = ⟨B ⟩_s = s$, meaning the right side of your equation is always zero while the left side isn't necessarily.
Comment: Then one the left side it should be the integrals w.r.t. the processes themselves, not w.r.t. their quadratic variation
Comment: I think it holds as it is an exercise on the notes that I'm following [Page 54, exercise 5.2.6]: https://www.lpsm.paris/pageperso/fournier/PolyCS.pdf   Also I think maybe your reasoning is not right because if the brownian motions are independent, then we can separate the expectation on the left side, and we would also get zero, as the expectation of the stochastic integral defined like that is zero.
Comment: In this case you can show the result using remark 5.1.10 ii) of the document you linked in combination with the polarization identity applied the quadratic variation
Comment: You're absolutely right, I made a mistake on the writing, and already corrected it, thank you. Is that remark applicable in that case? as we don't have that $L_t := (H \cdot M)_{t}(K \cdot N)_{t} - \langle H\cdot M,K \cdot N \rangle_t$ is a martingale (or I don't know how to prove it), but a local martingale, and T is a stopping time.


Number of comparisons of putting a sorted array of size $n$ into $k$ boxes

Question: Suppose I have an array of numbers with size $n$, denoted as $A$ and the numbers inside have already been sorted from smallest to largest. I have $k$ boxes, and $k<n$. For each box, there is a number, called splitter. Suppose the splitter of box $i$ is smaller than that of $j$, if $i<j$. Denote $B$ is an array whose $i$-the element is for the splitter of box $i$ and we have $B[i]<B[j]$ if $i<j$.
I want to put the numbers of the array into the boxes in the following way. If $A[0]<B[0]$, we put $A[0]$ into box 1, and compare with $A[1]$ and $B[0]$. If $A[0]>B[0]$, we then compare $A[0]$ and $B[1]$ and repeat the procedure above, until we put all the number into the box. If there is a $i$ such that $A[i] > B[k]$. We then leave those elements in $A$.
Then how many comparisons in total for this process. Is it $\mathcal{O}(n+k)$?
Many thanks
Answer: The number of comparisons is not just $O(n+k)$: it's at most $n+k-1$.
To see that's the case, call a box "full" as soon as you find an element that is larger than the splitter of the box (which means no more elements will go into the box). Then note that, every time you compare an element to the splitter of the current box either the element is smaller, in which case you put it into the box, or it is larger, in which case you declare the box full. In either case, you have "eliminated" either a box or an element. Since you start with $k$ boxes and $n$ elements, and you end up either with at least $1$ non-full box (if some splitter was larger than every element) or with at least $1$ un-boxed element (if at least one element was larger than every splitter), you can perform no more than $n+k-1$ comparisons.
It's also easy to see that in some cases you perform exactly $n+k-1$ comparisons: e.g. if all elements are smaller than the largest splitter, but larger than every other splitter, you perform $k$ comparisons on the first element, and $1$ comparison on each of the remaining $n-1$ elements.


A set of 12 cards contains 7 red and 5 black

Question: As the question follows,

In how many ways if we do not want $4$ cards of the same colour?

My solution so far
Total number of options of selecting 4 any cards: $495$
The number of options of $4$ of the same colour is: $175$
Therefore that means $495 - 175 = 320$ options that dont include $4$ cards in the same colour
My workings to get the answer above are:
$\dbinom{12}{4,8}$ Any selection of cards
$\dbinom74 \dbinom{5}{4}$ This is when there is either $4$ of red or  $4$ of black.
Question
Would the answer $320$ be the correct answer?
Comment: In how many ways ***what*** if we do not want four cards of the same colour?
Comment: If here are 7 red and 7 (instead of 5) black, would the answer be ${14\choose4}-{7\choose4}*{4\choose4}$?
Comment: This is not a permutations question since we are not arranging the cards, just selecting them.
Comment: Your idea is correct but it's C7,4 + C5, 4. I was thinking that I was wrong because your idea is right but our answers differ.
Answer: There are only two colors, so you may first list all different size pairs first and then assign color for each of two stacks.
Consider stacks of size $3$ and $1$:
$(3, 1)$: ${7\choose 3}\cdot{5\choose 1} + {5\choose 3}\cdot{7\choose 1} = 35\cdot5+70=245.$
For this case: (3 red 1 black) + (3 black 1 red)
$(2, 2)$: ${7\choose 2}\cdot{5\choose 2} = 21\cdot 10 = 210$
So the total is $245+210=455.$
Answer:
In how many ways can $4$ cards be selected from $7$ red and $5$ black cards if we do not want $4$ cards of the same colour?

Let's correct your solution.
There are
$$\binom{12}{4}$$
ways to select four of the twelve cards.
From these, we must subtract those selections in which all of the selected cards are the same colour.
There are
$$\binom{7}{4}$$
ways to select four of the seven red cards.
There are
$$\binom{5}{4}$$
ways to select four of the five black cards.
Thus, the number of selections of four cards in which not all of the cards are of the same colour is
$$\binom{12}{4} - \binom{7}{4} - \binom{5}{4} = 495 - 35 - 5 = 455$$
Your error:  The number of ways of selecting four red or four black cards is found by adding the number of ways of selecting four black cards to the number of ways of selecting four red cards.
$$\binom{7}{4} + \binom{5}{4}$$
Your calculation
$$\binom{7}{4}\binom{5}{4}$$
counts the number of ways of selecting four red and four black cards, which is found by multiplying the number of ways of selecting four black cards by the number of ways of selecting four red cards.
When you see the word or, you should add.  When you see the word and, you should multiply.
Comment: Thank you very much for your answer, as that gives another insight on how to approach this problem. Thanks


Different size of vocabulary made by Weka and R's tm

Question: I own around 40,000 text files for preprocessing (in purpose of document classification). I used R (with tm package) for prototype and now looking for a equivalent Java library for products.
However, for very fundamental tasks, i.e. text preprocessing, I found a very strange problem. That, with Weka, I apply punctuation and stop-words removal, and the same operations with R. Basically, the generated vocabulary (terms) size should be relatively the same. However, weka returns a vocabulary (attributes in arff file) with only 35,000 terms, while in R, there are more than 1 million distinct terms.
Can anyone help me understand this problem, or introduces me some more reliable Java libraries for text preprocessing?
Answer: Did you apply the StringToWordVector in Weka? If so, then you did more than just punctuation and stop-words removal. StringToWordVector outputs only the doc-term matrix of the input text files, so once the above mentioned preprocessing is done Weka will create 1 term for each unique word. 35k terms sounds logical for 40k texts.
The preprocessing in R seems to have been only the punctuation and stop-words removal. So 40k documents results in 1M words, but not unique words. Are your text files approximately 25 words on average? If this is not the case, then there is something else going on indeed.
Comment: I would say your hypothesis is not my case. For me, every document is larger than 1KB, and in my opinion, for **TM** package in R, when it constructs the doc-term matrix, all terms are indeed unique ! So I think Weka has do something more than expected, but I don't know what it is exactly .
Comment: I see. Could you specify which R and Weka commands/actions you performed?
Comment: I can hardly paste the full code in the comment here, so I explain you briefly. For R, I create VCorpus object which holds all 40K texts, then I use **tm_map** function with **tolower,removePunctuation,removeWords** filters. As to Weka, I create an **Instances** object which contains all texts, and a **StringToWordVector** filter which set usestoplist true, and also give options (L,C), which means to lowercase and output word counts rather than boolean word presence. That's all operations I take. I don't see big differences. How about you ?
Comment: I am guessing is it possible that tm package and Weka use different tokenization algorithms, which results in different vocabulary size. What do you think of this?


Are all basic sets open? (and other issues of confusion for me)

Question: According to Willard,
If $(X,\tau)$ is a topological space, a base for $\tau$ is a collection $\mathscr{B} \subset \tau$ such that $\tau=\{ \bigcup_{B \in \mathscr C} : \mathscr C \subset \mathscr B\}$. Evidently, $\mathscr B$ is a base for $X$ iff whenever $G$ is an open set in $X$ and $p \in G$ there is some $B \in \mathscr B$ such that $p \in B \subset G$.
Question 1: Is it safe to assume that in the sentence beginning with "Evidently" it is assumed that $\mathscr B \subset \tau$, for otherwise the iff statement is not true.
Question 2: I've been told that not all basic sets are open, but it seems by the above definition that they are defined to be open.
Comment on Question 2: There is also a definition of being a base for "a" topology.  Is this what was meant by not all basic sets are open? Is this just a semantic issue, i.e. the basic sets are open in the topology that the base is a base for but not open in general? Or can there be a base for a topology where the basic sets are not open in that same topology?
Comment: One can also consider *bases of neighborhoods*, and then its elements are not necessarily open.
Answer: Question 1: Yes. It says so in the first sentence. Unless I am misunderstanding your question.
Question 2: Every base is a subset of the topology it generates. By definition, all sets in a topology are open. Hence all sets in a base also have to be open. Hence "no" to your last question in the comment to question 2.
Answer: Question 1: Yes, Willard was still talking about collections $\mathscr{B}\subseteq\tau$. However, with a minor change in wording he wouldn't have to be, because we could deduce that $\mathscr{B}\subseteq\tau$. Specifically, suppose that $\langle X,\tau\rangle$ is a topological space, and that $\mathscr{B}$ is a family of subsets of $X$ such that

$(*)\quad$ for each $G\subseteq X$, $G\in\tau$ iff for each $p\in G$ there is some $B_p\in\mathscr{B}$ such that $p\in B_p\subseteq G$.

If $B\in\mathscr{B}$, the condition is satisfied if for each $p\in B$ we let $B_p=B$, so $B\in\tau$, and therefore $\mathscr{B}\subseteq\tau$. Thus, he could have said that a family $\mathscr{B}$ of subsets of a space $X$ is a base for the topology of $X$ iff $(*)$ holds. He wouldn't have had to specify that the members of $\mathscr{B}$ are open, because that follows from $(*)$.
Question 2: You were misinformed: basic open sets in a topological space are always open sets in that space. If $\mathscr{B}$ is a base for a topology $\tau$ on a set $X$, then $\mathscr{B}\subseteq\tau$.
Question 2': There is a difference between '$\mathscr{B}$ is a base for the topology $\tau$ on $X$' and '$\mathscr{B}$ is a base for some topology on the set $X$'. The definition from Willard at the beginning of your question is the definition of the first of these. However, it is also possible to characterize the collections of sets that are bases for some topology on a given set.

Let $X$ be a set, and let $\mathscr{B}$ be a family of subsets of $X$. Then the following are equivalent:

$\mathscr{B}$ is a base for some topology on $X$.
$\left\{\bigcup\mathscr{A}:\mathscr{A}\subseteq\mathscr{B}\right\}$ is a topology on $X$.
$\bigcup\mathscr{B}=X$, and whenever $p\in X$, $B_1,B_2\in\mathscr{B}$, and $p\in B_1\cap B_2$, there is a $B\in\mathscr{B}$ such that $p\in B\subseteq B_1\cap B_2$.

(This is worth trying to prove if you've not seen it already.)
Answer: Q1: Base sets are open, indeed: consider the one element $\mathscr C$ subsets.
Q2: Lied. Or thought about something else..
A system $\mathscr B$ of subsets of a set $X$ can be a basis for a topology iff $\forall B,C\in\mathscr B\ \forall x\in B\cap C\ \exists D\in\mathscr B$ such that $x\in D\subseteq B\cap C$'. This guarantees that $\tau$ as defined above, will be closed under finite intersection.


Read an unsupported mix of union types from an Avro file in Apache Spark

Question: I'm trying to switch from reading csv flat files to avro files on spark.
following https://github.com/databricks/spark-avro
I use:
import com.databricks.spark.avro._
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val df = sqlContext.read.avro("gs://logs.xyz.com/raw/2016/04/20/div1/div2/2016-04-20-08-28-35.UTC.blah-blah.avro")

and get
java.lang.UnsupportedOperationException: This mix of union types is not supported (see README): ArrayBuffer(STRING)

the readme file states clearly:

This library supports reading all Avro types, with the exception of
  complex union types. It uses the following mapping from Avro types to
  Spark SQL types:

when i try to textread the same file I can see the schema
val df = sc.textFile("gs://logs.xyz.com/raw/2016/04/20/div1/div2/2016-04-20-08-28-35.UTC.blah-blah.avro")
df.take(2).foreach(println)

{"name":"log_record","type":"record","fields":[{"name":"request","type":{"type":"record","name":"request_data","fields":[{"name":"datetime","type":"string"},{"name":"ip","type":"string"},{"name":"host","type":"string"},{"name":"uri","type":"string"},{"name":"request_uri","type":"string"},{"name":"referer","type":"string"},{"name":"useragent","type":"string"}]}}
<------- an excerpt of the full reply ------->

since I have little control on the format I'm getting these files in,
my question here is - is there a workaround someone tested and can recommend?
I use gc dataproc with

MASTER=yarn-cluster spark-shell --num-executors 4 --executor-memory 4G  --executor-cores 4 --packages com.databricks:spark-avro_2.10:2.0.1,com.databricks:spark-csv_2.11:1.3.0

any help would be greatly appreciated.....
Comment: you can use newHadoopApi for reading avro files - you'll need to use core api of spark and not sql-api. any special reason not to use it?
Comment: http://stackoverflow.com/questions/23944615/how-can-i-load-avros-in-spark-using-the-schema-on-board-the-avro-files
Answer: You won't find any solution that works with Spark SQL. Every column in Spark has to contain values which can be represented as a single DataType so complex union types are simply not representable with Spark Dataframe.
If you want to read data like this you should use RDD API and convert loaded data to DataFrame later.


Question regarding hook_user_presave() and setting roles

Question: When a new user is created, either via admin or by normal user registration, I need to assign a custom role to that user.  It works fine, but I'm not convinced I've checked the user status enough.
In my hook_user_presave() implementation, I check to make sure the administration role isn't set, and uid isn't 1.  To set my role:
if ($account->uid == 1 || array_search('administrator', $account->roles))
        return;

$account->roles[] = 'my_custom_role';

I only want to do this if it is a brand new user being created/inserted.  The documentation says hook_user_presave() is called if the user is being updated.  What is the best way to check if this is a brand new user creation?  Would uid be set to 0?
Answer: The documentation for hook_user_presave() says the hook is invoked when an user account is updated, or created, not just when the user account is updated.
If you look at user_save(), you will notice that $account->is_new is set before invoking hook_user_presave(), when the user account is being created. If the account is being updated, $account->is_new is not set, and $account->original contains the user object before any changes.
In Drupal 8, $user->is_new will be replaced with Entity::isNew().
Answer: It looks like the $account->is_new flag is how to test.  I've been testing and it seems to be the way to go.
Answer: hook_user_presave() is fine, just pay attention to $account->is_new.
And save fields in the $edit variable in the usual format, ex:
if ($account->is_new) {
  $edit['field_time'][LANGUAGE_NONE][0]['value'] = (string)time();
}

$edit is passed as reference and so you don't need to do anything else.
Answer: Try hook_user_insert which is called only when saving a new user.
Also, make sure that $account->uid is set. If uid is set, that means some save function( user_save() ) is called before.
Also, try in_array() instead of array_search. You just wanted to check if the role is in the array, which in_array does out of the box.
Comment: I looked at hook_user_insert(), but that is called after the insert.  If there was a quick API function I could call to pass a UID and roll id, then that would work as well.
Comment: Yes.. so you have the uid there. Also, make sure that you pass $account by reference (&$account) or your changes won't get saved.
Comment: What is a quick API call to set a role to a user?  I couldn't find one.


Cakephp 3.0 - Validating Input against data in another model

Question: I'm a pretty new to Cakephp 3.0 and I'm quite stuck on data validation, or rather Application Rules.
I have a simple user registration form to create a new user in the table 'users'.  In the user registration form, there is a field for "ticket_number" where the user has to enter a ticket number which must exist in the table tickets.ticket_number, and also tickets.registration_status must be false (that ticket had not had a user registered with it yet).
My tables look like this (simplified:)
users:
id | username | password

tickets:
id | ticket_number | user_id | registration_status

In my users model, I have defined (user can have many tickets):
    $this->hasMany('Tickets', [
        'foreignKey' => 'user_id'
    ]);

In my Tickets model, I have defined (a ticket belongs to a user):
    $this->belongsTo('Users', [
        'foreignKey' => 'user_id',
        'joinType' => 'INNER'
    ]);

In the users model, I added a rule to attempt to check whether the entered ticket_number exists in the tickets.ticket_number column:
    public function buildRules(RulesChecker $rules)
{
    $rules->add($rules->isUnique(['username']));
    $rules->add($rules->isUnique(['email']));
    $rules->add($rules->existsIn
               (['ticket_number'], 'Tickets.ticket_number'));

    return $rules;
}

This gives me the error:
Error:  Call to a member function primaryKey() on a non-object

I'm very stuck here.  Can anyone advise on the right approach implementing this check?  And then, how to also implement the check to see whether the associated registration_status is 0 (that ticket has not been registered yet).
Thanks in advance for any advice!
--D.
Comment: UPDATE:  I updated my rule to this: `$rules->add($rules->existsIn(['tickets.ticket_number'], 'tickets'));` and the Error goes away, but it seems the check does not succeed.  It does not fail with a ticket number that does not exist.
Comment: still this needs to read $rules->add($rules->existsIn(['tickets.ticket_number'], 'Tickets'));
Capital T on the Tickets, your relation is called like: $this->hasMany('Tickets'); this is what it refers too.
Answer: The existsIn rule will not solve your problem, as it will not take your additional condition into account (registration-status).
You will need to provide a custom rule to accomplish the problem you described.
Regarding your update: The rule will return true - so passing the rule, if your tables are not setup correctly or it cannot find the field you are referencing or your field is nullable, check the source here: http://api.cakephp.org/3.0/source-class-Cake.ORM.Rule.ExistsIn.html#74-91
Answer: OK, after considerable tinkering, I solved the first issue (On the user add form, the user must enter a ticket number which must exist in the table tickets.ticket_number.)
The only way I could get this to work using an existsIn rule in the UsersTable model, was to declare the ticket_number field as the primary key in the initialize method of the TicketsTable model:
$this->primaryKey('ticket_number');

Then the associations and existsIn rule in the UsersTable model worked.  However, this isn't good as I don't want the primary key in tickets permanently set to ticket_number.  It also doesn't solve my second problem (checking tickets.registration_status is false.)
So I took a different approach and instead used $this->loadModel('Tickets'); in the add method of my users controller.  Then, I could easily perform the required checks:
//Look for this particular ticket
    $ticketCount = $this->Tickets->find()->where(['Tickets.ticket_number' => $ticket_number])->count();
    $this->set(compact('ticketCount'));
    $this->set('_serialize', ['ticketCount']);
//If the ticket isn't found, then return with the Flash error
    if (!$ticketCount > 0) {
        $this->Flash->error(__('Sorry, but a Ticket with this number could not be found. Please, try again.'));
                   return;
    } //otherwise move on...

I then similarly query the registration status of the ticket, and flash a different error back if it is already registered.
After the checks are passed, it goes on to the normal Add User checks and saving.
While this solution isn't as elegant as what I was hoping to do in the Users model with a custom association and rule, it works.
I'm also not convinced it can't be done in the model with a rule.  But for now, this solution works.
Anyway thanks hmic for a response.
DBZ


Different value of offsetwidth in firefox and chrome

Question: I'm getting different values for offsetwidth for the same element in Firefox and Chrome, which makes the element display incorrectly. Please suggest some solution so as to have consistency.
Comment: Different browsers will interpret offsetHeight and offsetWidth differently, you will receive different values unless heights and widths are explicitly stated (in px).
Comment: @GGG : it.style.width = it.offsetWidth + 'px';
 it.style.height = it.offsetHeight + 'px';
where it is object of element referred.
Comment: Can you give us some more information, such what elements you're styling, what they look like and what you want them to look like? You could e.g. post some code to jsfiddle.net with a reproducible case.
Answer: I'm having a similar issue when I load content via AJAX with Firefox 12.
Both Chrome and IE9+ report the offsetWidth correctly after the the ready state is complete, but Firefox hasn't seem to have calculated it yet.
If you check a bit later (even delaying a half second sometimes), Firefox has the right width.
I suspect someone tried to make Firefox faster by firing the ready signal before they should be.
The original poster didn't give a lot of details, but I suspect this might be the problem.
Edit: P.S. The only way I've found to get this to work correctly is to delay and keep checking back until it has the right value.
Answer: I had problem with offsetHeight, I replaced it to scrollHeight and it works for me.