Euler products, Merten's theorems, and an unexpected result

Question: I'm going to start by saying I'm mostly out of my depth here. I'm an amateur recreational mathematician. But I've been looking at the Twin Prime Conjecture lately, because it is so fascinating. Easy enough for an 8-year-old to understand, but perplexing mathematicians for millennia. As a science teacher, the whole preponderance-of-empirical-evidence thing seems reasonable enough... but of course, not for mathematicians.
I have no delusion I'll prove anything, but maybe I can contribute something useful? And I have a weird result that may or may not be useful, that I also can't explain.
In relation to the TPC, I've been working on a sieve that generates This OEIS sequence, the numbers for which $6k \pm 1$ are both prime. I'm trying to build off of the work of Dinculescu's various papers (one linked, others at the OEIS link). The TPC is the conjecture that this sequence is infinite.
What does this have to do with Euler products? I've been working with the following one:
$$\prod_{5 \leq p \leq n} \left(1-\frac2p\right)$$
Simple enough. It starts at 5 because it's looking at twin primes, and has a 2 in there because the sieve removes $\frac2p$ of $\mathbb{N}$ each pass.
There are a lot of constants that can be calculated using Euler products. The one I'm working with above isn't one of them, or even similar. So I poked around a bit. Merten's 3rd theorem says this:
$$\lim_{n \to \infty}\ln n\prod_{p \leq n} \left(1 - \frac1p \right) = e^{-\gamma}$$
This theorem seemed interesting to me because the Euler product portion is basically the leftovers in $\mathbb{N}$ after $n$ iterations of the Sieve of Eratosthenes. As $n \to \infty$, the "leftover" set is just $\mathbb{P}$. So, the Sieve of Eratosthenes diverges to zero (as we'd expect since the density of $\mathbb{P}$ is zero), but when multiplied by $\ln n$, it converges. That's a pretty cool trick!
For the heck of it, I tried multiplying the iterations of my product by $\ln n$ to see what came out, which was still divergence to zero, uninteresting. So I looked at Hardy and Littlewood and noticed their constant ends up with an $(\ln n)^2$ term when calculating $\pi_2(n)$. Since my product diverges to zero about twice as fast as Mertens's, I decided to try that out, and I got something that converged! And I tried to figure out if the value it converged to ($\approx 2.49726$) was a known constant. After some trial and error I found:
$$\lim_{n \to\infty} (\ln n)^2 \prod_{5 \leq p \leq n} \left(1-\frac2p\right) \approx 4\lambda$$
Where $\lambda$ is... the Golomb-Dickman constant, something I'd never heard of. (This is by empricial computer calculation only, but it converges to four digits with about 6 million primes.) Apparently $\lambda d$ is the (asymptotic) average number of digits in the largest prime factor a $d$-digit integer.
But! We come around full circle. The complement of that OEIS sequence is the subset of $\mathbb{N}$ for which $6k \pm 1$ are not twin primes. These are all the natural numbers of the form $6ab \pm a \pm b, 1 \leq a \leq b$. That fact was discovered by.... Solomon Golomb.
I have exactly zero clue as to whether what I've written here is particularly useful. I certainly can't explain it. But at least to me, it's pretty interesting. Has anyone here worked with this sequence, and the TPC, and might have an explanation for what seems like it's maybe just a cosmic coincidence? Is this a new result, or am I chasing rabbits others have already chased?
(Edited (1): Typo in the numerical result. After ~6M primes, my value has converged to $2.49726$, not $2.49276$. Only correct to four digits, not five.)
(Edited (2): Gerry Myerson, in the comments, helpfully pointed out a reference that contained the product I was looking at, though starting at 3 rather than 5.  It turns out that my original product converges to $12C_2e^{-2\gamma} \approx 2.497287$, which just happens to be very very close to $4\lambda \approx 2.497320$. The $0.0013%$ difference between the makes them easy enough to confuse when you don't know what you're approaching asymptotically.)
Comment: In Section 2.1, Finch gives $\lim_{n\to\infty}(\log n)^{-2}\prod_{2<p<n}{p\over p-2}=1.20130356=(0.83242907)^{-1}$, rounding to eight decimals. Note this starts with $p=3$, not $p=5$.
Comment: According to Finch, Mathematical Constants, Section 5.4, the Golomb-Dickson constant is $\lambda=0.62433$ to five decimals, giving $4\lambda=2.49732\ne2.49276$.
Comment: Oh, that was a typo. I currently have 2.49726, not 2.49276.  So it's only correct to 4 decimal places currently. I believe $\gamma$ converges similarly slowly if you try to calculate it from Mertens's theorem.
Comment: That second limit from Finch ($0.8324...$) is in fact $\frac13 = \frac{3-2}{3}$  of the value $2.49726$, to four digits.
Comment: Does Finch give a context for the calculation of that product?
Comment: Oh interesting, thanks Google books.  Apparently the (reciprocal) value is in fact $4C_2 e^{-2 \gamma}$, and just happens to be *close* to 4/3 of the Golomb-Dickman constant. That's perhaps even more interesting.
Answer: I wanted to follow up on this briefly, in case anyone was actually watching it. I found the reasoning for the constant given in Finch. First, we take all of the infinite products involved and start them at 3--rather than at 2 (Merten's 3rd theorem) or 5 (the product I was working with). H&L's $C_2$ already starts at 3. Then we have these, assuming $ 2 < p < n $ for all products:
$$C_2 = \prod\left({\frac{p(p-2)}{(p-1)^2}}\right) =\prod\left({\frac{p}{p-1}}\right) \cdot\prod\left({\frac{p-2}{p-1}}\right) \tag 1$$
$$\frac{2e^{-\gamma}}{\ln n} \sim \prod\left({\frac{p-1}{p}}\right) \tag 2$$
Then multiplying $(1)$ by $(2)$ (leaving behind equality but keeping asymptotic notation):
$$C_2\frac{2e^{-\gamma}}{\ln n} \sim \prod\left({\frac{p-2}{p-1}}\right) $$
If we multiply by $(2)$ a second time, we get:
$$C_2\frac{4e^{-2\gamma}}{(\ln n)^2} \sim \prod\left({\frac{p-2}{p-1}}\right) \cdot\prod\left({\frac{p-1}{p}}\right) = \prod\left({\frac{p-2}{p}}\right) $$
$$C_2\frac{4e^{-2\gamma}}{(\ln n)^2} \sim \prod\left({1-\frac2p}\right)$$
which is the result given by Finch, and the result I was looking for in the original question.
Answer: The connection between Euler's totient function
$$\phi(n)=n\prod_{p|n} \bigg(1-\frac{1}{p}\bigg)$$
and your formula
$$n\prod_{5\le p \le n} \bigg(1-\frac{2}{p}\bigg)$$
is $\to$ both are 'emptiness calculating formulars'.
This example PHI(105)InGeneralDivisorPlane shows the 48 resulting values of $\phi(105)$ in the 'general divisor plane':
$\to$ If all numbers divisible by $3, 5$ and $7$ are excluded, 48 'empty lines' remain (here 'filled' as green parallels).
In my answer to this Why is the twin prime conjecture hard? post I argued, that in the 'special divisor plane for twin primes', where $y$ is $n \equiv 0 \pmod 6$, this also 'emptiness calculating formula'
$$y\prod_{(x \equiv 0 \mod 6)\pm 1} \bigg(1-\frac{2}{x}\bigg)$$
represents a lower bound for twin primes (since including non-prime x-values leads to an underestimation of twin primes).
By the way, lower bounds for prime quadruplets etc. can also be constructed using the same approach.
I'm always surprised that the structures that exist in $\Bbb Z$ are usually not visualized using divisor planes !?
Comment: I hadn't heard of a divisor plane before. What is that chart supposed to show? I understand the lines dx and the labeling of divisors as points, but how is it calculating the totient? And what's up with the quadratic in the bottom right?
Comment: Yes, divisor planes are not yet known where they can visualize so much so easily. You get the totient of 105 = 3 * 5 * 7 = 2/3 * 4/5 * 6/7 = 48. ALL green lines together are the resulting 48 lines. And the green lines (= natural numbers) are solutions because they are neither divisible by 3, nor by 5 or 7, so to say fill the 'horizontal emptiness' between divsor points in the 'x-columns' 3, 5 and 7. Twin primes can be counted in the same way --> https://i.stack.imgur.com/LK5Gb.jpg
Comment: If you are interested in the parabolic properties of the general divisor plane  --> https://de.wikipedia.org/wiki/Faktorisierungsmethode_von_Fermat#/media/Datei:Fermat's_factorization_in_the_divisor_plane.gif


Equivalence between two definitions of winding number

Question: I've noticed that the definition of a winding number is rather different in Stewart & Tall's Complex Analysis than in Ahlfors' Complex Analysis:
Let $\gamma:[a,b]\to\mathbb{C}$ be an arc and let $a\in\mathbb{C}$ lie outside its image.

Definition (Stewart, Tall): the winding number $w(\gamma,a)$ is defined as
$$\frac{\theta(b)-\theta(a)}{2\pi}$$
for any continuous choice of argument along $\gamma$ i.e. for any continuous map $\theta:[a,b]\to\mathbb{R}$ such that
$$e^{i\theta(t)} = \frac{\gamma(t)}{|\gamma(t)|}.$$

Definition (Ahlfors): the winding number $n(\gamma,a)$ is defined as
$$\frac{1}{2\pi i}\int_\gamma\frac{1}{z-a}.$$

How can one show these two definitions are equivalent?
Comment: You have a clash of variables between your two sources: $a$ is the left endpoint of the domain of $\gamma$; and $a$ is also the point around which the winding number is defined.
Comment: I don't have a copy of Ahlfors' *Complex Analysis* at hand right now, but I strongly suspect that is definition of winding number is$$\color{red}{\frac1{2\pi i}}\int_\gamma\frac1{z-a}\,\mathrm dz.$$
Comment: You are misusing the term "arc".  It typically refers to a [injective] curve but this does not imply a closed curve hence the Ahlfors definition does not apply.   Compounding this, Stewart & Tall define "arc" on page 240 of their text to refer to a "mod $2\pi$ version of $\text{arg}$", so it is not a curve and conflicts with your co-domain. The term you are looking for is closed curve.
Comment: @JoséCarlosSantos completely right. Edited the post.
Answer: I shall assume that $\gamma$ is a closed curve.
Let $[b,c]$ be the domain of $\gamma$ and define$$\begin{array}{rccc}F\colon&[b,c]&\longrightarrow&\Bbb C\\&t&\mapsto&\displaystyle\int_b^t\frac{\gamma'(u)}{\gamma(u)-a}\,\mathrm du.\end{array}$$I shall prove that$$(\forall t\in[b,c]):\gamma(t)-a=(\gamma(b)-a)e^{F(t)}.\label{a}\tag1$$Assertion \eqref{a} is clearly true when $t=b$. In order to prove that it is true in general, it is enough to prove that $(\gamma-a)e^{-F}$ is constant, which is true, since you get $0$ when you differentiate it (note that this uses the fact, that follows from the definition of $F$, that $F'=\frac{\gamma'}{\gamma-a}$). It follows that\begin{align}\gamma(b)-a&=\gamma(c)-a\text{ (since $\gamma$ is a closed curve)}\\&=(\gamma(b)-a)e^{F(c)},\end{align}and therefore $e^{F(c)}=1$. So, $F(c)\in2\pi i\Bbb Z$.
Now, take $\alpha\in\Bbb R$ such that $\alpha$ is an argument of $\gamma(b)-a$. It follows from \eqref{a} that, for each $t\in[b,c]$,\begin{align}\gamma(t)-a&=|\gamma(b)-a|e^{i\alpha}e^{F(t)}\\&=|\gamma(b)-a|e^{\operatorname{Re}f(t)}e^{i(\alpha+\operatorname{Im}F(t))}\end{align}and therefore the map$$\begin{array}{rccc}\theta^*\colon&[b,c]&\longrightarrow&\Bbb R\\&t&\mapsto&\alpha+\operatorname{Im}F(t)\end{array}$$is a continuous map and, for each $t\in[a,b]$, $\theta^*(t)$ is an argument of $\gamma(t)-a$. And\begin{align}\frac1{2\pi i}\int_\gamma\frac1{z-a}\,\mathrm dz&=\frac{F(c)-F(b)}{2\pi i}\\&=\frac{F(c)}{2\pi i}\\&=\frac{\operatorname{Im}F(c)}{2\pi}\text{ (since $F(c)\in2\pi i\Bbb Z$)}\\&=\frac{\theta^*(c)-\theta^*(b)}{2\pi}.\end{align}And now, if $\theta\colon[b,c]\longrightarrow\Bbb R$ is an arbitrary continuous functions such that, for each $t\in[b,c]$, $\theta(t)$ is an argument of $\gamma(t)-a$, then it is easy to prove that $\theta$ and $\theta^*$ differ by a constant, and therefore\begin{align}\frac{\theta(c)-\theta(b)}{2\pi}&=\frac{\theta^*(c)-\theta^*(b)}{2\pi}\\&=\frac1{2\pi i}\int_\gamma\frac1{z-a}\,\mathrm dz.\end{align}


Find the Lebesgue set w.r.t. to the functions

Question: I'm working on a problem in measure theory:
Find the Lebesgue set $L_f=\{x:\lim_{r\to 0} \frac{1}{m(B(r,x))} \int_{B(r,x)}\lvert f(y)-f(x)\rvert dy=0\}$ if:

$f:\mathbb R^n \to \mathbb C$ is continuous
$f=\chi_{E}$, where $E\in\mathbb R^n$ is Lebesgue null; $m(E)=0$. $\chi$ denotes the indicator function.
$f(x)=\lfloor x\rfloor$ on $\mathbb R$

I'm having a bit of a tough time wrapping my head around what the Lebesgue sets really are, and consequently is struggling to characterize the Lebesgue sets w.r.t to these functions. I could really use some help on these!
Comment: For 1)  all points are Lebesgue points. For 2) $x$ is a Lebesgue point iff $x\in E$. For 3) Lebesgue points are all points except integers. All these follow easily from definition.
Comment: @KaviRamaMurthy maybe I have an ill understanding of the definition... could you elaborate a bit more?
Comment: What do **you** have as the definition?
Comment: @RobertIsrael I edited the post with the definition in the question.
Answer: If $f$ is continuous at $x$ then  $|f(y)-f(x)| <\epsilon$ foe all $y \in B(x,r)$ provided $r$ is small enough. Hence $x$ is a Lebesgue point. In 2) $f=0$ a.e. so you can simply repalce $f(y)$ by $0$ in the definition without changing the integral. Hence $x$ is a Lebesgue point iff $f(x)=0$ which means $x \in E^{c}$. (There was mistake in my comment. I meant $x \in E^{c}$ not $x \in E$). In 3) all points except integers are points of continuity and hence they are Lebesgue points. At integer points it is easy to see that the limit in the definition equals $\frac 1 2$, not $0$.


