Derivation of path equation for automobile steering

Question: so I have this equation related to the picture. My problem is, I can't see how the equation geometrically/mathematically makes any sense. The equation is true and returns the correct result when being used.

$R_{sp,3}$ is the radius that approximately goes where I have marked it with red on the picture. $d_{ki}$ and $b_v$ are both distances.
I don't know if you need the equations for radius $R_{ki,1}$ og $R_{ki,2}$ but here they are ($V_p$ is just a number related to the specific case):
$$R_{ki,1} = \frac{1}{\dfrac{1}{R_{sp,3}}+\dfrac{108}{V_p^2}} \quad\text{and}\quad R_{ki,2} = \frac{1}{\dfrac{1}{R_{sp,3}\pm b_v}+\dfrac{108}{V_p^2}}$$
The equation I need help with is:
$$d_{ki} = R_{sp,3} + \sqrt{R_{sp,3} \cdot (R_{sp,3} \pm b_v)}$$
Can anyone tell me how that equation is derived?
Comment: Your question is so strange... You mention "radii", I don't think it is radii of curvature but **arcs of curves** ? Confirm it. Now what is the variable (I mean : what are the units on the abscissa axis ?) What is the context: Electrical Engineering, Mechanics ... etc.
Comment: Perhaps ypu may get help better from https://engineering.stackexchange.com/ chek the "civil-engineering" tag
Comment: I can't understand the numbers you gave as example. I'm starting to think that this is not a plan view, but a section skecht about superelevation (aka "cant") transition. Please, post the link to where you got the info, no matter which language it uses.
Comment: Hi, sorry for the confusion. The context is road engineering, where these equations help calculate the radius of the connection curves and different distances used to design a left turn lane and a width extension of the road. The equation was made by a clever engineer 50 years ago, and nobody (that i know of) has figured out how he made that equation since.
Alle the units are in meters.
R_sp,3 defines the radius of the connection curve between R_sp,1 and R_ki,1.
Comment: Thanks for your answer. I have been working some time ago with curves connecting other curves ("trochoids", less simple than hyperbolas). I am still puzzled: as you will see: I have changed my graphics using the data you have provided but I still haven't connected curves... Can you confirm that the dotted line is the axis of abscissas for variable $x=R_{sp3}$ ? Can you confirm that the equations are equations of **curves** and not **radii of curvature** ?
Comment: I am not sure if the dotted line represents the axis of abscissas for R_sp,3. The dotted line in the picture just represents the centerline of the road, when there is no width extension happening. The equations must be equations of curves. They are just part of a circle that is a tangent to the connecting parts of a circle.
Comment: So, these equations should be radii of curvature (for example: "radius of curvature = constant" is an arc of circle, "radius of curvature constantly decreasing" is a piece of spiral, etc. The problem is "where are the centers of curvature" ?
Comment: Then the equations are radii of curvature. The centers is something i have a problem with too, because it is not defined anywhere. To draw this in CAD-programs, you do not need centers of curvature. All i know, is that all the arcs are a tangent to the connecting arc
Comment: Hi again, have you given up?
Comment: No, the reason is plainly that I was unable to retrieve your question( I hadn't kept any link to it).
Comment: Okay, thank you. Is there any more information you need?
Comment: I'm unclear what is happening here, and I am unable to replicate the situation based on the information given. Are you saying that the various sub-paths are *circular* arcs with the specified radii ($R_{sp,1}$, $R_{sp,3}$, $R_{ki,1}$, $R_{ki,2}$)? Or are they more-arbitrary curves (cubic splines?) that attain those radii-of-curvature somewhere (*where*?)? Could you provide the specific numerical values used to make the figure (or provide an alternative figure)? Can you provide an online reference for these equations?
Comment: Can you give examples of $d_{ki}$ and $R_{sp,3}$ ? It seems to me that $d$ should be a few meters, while $R$ would be much bigger. But then the equation can't be good ($d$ is similar to $2R$)
Comment: Blue: Yes, they are all cirbular arcs with the specified radii. I can link you to an online reference for the equations, but all the text is in danish (So not sure how that would help - and it doesn't specify the equations further).
Ripi2: Examples of numbers for d_ki and R_sp,3 could be d_ki = 1,75 m and R_sp,3 = 533 m. R_sp,3 can vary alot depending on the specific case, while d_ki almost always will be in between 1,5-2 m.
Comment: @Math9876: The image pretty clearly suggests (and your commented sample values confirm) that $d_{ki}<R_{sp,3}$. But the target formula indicates $d_{ki}$ is *more* than $R_{sp_3}$. Should(n't) there be a $-$ before the square root?
Comment: @Math9876: Also, what exactly *is* $d_{ki}$? Is it the "vertical" distance from the inflection point to the "horizontal"? Is it the "vertical" distance from the inflection point to the red arc? Is it the distance from the inflection point "perpendicular" to the red arc? Something else? If $d_{ki}$ is not  actually "vertical", then do the $L_{ki}/2$ distances measure the horizontal distances between the landmark points on the roadway *upper* end of $d_{ki}$ (ie, the inflection point)? or the *lower* end?
Comment: @Math9876: Here's where I am with this: I setup some parameters and managed to get a path of circular arcs with appropriate tangencies at the three $L/2$-related landmarks. The dependencies leave me with two free parameters ... which is consistent with the apparent situation that the final "S" curve is determined by the two parameters, $b_v$ and $L_{ki}$. Now, the $V_p$ relations reduce the free parameters to just one, but I can't find an interpretation of $d_{ki}$ (see above comment) that causes the target relation to fall out. There seems to be a piece of the puzzle that I'm just not seeing.
Comment: @Blue: I don't know if there is missing a -.
d_ki is the "vertical" distance from the inflection point to the red arc, where L_ki/2 is. That is my problem too. There are no other mentioning of d_ki except from the equation I can't figure out. To make the equations for R_ki,1 and R_ki,2, it is assumed that d_ki = b_v/2. But that is not 100% accurate.
Comment: @Math9876: "vertical to red arc" is the worst option. :) Geometrically, "perpendicular to red arc" is best, as then the target is a [power of a pt](https://en.wikipedia.org/wiki/Power_of_a_point) relation involving the center of the $R_{sp,3}$ arc & a family of circles. For $R_{sp,3}$ very large compared to $d_{ki}$, "vertical to red arc" is *approximately* "perpendicular to red arc", but this may not help. ... In any case, if the $V_p$ eqns are inaccurate, and you aren't sure about signs, then how strongly should we believe when you say the target eqn is "true" and gives "the correct result"?
Comment: @Blue: Sorry, i don't know that much about the original equation and the figure - only what i can see. What i mean when i say the equation is "true", is that it gives a correct result that is possible to draw in a CAD-program. I honestly don't know how the equation is made, that is why i am asking. I think you can assume pretty much anything, as long as it fits with the figure.
Comment: @Blue: But bear in mind, that this equation was made over 50 years ago. So i don't know, if the engineer used different helping tools or where thinking in a different way?
Comment: @Math9876: I'm now of a mind that this isn't a *mathematics* question, but an *engineering* one. (Maybe even a "mind-reading" one! :) In [my attempt at an answer](https://math.stackexchange.com/a/4170558/409), I can only get within 3 or 4 degrees of freedom of the formula, with no intuition about what additional assumptions might help. So, I echo Ripi2's comment in suggesting that you might want to pose this to the folks at [Engineering.SE](https://engineering.stackexchange.com/). Someone there may be more familiar with the nuances of the formula and any unspoken assumptions or approximations.
Comment: @Ripi2: It is just a plan view. But here is the link: https://vejregler.dk/h/7e0fba84-06dd-483b-898a-c7b3e3affaa1/vd20170120?showExact=true - if you want it as a PDF, then download the danish version, do not download the english version as it is not updated and some of the chapters are missing. The equations are in chapter "2.3.2 Begrænsningslinjen mod venstresvingssporet" under section "Kilestrækning".
Answer: Here's a reimagining of OP's figure:

(In OP's figure, the "red arc" extends from $B$ to $G$. The portion to the left of $C$ is irrelevant here; so, for the purposes of this discussion, the "red arc" simply connects $C$ to $G$.)
To simplify notation, define
$$r := R_{sp,3} \qquad s := R_{ki,1} \qquad t := R_{ki,2} \qquad\quad c := \frac12b_v \quad m := \frac14 L_{ki} \qquad\qquad d := d_{ki} $$
Also, let $R$, $S$, $T$ be the centers of the arcs of respective radii $r$, $s$, $t$. Let the measures of the central angles be $2\theta$ at $R$, $2\phi$ at $T$, and $2\theta+2\phi$ at $S$.
Taking OP's $L_{ki}/2$ measurements to indicate horizontal distances between landmark points on the roadway ($C$, $D$, $E$ in the figure), we readily determine
$$\begin{align}
2m &= s(\sin2\theta+\sin2\phi) \tag1 \\
2m &= t \sin2\phi \tag2
\end{align}$$
Vertically, we find
$$\begin{align}
2c &= r - (r-s)\cos2\theta-(s+t)\cos2\phi+t \\
\to\qquad c &= r\sin^2\theta+t\sin^2\phi + s\sin(\phi+\theta)\sin(\phi-\theta)
\tag3
\end{align}$$
These equations impose dependencies that reduce our collection of parameters from seven $(r, s, t; c, m; \theta, \phi)$ to some choice of four. For instance, we can solve for the radii in terms of the angles and horizontal/vertical offsets:
$$\begin{align}
r &= \sec\phi\csc^2\theta\sec(\phi-\theta) (
c \cos\phi\cos(\phi-\theta) - m\sin(2\phi-\theta)) \\
s &= m \csc(\phi+\theta)\sec(\phi-\theta)\\
t &= m \csc\phi\sec\phi
\end{align} \tag4$$
From here ... It's not quite clear how to proceed.
OP has additional relations we can write as
$$\frac1s - \frac1r \;= \frac1v =\; \frac1t - \frac1{r+2c} \tag5$$
where $v := V_p^2/108$ (and where we deduce that OP's "$\pm$" must be "$+$", since $t$ must be larger than $s$, as the $s$-arc makes a sharper turn than the $t$-arc). However, OP has commented "To make the equations for [$s$ and $t$], it is assumed that [$d=c$]. But that is not $100\%$ accurate." But applying $d=c$ to the target relation ($(\star)$ below) implies $d=c=0$, so the assumption is inconsistent with the target, which suggests that there are other layers of approximation going on. So, $(5)$ doesn't seem directly applicable here. Even if it were, by introducing an additional parameter $v$, the two equations in $(5)$ only reduce the number of free parameters to $3$; and it's not clear (to me) which might be subject to approximation.
As for the target relation itself, OP's rendering of it is inconsistent with the figure given, as it makes $d_{ki}$ larger than $R_{sp,3}$, which is one of the largest radii. Presumably, the relation should be a subtraction. Moreover, to ensure $d_{ki}$ is positive, we must take the "$\pm$" to be "$-$". Thus, it would appear that the target "should be"
$$d = r - \sqrt{r(r-2c)} \qquad\to\qquad (d-r)^2 = r(r-2c) \tag{$\star$}$$
The right-hand version is tantalizing in that it suggests a power of a point situation, in which $D$ lives on a circle orthogonal to a family of circles whose line-of-centers is the perpendicular bisector of the $2c$ segment:

In this situation, the $d$ segment (OP's $d_{ki}$) is an extension of $\overline{RD}$ that meets $\bigcirc R$, so that $(\star)$ asserts $|RD|^2=r(r-2c)$. One can show (I used coordinates) that this is equivalent to the condition
$$c r = 2 (r - s) s \sin^2(\phi+\theta) =2m(r-s)\tan(\phi+\theta) \tag6$$
where $r$ and $s$ can be rewritten via $(4)$, if desired.
Unfortunately, OP has commented that the $d$ segment is not orthogonal to the red arc (ie, is not an extension of $\overline{RD}$), but rather the vertical distance from $D$ to that arc. The messiness of the counterpart of $(6)$ for that situation exceeds my patience to TeX-ify it, so for now I'll leave its derivation as an exercise to the reader.
I'm inclined to wonder if maybe part of the engineer's genius here is to approximate the vertical distance by the orthogonal-to-arc one, leveraging the power of a point formula to simplify calculations. After all, OP has commented that $r$ tends to be on the order of $500$m, while $d$ is in the neighborhood of $1.5$m; at those scales, the computational error from the different interpretations of $d$ may be "acceptable".
Not that it really matters. I'm at a loss for how to arrive at even the nicer relation $(6)$. (Fiddling in GeoGebra shows that $D$ is not on the green circle for arbitrary values of the free parameters; more conditions are needed to lock this in.) Nor do I have the intuition to see why it would be an appropriate approximator. (I guess that's why I'm a mathematician, not an engineer.)
Without more information or insight, this is about as far as I can go.

Some additional thoughts ...
The flip side of my saying $(\star)$ implies "$c=d=0$" when $c=d$ is that $(\star)$ could alternatively imply $r=\infty$ when $c=d$. That is, the $c=d$ condition behind the approximations in $(5)$ effectively assumes "$r$ is large" (compared to other parameters). I suspect that this assumption also drives the target relation $(\star)$ ... which is to say: I don't believe $(\star)$ is a derivable geometric fact, but a contrived computational convenience.
Even so, let me cover one more bit of geometry:

Let the horizontal from my point $E$ and the vertical from $D$ meet at $D'$. Then
$$|D'D|=2m\tan\phi \tag{A.1}$$
Now, for the contrivance(s). Beware of vigorously waving hands!
For "large" $r$ ...

The red arc is "flat", so that path $CDE$ becomes a "symmetric" $S$-curve that places $D$ at the vertical half-way mark of the $2c$ segment. This gives us
$$c \approx |D'D| = 2m\tan\phi \tag{A.2}$$
The central angle at $R$ is "small", so that $\theta\approx 0$ and thus
$$c\approx 2m\tan(\phi+\theta) \tag{A.3}$$
Now, if $r$ is large compared to $s$ then we can write $1-\dfrac{s}{r}\approx 1$, so that
$$c\approx 2m\left(1-\frac{s}{r}\right)\tan(\phi+\theta) \tag{A.4}$$
But this is simply (though only approximately) $(6)$, which is equivalent to the target $(\star)$; I'll write this as
$$|RD|^2 = r(r-2c) \tag{A.5}$$
Finally, the target $d$ segment —defined by OP as the vertical distance from $D$ to the "red arc"— is, as described before, approximated by the extension of $|DR|$ that meets that arc, so that $|DR|\approx r-d$, giving
$$(r-d)^2 \approx r(r-2c) \tag{A.6}$$
as (approximately) desired.

This loosey-goosey approach almost-certainly doesn't pass muster with numerical analysts (of which I am not one), but it might at least serve as a scaffolding for a rigorous treatment.
Comment: Thank you so much! I will accept this as an answer, as i think this is the closest we will get to a derivation of the equation :-)
Answer: The way I understand it at present time.
The radius of curvature of a curve with cartesian equation $y=f(x)$ is known to be:
$$R(x)=\dfrac{(1+f'(x)^2)^{3/2}}{f''(x)}\tag{1}$$
In our case where
$$f(x)=\sqrt{x(x+b)}\tag{2}$$
(whose curves are hyperbolas), the radius of curvature is
$$R_b(x)=\frac{1}{2b^2}\left(8x^2+8bx+b^2\right)^{3/2}\tag{3}$$
The corresponding curves grow like $kx^3$ and (almost) cover the first quadrant.

Fig. 1: (abscissas: distances ; ordinates: radii of curvature) Determination of an adequate value of $b$ for the connecting arc, being known the initial (resp. final) radius of curvature $R'=100$ (resp. $R''=300$). Among all candidates (the arc of curves situated between the two red lines), only one will correspond to a given length ; for example if we desire a curve length $2$ meters (to be read on the $x$ axis), we should take $b \approx 4$ (probably unrealistic values, I agree ; our aim here is understanding/agreeing on the principle).
Otherwise said, Fig. 1 is a kind of nomogram allowing, through the determination of a certain curve to get the good value of parameter $b$, and then use equation (2).
What do you think about all this interpretation ?
Comment: The ± sign in x±b depends on whether the curve of R_sp,3 and R_ki,1 turns the same direction or not (the s-shape).
Which again depends of the road, if it's in a left or right turning curve. But it's different from each time.
Comment: A realistic number for your constants a, b and d would be: a = 0,016875, b = 3,5 and d = 3,5. Both b and d are = b_v
Comment: To be honest, i am not a hardcore mathematician, but it looks better and better. I am still not sure, that it is the radius of curvature of a curve, since i still believe it is just a circular curve.
Comment: I have read your comment. Besides, I have somewhat modified the legend of the figure in order to eliminate any ambiguity.
Answer: The path model is clear so far.. ok for me to start understanding/interpreting. There could be typos with given labels:
At the end of interpretation, we should be able to state what the distances and acceleration are given and resulting equation is for path going above center line.
Basically, $d_k$ depends on R which again depends on acceleration due to $V_p$
$R_{k1}, R_{k2}$ are radii or $y$ coordinate on either side of designed inflection point at center.In other words with this point, an odd function describes the curve.
Curve $R_{sp,1}$ transitions to $R_{sp,3}$ through a point of inflection. Likewise $R_{sp,2}$ transitions to $R_{sp,4}$ through a point of inflection. The indicated vertical line is not labelled.
$L_{bu}/2$ is length designed between inflection and horizontal tangent point of merge completion where the vehicle resumes pure x- velocity without acceleration.
At distance $dki$ there is an inflection point maneuver designed on the center line of the road on x-axis.
To find $d_k$ following quadratic equation has been solved :
The first and second roots  are
$$d_{ki1} = R_{sp,3} + \sqrt{R_{sp,3} \cdot (R_{sp,3} \pm b_v)};\;d_{ki2} = R_{sp,3} - \sqrt{R_{sp,3} \cdot (R_{sp,3} \pm b_v)} \tag 1$$
we can find sum and product and form quadratic equation that contains both these roots $d_{ki}$:
$$ d_k^2 - 2 R_{sp,3} \; d_k \mp R_{sp,3}= 0 \tag 2 $$
Radial correction/compensation is due to influence of a lateral acceleration, follows a parallelly connected electric resistances model or pattern ( effective less than least radius etc). 108 could be some permissible limit acceleration value. Radius $ R_{ki,1} $ has no influence of acceleration. $R_{ki,2}$ takes acceleration into account.
$$ \dfrac{1}{R_{ki,2}}=\dfrac{1}{R_{sp,3 \pm b_v}}+ \dfrac{108}{V_p^2} \tag3 $$
We can continue through comments if you wish.
Comment: 108 is a factor that includes lateral acceleration and units conversion.
Comment: Can you explain where that quadratic comes from? $d_{ki}$ is not a radius, but a distance (or $y$ coordinate in the sketch). And notice that while tangent-continuity is granted between two circles, curvature (radius sign in roads parlance)  is not (concave vs convex)
Comment: @Ripi: Thanks, noted... you meant curves, there are no circles here.
Comment: In roads horizontal design the only curves considered are straights, circumferences and clothoids (Euler's spiral). Adjacent elements have the same tangent. If using clothoids then second derivatives (curvature) are same too.
Comment: Thanks for your answer! I don't think you need to think all the acceleration into the equations. It should just be purely geometry (but i am not a mathematician, so maybe it does matter?).
And Ripi2 is right, there are only straights, circumferences and clothoids. (Even though there are only circular arcs and straight lines in this specific case)


PHP or Linux Shell: Fastest way to reduce a text file that has more than 10 lines to only have the last 10 lines

Question: I need to know the fastest way possible, in PHP or Linux Command Shell, to reduce a text file that has more than 10 lines to only have the last 10 lines. I want to use 10 lines for this example.
Thanks :)
Comment: @MikeBrant no, buddy, that won't work as it zero-bytes the file
Comment: Have you tried to measure it with `time` on your shell?
Comment: Fastest way in terms of code length/complexity? Fastest runtime?
Comment: `tail [file]`. fastest thing around
Comment: I forgot to mention that I need that reduction to occure in that SAME file. I don't want to make another file.
Comment: the smaller the code the better :)
Comment: If you want to do it in the same file, then just use tail -n 10 path/to/file > path/to/file
Comment: @webdev Sorry, but you will not be able to do this reduction in the same file. Even if you were to use sed, you would still be operating on an internally created temp file. See http://backreference.org/2011/01/29/in-place-editing-of-files
Answer: to reduce a file to it's last then lines do
tail -n 10 /path/to/file > /path/to/file.tmp && rm /path/to/file && mv /path/to/file.tmp /path/to/file

explanation:
tail outputs the last n lines of a file.
> writes it to a file.
&& is for chaining commands.
rm removes a file.
mv renames a file (or moves to a different location).
Comment: well he wanted to use 10 lines as an example. if we don't tell him he won't be able to use a different number ;)
Comment: i need a fast solution but at the same time i'd like to know a little of whats going on behind the code andrew. im still new to linux admin, so ty so much for adding that -n 10 andreas :)
Comment: You don't need the `-n 10` - tail defaults to 10 lines unless you say otherwise.
Comment: Yeah, but since he's after a fast solution, this way you need to type 6 characters fewer.
Comment: @Radix - was that addressed to me?
Comment: okay big shot, do it in one file, as the OP has now commented :)
Answer: If you really really don't want to generate a new file, then you might try
echo "$(tail -n 10 path/to/file)" >path/to/file

The $() works just like backticks (it evaluates whatever's inside and equates to the output of said command), except it works better in strings.  On my Linux machine, the string gets evaluated (meaning tail is run) before the output goes to the file (which is what causes the problems with tail file >file; the file gets opened -- and truncated -- as soon as the command starts).  I'm thinking it's because echo is a builtin, but don't quote me on that.
EDIT:  Actually, i took a closer look.  It's because the tail is in a string.  There are several phases -- here, the ones we care about are interpolation, redirection and execution -- that always happen in a certain defined order (namely, the order just given).   And in the process of interpolating the string, Bash has to run the command within it.  Before any redirection is done.  With a straight tail file >file, the tail program is what actually generates the output, so no output happens til the command is executed -- but the file's already been opened for writing (due to redirection) right before that, and opening that file for writing truncates it.)
phase          tail file >file                   echo "$(tail file)" >file
-------------+---------------------------------+-------------------------------
interpolate   nothing                           run "tail file"; get output (+)
redirect      open file for write; set stdout   open file for write; set stdout
execute       run "tail file" (*)               run 'echo "line\nline\n..."'

(*): Note, since file was already opened for writing by this point, it's now truncated.  So tail reads 0 bytes.
(+): Since file was read in before the redirection opened it for writing, tail manages to see all the data.
Even though the behavior is obviously defined and predictable, this still feels kinda hacky to me.  It relies on bash arcana, and may very well cause memory issues if you try to get too many or too long lines.  (I tested with ~500 char lines, and didn't see any issues.  But haven't tested with more than 10 lines yet.)  It'd probably be better to use a temp file unless you need to preserve the same inode number for some odd reason.
Comment: where is the data of the last 10 lines stored O_o? is it in memory? Whichever method uses the least system resource. So if making a tmp file and then overwriting the original requires the least system work then I'd go with that. I want simplicity but also performance ;)
Comment: `"$(tail...)"` gets interpolated and becomes a string containing the entire output of the `tail` command.  Which, yeah.  Gets stored in memory temporarily.  It'll take more memory, but should be a tiny amount faster as it only has to mess with the file system once for input and once for output (rather than shuffling filenames around).  Still, a temp file feels more "safe" to me -- though unlike temp files, it's next to impossible for two strings in different processes to collide.  :)
Comment: that's a neat hack ;)  and thanks for warning me about the mem issue. It will be handy to retain the same file since I know when the file was made. Is there a way to make the original blank and copy the contents of the temp file onto the original file?
Comment: If you use a temp file, you could say `cat path/to/temp/file >path/to/file` to overwrite the original, and then `rm path/to/temp/file`.  But then you get all the downsides of temp files *plus* all the downsides of overwriting the original file.
Answer: tail -n 10 yourfile > yourfile.tmp && mv yourfile.tmp yourfile

Edited to give exclusively the last 10 lines of a file with potentially greater than 10 lines.
Comment: well, mine and Mike Brant's certainly did solve the issue as ORIGINALLY stated.
Comment: this won't work as well, the file will be empty after that ^^
Comment: well you just copied my answer ;)
Comment: uh, no I did not -- there really is only one solution if using tmp file
Comment: sure this is why your answer twice changed to exactly what i wrote ;)
Answer: In linux you could do
tail -n 10 path/to/file > path/to/output/file

This will take the last ten lines (# of lines specified by value after -n) of the file and then append them to another file.
If you want to overwrite the existing file with only its last ten lines then do
tail -n 10 path/to/file > path/to/output/file && mv path/to/output/file path/to/file

Note the first example outputs to a different file while the second overwrite the existing file.
You could also do this in PHP by using exec() with the shell command inside, or the more laborious way of reading the entire file into an array with file() and then taking the last ten elements of the array and adding them back into the file with fwrite() or similar.
Comment: @cHap Isn't that what he was asking to do?  Take the last ten lines and then add them back into the same file? Well I guess not since the question has now been edited.  Oh well.
Comment: @GabiPurcaru I didn't -1  but did just +1 it to even out for you :)
Comment: If you append the last 10 lines to the file, then every time you run this command the file will have up to 10 *extra* lines, rather than being trimmed down.  You want to use `>` rather than `>>`.
Comment: your answer `tail -n 10 path/to/file >> path/to/file` duplicates the last ten lines of the file ;)
Comment: No.  He's trying to make the file *only* contain the last 10 lines.
Comment: @cHao  His original question was stated much different and has since been edited. I have edited my answer accordingly.
Comment: Even with the original question...think it over to yourself.  Realize that when interpreted as the question you answered, it would be pretty useless.  Anyway, now that it's fixed, +1 :)
Comment: @cHao Sure it may have been useless in many cases, but what if he wanted to build a sample data file by quickly duplicating data in the file on top of itself or something similar.  The question was specifically stated as asking how to take ten lines from the end of a file and append them back to the SAME file. Note all the other answers above where everyone interpreted the question the exact same way.
Comment: @MikeBrant: No, he was asking how to take the last 10 lines and *write* them to the same file.  Not append.  If you want to give an overly literal answer, at least go all the way with it.
Comment: @MikeBrant can you believe the degree of attention we are giving this little question? :)
Comment: cHao is right in that I didn't need to explain since i already stated "Reduce a text file" NOT "Append a  text file" haha ;). I just edited it to state the SAME file only because all the answers were about appending and making new file haha. But I understand it's a habit to make new files as linux admins so I apologize if I wasn't clear enough ;)


multiple virtual hosts on EC2 ubuntu - cannot access second host, always accesses default

Question: I have been searching and reconfiguring for 6 days now and have lost several clumps of hair.
PROBLEM:
I want 2+ virtual hosts on my ubuntu server (1 ip)
BUT - Only the first "alphabetically" listed sites-enabled shows.
000-default
www.domain1.com
www.domain2.com
Individually they all work (if i a2dissite for each leaving 1)
CONFIG:
 - UBUNTU 10.10 Server
 - EC2 instance (dont shoot me for this part - hoping this isnt the issue!)
 - APACHE 2.2.16
 - DNS my.domain.com
 - to my public ec2 dns (this works)
Virtual Hosts: # default
<VirtualHost *:80>
        DocumentRoot "/home/www/"
        <Directory />
        Options FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Satisfy all
        </Directory>
        <Directory /home/www>
        Options Indexes Multiviews FollowSymLinks
        AllowOverride all
        Order allow,deny
        Allow from all
        </Directory>
        LogLevel debug
        ErrorLog /home/www/logs/error.log
        CustomLog /home/www/logs/access.log "combined"
</VirtualHost>

Virtual Host 1 - domain1
<VirtualHost *:80>
        ServerAdmin email@example.com
        ServerName domain1.com
        ServerAlias domain1.com www.domain1.com
        # Indexes + Directory Root.
        DirectoryIndex index.php
        DocumentRoot "/home/www/www.domain1.com/"
        # Directory directive
        <Directory "/home/www/www.domain1.com">
                Options Indexes Multiviews FollowSymLinks
                AllowOverride none
                Order allow,deny
                Allow from all
        </Directory>
        # CGI Directory
        ScriptAlias /cgi-bin/ /home/www/www.domain1.com/cgi-bin
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>
        # Logfiles
        LogLevel debug
        ErrorLog "/home/www/www.domain1.com/logs/error.log"
        CustomLog "/home/www/www.domain1.com/logs/access.log" combined
</VirtualHost>

I also have the above for domain2 in another document root with a different domain name
sym links are in place. apache2ctl -St shows the following -
VirtualHost configuration:
*:80       is a NameVirtualHost
         default server  (/etc/apache2/sites-enabled/000-default:4)
         port 80 namevhost  (/etc/apache2/sites-enabled/000-default:4)
         port 80 namevhost domain1.com (/etc/apache2/sites-enabled/www.domain1.com:4)
         port 80 namevhost www.domain2.com (/etc/apache2/sites-enabled/www.domain2.com:4)
Syntax OK

My ports.conf:
NameVirtualHost *:80
Listen 80

<IfModule mod_ssl.c>
    NameVirtualHost *.443
    Listen 443
</IfModule>

<IfModule mod_gnutls.c>
    Listen 443
</IfModule>

No changes from default to my apache2.conf and httpd.conf is empty.
I have tried the following (and about a hundred others):

http://ubuntu-tutorials.com/2008/01/09/setting-up-name-based-virtual-hosting/
-http://httpd.apache.org/docs/2.2/rewrite/vhosts.html"]http://httpd.apache.org/docs/2.2/rewrite/vhosts.html
http://ubuntuforums.org/archive/index.php/t-1466665.html"]http://ubuntuforums.org/archive/index.php/t-1466665.html
http://flurdy.com/docs/ec2/basics/index.html"]http://flurdy.com/docs/ec2/basics/index.html
http://www.webmasterworld.com/apache/3282118.htm"]http://www.webmasterworld.com/apache/3282118.htm

It seems I have tried everything that everyone else is having issues with but nothing seems to fix mine.
Possibilities:

EC2
Permissions on the files - I changed everything to the apache2 user "www-data" - no dice.
I am a dope...lets hope its that and one of you kind people point me to my issue. :)
Answer: Speaking about virtualhosts, we had the same problem for a project of ours. The Apache's "ServerName" directive isn't optional, it's mandatory to use. Apache has no such thing as a "default" virtualhost to load when it has no ServerName match. And if virtualhost servernames overlap, well, that's just going to give you problems. In the end in those cases Apache just decides what to do depending on its configuration files loading order. And if you try to contact the server through an unspecified address (ip address, or something not mapped on the virtualhost) it will load the first virtualhost he got a configuration loaded for that address. The correct way to do it all is to have virtualhosts detailing all of the possible servernames in ServerName and ServerAlias directive.
See name based virtual hosting on apache, and consider carefully the evidenced note "Main host goes away".
Answer: Is it possible that your ports.conf is failing due to the period used instead of colon?
<IfModule mod_ssl.c>
    NameVirtualHost *.443
    Listen 443
</IfModule>

Note the **.443* should be **:443* above.
Comment: Also, put a "ServerName" directive in the default host. It will still be the default as it is the first one loaded.
Comment: what server name should I use?
Comment: Fixed the : . issue, didnt fix anything tho. Cheers anyway.
Comment: @FrandsHansen I'm interested to know why you suggest putting ServerName in the default host? It might help with debugging but I would have thought setting it in the virtuals would be fine.
Answer: Just guessing.
Looking on result of apache2ctl -St, you have difference in output :
port 80 namevhost domain1.com
port 80 namevhost www.domain2.com

Maybe you should also set domain2.com as ServerName (without "www" prefix)
Comment: nope, fixed that piece but no dice. Thanks for taking the time anyway.
Comment: Looks like it might be DNS related.
Trying a fix now...lets see what happens when the caches clear themselves!


How can i get local notifications plugin to work on my cordova app?

Question: i'm using cordova-plugin-local-notification and I want the notification to trigger on setReminder() function. Please how can I do this
var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
    this.receivedEvent('deviceready');
    document.getElementById("setreminder").addEventListener("click", setReminder);
    cordova.plugins.notification.local.registerPermission(function (granted) {
    });
    },

    // Update DOM on a Received Event
    receivedEvent: function(id) {
    StatusBar.overlaysWebView(false);
    StatusBar.backgroundColorByHexString('#4281F5');
    StatusBar.styleDefault();
    document.getElementById("setreminder").addEventListener("click", setReminder);
                   var sound = "js/when.mp3";
                   var date = new Date();
                function setReminder() {
                    cordova.plugins.notification.local.schedule({
                    id: 1,
                    title: "IKD-FAD",
                    message: "Your Bus Is Almost Here!",
                    at: date,
                    sound: sound,
                    icon: "js/citybus.png"
                });
                }
    }
};

