Calculating the probability my observation, $Y_i$, is drawn from a random variable $X$?

Question: If I sample a population distribution 2,000 times and get an estimator for the population mean, $\mu$, and the standard deviation, $\sigma$, how can I use these to get the probability that an observation is part of the population distribution?
Mathematically, say I sample the population distribution and get estimators for the mean and standard deviation. I assume my population is distributed:
$$ X \sim \mathcal{N}(\mu, \sigma) $$
where $X$ is a normally distributed random variable, $\mathcal{N}$ is a normal distribution, $\mu$ is an estimator for the population mean, and $\sigma$ is an estimator for the population standard deviation.
How can I use this distribution to test the probability that an observation, $Y_i$, has been drawn from the population distribution?
Comment: How is $Y_i$ related to $\bar X$?
Comment: This sounds like part of a simple hypothesis test. Just compute $$z = \frac{Y_i - \mu}{\sigma}$$ and then to compute the probability *density* of $Y_i$ being sampled from $\mathcal N(0,1)$, compute $f(z | 0,1)$, where $f$ is the standard normal pdf. Alternatively, for a more standardized approach, see the [Student's $t$ test](https://en.m.wikipedia.org/wiki/Student%27s_t-test)
Comment: @Connor I don't mind what you use, but use different notation for different things, and explain it clearly. In standard theory, by the way, the sd of the sampling distrbution of the mean is $\sigma/\sqrt{n}$ if $\sigma$ is the sd of the underlying population. Also, if $\bar X$ is meant to be a mean of observations called $Y_i$, chances are $\bar Y$ would be a more intuitive notation.
Comment: Do you mean $P(Y_i\sim N(\hat\mu,\hat\sigma)\vert Y_i = y)?$ // Yes, it matters why you want to know. Many questions about statistics (among other fields) are asked because someone is trying to solve a problem, develops an idea for a solution, gets confused about that idea, and asks about that confusion, rather than asking about the original problem for which the idea might be a poor approach. This results in the original problem not being solved. This is often called an [XY Problem](https://en.wikipedia.org/wiki/XY_problem).
Comment: It would help immensely if you wrote the exact conditional probability you want to calculate, since it seems not to be the one I wrote a few comments ago. Please edit that into your original post and not just write it in the comments, as comments often get overlooked.
Comment: You seem to use $\sigma$ for both the population distribution and the sampling distribution of the mean here, which is confusing.
Comment: But you are talking about the sampling distribution *of the mean* while $Y_i$ seems to be a single sample here, or is it a mean?
Comment: Why would you want to test if a single observation comes from the same distribution as the distribution of the mean of many samples? It's like asking if orange is the same as orange juice.
Comment: @Connor I've seen it, and I've seen other things.
Comment: Could you edit your question to explain what exactly does each symbol mean and how its value was obtained? *Why* do you want to calculate the probability? What do you need it for?
Comment: In order to give a value to the conditional probability I wrote above, there should be a probability space. Do you have a sense of what that should be? Note that the probability space is a sigma algebra over the set of Gaussian distributions (or maybe more than just Gaussians), rather than being over the real numbers, since the event in question is having a random variable being distributed a certain way, rather than a random variable equaling a certain value. (If you want to think of $Y_i = N(\hat\mu,\hat\sigma)$, that is abuse of notation but might convey the idea of the event in question.)
Comment: You mean the p-value of a z-test?
Comment: Unless you put some serious assumptions on your probability space, assumptions that it is not a given that you want to make, that conditional probability above is likely zero. Likewise, in a z-test, the probability of getting the exact z-stat you get is zero, but there is that "or more extreme" part of the p-value that has you integrate the tail(s) to calculate the probability of a continuum of values instead of just the one value.
Comment: @mhdadk Why can't I pull the probability from the $z$ I calculate in the first equation? Is the student's t-test the usual way something like this is calculated?
Comment: @ChristianHennig Okay, so here I should use something like $\sigma_{\bar X}$?
Comment: @Tim $Y_i$ is the same type of variable. Specifically, I'm thinking of correlations. So $\bar X$ is drawn from a background population of correlations and $Y_i$ is a potentially related correlation. I tried posting a long question on precisely what I mean, but it got no traction! If you'd like to know exactly what I'm getting at, please see: https://stats.stackexchange.com/questions/611190/can-you-use-a-z-test-with-a-sample-size-of-1
Comment: $Y_i$ is a single sample. Should I change the notation to make that clearer?
Comment: @Tim I don't know if $Y_i$ *is* from the same distribution. It could be, I want to test that. That's why I have it as $Y_i$ not $X_i$. I'll update the question to make that clearer.
Comment: @ChristianHennig is $\sigma_{ \bar X}$ standard?
Comment: @Tim I've edited the question to explain all the symbols. I can re-explain everything in the question I've posted above. But it seems to me that what I'm asking about is fairly common. Wanting to know if an observation is in a distribution or not is pretty fundamental. If I'm assuming that it's normal and I know the mean and the variance, does it really matter why I want to know? I apologise if my poor use of concepts and terminology is obscuring my question.
Comment: @Dave Yes, if I'm reading what you wrote correctly, that is exactly what I mean! I get your point, but I've been extremely clear on the underlying problem I'm having in another question: https://stats.stackexchange.com/questions/611190/can-you-use-a-z-test-with-a-sample-size-of-1, and that didn't help me get an answer at all! The reason I'm hesitant to explain is that there's a lot of background and it seems to overload the question. I don't know the best way forward. If you happen to read the question I posted, could you let me know how much detail needs porting over to this question?
Comment: @Dave I believe that distribution will be a Gaussian as well. But do I need to know the precise distribution of $Y_i$ to say with certainty that it's not the same as $\mathcal{N}(\hat \mu,\hat \sigma)$? (**BTW:** I'm taking sigma algebra here to mean, "some distribution with assigned probability". From wikipedia's third paragraph: https://en.wikipedia.org/wiki/%CE%A3-algebra)
Comment: @Dave Isn't your statement of the problem perfect for a Z-Test? Isn't that essentially what a Z-Test is?
Comment: @Dave Yes, the p-value of a z-test, is that a reasonable way of calculating this?
Comment: @Dave Yes, that's exactly what I'm talking about. Getting the probability of a value **at least as extreme** as the observation, and using significance to reject or accept the null hypothesis that $Y_i$ is in the distribution $\mathcal{N}(\hat \mu, \hat \sigma)$. Is using the z-test in this fashion reasonable?
Comment: @Dave Okay, thank you for all of your input, it's massively helpful. Is this the correct way to state the problem: $$ P(Y_i = y  |  Y_i \sim \mathcal{N}(\hat \mu, \hat \sigma))$$ Also, it would really help me if you could say if the z-test is useful here.
Comment: **Comments have been [moved to chat](https://chat.stackexchange.com/rooms/144985/discussion-on-question-by-connor-calculating-the-probability-my-observation-y); please do not continue the discussion here.** Before posting a comment below this one, please review the [purposes of comments](/help/privileges/comment). Comments that do not request clarification or suggest improvements usually belong as an [answer](/help/how-to-answer), on [meta], or in [chat]. Comments continuing discussion may be removed.
Answer:
I assume my population is distributed:
$$ X \sim \mathcal{N}(\mu, \sigma) $$
where $X$ is a normally distributed random variable, $\mathcal{N}$ is a  normal distribution, $\mu$ is an estimator for the population mean, and $\sigma$ is an estimator for the population standard deviation.

This statement does not make sense. Either $\mu$ and $\sigma$ are the parameters of the population, or they are estimates of the parameters. They cannot be both.

How can I use this distribution to test the probability that an observation, $Y_i$, has been drawn from the population distribution?

Assuming that you are asking about what you are saying here, you want to calculate what is the probability for $Y_i$ assuming the $\mathcal{N}(\mu, \sigma)$ distribution. If that is the case, just plug-in $Y_i$ to the Gaussian cumulative distribution function parametrized by $\mu$ and $\sigma$ and read the probability it returns. There's nothing more to it, if this actually is what you mean.
Something like

$$ P(Y_i = y  |  Y_i \sim \mathcal{N}(\hat \mu, \hat \sigma))$$

(in the comments), does not make sense, it's like asking "is the color red if we know that the color is red". You cannot have conditional distribution conditioned on this distribution itself. The only way to read this notation would be as $P(Y_i|Y_i)$, which is a tautology.
Comment: @Connor you cannot condition a random variable on itself. What is confusing with your notation is that the parameters of the population and their estimates are different things. The parameters of the population are some possibly unknown values, estimates are estimated from the samples. It's like winning lottery numbers vs your guess about the winning numbers, if they would be the same, you'd be a lucky man.
Comment: Thank you! What's the confusion with my notation? I've re-read the question and it seems clear to me that $\mu$ and $\sigma$ are estimators (or estimates, I'm using estimators as I read today that is a type of statistic).
Comment: What should the notation be? $$ P(y | Y \sim \mathcal{N}(\hat \mu, \hat \sigma), y \in Y)$$ Or is this hopelessly wrong too!
Comment: I guess true to some extent. But I could guess the average of winning lottery numbers over a few thousand samples right? If that's all I care about, what's the issue?
Comment: @Connor it's completely untrue. I'd recommend you review basic probability and statistics concepts as you seem to be confusing many of them and without understanding the basics it may be impossible to move forward.
Comment: How is it completely untrue? If I pull numbers from 1-100 enough times, will they not have a mean and standard deviation that stabilises?
Comment: @Connor if you have a *very* detailed photograph of a thing is it *the same* as the thing itself?
Comment: No, but I'm not suggesting it is, just that it's a guess that improves the more samples you draw. As far as I'm aware the mean of a discrete random variable is knowable! I think we're going off into the weeds here though! Is this the correct notation: $$ P ( Y_i < y | X \sim \mathcal{N}(\hat \mu, \hat \sigma)) $$ Using the answer from this question: https://stats.stackexchange.com/questions/110194/what-do-vertical-bars-mean-in-statistical-distributions#:~:text=The%20vertical%20bar%20is%20often,it%20as%20'conditional%20on'. (I read $|$ as "given that", which explains the confusion )
Comment: @Connor It might help to say in words what you want to know. If you can say why you want to know it, that will help, too, since this appears to be a major XY problem.


Prove the trigonometric Identity involving secant

Question: The question I am currently working on is:
$\sec^2x-2\sec x\ \cos x+\cos^2x=\tan^2x-\sin^2x$.
Okay, judging by the expression here, I am going to need to work with the left side of the equation first, the $\sec^2x-2\sec x\ \cos x+\cos^2x$.  So I'm going to probably want to convert the secant ratios.  I know that $\sec^2-1=\tan^2x$ and that $\sec x=1/\cos x$.  The second option looks like it will be more useful in this case. Let me know how I am doing and if there are any errors or information that I should know about to finish this question, please :).
Comment: Both can be useful for the calculation.
Comment: A friendly reminder that visiting [here](http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference) to learn how to type mathematics on this site will help make your posts much easier to read.  For the most part, even simply enclosing your formulae with \$ signs will make a big improvement on readability.
Comment: Okay, I will take a look at it.  Thanks for the heads up.
Comment: As for your question, note that $2\sec(x)\cos(x) = 2\cdot \frac{1}{\cos(x)}\cdot \cos(x) = 2 = 1+1$.  Try and use one of the 1's for simplifying one part, and the other 1 for simplifying the other part.
Comment: What I did so far was I got $(1/cosx)(1/cosx) for sec^2x$.  and $2(1/cosx)$ for 2secx --my appologies if I again didn't type properly.  I'm still figuring it all out.--
Answer: By multiplying both terms by $\cos^2 x$ you are left to prove:
$$ 1-2\cos^2 x+\cos^4 x = \sin^2 x-\sin^2 x\cos^2 x \tag{1}$$
and that is straightforward to check by just replacing every occurrence of $\sin^2 x$ in the RHS with $1-\cos^2 x$.
Answer: $secx=\frac{1}{cosx}$,then the left is $\frac{1}{cos^2 x}-2+cos^2 x=\frac{sin^2 x+cos^2 x}{cos^2 x}-2+cos^2 x=tan ^2 x-1+cos^2 x=right side$,hope it will be helpful.
Comment: I have been following your model and have managed to understand up to the point to get $1/cos^2x-2 + cos^2x$ on the left side.  However, how did you get the $sin^2x+cos^2x/cos^sx$, and how does this make it become the equivalent of the right side? Since $tan^2x=sec^2x-1$
Comment: Because $1=cos^2 x+sin^2 x$,you can replace 1 with them, you don't have to use $tan^2 x=sec^2 x-1$
Comment: Okay, well I see that now where the answer comes from, but I still do not understand quite how it is the equivalent to $tan^2x-sin^2x$ on the right side.
Comment: Oh,$-1+cos^2 x=-sin^2 x$ for the same reason $1=sin^2 x+cos^2 x$,right?
Comment: Yes. I see that.  I think I may be forgetting one of the identities here.
Comment: Nevermind, I worked the rest of it out and I got the right answer.  Thanks for the help.
Answer: $$sec^2(x)-2sec(x)cos(x)+cos^2(x)=(sec(x)-cos(x))^2=$$$$\Big[\frac{1}{cos(x)}-cos(x)\Big]^2=\Big[\frac{1-cos^2(x)}{cos(x)}\Big]^2=$$$$\Big[\frac{sin^2(x)}{cos(x)}\Big]^2=\frac{sin^4(x)}{cos^2(x)}=\frac{sin^2(x)}{cos^2(x)}\cdot sin^2(x)=\frac{sin^2(x)}{cos^2(x)}[1-cos^2(x)]=$$$$tan^2(x)-sin^2(x)$$


98 percent of all babies born survive. 15% are a c section & 96% survive. If a baby is randomly selected and is

Question: not from a c section what is the probability that her baby survives?
$P(E) =$ percent that survives minus the percent that survive a c section (should give the percentage of survival that are not c section)
$P(E) = .98-.96.15$
$P(E) = 97.856$
However the book says its .9835. THIS MAKES NO SENSE how could the probability that a baby is not a c section and survive be higher than the overall survival rate of all births even including those that result from a c section?
Comment: Why do probabilists have to be so damn grim?
Answer: Of course it makes sense; in fact, it must be greater than the overall proportion.
To understand why, suppose I said that half of all flowers are white.  Of those flowers with five petals, only a fourth of these are white.  Then of those flowers that do not have five petals, you must have more than half of them being white, otherwise there is no way to have half of the overall population of flowers be white.  The extent to which non-five-petaled flowers are white depends on the relative incidence of five-petaled flowers versus non-five-petaled flowers.
Things look better if we assign actual events and probabilities.  Let $C$ represent the event that a birth is a c-section.  Let $S$ represent the event that a baby survives.  Denote by $\bar C$ (the birth is not c-section) and $\bar S$ (the baby does not survive) the complementary events.  Now we have $$\Pr[S] = 0.98, \quad \Pr[C] = 0.15, \quad \Pr[S \mid C] = 0.96,$$ as given by the question.  Your calculation was:  $$\Pr[S \mid \bar C] = \Pr[S] - \Pr[S \mid C]\Pr[C].$$  This is not correct.  The RHS can be written $$\Pr[S] - \Pr[S \cap C] = \Pr[S \cap \bar C] \ne \Pr[S \mid \bar C].$$  You can now see where you went wrong:  a joint probability is not the same as a conditional probability.  To get the right answer, then, you must observe that $$\Pr[S \mid \bar C] = \Pr[S \mid \bar C] \Pr[\bar C],$$ so that you need to divide your answer by the probability of a non c-section birth; i.e., $\Pr[\bar C] = 1 - \Pr[C] = 1 - 0.15 = 0.85$.  Therefore, the correct answer is $$\Pr[S \mid \bar C] = \frac{\Pr[S] - \Pr[S \mid C]\Pr[C]}{\Pr[\bar C]} = \frac{0.98 - (0.15)(0.96)}{0.85} = 0.983529,$$ as claimed.

Another way to reason is to use a frequentist model.  Suppose we have a cohort of $8500$ births.  Then the number of survivors overall is $(0.98)(8500) = 8330$, the number of non-survivors is $170$; the number of c-sections is $(0.15)(8500) = 1275$, the number of non c-sections is $7225$; and the number of survivors among those with c-sections is $(0.96)(1275) = 1224$.
We then create the following table:  $$\begin{array}{c|c|c|c} & C & \bar C & \\ \hline S & 1224 & ? & 8330 \\ \hline \bar S & ? & ? & 170 \\ \hline & 1275 & 7225 & 8500 \end{array}$$
It is now simple to fill in the blanks, since the columns and rows must add up to their respective totals:
$$\begin{array}{c|c|c|c} & C & \bar C & \\ \hline S & 1224 & 7106 & 8330 \\ \hline \bar S & 51 & 119 & 170 \\ \hline & 1275 & 7225 & 8500 \end{array}$$  Now what is the desired probability?  Count the proportion of survivors among non c-section births:  this is simply $7106/7225 = 418/425 \approx 0.983529$.
Comment: Here is my earlier comment: where did I go wrong with my logic?

"Let's say that there are $1000$ babies to choose from.

P(survive) $= 0.98$ so $980$ babies survive in total.

P(C-section) $= 0.15$ so $150$ babies must be removed via C-section.

P(survive|C-section) $= 0.96$ so $0.96*150 = 144$ babies survive C-sections.

We want P(survive|not from C-section). Looking at our sets, we have:
$980 - 144 = 836$ babies who survive AND are not from C-sections (subtracting the 144 Cs from 980 total survivors)

Hence P(survive|not from C-section) = $836/1000 = 0.836$"
Comment: @M.Diggerson you went wrong with your logic when you took $836/1000$ instead of $836/(1000-150)$
Comment: Ah I see now that I selected total babies instead of just the non-C-section babies, thanks for pointing it out @Dario
Answer: Let $S$ be the event of surviving a birth, and $C$ the event that the birth is a C-section.
We are given $\mathsf P(S)=0.98, \mathsf P(C)= 0.15, \mathsf P(S\mid C)=0.96$
The Law of Total Probability states, $\mathsf P(S) = \mathsf P(S\mid C)\mathsf P(C)+\mathsf P(S\mid C^\complement)\mathsf P(C^\complement)$
So what we seek is: $\mathsf P(S\mid C^\complement) = \dfrac{P(S)-P(S\mid C)P(C)}{1-P(C)} = \dfrac{0.98-0.96\cdot0.15}{1-0.15} \approx 0.9835$

THIS MAKES NO SENSE how could the probability that a baby is not a c section and survive be higher than the overall survival rate of all births even including those that result from a c section?

Because the probability we seek is for the baby to survive when given that it is not a C-section.
The probability that a baby survives and the birth is not a C-section is $\mathsf P(S\cap C^\complement) = 0.98-0.96\cdot0.15 = 0.836$.   Not the same thing at all.
Comment: what is $P(S \vert C)$ as an event?
Comment: $\mathsf P(S\mid C)$ is the probability for survival among births by c-section. The $96\%$ rate.


How to read incoming SMS in iOS 11

Question: I'm not a real IOS developer but I know from doing my research it's not possible to read iPhone SMS because the app is going to be rejected by Apple and no public API available.
but what if I want to create an app for myself (I will never submit it to AppStore), I want to know from your experience if there is any private API (in iOS 11(.4)) that will allow me to read any incoming SMS so I can make immediate action on it?
Comment: Possible duplicate of [Get SMS broadcast with text body without Jailbreak BUT private frameworks in IOS](https://stackoverflow.com/questions/26642770/get-sms-broadcast-with-text-body-without-jailbreak-but-private-frameworks-in-ios)
Answer: Reading One Time Password from sms is available after iOS 12  . Check the Official Documentation for Enabling Password AutoFill on a Text Input View

How to read incoming SMS in iOS 11


Show that $\lim\limits_{n\to\infty} \frac{1}{n} \mathbb{E}(\max\limits_{k\leq n} |X_{k}|) = 0.$

Question:
Let $\{X_{n}\}_{n\in \mathbb{N}}$ be a sequence of i.i.d. random
  variables in $L^{1}(\Omega)$. Show that
$$\lim_{n\to\infty} \frac{1}{n} \mathbb{E}\left(\max_{k\leq n}
 |X_{k}|\right) = 0.$$

I am really not so sure how to approach this problem. I am given the hint to use the following result for any non-negative random variable $X$.
$$\mathbb{E}X = \int_{0}^{\infty} \operatorname P(X > x) \mathop{dx}. $$
So following this approach I get
$$\lim_{n\to\infty} \frac{1}{n} \int_{0}^{\infty} \operatorname P\left(\max_{k\leq n} |X_{k}| > x\right) \mathop{dx} $$
And also I think this integral
$$\int_0^{\infty} (\operatorname P(|X_{1}| \leq x)^{k} \mathop{dx} $$
will be useful but I'm not sure how to continue and will really appreciate your help.
I guess we may need law of large numbers. Not sure though.
Answer: Since $|\sigma(|X_k|)|<|\sigma(X_k)|$, given that $X_k$ are i.i.d. we can infer that $|X_k|$ are i.i.d. Thus $$\mathbb{E}\left[\max_{k\leq n}
 |X_{k}|\right]=\int_0^\infty\operatorname P\left(\max_{k\leq n}
 |X_{k}|>x\right)\,dx=\int_0^\infty\prod_{k\le n}\left(1-\operatorname P(|X_k|\le x)\right)\,dx.$$ Since $\mu:=\Bbb E[|X_i|]<\infty$ for $i\le k$, splitting $[0,\infty)$ into $[0,\mu)\cup[\mu,\infty)$ yields \begin{align}\mathbb{E}\left[\max_{k\leq n}
 |X_{k}|\right]&=\int_0^\infty\left(1-F_{|X_i|}(x)\right)^n\,dx\\&\le\int_0^{\mu}\left(1-F_{|X_i|}(x)\right)^n\,dx+\int_{\mu}^\infty\left(\frac{\mu}x\right)^n\,dx\end{align} by Markov's inequality. The function $\left(1-F_{|X_i|}(x)\right)^n$ is decreasing, so \begin{align}\mathbb{E}\left[\max_{k\leq n}
 |X_{k}|\right]&\le \mu\left(1-F_{|X_i|}(0)\right)^n+\mu^n\int_{\mu}^\infty\frac1{x^n}\,dx\le\mu+\frac{\mu^{1-n}}{n-1}\cdot\mu^n.\end{align} Hence $$\lim_{n\to\infty} \frac{1}{n} \mathbb{E}\left(\max_{k\leq n}
 |X_{k}|\right)=\lim_{n\to\infty}\frac\mu{n-1}=0.$$


Does a random variable needs to be numeric?

Question: Everywhere I have seen, random variable is a numeric quantity. Is it a rule? Can random variable be a non-numeric quantity?
Suppose I toss a coin. Can I declare my random variable X to be the outcome of the toss, i.e. X will take values from {'heads', 'tail'} rather than some numeric values like {0, 1}?
If say random variable cannot take numeric values then how should I describe something like P(X='heads') = 1/2. What is X in this case?
Comment: Sometimes the term "random element" is used for quantities that are not necessarily real numbers.
Comment: What's exactly what I'm confused about! Thanks so much!
Comment: This is the Wikipedia article explaining random element. I bet it's what you were looking for. https://en.wikipedia.org/wiki/Random_element
Answer: The nomenclature I'm familiar with is that the term "random variable" is reserved for a random quantity that is real-valued. "Random quantity" is the more general term for a measurable function on a probability space. Such a function's codomain need not be a subset of the real numbers, or have anything to do with numbers at all.
Comment: What would you call X in case you want to express `P(X='heads') = 1/2`. Would you call X an event, outcome or random variable?
Comment: I am not sure. I want to do is to express the idea probability of X that will take value "heads" is 1/2. What should be X be and "heads" in this case, I do not know.
Comment: @Lokesh Precisely what mathematical object do you mean by "heads"?
Comment: @Lokesh The usual approach is to use 1 for heads and 0 for tails, as you mentioned in the original question.
Answer: Random variable is simply a mapping from outcome to real number (from outcome space to real space).
Probability is a mapping from event space to [0, 1]. Here event can be "getting a head".
Probability in itself was sufficient to describe expectation of events. But sometimes we want expectation of f(event). For example, probability of sum(heads) in three tosses.
So now you see, sum(heads) is a real quantity, but is derived from the outcomes. That's how notion of Random Variable came into usage.
Take away is :
For events, use probability. But for f(event), use random variable.


Finding the maximal ideals of the quotient of a polynomial ring by an ideal

Question: Let the field $k$ be algebraically closed, let $f(X) \in k[X]$ be a separable polynomial of degree at least $2$, let
$$
B = \frac{k[Y,X]}{(Y^2 - f(X))}
$$
and write $y,x$ for the images in $B$ of $Y$ and $X$, respectively.

The Nullstellensatz tells me that the maximal ideals of $k[Y,X]$ are of the form $(X-\alpha, Y-\beta)$ for $\alpha,\beta\in K$. But what do the maximal ideals of $B$ look like?

I can write $B = k[y,x]$, but this is not isomorphic to the polynomial ring $k[Y,X]$ because $y^2 = f(x)$. Thus, I don't think I can directly apply the Nullstellensatz to $B$. Is there another way?
Answer: Recall the ideal correspondence for quotient rings: The ideals of a ring of the form $R/I$ are ideals of $R$ which contain $I$. This is just like the subgroup correspondence for quotient groups.
So you are right to start off by noting the maximal ideals of $k[x,y]$ are of the form $(x-a, y-b)$ by the Nullstellensatz. Now any maximal ideal $m$ of your ring $B$ must also be of the form $(x-a,y-b)$ but it must also contain the defining ideal $(y^2 - f(x))$. Therefore, we must have $y^2 - f(x)$ in $(x-a,y-b)$. One can work out at this point that this means that this is equivalent to the condition $b^2 = f(a)$.
Its even easier to see this geometrically! In particular, by denoting $V(y^2 - f(x))$ the vanishing locus in $k^2$, i.e., the set of points $(a,b)$ such that $b^2 = f(a)$, and recalling that the Nullstellensatz gives a correspondence between such general points $(a,b)$ in $k^2$ and maximal ideals $(x-a,y-b)$, we see that $(y^2 - f(x)) \subseteq (x-a,y-b)$ if and only if the point $(a,b)$ lies on the curve $V(y^2 -f(x))$. This recovers again the condition $b^2 = f(a)$.


Make another table with ID and distinct errors between two rows containing same id - SQL

Question: I have a spark dataframe that has two columns ("time_stamp" and "message").
Example dataframe:
Time_stamp                   Message
2020-12-01 05:28:34:215      some text1 ID: 1
2020-12-01 05:28:40:210      some text2 error: A
2020-12-01 05:28:40:220      some text3 error: B
2020-12-01 05:28:41:203      some text4 error: A
2020-12-01 05:30:43:201      some text5 ID: 1
2020-12-01 05:32:50:215      some text6 ID: 2
2020-12-01 05:32:50:220      some text7 error: A
2020-12-01 05:48:51:220      some text8 error: C
2020-12-01 05:48:52:203      some text9 error: B
2020-12-01 05:51:53:201      some text10 ID: 2

I want to make another dataframe with ID and distinct errors between two rows containing same id.
Expected output:
Example table:
ID          Error
1           A
1           B
2           A
2           C
2           B

Thanks
Comment: I am basically using Apache spark on databricks and having a spark dataframe. SQL query can be run on this dataframe. So mysql query can be easilt run for spark dataframe. using spark.sql
Comment: Spark SQL is not the same as MySQL. You cannot run MySQL queries on Spark dataframes. Those queries are called Spark SQL queries.
Comment: Oh I see. thanks for clarifying it.
Answer: Try the code below. The code groups by ID, collects the error messages and gets the earliest error message for each distinct error message. Time order is maintained.
import pyspark.sql.functions as F
from pyspark.sql.window import Window

df2 = df.withColumn(
    'Time_stamp',
    F.to_timestamp('Time_stamp', 'yyyy-MM-dd HH:mm:ss:SSS')
).withColumn(
    'ID',
    F.regexp_extract('Message', 'ID: ([a-zA-Z0-9]+)', 1)
).withColumn(
    'ID',
    F.last(F.when(F.col('ID') != '', F.col('ID')), True).over(Window.orderBy('Time_stamp'))
).filter(
    F.col('message').rlike('error')
).withColumn(
    'Message',
    F.regexp_extract('Message', 'error: (.*)', 1)
).groupBy('ID').agg(
    F.collect_set(F.array('Message', 'Time_stamp')).alias('Message')
).select(
    'ID',
    F.explode('Message').alias('Message')
).selectExpr(
    'ID',
    'Message[0] as error',
    'Message[1] as Time_stamp'
).withColumn(
    'rn',
    F.row_number().over(Window.partitionBy('ID', 'error').orderBy('Time_stamp'))
).filter('rn = 1').orderBy('Time_stamp').select('ID', 'error')

Comment: Thank you. let me understand the code and implement it in actual data. Actual data is little bit different, so need to modify it accordingly. Shall I update my data to match with actual data?
Comment: @Dataholic the code should also work without any modifications.
Comment: I ran the code. The output dataframe contains two columns (ID and error), but those columns are blank.
Comment: Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225889/discussion-between-dataholic-and-mck).


identifier " " is undefined

Question: I am coding a 3D opengl test application, I have a function set up like this:
Shader::Shader(const std::string& fileName)
{
    program = glCreateProgram();
    shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX_SHADER);
    shaders[1] = CreateShader(LoadShader(fileName + ".fs"), GL_FRAGMENT_SHADER);

    for (unsigned int i=0; i < NUM_SHADERS; i++)
        glAttachShader(program, shaders[1]);

    glBindAttribLocation(program, 0, "position");

    glLinkProgram(program);
}

However when I try to declare shaders[0] = CreateShader(LoadShader(fileName + ".vs"), GL_VERTEX SHADER); I get the error: identifier "shaders" is undefined, how do I fix this?
Comment: Ask yourself : where `shaders` is defined and then show us how it is declared.
Comment: You have not declared an array of shaders.
Comment: *However when I try to declare* -- That line doesn't declare anything.  That line of code assigns the return value of `CreateShader` to something we (and the compiler) knows nothing about, i.e. `shaders[0]` and `shaders[1]`.
Comment: Is shaders[] a data member? or global varaible? What is its data type?
Answer: You forgot #include "shader.h". This will fix your issue.
I guess you are working on something similar to https://github.com/BennyQBD/ModernOpenGLTutorial. Here is the declaration for m_shaders https://github.com/BennyQBD/ModernOpenGLTutorial/blob/master/shader.h#L29. You are using shaders instead.
Comment: Yes that would be it;) - Will accept the answer as soon as I can
Comment: @JakeWickham So the only way to have answered your question is to take a random guess?  You should have provided those details as to what you're working with in the original question.
Comment: My answer wasn't a random guess. I have edited the answer for further details. But I agree with @PaulMcKenzie that you should include background details in the question.
Comment: @ShridharR.Kulkarni I am directing this to the OP.  It is a poor question to begin with if only 2 people have knowledge on where the code comes from.
Comment: I am new to answering questions here. Can you tell me what 'directing to OP' means? and have I answered something wrong?
Comment: @ShridharR.Kulkarni -- When a question is asked on SO, it should have all the requisite information so that the community knows how to answer the question.  How many of us knows about that site you linked to?
Comment: @PaulMcKenzie: Yes I now understand your concern.

identifier " " is undefined


Search a base class's object in a Set of Derived class

Question: I've a class called Skill as:
public class Skill {
    private final int type;
    private final int level;

    public Skill(int type, int level) {
        this.type = type;
        this.level = level;
    }

    // Getters

    @Override
    public int hashCode() {
        int h = 17;
        h = 31 * h + type;
        h = 31 * h + level;
        return h;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj instanceof Skill) {
            Skill that = (Skill) obj;
            return this.type == that.type && this.level == that.level;
        }
        return false;
    }
}