Translating jQuery to Vanilla JS issue

Question: I was watching a tutorial that used jQuery and wanted to turn it into JS, but my code is broken - was hoping someone could help me with this:
Tutorial JS:
$(function() {
    var btn = $('button');
    var progressBar = $('.progressbar');

    btn.click(function() {
        progressBar.find('li.active').next().addClass('active');
    })
})

Taken from URL:http://www.kodhus.com/kodity/codify/kod/mGXAtb
Here is my failed attempt at rewriting the jQuery using JavaScript DOM:
var btn1 = document.getElementsByTagName('BUTTON');
var progBar = document.getElementsByClassName('progressbar');

function clickMe1() {
    var elm = progBar.querySelectorAll("li");
    var emlClass = elm.querySelector(".active");
    return emlClass.nextElementSibling.addClass('active');
}

btn1.addEventListener("click", clickMe1, false);

where did I go wrong?
Comment: Thank you, this really clarifies the .find() in the jQuery above. I didn't realize it was it was an array issue.
Answer: Working fiddle.
Your code will work after several changes check the notes below :

You've missed addClass() there it's a jQuery function, for vanilla JS use .classList.add() instead:
return emlClass.nextElementSibling.classList.add("active");

querySelectorAll(); will return a list of nodes you have to loop through them and add class, use :
var emlClass = progBar.querySelectorAll("li.active");

Instead of :
var elm = progBar.querySelectorAll("li");
var emlClass = elm.querySelector(".active");

Then loop and add active class:
for(var i=0;i<emlClass.length;i++){
    emlClass[i].nextElementSibling.classList.add("active");
}

getElementsByTagName() and getElementsByClassName() will also returns a list of nodes with given name, you have to specify which one you want to pick (selecting the first in my example) :
var btn1 = document.getElementsByTagName('BUTTON')[0];
var progBar = document.getElementsByClassName('progressbar')[0];

Hope this helps.

var btn1 = document.getElementsByTagName('BUTTON')[0];
var progBar = document.getElementsByClassName('progressbar')[0];

function clickMe1() {
  var emlClass = progBar.querySelectorAll("li.active");

  for(var i=0;i<emlClass.length;i++){
    emlClass[i].nextElementSibling.classList.add("active");
  }
}

btn1.addEventListener("click", clickMe1, false);
.container {
  width: 100%;
}

.progressbar {
  counter-reset: step;
  margin: 0;
  margin-top: 50px;
  padding: 0;
}

.progressbar li {
  list-style-type: none;
  float: left;
  width: 33.33%;
  position: relative;
  text-align: center;
}

.progressbar li:before {
  content: counter(step);
  counter-increment: step;
  width: 30px;
  height: 30px;
  line-height: 30px;
  border: 2px solid #ddd;
  display: block;
  text-align: center;
  margin: 0 auto 10px auto;
  border-radius: 50%;
  background-color: white;
}

.progressbar li:after {
  content: '';
  position: absolute;
  width: 100%;
  height: 2px;
  background-color: #ddd;
  top: 15px;
  left: -50%;
  z-index: -1;
}

.progressbar li:first-child:after {
  content: none;
}

.progressbar li.active {
  color: green;
}

.progressbar li.active:before {
  border-color: green;
}

.progressbar li.active + li:after {
  background-color: green;
}
button {
  position: relative;
  border: none;
  padding: 10px 20px;
  font-size: 16px;
  border-radius: 2px;
  left: 50%;
  margin-top: 30px;
  transform: translate(-50%);
  cursor: pointer;
  outline: none;
}
button:hover {
  opacity: 0.8;
}

Step 1

Step 2

Step 3

<button>Next step</button>
Comment: Thank you all, this really clarifies the .find() in the jQuery above. I didn't realize it was it was an array issue.
Answer: .querySelectorAll("li") will return an array (or an array-like object) with one or more
 tags. So you need to either:

loop through every
 in that list and do the rest,
or just take the first item from that list if you don't want to worry about there being more than one li in the page,
or use .querySelector (not .querySelectorAll) to just take the first
 for you.

