C++ file output only accepts the first word

Question: I'm trying to output a string to a basic .txt file. And it only partially works, and by that I mean that it only accepts the first word of whatever I type. I need to be able to have no character limit as well (the user can type in however much they want)
#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("advice.txt");
    if (in_stream.fail())
    {
     cout << "input file opening failed.\n";
     exit(1);
    }

    char next;

    in_stream.get(next);
    while (! in_stream.eof())
    {
     cout << next;
     in_stream.get(next);
    }

    out_stream.open("advice.txt", ios::app); //Append data to file
    if (out_stream.fail())
    {
     cout << "output file opening failed.\n";
     exit(1);
    }

    //Output text into the file (Problem is in here)

    string mystring;

    cin >> mystring, "\n\n";
    out_stream << " - " << mystring << "\n\n";

    in_stream.close();
    out_stream.close();

    cin.get();
    cin.get();
    return 0;

}

Any future help will be greatly appreciated :) Thanks in advance!
Answer: The extraction operator truncates on whitespaces. Use std::getline() to read the entire line.
Instead of:
cin >> mystring, "\n\n";

Make it:
std::getline( std::cin, mystring );

Comment: Where exactly do i place this in reference to my code? does it replace my cin statement? where do i put it in reference to my out_stream?


Finding the asymptotes of a general hyperbola

