Probability of line intersecting with randomly scattered spheres.

Question: We have a very big (as in: a unit volume is negligibly small compared to it) volume V, randomly (uniformly and independently) filled with points. The points are the centers of spheres of radius r. The spheres do not overlap (Yes, that means depending on r some points need to be redrawn, and approaching the packing limit the placement will look less and less random). The space is filled to a mean density (spheres' volume to unit V) d.
I'd say a point P, randomly placed inside the volume, has probability d of being inside a sphere.
What about a randomly placed line segment of unit length? What is the probability P(r,d) of it intersecting a sphere at any point?
Comment: Infinite volume with uniform probability distribution has some theoretical difficulties. As in, it cannot be done.
Comment: @Arthur ok. How about a very big volume (very big as in a unit volume is negligible?)? --- And can you give me some references about the problem?
Comment: Yes, that would work
Answer: Rough answer for the low density case (too long for a comment).
Let $B(r,x)$ denote the sphere of radius $r$ centered at point $x$.  Let $C$ be the set of centers of the spheres.
The point $P$ is inside $B(r,c)$ for some $c \in C$ iff $B(r,P)$ contains some $c \in C$.  We're saying this prob $= d$.
In the low density case, my guess is that the prob of any region $\Sigma$ (sphere, cube, torus, whatever) containing some $c \in C$ is proportional to the volume of $\Sigma$, as long as this volume $\ll$ the total volume $V$.  This is because:

Let $X = $ the number of $c \in C$ inside $\Sigma$.  By linearity of expectation, $E[X]$ is proportional to the volume of $\Sigma$.  (This is true whether density is high or low, and also regardless of the shape of $\Sigma$.)
In the low density case and when $\Sigma$ is much smaller than the total volume, the prob that $2$ or more $c \in C$ are inside $\Sigma$ approaches $0$.
Therefore, $X$ is actually $\{0,1\}$-valued, i.e. it is an indicator variable, and $E[X] = Prob(X = 1) = Prob(X \ge 1) =$ prob that some $c \in C$ is inside $\Sigma$.

If you believe everything above :) then the answer can be calculated as follows.  Consider a line segment $L$ of length $l$, and define $\Sigma$ to be the region of distance $\le r$ from the line segment.  This region is a cylinder of radius $r$ around $L$, plus two half-spheres at either end.  Its volume is $V_\Sigma = {4\over 3} \pi r^3 + l \pi r^2$.
Meanwhile, in the point $P$ case, we know there is prob $d$ that $B(r,P)$ contains some $c \in C$.  Volume of $B(r,P)$ is of course ${4\over 3} \pi r^3$.  We're claiming the probabilities are proportional to the volumes, so:

$L$ intersects $B(r,c)$ for some $c \in C$ iff $\Sigma$ contains some $c \in C$
${P(\Sigma \text{  contains some  } c \in C) \over P(B(r,P) \text {  contains some  } c \in C)} = {Volume\ \ of\ \ \Sigma \over Volume\ \ of\ \ B(r,P)} = {V_\Sigma \over {4\over 3} \pi r^3}$
$P(B(r,P) \text {  contains some  } c \in C) = d$
So: $P(L \text{  intersects  } B(r,c) \text{  for some  } c \in C) = P(\Sigma \text{  contains some  } c \in C) = d \cdot {V_\Sigma \over {4\over 3} \pi r^3}$.

This approach does not work well for the high density case, because in that case $X$ has a non-negligible prob of having values $> 1$.  So it is no longer an indicator variable and $E[X]$ overestimates $Prob(X \ge 1)$.  As a result, the value calculated above will be an upper-bound only... and often the upper-bound is useless (too loose).  For the high density case, this method does not provide a lower bound (beyond the obvious lower-bound of $d$).
E.g. the densest regular sphere packing (in 3-D) has $d \approx 0.74$, and a dense "random" packing has $d \approx 0.64$ according to wikipedia.  If the line segment has length $l=\alpha r$, then $V_\Sigma = ({4 \over 3} + \alpha) \pi r^3$ and the ratio of volumes $=({4 \over 3} + \alpha) / {4 \over 3} = {4 + 3\alpha \over 4}$.  So the upperbound will be:
$$P(\Sigma \text{  contains some  } c \in C) \le d \cdot {4 + 3 \alpha \over 4}$$
For $l=r$ (i.e. $\alpha = 1$), this bound $> 1$ for $d = 0.64$, making it a useless upperbound.  I.e. you can conclude the prob of intersect is between $d=0.64$ and $1$, but you already knew that!
However, for really short line segments, this still gives a non-trivial upper-bound, e.g. $l = r/10$ (i.e. $\alpha = 0.1$) and $d = 0.64$ gives an upperbound of  $0.688$, so you can conclude, for such a short line segment, the prob of intersect is between $d =0.64$ and $0.688$.
Comment: Thank you for your fine answer! the third item after "This is because" - could you have a look at that again? i do not understand it, and suspect it might suffer from wrong parantheses and/ or equalities?
Comment: If $X$ is a $\{0,1\}$-valued r.v., then $E[X] = 1 \cdot P(X=1) + 0 \cdot P(X=0) = P(X=1)$.  Also since it is $\{0,1\}$-valued, $P(X=1) = P(X\ge 1)$.  And of course $X \ge 1$ is exactly the event that some $c \in C$ is inside $\Sigma$.  Does this explanation help?  Would it help more if I point out that $P(X=1) = P(X \neq 0)$?
Comment: The idea of looking at it not as a line in a cloud of spheres, but as a sausage in a cloud of points is very helpful. I still have questions, though. $d = {V_{spheres} \over V_{tot}}$ , right? And shouldn't then $P($any c in $ \Sigma) = d * {V_\Sigma \over V_{spheres}} = {V_\Sigma \over V_{tot}}$. Also, for the densest packing, the spheres do not fill the volume, yet there is no free path anymore, so the probability of the line hitting a sphere is 1. ln that case d is still not 1, so wouldn't your result **under** estimate in that case?
Comment: you seem a little confused between spheres (all of them, with centers $c \in C$) and a single sphere (centered at $P$).  I have rewritten that part of the answer.  I have also expanded the discussion on the high density case.  hopefully my edits help!  if you have more questions, feel free to ask.


Brownian motion: changing the order of expectation and integration in $E \left( \int_s^t B_x dx \mid F_s \right)$

Question: Let $B$ be a standard Brownian motion with induced filtration $F$.
Is it true that, for $s<t$,
$$
E \left(  \int_s^t  B_x dx \mid F_s \right) =
\int_s^t E \left( B_x  \mid F_s\right) dx \;?
$$
To prooved this, I would use the dominated convergence theorem.
But I don't see any dominating function.
Viewing the integral as a standard Riemann integral, I think the result follows.
Answer: Write $B_x=B_x-B_s+B_s$. By linearity of conditional expectation and using the fact that $B_x-B_s$ is independent of $\mathcal F_s$, we are reduced to show that $$E\left(\int_s^t(B_x-B_s)dx\mid\mathcal F_s\right)=0.$$
It's enough to show that $\int_s^t(B_x-B_s)dx$ is independent of $\mathcal F_s$. To see that, write $\int_s^t(B_x-B_s)dx$ as the almost sure limit of $\frac 1n\sum_{j=0}^{n-1}(B_{s+xk/n}-B_s)$. If $X_n$ is independent of $Y$ for all $n$ and $X_n\to X$ almost surely, then $X$ and $Y$ are independent. Indeed if $O_1$ and $O_2$ are open,
$$P(X\in O_1,Y\in O_2)=\lim_{n\to +\infty}P(\bigcap_{j\geq n}X_n\in O_1,Y\in O_2)=\lim_{n\to +\infty}P(\bigcap_{j\geq n}X_n\in O_1)P(Y_n\in O_2),$$
and we conclude by a monotone class argument (I should introduce the set of probability one where we have the convergence).
Comment: I rarely see this kind of argument. I enjoyed reading it a lot. Thank you.


What is this time signature with only one number?

Question: I have recently come across a time signature I've never seen before. It is in a Baroque piece of music. Each bar has a minim crotchet minim crotchet which I think should indicate 6/4 compound time, but there is only one number in the time signature: 3.
What is this time signature?
Comment: Is there a question hidden in here somewhere?
Answer: Time signatures occasionally do away with the bottom number. While I tend to see this more often in modern music, the tradition goes back centuries (in fact, to before time signatures were actually a thing).
If I understand your question correctly, you have this:

Which tells you that there are three "beats" in the measure, and it's up to you to determine the denominator of the key signature. The above measure is equivalent to:

Perhaps a clearer notation would be:

Although one measure of 6/4 has the same note durations as 3/2, 3/2 has three large beats in the measure instead of the two beats that 6/4 has. (It's just like the distinction between 6/8 and 3/4.)
Since you're given a numerator of 3 telling you there are three beats in a measure, this is 3/2.
Comment: I have pondered the answer but am still a little in the dark. The tune is "Greensleeves to a Ground" so the 6/4 feel would be changed in 3/2 time.
Comment: @Colleen Hm, that is odd. Do you have a picture by any chance?
Comment: Here we go: https://drive.google.com/file/d/19jS4RdEqJ2yMt3IaoBlDiKd5bdnQxtaQ/view?usp=sharing


How should I go about choosing a kernel for this function?

Question: I'm playing around with GPR and thinking about kernels.  I am looking for a kernel that could do a good job of fitting this function.

It is something I drew up just to see how well GPR can do.  Its functional form is
$$ F(x) = -x^2 + 10 + 0.3\sin(4 \pi x) + \varepsilon$$
Where $\varepsilon \sim \mathcal{N}(0,0.5)$.
I'm doing things in python specifically sklearn, so I have a bunch of kernels at my disposal.  So far I have tried a sum of the exponential sine wave to account for periodicity and the radial basis function.  Is there any other way to account for the quadratic part of the function?
Comment: If you are just playing around why don't you just try out some and observe what difference it makes?
Comment: I assume your F(x) is the generating function?  Is so (I may be wrong) why not fit it using some optimization routine?
Answer: Hello have you tried the "Locally Periodic Kernels", these will work with periodic functions that slowly vary over time. You use a periodic kernel and add or multiply a local kernel, for example a RBF kernel  multiplied by a periodic kernel. This will allow one to model functions that are only locally periodic  the shape of the repeating part of the function can now change over time.
For example you can use a Periodic Kernel:
$k_{\textrm{Per}}(x, x') = \sigma^2\exp\left(-\frac{2\sin^2(\pi|x - x'|/p)}{\ell^2}\right)$.
Then multiply it by a local RBF kernel (LC):
$k_{\textrm{LocalPer}}(x, x') = k_{\textrm{Per}}(x, x')k_{\textrm{LC}}(x, x') = \sigma^2\exp\left(-\frac{2\sin^2(\pi|x - x'|/p)}{\ell^2}\right) \exp\left(-\frac{(x - x')^2}{2\ell^2}\right)$.


Installing Ubuntu guest crashes Hyper-V host

Question: I have a weird problem that I don't even know where to begin diagnosing.  Trying to install Ubuntu to a VM locks up the host system!
My setup is:

Dell R715 server, dual 16 core AMD opteron processors, 96GB RAM
Dell MD3600f SAN
Server 2008 R2 Datacenter
System Center VMM 2012

There are 5 windows virtual machines running that have had no problems.  This is the first linux VM I've tried to create.  I setup a VM through virtual machine manager, set the CD drive to a Ubuntu 12.04 server x64 iso, and started it up.
It boots up the normal ubuntu install menu, but the second I hit enter on "Install Ubuntu Server", I get disconnected.
The HOST machine stops responding to pings.  So do all virtual machines on it.  It locks up entirely - keyboard on the host won't work, mouse won't move, numlock light won't change.
There's no blue screen - the host is sitting at the login screen completely unresponsive.  I can't find any relevant logs in event viewer after rebooting.
What could cause the host machine to freeze like that?
It's not a one time occurrence - it happens every time at the exact same point.  Thank god this server isn't in production yet!
Update:
It doesn't seem to be a hardware problem.  I fired up a dozen extra virtual machines to see if I was maybe hitting a bad spot of RAM, but everything worked fine.
Ubuntu 10.04 installed with no issues.
When doing a do-release-upgrade from 10.04 to 12.04, it got part way through the upgrade before it threw the root filesystem into read only mode and started getting tons of IO errors.  Not a disk issue since I was able to run hard drive benchmarks and read/write large files before I started the upgrade.
Next things to test are 12.04 alternate CD, and 12.04 32bit and see if they show the same behavior.
Update 2
Well now this is fun...it was consistently freezing on Thursday and Friday, at least 20 times in the same spot.  Nothing changed over the weekend.  I'm the only one who has access to the server at the moment.  And today I can boot that ISO just fine without any crashes.
I hate problems that go away on their own.  I was hoping to narrow it down so I could at least know what not to do when it goes into production.
Answer: When you say Windows Updates are installed, are you referring to the Windows Update applet, or did you check for the Hyper-V hotfixes on the Microsoft web site?  Here are a few:
2550569 - 0x20001 Stop error when you start a Linux VM in Windows Server 2008 R2 SP1
https://support.microsoft.com/kb/2550569
2517329 - Performance decreases in Windows Server 2008 R2 when the Hyper-V role is installed on a computer that uses Intel Westmere or Sandy Bridge processors
https://support.microsoft.com/kb/2517329
2636573 - FIX The guest operating system may crash when you perform a live migration of Hyper-V vm in Windows Server 2008 R2
https://support.microsoft.com/kb/2636573
2263829 - The network connection of a running Hyper-V virtual machine may be lost under heavy outgoing network traffic on a computer that is running Windows Server 2008 R2 SP1
https://support.microsoft.com/kb/2263829
AMD-specific:
2568088  - Virtual machine does not start on a computer that has an AMD CPU that supports the AVX feature and that is running Windows Server 2008 R2
http://support.microsoft.com/kb/2568088
Comment: I do not have the first 2 installed.  Though they appear to only affect intel CPUs, I'll try installing them anyways and see if it makes a difference.  The 3rd and 4th ones are already installed.
Comment: Understood.  There is one specific to AMD, 2568088.  I edited the answer with that information.
Comment: 2568088 is definitely installed, since I ran into that issue where no VMs would start.
Comment: +1 I *really* have seen some of these issues with Hyper-V 2008 R2 in the past.
Answer:
What could cause the host machine to freeze like that?

Defective hardware, defective Drivers, defective OS.
Simple like that.
Start with checking all updates, that the drivers are current and that the hardware is good.
Comment: Windows updates are all installed.  When I installed the machine less than a month ago I ran it through Dell's built in diagnostics, a weekend of memtest, and after installing windows a couple days of running many copies of Prime 95.
Comment: You could always open a MS PSS SUpport case. Yes, Ubuntu is not supported, but the Hyper.Visor freezing means in a public cloud some idiot customer so to say yould shut down your servers - not support, it should NOT shut down the hyperviror, Period. If that is not a driver issue, MS may WANT to investigate that.
Comment: Well, ok - anyhow, issue here being: whatever you do in a VM it should NOT NEVER EVER crash the hypervisor. This IS a bug. Even if I try to load something totally unsupported, the hypervisor crashing is not acceptable.
Comment: Definitely a bug, we've got Solaris, among other unsupported OS's running in Hyper-V and I've never seen an issue like this myself.
Comment: I may end up calling MS support.  I don't care if ubuntu doesn't work - it's unsupported, I get that.  I DO care if things running on the virtual machine can crash the host, intentionally or not.
Comment: @Grant Exactly my thinking. This is A series Critical issue. It totally invalidates the isolation of the virtual machienes and opens a DDOS possibility. BAD BAD BAD.
Answer: it could be a memory fault, starting up the VM guest using more memory, but if the host runs normally seems unlikely.
try removing unnecessary virtual hardware from the guest - i.e. disk, network cards etc, and see if you can simply boot off CD. then add each device back one by one to see if it eliminates the problem.
my guess is that it's a "thin" hv_vmbus driver in the guest which is breaking hyper-v, so you might be better off using the legacy network device rather than hv_netvsc for example.
Answer: I was getting this same thing and thought I would post in case you or anybody else is still having this trouble.
I don't recall which, but there were two settings that come to mind that would trigger this for me.

VM Properties - Processor - disable the different processor version option
VM Properties - HDD - disable contains operating system

Again, I don't recall which one it was, at this point I just disable both when I create a Linux VM.
Comment: Daniel Scott, thanks for posting your answer, would be great if you could make your answer more precise.
Comment: Can you test and determine exactly which one of the two was the problem?
Comment: I'd love to know for certain which of those causes it.  Unfortunately the system is now in production and people would...frown upon...me testing something that might intentionally crash it.  If you can test it great!
Answer: Ubuntu is not supported by hyper-v, only Suse, redhat and Centos are supported.
Not that this means it should crash your Hyper-v host, but still it's something to bear in mind, I've seen some older Linux OS' cause esxi 4 to have a hissy fit for a while, never seen it cause the hypervisor to keel over though.
According to this article http://www.theregister.co.uk/2012/04/26/ubuntu_12_04_lts/ Ubuntu have worked toward making their latest release hyper-v compatible, though microsoft haven't certified it yet, so in theory it stands a good chance.
Have you tried the alternative installer disk? This is available from http://releases.ubuntu.com/precise/ubuntu-12.04-alternate-amd64.iso and uses a different installer to the default iso.
Comment: Alternative install might work.  For the moment I tried installing from a 10.04 disk, and that worked perfectly.


Carregar checkbox explode

Question: Fiz um código com explode para carregar checkbox do banco, porém, ele só está carregando quando contém dados na exata ordem das variáveis, por exemplo 'Teatro 1, Teatro 2' carrega, mas 'Teatro 1, Baby Class' não carrega nenhum...
Embora eu tenha projetado para apenas duas opções fiz um teste com um insert de três, fora de ordem: 'Teatro 2, Teatro 1, Dança 1', carregou apenas o 'Dança 1'
 <?php $Oficina == $Oficina_Explode;
 $Oficina_Explode = explode(', ', $Oficina);
 $Oficina_Explode[0]; //ganha o valor 'Teatro 1'
$Oficina_Explode[1]; //ganha o valor 'Teatro 2
$Oficina_Explode[2]; //ganha o valor 'Dança 1'
$Oficina_Explode[3]; //ganha o valor 'Dança 2'
$Oficina_Explode[4]; //ganha o valor 'Baby Class'
?>
    <meta charset="utf-8">
    Teatro 1 <input class='shared' type='checkbox' value="Teatro 1" name="Oficina[]" required="required" <?php if($Oficina_Explode[0] == 'Teatro 1') echo 'checked';?> />
    Teatro 2 <input class='shared' type='checkbox' value="Teatro 2" name="Oficina[]" required="required" <?php if($Oficina_Explode[1] == 'Teatro 2') echo 'checked';?> />
    Dança 1 <input class='shared' type='checkbox' value="Dança 1" name="Oficina[]" required="required" <?php if($Oficina_Explode[2] ==   'Dança 1') echo 'checked';?> />
    Dança 2 <input class='shared' type='checkbox' value="Dança 2" name="Oficina[]" required="required" <?php if($Oficina_Explode[3] == 'Dança 2') echo 'checked';?> />
     Baby Class<input class='shared' type='checkbox' value="Baby Class" name="Oficina[]" required="required" <?php if($Oficina_Explode[4] == 'Baby Class') echo 'checked';?> />

Como posso incrementar meu código para resolver este problema?
Comment: Confirmou se o valor armazenado no array é realmente a string que deseja?
Comment: Fiz os testes, acabo de ver que não se trata de acento... ele só está carregando quando contém dados na exata ordem das variáveis, por exemplo 'Teatro 1, Teatro 2' carrega, mas 'Teatro 1, Baby Class' não carrega nenhum...
Comment: Se vc deu a entrada com  'Teatro 2, Teatro 1, Dança 1', o que te garante que $Oficina_Explode[0] vai  ter o valor "Teatro 1", $Oficina_Explode[1] vai ter o valor "Teatro 2"? Imagino que esteja faltando informação
Answer: Resolvido. Obrigado aos comentários que me ajudaram a chegar na solução.
Criei um if para verificar cada checkbox, e assim, preenche-los corretamente.
    <?php $Oficina == $Oficina_Explode;
 $Oficina_Explode = explode(', ', $Oficina);
 $Oficina_Explode[0]; //ganha o valor 'Teatro 1'
$Oficina_Explode[1]; //ganha o valor 'Teatro 2
$Oficina_Explode[2]; //ganha o valor 'Dança 1'
$Oficina_Explode[3]; //ganha o valor 'Dança 2'
$Oficina_Explode[4]; //ganha o valor 'Baby Class'
if(in_array('Teatro 1', $Oficina_Explode)){
 echo "Teatro 1 <input class='shared' type='checkbox' value='Teatro 1' name='Oficina[]' required='required' checked />";
}
else {
      echo "Teatro 1 <input class='shared' type='checkbox' value='Teatro 1' name='Oficina[]' required='required' />";
}
if(in_array('Teatro 2', $Oficina_Explode)){
echo "Teatro 2 <input class='shared' type='checkbox' value='Teatro 2' name='Oficina[]' required='required' checked />";
}
else{
      echo "Teatro 2 <input class='shared' type='checkbox' value='Teatro 2' name='Oficina[]' required='required' />";
}
if(in_array('Dança 1', $Oficina_Explode)){
echo "Dança 1 <input class='shared' type='checkbox' value='Dança 1' name='Oficina[]' required='required' checked />";
}
else
{
    echo "Dança 1 <input class='shared' type='checkbox' value='Dança 1' name='Oficina[]' required='required' />";
}
if(in_array('Dança 2', $Oficina_Explode)){
echo "Dança 2 <input class='shared' type='checkbox' value='Dança 2' name='Oficina[]' required='required' checked />";
}
else{
      echo "Dança 2 <input class='shared' type='checkbox' value='Dança 2' name='Oficina[]' required='required' />";
}
if(in_array('Baby Class', $Oficina_Explode)){
echo " Baby Class<input class='shared' type='checkbox' value='Baby Class' name='Oficina[]' required='required' checked />";
}
else{
      echo " Baby Class<input class='shared' type='checkbox' value='Baby Class' name='Oficina[]' required='required' />";
}
?>

Carregar checkbox explode


Sending specific traffic through VPN

Question: I am trying to configure a server to automatically copy some files to a remote machine. In order to make the copy work, I need to establish a VPN connection to the remote network. I can use rasdial.exe to establish the connection.
To avoid interfering with anyone else using the server, I would like to specify that only certain traffic should flow through the VPN. Ideally, this would be traffic to specific IP addresses.
A similar question exists for Cisco VPN. The answers to this question make me suspect that this may only be possible with actual hardware rather than software.
Is it possible to set up my machine to send traffic through a VPN opened using rasdial.exe? If so, how can I accomplish it?
Answer: If you're looking to send traffic through an IP for only a specific range of IP addresses, then you've got a relatively easy task that network routing was designed for.
Most consumer VPN connections set the default route to be the VPN's endpoint when it is activated. What you want to do is leave the default route entry pointing where it was. You'll then add specific entries for the subnet ranges that you're interested in accessing over the VPN.
Microsoft documentation for the Windows routing table is a good reference point. I'm not sure if you can setup the routes with rasdial beforehand, but you can definitely script the appropriate add / remove commands to trigger when the interface comes online.
Update from @graveyface's comment
Connection Manager Administration Toolkit has a way to define routing tables in your connection besides the default routing entry that a connection would otherwise have.
Comment: if you roll a connection with the CMAK kit, you can specify routes, etc.  Then all he would need to do is call the connection after installing it (CMAK creates an executable installer) with rasdial and terminate when he's done.

Sending specific traffic through VPN


Scrapy script , how to find specific keyword and return or print url

Question: Ok, so i have to edit this completely. I have got the script partially working, i can start it without any problem, and here is the script. The paste link is here: http://pastebin.com/SKa5Wh1h and you can see what i get in the cmd line. I'm sure the keyword that is being searched is in those links, because i tried other words too, but it's not downloading them.
import scrapy

import requests
from scrapy.http import Request

import scrapy
from FinalSpider.items import Page  # Defined in items.py

URL = "http://url.com=%d"
starting_number = 60000
number_of_pages = 100
class FinalSpider(scrapy.Spider):
name = "FinalSpider"
allowed_domains = ['url.com']
start_urls = [URL % starting_number]

def __init__(self):
    self.page_number = starting_number
def start_request(self):
    # generate page IDs from 1000 down to 501
    for i in range (self.page_number, number_of_pages, -1):
        yield Request(url = URL % i, callback=self.parse)

def parse(self, response):
    for link in response.xpath('//a[text()="Amount"]/@href').extract():
        yield Page(url=link)

Comment: It's unclear to me as to the exact issue you're experiencing. Can you provide some more concrete details?
Comment: Yes. I'm trying to combine some scripts that I'm finding on the internet to do the following task. Go to the website, find the specific keyword, which is for example "100", then just print out/save the url where it was found. I've tried to combine those scripts from the net to do this, but i keep failing because I do not understand the language. So, I posted here to see if somebody can direct me or knows the code that can do this. I'm just getting frustrated doing this since 4 days ago,and I have only gotten through maybe 50% of the project I'm working on. So I decided to seek help here.
Comment: @user5616520, If you want to get used to extract data from a webpage in scrapy, use the [scrapy shell](http://doc.scrapy.org/en/latest/topics/shell.html), there you can interactively play Selectors and XPaths. I highly recommend you to also install [Ipython](http://ipython.org/install.html) to have a more friendly experience.
Comment: Hi, memoselyk. I  will install lpython and poke around it But I don't need to extract any data per se. What i need is to just find the specific keyword and extract the url where it was found, because i have to check the urls manually. Yes, this is data extraction, but it's a lot more simple, I believe, than extracting different type of data. However, so far I don't know how to implement these two tasks in my code ..
Comment: Possible duplicate of [Using scrapy to find specific text from multiple websites](http://stackoverflow.com/questions/33989925/using-scrapy-to-find-specific-text-from-multiple-websites)
Answer: Here you are asking two things,

How to extract some element?

The xpath that you are providing, response.xpath('//100.00()'), is an invalid xpath expressions.
If you want to find an a tag with some subtring in the text, like  100.00 , the correct xpath would be '//a[contains(text(), "100.00")]'. Note the use of contains, if you have the exact text you could use '//a[text() == "100.00"]'.

What do you with the found element?

In Scrapy, it's customary to create an Item class that would hold the data you have scrapped, logically structured by the Fields you have defined.
So first, you create a Item subclass, with a url Field, and in your spider, return or yield a new instance of that Item with the field url set to the value you found in the page.
Putting all this together,
You have to create an Item, as shown here:
import scrapy

class Page(scrapy.Item):
    url = scrapy.Field()

Then, in your spider extract all the meaningful data from the response object. Look at the examples here to get a feeling. But in general your code will be like,
import scrapy
from myproject.items import Page  # Defined in items.py

class MySpider(scrapy.Spider):
    [...]

    def parse(self, response):
        for link in response.xpath('//a[text()="100.00"]/@href').extract():
            yield Page(url=link)

Comment: Hi. I have the exact text i need to be found. It's 100.00, so i need to use what you suggested " //a[text() == "100.00"]" ? But, what I don't understand is do i implement this in the main script,and how, or do i have to put this in the items.py script as well ?  And 2, I would like the found element to be saved in the form of urls. So for example out of 500 urls, if it finds the text in 10 urls to save those 10 urls. Is this too much work or even possible or it just seems to me complicated  because I just started learning all this.
Comment: @user5616520, I've added some sample code trying to provide you general guidelines. If you get stuck in the parsing, use the scrapy shell until you got working code that you can simply copy to the `parse` method.
Comment: This is the error msg i get @memoselyk,   http://pastebin.com/KsWKEjsB I can't paste it here, so can you tell me what I'm doing wrong ? I know that the module myproject.items doesn't exist. So this should be items.py , if i'm not mistaken . Actually , i don't think it should, because items is different.
Comment: Ok, so I finally managed, I think, to get this working. Well, at least i can start it without any errors, but this is what i get now. It doesn't crawl any page. Here is paste bin link http://pastebin.com/SKa5Wh1h


How to set paragraph margin when editing in CKeditor?

Question: I want to make a subtitle on a page, which needs to be centered and to have no space between it and the title.
In the CKeditor window, I entered the subtitle as the first line of text and centered it. In source, CKeditor represented this as:

...

which results in a blank line above. I tried to remove the blank line by changing the above code to:

...

or just
...

But CKeditor didn't like either of those. It removed the center tag and replaced the
 with
.
I also tried to add an instruction in CSS as either:

...

or as:

...</style>

CKeditor also didn't like either of those and just removed the style attribute from the first one and removed the span tag from the second.
I'm not very conversant in CSS, so I'm not sure the above code is properly formed. But the problem is that CKeditor is just removing it so I don't even get a chance to troubleshoot errors in the CSS.
CKeditor seems determined to have that blank line there. How can I get it to let me adjust this spacing between paragraphs (not globally, but on an individual paragraph)?
Thank you.
Answer: In your example you have:

...</style>

I'm assuming that should be:

...

In general in terms of controlling the space between paragraphs I think the best way to do that is your CSS stylesheet, not really HTML.
Drupal and CKEditor do some HTML manipulation that you may want to checkout - admin/config/content/ckeditor, then choose your profile and click edit.
Under appearance you can select/deselect some plugins that you might not need.
In terms of a quick fix though I would think that this would work:

...

But it also depends on what is in your site's stylesheet and your theme.
Comment: Your proposed quick fix is the same as one of the things I tried, as posted in my question, except for adding the style to also center.
Answer: Seems like CK Editor is stripping out your tags.
Are you using filter html? if so, you should check on CK Editor's allowed tags in admin/config/content/formats/filtered_html and add the tags that you need.
If the above doesn't work, check the cleanup and output settings in admin/config/content/ckeditor/edit/Advanced
Comment: I was using "Full HTML" with all filters turned off. --- I've decided for now to stop using CKeditor or any other WYSIWYG device and just code my pages directly in HTML and CSS. Every time I save I get my page rendered (whether I want that or not) so I can check the results of what I've done. It seems that CKeditor is suited to allowing site visitors to add content to a site. But I'm not a visitor and I want total control of what I'm doing.
Answer: The question does not say if you're using the CKEditor module, or if you're using the WYSIWYG module (in which you then configure the CKEditor 'library').
I always use the WYSIWYG module (to leave the options open for a customer to pick another editor if they'd want, e.g. TinyMCE). If however I select the CKEditor library (using WYSIWYG module), then I've noticed that I must use the (older) 3.6.x version of the CKEditor, since WYSIWYG module seems to not yet support the 4.x version of CKEditor. That is: if I first try the 4.x version of CKEditor, then the WYSIWYG module doesn't find/enable it.
As per the comment "Seems like CK Editor is stripping out your tags." (in another answer), it appears to me that it might be worth trying the WYSIWYG module approach, together with CKEditor 3.6.x library. Another option might be, if you use CKEditor "module", to try to use the 3.6.x version of the "library" (I don't have experience with that combination though).
Curious to hear if this helps ...
Comment: Since CKeditor is to be in core in Drupal 8, it seemed to be the way to go, directly, not via the WYSIWYG module. See my comment to "No Sssweat" for the solution I've decided on for now.


Order (Bourbaki)

Question: Bourbaki define the order in the book "theory of sets" by :
Let $R(x,y)$ be a order relation if :
1) $R(x,y)$ and $R(y,z)\implies R(x,z) $
2) $R(x,y)$ and $R(y,x)\implies x=y $
3) $R(x,y)\implies R(x,x) $ and $R(y,y)$
Why we add the third condition ?
Thank you .
Comment: Not every set $x$ needs to be $R$-comparable to itself; but the third condition states that if $x$ is comparable to *anything*, then it is comparable to itself as well.
Comment: The third is reflexivity.
Comment: What do you mean by "why"? That's just how an "order relation" is defined.
Answer: The idea is that an order relation is a relation that behaves like $\leq$.  In particular, for any $x$, $x\leq x$ should be true (since it's $\leq$, not $<$).
Now you might ask, why does condition (3) say that if $R(x,y)$ then $R(x,x)$ and $R(y,y)$, rather than just saying $R(x,x)$?  The answer is that the relation $R$ doesn't make sense for arbitrary $x$.  For instance, if $R$ were the relation $\leq$ on real numbers, $R(x,x)$ isn't literally true for all $x$, only for $x\in\mathbb{R}$.  So condition (3) is written to say that for any $x$ or $y$ that the relation $R$ knows how to talk about (in particular, any $x$ or $y$ such that $R(x,y)$ is true), $R(x,x)$ and $R(y,y)$ are true.
A more modern definition would instead define an order relation $R$ on a set $X$ to be a relation $R\subseteq X\times X$ satisfying conditions (1), (2), and such that $R(x,x)$ for all $x\in X$.
Comment: There is a difference.  B's definition allows antichains to be empty.  Today's definition  does not allow empty antichains (except for the empty set).
Comment: The "more modern" definition is less general though, e.g. $R(x,y)\equiv x \subseteq y$ is a partial order a la the definition given in the question, but there is no set $X$ such that $x \subseteq x \iff x \in X$. Such an $X$ would be a "set" of all sets.
Comment: Well, if $R$ itself is not required to be a set, sure (but then you might as well similarly allow $X$ to be a class).
Comment: Agreed, but from the wording of the definition it seems likely that "relation" is being used in the logical sense and not as a set. Skimming through "Theory of Sets", it seems pretty clear that this is Bourbaki's intent (though I skimmed fairly quickly) and that the term "correspondence" is used for a set-theoretic relation.

Order (Bourbaki)


Java Input Needed Twice

Question: I'm having issues with an input validation code I have. I enter in my seed value it prompts me for, but afterwards I have to enter in another valid input in order to get my menu choice to print out. It happens every time I also choose one of the menu choices as well.
The input validation is supposed to take in integer values between 1 through 5 only. So if someone puts in letters, negative numbers, or integers not within the range, it asks them to put one in so that they can continue with menu choices.
Am I writing the code wrong? I want to get rid of that unnecessary second input and just only need one in order to continue with game. I also notice values after 10 works for some reason, even though 6-9 are counted as invalid. I'm not sure why that works. Thanks!
import java.util.Random;
import java.util.Scanner;

public class Sonowah
{

    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter a seed value [integer]: ");
        //assume input is integer for seed for RNG
        int seed = in.nextInt();
        Random rng = new Random(seed); //RNG

        int charge = 100;

    //loop here
    while (charge > 0)
    {
        int number;
        do
        {
            while (!in.hasNextInt(5))
            {
                System.out.println("Please enter valid interger");
                in.next();
            }
            number = in.nextInt();
        }
        while (number <= 0);
        System.out.println("Interger is valid!");

            System.out.println("1. Surrender");
        System.out.println("2. Defend");
        System.out.println("3. Fight");
            System.out.println("4. Charge");
            System.out.println("5. Potions");
            String menuchoice = in.next();

            if (menuchoice.equals("1"))
            {
                    statement
                }

Comment: Where is the rest of `main` ?
Comment: When posting code it helps to post the whole file/class so we can provide more informed answers.
Comment: The OP posted enough info. Actually, too much!
Answer: I don't think your loop should be using in.hasNextInt()...you already ask the user for a number, just check if that number is between 1 and 5. Scanner.hasNextInt(5) asks the user for an int and then tries to interpret it in radix 5. See the documentation
Comment: Hi. Thanks Tim and drew for the help! I didn't realize it was not needed. The integers are working just fine now!
Answer: Here is your problem:
do
{
    while (!in.hasNextInt(5))
    {
        System.out.println("Please enter valid interger");
        in.next();
    }
    number = in.nextInt();
}
while (number <= 0);

Basically, all of this code is useless in context of the rest of your program code.

Java Input Needed Twice


Quick floor function

Question: This isn't true, right?
$$k\left\lfloor\frac n {2k}\right\rfloor\leq \left\lfloor\frac n k\right\rfloor$$
$2<k\leq \left\lfloor\dfrac {n-1} 2\right\rfloor$, $n>4$, $k,n$ are coprime.
Comment: Those $k$ and $n$ do not meet the conditions provided
Answer: As with your previous question one check is what happens at the numbers grow.  If $k$ is as large as possible (for a given $n$) the right side will be $2$.  The left side will be about $\frac n2$, which can be large.  Take, for example, $n=101,k=50$.  Then the left is $50$, the right is $2$.  Often it helps just to ignore the floor function for a preliminary investigation.
Answer: Let $n=15$ and $k=4$. Not that $15>4$, $2<4<\left\lfloor\dfrac{15-1}{2}\right\rfloor=7$, and that $gcd(4,15)=1$
Now
$$k\left\lfloor\dfrac {2n} k\right\rfloor=4\left\lfloor\dfrac {30} 4\right\rfloor=28$$
and
$$\left\lfloor\dfrac n k\right\rfloor=\left\lfloor\dfrac {15}4\right\rfloor=3$$
Clearly $28>3$, so this provides a counterexample.
Comment: SHOOT! I meant to put the 2 in the denominator, sorry!

Quick floor function


How can I name a geometrical entity similar to a plane but with finite length and width?

Question: I was considering just using the name rectangle for representing the set of points contained in a 3D plane for a given rectangular area.
I would like to know whether there's a more appropriate name. If I understood correctly, by definition, that would not be a finite plane.
UPDATE
I know the difference between disk and circle: the disk's set of points includes points inside the circular region.
Is there something equivalent for rectangles?
Comment: In my experience, a circle *is* the set of points on the circumference. The set that also includes points in the interior is called a *disk*.
Answer: It's fine to use rectangle; the context should make it clear whether you're talking about the rectangular region itself or just the perimeter (and this sentence shows how to distinguish the two if necessary).
Answer: You might want to take refuge to attributes here. Then you could distinguish between an "open rectangular region" and a "closed rectangular region". And sure there is the "rim of that rectangular region".
Wrt. some finite 2D fabric you also could take refuge alongside to the greekish term "polyhedron", i.e. some solid having many (poly) seets (hedra). Accordingly a finite 2D fabric might well be termed some "hedrix". - Cf. here also the Polygloss of Wendy Krieger, http://os2fan2.com/gloss/pglossh.html, which was piled up right for the purpose to dig out the ethymological stems of terms and to base dimensional extrapolations of those in a ethymological correct sense.
--- rk
Answer: The thesaurus suggested slab, pane, and lamina; among others.


how to debug google map on release version

Question: I'm having an issue with the release version (map is blank) I have tried everything such as building different sha for debugging and release even different key for debugging and release but still nothing, the code is working fine in debug mode but it doesn't work in a live version on google play store.
when I sent mail to tech support they asked me to debug and see the response

You should be able to debug your own app, please note that my scope of
support is limited to the Google Maps APIs, downloading your app and
debugging it is done as best-effort support.
Comment: have you check with realse mode and please check some time user used his kye in debug mode in android
Answer: I recommend you to check the Maps API key, you have to double-check if you inserted the API key for the release version.
Comment: is theres way to debug or listen to google map response on release version ?
Comment: You can build a release version and test it on your device.
Answer: i found easy way to get the correct sha key if the app is already published on google play store go to: https://play.google.com/console than select your app and inside you will find tab called integrity open it and copy SHA-1 certificate fingerprint from there.


Add own field to Admin Sonata

Question: I would like make log system for many Entity. For example i have Entity: Blog, Page and News. They are admin class:
class (Blog|Page|News/)Admin.php
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('title', null, array())
                ->add('body', null, array())
            ->end();
    }
}

I would like also Entity Log. This has fields: log, description and date. I would like add field description to Blog, Page and News:
class (Blog|Page|News)Admin.php
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->with('General')
                ->add('title', null, array())
                ->add('body', null, array())
                ->add('description', 'text', array())
            ->end();
    }
}

but this return error - this field not exist in Entity (Blog|Page|News). I can add setDescription, getDescription etc to Entity, but this is wrong way if i have a lot of entity to logging and i will have all logs in one table (Entity Log), not in all Entities - (Blog|Page|News). I use preUpdate to save this data to table Log, but i dont know how i can add field description from Entity Log to these Entities. I can also make relation but this is also wrong way.
Maybe i should use extends class? But how?
Answer: You can add your field in sonata admin ..
It's right way to add optional filed in sonata admin form
     $formMapper
        ->with('General')
            ->add('title', null, array('mapped'=>false))
            ->add('body', null, array())
            ->add('description', 'text', array())
        ->end();

Add 'mapped'=>false in third argument
Comment: you have to spécify the type now -> like this : ->add('title', 'text', array('mapped'=>false))
Answer: shouldn't optional fields have 'required' set to false
protected function configureFormFields(FormMapper $formMapper){
//
->add('title', 'text', array('mapped'=>false, 'required'=> false))
//
}

Add own field to Admin Sonata


Using not() in XPath

Question: I would like how to use the "not" in XPath properly. I just can't seem to get it to work with attributes.
Say I have this expression: //*[@name = 'Bob'] It is valid, and will return all nodes that have a name attribute equaling 'Bob'.
Now if I want all nodes that have a name attribute that do not equal 'Bob', I need to use an XPath such as: //*[@name not(='Bob')] but this is invalid.
I have tried multiple combinations with not() being placed in a different order, but I can't seem to get this to work. Could someone please inform me how to use not() properly?
Also, does the order change when using elements instead of attributes? Such as: //name[text() = 'Bob']
Thanks!  :)
Answer: According to : http://msdn.microsoft.com/en-us/library/ms256086.aspx, have you tried
//*[@name != 'Bob']

Comment: @mathieu: I would be better to quote the specs (not MS documentation), from http://www.w3.org/TR/xpath/#booleans *"If one object to be compared is a node-set and the other is a string, then the comparison will be true if and only if there is a node in the node-set such that the result of performing the comparison on the string-value of the node and the other string is true"*
Comment: @mathieu: This method works for numerical values but not for string values. //*[@numerical != 1] works. //*[@string != "someString"] does not. Only the root element is being returned!
Comment: @Alejandro: I did, but it only works for numerical values, not string values like I mentioned above. With the string value, the only result I get is the root node.
Comment: @iHeartGreek: This works. Other order comparison operators (like >) only works for numbers (in XPath 2.0 you could use `op:gt` that works with collations).
Comment: @Alejandro: I don't know what the problem could be then.. it doesn't work in the implementation I have. What could be possible sources of the problem?
Comment: @iHeartGreek: Ask other question with input sample, desired output and in progress stylesheet, and someone could answer that to you. Meanwhile, you should mark this as answer, I think.
Comment: @Alejandro: ok, I selected it as the answer. I will try to ask another question next week.
Comment: @Alejandro: I think there is an issue with my XSL code, but it only became apparent to me once I used !=. I did some further testing, and found the searches do not go through entire hierarchy. I posted a question as I still can't fix it. http://stackoverflow.com/questions/3451325/xpath-results-from-xslt-are-incorrect-not-going-through-entire-xml
Answer: Try
 //*[@name != 'Bob']

or
  //*[not(@name = 'Bob')]

should work both.
Comment: Note that `//*[not(@name='Bob')]` will also match elements without a name attribute: since the `@name` node-set will be empty, `@name='Bob'` will be trivially false, and `not(@name='Bob')` will be trivially true.
Comment: This method works for numerical values but not for string values. //*[@numerical != 1] works. //*[@string != "someString"] does not. Only the root element is being returned!
Comment: I am pretty sure that this will work for strings. Sure you did not make an error, for example, wrong case? The comparison is case-sensitive.
Comment: yes I tried various searches. I am only getting the root node returned. :S
Comment: Please post a complete example, a small XML file and the XPATH you are really trying, then we shall see further.
Comment: I think there is an issue with my XSL code, but it only became apparent to me once I used !=. I did some further testing, and found the searches do not go through entire hierarchy. I posted a new question as I still can't fix it. http://stackoverflow.com/questions/3451325/xpath-results-from-xslt-are-incorrect-not-going-through-entire-xml
Comment: Thanks for this suggestion.  For some reason != wasn't working, but not() does.  I was trying to hide the header row in a table with a certain class.  The former returned no rows at all, the latter returned the non-header rows.
Answer: //*[@name and @name != 'Bob']

Comment: Doesn't this give all elements whether they have @name or not? I need it to be an element with @name and then have it not equal 'Bob'
Comment: I think just //*[@name != 'Bob'] would work.. as others suggested.

BUT This method works for numerical values but not for string values. //*[@numerical != 1] works. //*[@string != "someString"] does not. Only the root element is being returned!
Comment: Sorry, it is my understanding that your solution does in fact work. I think there is an issue with my XSL code, but it only became apparent to me once I used !=. I did some further testing, and found the searches do not go through entire hierarchy. I posted a new question as I still can't fix it. http://stackoverflow.com/questions/3451325/xpath-results-from-xslt-are-incorrect-not-going-through-entire-xml

Using not() in XPath


How can I view the SQL Server database created by an ASP.NET MVC web application?

Question: I have an ASP.NET MVC web application deployed to IIS on a Windows Server 2012 machine.  When the application first starts, it will create the database and tables it needs if necessary.  This all works fine, and now I want to do some administration to the database through SQL Server Management Studio.  However, when I open up that tool I only see the System Databases.  What do I need to do in order to view the database created by the web application?
Some details that may help:
The application uses the following connection string in Web.config:
<add name="DefaultConnection" connectionString="Data Source=.\SqlExpress; Initial Catalog=Foo.Dashboard;Integrated Security=SSPI;User Instance=true" providerName="System.Data.SqlClient" />

In IIS, the application pool is set to run with the identity LocalService and the Load User Profile setting is True.  On Sql Server Express, we added a login for the Local Service account and gave it the dbcreator role.  When the application started up, it was then able to create the database and tables, and everything appears to be functioning.
I then connect to the server with remote desktop and open SQL Server Management Studio.  To connect to the database, I enter .\SQLEXPRESS as the server name and authenticate with Windows Authentication.  I then connect and only see the System Databases.  I've added all the possible database roles to my login, and I see that I have the VIEW ANY DATABASE permission in the Effective tab.
Answer: User Instance=true

It's because it's running as a user instance. You might be able to connect to it using the details here
Alternatively, if you remove the User Instance=true from the connection string, if you're using EF code first then this should cause a new DB to be created on the SQL instance proper. Bear in mind that the tables will be empty and you'll need to move any data over from your user instance, assuming there is any.
Comment: One thing that confuses me, if I login to Management Studio with an account that has been given all roles, I would expect it to be able to view and manage all databases.  Why is that not the case?
Comment: Because when you fire up Management Studio and connect to `.\SQLEXPRESS`, you're looking at the machine instance. User instance DBs will not appear here.


Issues in testing a simple linear relationship. Collinearity? Misspecification? Any other insight?

Question: I have a theoretical model saying that Y should be equal to:
Y = X + c * (W - X) + (Z1 - Z2),
where c is a given constant.
Here, it may be important to say that X is measured with error.
Someone proposes to test that assumption by estimating a linear model:
Y = b0 + b1 * X + b2 * c * (W - X) + b3 * (Z1 - Z2) + u,
and testing that b0 = 0, toghether with b1 = b2 = b3 = 1.
In my concrete estimation, I found that the estimators of b0 and b1 are highly correlated, about -0.80.
When I include the intercept, the estimate of b1 is about 0.75, significantly different from 1.
When I do not include the intercept, the estimate of b1 is very close to 1, 1 being in the corresponding confidence interval at 95%, and thus validating part of the story.
Regardless of the intercept, all the other coefficients are never close to 1, so that I am constantly rejecting the null hypothesis of b0 = 0, b1 = b2 = b3 = 1, altogether.
If I reformulate:
Y2 = Y - c * W - (Z1 - Z2),
such that:
Y2 = (1 - c) * X,
when I estimate Y2 = b1 * (1 - c) * X without intercept gives me an estimate of b1 very close to 1, with 1 being in the interval.
Any guess of why?
I am tempted to present results from the second model, which estimate only 1 coefficient, and the results are in line with expectations.
However, I am not really seeing the reason why the results from the two models differ so much, and especially why the estimates from the first model are so far from the theory.
Any insight? :-)
Thank you very much!
Comment: Not including the intercept is rarely needed, useful, or insightful, and appears to be the source of your questions.  Could you therefore explain why you are looking at any models without the intercept?
Answer: In the first regression, the third term $c*(W - X)$ has $X$ as a component.  This will make the second and the third term correlated. Consequently, the OLS estimates of the coefficients will be inaccurate (i.e. large standard errors).
The regression can be rewritten as
$$ Y = \beta_0 + \beta_1X^{*}+\beta_2W^{*}+\beta_3Z^{*} + \epsilon$$
where
$X^{*} = (1-c)*X$, $W^{*} = c*W$, $Z^{*} = Z_1 - Z_2$. This formulation avoids the correlation issue.  The validity of the theoretical relationship can then be tested as $H_o: \beta_1 = \beta_2 = \beta_3 = 1$.
But the above formulation is workable if there is no measurement error.  Since you mentioned $X$ is measured with errors, what is actually observable is $\tilde{X}$, where  $\tilde{X} = X + \eta$ and  $\eta \sim  N(0, \sigma_x)$. The errors-in-variable regression is then
$$ Y = \beta_0 + \beta_1X^{**}+\beta_2W^{*}+\beta_3Z^{*} + \gamma$$
where
$ X^{**} = (1-c)*\tilde{X}$ and $\gamma = \epsilon - (1-c)*\eta$.  This is where it gets complicated because now $X^{**}$ is correlated with $\gamma$. OLS will be biased in this case. Please refer to this reference for the details on errors-in-variable analysis.


iOS - Friendly NSDate format

Question: I need to display the date of posts in my app to the user, right now I do it in this format: "Fri, 25 May". How would I format an NSDate to read something like "2 hours ago"? To make it more user friendly.
Comment: possible duplicate of [Smart Formatting of time span](http://stackoverflow.com/questions/5741952/smart-formatting-of-time-span)
Comment: check [this answer](http://stackoverflow.com/a/34359788/1106035) which has sample link.
Answer: Take a look at FormaterKit https://github.com/mattt/FormatterKit
Created by mattt who also created AFNetworking.
Answer: NSDateFormatter can't do things like that; you're going to need to establish your own rules. I guess something like:
- (NSString *)formattedDate:(NSDate *)date
{
     NSTimeInterval timeSinceDate = [[NSDate date] timeIntervalSinceDate:date];

     // print up to 24 hours as a relative offset
     if(timeSinceDate < 24.0 * 60.0 * 60.0)
     {
         NSUInteger hoursSinceDate = (NSUInteger)(timeSinceDate / (60.0 * 60.0));

         switch(hoursSinceDate)
         {
              default: return [NSString stringWithFormat:@"%d hours ago", hoursSinceDate];
              case 1: return @"1 hour ago";
              case 0:
                  NSUInteger minutesSinceDate = (NSUInteger)(timeSinceDate / 60.0);
                  /* etc, etc */
              break;
         }
     }
     else
     {
          /* normal NSDateFormatter stuff here */
     }
}

So that's to print 'x minutes ago' or 'x hours ago' up to 24 hours from the date, which will usually be one day.
Answer: I wanted a date format like Facebook does for their mobile apps so I whipped up this NSDate category - hope it is useful for someone (this kind of stuff should really be in a standard library!) :)
https://github.com/nikilster/NSDate-Time-Ago
Answer: There are about a million ways you could do this, but here's a quick one:
NSString* hoursAgo = [NSString stringWithFormat:@"%.0lf hours ago", fabs([date timeIntervalSinceNow] / 3600.0)]

Of course, this doesn't check that date is actually from the past, doesn't do anything but hours, etc. But, you probably get the idea.
timeIntervalSinceNow returns how many seconds have passed since a given date, with positive numbers being a date in the future and negative numbers being a date in the past. So, we get how many seconds have passed, divide it by 3600 seconds in an hour to compute the hours that have passed, and then put its absolute value into the string "n hours ago".
Answer: Here is a pretty good answer this will take in seconds since the epoch(Jan 1, 1970) and return you a nice formatted string like '3 minutes ago'. Simply call it with your date object like so:
[timeAgoFromUnixTime:[myDateObject timeIntervalSince1970]];

+ (NSString *)timeAgoFromUnixTime:(double)seconds
{
    double difference = [[NSDate date] timeIntervalSince1970] - seconds;
    NSMutableArray *periods = [NSMutableArray arrayWithObjects:@"second", @"minute", @"hour", @"day", @"week", @"month", @"year", @"decade", nil];
    NSArray *lengths = [NSArray arrayWithObjects:@60, @60, @24, @7, @4.35, @12, @10, nil];
    int j = 0;
    for(j=0; difference >= [[lengths objectAtIndex:j] doubleValue]; j++)
    {
        difference /= [[lengths objectAtIndex:j] doubleValue];
    }
    difference = roundl(difference);
    if(difference != 1)
    {
        [periods insertObject:[[periods objectAtIndex:j] stringByAppendingString:@"s"] atIndex:j];
    }
    return [NSString stringWithFormat:@"%li %@%@", (long)difference, [periods objectAtIndex:j], @" ago"];
}

Answer: In newer versions of iOS since this question was asked, NSDateFormatter has had this ability added. It can now do it using the doesRelativeDateFormatting property.
Answer: +(NSString*)HourCalculation:(NSString*)PostDate

{
    NSLog(@"postdate=%@",PostDate);
    // PostDate=@"2014-04-02 01:31:04";
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormat setTimeZone:gmt];
    NSDate *ExpDate = [dateFormat dateFromString:PostDate];
    NSLog(@"expdate=%@",ExpDate);
    NSLog(@"expdate=%@",[NSDate date ]);
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSDateComponents *components = [calendar components:(NSDayCalendarUnit|NSWeekCalendarUnit|NSMonthCalendarUnit|NSYearCalendarUnit|NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:ExpDate toDate:[NSDate date] options:0];

//    NSLog(@"year=%d",components.year);
//
//    NSLog(@"month=%d",components.month);
//
//    NSLog(@"week=%d",components.week);
//
//    NSLog(@"day=%d",components.day);
//
//    NSLog(@"hour=%d",components.hour);
//
//    NSLog(@"min=%d",components.minute);
//
//    NSLog(@"sce=%d",components.second);
//

    NSString *time;

    if(components.year!=0)
    {
        if(components.year==1)
        {
            time=[NSString stringWithFormat:@"%ld year",(long)components.year];
        }
        else{
            time=[NSString stringWithFormat:@"%ld years",(long)components.year];
        }
    }
    else if(components.month!=0)
    {
        if(components.month==1)
        {
            time=[NSString stringWithFormat:@"%ld month",(long)components.month];
        }
        else{
            time=[NSString stringWithFormat:@"%ld months",(long)components.month];
        }
      //  NSLog(@"%@",time);
    }
    else if(components.week!=0)
    {
        if(components.week==1)
        {
            time=[NSString stringWithFormat:@"%ld week",(long)components.week];
        }
        else{
            time=[NSString stringWithFormat:@"%ld weeks",(long)components.week];
        }
       // NSLog(@"%@",time);
    }
    else if(components.day!=0)
    {
        if(components.day==1)
        {
            time=[NSString stringWithFormat:@"%ld day",(long)components.day];
        }
        else{
            time=[NSString stringWithFormat:@"%ld days",(long)components.day];
        }
    }
    else if(components.hour!=0)
    {
        if(components.hour==1)
        {
            time=[NSString stringWithFormat:@"%ld hour",(long)components.hour];
        }
        else{
            time=[NSString stringWithFormat:@"%ld hours",(long)components.hour];
        }
    }
    else if(components.minute!=0)
    {
        if(components.minute==1)
        {
            time=[NSString stringWithFormat:@"%ld min",(long)components.minute];
        }

        else{
            time=[NSString stringWithFormat:@"%ld mins",(long)components.minute];
        }
      //  NSLog(@"time=%@",time);
    }
    else if(components.second>=0){

       // NSLog(@"postdate=%@",PostDate);

       // NSLog(@"expdate=%@",[NSDate date ]);

        if(components.second==0)
        {
            time=[NSString stringWithFormat:@"1 sec"];
        }
        else{
            time=[NSString stringWithFormat:@"%ld secs",(long)components.second];
        }
    }
    return [NSString stringWithFormat:@"%@ ago",time];

}

This code will show you time in
------------sec like 2 sec ago
------------min like 2 mins ago
------------hours like 2 hours ago
------------days like 2 days ago
------------week like 2 weeks ago
------------month like 2 months ago
Lastly....
years like 2 years ago
:) try this
Answer: Adding to the solution try this more simplified method
    NSDateComponents *today = [[NSCalendar currentCalendar] components:NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:passed toDate:[NSDate date] options:0];
    NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:date];

    if (interval < 60) timestampString = [NSString stringWithFormat:@"%d seconds ago" ,today.second];
    else if (interval < 60 * 60) timestampString = [NSString stringWithFormat:@"%d minutes ago" ,today.minute];
    else if (interval < 60 * 60 * 24) timestampString = [NSString stringWithFormat:@"%d hours ago" ,today.hour];
    else timestampString = [NSString stringWithFormat:@"%d days ago" ,today.day];