MDN
Comment: Care to explain please more please.
Comment: `progBar.querySelectorAll("li")` will return an array with one or more  tags. So you need to either loop through every  in that list and do the rest, _or_ just take the first item from that list if you don't want to worry about there being more than one li in the page, _or_ use `.querySelector` (not `.querySelectorAll`) to just take the first  for you.
Comment: And you'd need to do that for each class selector.
Comment: Also, you're finding "li .active" rather than "li.active", which would find different elements. And you can use `.querSelectorAll("li.active")` instead of doing 2 separate queries.
Comment: The answer itself focussed on the actual problem that was making your code not run, so sorry for the ton of stuff added in the comments.
Comment: Please not in comments but edit you answer (note, i'm not the guy who ask ).
Comment: @ZakariaAcharki: Yeah, fair enough. Though only that first comment clarified the answer. The rest are just pointers as to what the OP could do.

Translating jQuery to Vanilla JS issue


What metrics work well in unbalanced assemblies?

Question: I wanted to know if there are some metrics that work well when working with an unbalanced dataset. I know that accuracy is a very bad metric when evaluating a classifier when the data is unbalanced but, what about for example the Kappa index?
Best regards and thanks.
Comment: [Cross-posted on Data Science](https://datascience.stackexchange.com/q/110722/73930) // [I would argue that class imbalance is minimally problematic.](https://stats.stackexchange.com/questions/357466/are-unbalanced-datasets-problematic-and-how-does-oversampling-purport-to-he) // Check out Frank Harrell's two blog posts related to the idea of evaluating probability predictions: [(1)](https://www.fharrell.com/post/classification/) [(2)](https://www.fharrell.com/post/class-damage/)
Answer: The choice of metric depends on the needs of the application, not the problems with the methods/tools.
Accuracy is not a very bad metric; the main problem is that practitioners fail to use the relative class frequencies to calibrate their expectations.  If 95% of the data belong to the majority class and you get 94% accuracy then of course that isn't very impressive.  One way to get around this is to look at accuracy gain, something like
$$\frac{Accuracy - \pi}{1 - \pi}$$
where $\pi$ is the relative frequency of the majority class.  If you achieve perfect performance you get a score of 1 - if you do as well as the majority classifier you get a score of 0 (indicating that your model has probably learned nothing of interest by looking at the attributes).  In the example above, you would get a negative score, indicating that the classifier is useless.   Now this is an affine transformation of accuracy, so it is still measuring exactly the same thing, just on a more interpretable scale.
Imbalanced problems often have unequal misclassification costs, with false-negatives usually being more costly than false-positives, in which case you should probably look at the expected loss of the classifier instead of the accuracy.  Again, this means focussing on the needs of the application rather than the methods.
However, for this sort of problem you should use a probabilistic classifier, such as [kernel] logistic regression, so you should look at metrics that measure the quality of the predictions of probability, such as the cross-entropy or Brier score.  Probabilistic classifiers are likely to be better as you can experiment with misclassification costs without refitting the model (and do things like implement a rejection operator).  When you have done that as a baseline, then perhaps experiment with non-probabilistic classifiers to see if they have benefits.


How do I prove that this complex function can be approximated uniformly by rational functions?

Question: I have the following problem:

We have $K\subset \Bbb{C}$ a compact subset and $f:K\rightarrow \Bbb{C}$ a continuous function. We take $a\in \Bbb{C}\setminus K$. We assume that there exists a sequence of rational functions $R_n$ with poles in $a$ such that $$\max_{z\in K} |f(z)-R_n(z)|\rightarrow 0$$ Now i fix $k\in \Bbb{N}$ I need to show that there is a sequence of rational functions $Q_n$ with poles in $a$ s.t. $\max_{z\in K} |f(z)^k-Q_n(z)|\rightarrow 0$$

I somehow don't see where to start. I first thought about Runges theorem but there I need a connected set doesn't I?
I also thought about proving it by induction.
I'm really sorry that I can't give you more work, but I have some trubles in working with this approximation stuff. It always seems extreamly frightening to me…
I had the following version of runges theorem:

Let $f:\Omega\rightarrow \Bbb{C}$ be analytic. Suppose $K\subset \Omega$ is compact. Assume $\Bbb{C}\setminus K=U_0 \cup \left(\bigcup_{i\geq 1}U_i\right)$ where the $U_i$'s are open and connected, $U_0\supset\{|z|>R\}$ and $U_i$ are bounded for all $i\geq 1$. Then pick $a_i$ in each connected component $U_i$ for $i\geq 1$ then there exists a rational function $R_n$ with poles in $(a_i)$ s.t.$$\max_{z\in K} |f(z)-R_n(z)|\rightarrow 0$$

Thanks for your help
Answer: Fix $M>\max_K |f|$. Then for some $n_0=n_0(M)$, we have $\max_K |R_n|<M$ for all   $n>n_0$.
Now $Q_n=R_n^k$ satisfies, for all   $n>n_0$ and $z \in K$,
\begin{eqnarray}
|Q_n(z) -f(z)^k| &=& \Bigl|\sum_{j=0}^{k-1} R_n(z)^j \cdot f(z)^{k-1-j}\Bigr| \cdot |R_n(z)-f(z)| \\ &\le&
kM^k \cdot|R_n(z)-f(z)| \,.
\end{eqnarray}
Therefore,
$$\max_{z \in K} |Q_n(z) -f(z)^k|  \le
kM^k \cdot \max_{z \in K}|R_n(z)-f(z)|  \to 0 $$
as $n \to \infty.$
Comment: Sorry how do you see that we need to write $|Q_n(z)-f(z)^k|$ as a sum, as you did? I quite don't see that
Comment: we use $x^k-y^k=(x-y)(x^{k-1}+x^{k-2}y+\ldots+y^{k-1})$ which is standard and easy to check.
Comment: ah perfect thanks. But if I understood this correclty, the information that the poles of $R_n$ are at $a$ is only useful in this case to say that $Q_n$ has only poles at $a$, i.e. this information is not needed elsewhere
Comment: sorry if I disturb again. I thought about where this proof would fail if we wouldn't have no information about neither the poles of $R_n$ nor the one of $Q_n$. In my opinion it would not fail, so I mean it is not necessairly to know where the poles are isn't it?
Comment: Right, no information about the poles is needed.  Do you know how to accept an answer?
Comment: Perfect thank you very much for your answer I appreciate that:)


How do I override the date filter to filter by years instead of exact date?

Question: I want to add another option to Date filter to filter with only years not the exacte date.
So i want to add another radio option here:

Like:

The above options added by Date Filter.
I have tried two ways:

Add new filter like date filter:
modules/custom/my_module/src/Plugin/views/filter/DateYear.php

namespace Drupal\my_module\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\NumericFilter;

/**
 * Filter to handle dates stored as a timestamp.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("date_year")
 */
class DateYear extends NumericFilter {
  // Same Code Date class with a little changes.
}

I tried to extend Date class and alter date filter plugin.

I create Class DateYear extending Date filter class:

namespace Drupal\my_module\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\Date;
/**
 * Filter to handle dates stored as a timestamp.
 *
 * @ingroup views_filter_handlers
 *
 * @ViewsFilter("date_year")
 */
class DateYear extends Date {
  // Override valueForm method..
}

Then i implement hook_views_plugins_filter_alter to override class.

   /**
 * Implements hook_views_plugins_filter_alter().
 */
function cam_views_views_plugins_filter_alter(array &$plugins) {
  if ($plugins['date']) {
    $plugins['date']['class'] = 'Drupal\my_module\Plugin\views\filter\DateYear';
  }
}

But none of the above works ?
Comment: What doesn't work, the discovery? Then check the discovered plugins of this plugin type with `drupal plugin:debug views.filter`? At first glance the fully-qualified class name in the second example looks odd, should contain `\Plugin\views`. Also if you introduce a new plugin like in the first example you have to connect it in views data.
Comment: see https://drupal.stackexchange.com/questions/252874/how-to-alter-view-path-field-plugin
Comment: What doesn't work, in both cases my class doesn't called, and I didn't find the filter in views hi ,I already tested the second option with /plugin/views/filter and it doesn't work,
Answer: I contribute a module for this check it out here:

This module add views filter by year exact, instead of full date like mm-dd-yyyy you can use date year like yyyy.

https://www.drupal.org/project/views_year_filter


Sorting mysql columns (php)

Question: Im trying to make a sort button for a table in php using data from a mysql table. My question is, Do you have to sort the mysql column directly using an ORDER BY query or can you sort the output of the MYSQL data so that it sorts the displayed data without effecting the mysql column?
Here's my code so far that's not doing anything.
<code>if (isset($_POST['sort_torrent'])) {

      try{
    $sort_query = "SELECT movie_name FROM torrent_list ORDER BY
movie_name";

    $db->query($sort_query);
}
catch (exception $e) {
    echo 'Error!' . $e->getMessage();
}
</code>
}
Thanks
Comment: `ORDER BY` doesn't affect the columns in the table. It only affects the result/output from the query.
Comment: The result is just an array. You can sort the array anyway you like. Even after you output to html , you can still sort the result anyway you like, using JavaScript.
Answer:
Do you have to sort the mysql column directly using an ORDER BY query OR can you sort the output of the MYSQL data so that it sorts the displayed data without effecting the mysql column?

Not sure on your question, however, I will try.
The SQL Statement you have there will not affect the data in the database, you have just called the data from the database, then you ordered it in that instance or you could look at it like a copy of the data from the database, now you can do whatever you like with it and not affect your raw data.
When you use INSERT OR UPDATE Etc that will change the data in your database.

Sorting mysql columns (php)


Queries regarding Oracle Siebel Testing

Question: I am new to Oracle Siebel testing. I have read about Oracle Application Testing Suite which can be used for testing Siebel.
I have gone through Oracle videos regarding application testing suite and found below which can be used for Siebel testing:
1. Oracle functional testing (OFT) using openscript
2. Oracle Load and performance testing
I have went through following links:
https://blogs.oracle.com/aamat/entry/siebel_test_automation
http://download.oracle.com/oll/tutorials/ATS9_Siebel_Testing_Accelerators/041210_88547/index.htm
http://www.oracle.com/technetwork/oem/grid-control/overview/twp-ats-test-siebel-133042.pdf
http://www.oracle.com/technetwork/oem/grid-control/overview/ds-application-1.pdf
I have below queries :

Is there any other efficient way of testing Siebel application or is ATS best way to start for Siebel.
Also, what are the frameworks which can be used in ATS ?
IS there sample ATS framework available for Siebel?
Is there any guide or walk-through available which can help in doing a POC for it ?

Please help.
Thanks in advance
Answer: The best way to proceed is to use ATS test Accelerator
The Testing Accelerator for Siebel includes a Test Starter Kit for Siebel 8.1.1, providing example test automation scripts for functional as well as load / performance testing. The test scripts may be used as templates in order for testers to better understand how to script against Siebel applications or they may be modified to work against your own customized Siebel environment and speed up your testing process.
Visit this URL to download Oracle Testing Accelerator for Oracle Siebel: http://www.oracle.com/technetwork/oem/app-test/apptesting931-1610487.html
Answer: Oracle ATS is the official tool to automate Siebel GUI.
There are 2 different Siebel GUI, one is the old HI/SI, which is activeX and need IE to view. another one is called OpenUI, which is pure web.
Oracle ATS both support them, following video at Oracle show how to test Siebel.
the first one is created by me. and the last two for load testing Siebel.
https://otube.oracle.com/media/Siebel+OpenUI+functional+testing+new+feature++in+OATS+126.96.36.199+by+Sophia+Du/0_eya24f4k/2902
https://otube.oracle.com/media/Siebel+load+testing+%28Part+IA+creating+load+script%29/0_2hlxkxa6/2902
https://otube.oracle.com/media/Siebel+Load+Testing+%28Part+IIA+How+to+Do+Performance+Testing+for+Siebel+OpenUI+Application%29+by+Anna+Huang/0_raccbegx/2902

Queries regarding Oracle Siebel Testing


How can I send an email if there are any change in fieldname values (example email, stats of task) by checking from existing database

Question: I need your help or suggestions in create custom module where I need send an email to user and admin if there is any status change in ticket or field values changes (such as description updated) in existing database.

Things I know:
drupal_mail() function helps me to send email and HOOK_mail() helps me to format the mail structure and I need to implement these in '<module_name>.module' file.

But,i need your suggestions in implementation for checking the data values of specific field(such as description field). if values are change, mail need to trigged.
I am open for any suggestions. Thank you
Answer: If I understand your question correctly, you need to know what has been changed when an entity is updated. You can do that using hook_entity_update() or hook_ENTITY_TYPE_update():

This hook runs once the entity storage has been updated. Note that hook implementations may not alter the stored entity data. Get the original entity object from $entity->original.

For instance, assuming you want to know whether the field_description has been changed when a node gets updated, you can do that in your .module file:
/**
 * Implements hook_ENTITY_TYPE_update() for node entities.
 */
function <module-name>_node_update(NodeInterface $node) {
  if ($node->get('field_description')->value !== $node->original->get('field_description')->value) {
    // Apply the logic when the description field changes.
  }
}

where <module-name> is the name of your module.
Comment: Worked... thank you


Converter Json para um tipo Long

Question: Tenho um objeto Json vindo do localStorage e preciso que  ele seja convertido para um tipo Long, no caso entidadeId
idEntidade = localStorage.getItem("idEntidade");
var entidadeId = JSON.parse(idEntidade);

Comment: @Sergio Verdade. Valeu!
Comment: _" seja convertido para um Long"_ - o que é um "Long"?
Comment: do tipo Long, tal como int
Comment: Isso não é `Java`?
Comment: To fazendo pelo javaScript
Comment: Não conheço esse tipo _Long_.
Comment: @EduardoKrakhecke ok. No JavaScript não há _Long_. Essa string que queres converter tem texto ou só numeros?
Comment: @Sergio tem só um numero
Comment: Então a resposta do Breno faz o que precisas.
Comment: Mesmo o Long não existindo, fiquei curioso pra saber qual seria o propósito em converter? Ficaria muito grato em saber :)
Comment: @DvD é porque o `localStorage` guarda tudo em _String_.
Comment: @DvD é que eu estou guardando um parâmetro que é um id no localStorage, e nesse momento eu preciso passar ele em uma requisição,e a requisição pede um tipo Long, porém ele é uma String no localStorage, então precisei converter.
Comment: Entendi. MAs esse Long vc tirou de onde, do Java?
Comment: @DvD sim, está vindo do Java.
Answer: Como JavaScript não é uma linguagem fortemente tipada, sugiro você apenas converter sua váriavel para um número, da seguinte forma:
idEntidade = Number(localStorage.getItem("idEntidade"));