app.initialize();

Answer: document.addEventListener("deviceready", ondeviceready, false);
        function ondeviceready() {

        }
function setReminder(btn) {
            navigator.vibrate(1000);
            cordova.plugins.notification.local.schedule({
            id: 1,
            title: btn.dataset.name,
            text: "Your Bus Will Be At The Station In " + changedTime + " Minutes",
            trigger: { in: btn.dataset.diff, unit: 'minute' },
            sound: "file://js/when.mp3",
            icon: "file://img/mmArtboard.png"
            });

This fixed it for me


Python - Incluir caractere em palavras importadas do excel

Question: Como faço para incluir "-" um traço após a terceira letra das placas,"na coluna Placa". Isso nas placas existentes e também toda vez que outra placa for inserida na lista em nova linha (Coluna "Placa")? A
EXEMPLO: NKK-89346
import pandas as pd
xp = pd.read_excel("G:/pythonProject/estudos.py/carros.xlsx")

print(xp['Placa'])

Resultado:
0    NK462519
1    NKN00699
2    NKL48559
3    NKK89346

Answer: A propriedade pandas.Series.str oferece métodos vetorizados para trabalho com strings.
Entre esses método encontra-se pandas.Series.str.slice_replace() que substitui uma fatia posicional de uma string por outro valor.
O método possui três parâmetros:

start int, opcional: Posição de índice esquerda a ser usada para a fatia. Se não for especificado a fatia é ilimitada à esquerda, ou seja, fatia desde o início da string.
stop int, opcional: Posição de índice direita a ser usada para a fatia. Se não for especificado a fatia é ilimitada à direita, ou seja, fatia até o final da string.
repl str, opcional: String para substituição. Se não for especificado a região fatiada será substituída por uma string vazia.

Para para incluir um traço após a terceira letra basta estabelecer que a fatia a ser substituída inicia e termina no índice 3:
import pandas as pd

s = pd.Series(["NK462519","NKN00699","NKL48559","NKK89346"])

print(s.str.slice_replace(3,3,"-"))
#0    NK4-62519
#1    NKN-00699
#2    NKL-48559
#3    NKK-89346
#dtype: object

Aplicado no seu exemplo:
import pandas as pd
xp = pd.read_excel("G:/pythonProject/estudos.py/carros.xlsx")

xp['Placa'] = xp['Placa'].str.slice_replace(3,3,"-")

print(xp['Placa'])

Como já disse uma vez em https://pt.stackoverflow.com/a/539045/137387

EVITE USAR DATAFRAME.APPLY() ou SERIES.APPLY()
Como sugestão essa palestra sobre ganho de performance com
vetorização em Pandas e Numpy
O fato é que os métodos DataFrame.apply() e
pandas.Series.apply() operam por loops e portanto são
inerentemente lentos.    O uso do método Series.apply() é lento pois
o Pandas não faz julgamentos sobre a natureza da função usada e que
portanto aplica iterativamente a função usada a série conforme
necessário.
Embora iterações forneçam uma utilidade maravilhosa, cada iteração
sobre um elemento é essencialmente uma única etapa na rota por todos
os elementos da coleção. Este processamento passo a passo é útil
quando a ordem da operação é estritamente importante.
O processamento vetorizado, em contraste, pode ser aplicado quando a
ordem de processamento não importa. Métodos NumPy e Pandas permitem a
vetorização. A vetorização quase sempre funciona mais rápido, pois o
tempo de execução é constante ou cresce em uma taxa muito mais lenta
com um número maior de elementos.
Para deixar claro, muitas CPUs têm conjuntos de instruções "vetoriais"
ou "SIMD" que aplicam a mesma operação simultaneamente a conjunto de
dados. Então vetorização é o processo de reescrever uma ação iterativa
de modo que, em vez de processar um elemento de uma coleção por vez
sejam aproveitadas a operações "SIMD" para aplicar essa operação
simultaneamente a vários elementos dessa coleção.
Answer: Acho que esse código resolve seu problema...
Crie uma função para add '-' depois do terceiro caractere e depois aplique a função na coluna com a função aplly
def mod(placa):
    if placa[3] == '-':
        return placa
    else:
        inicio = placa[0:3]
        final = placa[3:]
        new_placa = y+'-'+z
        return new_placa

xp['Placa'] = xp['Placa'].apply(mod)

Comment: Acabei de encontrar a solução, mas se meu código não estiver correto usarei o seu:

qtd_linhas = xp['Placa'].count()
ix = 0
while ix < qtd_linhas:
        celula0 = xp.loc[ix, 'Placa']
        placax = (celula0)
        tamanho = (len(placax))
        inicio = placax[0:3]
        final = placax[3:tamanho]
        separacao = '-'
        juncao = ('{} {} {}'.format(inicio, separacao, final))
        juncao2 = juncao.replace(" ", "")  # Para remover espaços
        print(juncao2)
        ix = ix + 1
Comment: Tem que ter um verificação se a terceira letra não é igual a '-' pq senão cada vez que vc rodar este código vai add '-' na posição 3, ou seja vc tem 10 placas com '-' adiciona mais uma e as 10 placas que estão corretas vão ficar NKK--89346. Isso server para o código que o Augusto Vasques postou também.
Answer: Acabei de encontrar a solução, mas se meu código não estiver correto usarei o seu:
qtd_linhas = xp['Placa'].count()
ix = 0
while ix < qtd_linhas:
        celula0 = xp.loc[ix, 'Placa']
        placax = (celula0)
        tamanho = (len(placax))
        inicio = placax[0:3]
        final = placax[3:tamanho]
        separacao = '-'
        juncao = ('{} {} {}'.format(inicio, separacao, final))
        juncao2 = juncao.replace(" ", "")  # Para remover espaços
        print(juncao2)
        ix = ix + 1


Extjs 4 Ext.getBody().mask() - Make this particular mask transparent

Question: I am trying to create an ExtJS 4 mask which is transparent with no loading message in only one place.
Ext.getBody().mask();
<do something.....>
Ext.getBody().unmask();

The above code gives a mask without any loading message but still the mask is visible. I want to make it transparent. I tried to add another cls to it something like:
Ext.getBody().mask(false, 'myMask');
CSS
.myMask { background-color: transparent; opacity: 0; }

But it didn't work. :(
Also tried Ext.LoadMask but the loading message box appears even when no message is set.
Please help. Thanks in advance!
Answer: the myMask class is applied to the loading message but not to the mask element.You need to use following css:
.x-mask {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0); // for older browsers
opacity: 0;
background: transparent;
}