iOS - Friendly NSDate format


verify text presence and visibility without locator in selenium webdriver

Question: I have written code to verify text and visibility WITHOUT locator. please suggest guys, is this correct method?
If there is any alternate method then your suggestion is appreciated.
public class TextVisibility {

WebDriver driver = new FirefoxDriver();
public static String baseUrl = "website url";
public String client = "your search string";

public void visibilityOfText(){

    driver.get(baseUrl);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    WebElement element = driver.findElement(By.cssSelector("body"));
    boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
    boolean feedbackVisible = element.isDisplayed();
    if(feedBack==true){
        System.out.println(client+ "is present");
        if(feedbackVisible==true){
            System.out.println(client+ "is visible");
        }
        else{
            System.out.println(client+ "is not visible");
        }

    }
    else{
        System.out.println(client+ "is not present");

    }

    driver.close();
    driver.quit();

}

}

Comment: To be honest, I don't understand what you are trying to achieve, did you get what they said in your yesterday's question? You accepted the answer and not using it? Do you realize `element.isDisplayed()` checks the visibility of the body element, not the text?
Comment: //this will show whether my text present or not (without using locator)
boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
// this will return whether present text is visible or not (without using locator)
WebElement element = driver.findElement(By.cssSelector("body"));
boolean feedbackVisible = element.isDisplayed();
Comment: WebElement element = driver.findElement(By.cssSelector("body"));

  will extract all elements inside body rit? then if body is visible then why not content inside body is visible?