Converter Json para um tipo Long


If $u = \sum_{k=1}^\infty d_k w_k$ where $d_k = (u,w_k)_{L^2(U)}$, why is $\sum_{k=1}^\infty d_k^2 = \|u\|^2_{L^2(U)}$.

Question: I'm going through a proof from Evans, and I'm stuck on what I think must be an easy linear algebra question.
Let $\{w_k\}_{k=1}^\infty$ be an orthonormal basis of $L^2(U)$, where $w_k\in H_0^1(U)$ is an eigenfunction with eigenvector $\lambda_k$.
If $u\in H_0^1(U)$, we can write
$u = \sum_{k=1}^\infty d_k w_k$
where $d_k = (u,w_k)_{L^2(U)}$. Additionally,
$\sum_{k=1}^\infty d_k^2 = \|u\|^2_{L^2(U)}$.
Why does this last equality hold? Is there some simple way to determine $||u||^2_2$ in terms of the orthonormal basis?
Any help appreciated!
Answer: This is just the Parseval Identity:
Let $H$ be an inner product space, and $(e_n)_{n=1}^\infty$ an orthonormal basis for $H$. Then for any $x \in H$ we have:
$$\|x\|^2 = \sum_{n=1}^\infty \left|\langle x, e_n\rangle\right|^2$$
This follows from linearity and continuity of the inner product:
Since $(e_n)_{n=1}^\infty$ is an orthonormal basis we have:
$$x = \sum_{n=1}^\infty \langle x, e_n \rangle e_n$$
So:
\begin{align}
\|x\|^2 = \langle x, x \rangle = \left\langle \sum_{n=1}^\infty \langle x, e_n \rangle e_n, \sum_{n=1}^\infty \langle x, e_n \rangle e_n\right\rangle = \sum_{n=1}^\infty \langle x, e_n \rangle \overline{\langle x, e_n \rangle} = \sum_{n=1}^\infty \left|\langle x, e_n\rangle\right|^2
\end{align}
Answer: We compute:
\begin{align*}
\|u\|_{L^{2}(U)}&=\langle u,u\rangle_{L^{2}(U)}\\
&=\left\langle\sum_{k=1}^{\infty}d_{k}w_{k},\sum_{\ell=1}^{\infty}d_{\ell}w_{\ell}\right\rangle_{L^{2}(U)}\\
&=\sum_{k,\ell=1}^{\infty}d_{k}d_{\ell}\langle w_{k},w_{\ell}\rangle\\
&=\sum_{k=1}^{\infty}d_{k}^{2},
\end{align*}
since $\langle w_{k},w_{\ell}\rangle=\delta_{k,\ell}$ by definition of an orthonormal basis.


Teste unitários de um BackgroundService

Question: Preciso implementar testes unitários em um BackgroundService. Já li muitos post e links a respeito, mas nada foi muito esclarecedor. Como exemplo, criei um Worker que recebe um Parametro por injeção de dependência, que contem uma propriedade do tipo DateTime e ele deve definir qual é o turno do horário.
namespace MessageWorker;

public class Worker : BackgroundService
{
    private readonly ILogger<Worker> _logger;
    private readonly Parametros _parametros;

    public Worker(ILogger<Worker> logger, Parametros parametros)
    {
        _logger = logger;
        _parametros = parametros;
    }

    protected override async Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {
            string turno;
            if (_parametros.DataEHora.Hour < 12)
                turno = "Turno 1";
            else
                turno = "Turno 2";

            _logger.LogInformation("Worker running at: {time} - {turno}", DateTimeOffset.Now, turno);
            await Task.Delay(1000, stoppingToken);
        }
    }
}

E o código do Program que faz o registro do HostedService
using MessageWorker;

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddSingleton(new Parametros
        {
            DataEHora = new DateTime(2024, 1, 5, 9, 0, 0, DateTimeKind.Utc)
        });
        services.AddHostedService<Worker>();
    })
    .Build();

host.Run();

Ao rodar via Visual Studio, o código funciona e consigo ver a mensagem, mas como posso escrever um teste unitário para esse Worker e testar o seu retorno? Qualquer exemplo me ajuda. Obrigado.
Comment: olá veja se isso ajuda: https://stackoverflow.com/questions/56644654/how-can-i-write-unit-test-for-my-background-service
Answer: Consegui implementar da seguinte forma.
[TestMethod]
public async Task TestIsRunning()
{
    var loggerMock = new Mock<ILogger<Worker>>();

    var worker = new Worker(loggerMock.Object, new Parametros { DataEHora = new DateTime(2024, 1, 5, 9, 0, 0, DateTimeKind.Utc) });
    var cancellationTokenSource = new CancellationTokenSource();

    await worker.StartAsync(cancellationTokenSource.Token);
    await Task.Delay(3000); // Wait for some time to let the worker log information
    cancellationTokenSource.Cancel(); // Stop the worker
    cancellationTokenSource.Dispose();

    loggerMock.Verify(
        x => x.Log(
            LogLevel.Information,
            It.IsAny<EventId>(),
            It.Is<It.IsAnyType>((o, t) => o.ToString().Contains("Turno 1", StringComparison.InvariantCultureIgnoreCase)),
            It.IsAny<Exception>(),
            It.IsAny<Func<It.IsAnyType, Exception?, string>>()),
        Times.AtLeastOnce);
}

Bem simples e me fornece o resultado que eu precisava

Teste unitários de um BackgroundService


Após fazer insert, redirecionar para a listagem de registros no Kohana

Question: Comecei a trabalhar com o Kohana ontem e me apareceu um problema aqui.
Meu Controller default é o "page" e a action é "home". No Controller "page" tenho essas funcções aqui para se montar o layout:
class Controller_Page extends Controller_Index {

 public function action_home(){
     $this->template->title = "Helpdesk";
     $this->template->content = View::factory('home');
 }
 public function action_newClient(){
     $this->template->title = "Helpdesk";
     $this->template->content = View::factory('newClient');
 }
 public function action_listClient(){
     $this->template->title = "Helpdesk";
     $this->template->content = View::factory('listClient');
 }
}

Também tenho um Controller chamado "client" e dentro dele essas funções:
class Controller_Client extends Controller_Template {
//put your code here
public function action_novo(){
    $cliente = ORM::factory('cliente');
    $cliente->nomeCliente = $this->request->post('nomeCliente');
    $cliente->cnpjCliente = $this->request->post('cnpjCliente');
    $cliente->dataCadastro = date("Y-m-d H:i:s");
    if($cliente->save()){
        $session = Session::instance();
        $session->set('msg', 'Cliente cadastrado com sucesso!');
        $this->redirect('page');
    }

}
public function action_lista(){
    $cliente = ORM::factory('cliente')->find_all();
    $view = View::factory('listClient');
    $view->set('clientes',$cliente);
    $this->response->body($view);
}
}

No template que está montando, tenho um link que aponta para um formulário, que no caso é a função action_newClient do controller page aonde se cadastra um cliente, funciona normal. Nesse formulário, na action eu estou apontando ela para o método action_novo do controller Client.
Como eu posso redirecionar para uma tela que mostre todos os resultados?
Vejam que fiz o método action_lista no controller Client, mas eu não sei como chamar esse método na view que vai listar os dados.
Comment: Deixa eu ver se entendi. Você quer saber como consumir os dados de clientes na view?
Comment: Mais ou menos isso, na verdade eu até consegui fazer o que eu queria e minha dúvida mudou agora
Seria como eu chamo um metodo de um controle externo dentro de outro controller
Comment: Na boa, não faça isso. Não chame um metodo de um controller em outro, isso é uma má prática. O que eu lhe recomendaria é redirecionar para um outro controller, ou extrair o código em comum para sua própria classe.
Answer: Pelo que entendi vc buscou no banco todos os dados da tabela 'cliente' e ali no

$view->set('clientes',$cliente);

Agora é sua var na view pra mostrar as infos é a 'clientes' você na verdade não chama o método pois tu ja está na view correspondente.


How to export a bazaar branch into a new subversion repository and then pull/push from it