EDIT:
To do it for a paritcular case. We need to add a class to body & remove that class while unmasking.
Ext.getBody().addCls('transparentMask');
Ext.getBody().mask();
<do something.....>
Ext.getBody().unmask();
Ext.getBody().removeCls('transparentMask');

CSS:
.transparentMask .x-mask {
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0); // for older browsers
opacity: 0;
background: transparent;
}

Comment: But in this case all the masks in the application will get impacted. I want to do it only for a particular mask.


Recurrence relation with ternary strings (at least two consecutive same numbers)

Question: Let c_n be the number of strings in {0,1,2} of
length n that contain at least two consecutive
symbols that are the same, i.e., 00, 11, or 22.
Find a recurrence relation for c_n when n >= 2.
What are c_0 and c_1?
My work:
c_0 and c_1 are obviously 0. I found that c_2 = 3 and c_3=15 by inspection, however I can not derive a recurrence relation. Intuitively, I thought that c_n might be 4*c_n-1 + 3 but I can not prove it. I thought about adding new numbers to left and right sides of the existing strings but there will be duplicates. How should I approach to this question?
Comment: It may be easier to count the complement on this one; but that doesn't give a recurrence relation.
Answer: HINT: Say that a ternary string is good if it contains two consecutive identical symbols and bad otherwise. Suppose that $t_1\ldots t_nt_{n+1}$ is good. Then exactly one of two things must be true: either $t_1\ldots t_n$ is good, or $t_1\ldots t_n$ is bad and $t_n=t_{n+1}$.