Another called PSkill as:
public class PSkill extends Skill {
    private final int preferenceLevel;
    private final boolean mandatory;

    public PSkill(int type, int level, int preferenceLevel, boolean mandatory) {
        super(type, level);
        this.preferenceLevel = preferenceLevel;
        this.mandatory = mandatory;
    }

    // Getters

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj instanceof PSkill) {
            PSkill that = (PSkill) obj;
            return super.equals(obj)
                && this.preferenceLevel == that.preferenceLevel
                && this.mandatory == that.mandatory;
        }
        return false;
    }
}

My requirement:
Search a set of PSkill objects to find a match for Skill object.
For e.g.: Skill - type:1, level:2 is a match for PSkill - type:1, level:2, preferenceLevel: any, mandatory: any
When I run the below, it works.
public class Invoker {
    public static void main(String[] args) {
        Set<PSkill> skills = new HashSet<>(Arrays.asList(new PSkill(1, 1, 1, true), new PSkill(1, 2, 1, true)));
        System.out.println(skills.contains(new Skill(1, 1))); // prints true
        System.out.println(skills.contains(new Skill(1, 3))); // prints false
    }
}

And I know why because the hashCode() implementation for both types is same and in HashSet implementation, key.equals(k) is used and key in my case is Skill object and hence, the equality works.
From HashSet implementation
final Node<K,V> getNode(int hash, Object key) {
    Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
    if ((tab = table) != null && (n = tab.length) > 0 &&
        (first = tab[(n - 1) & hash]) != null) {
        if (first.hash == hash && // always check first node
            ((k = first.key) == key || (key != null && key.equals(k))))  // here
            return first;
        if ((e = first.next) != null) {
            if (first instanceof TreeNode)
                return ((TreeNode<K,V>)first).getTreeNode(hash, key);
            do {
                if (e.hash == hash &&
                    ((k = e.key) == key || (key != null && key.equals(k)))) // and here
                    return e;
            } while ((e = e.next) != null);
        }
    }
    return null;
}