Question: I've been working on a project, using Bazaar as a version control system.
Now I have to work with people offshore, and they only want to use SVN.
What I have:

My bazaar branch with its files and revisions.
A brand new subversion repository.

What I want:

My bazaar branch with its files and revisions.
The subversion repo with the same files and revisions (includings dates and commit messages).
Being able to pull / push from/to the SVN using bzr.

I managed to copy the branch into the svn repo using tailor, but bazaar won't recognized it (Branches have no common ancestor).
Is such a thing possible?
Answer: Yes, this is possible using bzr-svn. After you've created your Subversion repository, simply push to trunk:

$ svnadmin create /my/svn/repo
$ cd /my/bzr/branch
$ bzr push /my/svn/repo/trunk
Pushed up to revision X.

After that, you should be able to pull from the trunk whenever they have made changes. You can push your changes using bzr push, possibly after rebasing them if the branches have diverged (using bzr rebase in the bzr-rewrite plugin).
Comment: Yes, bzr-svn is definitely the way to go! I personally wouldn't go the rebase route though: if you rebase *one* branch, you'll probably need to rebase a bunch of other branches. Instead, I would just make a branch of the SVN trunk and `bzr merge` your changes into that, then `bzr push` back to SVN. (If jelmer tells you that won't work, though, he's probably right: he *is* the author of bzr-svn, after all!)
Comment: This worked for me when I added "--overwrite" key (otherwise I was being reported about "diverged" branches).


TypeError: Cannot read properties of undefined (reading 'type') in redux-toolkit

Question: Does anybody know why I get this error?
When I go to push the book object into the state array then shows this error.

Folder structure

Here is my BookSlice code
import { createSlice } from "@reduxjs/toolkit"

const initialState={
    books:[
        {id:"1", name:"Data Structure", author:"motalib"},
        {id:"2", name:"Structure", author:"hossain"},
        {id:"3", name:"Structure", author:"shamim"},
    ]
}

export const bookSlice=createSlice({
    name:"books",
    initialState:initialState,
    reducers:{
        showBook:(state)=>state,
        AddBooks:(state,action)=>{
            state.books.push(action.payload);
        }
    },
})

export const {showBook, AddBooks}=bookSlice.actions;
export default bookSlice.reducer;

Here is my store code
import { configureStore } from "@reduxjs/toolkit";
import bookReducer from "../Features/BookSlice";

const store=configureStore({
    reducer:{
        bookReducer:bookReducer
    }
})
export default store

Here is my dispatch function

when I go to dispatch the add book reducer then shows this error

import React, { useState } from "react";
import { useDispatch } from "react-redux";
import AddBooks from "../Features/BookSlice"

const AddBook = () => {
    const dispatch=useDispatch()
    const [book, setBook]=useState({
        id:'',
        name:"",
        author:""
    })

    const HandelChange=(e)=>{
        const {name, value}=e.target
        setBook((prev)=>{
            return{...prev, [name]:value}
        })
    }
    console.log(book);
    const HandleSubmit = (e) => {
        e.preventDefault();
        dispatch(AddBooks(book))
        e.target.reset()
        console.log(book);

    }
return(
here is HTML form
);
};

Comment: You've a typo exporting an undefined action. `showBoo` should be `showBook`, e.g. `export const { showBook, AddBooks } = bookSlice.actions;`. Voting to close as "Not reproducible or was caused by a typo".
Comment: `bookSlice.reducer` is the default export from `BookSlice`. You likely meant to import the ***named*** export `AddBooks`. You've still some typos.
Comment: yes, correct it.But still the same problem
Answer: There is a typo here
export const {showBoo, AddBooks}=bookSlice.actions;

It should be
export const {showBook, AddBooks}=bookSlice.actions;

Answer: I think:
import AddBooks from "../Features/BookSlice"

should be changed to:
import { AddBooks } from "../Features/BookSlice"

That is, if you have a similar situation, when you do non-default export, you should use {Curly brackets} during import.


Find three infinite sets A, B, C such that A∆B∆C = {−1, 0, 3}.

Question: I have tried with Z numbers with three sets:
A={n∈Z | n≡0 mod3}
B={n∈Z | n≡1 mod3}
C={n∈Z ∣ n≡2 mod3}
AΔBΔC=(A∪B∪C)−(A∩B)−(B∩C)−(C∩A)+(A∩B∩C)
How could I continue to solve this problem, or am i taking the right approach to this kind of problem. I'm new to discrete mathematics.
Need a bit help if possible.
Thank you in advance :)
Answer: I take it that $\Delta$ is the symmetric difference. It's not obvious that it's associative, but it is.
If you were asked to find three infinite sets $A,B,C$ such that $A\cap B\cap C=\{-1,0,3\}$, hopefully you can see that you need to start by adding $-1,0,3$ to $A,B,C$.
Then you need to make the sets infinite without affecting the intersection. One way to do this is to split the other members of $\mathbb{Z}$ into three infinite sets. A good way to do this is with $0,1,2\pmod 3$, as you suggest, so $A=\{-1,0,3\}\cup \{n\in \mathbb{Z}:n\equiv 0 \pmod 3 \land n\notin\{-1,0,3\}\}$ etc.
It might help you to draw a three-set Venn diagram, shade in the region $A\cap B\cap C$ and see where $-1,0,3$ and the rest of our integers go, to see what we have done.
Then draw another three-set Venn diagram and shade in the region that represents $A\Delta B\Delta C$.
Can you see where $\{-1,0,3\}$ need to go? In what regions can you put infinitely many objects to make $A,B,C$ infinite without adding anything to the symmetric difference?
Answer: The symmetric difference $A\operatorname{\triangle}B\operatorname{\triangle}C$ consists of all elements that belong to an odd number of the sets $A,B,C$.
So put each of the elements $-1,0,3$ into just one (or all three) of the sets $A,B,C$. That gets us $A\operatorname{\triangle}B\operatorname{\triangle}C=\{-1,0,3\}$. But we want the sets to be infinite, so we add more elements to them; in order not to change the symmetric difference, each additional element goes into an even number, namely two, of the three sets.
Put $4,7,10,13,\dots$ in $A$ and $B$.
Put $5,8,11,14,\dots$ in $A$ and $C$.
Put $6,9,12,15,\dots$ in $B$ and $C$.
Finally we have:
$$A=\{-1,4,5,7,8,10,11,13,14,\dots\}$$
$$B=\{0,4,6,7,9,10,12,13,15,\dots\}$$
$$C=\{3,5,6,8,9,11,12,45,15,\dots$$


Zeppelin does not see dependencies from custom repository

Question: I want to add company artifactory to Zeppelin spark interpreter and try to use this document.
So, the URL of our artifactory looks like
http://artifactory.thecompany.com:8081/artifactory/
The access is not restricted to specific user and artifacts are downloadable both from my machine and from machine where Zepplin is running (I tried this with curl).
I've copied the artifact ID from by build.gradle, so I am pretty sure it is correct. However when I try to add the artifact that should be found in my company's artifactory I get error

Error setting properties for interpreter 'spark.spark': Could not find
  artifact
  com.feedvisor.dataplatform:data-platform-schema-scala:jar:3.0.19-SNAPSHOT
  in central (http://repo1.maven.org/maven2/)

This error message sounds like Zeppelin did not try to look for my dependency in custom repository.
I tried to play with artifactory URL using:

http://artifactory.thecompany.com:8081/artifactory/
http://artifactory.thecompany.com:8081/

as well as with "snapshot" property of "Add New Repository" form (using true and false) but nothing helped. The error message does not disappear and classes from the referenced artifact are not found.
Thanks in advance.
Answer: For Zeppelin to use your company's repo by default you can set ZEPPELIN_INTERPRETER_DEP_MVNREPO in your ${Z_HOME}/conf/zeppelin-env.sh:
export ZEPPELIN_INTERPRETER_DEP_MVNREPO=http://artifactory.thecompany.com:8081/artifactory/

Alternatively, you can use Dynamic Dependency Loading feature of the notebook:
%dep
z.reset()
z.addRepo("Artifactory").url("http://artifactory.thecompany.com:8081/artifactory/").snapshot()
z.load("com.feedvisor.dataplatform:data-platform-schema-scala:3.0.19-SNAPSHOT")

Comment: Are there any other configuration options you needed to set to get this to work? It seems like part of the problem is the resolver is adding ":jar:" to the coordinates.
Comment: @dpk Is that a problem? The Maven coordinate system, IIRC is `groupId:artifactId:version:packaging` and the latter is `jar`.


Deleting mongodb record using spring boot MongoTemplate throws error: "Second is Null"

Question: I have created a simple spring boot REST based web application which persists data from mongodb. I am using SpringBoot 2.0.4.RELEASE version. The application works fine with basic CRUD operations provided by MongoRepository interface. I have also implemented custom CRUD operations using MongoTemplate. Everything works fine, but when it comes to delete operation, I get error and can't find what's causing it.
Here's my code snippet:
Rest Controller Class
@RestController
@EnableAutoConfiguration
@EnableMongoRepositories(basePackages = "com.aman.springboot.repository")
@RequestMapping(value = "/student")
public class StudentController {

    @Autowired
    private StudentRepository studentRepository;

    @RequestMapping(value = "/getStudents", method = RequestMethod.GET)
    public List<StudentRepo> getAllStudents() {
        return studentRepository.findAll();
    }

    @RequestMapping(value = "/getStudent", method = RequestMethod.GET)
    public StudentRepo getStudent(@RequestParam(required = true) int id) {
        return studentRepository.findStudentById(id);
    }

    @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
    public void addStudent(@RequestBody(required = true) StudentRepo
                           studentRepo) {
        studentRepository.addStudent(studentRepo);
    }

    @RequestMapping(value = "/removeStudent", method = RequestMethod.POST)
    public void removeStudent(@RequestBody(required = true) StudentRepo
                              studentRepo) {
        studentRepository.removeStudent(studentRepo);
    }
}

Repository Interface
@Repository
public interface StudentRepository extends MongoRepository<StudentRepo,
                                     String>, StudentRepositoryCustom {
    public StudentRepo findStudentById(int id);
}

Custom Repository Interface
Here I have two custom CRUD operations: Add & Delete.
import org.springframework.stereotype.Repository;
import com.aman.springboot.bean.StudentRepo;

@Repository
public interface StudentRepositoryCustom {
    public void addStudent(StudentRepo studentRepo);
    public void removeStudent(StudentRepo studentRepo);
}

Custom Repository Impl
This class implements methods of StudentRepositoryCustom
@Service
public class StudentRepositoryImpl implements StudentRepositoryCustom {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void addStudent(StudentRepo studentRepo) {
        mongoTemplate.save(studentRepo);
    }

    @Override
    public void removeStudent(StudentRepo studentRepo) {
        mongoTemplate.remove(studentRepo);
    }
}

Bean class
@Document(collection = "studentRepo")
public class StudentRepo {

    @Id
    private String _id;
    private int id;
    private String name;
    private String address;

    // Constructors, getters and setters
}

Main Class
@SpringBootApplication(scanBasePackages = "com.aman.springboot")
public class ApplicationLauncher {

    public static void main(String[] args) {
        SpringApplication.run(StudentController.class, args);
    }
}

I have used MongoTemplate for save and remove operations. Save works without errors but remove operation throws following errors(This is full stack trace):
java.lang.IllegalArgumentException: second is null
at org.springframework.data.util.Pair.<init>(Pair.java:41) ~[spring-data- commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]

at org.springframework.data.util.Pair.of(Pair.java:55) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.extractIdPropertyAndValue(MongoTemplate.java:1504) ~[spring-data-mongodb-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.getIdQueryFor(MongoTemplate.java:1518) ~[spring-data-mongodb-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.remove(MongoTemplate.java:1470) ~[spring-data-mongodb-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at com.aman.springboot.repository.StudentRepositoryImpl.deleteStudent(StudentRepositoryImpl.java:29) ~[classes/:na]
at com.aman.springboot.repository.StudentRepositoryImpl$$FastClassBySpringCGLIB$$d56c3928.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at com.aman.springboot.repository.StudentRepositoryImpl$$EnhancerBySpringCGLIB$$a666c62d.deleteStudent(<generated>) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.data.repository.core.support.RepositoryComposition$RepositoryFragments.invoke(RepositoryComposition.java:377) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.data.repository.core.support.RepositoryComposition.invoke(RepositoryComposition.java:200) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$ImplementationMethodExecutionInterceptor.invoke(RepositoryFactorySupport.java:629) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:593) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.0.9.RELEASE.jar:2.0.9.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at com.sun.proxy.$Proxy63.deleteStudent(Unknown Source) ~[na:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at com.sun.proxy.$Proxy63.deleteStudent(Unknown Source) ~[na:na]
at com.aman.springboot.controller.RemoveStudentController.removeStudent(RemoveStudentController.java:30) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:877) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:851) ~[spring-webmvc-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.32.jar:8.5.32]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:109) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-5.0.8.RELEASE.jar:5.0.8.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) ~[tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:800) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1471) [tomcat-embed-core-8.5.32.jar:8.5.32]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.32.jar:8.5.32]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_101]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_101]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.32.jar:8.5.32]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]