Show that there are $3c_n$ good strings $t_1\ldots t_nt_{n+1}$ of length $n+1$ such that $t_1\ldots t_n$ is good.
Show that there are $3^n-c_n$ bad strings of length $n$ and explain why there are therefore $3^n-c_n$ good strings $t_1\ldots t_nt_{n+1}$ such that $t_1\ldots t_n$ is bad.
Combine these results to get a recurrence expressing $c_{n+1}$ in terms of $c_n$ and $n$.


Help proving the continuous law of total probability

Question: How do I prove the following law of total probability? Any help is appreciated.
Let $X,Y:\Omega\to\mathbb{R}$ be jointly continuous. Then, for $B\subseteq\mathbb{R}^{2}$:
\begin{equation}
    \mathbb{P}\left((X,Y)\in B\right)=\int_{-\infty}^{\infty}\mathbb{P}\left((x,Y)\in B\mid X=x \right)f_{X}(x)\mathrm{d}x.
\end{equation}
Comment: Does this answer your question? [Can we prove the law of total probability for continuous distributions?](https://math.stackexchange.com/questions/2035418/can-we-prove-the-law-of-total-probability-for-continuous-distributions)
Answer: Here are two different ways.  I like the second best.
Iterated expectations:
For events $A$ and random variables $X$ we can define:
$$ P[A|X] = E[1_A|X]$$
where $1_A$ is an indicator function that is 1 if event $A$ holds, and 0 else.
Then iterated expectations gives
$$ P[A] = E[E[1_A|X]] $$
For the event $A = \{(X,Y) \in B\}$ we get
\begin{align}
P[(X,Y)\in B] &= E[E[1_{\{(X,Y) \in B\}}|X]] \\
&= \int_{-\infty}^{\infty} f_X(x) P[(X,Y)\in B|X=x] dx \\
&= \int_{-\infty}^{\infty} f_X(x) P[(x,Y)\in B|X=x] dx \\
\end{align}
where the last line is using the idea in the Graham Kemp answer.
Another way:
For events $A$ and random variables $X$ with $f_X(x)>0$ we can define:
$$P[A|X=x] = \frac{f_{X|A}(x)P[A]}{f_X(x)}$$
and you can verify that
$$ P[A] = \int_{-\infty}^{\infty} P[A|X=x] f_X(x)dx$$
For the event $A = \{(X,Y) \in B\}$ we get
\begin{align}
P[(X,Y) \in B] &= \int_{-\infty}^{\infty} P[(X,Y) \in B| X=x] f_X(x)dx\\
&=\int_{-\infty}^{\infty} P[(x,Y) \in B| X=x] f_X(x)dx\\
\end{align}
where again the last line is using the idea in the Graham Kemp answer.
Answer: Under the condition that $X=x$, the events of $\langle X,Y\rangle\in B$ and $\langle x,Y\rangle\in B$ will clearly be identical.
$$\{\omega:\langle X(\omega),Y(\omega)\rangle\in B\land X(\omega)=x\}=\{\omega:\langle x,Y(\omega)\rangle\in B\land X(\omega)=x\}$$
Everything else is just the definition for conditional probability over a zero measure condition.
Comment: What exactly is the definition for conditional probability over a zero measure condition?


How do I get the login link?

Question: I have a backup of a Drupal 7 website with its database. Is there any solution to get the login path? I have tried many options: /?q=user/login, /login, /user, and /user/login.
Comment: Welcome to Drupal Answers! Can you clarify what do you need exactly - do you need this to create a link, or you are trying to login to the website?
Comment: use drush command "drush uli" to get one time login link
Comment: @AramBoyajyan yes i'm trying to login but i can't find the login path .
Comment: @user15837 thx, i'll try this command :)
Comment: @user15837 thx drush command work ;)
Comment: cool!!! @ Grub_elm
Answer: use drush command to login, it will print the login URL.
<code>drush uli;
</code>

How do I get the login link?


An example of a subset of N which intersects every set of form {a + nd : n ∈ N}, a, d ∈ N, is (b) {$k^2$: k ∈ N} or (c) {k + k! : k ∈ N}

Question: An example of a subset of N which intersects every set of form $\{a + nd : n \in \mathbb N\}$, where
$a, d \in \mathbb N$, is
(a) $\{2k : k \in \mathbb N\}$.      (b) $\{k^2: k \in\mathbb N\}$.
(c) $\{k + k! : k \in \mathbb N\}$ (d) $\{k + k^2
: k \in\mathbb N\}$
options (a) and (d) can be eliminated as they are subsets of even numbers .
And they don't intersect the set of the form $\{1+2n :n \in\mathbb N\}$
I can't do anything with (b) and (c).
Please give a hint.
Comment: Why did you put `$` signs only around $k^2$?
Comment: @EspeciallyLime: Don't forget the title! :-)
Answer: Hint: what are the possible last digits of perfect squares?
Answer: You can eliminate (b) because the set $\{3n+2: n \in \mathbb{N}\}$ doesn't contain any squares. On the other hand, $(a+d)+(a+d)!$ shows up in $\{a+dn: n \in \mathbb{N}\}$ regardless of $a,d,$ so option (c) is correct although you wouldn't have to prove this for a multiple choice test.


How to store in cookie a list with objects MVC C#