I know I broke the hashCode() and equals() contract. But the code is working as it should, i.e. check if a Skill matches any of the PSkills in the set.
My question is:
Is the equality check key.equals(k) in HashSet implementation dependent and could be reversed in a future release i.e. k.equals(key) and the code will stop working?
Also, any better ways to do it which could make it less fragile without simply looping over the collection? Thanks
Comment: will it still work if you change the order of insertions?
Comment: @AdamSkywalker changing order of insertions of `PSkill` objects into the set? Well yes.
Answer: This behavior is certainly not guaranteed. The contract is that if you provide correct implementations of hashCode and equals, you get a correct implementation of a hashset. If you don't follow your end of the contract, you don't have any guarantees on how well a set will work.


How to find density? (Changing units)

Question: I have a 10 kg/m^2 weight load for a steel sheet and I would like to calculate the density of the steel sheet.Is it possible to calculate the density of the steel sheet using the value of 10 kg/m^2 weight load?
I have tried calculating value for density by multiplying by the area (m^2) of the steel sheet, then dividing by the volume of the steel sheet which gives units of kg/m^3. However when I change the size of the area or volume for example, my density value will change? Shouldn't the density be constant for a material.
How can I calculate the density from 10 kg/^2? (If it is possible)
Thanks in advance.
Comment: Could you be more specific about your question? I have no sense of the physical situation that you are describing.
Comment: @Tucker sorry, updated..
Answer: You need the thickness of the sheet, which will give you the factor of length that you need.  Yes, the density should be constant.  Typical densities of steel range from $7750$ to $8050$ kg/m$^3$, which says your sheet is about $\frac {10}{7900}$meters thick.
Comment: I am finding that the density will change as the thickness varies, area stays the same, hence volume increases (due to thickness increase) and therefore the density will change? Does that sound correct?
Comment: If you are thinking of holding the areal density constant, that would be correct.  What is constant in reality is the material density.  The areal density of $10 kg/m^2 is the product of the material density and the thickness.  If you double the thickness the areal density will double.