Answer: I found the solution after a long research. The problem was, I was sending wrong json data in request body. Wrong in the sense, in DTO, the field annotated with @Id was missing in Json Request which is mandatory for remove() of MongoTemplate. That's why the error Second is null was coming.


Calculate ellipse diameters with five points (center point and four other).

Question: Is it possible to calculate radii (or diameters) of ellipse given:
<code>the center point,
four other points on the ellipse,
and four tangents passing through these four points.
</code>
?
Any algebraic, geometric, or programmatic answer will do the job.
Thanks.
Answer: First of all, there is the question of what it means for points and tangent lines to be "given". I will assume that we have Cartesian coordinates on the plane and that all the given objects are given in these coordinates.
The plan will be pretty straightforward: we'll find the equation of our ellipse, and from that we'll find its radii.
It may seem that 4 points together with their tangent lines is way too much information, but it's not. The thing is, if two of the points are opposite to each other (are inversions of each other through the center of the ellipse), then only one of them gives us any meaningful information. The other one basically repeats what the first one is saying. So, out of the 4 given points, let's pick two that are not opposite to each other. We'll only use these two points and completely ignore the other two.
Let's assume the the center of the ellipse is the origin of coordinates (if it's not, then we can just shift it there), and the two points on the ellipse have coordinates $(x_1,y_1)$ and $(x_2,y_2)$. The general equation of an ellipse centered at the origin is like this:
$$
    Ax^2 + By^2 + 2Cxy = 1.
$$
So, we need to find $A$, $B$ and $C$. We know two points on the ellipse, which gives us two equations (linear in $A$, $B$ and $C$):
$$
    Ax_1^2 + By_1^2 + 2Cx_1y_1 = 1,\\
    Ax_2^2 + By_2^2 + 2Cx_2y_2 = 1.\\
$$
There are $3$ unknowns and $2$ equations. Clearly, this is not enough. So, we'll have to use our tangent lines. Suppose that the tangent line to the ellipse at point $(x_1,y_1)$ has direction $(\Delta x, \Delta y)$. Since the line is tangent to the ellipse, we have an equation:
$$
   A x_1(\Delta x) + B y_1(\Delta y) + C (x_1 \Delta y  + y_1 \Delta x) = 0.
$$
Now we have 3 equations and 3 unknowns. In general, this doesn't mean anything. But in our case it does. We have picked the two points on the ellipse that are not opposite to each other. My geometric sixths sense tells me that these two points and a tangent line at one of them define the ellipse uniquely, therefore the $3$ equations above should have a unique solution $(A,B,C)$. You should probably try and prove this yourself. It seems to be a rather straightforward technical task, and I'm not up to it at this late hour. Or, if you are writing a program, you can just solve the equations programmatically, make your program crash if the solution isn't unique, and hope that it never actually happens ))
Anyway, you can solve this system and find the equation of the ellipse. And when you know the equation of the ellipse ($Ax^2 + By^2 + 2Cxy = 1$), its big and small radii are easy to find: they are equal to $\frac{1}{\sqrt{\lambda_1}}$ and $\frac{1}{\sqrt{\lambda_2}}$, where $\lambda_1,\lambda_2$ are solutions of the quadratic equation
$$
    \left|\matrix{A - \lambda & C \\
                  C & B - \lambda}\right| = 0.
$$
Maybe this is obvious to you, may it isn't. If it's not and you want a proof, here's a sketch: first prove this in the fortunate case when $C=0$. To prove this in general, you can switch to the ellipse's canonical system of coordinates. Oh, by the way, this also explains why I keep writing $2Cxy$ instead of simply $Cxy$ in the equation of an ellipse: if I used $Cxy$, then there would be $C/2$ entries in the matrix above, and that doesn't look good.
So this is it. You first solve a system of 3 linear equations to get $A$, $B$ and $C$, then solve the quadratic equation above to get $\lambda_1$ and $\lambda_2$, and the radii are inverse square roots of these lambdas.
Comment: Thanks, Dan. Still seems hard to me, but I will try this.


Algorithm: Find all common substrings between two strings where order is preserved

Question: Would like to discuss algorithms, no code.
Problem: Let S and T be two sequences of elements. Find the common subsequences between them where the order of the elements is preserved.
It should have O(n + m) running time where n is the length of S, and m is the length of T. I would also like to make the assumption that for the most part the two sequences will be similar.
An optimal solution?: After some research, one solution that appears to be optimal is to first build a generalised suffix tree for the two sequences. Then find the longest common substring and consider this subsequence to be part of the solution. Then either remove this subsequence from the tree or build a new suffix tree with this subsequence removed from the two original sequences to form S' and T'. Then find the longest common substring between S' and T', and so on.
To analyze the running time, building the tree takes O(n) and you can find the lengths and starting positions of the longest common substrings of S and T in O(n + m).
Are there other (more) practical solutions that someone knows of or can link to? Any published papers considering the same or related problem you all know about? Input and constructive criticism about the above solution? Thanks for all your time!
Comment: You may want to ask this in http://cstheory.stackexchange.com/
Comment: @SaeedAmiri Firstly, when you say LCS, are you referring to the longest common substring as my problem discusses, or longest common sequence which is a similar yet different problem? Second, the longest common substring problem can be solved in O(nm) using dynamic programming but by using a suffix tree as I stated in my solution, it is solvable in O(n+m) and in fact can be improved to be solvable in linear time!
Comment: @mcorley, Yeah you are right I think you mean longest common subsequence (as your problem title) and I didn't read it carefully.
Comment: assume strings like `aaaaabbbbcccdde` and `eddcccbbbbaaaaa` in such a way your construction takes sqrt(n) * n not n to find all substrings. (assume alphabet has at least `n` variable, in other case you can find similar contradiction.)
Answer: My first thought was the use of a suffix tree, and relating it to the LCS problem.  But I am not sure what a better solution would be off the top of my head.  I did a quick search and came across a few papers and projects that might be useful, but no guarantees.
http://dl.acm.org/citation.cfm?id=1625377 (direct link here I believe: http://www.aaai.org/Papers/IJCAI/2007/IJCAI07-101.pdf)
http://code.google.com/p/all-common-subsequences/
Sorry, it has been a long day and I am not quite awake enough to attempt a better solution myself.


Is there an alternative to rowCount in a query?

Question: In Drupal 8 we can not use the rowCount property anymore in case of a select statement. Is there an elegant alternative?
I can use a foreach, but when converting from Drupal 7 I would like to replace the result->rowCount() more elegantly.
Answer: According to the Drupal API, "in D8 rowCount() is the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed or throws \Drupal\Core\Database\RowCountException if the last executed statement was SELECT."
If you need to select count of rows you can use $query->addExpression('COUNT(*)');
   $query = \Drupal::database()->select('table', 't');
   $query->addExpression('COUNT(*)');
   $count = $query->execute()->fetchField();

Answer: You can try this:
$query = db_select('node', 'n')->fields('n', ['nid'])->execute();
$query->allowRowCount = TRUE;
$count = $query->rowCount();

Or:
$query = db_query('Select * from node');
$query->allowRowCount = TRUE;
$count = $query->rowCount();

Note: db_select() and db_query() are marked as deprecated, will be removed in Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call select() or query() on it.
Comment: Are you sure this works for Drupal 8 db_select? I never got it to work. Александр Афанасьев worked for me.
Answer: Setting the allowRowCount property to TRUE is not a good way to solve this issue.
A better way to do this is to use  the built in countQuery:
$query = db_select('node', 'n')->fields('n', ['nid']);
$num_rows = $query->countQuery()->execute()->fetchField();