Comment: my main motto is to verify whether text is present+visible without using locator. it help me to write test script before website design is ready.
Answer: I would do it little bit better. I will skip questioning why would you want doing it and assume you just need to do it.
So. The flaw is in this line:
boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);

because the text can in theory be something which is not on the page and then you will get NoSuchElementException
So better approach would be
//skipping initialization part
try{
  boolean feedBack = driver.findElement(By.cssSelector("body")).getText().contains(client);
  boolean feedbackVisible = element.isDisplayed();
} catch (NoSuchElementException e){
   boolean feedback = false;
   boolean feedbackVisible = false;
 }

if(feedBack==true){
    System.out.println(client+ "is present");
    if(feedbackVisible==true){
        System.out.println(client+ "is visible");
    }
    else{
        System.out.println(client+ "is not visible");
    }

}
else{
    System.out.println(client+ "is not present");

}

driver.close();
driver.quit();

}

}


How to see conda installed packages in Pipfile

Question: According to Pipenv doc (https://pipenv.readthedocs.io/en/latest/advanced/#pipenv-and-other-python-distributions), one can "reuse Conda–installed Python packages, use the --site-packages flag":
$ pipenv --python=/path/to/python --site-packages

When using the above command, the Pipfile did not get updated with the packages already installed in the conda environment specified in the --python flag.
1) Is this the expected behaviour of the command?
2) Is there a way to populate the Pipfile automatically?
Comment: Though not exactly what you are looking for, you can always run `pip list` inside your Conda environment, or `conda run -n myenv pip list`, or most simply `pipenv run pip list`.
Answer: As stated in the docs, the --site-packages flag is used for:

reuse Conda–installed Python packages

If you use it, the conda-installed packages will be available for you in the pipenv environment.
You can see that using pip freeze within your pipenv environment.
pipenv run python -m pip freeze

# installed packages will be listed - including conda-installed packages

If you don't use the --site-packages, you will get an empty environment.
In conclusion:

This will not update your Pipfile, and it is the expected behavior.
There is no automatic way to fill your Pipfile.


How to convert raw material into finished product such that the availability in days for all product will be maximum

Question: How can I distribute 5000 Jars into finished product. Such that the all three product will become 0 after particular days.
I had a problem where I want to convert my available raw materials into finished products. We have our own brand which is into female beauty product. There are multiple products in our brand and there are chances that the same raw material are used for different product with different label. For example, We can have multiple product having the same jar with different raw material in it.
Now let me explain my problem, I have 3 products that are having the same jar but the material filled in these jars and label are different. My finished/sellable product will be a complete product with jar, material and label on it.
Next we are having Daily Run Rate (DRR) of product. It means how many quantity of product do we sell daily from our store. So as explained, we have 3 product and their DRR as below. (DRR is calculated on last 30 days stock consumption)
<code>+-----------------+-------------+
|    Product      |     DRR     |
+-----------------+-------------+
| Product1        | 100 qty/day |
| Product2        | 50 qty/day  |
| Product3        | 20 qty/day  |
+-----------------+-------------+
</code>
Now we have available finished product stock with us for this 3 product
<code>+-----------------+------------+
|    Product      | Stock(qty) |
+-----------------+------------+
| Product1        | 2000 qty   |
| Product2        | 1000 qty   |
| Product3        | 1000 qty   |
+-----------------+------------+
</code>
As shown in the table above, there are 4000 finished products which means that there are 4000 jars that are converted to finished product. Apart from finished product I have 5000 more jars available with me.
I want to convert this 5000 jars into finished product such that all the stock will get finished after some specific days.
For example, I am having 2000 Product1 finished product and its DRR is 100. So I am having the stock of Product1 for 20 days. Same for others as below.
<code>+-----------------+-----+------------+-------------+
|    Product      | DRR | Stock(qty) | Stock(days) |
+-----------------+-----+------------+-------------+
| Product1        | 100 |       2000 | 20 Days     |
| Product2        |  50 |       1000 | 20 Days     |
| Product3        |  20 |       1000 | 50 Days     |
+-----------------+-----+------------+-------------+
</code>
Here I need to do stock planning and for that I want to convert 5000 available jars into finished product in such a way that at particular date my all stock will be completed. So that before 15 days of completion of my stock I can plan for other stock.
For example on today I am having stock of each product available for different days like Product1 for 20 days, Product2 for 20 days and Product3 for 50 days. So after converting my 5000 Jars into finished product. My Product1, Product2 and Product3 will be available for approximately some (x) days. This is what I want to achieve. I want the formula to calculate (x) days so that I can code the same in my code to display the same.