Question: I need to store the shopping list items in cookie.
I have the products page where each product has an "Add" button where it should be added to cookie.
I run into a problem, I observed that I can store in the cookie only one product. If the user wants to add the second product to the basket, the first product will be overwrite with the newly added product.
I didn't find any solution on the net which can help me, my question is: I can store a list of objects in the cookie.
I tried this way:
string myObjectJson = new JavaScriptSerializer().Serialize(cartItem);

var cookiea = new HttpCookie("myObjectKey", myObjectJson)
{
  Expires = DateTime.Now.AddYears(1)
};

When I need the cookie value, I can get this way. It works:
var tttt = new JavaScriptSerializer().Deserialize<ShoppingCartVM>(myObjectJson);

How should I resolve the problem if the cookie already contains an item, to add a the new item to the same cookie, meaning the cookie had to store a list of objects, and when I need it to return me the entire list of the objects?
Could anyone help me on this? Thanks in advance!
Comment: Are you sure you save the whole list? I can see you serialize cartItem variable. The name suggests you really save only one item.
Comment: Yes, you're right. The example from code really saves one item, but the question was how can I save an object list to cookie, adding a new item to a cookie which contains already some elements. I need to save the whole list, because this should be the shopping list, which can contain more elements, whenever the user press the Add button from a product, it should be added to shopping list, in my case in the cookie. And when the user press the Shopping Cart button, there I should retrieve the whole list from the cookie.
Answer: You really shouldn't use Cookies for storing shopping cart items. Session is much better in this case. Cookies are stored on the client side whereas Session is stored on server. When you use cookies, you send whole serialized shopping cart back and forth between the client and server, that's not necessary. Also, Cookies's size is limited and Session does allow you to store complex objects.
To save the shopping cart, use (in controller):
ShoppingCartVM cart; // create instance of the shopping cart
this.Session["ShoppingCart"] = cart;

To get the values, use:
if (this.Session["ShoppingCart"] !== null)
{
    ShoppingCartVM cart = (ShoppingCartVM)Session["NeededByDateTime"];
}

If you need something persistent. To make sure that the user will find their products in the cart even after couple months. You should consider saving their shopping cart in database.
Comment: Thank you for the tip. I guess I will use session to store the shopping cart items.


With node/express/Multer, how do you "first" check a parameter?