Question: I'm looking to find the asymptotes of a general hyperbola in $Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0$ form, assuming I know the center of the hyperbola $(h, k)$. I came up with a solution, but it's too long for me to be confident that I didn't make a mistake somewhere, so I was wondering if I could run it by someone and see if it works. It's mostly algebraic, and I'm prone to making tiny errors in algebra that throw off the entire problem.
So to start, since we know the center $(h, k)$, we can first translate the hyperbola by $(-h, -k)$ using the transform $x_0 = x - \Delta{x}, y_0 = y - \Delta{y}$ with $\Delta{x} = -h$ and $\Delta{y} = -k$. Assuming $F'$ is the translated $F$, we can divide the entire equation by $-F'$ to put it in the following form:
$$
ax^2 + bxy + cy^2 + dx + ey = 1
$$
With $a = -A'/F'$ and $A'$ the translated $A$, $b = -B'/F'$ and $B'$ the translated $B$, and so on.
Next we convert to polar coordinates to get the following:
$$
r^2(a\cos^2{\theta} + b\cos{\theta}\sin{\theta} + c\sin^2{\theta}) + r(d\cos{\theta} + e\sin{\theta}) - 1 = 0
$$
Solving for $r$ will give us
$$
r = \frac{-d\cos{\theta} - e\sin{\theta} \pm \sqrt{(d\cos{\theta} + e\sin{\theta})^2 + 4(a\cos^2{\theta} + b\cos{\theta}\sin{\theta} + c\sin^2{\theta})}}{2(a\cos^2{\theta} + b\cos{\theta}\sin{\theta} + c\sin^2{\theta})}
$$
Now assume $\theta_0 = 2(a\cos^2{\theta} + b\cos{\theta}\sin{\theta} + c\sin^2{\theta})$, this means that as $\theta_0 \rightarrow 0^{\pm}$, $r \rightarrow \pm\infty$. The angles at which $r \rightarrow \pm\infty$ are the asymptotes of the hyperbola, so now it's just a matter of solving for where $\theta_0 = 0$. This is where the majority of the algebra takes place and this is where I'm worried I made some miniscule mistake.
$$
\begin{align}
& 2(a\cos^2{\theta} + b\cos{\theta}\sin{\theta} + c\sin^2{\theta}) = 0\\
& \Longleftrightarrow a(1 - \sin^2{\theta}) + b\cos{\theta}\sin{\theta} + c\sin^2{\theta} = 0 \\
& \Longleftrightarrow \sin^2{\theta}(c - a) + b\cos{\theta}\sin{\theta} = -a \\
& \Longleftrightarrow \frac{1 - \cos{2\theta}}{2}(c - a) + \frac{b}{2}\sin{2\theta} = -a \\
& \Longleftrightarrow (c - a)(1 - \cos{2\theta}) + b\sin{2\theta} = -2a \\
& \Longleftrightarrow (c - a)(1 - \cos{2\theta}) + 2a = -b\sqrt{1 - \cos^2{2\theta}} \\
& \Longleftrightarrow \frac{a - c}{b}(1 - \cos{2\theta}) - \frac{2a}{b} = \sqrt{1 - \cos^2{2\theta}} \\
& \Longleftrightarrow (\frac{a - c}{b}(1 - \cos{2\theta}))^2 - 4a\frac{a - c}{b^2}(1 - \cos{2\theta}) + (\frac{2a}{b})^2 = 1 - \cos^2{2\theta} \\
& \Longleftrightarrow (\frac{a - c}{b})^2(1 - 2\cos{2\theta} + \cos^2{2\theta}) - 4a\frac{a - c}{b^2}(1 - \cos{2\theta}) + (\frac{2a}{b})^2 = 1 - \cos^2{2\theta} \\
& \Longleftrightarrow [(\frac{a - c}{b})^2 + 1]\cos^2{2\theta} + [2(\frac{a - c}{b})(\frac{a}{b} - \frac{a - c}{b})]\cos{2\theta} + [(\frac{2a}{b})^2 + (\frac{a - c}{b})^2 - 4a\frac{a - b}{b^2}) - 1] = 0 \\
& \Longleftrightarrow [(\frac{a - c}{b})^2 + 1]\cos^2{2\theta} + 2(\frac{a - c}{b})(\frac{c}{b})\cos{2\theta} + [(\frac{2a}{b})^2 + (\frac{a - c}{b})(\frac{-3a - c}{b}) - 1] = 0 \\
& \Longleftrightarrow [(\frac{a - c}{b})^2 + 1]\cos^2{2\theta} + 2(\frac{a - c}{b})(\frac{c}{b})\cos{2\theta} + [\frac{a^2 + 2ac + c^2}{b^2} - 1] = 0 \\
\end{align}
$$
Now let
$$
U = [(\frac{a - c}{b})^2 + 1] \\
V = 2(\frac{a - c}{b})(\frac{c}{b}) \\
W = \frac{a^2 + 2ac + c^2}{b^2} - 1
$$
So that the above equation becomes
$$
U\cos^2{2\theta} + V\cos{2\theta} + W = 0.
$$
Solving for $\cos{2\theta}$ gives us
$$
\cos{2\theta} = \frac{-V \pm \sqrt{V^2 - 4UW}}{2U}.
$$
Finally we may solve for theta like so,
$$
\theta = \frac{1}{2}\arccos{\frac{-V \pm \sqrt{V^2 - 4UW}}{2U}}.
$$
This gives us two numbers, $\theta_1$ and $\theta_2$, each corresponding to the slopes $m_1$ and $m_2$ of the asymptotes.
The relationship between the slope of a line $m$ and the angle $\theta$ between the line and the positive x-axis is $m = \tan{\theta}$. You can use the identity $\tan{(\frac{1}{2}\arccos{x})} = \sqrt{\frac{1 - x}{x + 1}}$ to solve for $m_1$ and $m_2$ in terms of non-trig functions, but I think this answer is sufficient enough.
Now that we have the slopes of the asymptotes, we can find the y-intercepts $b_1$ and $b_2$ of each line by simply plugging in the original center $(h, k)$ into each equation for the line and solving.
$$
b_1 = k - m_1h \\
b_2 = k - m_2h
$$
Thus, the asymptotes of the hyperbola with general equation $Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0$ have equations $y = m_1x + b_1$ and $y = m_2x + b2$.
Does this look correct? Also, did I overcomplicate things? Was there an easy solution all along that I was missing?
Comment: If you've translated the origin to the hyperbola's center, you shouldn't have any linear terms left in the equation.
Answer: So a fairly simple calculus based solution arises from knowing that the Asymptotes are the points where the slope tends toward being constant
That is given
$$Ax^2 + By^2  + Cxy + Dx + Ey + F = 0$$
We begin by deriving with respect to $x$
$$2Ax + 2By \frac{dy}{dx} + Cy+Cx\frac{dy}{dx} + D + E\frac{dy}{dx} = 0 $$
And now solve for $\frac{dy}{dx}$
$$\frac{dy}{dx} = -\frac{2Ax + Cy + D}{2By + Cx + E} $$
Now we can take the limit as $x \rightarrow \pm \infty$ to determine the slopes of the Asymptotes and call this values $m$
Now take the original curve, and solve it for $y$ (it won't be too pretty)
$$y = \frac{-E- Cx \pm \sqrt{(E + Cx)^2 - 4B(Dx + F)}}{2B} $$
And evaluate $$ \lim_{x \rightarrow \pm \infty} y(x) - mx $$
to recover the y intercepts of the asymptotes.
This ENTIRE process can now be repeated with $\frac{dx}{dy}$ in place of $\frac{dy}{dx}$ and y exchanged with x to interpret the asymptotes in a reflected coordinate systems
Comment: OOOH jeez I don't know why I didn't even consider that as a solution it's much less involved than mine :). Though I'm worried about the limit. I'm trying to find a solution which I can code (floating point precision isn't a concern), and I'm not sure how I would program something to evaluate a limit like that.
Comment: Built in libraries are an option, the other more work intensive option is to basically use if-else to identify if the dy/dx and dx/dy expressions are even defined (think about what happens if A or B or C etc... = 0 to cover all cases) if they are defined its easy to find the limits (L'Hopitals rule by hand will make it pretty obvious what needs to be coded), for the second part finding the limit can just involve increasing $x$ (or y) for expression $y(x) - mx$ to compute a difference $d_i$ and then pick a desired level of accuracy, w, break program and return when $|d_{i+1} - d_i| < w$
Comment: When you took the derivative with respect to $x$, you missed applying the product rule to $Cxy$. The correct derivative is $-(2Ax + Cy + D)/(2By + Cx + E)$
Comment: @frogeyedpeas just to clarify, what do you mean when you say $\lim_{x \rightarrow \pm \infty}y(x) - mx$? $y(x) - mx \rightarrow \pm\infty$ for all $y(x)$ and $m$, and no test scenario I've been able to come up with has yielded anything other than $\pm \infty$ as a solution for $b$ (take a look at this example problem: http://www.wolframalpha.com/input/?i=limit+as+x+approaches+infinity+of+%28%28-7x+-+sqrt%2849+%2B+4%283x%5E2+%2B+x+-+9%29%29%29%2F-2+-+12.304591605141793x%29).
Comment: Hello @frogeyedpeas, i'm in difficult to compute the limit because the coefficients can be positive or negative. Can you do it the step by step please?
Comment: Note: a method that may be easier; after finding the slope, use the [center](https://math.stackexchange.com/q/1286146/527653) and the obtained slope to get the equation of the asymptotes, by using the point-slope form.
Answer: For a conic, $$ax^2+2hxy+by^2+2gx+2fy+\color{blue}{c}=0$$
which is a hyperbola when $ab-h^2<0$.
Its asymptotes can be found by replacing $\color{blue}{c}$ by $\color{red}{c'}$ where
$$\det
\begin{pmatrix}
  a & h & g \\
  h & b & f \\
  g & f & \color{red}{c'}
\end{pmatrix}
=0$$
That is
$$\color{red}{c'}=\frac{af^2+bg^2-2fgh}{ab-h^2}$$
The asymptotes are
$$\fbox{$ax^2+2hxy+by^2+2gx+2fy+\frac{af^2+bg^2-2fgh}{ab-h^2}=0 \,$}$$
Alternatively, using the centre of the conics
$$
\left(
  \frac{bg-fh}{h^2-ab},
  \frac{af-gh}{h^2-ab}
\right)$$
and the slope $m$ of an asymptote is given by
$$a+2hm+bm^2=0$$
On solving,
$$m=\frac{-h \pm \sqrt{h^2-ab}}{b}=\frac{a}{-h \mp \sqrt{h^2-ab}}$$
Therefore
$$\fbox{$
y-\frac{af-gh}{h^2-ab}=
\frac{-h \pm \sqrt{h^2-ab}}{b}
\left( x-\frac{bg-fh}{h^2-ab} \right)
$}$$
See another answer here for your interest.
Comment: @GennaroArguzzi You must have made some computing error: you should get $c'=AB$, which then gives the right equations.
Comment: @GennaroArguzzi It is $2g=A$.
Comment: Solve for $x$ instead of $y$. Or, better, factor the equation.
Comment: Hello @Ng Chung Tak I tried to use your equation to compute the asymptotes of the equilateral hyperbola $Ax+By+xy-C=0$, but it does not work, I get $y=\frac{-4Ax+(2B-1)AB}{4(B+x)}$, instead the right ones are $y=-A$ and $x=-B$. Can you help me to understand where I did a mistake please?
Comment: @GennaroArguzzi Note that $$\frac{-h \pm \sqrt{h^2-ab}}{b}=\frac{a}{-h \mp \sqrt{h^2-ab}}$$ re-arranging the denominators and putting $a=b=0$ you may get  $hx+f=0$ and $hy+g=0$ for each cases.
Comment: @GennaroArguzzi By the way, using the straight line pair equation in the first box gives $$\frac{2(hx+f)(hy+g)}{h}=0$$ with $a=b=0$.
Comment: Hi @Aretino, I get $c'=2AB$ because in my case $c'=\frac{2fgh}{h^2}$ with $2f=B$, $g=A$, and $h=1/2$. Is it ok? If yes, thus $Ax+By+xy+2AB=0$, from which follows $y=-\frac{Ax+2AB}{x+B}$. This is for sure wrong.
Comment: Thank you @Aretino. In this way I found only one asymptote $y=-A$. Can you tell me how can I get the other one please?
Comment: Thank you for your help Aretino and Ng Chung Tak.
Comment: Direction vectors for the asymptotes can also be found by computing the intersections of the hyperbola with the line at infinity, i.e., by finding nonzero solutions to $ax^2+2hxy+by^2=0$. This is usually a matter of applying the quadratic formula and setting the free variable to some convenient value.


Java : Determine if String is an URL(without www or http), take its screenshot

Question: I am working on a Spring-MVC webapplication in which we are trying to get a screenshot of an URL. Currently I am using PhantomJS for that task, but it's too slow(>10seconds). Also, the URL's have to be with http/https and www for detecting that it's an URL. As this is a chat application, there can be simple URL's which users add like helloworld.com. Any help would be nice. Thank you.
Code:
 String[] words = message.split(" ");
                for( String item : words ){
                     boolean val = ResourceUtils.isUrl(item);
                    if(val){
                        urlIdentifier = calcUrl(item);
                        break;
                    }else {
                        System.out.println("Url false is "+item);
                    }
                }

                if(urlIdentifier!=null){
                    replies.setPreviewIdentifier(urlIdentifier);
                    input.put("preview_identifier",urlIdentifier);
                }

Method to calculate screenshot :
   private String calcUrl(String website){
        try {
            String identifier = String.valueOf(new BigInteger(130, random).toString(32));
            String previewLocation = msg + "chatthumbs/" + identifier ;

            Process proc = Runtime.getRuntime().exec("phantomjs --ssl-protocol=any " +
                    "/home/deploy/phantom/rasterizepdf.js " +" "+website+" " +previewLocation);
            proc.waitFor();
            BufferedImage image = ImageIO.read(new File("/home/akshay/testme.png"));
            if(image!=null){

                if (image.getWidth() > image.getHeight()) {
                    image = Scalr.resize(image, Scalr.Mode.FIT_TO_HEIGHT, 250, 250);
                } else {
                    image = Scalr.resize(image, Scalr.Mode.FIT_TO_WIDTH, 250, 250);
                }
                image = Scalr.crop(image, 250, 250);
                ImageIO.write(image, "png", new File(previewLocation));
            }
            return identifier;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

Any help would be nice. Thank you.
Answer:
(a) I think the process of taking a screen shot is taking time. Is this code running on the same device as the chat screen? Why not use java.awt.Robot to take the screen shot ? or just save the text why do you need a screen shot?

(b) Is the system too busy/ Use on a SSD to see if faster?
(c) But curious as to the end application, is this part of a web app? How will your code run on the client systems? Or will you java agent be installed on all user systems that monitors the web page and takes screen shots? Then why use a web page, use a java app to chat, and parse the text as typed.

Parsing the text. What if user types/ pastes a long message? Are you parsing everything once or on change? Think of ways to improve that if it seems to be a problem. Ignore if not the immediate issue now.
Also if the msg is too long the parsing can take a lot of time. maybe process after every key press or change event (if paste) keep local js copy of previous text to get diff?
Comment: Yes, process of screenshot is taking a lot of time, even on faster network on localhost. That's what I don't understand. As it's a chat application, we want to show preview of the website. I didn't understood your 2nd point. Thank you.
Comment: It depends on the OS, how busy the system is, how fast the drive is (SSD will be much faster).
Comment: This is a difficult question to answer without context, how long is each method taking, what is your goal ... i just wrote a few generic things in my answer, might help you find a soltion


Prove there is no solution for a specific type of polynomials

Question: On an article about Group theory -if I understood correctly- the writer came with a claim to which he/she provided no proof of.
If $p(α)$ is a polynomial construed as $c_0+c_1α+c_2α^2…+c_nα^n$ where each $c_i$ is a rational number, and $α$ is equivalent to the $(n+1)$th root of $k$, being $k$ an integer, then $p(α)$ cannot be equal to $0$.

My shot at proving the latter statement is to write $p(α)$ as $(r_0/s_0)+(r_1/s_1)α+(r_2/s_2)α^2…+(r_n/s_n)α^n$, where each $r_i$ and $s_i$ are integers.
If $p(α) = 0$, then
$(r_0/s_0)+(r_1/s_1)α+(r_2/s_2)α^2…+(r_n/s_n)α^n = 0$
$(s_1*s_2*...*s_n)[(r_0/s_0)+(r_1/s_1)α+(r_2/s_2)α^2…+(r_n/s_n)α^n] = 0(s_1*s_2*...*s_n)$
$b_0+b_1α+α^2…+b_nα^n = 0$, all $b_i$ are integers
$b_1α+b_2α^2…+b_nα^n = -b_0$
From there it seems somehow intuitive that the equality is simply false. But I was not able to prove it, and that is why I'm asking for help.
Any thoughts/ideas would be really appreciated!
Comment: Are there any other restrictions on the $c_i$ or $k$? It seems like $c_i = 1\;\forall i$ makes $\alpha = \exp[2\pi i/(n+1)]$ give $p(\alpha) = 0$.
Comment: Well, α has to be the (n+1)th root of k, being both (n+1) and k integers, so if exp[2πi/(n+1)] is equivalent to an α, then you would have disproved my statement. Maybe I got it wrong.
Answer: Your statement is not true in general. However, it is true if $k$ is a prime. Indeed, under this assumption, $x^{n+1}-k$ is irreducible (in $\mathbb{Q}[x]$), by Eisenstein's criterion. So $k$ is an algebraic number of degree $n+1$ and therefore it cannot be a root of a non-null polynomial with rational coefficients and degree smaller than $n+1$.


Pop Up login Forms for LoginToBoggan

Question: I am using LoginToBoggan Module to unable anonymous user to register using self created password.
Now in some other page I want a popup login form. I have tried using other module like modal forms, but these seems not to work.
Please show me the right way to do this.
Thank you
Answer: Try Popup forms or Colorbox modules. These provide this functionality.

Pop Up login Forms for LoginToBoggan


Formula for additive terms?

Question: If I use GAM, what is the formula for the estimated smoother $s$, i.e. if we have the model $$y \sim s(x)$$
using mgcv?
I can plot the term of $s$, but that isn't really all that informative. I want the exact formula, so that I can relay it in my report?
Answer: There isn't an easily conveyable formula or equation for this model. An alternative way of writing it is in terms of the basis expansion of $x$. If we gather each of the basis functions in the expansion of $x$ (with suitable identifiability constraints applied), plus a column vector of 1s for the model intercept, your model is just
$$ \boldsymbol{y} = \boldsymbol{X\beta} + \boldsymbol{\varepsilon}$$
which is just a multiple linear regression (assuming the family was Gaussian).
As such, you can write out an equation for the model taking the estimated coefficients for each of the terms in $\boldsymbol{X}$ just as you would for a linear model.
The real question is whether this is of any use? By default, what mgcv is doing to the thin plate spline means that the basis functions may well not be all that informative.
If you want something to use to predict from the model in software outside of R, then you can use the predict(model, newdata, type = "lpmatrix") option.
Beyond that there isn't much to offer you; if you want a formula in terms of the basis functions you can write it as:
$$\hat{\boldsymbol{y}} = \beta_0 + \beta_1 \boldsymbol{x}_{b_1} + \beta_2 \boldsymbol{x}_{b_2} + \cdots + \beta_9 \boldsymbol{x}_{b_9}$$
assuming 9 basis functions for the default k = 10 and where $\boldsymbol{x}_{b_j}$ is the $j$th basis function of $x$, replacing the $\beta_j$ with the estimated values from coef(model).

Formula for additive terms?


Calculate the Taylor polynomial of a definite integral

Question: I want to determine the Taylor polynomial of degree 1 to $f$ where $x=0$
$$f(x)=\int_0^{\sin x} \frac{e^t}{1+t^3} \,dt$$
This is my attempt:
$$f'(x)=\frac{e^{\sin x}}{1+\sin^3 x}$$
$$P(x)=f(0)+f'(0)x$$
$$f(0)=\int_0^0 \frac{e^t}{1+t^3}\,dt=0$$
$$f'(0)=\frac{e^{\sin 0}}{1+\sin^3 0}=\frac{1}{1}=1$$
$$P(x)=0+1\cdot x=x$$
Is this correct if not, can you help me where I calculated wrong? :)
Many thanks
Comment: Your answer is correct, your computation is not. Let $g(y) = \int_0^y {e^t \over 1+t^3} dt $, then $f = g \circ \sin$.
Comment: I suggest you this way to solve this kind of exercises: find the Taylor's polynomial of the integrand funtion and then inegrate the new primitive function. In you case you could write the expansion of the exponential and then integrate in $[0, \sin (x)]$.
Answer: You neglected the chain rule, thus:
\begin{align}
f(x) = y & =\int_0^{\sin x} \frac{e^t}{1+t^3} \,dt \\[8pt]
& =\int_0^u \frac{e^t}{1+t^3} \,dt \qquad \text{where } u = \sin x \\[8pt]
\frac{dy}{dx} & = \frac{dy}{du} \cdot \frac{du}{dx} \qquad \text{(This is the chain rule.)} \\[8pt]
& = \frac{e^u}{1+u^3} \cdot \cos x = \frac{e^{\sin x}}{1+(\sin x)^3} \cdot\cos x
\end{align}
So $f'(0) = 1.$
Then
$$
P(x) = f(0) + f'(0)(x-0) = x.
$$
Forgetting the chain rule didn't upset the answer in this case because what you needed to multiply by is $1.$ But in some cases—all cases in which the thing to be multiplied by is not $1$—that will lead to a wrong answer.
Answer: You have
$$f(x)=\int_0^{a(x)} g(t) \, dt \implies f'(x)=g(a(x))\, a'(x)$$ by the fundamental theorem of calculus.
So, for your case
$$f'(x)=\frac{e^{\sin (x)}\cos (x)}{1+\sin^3(x)} $$
Now, it is a nice problem of series composition
$$\sin(x)=x-\frac{x^3}{6}+\frac{x^5}{120}-\frac{x^7}{5040}+O\left(x^9\right)$$
$$e^{\sin (x)}=1+x+\frac{x^2}{2}-\frac{x^4}{8}-\frac{x^5}{15}-\frac{x^6}{240}+\frac{x^7}{90}+\frac
   {31 x^8}{5760}+O\left(x^9\right)$$
$$\cos(x)=1-\frac{x^2}{2}+\frac{x^4}{24}-\frac{x^6}{720}+\frac{x^8}{40320}+O\left(x^{10}\right)$$
$$e^{\sin (x)}\cos (x)=1+x-\frac{x^3}{2}-\frac{x^4}{3}-\frac{x^5}{40}+\frac{7 x^6}{90}+\frac{31
   x^7}{720}+\frac{x^8}{630}+O\left(x^9\right)$$
$$1+\sin^3(x)=1+x^3-\frac{x^5}{2}+\frac{13 x^7}{120}-\frac{41 x^9}{3024}+O\left(x^{11}\right)$$
Long division to get
$$f'(x)=1+x-\frac{3 x^3}{2}-\frac{4 x^4}{3}+\frac{19 x^5}{40}+\frac{187 x^6}{90}+\frac{913
   x^7}{720}-\frac{839 x^8}{630}+O\left(x^9\right)$$ Integrate termwise
$$f(x)=x+\frac{x^2}{2}-\frac{3 x^4}{8}-\frac{4 x^5}{15}+\frac{19 x^6}{240}+\frac{187
   x^7}{630}+\frac{913 x^8}{5760}-\frac{839 x^9}{5670}+O\left(x^{10}\right)$$


Why interface is created

Question: i am new to spring
in my spring web project DAO and service they created the interface and implementing it created class. can we liminate interface.
Comment: why you want to eliminate interface?
Comment: Interface gives you flexibility, in case in future you want to pass it with some different implementaion.
Comment: https://stackoverflow.com/questions/256255/spring-and-interfaces
Comment: @Sangam.R, It's a design pattern. Please check about the design pattern https://www.tutorialspoint.com/design_pattern/data_access_object_pattern.htm
Comment: A better explanation to it can be found [here](https://stackoverflow.com/questions/256255/spring-and-interfaces).
Comment: It seems that you need to go through Java Core and design patterns first because it seems you still cannot understand why interfaces are required overall in Java.
Answer: The whole point of using interface is to create abstraction. When you call you DAO from Service, your service is not aware of the actual implementation of DAO/ cases in which you have multiple implementations of your DAO interface, your service is not aware of the actual impl being used.
With spring you will use - Autowire to inject the dependency. You will refer your Dependency using interface.
<code>    @Component/@Service
    class ServiceImpl {
      @Autowired
      private DAOInterface dao;

      // Rest of code
    }
</code>
Spring knows - to create the instance of this ServiceImpl it needs to inject a concrete impl of DAOInterface type. In case you have concrete implementation, it does so. In case you have multiple impls - you need to define by bean name which implementation you need using @Qualifier.
Interface is kind of a contract for your impl classes. Other than abstracting the actual impl, it helps you decoupling the layers by not directly having a dependency on the concrete classes. This is a good design pattern

Why interface is created


Kafka version: 3.0.1- kafka admin clients created repeatedly - Memory leak

Question: We have springboot app that consumes from a single topic and produces records to multiple topics.
Recently upgraded this app to Sprinboot-2.6.7 and other dependencies accordingly in gradle project.
App is able to consume & produce correctly, BUT the issue is it seems to create kafka adminclients repeatedly(1000s) and seems to be leaking memory(potentially due to this?), ultimately leading to instance crashing and not being able to keep up with lag.
Some kafka related dependent jars in external libraries
<code>   org.apache.kafka:kafka-clients:3.0.1
   org.springframework.cloud:spring-cloud-stream:3.2.3
   org.springframework.cloud:spring-cloud-stream-binder-kafka:3.2.3
   org.springframework.cloud:spring-cloud-stream-binder-kafka-core:3.2.3
   org.springframework.integration:spring-integration-kafka:5.5.11
   org.springframework.kafka:spring-kafka:2.8.5

</code>

Is there a reason for this? missing configuration?
Comment: The admin client is only used to provision topics and it is in a try with resources block; as can be seen in the log the client is closed. Your log seems to indicate provisioning activity. It is not clear why you would provision so many bindings in one application. And, what makes you believe this is the source of the memory leak? Have you looked at a heap dump? Please provide an [MCRE](https://stackoverflow.com/help/minimal-reproducible-example) that exhibits this behavior so we can see what is wrong.
Comment: Your producer log line also has a `3xx` thread number afterwords, so this seems to be related to the AdminClient needing to be created just as many times.
Comment: @GaryRussell  Found the memory leak issue. After all this, we still had to discard the use of StreamBridge anyway and use the (depricated) StreamListener as we were using with Older springboot version. Streambridge version just could not keep up and the throughput was down from 70GB/5 minute to 45GB/5 min. Which got back to 70GB/5min as soon as StreamBridge was removed.
Answer: So adminClient was not the issue. The problem was from the default size 10 of the hashmap that stores output channels.
I have set spring.cloud.stream.dynamic-destination-cache-size=30, since we have actaully 17 output destinations in app already.
In case of the default size 10 of this hashmap "StreamBridge.channelCache" it keeps removing and adding the values to map repeatedly  "Once this size is reached, new destinations will trigger the removal of old destinations" calling GC every now and then.
Answer: In my case it was thread leaking. Threads that write to kafka starts growing "kafka-producer-network-thread". So adding property spring.cloud.stream.source or spring.cloud.stream.output-bindings helps.
i use streamBridge to send.
springBoot version 2.7.14


On the $\pi-\lambda$ theorem and conditional expectation.

Question: I am working on this exercise:

Suppose that $\mathcal{P}$ is a $\pi-$system of subsets of $\Omega$ such that $\Omega\in\mathcal{P}$ and $\mathcal{G}=\sigma(\mathcal{P})\subset\mathcal{F}$. Show that if $X\in L^{1}(\Omega,\mathcal{F},\mathbb{P})$ and $Y\in L^{1}(\Omega,\mathcal{G},\mathbb{P})$ are such that $\mathbb{E}(X;G)=\mathbb{E}(Y;G)$ for all $G\in\mathcal{P}$, then $Y=\mathbb{E}(X|\mathcal{G})$.

I've worked out most parts but I got stuck in the end.
Below is what I've got:
Firstly since $Y\in L^{1}(\Omega, \mathcal{G},\mathbb{P})$, it is automatically $\mathcal{G}-$measurable. It then remains to show that for all $A\in\mathcal{G}$, we have $$\int_{A}Xd\mathbb{P}=\int_{A}Yd\mathbb{P}.$$
Define the collection of sets $\mathcal{L}:=\{A\in\mathcal{F}:\mathbb{E}(X;A)=\mathbb{E}(Y;A)\}$, then we know that $\mathcal{P}\subset\mathcal{L}$ since for all $G\in\mathcal{P}$ by hypothesis we have $\mathbb{E}(X;G)=\mathbb{E}(Y;G)$ so $G\in\mathcal{L}$.
It then suffices to show that $\mathcal{L}$ is a $\lambda-$system, since if $\mathcal{L}$ was a $\lambda-$system,  $\pi-\lambda$ theorem and the above result of $\mathcal{P}\subset\mathcal{L}$ would together imply that $\sigma(\mathcal{P})\subset\mathcal{L}$, which would imply that for all $A\in\sigma(\mathcal{P})=\mathcal{G}$, we would have $\mathbb{E}(X;A)=\mathbb{E}(Y;A)$ as desired.
Firstly $\Omega\in\mathcal{P}\subset\mathcal{L}$ is immediate. Now let $A, B\in \mathcal{L}$ and $A\subset B$, then by the definition of $\mathcal{L}$, we know that $$\int_{A}Xd\mathbb{P}=\int_{A}Yd\mathbb{P}\ \text{and}\ \int_{B}Xd\mathbb{P}=\int_{B}Yd\mathbb{P},$$ but $A\subset B$, so we can use the difference of the integral that $$\int_{B\setminus A}Xd\mathbb{P}=\int_{B}Xd\mathbb{P}-\int_{A}Xd\mathbb{P}=\int_{B}Yd\mathbb{P}-\int_{B}Yd\mathbb{P}=\int_{B\setminus A}Yd\mathbb{P},$$ which implies that $B\setminus A\in\mathcal{L}$.
Then, I tried to show $\mathcal{L}$ is cloud under the limit of monotone sequence, i.e. if $A_{i}\in\mathcal{L}$ and $A_{i}\nearrow A$, then $A\in\mathcal{L}$. I was able to prove when $X,Y\geq 0$, but was not able to extend to a general case.
Below is my attempt:
Let $A_{i}\in \mathcal{L}$ be such that $A_{i}\nearrow A$. By hypothesis both $X$ and $Y$ are integrable. Suppose firstly $X, Y\geq 0$, then since $A_{i}\nearrow A$, we must have $X\mathbb{1}_{A_{i}}\nearrow X\mathbb{1}_{A}$ and $Y\mathbb{1}_{A_{i}}\nearrow Y\mathbb{1}_{A},$ it then follows from the monotone convergence theorem that $$\int_{\Omega}X\mathbb{1}_{A_{i}}d\mathbb{P}\nearrow\int_{\Omega}X\mathbb{1}_{A}d\mathbb{P}\ \text{and}\ \int_{\Omega}Y\mathbb{1}_{A_{i}}d\mathbb{P}\nearrow\int_{\Omega}Y\mathbb{1}_{A}d\mathbb{P},$$ but by definition of $\mathcal{L}$, for each $i$, we have $$\int_{\Omega}X\mathbb{1}_{A_{i}}d\mathbb{P}=\int_{A_{i}}Xd\mathbb{P}=\int_{A_{i}}Yd\mathbb{P}=\int_{\Omega}Y\mathbb{1}_{A_{i}}d\mathbb{P},$$ so it follows that $$\int_{A}Xd\mathbb{P}=\int_{\Omega}X\mathbb{1}_{A}d\mathbb{P}=\int_{\Omega}Y\mathbb{1}_{A}d\mathbb{P}=\int_{A}Yd\mathbb{P}.$$
The problem here is that when I tried to extend this result to general case, we normally set $X:=X^{+}-X^{-}$ and $Y:=Y^{+}-Y^{-}$, but then it is not necessary that $\mathbb{E}(X^{+}\mathbb{1}_{A_{i}})=\mathbb{E}(Y^{+}\mathbb{1}_{A_{i}})$ and $\mathbb{E}(X^{-}\mathbb{1}_{A_{i}})=\mathbb{E}(Y^{-}\mathbb{1}_{A_{i}})$, so we cannot go back to the first case and conclude.
It is not necessarily true because the hypothesis is only about $X$ and $Y$, so we have $$\int_{A_{i}}X^{+}-X^{-}d\mathbb{P}=\int_{A_{i}}Y^{+}-Y^{-}d\mathbb{P},$$ from which we cannot conclude the positive parts being equal and the negative parts being equal.
What should I do? Thank you!
Edit 1:
Okay I figured it out, I asked a dumb question.... I will answer my own post.
Answer: Okay I figured it out, I asked a dumb question. I don't really need $X\mathbb{1}_{A{i}}\nearrow X\mathbb{1}_{A}$, I only need $X\mathbb{1}_{A{i}}\longrightarrow X\mathbb{1}_{A}$, and then we apply dominated convergence theorem.
Below is the updated proof of the last part:
Let $A_{i}\in \mathcal{L}$ be such that $A_{i}\nearrow A$. By hypothesis both $X$ and $Y$ are integrable. Since $A_{i}\nearrow A$, then we have $X\mathbb{1}_{A_{i}}\longrightarrow X\mathbb{1}_{A}$, and $|X\mathbb{1}_{A_{i}}|\leq |X|$, so it follows from dominated convergence theorem that $$\int_{A_{i}}Xd\mathbb{P}\longrightarrow\int_{A}Xd\mathbb{P},$$ similarly we can conclude that for $Y$, $$\int_{A_{i}}Yd\mathbb{P}\longrightarrow\int_{A}Yd\mathbb{P},$$ but by hypothesis we have $$\int_{A_{i}}Xd\mathbb{P}=\int_{A_{i}}Yd\mathbb{P}\ \text{for all}\ i,$$ and thus it follows that $$\int_{A}Xd\mathbb{P}=\int_{A}Yd\mathbb{P},$$ which implies $A\in\mathcal{L}$.
Therefore, $\mathcal{L}$ is a $\lambda-$system and the proof is then concluded.


Notation for the sample total variation

Question: Given a sample $x=(x_1, \ldots, x_n)$, what is your favorite notation for the sum of squares $\sum (x_i -\bar x)^2$ ? I like to keep $s^2_x$ for the sample variance, and I wonder whether $ss^2_x$ is a nice notation for the sum of squares.
Now if there are two paired samples $x$ and $y$, how to denote $\sum(x_i-\bar x)(y_i - \bar y)$ (and by the way how do we call it) ? If we use $ss^2_x$ for the total variation, should we use $ss_{xy}$ or $ss^2_{xy}$ ?
Actually I'm currently writing a doc about Deming regression and here is an example of an expression using the above notations :
$$\frac{ss^2_x ss^2_y - ss^4_{xy}}{ss^4_{xy}}. $$
Strange for the eyes, no ? Moreover the first time I typed this expression I did a mistake by confusing $ss_{xy}$ and $ss^2_{xy}$.
What do you think about $v^2_x$ ?
Comment: I have seen lots of horrible notation in this territory. Composite symbols such as $ss$ are difficult to read, but if you are determined to use them roman ss is preferable and most of the precedents seem to use upper case, e.g. SS. An analogue is that exp is fine as a composite, while $exp$ is classifiable as learner error.
Comment: I sympathise. But I wouldn't want to combine this kind of notational distinction with that sort of discussion. BTW, I would classify myself as a regression-type person who finds much ANOVA literature unreadable because of its barbarous notation, like reading old FORTRAN programs or literature about Soviet planning.
Comment: I'd add: best not to use such notation unless you use it a lot; best to use a distinctive font if you use it at all.
Comment: @NickCox Hello. Sometimes I use lower case / upper case to distinguish the observation and the random variable. That's why I'm not enthusiastic with regard to $SS$.
Comment: @NickCox :-) I understand. That's also why I've opened this post :)
Comment: Capital $S$ is often used (sometimes $S^2$), with various subscripts, as are variations on "SS". See [1](http://en.wikipedia.org/wiki/Least_squares#Solving_the_least_squares_problem) [2](http://en.wikipedia.org/wiki/Mean_squared_error#Examples) [3](http://en.wikipedia.org/wiki/Squared_deviations#Sample_variance) [4](http://en.wikipedia.org/wiki/Explained_sum_of_squares#Partitioning_in_the_general_OLS_model) [5](http://en.wikipedia.org/wiki/Analysis_of_variance#Effect_size). I'm not a fan of "SS", but it and $S$ - while breaking the 'capitals are r.v.s' convention - won't go away.
Answer: In the maths and stats degree I did, the sum of squares was represented by $S_{xx}$:
$$S_{xx} = \sum_{i=1}^n(x_i-\bar{x})^2$$
with an analogous formula for $S_{yy}$.
One nice aspect of this notation is that the sum of cross-products is denoted by $S_{xy}$:
$$S_{xy} = \sum_{i=1}^n(x_i-\bar{x})(y_i-\bar{y})$$
For the sample mean and variance, we used $\bar{x}$ and $s^2$. For the mean of a random variable $X$ or of a population, $E(X)$ or $\mu$, and the for variance $V(X)$ or $\sigma^2$.
Comment: I agree this is a good notation except for the usage of lower/upper to distinguish observation/random variable. But actually when writing statistics I never use the distinction with lower/upper, I prefer $x^{\text{obs}}$ and $x$ (though this could cause problem when there is already an exponent). And for the sample variance, do you use $s^2_x$ ?
Comment: Yes, notation can be pretty awkward. I've looked up my university notes and amended my answer. I think I've used something like $x^i$ for individual observations, and the vector $\mathbf{x}$ for a set of observations. Certainly, $s^2_x$ would be fine for the sample variance. To be honest, so long as you define what you're doing and it's clear, any notation is going to be ok. (Except that using $\Sigma$ for a covariance matrix still confuses the heck out of me!)
Comment: Where are you guys from? I can't remember the last time I saw "obs" and "_i_" as superscripts; I always use them as subscripts.
Comment: @WaldirLeoncio I'm from the UK. I don't know whether notation is a national thing however, and moreover I'm student of statistics and a professional software developer who uses statistics, rather than a professional statistician.

Notation for the sample total variation


I found this code in the head of a website, i've never seen something like this before, does anyone know what it does?

Question: Could someone explain to me what purpose this code serves? I found it in the source code  in the  of the page for this site http://blog.brianbeck.com/tagged/jquery

    <meta name="color:Top Border" content="#333" />
    <meta name="color:Site Title" content="#D53C2E" />
    <meta name="color:Link" content="#333" />
    <meta name="color:Link Hover" content="#D53C2E" />
    <meta name="color:Button" content="#ccc" />
    <meta name="color:Button Hover" content="#D53C2E" />
    <meta name="color:Post Title" content="#333" />
    <meta name="color:Post Title Link" content="#D53C2E" />

    <meta name="text:Website Address" content="" />
    <meta name="text:Blog Address" content="" />
    <meta name="text:Flickr Username" content="" />
    <meta name="text:Twitter Username" content="" />
    <meta name="text:Facebook Username" content="" />
    <meta name="text:YouTube Username" content="" />
    <meta name="text:Lastfm Username" content="" />
    <meta name="text:Delicious Username" content="" />
    <meta name="text:FriendFeed Username" content="" />

Answer: It is something unique to tumblr
"Enabling Custom Colors
By including the special meta-color tags in your theme, users you share your theme with will be able to easily tweak those colors using their "Customize Theme" screen. "
The meta tags are essentially used as variables which users can change. They are referenced by the Javascript code on the page to make them automagically work.
Comment: Maybe this will help: http://chrismeller.com/2008/06/meta-name-color#comment-1114
Comment: Awesome, you guys are the shizzle... or whatever kids say these days.