Below information will be displayed to user on screen.

Available Finished product stock and stock in days.
Available raw stock. And a text box where the user will enter the stock he/she want to convert to finished product. For example 7000 jars are in stock and user choosed to convert only 5000 jars to finished product.
If user choose to convert 5000 jars to product. Then after converting jars to finished product. For how many days the stock will be available.
Every day the availability of stock will change as per DRR

Note:

DRR is calculated on the basic of stock consumed in last 30 days. (DRR can change everyday).
Consider that apart from jar all other raw material are available in excess quantity.
Answer: I depends on available stock product have maximum time for consumption. What should other product are made such that they consumption finish at same day so formula be something like this
products_need_to_make=(product_max_time - current_prod)×rate_of consumption_of_current_product
Total material =sum of all product produce
If raw material is less then total material require then choose second highest time needed material
If raw material is more then distribute them to make all product such that time is same
Comment: how can i calculate product_max_time. Can you show with some example
Comment: It's 50 in your example
Comment: can you please show me the formula to calculate this. How does it derived?
Comment: It's based on that number of product available in stock;one of product need maximum time for consumption respective to other as in your case 50day is maximum; so you want to finish all product same time;you can't reduce time(50 days). So you make rest of product such that it's finish at after (50days); t days product already have in stock; so make 50-t days product that is (50-t)*product_consuption_day
Comment: As per this calculation. My all product will become available for 50 days. But still there will be available stock with me. I want the stock to be 0
Comment: Yes after above procedure rest of material will distribute uniform in all product as total_raw_remain_after_first_procedure×(rate_of_consuption(50,20,20)/sum_of_rate_of_all_product(50+20+20)
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/99816/discussion-between-naresh-teli-and-mohinuddin-luhar).


form does not return values

Question: as soon as I send the form, it does not return the values I want. i'm using ajax to receive the data.
                         <form action="registro-de-dominios.php" method="POST" id="ajax-form" class="form-inline">
                            WWW
                            <input type="text" class="p-3 form-control form-control-lg mb-2 mr-sm-2 input-ro www" id="inlineFormInputName2" name="www" value="www" placeholder="www." readonly>

                            Domínio

                                <input type="text" name="dominio" id="dominio" class="input-dom form-control form-control-lg" placeholder="Digite o domínio desejado">

                                <select name="point" id="point" class="form-control form-control-lg">
                                    <option value=".com">.com</option>
                                    <option value=".com.br">.com.br</option>
                                    <option value=".net">.net</option>
                                </select>

                            <button type="submit" id="salvar-form" class="btn btn-dominio btn-outline-danger" name="enviar" value="Enviar" data-toggle="modal" data-target="#ExemploModalCentralizado">

                            </button>
                        </form>

AJAX:

this is what returns:

does not return anything in the "dominio" field and returns the ".com" option, even if I choose another option
Comment: what is the value of `$('#dominio').val()` ?
Comment: Remove `action="registro-de-dominios.php" ` from `form` and set `type="button"` for the `button`
Answer: You are assigning the www, dominio, and com consts outside the button click event handler. So they are only set once, while the document is loading, not when the button is clicked. So you are getting the default values in AJAX call. They should be assigned inside the click handler, like this:
$(document).ready(function() {
        $("#salvar-form").click(function(e) {
            const www = $('.www').val();
            const dominio = $('#dominio').val();
            const com = $('#point').val();
            ...
        });
});

form does not return values


How to remove hash from url angular app?

Question: It may be the duplicate question. but i have tried so many solutions,no answer  work for me. I am using ui-router for angular web app.
I am using apache2 server to serve the angular app. I am using Ubuntu.
My project code is inside of the folder \var\www\html\myapp
here myapp is the my application folder.
I have tried the following
1 adding <base href="/myapp/"> to the head tag
2 adding $locationProvider.html5Mode(true);  or $locationProvider.html5Mode({    enabled: true,    requireBase: false});
(tried with both separately)
3 adding the .htaccess file with the following content
Options +FollowSymLinks

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteRule (.*) index.html [L]
</ifModule>

and some other content for .htaccess file which i got from google to serve the path.
I have tried putting .htaccess file inside \var\www\html\ as well as \var\www\html\myapp.
It will remove the hash from the url, but when i clicked refresh ,it will show not found. Can anyone please help me.
Answer: Try this in .htaccess file under \var\www\html\myapp :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]

In index.html head section :
<base href="/myapp/">

In app config :
$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
});

/* Records full path */
$analyticsProvider.withBase(true);


Translated taxonomy term in contextupal filter not validating

Question: I have a translated taxonomy term:

And have a simple view:

With a contextual filter on the translated taxonomy term:

Which works in english:

But not in french:

Any idea ?
Answer: I had to set the taxonomy vocabulary type from "Localize" to "Translate".


No data is getting into a table built over a stream from ksql binary, it get's there if I do the same with ksql prompt?