How to find density? (Changing units)


True or false? We have that $\text{det(A)} \neq 0$ for every diagonal matrix $A \in \mathbb{R}^{n \times n}$

Question:
Let $n \in \mathbb{N}.$
We have that $\text{det(A)} \neq 0$ for every diagonal matrix $A \in
\mathbb{R}^{n \times n}$. Is this statement true or false? State why.

I don't know if zero is excluded when they say $n \in \mathbb{N}$. But if it's included then the statement is wrong because let $n=0$ then determinant will be zero too.
If zero is excluded, then the statement will be true because we only have that diagonals $\neq$ zero which means if we split the matrix to vectors, we will get linearly independent vectors. And determinant of linearly independent vectors is $\neq $ zero.

What do you think about it? Did I do it correctly for both cases?
Comment: Note that the zero matrix is diagonal. What is its determinant?
Comment: I did an English mistake.. Determinant will be zero then and the statement will be false.
Answer: The statement is always false, because the $0$ matrix is a diagonal matrix with $det A =0$. Moreover, every diagonal matrix with a $0$ on the diagonal will have $det A = 0$.
Answer: The determinant of a diagonal matrix is the product of the elements in the diagonal,so
$$\det\begin{pmatrix}
a_1 & 0 & ... & 0\\
0 & a_2 & ... & 0\\
\vdots & \ddots & \ddots & 0\\
0 & ... & 0 & a_n
\end{pmatrix}
=\prod\limits_{i=1}^n a_i.$$
Therefore, the determinant is nonzero iff all diagonal entries are nonzero.
Answer: A matrix $A=[a_{ij}]$ is said to be diagonal if, when $i\ne j$, we have $a_{ij}=0$.
It is a common misunderstanding to interpret this as imposing $a_{ii}\ne0$, but this is wrong: no condition is set on $a_{ii}$. For instance, the matrix
$$
\begin{bmatrix}
1 & 0 \\
0 & 0
\end{bmatrix}
$$
is diagonal, because $a_{12}=0$ and $a_{21}=0$. Nothing else is required in order to satisfy the definition.
The same misunderstanding is often seen with triangular matrices: the matrix
\begin{bmatrix}
2 & 1 & 0 \\
0 & -1 & 1 \\
0 & 0 & 0
\end{bmatrix}
is upper triangular, notwithstanding the fact that $a_{13}=0$ and $a_{33}=0$. What's needed in order that a matrix is upper triangular is that $a_{ij}=0$ for $i>j$; no condition is imposed on $a_{ij}$ for $i\le j$.


Como posso usar o Firebase como base de dados em uma aplicação Django?