Comment: It should be pointed out that the `if()` statement is not necessary, to count the number of selected rows.
Answer: rowCount() is supported for DELETE, INSERT, or UPDATE statements performed with structured query builders only, since they would not be portable across database engines otherwise
Answer: This is how I did it:
$database = \Drupal::database();
$query = $database->select('node__field_node_url', 'nnu');
$query->addField('nnu', 'entity_id');
$query->condition('nnu.field_node_url_uri', "entity:node/" . $article->entity_id);
$results = $query->execute()->fetchAll();
$count = count($results);


How to do a simple test in ruby

Question: I have some code below that gives square numbers. I also wrote a simple test that prints true or false.
numbers = (1..20)
test=numbers.each do |number|
  puts number * number
end

puts test == [1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400]

I expect it to print true, but it puts false.
I was wondering if anyone could help to see why.
Comment: `numbers.each` returns the input, in this case the range `1..20`. You want `numbers.map` to accumulate the results of the block into a new array.
Comment: ive changed it to .map but it still says false. Anything else need changing? @ChrisHeald
Comment: `puts` returns `nil`, which you could test easily in the REPL (i.e., `irb`). The REPL is your friend: it allows you to instantly test assumptions if you're going to actively avoid tutorials or reading the documentation.
Answer: Try below code to get your answer :
numbers = (1..20)
test = numbers.map{|a| a*a}
puts test == [1,4,9,16,25,36,49,64,81,100,121,144,169,196,225,256,289,324,361,400]

You are using first test which contents 1..20. So you have to modify your code.
Answer: In your code numbers.each do |number| .. end returns the range (1..20) and that's the value that test will be assigned to after the loop through numbers is done. So even if you deleted the line puts number* number, test will still have the value (1..20). So using map would be the right choice to keep your values in an array. But you should first delete puts, because it shows the value on the screen but returns nil. So if you used map but didn't delete puts test will be assigned to an array of nils.

How to do a simple test in ruby


General guidelines on how to derive a hypothesis statistical test?

Question: In general, the process of hypothesis testing can be divided in 4 steps:

Formulate the practical problem in terms of hypotheses.
Calculate a statistic $T$, a function purely of the data. All good test statistics should have two properties: (a) they should tend to behave differently when $H_0$ is true from when $H_1$ is true; and (b) their probability distribution should be calculable under the assumption that $H_0$ is true.
Choose a critical region. We must be able to decide on the kind of values
of $T$ which will most strongly point to $H_1$ being true rather than $H_0$ being true.
Decide the size of the critical region. This involves specifying how great a risk we are prepared to run of coming to an incorrect conclusion. We define the significance level or size of the test, which we denote by $\alpha$, as the risk we are prepared to take in rejecting $H_0$ when it is in fact true.