Question: I'm trying to create a table over a stream using ksql binary. The table gets created and starts running but when I query it there is no data inside the table. When I create the same using ksql prompt the table gets created I'm also able to query the data.
PS: I am setting this property  SET 'auto.offset.reset'='earliest'; to query the data form beginning.
I tried both methods. It works when I create table using ksql prompt. Same command does not work when try to do the same with ksql binary.
sudo echo "CREATE TABLE movie_table AS SELECT title, count(*) AS appearance FROM test WHERE year > 1988 GROUP BY title;" | sh /home/binary/ksql http://devopsksql.default.svc.cluster.local:8088

ksql> CREATE TABLE movie_table AS SELECT title, count(*)

 Message
---------------------------
 Table created and running
---------------------------

ksql> select * from MOVIE_TABLE;
^CQuery terminated

Same when I run with ksql prompt open it works as is.
I am expecting a way to create a table and query it without using the ksql prompt.
Comment: which version of KSQL are you running?
Answer: You need to set SET 'auto.offset.reset'='earliest'; before your CREATE query.
If you want to interact with KSQL from a script I'd suggest using the REST API (e.g. via curl) than trying to pipe to the ksql binary.


Monitoring nginx (500's) with telegraf

Question: I'd like to understand what my nginx instance is returning and who it's asking to handle requests.  What fraction of my queries get handled by rails, what fraction are handled directly by nginx, what fraction are heading off to nginx_status, etc.
Similarly, I'd also like to understand things like how many of which HTTP result codes I'm returning.  If there's a peak in 500's, I'd like to know.
The telegraf nginx plugin provides some very basic stats on nginx but no more.  I've seen some vaguely complicated solutions for result codes that basically involve setting up log monitoring infrastructure.  This data seems so fundamental I feel I must be missing something.
I've seen nothing that will help me understand who is actually handling queries (i.e., which handler).
All of this is interesting because (1) secular growth in handler dispatches can indicate scaling issues in clearer ways than simple load on the handler machines, and (2) peaks in anything can alert to problems.
Any pointers?
Answer: If it's actual I would like to write my own config. It based on tail telegraf plugin.

Add speshial log format to http section

    log_format codes_combined 'code=$status ts=$time_iso8601';

Use this format in server section

    access_log /var/log/nginx/codes.log codes_combined;

Edit /etc/telegraf/telegraf.conf:

[[inputs.tail]]
    files = ["/var/log/nginx/codes.log"]
    data_format = "logfmt"

After restart nginx and telegrap data should be avalable in Graphana. I configured a new graph

SELECT count("code") as code_2xx FROM "tail" WHERE $timeFilter AND code >= 200 AND code < 300 AND code  <> 204  GROUP BY time($__interval)
SELECT count("code") as  code_3xx  FROM "tail" WHERE $timeFilter AND code >= 300 AND code < 400 GROUP BY time($__interval)
SELECT count("code") as  code_4xx  FROM "tail" WHERE $timeFilter AND code >= 400 AND code < 500 GROUP BY time($__interval)
SELECT count("code") as  code_5xx  FROM "tail" WHERE $timeFilter AND code >= 500  GROUP BY time($__interval)
SELECT count("code") as code_204 FROM "tail" WHERE $timeFilter AND  code  = 204  GROUP BY time($__interval)

Don't forger to check  /etc/logrotate.d/nginx. Permisions should be like this:

create 0644 www-data adm

Answer: You can let telegraf collect your nginx access logs. Then you can analyze how many requests had which HTTP status code (1xx, 2xx, etc).
Add this to your /etc/telegraf/telegraf.conf (and make sure telegraf has access rights to the logfile, it won't tell you, if it hasn't):
[[inputs.logparser]]
   files = ["/var/log/nginx/access.log"]
   from_beginning = true
   name_override = "nginx_access_log"

   [inputs.logparser.grok]
     patterns = ["%{COMBINED_LOG_FORMAT}"]
     measurement = "nginx_access_log"

Comment: Logparser has since been deprecated. This is still useful for reference (and it still works for now, I just wouldn't start _new_ setups with it).
Answer: If you need to modify the permissions of files under the /var/log/nginx/ directory, you can try using the following command:
sudo chmod 777 /var/log/nginx/*
By running this command with superuser privileges (sudo), you will grant read, write, and execute permissions to all users (owner, group, and others) for all files within the /var/log/nginx/ directory.
Please exercise caution when using chmod 777 as it grants full permissions to all users, which may have security implications. It is generally recommended to set more restrictive permissions based on your specific requirements and security guidelines.
Before proceeding, ensure that modifying the permissions of the files under /var/log/nginx/ is necessary and aligns with your intended use case or troubleshooting needs.

Monitoring nginx (500's) with telegraf


during Chinese remainder congruence simplify

Question:
I don't understand highlighted part, which means I don't know how to simply congruences.
how can 20y1 = 1 (mod3) can be simply 2y1 = 1 (mod 3)
how 2 y1 = 1 (mod3) change to 2y1 = 2 (mod3)
I don't understand every steps....
I only know what a = b (mod 3)
means that b is remainder of a/3 ....
Is there any rules? for it ?
Comment: Please learn [MathJax](http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference). It makes your post easier to read and helps other with similar questions find your post.
Answer: \begin{array}{r|l}
   20y_1 \equiv 1 \pmod 3 & \text{Given} \\
    2y_1 \equiv 1 \pmod 3 & \text{Because $20 \equiv 2 \pmod 3$.} \\
    4y_1 \equiv 2 \pmod 3 & \text{Multiply both sides by $2$.} \\
     y_1 \equiv 2 \pmod 3 & \text{Because $4 \equiv 1 \pmod 3$.} \\
\end{array}
Why is $20 \equiv 2 \pmod 3$?
$\quad$ Because $3$ goes into $20$ six times with a remainder of $2$.
$\quad$ Or because $3$ divides $(20-2)$.
Why multiply both sides by $2$?
$\quad$ Because $\dfrac 12 \equiv 2 \pmod 3$. (See the next question.)
Why is $4 \equiv 1 \pmod 3$?
$\quad$ Because $3$ goes into $4$ one times with a remainder of $1$.
$\quad$ Or because $3$ divides $(4-1)$.
Answer: First note
\begin{align}
2^{-1} \equiv 2\mod 3
\end{align}
then it follows
\begin{align}
y_1 \equiv2^{-1}*2y_1 \equiv 2^{-1}\ast1 \equiv 2\mod 3.
\end{align}
Edit: Let me explain a bit more.
You start out with the equation
\begin{align}
20y_1 \equiv 1 \mod 3
\end{align}
and you basically want to solve for $y_1$. Of course, if this is just regular old middle school algebra then we would immediately multiply both sides by $20^{-1}$ to get
\begin{align}
y_1 \equiv 20^{-1} \mod 3.
\end{align}
But then we need to figure out what is $20^{-1}$.
However, the problem could be further simplified if we first reduced $20$ to $2$ since we are in the mod $3$ world to get
\begin{align}
2y_1 \equiv 1 \mod 3
\end{align}
which means
\begin{align}
y_1 \equiv 2^{-1} \mod 3.
\end{align}
Finally, observe
\begin{align}
2*2 \equiv 4 \equiv 1 \mod 3
\end{align}
which means $2^{-1} = 2$. Thus, we have the desired result.
Comment: ...... um ... I dont get it...


Asymptotic behavior of the summatory Liouville function

Question: EDIT: This is a major revision in light of sources provided by @ErickWong's comment.
The summatory Liouville function (OEIS sequence A002819) is $$L(n)=\sum_{1\le k\le n}(-1)^{\Omega(k)}\quad (n=1,2,3,\ldots)$$
where $\Omega(k)$ is the number of prime factors of $k$ counted with multiplicity. Thus $L(n)$ is the sum of the "parities" ($+1$ if even, $-1$ if odd) of the first $n$ values of $\Omega.$
Here is a plot of the first $10^9$ values of $L$, including overlays of some approximating curves:

The overlays are as follows:

A "central" curve (gray) ${\sqrt{n}\over\zeta({1\over 2})}$ whose functional form is suggested by Tanaka's 1980 proof (mentioned here and proved here) that $\ L(n)-{\sqrt{n}\over\zeta({1\over 2})}\ $ changes sign infinitely often as $n\to\infty.$

A "sinusoid" (red) which oscillates about the central curve with increasing "wavelength" and amplitude:
$$\hat{L}(n) = {\sqrt{n}\over\zeta({1\over 2})}\left( 1 + \sqrt{1\over 2}\,\sin\left(t_1\ln(n) + 2.75\right) \right)
$$
where $\zeta$ is the Riemann zeta function and $t_1$ is the imaginary part of the first nontrivial zero of $\zeta$. Thus $\zeta({1\over 2})=-0.684765236\ldots$ and $t_1=14.1347251\dots.$ The occurrence of $t_1$ in the argument of the sine function is suggested by the "summary description" of a Wikipedia plot here. I've adjusted the parameters of the sinusoid, including amplitude and phase, to obtain a visual fit.

Q1: The mentioned "summary description" cites this paper ("On Differences of Zeta Values"), which proves an asymptotically oscillatory behavior of the coefficients in a certain series representation of Riemann zeta.  How does this lead to a zero of Riemann zeta appearing in $\hat{L}?$
Q2: Can it be shown that regular oscillatory behavior of $L(n)$ persists for arbitrarily large $n$?

NB: This behavior says nothing about how often $L(n)>0$ may occur (such as the "spike" in the plot at $n\approx 9\cdot 10^8$); i.e., it may be independent of whether $L$ changes sign infinitely often.
Comment: Your "central" curve (gray) and "sinusoid" (red) are related to the explicit formula $L_o(x)=\frac{\sqrt{x}}{\zeta\left(\frac{1}{2}\right)}+\sum\limits_{k=1}^\infty\left(\frac{\zeta\left(2\,\rho_k\right)}{\rho_k\,\zeta'\left(\rho_k\right)}\,x^{\rho_k}+\frac{\zeta\left(2\,\rho_{-k}\right)}{\rho_{-k}\zeta'\left(\rho_{-k}\right)}\,x^{\rho_{-k}}\right)+I(x)$.
Comment: @ErickWong - Thank you very much -- I hadn't known quite what to search for!
Comment: I do not know about your specific question, but you will find more references by searching for "Liouville lambda".  For instance, Wikipedia compiles some well-known results here https://en.m.wikipedia.org/wiki/Liouville_function
Comment: Try plotting $f(x)=\frac{\sqrt{x}}{\zeta\left(\frac{1}{2}\right)}+\left(\frac{\zeta\left(2\,\rho_1\right)}{\rho_1\zeta'\left(\rho_1\right)}x^{\rho_1}+\frac{\zeta\left(2\,\rho_{-1}\right)}{\rho_{-1}\zeta'\left(\rho_{-1}\right)}x^{\rho_{-1}}\right)$ where $\rho_1=\frac{1}{2}+i\,14.1347$ and $\rho_{-1}=\frac{1}{2}-i\,14.1347$ is the first pair of non-trivial zeta zeros.
Comment: $L_o(x)$ above is the explicit formula for $L(x)=\sum\limits_{n\le x}\lambda(n)$ where $\lambda(n)=(-1)^{\Omega(n)}$.
Comment: The explicit formula mentioned by Steven is really the answer, but its convergence is much more difficult to make clear, you should look at the explicit formula for $\sum_{p^k \le x} \log p$ which is much simpler (because in $\zeta'(s)/\zeta(s)$ all the poles have residue one)
Comment: @StevenClark I will accept that if you post it as an answer, including a source for the formula. Mathematica gives $f(x)=\frac{\sqrt{x}}{\zeta\left(\frac{1}{2}\right)}+ \sqrt{x}\left(-0.1552456\cos(14.134725\,\ln x) + 0.310816\sin((14.134725\,\ln x)  \right)$, which fits quite well even if the cosine term is omitted.
Comment: @reuns We commented almost simultaneously! I'll look into what you say.
Answer: The summatory Liouville function is defined as follows where $\Omega(n)$ is the number of prime factors of n counted with multiplicity.
(1) $\quad L(x)=\sum\limits_{n\le x}\lambda(n)\,,\qquad\lambda(n)=(-1)^{\Omega(n)}$

The OP's "central" curve (gray) and "sinusoid" (red) are related to the explicit formula for $L(x)$ illustrated in formula (2) below (see THE EXPLICIT FORMULA FOR $L_o(x)$ By A. Y. FAWAZ).
(2) $\quad L_o(x)=\frac{\sqrt{x}}{\zeta\left(\frac{1}{2}\right)}+\sum\limits_{k=1}^\infty\left(\frac{\zeta\left(2\,\rho_k\right)}{\rho_k\,\zeta'\left(\rho_k\right)}\,x^{\rho_k}+\frac{\zeta\left(2\,\rho_{-k}\right)}{\rho_{-k}\zeta'\left(\rho_{-k}\right)}\,x^{\rho_{-k}}\right)+I(x)\,,\quad x>0$

Consider the function $f(x)$ defined below which represents the primary growth term plus the contribution of the first non-trivial zeta zero pair $\rho_1=\frac{1}{2}+i\,14.1347$ and $\rho_{-1}=\frac{1}{2}-i\,14.1347$.
(3) $\quad f(x)=\frac{\sqrt{x}}{\zeta\left(\frac{1}{2}\right)}+\left(\frac{\zeta\left(2\,\rho_1\right)}{\rho_1\zeta'\left(\rho_1\right)}x^{\rho_1}+\frac{\zeta\left(2\,\rho_{-1}\right)}{\rho_{-1}\zeta'\left(\rho_{-1}\right)}x^{\rho_{-1}}\right)$

Figure (1) below illustrates $L(x)$ in blue, the primary growth term $\frac{\sqrt{x}}{\zeta\left(\frac{1}{2}\right)}$ in gray, and $f(x)$ in red.

Figure (1): Illustration of $L(x)$ (blue), $\frac{\sqrt{x}}{\zeta\left(\frac{1}{2}\right)}$ (gray), and $f(x)$ (red)

Assuming the Riemann Hypothesis (RH):

$\rho_k=\frac{1}{2}+i\,\Im(\rho_k)$,
$\rho_{-k}=\frac{1}{2}-i\,\Im(\rho_k)$,
$x^{\rho_k}=\sqrt{x}\left(\cos\left(\Im\left(\rho_k\right)\log
   x\right)+i\,\sin\left(\Im\left(\rho_k\right)\log x\right)\right)$, and
$x^{\rho_{-k}}=\sqrt{x}\left(\cos\left(\Im\left(\rho_k\right)\log
   x\right)-i\,\sin\left(\Im\left(\rho_k\right)\log x\right)\right)$.

This can be combined with $\frac{\zeta\left(2\,\rho_{-k}\right)}{\rho_{-k}\zeta'\left(\rho_{-k}\right)}=\left(\frac{\zeta\left(2\,\rho_k\right)}{\rho_k\zeta'\left(\rho_k\right)}\right){}^*$ (where $*$ indicates the complex conjugate) to derive the following result which is strictly real.
(4) $\quad\frac{\zeta\left(2\,\rho_k\right)}{\rho_k\,\zeta'\left(\rho_k\right)}\,x^{\rho_k}+\frac{\zeta\left(2\,\rho_{-k}\right)}{\rho_{-k}\,\zeta'\left(\rho_{-k}\right)}\,x^{\rho_{-k}}=\\$ $\qquad 2\sqrt{x}\left(\Re\left(\frac{\zeta\left(2\,\rho_k\right)}{\rho_k\,\zeta'\left(\rho_k\right)}\right)\cos\left(\Im\left(\rho_k\right)\log x\right)-\Im\left(\frac{\zeta\left(2\,\rho_k\right)}{\rho_k\,\zeta'\left(\rho_k\right)}\right)\sin\left(\Im\left(\rho_k\right)\log x\right)\right)\quad\text{(Assuming RH)}$

In mathematics the natural logarithm of $x$ is commonly referred to as $\log(x)$ instead of $\ln(x)$, and $\log_b(x)$ refers to the base $b$ logarithm of $x$. This is consistent with the notation generated by Mathematica when formulas are copied as Latex. Also, $\rho_k$ is returned by the Mathematica function $ZetaZero[k]$.
Comment: The error term in Fawaz's formula is known to be O(1/sqrt(x)).  If you bound the absolute value of imaginary parts of the zeros of the zeta function by T in Fawaz's formula, then you get a formula with an error term that depends on x and T. Would you happen to know a formula for the error term depending on x and T?
Comment: @EGME Sorry, I'm not aware of such a formula, but I haven't purchased the full version of the paper by Fawaz for review. Apparently it does not address such a formula? For the function $M(x)=\sum\limits_{n=1}^x \mu(n)$, section 14.27 of Titchmarsh gives the formula $M(x)=\frac{1}{2\pi i}\int\limits_{2-i T}^{2+i T}\frac{x^s}{s}\frac{1}{\zeta(s)} ds+O\left(\frac{1}{T}\right)$ for a fixed non-integer $x$.
Comment: Thank you. Perhaps I will try to work it out.
Comment: @EGME For the function $\psi(x)=\sum\limits_{n=1}^x \Lambda(n)$, Theorem 12.1 of section 12.2 of Ivic's "The Riemann Zeta-Function" indicates $\psi(x)=x-\sum\limits_{|\Im(\rho)|\le T} \frac{x^{\rho}}{\rho}+O\left(x T^{-1} (\log(x T)^2)\right)+O(\log(x))$ which is I believe is more analogous to your desired formula. Theorem 12.5 of Montgomery and Vaughan gives a slightly different formula.
Comment: @EGME There are also some formulas at https://en.wikipedia.org/wiki/Liouville_function#Generalizations that might be of interest.
Comment: Thank you, yes.
Answer: All the explicit formulas come from the inverse Mellin transform + residue theorem $$\frac{\zeta(2s)}{\zeta(s)}=s\int_1^\infty (\sum_{n\le x}(-1)^{\Omega(n)})x^{-s-1}dx$$ $$\sum_{n\le x}(-1)^{\Omega(n)}  = \frac{1}{2i\pi} \int_{2-i\infty}^{2+i\infty} \frac{\zeta(2s)}{\zeta(s)}
\frac{x^s}{s} ds=\sum Res(\frac{\zeta(2s)}{\zeta(s)}
\frac{x^s}{s})$$
except that the convergence of that sum over the residues (over the trivial and non-trivial zeros) is quite unclear, thus either we add a regulator (looking at $\int_1^x \sum_{n\le y}(-1)^{\Omega(n)}dy$), or we approximate the integral by $ \int_{2-iT}^{2+iT}$ for well chosen value of $T$ such that we can "traverse the critical strip" to express it as a sum over the first few non-trivial zeros.


Criar array em php, guardando quantas vezes uma string aparece

Question: Bom Dia!
Estou com um problema, eu tenho duas strings $procurar e $nome_das_maquinas, dentro de procurar eu tenho o texto completo, e dentro de $nome_das_maquinas as palavras que eu desejo procurar na variável $procurar.
Ex dos valores da variável:
$procurar = "Nome da Maquina: Cavadeira, Linha: 1, Gravidade: Média, Operador:Rafael; Nome da Maquina: Cavadeira, Linha: 3, Gravidade: Baixa, Operador:Lucas; Nome da Maquina: Motoniveladora, Linha: 3, Gravidade: Alta, Operador:Joao; Nome da Maquina: Cavadeira, Linha: 1, Gravidade: Média, Operador:Rafael;"

$nome_das_maquinas = "Cavadeira/Cavadeira/Motoniveladora/Cavadeira"

Eu preciso contar quantas vezes esses $nomes_das_maquinas aparecem em $procurar e guardar dentro de um array em ordem decrescente
Ex:
Cavadeira = 3

Motoniveladora = 1

Estou a alguns dias tentando fazer isso, alguém pode dar um help? Segue meu código de teste:
$arquivo = 'arquivos/etiqueta.txt';

$nomedasmaquinas = 'arquivos/nomedasmaquinas.txt';

$handle = fopen( $arquivo, 'r' );

$handle2 = fopen( $nomedasmaquinas, 'r' );

$ler = fread( $handle, filesize($arquivo) );
$ler2 = fread( $handle2, filesize($nomedasmaquinas) );

$procurar = $ler; //Esta vindo de um arquivo que contem as informacoes das maquinas, como um texto
longo (EX: Nome da maquina: Cavadeira Linha: 1 Gravidade: Media etc...)
$nome_das_maquinas_str = $ler2; //Esta vindo de um arquivo, e as palavras estão separadas por /

$array = explode('/', $nomestr); //Separo as palavras por / e gravo no array
$i = 0;
foreach($array as $valores){
   if($valores){
      $quantidade_que_aparece = substr_count($procurar, $valores); //Conto quantas vezes os valores que estavam no $array aparecem.
       $nomes_vetor[$i] = $quantidade_que_aparece; //Gravo a quantidade em um vetor
   }
  $i++; //Conta +1 pra percorrer o vetor
 }

// Fecha o arquivo
fclose($handle);

Comment: E qual erro dá @Lucas?
Comment: Erro não dá nenhum, só não consigo pensar nessa solução "Eu preciso contar quantas vezes esses $nomes_das_maquinas aparecem em $procurar e guardar dentro de um array em ordem decrescente"
Answer: Lucas, seu código está praticamente pronto, só faltou ordenar o array. Fiz as seguintes modificações:

$nomestr não existe, substitui por $nome_das_maquinas_str
Substitui o índice numérico $i pelo nome da máquina.
$valores virou $maquina
Ordenei o array com arsort

<?php
$arquivo = 'arquivos/etiqueta.txt';

$nomedasmaquinas = 'arquivos/nomedasmaquinas.txt';

$handle = fopen( $arquivo, 'r' );

$handle2 = fopen( $nomedasmaquinas, 'r' );

$ler = fread( $handle, filesize($arquivo) );
$ler2 = fread( $handle2, filesize($nomedasmaquinas) );

$procurar = $ler; //Esta vindo de um arquivo que contem as informacoes das maquinas, como um texto longo (EX: Nome da maquina: Cavadeira Linha: 1 Gravidade: Media etc...)
$nome_das_maquinas_str = $ler2; //Esta vindo de um arquivo, e as palavras estão separadas por /

$array = explode('/', $nome_das_maquinas_str); //Separo as palavras por / e gravo no array

foreach($array as $maquina){
    $quantidade_que_aparece = substr_count($procurar, $maquina); //Conto quantas vezes os valores que estavam no $array aparecem.
    $nomes_vetor[$maquina] = $quantidade_que_aparece; //Gravo a quantidade em um vetor
}

//Ordena decrescentemente
arsort($nomes_vetor);

//Exibe
echo "
";var_dump($nomes_vetor);echo "
";

// Fecha o arquivo
fclose($handle);
fclose($handle2);
?>

Comment: Muitoo obrigado amigo, vou estudar mais arrays em php!

S2