Question: Acabei de criar um projeto Django e gostaria de utilizar o Firebase ao invés do sqlite como base de dados. Procurei em vários lugares mas não consegui achar em nenhum local como fazer a integração. Alguem poderia me ajudar?
Comment: https://github.com/xtrinch/fcm-django
Comment: Cara nesse link tu vai encontrar uma resolução.Aqui no [StackOverflow em Ingles](https://stackoverflow.com/questions/43681362/how-to-implement-firebasedb-with-a-django-web-application)
Answer: Segundo a documentação do Django na parte de databases que pode ser lida em inlgês no seguinte link https://docs.djangoproject.com/en/2.0/ref/databases/#using-a-3rd-party-database-backend:
As databases nativas são;

PostgreSQL
MYsql
SQLIte

E as suportadas por terceiros são;

SAP SQL Anywhere
IBM DB2
Microsoft SQL Server
Firebird
ODBC


PyMC3: up-to-date implementation of Price is Right example?

Question: So, getting into PyMC3 a lot more and working through examples, I found I cannot implement in an up-to-date form an example from Cameron Davidson-Pilon's Bayesian Methods for Hackers, specifically the Price is Right example, in the library's current version.
The model starts on page 139, and the crux of it is to calculate how one's prior changes after being modified by a summation of two previous priors, under normality assumptions. That is as much of the model's logic as I was able to deduce, and was hoping building it and tinkering would explain the rest to me.
The code he uses is
import pymc as pm

data_mu = [3e3, 12e3]
data_std = [5e2, 3e3]
mu_prior = 35e3
std_prior =  75e2

true_price = pm.Normal("true_price", mu_prior, 1.0 / std_prior ** 2)

prize_1 = pm.Normal("first_prize", data_mu[0], 1.0 / data_std[0] ** 2)
prize_2 = pm.Normal("second_prize", data_mu[1], 1.0 / data_std[1] ** 2)

price_estimate = prize_1 + prize_2

@pm.potential
def error(true_price=true_price, price_estimate=price_estimate):
    return pm.normal_like(true_price, price_estimate, 1 / (3e3) ** 2)

mcmc = pm.MCMC([true_price, prize_1, prize_2, price_estimate, error])
mcmc.sample(50000, 10000)

price_trace = mcmc.trace("true_price")[:]

The thing is, pm.potential is deprecated now, and sampling directly through pm.MCMC is discouraged as well, in lieu of
with pm.Model() as model:
   ...
   ...
   pm.sample()

I would extremely appreciate anyone helping me understand this model, and how a similar one would be built under modern PyMC3.
Comment: My implementation of Gelman's rat tumor example uses a similar methodology.  See [here](https://docs.pymc.io/notebooks/GLM-hierarchical-binominal-model.html).
Comment: Thank you for the link, it is not only interesting but also concise in its explanations. Why did you transform the joint prior in the way you did, first by defining a function for it and then by feeding that into `pm.Potential('p(a, b)', logp_ab(ab))`? The regular priors, `ab[0]` and `ab[1]`, are referenced throughout the rest of the model, without the immediate reason for `pm.Potential()` being seen.
Comment: I transformed the prior at the suggestion of one of the maintainers.  I think it was the preferable way to define a prior which was not composed of standard distributions.
Answer: The book has been ported to PyMC3. The updated example of the Price is Right model can be found here: https://render.githubusercontent.com/view/ipynb?commit=be1faee30c4eb6bef4c049b6e11499db144c5697&enc_url=68747470733a2f2f7261772e67697468756275736572636f6e74656e742e636f6d2f43616d4461766964736f6e50696c6f6e2f50726f626162696c69737469632d50726f6772616d6d696e672d616e642d426179657369616e2d4d6574686f64732d666f722d4861636b6572732f626531666165653330633465623662656634633034396236653131343939646231343463353639372f43686170746572355f4c6f737346756e6374696f6e732f4368355f4c6f737346756e6374696f6e735f50794d43332e6970796e62&nwo=CamDavidsonPilon%2FProbabilistic-Programming-and-Bayesian-Methods-for-Hackers&path=Chapter5_LossFunctions%2FCh5_LossFunctions_PyMC3.ipynb&repository_id=7607075&repository_type=Repository#Example:-Optimizing-for-the-Showcase-on-The-Price-is-Right
Comment: Thanks! I must to remember in the future to check for authors open-sourcing and updating their coding books. The link perfectly answers my question, so thank you, but since you are here now, would you be willing to elaborate on Demetri Pananos's comment ^^; ? In his link, why would he define his joint prior in a new function, and then plug that it into `pm.Potential()`?
At any rate, thanks for your help.


Concept not clear: Permutations and Combinations

Question: Q: There are $5$ shirts all of different colors, $4$ pairs of pants all of different colors, and $2$ pairs of shoes with different colors. In how many ways can Amy and Bunny be dressed up with a shirt, a pair of pants, and a pair of shoes each ? ( Question from Brilliant App ).
Solution: $(5C2 \times 2!) \times (4C2 \times 2!) \times (2C2 \times 2!) = 480$
My approach:
No of permutations $= 5 \times 4 \times 2 = 40$ ways of ordering
Choosing 2 from these $40$ orderings $= 40C2 \times 2! = 1560$
Please let me know what is wrong with my approach.
Comment: The problem with your answer is that the two outfits you choose may use the same shirt, pants, or shoes, since you're just choosing among all $40$ possible outfits. The solution of $480$ makes sure that the pairs of shirts, pants, and shoes chosen are all different.
Comment: 5 * 4 * 2 = 40 ( these are 40 orderings, each with a shirt, pant and shoes right )
Answer: I'd suggest to consider 2 approaches. The first one will help you to understand why your approach is not correct. Then second will help to understand how is it related to combinations and permutations.

The numbers are small. That's why first you can apply a straight forward approach, without combinations and permutations. How may choices of short do you have for Amy? It is 5. If you have chosen one color for Amy, Bunny can choose one of remaining colors. How many remain? 4. Thus, you have 5x4 pairs of shirt colors. Same about pants: Amy has 4 choices, for Bunny remain 3 choices. Thus there are 4x3 pants choices. For shoes Amy has 2 choices, but Bunny has no choice (i.e. only 1 choice) after Amy has chosen. That's why we have 2x1 choices of shoes. Thus the total number of ways is (5x4) x (4x3) x (2x1) = 480.

Now we can consider it from the point of view of combinations and permutations. We have 2 people. From 5 shirt colors we have to chose 2. How many ways are there to chose 2 out of 5? There are 5C2 ways. When we have chosen 2 shirt colors, in how many ways we can assign these colors to 2 persons? In 2! ways. If we had more colors and more persons, it would be easier to see, why we are talking about permutations here. E.g. if we had chosen 7 colors out of 20, the number of way to assign 7 colors to 7 persons would be the number of permutations, i.e. 7x6x5x4x3x2x1 = 7!. For 2 persons we have 2!.
So, for shirts we have 5C2x2! For pants we have 4C2 ways to chose 2 colors out of 4. And there are 2! ways to assign them to Amy and Bunny. Thus there are 4C2x2! ways for pants.
The same for shoes: 2C2x2!.
The total number of ways is (5C2x2!) x (4C2x2!) x (2C2x2!) = 480.
Comment: +1 : nicely elaborated.
Answer: Others have specifically addressed where you went wrong.
The standard way of attacking the problem is as follows:
Let
$$\binom{n}{k} = \frac{n!}{[k!][(n-k)!]} ~\text{where}~
n \in \mathbb{Z^+}, ~k \in \{0,1,2,\cdots, n\} ~\text{and}~ 0! = 1.$$
Here, $\binom{n}{k}$ represents the number of different ways of selecting $k$ items from a group of $n$ items.

Let $A, B, C$ denote the # of different ways that Amy and Bunny can select shirts, pants, and shoes, respectively.
Then the final answer will be
$$A \times B \times C.$$
If Amy and Bunny are selecting $2$ items, from a group of $n$
items, superficially, you would suppose that the number of ways of doing this is $\binom{n}{2}$.  However, this overlooks that once the two items (say one green, one red) are selected, then you have to distinguish between [Amy selecting the green item, Bunny selecting the red item] and vice-versa.
Therefore, when computing $A,B,C$, the general formula for each variable will be
$$\binom{n}{2} \times 2.$$
Thus, since the number of shirts, pants and shoes are $5,4,2$ respectively, you have that
$$A = \binom{5}{2} \times 2 = 20,$$
$$B = \binom{4}{2} \times 2 = 12,$$
$$C = \binom{2}{2} \times 2 = 2.$$
Answer: Your answer is completely wrong , because when you multiply $5,4,3$ , you find the possible choice for only one individual.The rest is also confusing , as far as i could understand , you try to have random $2$ combinations from among these $40$ different combination.The problem is that you try to pick up $2$ things from tne number of permutation.
Be careful!Permutation does not give you subsets, it only gives the number of ordering,so you cannot choose somethings from permutation you can make choosing only from sets.This was the key sentence for you.


Random walk and powers of 2

Question: I'm in trouble on the following problem:
given a random walk starting at point N on the integer number line, how many steps should I wait before the walk hits a power of two at least once, with probability $P$ with $P\gt 0$?
Comment: Supposing you're taking unit steps, take the length of the walk equal to the distance from $N$ to the nearest power of two. It will then be possible to reach that power of two, and it will happen with a positive probability. Maybe this is not what you wanted to ask, but your question isn't clearly formulated.
Comment: @Marc van Leeuwen: I take unit step. $N$ is a positive integer number $N\ge{0}$ What is the formula of the probability $P(N)$ to hit a power of two at least once, assuming I am interested only in the first power of two before N and the next after N
Comment: I am not sure whether this has much to do with powers of 2.  Note that with $A \lt N \lt B$, the expected time to hit either of boundary is $(B-N)(N-A)$
Answer: Given an integer number N, the first power of two before N is given by:
$$2^{Int\left(ln(N)/ln(2)\right)}$$
and the first power of two following N is:
$$2^{Int\left(ln(N)/ln(2)+1\right)}$$
where $Int$ is the 'integer part of' operator.
So we have
$$l=N-2^{Int\left(ln(N)/ln(2)\right)}$$ steps on the left and
$$r=2^{Int\left(ln(N)/ln(2)+1\right)}-N$$
on the right.
Then, the probability to hit the power of two on the left after $n$ steps is:
$$\left( \begin{array}{c} n \\ l \end{array}\right)\left(\frac{1}{2}\right)^n$$
and the probability to hit the power of two immediately after N is:
$$\left( \begin{array}{c} n \\ r \end{array}\right)\left(\frac{1}{2}\right)^n$$
so $P$ is given by:
$$P=\left( \begin{array}{c} n \\ N-2^{Int\left(ln(N)/ln(2)\right)} \end{array}\right)\left(\frac{1}{2}\right)^n+\left( \begin{array}{c} n \\ 2^{Int\left(ln(N)/ln(2)+1\right)}-N \end{array}\right)\left(\frac{1}{2}\right)^n$$
Comment: What you call *the probability to hit the power of two on the left after $n$ steps* is the probability to be there after $n$ steps, but not necessarily for the first time.
Comment: Also the answer glosses over a parity issue: one cannot move $l$ places left in $n$ steps unless $n$ and $l$ have the same parity. In fact to do so requires making $\frac{n+l}2$ left steps and $\frac{n-l}2$ right steps. So you binomial coefficients should be $\binom n{\frac{n+l}2}$ and $\binom n{\frac{n+r}2}$. And the question is about the expected time for the first hit, not the probability of a hit after some number of steps.
Comment: I wasn't able to find the probability to hit a power of two for the first time. Any Suggestion?

Random walk and powers of 2


Regarding predicate logic and universal quantifiers,

Question: What is the difference between
$$
\forall x \in X (P(x) \iff \exists y \in X \ (D(x,y)))
$$
and
$$
\forall x \in X, \exists y \in X , (P(x) \iff D(x,y))
$$
I'm a bit confused, wouldn't they end up meaning the same thing once you expand it out? Or am I totally wrong?
Comment: In the first sentence, the existence of $y$ is conditioned on $P$(x). In the second sentence, $y$ exists for sure, and only $D(x,y)$ is interdependent with $P(x)$.
Comment: Could you give an example that would make one statement true and the other false?
Answer: If they are equivalent then, comparing the formulas after the first quantification we must have
$$
(P(x) \leftrightarrow\exists y \in X \ : \,D(x,y)) \iff \exists y \in X : (P(x) \leftrightarrow D(x,y))
$$
the last equivalence you can check here that t is false.
Comment: I don't understand what you are trying to say. Can you please explain?
Comment: Can you explain this in simpler terms? We have not covered this in class. From my understanding the first statement is saying,

"for all x in X, P(x) is true if and only if for all x in X there exists a y in X where D(x,y) is true"

whereas the second one is saying:

"for all x in X, there exists a y in X where P(x) is true if and only if D(x,y) is true"
Comment: Have you learned anything called *prenex normal form* ?
Comment: Nope. Did the sentences I make make sense, or were they incorrect?
Comment: You have to bring the two statements into the prenex normal form, i.e., quantifiers stands on the left only, then compare the *matrix* i.e., the statements after the quantifiers
Comment: It is too late  here, I'll continue this question tommorrow


Measuring the performance of VPN connection

Question: We have a Netgear FVS336G VPN firewall at our office and we use it to access our e-mail and files when we are working from home/outside of the office.
At present, the performance is not too bad, however, sometimes it can take quite a while to complete a small file transfer.
What I want to do is test the performance of the VPN connection (i.e. baseline it) and try a few things on a test VPN policy that I have created.
For example, I want to measure the speed on different algorithms - right now, the main policy everyone is using is set to 3DES; from what I have read, AES is 'faster' (as well as being more CPU friendly and secure), however, when I tried it on my test policy, I did not notice very much - of course, this doesn't mean anything as it's not very scientific... I'm just looking at a bunch of numbers on the screen and trying to determine which is higher.
What I am looking for is some sort of command prompt/line based tool that will bypass all the Windows (the environment is all Windows based) file transfer-y stuff. For example, the 'discovering' phase which takes about a minute or two to complete before it gets on to actually transferring the file.
Is there such a tool for a Windows platform that will help me measure the performance accurately?
Answer: I did quite a bit of bandwidth testing recently with OpenVPN tunnels and I found that iperf gave me the best results. I wanted something that would purely rely on the network speed and not the hard drive speed. Also make you sure set the packet size higher than the defaults if you are going for quite a bit of bandwidth.
Server

iperf -l 64k -w 64k -p 5001 -s

Client

iperf -l 64k -w 64k -p 5001 -c X.X.X.X

It's also a good idea to set these windows the same on both ends because Linux default window size is different than Windows, which caused me a lot of issues with inconsistent results.
Comment: Thanks for the example syntax usage above.
I tweaked it to run for slightly longer using the -t flag :)
May need to play with the window sizes too - thanks!
Answer: Just to name a few: nuttcp,iperf,netperf.
If you want to baseline/monitor on a regular basis I'd deploy on of the Internet2's PerfSonar[1] VM images behind the concentrator - I found the Web100 Linux kernel very valuable when it comes to performance of internet links and if you need to debug that.
[1]http://www.internet2.edu/performance/pS-PS/
Comment: Thanks for answering with the different alternatives available :-)
I am trying out iperf right now but will most likely play around with the others at some point - thanks again.


Detecting edges of a card with rounded corners