Question: I use multer exactly like this,
const specialExample = multer({ .. blah blah })
app.post(
    '/specialPath',
    specialExample.single('handled_by_multer'),
    await function (req, res) {
        fs.renameSync(.. move the file, etc
        // send a reply
        res.set('Content-Type', 'text/plain')
        res.status(200).send(`ok`)
    })

So note that things happen in this order:
app.post(
    '/specialPath',
// EXPRESS KNOWS TO GO THIS WAY
    specialExample.single('handled_by_multer'),
// MULTER LITERALLY IS GETTING THE FILE
// MULTER HAS COMPLETELY GOT AND SAVED THE FILE
    await function (req, res) {
// THIS FUNCTION NOW BEGINS RUNNING
        ... etc

That's all perfectly fine.
Now, imagine I want to look at some parameter.
In some cases I DO NOT want to get the file.
const specialExample = multer({ .. blah blah })
app.post(
    '/specialPath',
    specialExample.single('handled_by_multer'),
    await function (req, res) {
        const meta = req.body.meta
        if (meta !== "wth") {
           .. we actually DON'T want the  file
           .. delete the file
           .. send a 401 and return
        }
        fs.renameSync(.. move the file, etc
        // send a reply
        res.set('Content-Type', 'text/plain')
        res.status(200).send(`ok`)
    })

Note that that works fine, BUT, we are wastefully getting/saving the file (and only then deleting it).
Is there a way to do something more like this .......
app.post(
    '/specialPath',

    await function (req, res) {
        .. if I happen to want to, only then: {
            specialExample.single('handled_by_multer'),
        }
    })

How to?
Answer: Since multer is the middleware that's (also) parsing the field data that gets stored in req.body, you can't conditionally call it.
Instead, you can use Multer's fileFilter to ignore the file data if your conditions are met:
const specialExample = multer({
  fileFilter(req, file, cb) {
    const meta = req.body.meta;
    if (meta !== "wth") {
      // reject this file
      return cb(null, false);
    }
    return cb(null, true);
  },
  .. blah blah
});
...
app.post(
    '/specialPath',
    specialExample.single('handled_by_multer'),
    await function (req, res) {
      // also have to check here to make sure the correct response is sent
      const meta = req.body.meta
      if (meta !== "wth") {
        return res.sendStatus(401);
      }
      ...
    }
);

One possible issue: the field(s) that you depend on to check if the file should be rejects (meta in this example) needs to be placed into the multipart data before the file data, otherwise it won't be populated by the time fileFilter() is called.
Comment: RK , great, thanks, I didn't know about fileFilter.  I'll give it a go.  (As you may know, fileName seems to be buggy/tricky, folks report it not working, and I couldn't get it working. I'll see how I go with fileFilter!)  Thanks!
Comment: That's an amazing tip about adding the form fields actually before the file, who would have known ?!
Comment: hello, it seems that if you indeed return `false` in the `fileFilter`. Then in fact. In the path code, it seems you get `req.file === undefined`.  Thus, you can check for that rather than "performing the same check again" (in my case it was a very heavy / slow check, so it's handy).  Amazing!
Comment: @Fattie yeah you could use that, or be a bit more explicit and add a flag to `req` (say `req._dontContinue`) to indicate that the request should fail. Both the `fileFilter` function and your request handler will receive the same `req` instance.
Comment: great point about adding to `req.`  thanks a million for the amazing answer, I've noticed few people have expertise in this field. bounty en route :)
Answer: As per the amazing @robertklep answer/comments, you can alternately use
        if (req.file === undefined)

to branch in the function.
Here's a full example showing

use fileFilter to decide if you want to accept the file or not

use storage#diskStorage#destination (not "dest") to set the directory

use storage#diskStorage#filename function to decide on the filename

So:
const examp = multer({

    async fileFilter(req, file, cb) {
        // check for say authorization etc
        // to get any params, req.body.yourParameter ...
        if (permitted === true) {
            return cb(null, true)
        }
        else {
            return cb(null, false)
        }
    },

    storage: multer.diskStorage({

        destination: _some_directory,

        filename: function (req, file, cb) {
            // eg, you want the filename not the random string
            cb(null, file.originalname)
        }
    })
})

and then ...
app.post(
    '/happyPath',
    examp.single('multer_tag'),
    async function (req, res) {

        if (req.file === undefined) {
            // WE KNOW MULTER SAID 'NO' in fileFilter above
            res.set('Content-Type', 'text/plain')
            res.status(401).send("")
            return
        }

        res.set('Content-Type', 'text/plain')
        res.status(200).send(`ok`)
    }
)

NOTE filename is only available inside diskStorage which is a bit silly. (Disk storage seems to be the default anyway.) There are many internet posts "filename is not working in multer". The solution is simple, you have to explicitly include the diskStorage.


How can I reproduce R's prediction band calculation?

Question: Suppose I fit a linear regression model in R like so
<code>> head(mtcars[, c("mpg", "wt")])
                   mpg    wt
Mazda RX4         21.0 2.620
Mazda RX4 Wag     21.0 2.875
Datsun 710        22.8 2.320
Hornet 4 Drive    21.4 3.215
Hornet Sportabout 18.7 3.440
Valiant           18.1 3.460

# Fit linear regression model
> lrmodel <- lm(mpg ~ wt, data = mtcars)

> print(lrmodel)

Call:
lm(formula = mpg ~ wt, data = mtcars)

Coefficients:
(Intercept)           wt
     37.285       -5.344
</code>
I can make predictions on new data (or in this case, the same data) with prediction bands.
<code># Make predictions with lower and upper prediction bands @ 0.99 level
> preds <- predict(lrmodel, mtcars, interval = "prediction", level = 0.99)
> head(preds)
                       fit      lwr      upr
Mazda RX4         23.28261 14.72715 31.83807
Mazda RX4 Wag     21.91977 13.39748 30.44207
Datsun 710        24.88595 16.26877 33.50313
Hornet 4 Drive    20.10265 11.59662 28.60868
Hornet Sportabout 18.90014 10.38722 27.41307
Valiant           18.79325 10.27904 27.30747
</code>
Given my fitted model, how can I reproduce these prediction bands manually?

What I've tried
According to this article prediction bands are given by
$$
\hat{y}_h \pm t_{(1-\alpha/2, n-2)} \times \sqrt{MSE \times \left(1+ \frac{1}{n} + \dfrac{(x_h-\bar{x})^2}{\sum(x_i-\bar{x})^2}\right)}
$$
where

$x_h$ is the x location of the point of interest
$\hat{y}_h$ is the predicted value at the point of interest
$t_{(1-\alpha/2, n-2)}$ is the t-multiplier with n-2 degrees of freedom

Plugging and chugging..
<code># Lower band calculation
xh <- lrmodel$model$wt[1]          # 2.62
n <- length(lrmodel$residuals)     # 32
tval <- dt(x = 0.99/2, df = n - 2) # 0.3487666
xbar <- mean(lrmodel$model$wt)     # 3.21725
mse <- mean(lrmodel$residuals^2)   # 8.697561
yhat <- preds[1, "fit"]            # 23.28261
yhat - tval * sqrt(mse * (1 + 1/n + (xh - xbar)^2/sum((lrmodel$model$wt - xbar)^2)))
# 22.23202
</code>
My result, 22.23202, is quite different than R's result for the same $x$ value, 14.72715. Where did I go wrong?
Comment: Both the standard error and the critical value are wrong. Hints: a) Use `qt` for the critical value & make sure that you specify the quantile correctly. b) For the standard error, take into account that the model has two parameters.
Answer: Thanks to @dipetkov's hints in the comments, I was able to correct my mistakes, and reproduce the calculation as follows:
<code># Lower band calculation
xh <- lrmodel$model$wt[1]               # 2.62
n <- length(lrmodel$residuals)          # 32
tval <- qt(p = 0.01/2, df = n - 2)      # -2.749996
xbar <- mean(lrmodel$model$wt)          # 3.21725
mse <- sum(lrmodel$residuals^2)/(n - 2) # 9.277398
yhat <- preds[1, "fit"]                 # 23.28261

# upper band (31.83807)
yhat - tval * sqrt(mse * (1 + 1/n + (xh - xbar)^2/sum((lrmodel$model$wt - xbar)^2)))

# lower band (14.72715)
yhat + tval * sqrt(mse * (1 + 1/n + (xh - xbar)^2/sum((lrmodel$model$wt - xbar)^2)))
</code>
Comment: I suggest to use *qt(1 - alpha / 2, n - 2)* instead of *qt(alpha / 2, n - 2)*. Note that how this necessitates that the upper limit is yhat - tval and the lower limit is yhat + tval, which is a bit counterintuitive at first sight. Will correspond to the formula as well.


falha no atributo .append ao inserir cadastro em lista - (Python)

Question: Consegui deixar o meu cadastro em loop que será encerrado apenas se o usuário digitar N ou n.
Após encerrar o cadastro o programa exibe as listas de cadastro e lista de sorteio, porem, quando imprime a lista de cadastro, retorna apenas o ultimo nome e valor cadastrado.
preciso que o programa me retorne conforme exemplo abaixo:
Lista de Cadastro
Maria doou R$30,00João doou R$20,00
A lista de sorteio esta como eu preciso, multiplicando os nomes pelo valor doado.
Ex:
[Maria, Maria, Maria, João, João]
será que o programa não reconhece a instrução lista_cadastro.append([nome,valor])? como eu corrijo essa falha?
segue o código:
<code>    import random
    lista_sorteio = list()#lista que vai receber o nome para sorteio
    lista_cadastro = list() #lista que vai receber o cadastro

    while True: #loop de repetição para varios cadastros
     nome = str(input('Nome: ')).capitalize() #captura o nome do doador
     valor = int(input('Valor: R$')) #captura o valor doado
      print('') # inserir espaço
      chances = int(valor // 10)
      for c in range(chances):
          lista_sorteio.append([nome])
      alexa = str(input('INSERIR NOVO CADASTRO? [S/N] ')).strip().upper()[0] # sai do loop cadastro
      if alexa in 'Nn':
         break #encerra o loop digitando N
    print('') # inserir espaço
    print('LISTA DE CADASTRO:')
    for d in range(chances):
        lista_cadastro.append([nome, valor])
        print(f'{nome} doou R${valor:.2f};')  # mostra os nomes em
    lista_cadastro
    print('')  # inserir espaço
    print('LISTA DE SORTEIO:')
    print(lista_sorteio) # mostra os nomes em lista_sorteio
    print('') # inserir espaço
    ganhador = random.choice(lista_sorteio) #realizar sorteio
    print('O ganhador do Sorteio é {[0]}'.format(ganhador)) #mostra o nome do sorteio
</code>
Answer: Se bem entendi qual era o objetivo (aumentar as chances de quem colaborou mais), deu certo fazendo o final assim:
<code>print('LISTA DE SORTEIO:')
for nom in lista_cadastro:
    nomeSorteio = int(nom[1] / 10)
    for c in range(nomeSorteio):
        lista_sorteio.append(nom[0])

print
print(lista_sorteio)
ganhador = random.choice(lista_sorteio)  # realizar sorteio
print('O ganhador do Sorteio é {}'.format(ganhador))  # mostra o nome do sorteio
</code>
Sugiro analisar as diferenças para ver o que estava errado..
Comment: obrigado pela resposta Epx, no caso, eu imaginei um raciocínio no qual realizaria todos os cadastro e só depois retornaria  a lista na tela:
ex:

LISTA DE CADASTRO
XXXXXX
XXXXX

LISTA DE SORTEIO
XXXXXX, XXXXXX, XXXXX,  XXXXX

GANHADOR
XXXXXXX

eu consigo imprimir a lista de cadastro, mas não consigo imprimir a lista de sorteio sendo que ela multiplica o nome pelo valor doado.
ex:
lista de cadastro
maria doou 30,00
joão doou 20,00

Lista de Sorteio
[Maria, Maria, Maria, João, João]
Comment: alterei o código, consegui o retorno da lista de sorteio multiplicado pelo valor doado, porem agora a falha é no retorno da lista de cadastro, no qual não retorna todos os itens cadastrado, retorna apenas o ultimo item, poderia me auxiliar nessa falha?


Interpretation of Coefficients of Expanded Cyclotomic Polynomials

Question: Working out the following definition of the Cyclotomic Polynomial
$$    {\displaystyle \Phi _{n}(x)=\prod _{\stackrel {1\leq k\leq n}{\gcd(k,n)=1}}\left(x-e^{2i\pi {\frac {k}{n}}}\right),}
$$
you'll get
$$
{\displaystyle x^{\varphi(n)}-x^{\varphi(n)-1}\sum _{\stackrel {1\leq k\leq n}{\gcd(k,\,n)=1}}e^{2\pi i{\frac {k}{n}}}} + \dots + 1
$$
You'll spot right away that the next to leading coefficient gives the Möbius function:
$$    {\displaystyle \mu (n)=\sum _{\stackrel {1\leq k\leq n}{\gcd(k,\,n)=1}}e^{2\pi i{\frac {k}{n}}}}
$$
For the final $1$, you'll get:
$$
\prod _{\stackrel {1\leq k\leq n}{\gcd(k,n)=1}}\left(-e^{2i\pi {\frac {k}{n}}}\right)=\left(e^{\frac{2i\pi}{n} \sum_{\stackrel {1\leq k\leq n}{\gcd(k,n)=1}}k}\right )\prod _{\stackrel {1\leq k\leq n}{\gcd(k,n)=1}}(-1)=\left(e^{\frac{2i\pi}{n}\frac{n}{2}\varphi(n) }\right )(-1)^{\varphi(n)}=+1,
$$
which is expected since the coefficients are symmetric and $x^{\varphi(n)}$ also has $+1$.

Do the other terms also have number theoretical interpretation?

UPDATE:
A first partial result found at OEIS: Coefficient of $x^2$ in the $n$-th cyclotomic polynomial.
If $n$ is odd, $a(n) = 1/2 (\mu(n)^2-\mu(n))$,
if $n$ is even, $a(n) = 1/2 (\mu(n)^2-\mu(n)) - \mu(n/2)$, where $\mu$ is Möbius function...
Answer: Not sure if this is what you mean by "number theoretical interpretation", but the coefficients will, up to sign, be the elementary symmetric polynomials in the primitive $n$-th roots of unity.
For $n$ prime, these coefficients are all $1$.
For $n=2p$, where $p$ is an odd prime,  they alternate between $1$ and $-1$.  See this.
Comment: No not really, rather what other arithmetic functions are composed as elementary symmetric functions. Thanks anyway...
Comment: ... hmmm, but after thinking twice, it seems that this is the right way. I'm struglling to get my own answer in accordance with your general approach: Let $\mu(n)$ be the elementary symmetric polynomial $e_1(=p_1)$. Due to Newton's Identities $e_2=\frac12(e_1p_1-p_2)$, which is close to the odd case, but doesn't work out in general. Can you help? Is this a special property of the Möbius function..?
Answer: As Chris pointed out in his answer, the coefficient of the $m$th term of a cyclotomic polynomial is the elementary symmetric polynomial $e_m$ in the primitive $n$-th roots of unity, $\zeta_1,\zeta_2,...,\zeta_{\varphi(n)}$.
And $e_m$ can be written in the following way:
$${\displaystyle {
\begin{aligned}
e_{m}&={\frac {(-1)^{m}}{m!}}B_{m}(-p_{1},-1!p_{2},-2!p_{3},\ldots ,-(m-1)!p_{m})\\
     &=(-1)^{m}\sum _{r_{1}+2r_{2}+\cdots +mr_{n}=m \atop r_{1}\geq 0,\ldots ,r_{m}\geq 0}\prod _{i=1}^{m}{\frac {(-p_{i})^{r_{i}}}{r_{i}!i^{r_{i}}}}\\
\end{aligned}}}
$$
where the $B_n$ is the complete exponential Bell polynomial.
Now $p_k(\zeta_1,\zeta_2,...\zeta_{\varphi(n)}) $ can be seen as the Ramanujan sum evaluated at $k$:
$$
p_k(\zeta_1,\zeta_2,...\zeta_{\varphi(n)}) =\sum _{\stackrel {1\leq t\leq n}{\gcd(t,\,n)=1}}e^{2\pi i{\frac {t}{n}k}}=c_n(k)
$$
So
$$
{\displaystyle e_{m}={\frac {(-1)^{m}}{m!}}B_{m}(-c_n(1),-1!c_n(2),-2!c_n(3),\ldots ,-(m-1)!c_n(m))}
$$
and e.g. $$e_2=(-1)^2\left( \frac{(-c_1)^2}{2!1^2} + \frac{(-c_2)^1}{1!2^1} \right) = \frac12 \left( c_1^2-c_2 \right) \\ \color{grey}{\; \; \overset{?}{=} \frac12 \Big(\mu(n)^2-\underbrace{(\mu(n)+2(1-\bmod(n,2))\mu(n/2))}_{\overset{?}{=}c_2}\Big) }$$


Drupal 7 log-in form doesn't react

Question: Out of the blue I'm unable to login into my website:
http://karlssonwilker.com/dev_v2/kw/user
I type my username and my password and upon clicking "log-in" or pressing RETURN nothing happens.
Could it be something related with the database? Disabling javascript, css, removing any custom functions, or clearing the cache tables on phpmydamin seems to have no affect.
No idea what the problem is.
I had a test user created before I started having this problem. You can use it if you care to try.
user: test
pass: test
Anyone came across a similar problem?
Thanks!
Comment: If it's Drupal 6 make sure you have the anonymous user (uid:0) record in the users table
Answer: There is something wrong with your HTML:
<div id="node_container" <form="" action="/dev_v2/kw/user" method="post" accept-charset="UTF-8">

This is definitely not interpreted as form.
Comment: Hi Potney,
Thank you for answer.
I've changed the HTML and the form is now responding.  But still no success logging in
Comment: Are you sure these are the correct credentials? Run a one time login (drush uli) change the password and check again.
Comment: Reseting the password or inputing the correct password redirects you to the user log-in form and keeps you logged out.
Answer: I've noticed that your Drupal installation is at http://karlssonwilker.com/dev_v2/kw/ which is a subdirectory. This can mess up some rewrite rules in .htaccess when using clean urls.
Go to .htaccess and find this line that is greyed out.
# RewriteBase /drupal

You should uncomment that and replace it with...
RewriteBase /dev_v2/kw

This will most likely fix your problem. If you are using some sort of CVS and this is a staging site, I would recommend ignoring the .htaccess file. I wouldn't want you to accidentally destroying your live site.

Drupal 7 log-in form doesn't react


When is it OK to write "we assumed a normal distribution" of an empirical measurement?

Question: It is ingrained in the teaching of applied disciplines, such as medicine, that measurements of bio-medical quantities in the population follow a normal "bell curve." A Google search of the the string "we assumed a normal distribution" returns $\small 23,900$ results! They sound like, "given the small number of extreme data points, we assumed a normal distribution for the temperature anomalies" in a study on climate change; or "we assumed a normal distribution of chick hatching dates" on a possibly less contentious document on penguins; or  "we assumed a normal distribution of GDP growth shocks", referring to macroeconomic changers in markets (bringing up to memory this book, ... and other things).
Recently, I found myself questioning the treatment of count data as normally distributed due to their strictly positive nature. Of course, count data are discrete, making their normality all the more artificial. But even leaving this latter point aside, why should continuous empirical measures such as weight, height or concentration of glucose, deemed prototypically "continuous", be considered normal? They can't have negative realized observations any more than counts do!
I understand that when the standard deviation is substantially lower than the mean, indicating few negative values ("95% range check") it may be a practical assumption, and frequency histograms may support it if not too skewed. But the question didn't seem trivial, and a quick search yielded interesting stuff.
In Nature we can find the following statement on a letter by DF Heath: "I wish to point out that for the statistical analysis of certain types of data the assumption that the data are drawn from a normal population is usually wrong, and that the alternative assumption of a log-normal distribution is better. This alternative is widely used by statisticians, economists and physicists, but for some reason is often ignored by scientists of some other disciplines."
Limpert notes that "the log-normal model may serve as an approximation in the sense that many scientists perceive the normal as a valid approximation now", while noting the low power of goodness-of-fit tests of normality, and the difficulty in selecting the right distribution empirically when dealing with small samples.
Therefore the question is, "When is it acceptable to assume a normal distribution of an empirical measurement in the applied sciences without further supportive evidence?" And, Why other alternatives, such as the log-normal, have not, and probably are just not going to take hold?
Comment: The answer would depend on what sort of thing you're doing, and the sensitivity it has to potential deviations from normality (i.e. if you're testing equality of variances using an F test of the ratio, you'd better have distributions that are very close to normal ... but if you were constructing a t-interval for the difference in means, with large samples, you might not need to have them very close to normality at all). ... and on your tolerance (or your audience's) for the kind of impacts it would have on the inference you're doing.
Answer: I find your question really interesting. Let's have some things into account:

To say that an observed variable is continuous in real life is going to be always kind of wrong, because it's very difficult to measure really continuously.
Now add the properties of a normal random variable $N(\mu, \sigma^2)$: range $(-\infty; +\infty)$, symmetrical distribution (mean = mode = median),  the probability density function $f_X(x)$ has inflection points at $x = \mu - \sigma$ and $x = \mu + \sigma$.
To say that a random variable $X$ follows a Log-Normal distribution implies that the variable $Y=log(X) $ follows a normal distribution.

With that said, to say that any observed variable follows a normal or a Log-Normal distribution sounds kind of crazy. In practice, what's done is that you measure deviations of the observed frequencies from the expected frequencies, if that variable came from a normal (or any other distribution) population. If you can say that those deviations are just random, because you are sampling, then you can say something like there's not enough evidence to reject the null hypothesis that this variable comes from a normal population, which is translated into we will work as if (assuming that) the variable follows a normal distribution.
Answering to your first question, I don't think that there's someone as bold to say that a variable is assumed to be normally distributed without further evidence. To say something like that, you need at least a qq-plot, an histogram, a goodness-of-fit test or a combination of those.
To answer the second question, the particular interest in the normal distribution is that many of the classical tests are based on an assumption of normality of the variable, like the t-test, or the $\chi^2$-test for the variance. So, normality simplifies work, that's all.
Comment: Something that I didn't mention is the other part of the history if the normal distribution: it is the limit distribution of the standardization of a sum of iid random variables, as it's stated in the theorem of the central limit. If you can say that your variable is a sum of many iid random variables, like in the reasoning behind brownian motion, then you can say that it is a normal random variable. That's the only valid shortcut I know. I can include this in the answer if you want.
Comment: Thank you for your answer, which touches upon many key points. However, I tend to think that things in the "real-world" of applied sciences are less structured, and a direct tangent is often taken to assume normality.
Answer: This largely depends on the robustness of your inferences to errors in the distribution
When you are dealing with quantities that are either directly observable, or for which there is some close estimator (e.g., residuals for error terms), you can use the data to make non-parametric estimates of the distribution.  Assuming you have sufficient data to do this, it is generally best to avoid making a distributional assumption about those quantities, unless you are subjecting these assumptions to empirical tests/diagnostics to confirm the plausibility of the assumed form.  The reason for this is obvious --- if the values are observable (or closely estimable) then with a reasonable amount of data you can estimate the distribution, so there is no need to blindly assume its form.
Now, it is of course that case that parametric statistical models assume a parametric distributional form of some kind.  In most statistical analysis in a regression context, we specify a model form that has an assumed form for the "error terms".  For example, in a standard Gaussian regression we assume that the error terms are normally distributed.  Once we have fit our model to the data we can then construct diagnostic plots of residuals and use these to determine whether there are any obvious departures from the assumed distributional form.  If the assumed distribution is dubious we have two options: (1) we can either change our model assumptions and rinse-and-repeat; or (2) we can keep our existing model and use it only for inferences, etc., that are known to be robust to errors in the assumed error distribution in the model.  In many cases the latter approach is fine (so long as other assumptions in the model are okay), since many of the conclusions from a regression model are robust to the distribution of the error term.$^\dagger$
More generally, the importance of the distributional form is going to depend on what kind of inferences you are making and how robust these inferences are to errors in the assumed form.  This is contextual; in some projects you will want inferences about model coefficients, whereas in others you will want predictions of observable variables, and in others you may want something else.  Generally speaking, the following advice applies:

If you are using a parametric model (which assumes a parametric distribution of a particular kind), always make an effort to choose a form that is as close to the data as is reasonable.  This will usually involve conducting diagnostic tests on observable quantities in the model (e.g., residuals in a regression) and then varying the model with transformations, changes in form, etc., until you feel that you have a good representation of the data.  It is generally bad practice to make "bald assumptions" that are not scrutinised against diagnostic plots, etc.

Always consider the kinds of inferences/predictions you actually want to make in your analysis, and consider how sensitive/robust each of these are to errors in the assumed parametric distributional form.  If you need to make inferences/predictions that are highly sensitive to the assumed distributional form then you need to ensure that you apply diagnostic tests and you are satisfied that the assumed distribution is a reasonable representation of the data.

For some proposed transformations, like taking the logarithms of quantities under analysis, there are natural theoretical considerations that apply.  For example, variables are generally transformed onto a log-scale (i.e., taking logarithms) if they are positive quantities that tend to change by a percentage that has a roughly fixed distribution over time.  This is true of many quantities studied in economics and physics.  Transformation of variables should be informed by theoretical considerations and empirical analysis.

In some cases you will be limited by the available knowledge on model forms (i.e., the extent of the statistical literature on the forms of interest to you).  You might want to vary your model in some way but find that there is little or no research on the properties of the ideal model.  In such cases, you may need to fall back on suboptimal models, and include appropriate caveats in your analysis.

$^\dagger$ Specifically, assuming you have a reasonable amount of data, the estimated coefficients are highly robust to the assumed error distribution, but predicted values of the response variable are not.
Answer: Gnedenko and Hinchin in their textbook "Elementary introduction in the theory of probability" claim that there is a tedious theoretical proof of the following claim. If the value of a variable is influenced by numerous statistically independent factors, then it is appropriate to assume that the values of the variable is normally distributed. For instance, if one shuts bullets from the gun into the target, then the final position of the bullet is influenced by numerous independent factors (wind, shaking of the hands, the mass of a bullet, the friction between the hand and the gun, etc).
Comment: They are probably referring to the [tag:central-limit-theorem]
Comment: This is a good comment.  However, bear in mind that "influenced by" in this textbook context *must* be interpreted in terms of *additive* factors. The point of the question is that in many disciplines, those factors are not all additive: many are *multiplicative.* That leads to approximate lognormal distributions rather than normal distributions.
Comment: CLT relates to the distribution of the means. In this case, we formulate the conditions that are enough for the variable to be normally distributed. The factors do need to be additive. Besides, the contribution of each factor must be much smaller than the total contribution of all factors.
Comment: It is in my best intention not to sound sarcastic in the context of the policy of this website. Specifically, in the case when all factors are additive it looks like the normal distribution and the lognormal one produce the same results during testing. I believe that the multiplicative nature of the factors is the pertubative one. To my mind, in the context of the theory of errors this subtle difference can be ignored. I would be happy to see some comments since I do not know the reference of the novel result.


Encriptar variáveis na URL

Question: Com solução do @dvd, passamos variáveis para a URL através de um select: https://pt.stackoverflow.com/a/277883/95735
São várias que ficam assim quando são passadas: example.com/?fundo=img&cor=red
O que eu gostaria de fazer agora é: encriptar a variável e seu valor na URL.
Mais ou menos asssim: example.com/?d5t9a15d2a54ds54d5
Comment: Pra que finalidade fazer isso ?
Comment: @MarceloRafael para deixar curta a URL com as variáveis passadas.
Comment: Você pode converter os parâmetros para `base64` com o `btoa`, mas já adianto que não vai diminuir.
Answer: Pode alterar a URL no navegador com history.pushState. Para gerar um número variável, você pode pegar a hora do sistema com new Date().getTime() e colocar na URL preservando as variáveis passadas.
A URL no navegador ficaria algo como:
example.com/?1520231465457

O código ficaria assim:
document.addEventListener("DOMContentLoaded", function(){

   var url_short = new Date().getTime(),
       url_ = location.href,
       param = url_.substring(url_.lastIndexOf("/")+1, url_.length),
       params = ['layout','sidebar']; // insira aqui os nomes das variáveis

   history.pushState(null, '', '?'+url_short);

   for(var y=0; y<params.length; y++){
      if(param.indexOf(params[y]) != -1){

         var var_ = url_.substring(url_.indexOf(params[y])+params[y].length+1,url_.length).match(/^(\d|\w){1,}/)[0],
             a_ = document.body.querySelectorAll("a");

         document.body.querySelector("#"+params[y]).value = var_;

         for(var x=0; x<a_.length; x++){
            a_[x].href += (a_[x].href.indexOf("?") == -1 ? "?" : "&")+params[y]+"="+var_;
         }
      }
   }

   var sels = document.querySelectorAll("select");

   for(var x=0; x<sels.length; x++){
      sels[x].addEventListener("change", function(){

         var sId = this.id,
             sVa = this.value;

         if(sVa && url_.indexOf(sId) == -1){

            location.href = url_+(url_.indexOf("?") == -1 ? "?" : "&")+sId+"="+sVa;

         }else if(sVa && url_.indexOf(sId+"="+sVa) == -1){

            var var_ = url_.substring(url_.indexOf(sId)+sId.length+1,url_.length).match(/^(\d|\w){1,}/)[0];

            location.href = url_.replace(sId+"="+var_, sId+"="+sVa);

         }

      });
   }

});

Página de teste.
Comment: apliquei aqui, e está funcionando como esperado, imagino que o pequeno atraso que existe no input do browser ao mascarar a as vars passadas seja algo inevitável nesse caso.
Comment: Vou dar uma olhada melhor nesse atraso e ver se pode melhorar.
Comment: e um botão reset seria possível?
Comment: como assim um botão?
Comment: um botão para resetar todas as variáveis.
Comment: Basta dar um refresh na página sem as variáveis.
Comment: Vamos [continuar esta discussão no chat](https://chat.stackexchange.com/rooms/74067/discussion-between-john-quimera-and-dvd).
Comment: essas questões só você consegue resolver. https://pt.stackoverflow.com/q/281563/95735
Comment: Vc quer o valor em em pixels ou %? Digamos, imagem de 300x200, vai dar uma proporção de 1,5... vc quer que converta esse 1,5 em q?
Comment: É em %, é tudo com base nisso aqui: https://codepen.io/EightArmsHQ/full/OMGOyE/ 300x200 é igual a 66.67%; que é posto no padding-bottom
Answer: Use o pacote Crypto-js
Basta descomentar a primeira var search para pegar os paramêtros

//var search = window.location.search;
var search = '?name=fulaninho&pass=12312313&address=rua+dos+taubatucos+numero+22';
var pass   = '9090';

var enc = CryptoJS.AES.encrypt(search, pass).toString();
var dec = CryptoJS.AES.decrypt(enc, pass).toString(CryptoJS.enc.Utf8);

console.log(search);
console.log(enc);
console.log(dec);

Você pediu para deixar a URL curta, pra mim isso não tem como. Mas está aí, de uma certa forma adiciona um pouco de segurança para a sua aplicação
Comment: estou tentando aplicar sua solução a esse código https://pt.stackoverflow.com/a/277883/95735

Encriptar variáveis na URL


Strange HtmlUnit behavior (bug?)

Question: Take a look at this:
WebClient client = new WebClient();
WebRequestSettings wrs = new WebRequestSettings(new URL("http://stackoverflow.com/ping/?what-the-duck?"), HttpMethod.HEAD);
client.getPage(wrs);

Running this code results in throwing FileNotFoundException, because HTTP Status code on the page is 404 and getting the same page again with the GET method, with User-Agent set to Java-.... Why does it GET the page (it doesn't happen with "normal" status codes)? Is this a bug? Thanks
Here is the entire server response:
HTTP/1.1 404 Not Found
Cache-Control: private
Content-Length: 7502
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
Date: Thu, 11 Feb 2010 14:12:11 GMT

Where does it tell client to GET something? And how can I force WebClient to ignore it?
Here's a screenshot of HTTPDebugger:

The problem here is I don't understand why the second request is being sent and why is it sent with different useragent.
Answer: You execute a HEAD request - this returns a response with null content. HtmlUnit nevertheless tries to create a page. To do so, it creates an input source with url and content (which is null) and gives it to a parser. An when a parser tries to parse the input source it sees a null content and uses the URL to retrieve the content anew. So it's actually not the HtmlUnit which makes the second requests, it's the XML parser. And that's why the user agent is Java and not the HttpClient.

Strange HtmlUnit behavior (bug?)


Apache 2.4 on Windows 2008 large file downloads are corrupt

Question: I have a 400MB zip file on a server that I am attempting to access directly via a web browser (not through a script). The file seems to download normally, but cuts off at 124 MB as a corrupt file. The same results keep occurring regardless of the browser used.
The Apache access log appear to show a successful download:
IP - - [18/Jan/2013:10:07:33 -0500] "GET /url/to/file/myfile.zip HTTP/1/1" 200 455000714

There is nothing in the error log.
I've done some searching and the only thing I could find was to add AcceptFilter http none which seemingly had no affect.
I'm running Apache 2.4 on Windows 2008 server.
Comment: Are you sure the file is indeed not corrupt on the server?
Comment: Yes.  I can unzip it normally on the server and it works fine when downloaded via SFTP.
Answer: I had this same issue, and came across this hoping for an answer.
I strongly suspect there is a bug in 2.4 as this didn't happen in 2.2
After far too much mucking around I finally got my case fixed.
I hope this works for you to I know it's a work around but it's better than nothing.
In my httpd.conf I enabled sendfile.
So swap
#EnableSendfile On

with
EnableSendfile On

Comment: i'm using apache 2.4 on win 7 64b with webdav module and got the same issue.
Now solved with "EnableSendfile On".
Thank you
Comment: @Chip Looks like it doesn't happen in 2.2 because EnableSendfile is default 'On' in Apache 2.4 they switch it to default 'Off'. Struggled with this for a while. Thanks for posting this, helped a lot.
Comment: I am also having an issue of big ZIP files downloaded getting corrupted.
I am using a Docker image from : FROM httpd:2.4.29-alpine. The docker image is serving files mounted from a windows directory. And they were coming corrupted al the time until I enabled the EnabledSenfFile On as said above.  Also believe there mus be a very important bug - either in the intergation with docker windows - or somewhere else. For small tiny files nothing was na issue.
Comment: I don't know if it would appropriate to edit the answer with what I'm about to share, as the question is about windows, but as hinted by @99Sono be aware that if you the Apache server is on Linux or other non-Windows system containing a CIFS share that needs to be made available, it is probably best to use the enable EnableSendfile only for the CIFS mount.


iptables - ssh - access from specific ip only

Question: I would like to setup iptables rules on remote server in that manner that I am able to access to it only from specific IP address.
The current iptables list looks like this:
root@localhost:~# iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  220.127.116.11                 anywhere             tcp dpt:ssh
ACCEPT     tcp  --  184.108.40.206                 anywhere             tcp dpt:ssh
DROP       tcp  --  anywhere                anywhere             tcp dpt:ssh

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain l (0 references)
target     prot opt source               destination

Where 22.214.171.124 and 22.214.171.124 are the IP destinations from where I want to have access to this machine.
And all that works fine. If i type:
ssh root@server-ip-address-here

i have access only from those 2 locations. Any other location is encountering timeout and its not able to reach port 22.
The thing is, i have a domain associated with this server/ip for example, example.com.
Now, what puzzles me is, if i try to access to the ssh using the domain name, im able to reach the port without any kind of issues??
ssh email@example.com

Are you able to explain why this is happening? I was expecting different behaviour (DNS will resolve domain to IP, ssh will try to use that IP to reach the port and it will be blocked).
How i can fully block ssh access (tcp connection) on port 22, for other sources beside those 2 ips (no matter how they try to reach the port)?
Comment: What you're describing isn't possible.  There is something important you're not telling us.
Comment: https://meta.serverfault.com/q/963/126632
Comment: Having in mind that I don't have experience a lot with iptables, i thought I was missing something on that field. Confirmation that it should be blocked either way is fair enough. I will review things on my side and update this question accordingly.
Comment: @MichaelHampton Changed to example.com as suggested in the post you shared.
Comment: @womble Based on what i got after my investigation seems that this is possible scenario. The ssh is listening on IPV4 and IPV6 interface. Additionally DNS contains A and AAA records. When you are accessing to the server directly, it will use ipv4 and block things properly. If you are using the domain access from the client which support IPV6 as well, ipv6 will be used and access is granted. In this specific scenario at the end I used "ssh -6" to try to access to it directly via ipv6 and I was able to do it just fine. Was not aware about difference between ip6tables and iptables.
Comment: So the something important you're not telling us was that your server has IPv6 access.
Answer: The best rule for setting access to your server from a known IP is:
sudo iptables -A INPUT -p tcp -s IP-ADDRESS --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

And in case you wish to change your OUTPUT chain to DROP or REJECT, then you would need the following rule too:
sudo iptables -A OUTPUT -p tcp --sport 22 -m conntrack --ctstate ESTABLISHED -j ACCEPT

But your rule seems to be ok.
If you are connecting to a domain (domain.com for ex.) and reaching a ssh server, you should check if you have set a CDN or reverse proxy for the domain which doesn't have the iptables rule droping the connection. Also a firewall doing NAT could cause a response if that port is not forwarded and the firewall has the ssh service enabled, or anything in the middle between your client and server that has an ssh server running as a matter of fact. Most likely it is the ssh-server of another device, or you have something wrong with your network/setup.
Update after ipv6 comment
If you are using ipv6 you should check the ipv6 rules for iptables:
sudo ip6tables -L

You have, at least, 2 options:
1) If you are allowing everything, you can restrict to the ipv6 address of the ipv4 devices that you are allowing (notice the IPv6-ADDRESS) for the ssh service:
sudo ip6tables -A INPUT -p tcp -s IPv6-ADDRESS --dport 22 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT

2) Or directly DROP everything coming from IPv6 by default:
sudo ip6tables -P INPUT DROP
sudo ip6tables -P FORWARD DROP

Comment: That is the first thing i checked. To be sure that im trying to access to the proper server and that something is not happening in between. Basically, auth logs on server side are indicating that im hitting proper server with ssh. But i noticed that,  access via domain is available only if ipv6 address is used. Is it possible that maybe that is the issue?
Comment: It might be. I have extended my answer so you can check if that is the case.
Comment: I resolved this issue by removing ipv6 for ssh (updating ssh configuration and specifying address family to "inet"). It would be great if you can add that info into the answer as well. Marking this answer as accepted having in mind that includes info necessary to overcome the issue i mentioned in the question.


Detecting image width via JavaScript when maxWidth is used

Question: Consider this code:
var img = new Image();

img.onload = function() {
    console.log(this.width);
};

img.src = 'http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/800px-Gull_portrait_ca_usa.jpg';

document.body.appendChild(img);

This will print out the image width (800) correctly. But if I apply a max-width using CSS:
img {max-width: 400px}

Test case: http://jsfiddle.net/MSjnM/
The JS code above will print out 400 instead. That is a bit confusing, as one would think that the width attribute represents the original image width, not the computed width.
Now to something even more confusing, if I append the image in the onload event after the width detection I get a different result:
var img = new Image();

img.onload = function() {
    console.log(this.width);
    document.body.appendChild(img);
};

img.src = 'http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Gull_portrait_ca_usa.jpg/800px-Gull_portrait_ca_usa.jpg';

Test case: http://jsfiddle.net/MSjnM/2/
This will print out 800 instead, even though the CSS has applied. I assume it's because the image is appended after I detected the width and that the max-width will be applied as soon as the image is inserted into the DOM.
OK, so if I want to get the original image size, no matter when or if the IMG element is inserted in the DOM or whatever CSS styles has been applied, how would I do that fail-safe?
Comment: using Fx11 and Op11.61 both your fiddle print "400".
Comment: @FabrizioCalderan I was testing in Chrome, but that is even more alarming...
Comment: OK I changed the code slightly to get the same effect across browsers, thanks Fabrizio.
Answer: Load the image independently into a new Image object and get the width there:
var img = new Image();
img.src = 'http://placehold.it/350x150'
console.log(img.width);

Comment: Sidenote: the image should be in cache by now, so no extra request / performance loss here.
Comment: This is not reliable in Safari. Also, it doesn't work if the image is present in the DOM and a max-width has been applied.
Comment: @David Thought: is it only unreliable in Safari because you have a CSS rule making all ``s have a max-width? Surely even if it is applying the CSS rule to an `Image()` object not in the DOM, so long as you can make the CSS rule more specific, then there won't be a problem?
Answer: For me:
in IE9, Opera, Safari(PC), FF and chrome the naturalWidth/naturalHeight-properties return the desired values
http://jsfiddle.net/MSjnM/5/
Comment: nice, +1: in which IE version is supported?
Comment: IE9 as I wrote. In IE8 width returns 800 for me.


In Jsp,what's the difference between ${param.name} and ${param[name]}

Question: In Jsp,what's the difference between ${param.name} and ${param[name]}?
I've tested that they can both get the parameter I set from a servlet,but I want to find out the difference between them.e.g,in what sittuaton,one can work while another one doesn't?
Thanks in advance.
Comment: If `name` is a String variable with the value `"name"`, there is no difference. If `name` is `"foo"`, then `${param[name]}` is the same as `${param.foo}`.
Comment: @shmosel Thanks,I just can't find the relevant information and think there may be a difference:)
Answer: All the following ways are evaluated same in standard JSP EL:
${param.myvar}
${param[name]}  // here `name` is another EL variable with value `name = "myvar"`
${param["myvar"]}
${param['myvar']}

If you're referring to a variable, then you can use 2nd one. Otherwise if you're directly accessing through value, then you can use 3rd or 4th one.
Comment: And therefore there *is* a difference, since #2 is different. You even said so yourself in the comment.
Comment: Thanks for notifying @Andreas. I have edited accordingly.
Answer: According JSP specification:

The EL follows ECMAScript in unifying the treatment of the . and [] operators.

    expr-a.identifier-b is equivalent to expr-a["identifier-b"]; that is, the identifier
    identifier-b is used to construct a literal whose value is the identifier, and then the []
    operator is used with that value.


WSO2 adding permanent datasource configuration

Question: I configured a Postgresql Datasource on WSO2 admin panel but when I restart the WSO2 server, I cannot access the new configuration.

I tried to configure it in master-datasources.xml file, but nothing has changed. When I restart the server I cannot find the new conf in Datasources in WSO2 admin panel. I want the added configuration be permanent.
WSO2 version 6.4.0
Thanks for any idea!
Comment: Did the configuration succeed (are there no error in the log)? Are you not overwriting the configuration at the start? (e. g. in docker)
Comment: @gusto2 yep. Connection was healthy and I received the logs.
Comment: It seems there is identified issue similar to this.  Check whether the github issue is related. https://github.com/wso2/product-ei/issues/2772
Comment: @BK. I have the same issue. Did you resolve this issue? Could you please talk with me the solution? Thank you so much.
Comment: @DuongAnh Hi, I resolved the problem with editing master-datasources.xml file.
Answer: You can edit your custom-datasources.xml instead of master-datasources.xml. And make sure you have read access to this files when you save them.
Sometimes, we use root access with FTP and we overwrite these files
Comment: Not necessary. It can be a permission problem, or a default inside the master-datasources.xml. And it's a bad habit to put custom datasource inside "master-datasources".
Comment: If the datasources which added from master-datasources.xml does not appear, then the same will be applied to custom-datasources.xml file as well.