It seems the most creative step, the one that really sets a specific test apart from others is the choice of the statistic $T$. Hence, my question is: How did authors of statistical hypothesis tests come up with their statistics?
Given a specific problem, is it always obvious what the ideal (if this is definable on objective grounds at all) statistic ought to be? It seems those two requirements listed in step 2 above are two broad and many different statistics could be devised to test the same hypotheses.  For instance, would it have not been a different alternative test to the t-test based on medians or other statistic...?
Comment: Where do the four steps come from? Are you quoting something?
Comment: I would say that commonly your steps 1 and 2 are combined (at least implicitly). That is, conceptually you can imagine you have two data sets: One is collected *before* formulating the hypothesis, and the other is then collected to *test* the hypothesis. (The first may be implicit, e.g. from existing literature/prior work.) Then the you do [exploratory data analysis](https://en.wikipedia.org/wiki/Exploratory_data_analysis), and find some statistics that seems consistent with a preliminary hypothesis (e.g. "these sub-groups seem to be from different normal populations). This is then formalized.
Answer:
How did authors of statistical hypothesis tests come up with their statistics?

There are numerous ways to identify test statistics, depending on circumstances. It's important to try to identify the alternatives you see as important to pick up and try to get some power against those, under some plausible set of assumptions.
If you have a hypothesis relating to population means (in fact, let's make it simple and consider a one-sample test), for example, a statistic based on the sample mean would seem like an obvious choice for a statistic, since it will tend to behave differently under the null and the alternative. However (for example), if you're looking at shift-alternatives for a Laplace / double-exponential family ($\text{DExp}(\mu,\tau)$), something based on the sample median would be a better choice for a test of a shift in mean than something based on the sample mean.
If you have a specific parametric model (based on some particular distribution-family), it's common to at least consider a likelihood ratio test, since they have a number of attractive properties for large samples.
In many situations where you're trying to design a test from scratch, a test statistic will be based on a pivotal quantity. The test statistic in a one-sample t-test (as well as with many other tests you may have seen before) is a pivotal quantity.

Given a specific problem, is it always obvious what the ideal (if this is definable on objective grounds at all) statistic ought to be?

Not at all. Consider a test of general normality against an ominibus alternative, for example. There are many ways to measure deviation from normality (dozens of such tests have been proposed), and at typical sample sizes, none of them is most powerful against every alternative.
In trying to design a test for a situation like that, a certain amount of creativity is called for in coming up with a choice that will have good power against the kinds of alternatives you're most interested in picking up.

It seems those two requirements listed in step 2 above are too broad and many different statistics could be devised to test the same hypotheses.

Indeed. If you make some parametric assumption (assume the data are drawn from some distribution family and then make your hypothesis relate to one or more parameters of it) then there might be a best-possible test for all such situations (specifically, a uniformly most powerful test), but even then if your parametric assumption is more like a rough guess, then a desire for some robustness to that assumption may change things quite a bit.
For example (again, taking a one sample test of location shift to be simple), if I am sampling from a normal population then a t-test will be best. But let's say I think that it may not be exactly normal and on top of that there might be a small amount of contamination by some other process with a moderately heavy-tail, then something more robust (perhaps even a rank based alternative like the signed rank test) may tend to perform better across a variety of such situations.
Answer: A useful test statistic is one whose distribution depends on the parameter of interest and no other part of the statistical model. That way its distribution under the null hypothesis (i.e., when the parameter of interest has the value specified by the null hypothesis) can be fully specified. An ideal test statistic adds to that the property of having a distribution that is strongly dependent on the parameter of interest so that the resulting test has good power.
Consider Student's t-test. It was developed as a significance test (see What is the difference between "testing of hypothesis" and "test of significance"?) for small sample means. The difficulty that Gossett faced was that the distribution of the mean of a small sample from a normal population depends on the parameter of interest, $\mu$, but also a 'nuisance parameter', the standard deviation of the population, $\sigma$. The small sample condition meant that the standard deviation estimated from the sample, $s$, is not an adequate estimate of $\sigma$. To solve the problem Gossett devised the test statistic $t=\sqrt{n}\times \bar{x}/s$ which is dependent on only the data and that has a defined distribution for any given sample size, $n$. Importantly, that distribution is entirely unaffected by $\sigma$. (Actually, that form of the test statistic was a revision by Fisher, if I remember correctly.)
Nowadays it is not always easy to see the genius of Gossett's solution, particularly as his t-statistic looks almost identical to the z-statistic for a normal distribution with known variance (just substitute $\sigma$ for $s$). The hard part was determining the nature of the distribution of the test statistic. Proof that Gossett's distribution was correct didn't come until a later paper by Fisher.
In many cases statistical tests are devised by finding test statistics that take a distribution that can be proved to approximate known distributions under assumptions that are acceptable. Many tests are based on approximations to the chi-squared distribution, for example.


How to copy/move only new records from Table in dev environment to same table in Test environment

Question: I have added some new records in table Table_1 in dev database server. I have exactly same table Table_1 in test database server. I only want to copy the records from table Table_1 in dev database server to the same table Table_1 in test database server which are not present in Table_1 on test database server. What is the best way?
Comment: Do a SELECT INTO SERVER with a subquery filtering DEV with NOT IN
Comment: Do you have a linked server setup between the two?  If so, then you can query them directly (as though they were two tables in the same db).  If not, then you need to create a linked server or use a tool like Import/Export Wizard, define your source and destination and then define the query.  Is your question a t-sql one (ie. "how to find rows that exist in one table that aren't in another"?) or is it "how to move data across servers", or both?
Comment: if any example, that will be good?
Comment: my question is t-sql one (ie. "how to find rows that exist in one table that aren't in another"?)
Answer: Insert into servername.testdb.dbo.Table_1 (ID, column1, column2, etc)
Select ID, column1, column2, etc
From Devdb.dbo.Table_1
Where ID Not IN (Select ID From servername.testdb.dbo.Table_1)

If the ID is an Identity column, you will have to Set Identity_Insert before and after your inserts and also you'd have to run the insert from the Test server and pull the data from Dev.
Comment: This is correct but only if the servers are linked.
Comment: @WEI_DBA, where i run this query? on test server or dev server?
Comment: The statement above is run from the DEV Server. And yes, if you have linked servers. If not, the statement will error out.


Function to select, insert or update from sqlite3

Question: I need know a function to use the sqlite3 the function i want to use is like this:
public class<T> public Task<list<T>>getDB()
{
  _sqLiteConnection = DependencyService.Get<ISQLite>().
  var Query = sqLiteConnection.Table<T>().ToList();
  return Query;
}

but the error messages is:

CS0310 'T' must be a non-abstract type with a public parameterless >>constructor in order to use it as parameter 'T' in the generic type or method
  SQLiteConnection.Table()`
Answer: The error indicates that the Table method has a constraint that limits what types are acceptable for T. You can fix the problem by adding the same constraint to your method:
public Task<List<T>> getDB<T>()
    where T : new()
{
    ...
}

If your class defines T, you would need to apply the constraint to the class instead:
public class MyClass<T> where T : new()
{
    public Task<List<T>> getDB()
    {
        ...
    }
}


Groups of eighths

Question: A lot of times, I've seen eighths in groups of 2, and other times in groups of 4.
Is there any difference in the way you are supposed to play them?
Answer: No, not directly. The beaming may affect how you think about the time subdivisions (four groups of two eighth notes vs. two groups of four eighth notes) which might subtly affect your accenting of the overall piece, but it's not really an explicit thing, no.
For comparison, in choral music, you often see all the eighth notes unbeamed, unless they belong to the same syllable of a word.
Here's some more information as well:
http://www.ars-nova.com/Theory%20Q&A/Q4.html
As that page points out, the beaming does have some indication of meter, in that you wouldn't typically beam across the middle of a 4/4 measure, for example. (Just to be clear: this is more of a case of meter informing beaming, not beaming affecting performance).

Groups of eighths


Filtrar dados em coluna de um relacionamento usando Laravel Eloquent

Question: OBJETIVO: Em uma listagem de entidades Eloquent, quero filtrar pela coluna 'nome' que pertence à tabela relacionada 'pessoas'.
Tenho um conjunto de dados sendo apresentado a uma view com DataTable. Esses dados estão vindo de duas tabelas relacionadas: campanhas e pessoas. No final tenho a seguinte composição:
dt_adesao  | valor | codigo | nome
2023-01-30 | 30.00 | A-365  | Mateus Silva

Colunas da tabela 'campanhas':
'dt_adesao', 'dt_encerramento', 'valor', 'pessoa_id', 'grupo_id'...

Colunas da tabela 'pessoas':
'nome', 'codigo', 'celular', 'dt_nasc'...

Relacionamento no Model Campanha
public function toPessoa(): BelongsTo
{
    return $this->belongsTo(Pessoa::class, 'pessoa_id')->withDefault(['nome' => 'N/D']);
}

Eu preciso de ajuda para montar a query usando o Eloquent do Laravel, de tal forma que tenha ainda disponível os atributos, propriedades e relacionamentos dos Models. Eu até consegui fazer funcionar com Query Builder, mas assim ela não vem com todos os objetos montados, as propriedades personalizadas, então não é interessante.
O que preciso é retornar a lista, filtrando pelo nome que está no model 'Pessoa'.

Em partes, consegui com query builder:
$value = 'tiago';
$campanhas = DB::table('campanha')
    ->leftJoin('pessoas', 'pessoas.id', '=', 'campanha.pessoa_id')
    ->select('pessoas.nome', 'pessoas.codigo', 'campanha.*');

$campanhas->where('pessoas.nome', 'like', '%' . $value . '%');

O que resulta na seguinte consulta:
SELECT campanha_pessoa.dt_adesao, campanha_pessoa.valor, pessoas.nome, pessoas.codigo
FROM campanha_pessoa
left Join pessoas ON campanha_pessoa.pessoa_id = pessoas.id
WHERE pessoas.nome LIKE '%tiago%'

Porém dessa forma perco as propriedades e atributos do Model.
Estou tentando com sub-select, até consigo montar a consulta, porém na hora de filtra dá erro.
$campanhas = CampPessoaPivot::with(['toPessoa','toGrupo','toSituacao'])
->addSelect([
    // nome: alias da sub-select
    'nome' => Pessoa::query()
        // You can use eloquent methods here
        ->select('nome')
        ->whereColumn('id', 'pessoa_id')
        ->latest()
        ->take(1),
    // codigo: alias da sub-select
    'codigo' => Pessoa::query()
        // You can use eloquent methods here
        ->select('codigo')
        ->whereColumn('id', 'pessoa_id')
        ->latest()
        ->take(1)
]);

Passei filtro assim:
$campanhas->where('(select nome from pessoas)', 'like', '"%tiago%"');

O seguinte erro:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '(select nome from pessoas)' in 'where clause'

    SQL: select `campanha_pessoa`.*,
(select `nome` from `pessoas` where `id` = `pessoa_id` and `pessoas`.`deleted_at` is null order by `created_at` desc limit 1) as `nome`,
(select `codigo` from `pessoas` where `id` = `pessoa_id` and `pessoas`.`deleted_at` is null order by `created_at` desc limit 1) as `codigo`
from `campanha_pessoa` where `(select nome from pessoas)` like "%tiago%" and `campanha_pessoa`.`deleted_at` is null)

Answer:         // Valor sendo consultado
        $value = 'thiago';

        // Inicia a consulta trazendo o relacionamento com pessoa
        $consulta = Campanha::with(['toPessoa']);

        // Quando houver pesquisa, filtra pelo nome da pessoa
        $consulta->when(
            $value,
            function ($query) use ($value){
                return $query->whereRelation('toPessoa', 'nome', 'ilike', '%'.$value.'%');
            }
        );

        // Resultados
        $campanhas = $consulta->get();

Não sei se entendi exatamente, mas tente dessa forma. Documentação sobre o método whereRelation() aqui
Comment: Como está escrito atualmente, sua resposta não é clara. Por favor, pode [edit] para adicionar mais detalhes, que ajudarão outras pessoas a entender como isso resolve a pergunta feita. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).


Continuity of probability measure dependent on a measure-parameter

Question: Consider an arbitrary probability spaces $\Omega$ and $S$. Let $\mu$ be an arbitrary probability measure on $S$ and let $\mathbb P^\mu$ be a probability measure on $\Omega$ for every $\mu$ on $S$. Let $\mu_t$ also be a probability measure on $S$ for each $t\in[0,\infty)$ and $\nu$ also a probability on $S$. Suppose $\mu_t\to\nu$ as $t\to\infty$ in the usual sense of weak convergence, i.e. $\int_S f(s)\mu_t(ds)\to \int_S f(s)\nu(ds)$ as $t\to\infty$ for bounded continuous funcitons $f$. Let $B$ be some event in $\Omega$. Do I have that $\mathbb P^{\mu_t}(B)\rightarrow \mathbb P^{\nu}(B)$ a $t\to\infty$? Intuitively, it seems that this should work.
The setup I envision is that we are randomly choosing an element $s\in S$ and that the probability of event $B$ depends on what $s$ we choose. Let $\mathbb P^{\delta_s}(B)$ be the probability of event $B$ if we choose $s\in S$.
My thinking is that:
$$\mathbb P^{\nu}(B)=\int_S\mathbb P^{\delta_s}(B) \nu(ds)=\lim_{t\to\infty}\int_S\mathbb P^{\delta_s}(B) \mu_t(ds)=\lim_{t\to\infty}\mathbb P^{\mu_t}(B).$$
Please let me know if this is not clear, and I can probably refine my text or come up with a more concrete example to illustrate the idea.
Answer: That would be the case only if $B$ is a continuity set of $\nu$, that is if $\nu(\partial B) = 0$.
See the portmanteau theorem: https://en.wikipedia.org/wiki/Convergence_of_measures#Weak_convergence_of_measures
Your "proof" is false, the map $s \mapsto \mathbb P^{\delta_s}(B)$ (which you should really write $1_B(s)$, the indicator function) is not bounded continuous on $S$.
Comment: Yes, I believe this is correct. I suppose my problem is that I don't have an intuitive sense of what an event in the continuity set generally "looks like". So $1_B(s)$ is certainly bounded, but I suppose $B$ being a continuity set for $\nu$ is equivalent to $1_B(s)$  being continuous? If so, then the puzzle is slowly coming together for me.
Comment: No no no. $1_B$ is never continuous except in pathological cases. But the portmanteau theorem tells you that you can extract much more from weak convergence than just bounded continuous functions, in particular convergence of probabilities when the event is a continuity set of the limit.
Comment: I see the logical mistake I made there regarding my (erroneously) concluding the indicator should be continuous. Thanks! You examples are very helpful! I believe I now see that this is really just a straightforward implication of the Portmanteau theorem - though I hope to study the proof of that theorem carefully so that I really understand it
Comment: Secondly, let me give you an example. Assume $\nu$ is a continuous measure (no atoms) on $\mathbb R$, like the Gaussian distribution. Then segments are continuity sets because $\nu(\partial [a,b]) = \nu(\{a,b\}) = 0$. Then $\mu_t([a,b]) \to \nu([a,b])$.
Comment: On the other hands, take $\mu_t$ to be Gaussian with mean zero and variance $1/t$. then $\mu_t \to \delta_0$. But $0=\mu_t(\{0\}) \not \to \delta_0(\{0\}) = 1$. Can you see why the continuity set requirement is not verified here ?
Comment: To conclude, the point is that weak convergence allows to wiggle mass around a little, which causes problems with the boundary of sets.


Permissions issues with uploading to team drive

Question: I've been trying to get the Google Drive PHP API set up to upload a simple file to a shared drive using the following code.
<?php
require_once '../../../php/Services/JSON.php';
require '../vendor/autoload.php';
require '../helper.php';

$chunkSizeBytes = 1048576;

$client = new Google_Client();
$client->setAuthConfig(__DIR__.'/SERVICE-ACCOUNT-CREDENTIALS.json');
$client->setApplicationName('Uploader');
$client->setScopes(Google_Service_Drive::DRIVE);
$client->setDefer(true);

$file = 'testUpload.txt';

$service = new Google_Service_Drive($client);
$params = [
    'fields' => 'id',
    'supportsAllDrives' => true
];
$req = $service->files->create(new Google_Service_Drive_DriveFile([
    'name' => $file,
    'teamDriveId' => 'DRIVE ID',
    'parents' => '1Ik-tFv8UaOmlnZ3ojgPPba0o3hauh_63',
    'mimeType' => Helper::get_mime_type($file)
]), $params);

$media = new Google_Http_MediaFileUpload($client, $req, Helper::get_mime_type($file), null, true, $chunkSizeBytes);
$media->setFileSize(filesize($file));

$status = false;
$fileHandler = fopen($file, 'rb');
while(!$status and !feof($fileHandler)) {
    $chunk = fread($fileHandler, $chunkSizeBytes);
    $status = $media->nextChunk($chunk);
}

fclose($fileHandler);
$client->setDefer(false);
echo "https://drive.google.com/open?id=".$status['id']."\n";

After running this code it gives me a link to a file, but when I visit the link it says I need permission to access the file. When I open the folder on the shared drive the file is nowhere to be seen, so it's being uploaded, but not to the correct place as far as I know. I want to make sure that this file is uploaded to the shared drive, and to the folder specified, but so far I haven't been able to do so. I know some parameters have been deprecated from the API, but I'm pretty sure all the parameters that I'm using are not deprecated. I'm unsure of what I'm doing wrong, so any additional guidance would be appreciated, thanks!
Comment: [This](https://stackoverflow.com/a/56468780/2518525) might be a solution as it looks like a similar problem to do with shared drives?
Comment: There's also a way to set permissions through the API, you might have to make a separate call, passing the file ID returned by the initial upload method.
Answer: The servier account is not you. The service account is uploading the file there for it owns the file.  If you want access to it have the service account grant you access to it though permissions.create
$optParams = array(
   'emailMessage' => '[YourValue]',
   'sendNotificationEmail' => '[YourValue]',
   'supportsTeamDrives' => '[YourValue]',
   'transferOwnership' => '[YourValue]',
   'fields' => '*'
);

return $service->permissions->CreatePermissions($fileId, $optParams);

Also remember to add supportsAllDrives parameter to the initial file upload.