Question: Hi currently i am working on an OCR reading app where i have successfully able to capture the card image by using AVFoundation framework.
For next step, i need to find out edges of the card , so that i can crop the card image from main captured image & later i can sent it to OCR engine for processing.
The main problem is now to find the edges of the card & i am using below code(taken from another open source project) which uses OpenCV for this purpose.It is working fine if the card is pure rectangular Card or Paper. But when i use a card with rounded corner (e.g Driving License), it is failed to detect . Also i dont have much expertise in OpenCV , Can any one help me in solving this issue?
- (void)detectEdges
{
    cv::Mat original = [MAOpenCV cvMatFromUIImage:_adjustedImage];
    CGSize targetSize = _sourceImageView.contentSize;
    cv::resize(original, original, cvSize(targetSize.width, targetSize.height));

    cv::vector<cv::vector<cv::Point>>squares;
    cv::vector<cv::Point> largest_square;

    find_squares(original, squares);
    find_largest_square(squares, largest_square);

    if (largest_square.size() == 4)
    {

        // Manually sorting points, needs major improvement. Sorry.

        NSMutableArray *points = [NSMutableArray array];
        NSMutableDictionary *sortedPoints = [NSMutableDictionary dictionary];

        for (int i = 0; i < 4; i++)
        {
            NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:[NSValue valueWithCGPoint:CGPointMake(largest_square[i].x, largest_square[i].y)], @"point" , [NSNumber numberWithInt:(largest_square[i].x + largest_square[i].y)], @"value", nil];
            [points addObject:dict];
        }

        int min = [[points valueForKeyPath:@"@min.value"] intValue];
        int max = [[points valueForKeyPath:@"@max.value"] intValue];

        int minIndex;
        int maxIndex;

        int missingIndexOne;
        int missingIndexTwo;

        for (int i = 0; i < 4; i++)
        {
            NSDictionary *dict = [points objectAtIndex:i];

            if ([[dict objectForKey:@"value"] intValue] == min)
            {
                [sortedPoints setObject:[dict objectForKey:@"point"] forKey:@"0"];
                minIndex = i;
                continue;
            }

            if ([[dict objectForKey:@"value"] intValue] == max)
            {
                [sortedPoints setObject:[dict objectForKey:@"point"] forKey:@"2"];
                maxIndex = i;
                continue;
            }

            NSLog(@"MSSSING %i", i);

            missingIndexOne = i;
        }

        for (int i = 0; i < 4; i++)
        {
            if (missingIndexOne != i && minIndex != i && maxIndex != i)
            {
                missingIndexTwo = i;
            }
        }

        if (largest_square[missingIndexOne].x < largest_square[missingIndexTwo].x)
        {
            //2nd Point Found
            [sortedPoints setObject:[[points objectAtIndex:missingIndexOne] objectForKey:@"point"] forKey:@"3"];
            [sortedPoints setObject:[[points objectAtIndex:missingIndexTwo] objectForKey:@"point"] forKey:@"1"];
        }
        else
        {
            //4rd Point Found
            [sortedPoints setObject:[[points objectAtIndex:missingIndexOne] objectForKey:@"point"] forKey:@"1"];
            [sortedPoints setObject:[[points objectAtIndex:missingIndexTwo] objectForKey:@"point"] forKey:@"3"];
        }

        [_adjustRect topLeftCornerToCGPoint:[(NSValue *)[sortedPoints objectForKey:@"0"] CGPointValue]];
        [_adjustRect topRightCornerToCGPoint:[(NSValue *)[sortedPoints objectForKey:@"1"] CGPointValue]];
        [_adjustRect bottomRightCornerToCGPoint:[(NSValue *)[sortedPoints objectForKey:@"2"] CGPointValue]];
        [_adjustRect bottomLeftCornerToCGPoint:[(NSValue *)[sortedPoints objectForKey:@"3"] CGPointValue]];
    }

    original.release();

}

Comment: @John Are you going to share one or two sample images?
Comment: can you provide me the link for croping the card?
Comment: @raaz i have similar requirement can you suggest me that open source project you have used. It will be great help.
Comment: According to John, [here is a sample](https://i.stack.imgur.com/6tB2a.png).
Comment: Hey dude, if you think this question has been successfully answered, please click on the **checkbox** near it.
Answer: This naive implementation is based on some of the techniques demonstrated in squares.cpp, available in the OpenCV sample directory. The following posts also discuss similar applications:

OpenCV C++/Obj-C: Detecting a sheet of paper / Square Detection
Square detection doesn't find squares
Find corner of papers

@John, the code below has been tested with the sample image you provided and another one I created:

The processing pipeline starts with findSquares(), a simplification of the same function implemented by OpenCV's squares.cpp demo. This function converts the input image to grayscale and applies a blur to improve the detection of the edges (Canny):

The edge detection is good, but a morphological operation (dilation) is needed to join nearby lines:

After that we try to find the contours (edges) and assemble squares out of them. If we tried to draw all the detected squares on the input images, this would be the result:

It looks good, but it's not exactly what we are looking for since there are too many detected squares. However, the largest square is actually the card, so from here on it's pretty simple and we just figure out which of the squares is the largest. That's exactly what findLargestSquare() does.
Once we know the largest square, we simply paint red dots at the corners of the square for debugging purposes:

As you can see, the detection is not perfect but it seems good enough for most uses. This is not a robust solution and I only wanted to share one approach to solve the problem. I'm sure that there are other ways to deal with this that might be more interesting to you. Good luck!
#include <iostream>
#include <cmath>
#include <vector>

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/imgproc_c.h>

/* angle: finds a cosine of angle between vectors, from pt0->pt1 and from pt0->pt2
 */
double angle(cv::Point pt1, cv::Point pt2, cv::Point pt0)
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}

/* findSquares: returns sequence of squares detected on the image
 */
void findSquares(const cv::Mat& src, std::vector<std::vector<cv::Point> >& squares)
{
    cv::Mat src_gray;
    cv::cvtColor(src, src_gray, cv::COLOR_BGR2GRAY);

    // Blur helps to decrease the amount of detected edges
    cv::Mat filtered;
    cv::blur(src_gray, filtered, cv::Size(3, 3));
    cv::imwrite("out_blur.jpg", filtered);

    // Detect edges
    cv::Mat edges;
    int thresh = 128;
    cv::Canny(filtered, edges, thresh, thresh*2, 3);
    cv::imwrite("out_edges.jpg", edges);

    // Dilate helps to connect nearby line segments
    cv::Mat dilated_edges;
    cv::dilate(edges, dilated_edges, cv::Mat(), cv::Point(-1, -1), 2, 1, 1); // default 3x3 kernel
    cv::imwrite("out_dilated.jpg", dilated_edges);

    // Find contours and store them in a list
    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(dilated_edges, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);

    // Test contours and assemble squares out of them
    std::vector<cv::Point> approx;
    for (size_t i = 0; i < contours.size(); i++)
    {
        // approximate contour with accuracy proportional to the contour perimeter
        cv::approxPolyDP(cv::Mat(contours[i]), approx, cv::arcLength(cv::Mat(contours[i]), true)*0.02, true);

        // Note: absolute value of an area is used because
        // area may be positive or negative - in accordance with the
        // contour orientation
        if (approx.size() == 4 && std::fabs(contourArea(cv::Mat(approx))) > 1000 &&
            cv::isContourConvex(cv::Mat(approx)))
        {
            double maxCosine = 0;
            for (int j = 2; j < 5; j++)
            {
                double cosine = std::fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                maxCosine = MAX(maxCosine, cosine);
            }

            if (maxCosine < 0.3)
                squares.push_back(approx);
        }
    }
}

/* findLargestSquare: find the largest square within a set of squares
 */
void findLargestSquare(const std::vector<std::vector<cv::Point> >& squares,
                       std::vector<cv::Point>& biggest_square)
{
    if (!squares.size())
    {
        std::cout << "findLargestSquare !!! No squares detect, nothing to do." << std::endl;
        return;
    }

    int max_width = 0;
    int max_height = 0;
    int max_square_idx = 0;
    for (size_t i = 0; i < squares.size(); i++)
    {
        // Convert a set of 4 unordered Points into a meaningful cv::Rect structure.
        cv::Rect rectangle = cv::boundingRect(cv::Mat(squares[i]));

        //std::cout << "find_largest_square: #" << i << " rectangle x:" << rectangle.x << " y:" << rectangle.y << " " << rectangle.width << "x" << rectangle.height << endl;

        // Store the index position of the biggest square found
        if ((rectangle.width >= max_width) && (rectangle.height >= max_height))
        {
            max_width = rectangle.width;
            max_height = rectangle.height;
            max_square_idx = i;
        }
    }

    biggest_square = squares[max_square_idx];
}

int main()
{
    cv::Mat src = cv::imread("cc.png");
    if (src.empty())
    {
        std::cout << "!!! Failed to open image" << std::endl;
        return -1;
    }

    std::vector<std::vector<cv::Point> > squares;
    findSquares(src, squares);

    // Draw all detected squares
    cv::Mat src_squares = src.clone();
    for (size_t i = 0; i < squares.size(); i++)
    {
        const cv::Point* p = &squares[i][0];
        int n = (int)squares[i].size();
        cv::polylines(src_squares, &p, &n, 1, true, cv::Scalar(0, 255, 0), 2, CV_AA);
    }
    cv::imwrite("out_squares.jpg", src_squares);
    cv::imshow("Squares", src_squares);

    std::vector<cv::Point> largest_square;
    findLargestSquare(squares, largest_square);

    // Draw circles at the corners
    for (size_t i = 0; i < largest_square.size(); i++ )
        cv::circle(src, largest_square[i], 4, cv::Scalar(0, 0, 255), cv::FILLED);
    cv::imwrite("out_corners.jpg", src);

    cv::imshow("Corners", src);
    cv::waitKey(0);

    return 0;
}

Comment: karlphillip - Thanks for the reply, By using this code we are not able to detect the save edges for card on the same image every time. it works randomly.
Comment: If you are working with a static image, the result should be consistent: it should work or not (every time).
Comment: Thank you for getting back, We are selecting the image from the device photo library or by taking photo. Every time we select the same image form library the edge detection works differently.  Is there any specific reason ?
Comment: Not from the OpenCV side of things. I sugest you test with the sample image in my post.
Comment: We are not able to get the proper credit card edges, it is displaying selected portion of the notepad edges instead, so how can this issue be resolved using the same code. We will be getting images form the camera and hence they will come dynamic and won't be static at all, So kindly suggest us some generic way. Any help or suggestion is highly appreciated in advance.
Comment: I cant help you unless you share the image that you are using to test and the output you are having.
Comment: hey @karlphilip i tried ur code but it is not detecting any square in image.I tried many combination
Comment: @muku I believe you. This code is merely a proof-of-concept that works on a few selected cases. This is not production code. People still need to improve it. ;)
Comment: hello guys i am able to find the squares,but when i Log the cv::Point it is not plot with device coordinates.I want corners of square to make a UIView .But cv::Point is not according to that.values of cv::Point are(1850,2450 etc) and image size(3264*2448).Please help me.thank u
Answer: instead of "pure" rectangular blobs, try to go for nearly rectangular ones.
1- gaussian blur
2- grayscale and canny edge detection
3- extract all blobs (contours) in your image and filter out small ones. you will use findcontours and contourarea functions for that purpose.
4- using moments, filter out non-rectangular ones. First you need to check out moments of rectangle-like objects. You can do it by yourself or google it. Then list those moments and find similarity between objects, create your filter as such.
Ex: After test, say you found out central moment m30's are similar for rectangle-like objects -> filter out objects having inaccurate m30.
Answer: I know maybe it's too late for this post, but I am posting this in case it might help someone else.
The iOS Core Image framework already has a good tool to detect features such as rectangles (since iOS 5), faces, QR codes and even regions containing text in a still image. If you check out the CIDetector class you'll find what you need. I am using it for an OCR app too, it's super easy and very reliable compared to what you can do with OpenCV (I am not good with OpenCV, but the CIDetector gives much better results with 3-5 lines of code).
Answer: I don't know if it is an option, but you could have the user define the edges of it rather than trying to do it programatically.
Comment: This might be better placed as a comment in the question.


Cognitive Text Anaytics with Databricks in PySpark: Mixing string and dictionary/object document input unsupported

Question: I have the following text in my dummy.txt

"Redmond is a city in King County, Washington, United States, located
15 miles east of Seattle."

The document has been uploaded to my datalake folder "/dbfs/mnt/lake/RAW/export/dummy.txt"
I read in the data with the following code:
with open("/dbfs/mnt/lake/RAW/export/dummy.txt", "rb") as fd:
    documents = fd.read()

I then apply cognitive text analytics on the data in the dummy.txt file with the following:
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient

credential = AzureKeyCredential("xxxxxxxxxxxxxxxxxxxx")
endpoint= "https://xxxxxxxx.cognitiveservices.azure.com/"
text_analytics_client = TextAnalyticsClient(endpoint, credential)

response = text_analytics_client.extract_key_phrases(documents, language="en")
result = [doc for doc in response if not doc.is_error]

for doc in result:
    print(doc.key_phrases)

I should get the following:
['King County', 'United States', 'Redmond', 'city', 'Washington', 'Seattle']

But I get the following Type-error:
Mixing string and dictionary/object document input unsupported

Can someone let me know what to do to fix this?
Answer: You can install the client library with:
pip install azure-ai-textanalytics==5.1.0

Create a new Python file and copy the code below. Remember to replace the key variable with the key for your resource and replace the endpoint variable with the endpoint for your resource.
key = "paste-your-key-here"
endpoint = "paste-your-endpoint-here"

from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential

# Authenticate the client using your key and endpoint
def authenticate_client():
    ta_credential = AzureKeyCredential(key)
    text_analytics_client = TextAnalyticsClient(
            endpoint=endpoint,
            credential=ta_credential)
    return text_analytics_client

    client = authenticate_client()

    def key_phrase_extraction_example(client):

    try:
        documents = ["My cat might need to see a veterinarian."]

        response = client.extract_key_phrases(documents = documents)[0]

        if not response.is_error:
            print("\tKey Phrases:")
            for phrase in response.key_phrases:
                print("\t\t", phrase)
        else:
            print(response.id, response.error)

    except Exception as err:
        print("Encountered exception. {}".format(err))

    key_phrase_extraction_example(client)

Output –
Key Phrases:
    cat
    veterinarian

Refer - https://learn.microsoft.com/en-us/azure/cognitive-services/language-service/key-phrase-extraction/quickstart?pivots=programming-language-python
Comment: Abhishek, thanks for this. Could I ask if it would possible to show me how to retrieve the output in a CSV or table?


getting 403 in Ant media rest api

Question: I've tried to call api with postman and put IP filter already. I can access ANT api from postman with path parameters. But I can not access api exactly like in the Ant rest documentation.
For instance getting broadcast item in Ant api document,
/v2/broadcasts/{id}
What I can work in postman,
/rest/request?_path=WebRTCAppEE/rest/v2/broadcasts/{id}
I call this post method  to authenticate first with my email and password in postman.
http://myIp:5080/rest/authenticateUser.
And then I call others api like this after in postman.
http://myIp:5080/rest/request?_path=WebRTCAppEE/rest/v2/broadcasts/623967750864416207633038
It works in postman. But when I tried in my nodejs app, it only works for  authenticateUser. I'm getting 403 for other api. Can someone help me check? Thanks.
Answer: request?_path syntax is valid for the Dashboard requests. You should use as below:
http://myip:5080/WebRTCAppEE/rest/v2/broadcasts/623967750864416207633038

Note: Make sure add your IP address in Application/IP filter Setting
Comment: Thanks @Selim. Now I got access. Problem is I always tried to add my IP from my Dashboard settings like ant documentation. It only works after I call this api and updated remoteAllowedCIDR IP from it.
POST /rest/changeSettings/WebRTCAppEE
Comment: Thanks for answering @Selim
I updated with your api but I'm still getting 403.
I've already add my IP address in IP filter setting.
Comment: Please make sure to add your IP Address in correct application. IP Filter settings should be as below:
127.0.0.1,184.108.40.206
Note: 188.8.131.52 is your sample IP address.

getting 403 in Ant media rest api


How can I make views exposed filter case-sensitive?

Question: When I use the exposed filter, if I search for "IMAG" all results showing image show up.  Is there a way to have the exposed filter search case sensitive?
UPDATE
I added a screenshot to explain more.  I have tried the operators: is equal to, contains, contains any word, contains all words, and regex.  Neither of them are case sensitive.
Comment: Aren't exposed filters case-sensitive by default? What images are you searching for? Is there a way we can reproduce this issue? Can you roughly describe how you've set up your view?
Comment: I have tried the exposed filters in Drupal 8 and none of them are case sensitive.  I am not searching for images.  I am searching for the acronym IMAG.  To reproduce: 1. Create a view for whichever content(make sure that piece of content has IMAG in the title and another that has the word "image" in the title 2. Add exposed filter for Title 3. Add acronym IMAG to exposed filter.  You will notice that it will pull up the node that has the title of "image".  I only want it to pull up the node with the title of IMAG.
Answer: For the solution I used the hook_views_query_alter and used the LIKE BINARY operator in my condition.
<code>condition('node_field_data.title', "%{$word}%", 'LIKE BINARY')
</code>


Browsers not updating when sockets get emmited

Question: I'm trying to create a chat system. Here it is:

The top field takes the name of the user while the bottom textarea takes the message. Once the user pressed enter the middle textarea ( which is disabled) updates itself with the new record. This is done using nodejs sockets. My problem is that if I open another instance of google chrome and I type something the other google chrome textarea does not update. I'm puzzled by this since my code should cover this case:
Here is the server.js code that handles the insertion. After it inserts it, it emits a socket with the last insertion so that the index.html can update itself.
io.sockets.on("connection",function(socket){
socket.on("send",function(data){

    mongodb.connect("mongodb://127.0.0.1:27017/myDatabase",function(err,db){
        if(err) throw err;
        var to_be_inserted = {name: data.name,content: data.content};
        db.collection("chat").insert(to_be_inserted,function(err,objects){
            if(err) throw err;

            var cursor = db.collection("chat").find().sort({_id: -1}).limit(1);

            cursor.toArray(function(err,docs){
                console.log("abc");
                socket.emit("data_to_be_printed",docs);
            });

        });
    })
})

})
As you can see once the data is inserted a socket is emitted containing the last row of the db. The index.html should handle this socket by updating itself. Here is the code that handles it:

Should't the socket be emitted towards all opened sockets?
Answer: In your server program change socket.emit to io.sockets.emit.
socket.emit will send message to specific socket (client) that got connected. io.sockets.emit will send message to all connected sockets (clients)
Comment: so the socket that comes from the argument of the callback is simply the socket that just connected?


OSSIM In Production Environment

Question: I am trying to get some real-world feedback on OSSIM.
Are you using OSSIM in production?
If so, what has your overall experiance been?
How many nodes are in your enviroment?
Finally, what kind of bandwidth are you monitoring?
Thanks!
Anapologetos
Answer: While I don't use OSSIM in an enterprise, here's some feedback I read recently on whether or not OSSIM is "enterprise ready"
http://www.andrewhay.ca/archives/912
This was in a response to OSSIM's post:
http://www.alienvault.com/blog/dk/personal/is_ossim_an_open_source_siem.html
And yet another post related to the thread from Anton Chuvakin:
http://chuvakin.blogspot.com/2009/06/why-no-open-source-siem-ever.html
From what I have read in all of that, it would seem (SIEM?) the product has some potential but some analysts don't think it's there yet. On the other hand, Dominic at alienvault/OSSIM mentioned some examples of large enterprises successfully using OSSIM in their environment.
Hope that helps or provides you with some more information to chew on. Good luck!

OSSIM In Production Environment


how to capture an event for an entire grid

Question: I have a grid that takes up like a quarter of an interface.  I want to capture an event whenever anyone clicks anywhere withing this grid (or a quarter of the interface).  I created a stackpanel and I'vev set the PreviewMouseDown button, but it's not capturing the event when I click anywhere within the space for the grid.  What am I doing wrong here?
         <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>

            </Grid.RowDefinitions>

            <StackPanel Grid.Row="0" Grid.RowSpan="3"
                        Name="sp_activationSpace"
                        PreviewMouseDown="testing"></StackPanel>

             ......

Comment: Try to move PreviewMouseDown="testing" to the grid instead of stack panel like  .
Comment: When I do that, I can click on a button or a label within the grid, and the event is captured.  But it will not capture if I click on white space.  I want it to capture any click I make within the grid.
Comment: Try to check for the type of the OriginalSource in your event handler.
Answer: I don't know, how the rest of the window looks like, but what I usually do is create a Rectangle with usually white background (I don't think it can be transparent but I'm not sure) that is under the Grid I want to capture MouseButtonDown or PreviewMouseDown. It could look like this:
<Grid>
    <Rectangle PreviewMouseDown="MyGrid_PreviewMouseDown" Margin="0"/>
    <Grid>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="Auto"></RowDefinition>

        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Grid.RowSpan="3"
                    Name="sp_activationSpace"
                    PreviewMouseDown="testing"></StackPanel>

         ......
    </Grid>
</Grid>

Again, this is a simple and well-functioning solution, however it might not be suitable if you need transparency. I suspect you have just a white (or any solid color) window background.
Comment: A rectangle did not work, but a blank label did.  Close enough.  works like a charm.  Thanks!!!


Hide whole edit content link column to users

Question: I'm using drupal 7 and have a view with column edit content link. I want to hide a whole column edit to users and only administrator who can see that column.
How can I achieve that without coding since U don't have the permission to access that.
I already tried using Field Permission module but i can't find the solutions either..
Answer: Set up multiple page views displays with the same path.
You can clone your page views display to accomplish this.
Keep their path settings the same.
In the regular user one, remove the Edit field. Be careful that you are only editing "This page (override)" and NOT "All displays."
In the admin views display, under Page Settings > Access, restrict your admin view to role=admin.
Re-order the views displays so the most restrictive (admin) views display is first.
To reorder the displays, look for the item "edit view name/description" in the upper-right corner of the screen and expand it to select "reorder displays" - this will take you to a screen where you can drag and drop the views displays. After you put yours in the right order, click "Apply" then save your view.
Hope this helps.
Comment: I follow your instruction, and it only works for anonymous user. For certain roles, still can see the edit column. I already tried setting in Access under Page Settings (for regular page views) and choose the roles.
Comment: I just did a test view and it works fine on my end. What is the Access setting on your non-admin view? I have mine set to Permission | View published content. If you have something else, try changing it to this and see if that helps. Here is a screenshot of my admin-only views display:  http://i.imgur.com/MBCDFx0.png   And here is a screenshot of my everyone-else views display: http://i.imgur.com/EzX6UKf.png
Comment: This is my admin settings : http://imgur.com/7M8m94A and user settings : http://imgur.com/tCoBcmA. This is the results : Admin-> http://imgur.com/nkfk81o, Authenticated User-> http://imgur.com/nrpagX1, & Anonymous User-> http://imgur.com/nrpagX1. I want view of anonymous is same as authenticated user.. Am i missing something here?
Comment: That's odd: your setting look correct to me. Is it possible these views are being overwritten by a theme template? In other words, do you see a template in your sites/all/themes/yourtheme/template directory that have "view" in their file names?
Comment: hey @anne i resolved this with uncheck "bypass views access control" in permission people. Now the edit column is hidden. Thank you for your kindness answer my questions. Cheers


Formatar data em laravel

Question: tenho esse retorno e quero converter de modo a salvar a data no banco ANO-MES-DIA
$dataOs = date('Y-m-d', strtotime($request->data));

Quando imprimo assim dd($dataOs); as vezes sai da forma certa 2018-11-02 e as vezes sai assim 1970-01-01, só que em todos os testes que realizei testando pelo dd($request->data) a data vem correta parece que é algo na conversão não sei explicar?
Comment: se a data volta 1970-01-01 é por que o $request->data esta passando algo que ele não identifica como data e dai da pau na conversão. tenta printar na tela o $request->data e en seguida o $dataOs  e vai fazendo o debug disso
Comment: já fiz isso e por incrivel que pareça do request a hr que print aparece a data certa mas no $dataOs da o erro 1970-01-01  sabe, fiz esse teste para verificar
Comment: faz assim echo $request->data." | ".$dataOs."";
Comment: me manda o q apareceu
Comment: `$request->data` isso sai qual resultado?
Comment: Pessoal achei o problema, devido  a primeira conversão que usava as // convertendo de - para / notei que era esse o problema, quando passei só - convertendo de 2018-02-10 para 10-02-2018 resolveu acredito que de alguma forma as / estava dando algum conflito
Answer: O Laravel já vem com um biblioteca para se trabalahar com datas, é bem fácil de se utilizar. Documentação.
Para converter no formato que você precisa você pode trabalhar com os Mutators
diretamente em seu model, ficaria mais ou menos assim :
 function setDataAttribute($value){
       $dia = substr($value, 8, 2);
       $mes = substr($value, 5, 2);
       $ano = substr($value, 0, 4);

       this->attributes['data'] = Carbon::create($ano, $mes, $dia, 0, 0);

   }

Obs: Não testei o código pra ver se está funcionando, mas a idéia é essa. Desta forma você não precisa ficar tratando isto no controller. Suponhamos que você tenha que integrar via API sua aplicação, fazendo desta forma você não precisa se preocupar em converter o campo data novamente porque já esta pronto.

Formatar data em laravel


Index on a Date column - use Between/And or Greater/Less

Question: I was asked this question at an interview. AdventurWorks2012.Sales.CreditCard table. First, I created a non-clustered index on ModifiedDate column to simulate the interview question. I need to find records for, let's say July, 2007
<code>SELECT cc.CreditCardID, cc.ModifiedDate
FROM sales.CreditCard cc
WHERE cc.ModifiedDate >= '07/01/2007' OR cc.ModifiedDate < '08/01/2007'
------------
SELECT cc.CreditCardID, cc.ModifiedDate
FROM sales.CreditCard cc
WHERE cc.ModifiedDate BETWEEN '07/01/2007' AND  '07/31/2007'
------------
SELECT cc.CreditCardID,  cc.ModifiedDate
FROM sales.CreditCard cc
WHERE Year(cc.ModifiedDate) = 2007 AND month(cc.ModifiedDate) = 7
</code>
Here are the execution plans. 1) I don't get it why Between is the fastest query and the only one using NCIndex, whereas I was told standard SQL(first query) would be the fastest. 2) They say, avoid using functions in where clause, but, it is showing the same result with the first query where I don't use a function.
Comment: The first two aren't the same semantically - are you sure you mean `OR` in the first?
Comment: Note that your first question will give all rows because you have an or.
Comment: My bad, sorry, question closed. Thanks for the answer
Answer: As noted in the comments. The first statments where clause is incorrectly using OR instead of AND and will make the statement return all rows which will be slower than a subset of the rows.
Comment: well, my answer basically answers both his questions.


How to uniquely identify image in Windows Phone Image Library?

Question: Is there any way to uniquely identify an image in Windows Phone 8 Image Library ?
I want to check if an image exists in user's Library and if not let the user save this image. At this time i have solution that merely works by checking the name and Date the image was taken. There is however the possibility two images have the same name and date taken.
Comment: You could checksum it in some way, and compare checksums. (Or compute a hash - same thing.) You'd need to read the image's bytes to do this.
Answer: I don't know it it will work or not but atleast worth a try.
BY_HANDLE_FILE_INFORMATION structure.
Another link to support that might help, compare two files. The second link is in VB, if you know a little bit of VB, you can convert to C#
Comment: I am doing the same thing with music files on sd card in windows phone, Thanks for cofirming. I was going to break my head over that.
Comment: Unfortunately BY_HANDLE_FILE_INFORMATION is not supported in Windows Phone 8. Thank you anyway


Is an array a type or a value?

Question: I'm writing my own language (just for fun), and I'm about to implement arrays.
I define an array as a datastructure with a fixed, final length, that contains multiple values. So they should work like in Java i.e. C-like
An array should be created with:
nr[] a = [1, 2, 5, 3, 4]

Where nr is the type of the values, a is the name, and [1, 2, 5, 3, 4] the container.
Arrays shall be passed to functions per name with:
main {
    nr[] a = [1, 2, 5, 3, 4]
    printArray(a)
}

func printArray(array):
    print(array)

or literally
main:
    printArray([1, 2, 5, 3, 4])

func printArray(array):
    print(array)

The language contains three main datatypes, bool, text and nr, as well as the var keyword, which sets the type accordingly to the value. An array can be passed between methods, and also stores values itself, like a variable.
I call things that store a value, like literals (5, "text" or false), or names (a = 10), or calls (function()), "ValueHolders", because they all hold one specific value.
Now onto my question: Is my array a ValueHolder or a datatype, or do I have to come up with something completely new for that?
Please shower be with examples from other languages, I'd love to learn more!
EDIT; A few examples:

A type with Arraybrackets is an "arraytype": nr[] a = ...

Anything behind that declaration gets implicitly casted to a nr-array.

Arraybrackets with values/calls create an "arrayliteral": ["a", b(), [1, 2, 3]]

This is a var-array, where the type of the values in the array is unknown. There is no way of finding out in advance, which type these values share, because there not generated, until the line gets executed. Why? If a value in this array is the return-value of a funtion, the function has to get invoked, and in a non-functional language, this can possibly alter the whole structure of the program.

A function can set an array as a return-value: func a() -> nr:

That just means: Cast every return-value to a nr-array. If this doesn't work, the returnvalue is invalid, an the program throws a runtime-exception. (Not the cleanest way, i know, but returntypes are optional, and more like a guideline, than a strict rule.)
Comment: I removed the [tag:c] and [tag:java] tags because this question is not about either of those languages. The first comment above is unsatisfiable (as noted by the second comment). It's not tags which this question needs. What it might need is a clearer exposition of the semantics of your language. Also, I find the title a bit odd. Is an integer a type or a value? "Integer" is the *type* of a particular *value*, clearly. 42 is an integer value. Arrays are not different. `[1.2.3]` is an `nr[3]` value, in your telegraphic language.
Comment: The answer depends on the language envirnoment. Please [edit] the post and add appropriate language tags.
Comment: @JohnAlexiou well the OP says they are implementing a new language so I guess there isn't one
Comment: I've now added Java and C as tags, because thats the most similar implementation I know, but I dont really think that fits, as this question isn't about these languages.
Comment: I think of an array as sort of a "meta" type, because you can have arrays of numbers, arrays of booleans, arrays of strings, and (presumably) arrays of other arrays.  Or, at least, that's the way the C type system works: for any type T, you can also have types array-of-T, pointer-to-T, and function-returning-T.
Answer: I think what you want to know, really depends on the context, because var[] has a completely different purpose than [1, 2, 3].
In your words:

the container [val1, val2, ...] is a ValueHolder, because it can be passed to functions and declarations. It returns an array value.
If you defined an array as var[] array = ..., "array" is also a ValueHolder, which returns an array value as well.
var[] a = ... is a declarative type, it tells which type is expected for the value after the =.
a[1] = ... is an array access, but neither a ValueHolder, or a declaration.
function(a[2]) is another array access, but this time, it holds a returnvalue. Imagine this as something similar to a function call.

For your language, you have to deal with each of these cases seperatly, they all have different meanings. Good luck :)

Is an array a type or a value?


textfield with #disabled = true, #input = false

Question: A textfield with #disabled = TRUE will appear greyed out, and be unchangeable.
A textfield with #input = FALSE will not send its value to $form_state['values'], and will be ignored by system_settings_form_submit().
Unfortunately, a textfield with #disabled = TRUE and #input = FALSE no longer appears greyed out.
I want to make a textfield that is purely for display purposes, so a person can copy a value from there. What is the best way to do this?
EDIT I: What I tried so far
To clarify, my starting point is code like this:
$form['thetext'] = [
  '#type' => 'textfield',
  '#title' => t('The text'),
  '#disabled' => true,  // This should make it greyed out - but it doesn't due to #input = false below.
  '#input' => false,  // This should make this field ignored in $form_state['values'].
  '#value' => 'xyz',  // Either #value or #default_value.
];

EDIT II:
If this wasn't clear before: I really want the html output to be a textfield element. I also want to use the regular form + theme API, so that the element gets a label and wrapper div and theming like other form elements in the same form. E.g. if I would just hardcode the textfield html, it would no longer have the same classes, wrappers etc.
Comment: One might (quite reasonably) argue that from a semantics point of view, you shouldn't be using an `` if you don't want, well, input :) Output is arguably better left to tags/structures that suit that were designed for it. Take Digital Ocean as an example: when you hover over stuff that's useful to be copied, but can't be edited, a "copy" link appears to the right. Implementing something like that would be more pretty simple (a class and a few lines of JS), and it might, again arguably, be more idiomatic and better UI/X
Comment: You have a point there.. but I somehow doubt that the reason why #input = false and #disabled = true don't combine well is a conscious design decision from Drupal developers :)
Comment: I think a good answer would start with your caveat about design considerations, then explain why #input = false seems to nullify #disabled = true, then show how it can be done despite this. Maybe by explicitly setting something in #attributes.
Answer: Use a markup element instead.
$form['element'] = array(
    '#type' => 'markup',
    '#markup' => 'This is the thing you want people to see. You can use html, too.',
);

You don't actually have to specify #type => 'markup' because drupal defaults to that type if you don't specify, but I do anyway as it seems to be good practice.
Comment: But this does not appear as a text field. Of course I could hardcode the html into the #markup, but then it can no longer be themed..
Comment: Ah, well in that case I suppose you could just do '#default_value' => 'your value' for a regular text field. You don't have to disable it, just don't do anything with the value in the submit function.
Comment: But I want it to appear greyed out. And I want the input to be ignored, e.g. if I use system_settings_form() instead of a custom submit function. Of course I could add an additional submit handler that would unset the value form $form_state['values'].. but the goal was not to do that :)
Comment: That's a pretty specific idea... If I were doing it, I would probably use the markup type form element to code it manually so that it has the appearance of a textfield but isn't in $form or $form_state as such.
Answer: input = FALSE is redundant if #disabled = TRUE. Whatever value is in the field will already be ignored if #disabled = TRUE.