XenServer Performance

Question: we've got 2 x HP DL360 G5s with Quad Quad Xeons 2.6GHz and 32GB of memory each running XenServer 5.5 and they access an OpenFiler box (with 8 x 320GB SAS 10K drives) via copper CAT5 (1GB) for the storage.
We've used this setup for testing a lot of stuff which has worked out perfectly, but now we are moving to use this setup in production and are experiencing performance issues. There are currently 27 VMs split across the two servers which are all in use (albeit not doing a lot of work) but they seem "slow", especially our employee thin clients - they always complain logging in times and accessing files via the network are slow.
Personally, I think it's a throughput issue and we should go SCSI or FC for our storage but I need some evidence to back my theory up and I'm quite new to Xen (it was setup by a previous employee).
My questions: from the info I've gave would it be possible that the storage box is overloaded, trying to squeeze too much over that one cable;? how do I monitor network access in real-time from the XenServers themselves?
Thanks :-)
Answer: I have seen this issue many times.
I really love xenserver, however, its like an unpolished gem...
you should check with ifconfig -a (on dom0, xenserver console )
and look for dropped packets
you can use:
ifconfig  -a  | grep dropped | awk {'print $3'} | grep -v ":0"
if you see dropped packets , you should do:

On the Virtual Machines, Click Start, click Run, type regedit, and then click OK.
Locate and then click the following registry subkey:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
In the right pane, make sure that the DisableTaskOffload registry entry exists. If this entry does not exist, follow these steps to add the entry:
a. On the Edit menu, point to New, and then click DWORD Value.
b. Type DisableTaskOffload, and then press ENTER.
Click DisableTaskOffload.
On the Edit menu, click Modify.
Type 1 in the Value data box, and then press ENTER.
Exit Registry Editor.
Restart all Virtual Machines

and on server xenserver console:
Get the UUID of the physical interface:
xe pif-list host-name-label=XEN1
Disable checksum on the interfaces:
xe pif-param-set other-config:ethtool-tx="off" uuid=3281b044-2a93-2f1b-e8e1-eaf0faccbd1f;
xe pif-param-set other-config:ethtool-rx="off" uuid=3281b044-2a93-2f1b-e8e1-eaf0faccbd1f
Comment: you should apply them to any windows guests
Comment: I did turn this off on a few VMs but didn't notice any difference, should I apply it to all of them? There are no dropped packets on either of the hosts..
Comment: @John I'm pretty sure this TCP-offload trick has improved my guest performance quite a bit. Unsure on long-term testing just yet. Thanks for this! :D
Answer: During high load or a period of perceived performance drop, run top on the server.
You're looking for three warning signs that could help you along with finding your bottleneck:

%wa (near the top middle) - This is the IOWait measurement, or the amount of time that the CPU has to wait on i/o or storage requests to finish to continue working. If this is above 10-20%, you're going to start seeing problems. If this is the case, you need to upgrade your storage.
load average (set of three numbers on top) - This is the average usage of your CPUs over 1, 5, and 15 minutes. This is a very rough number to troubleshoot with, but a good rule of thumb is that you want to stay under 1.0*Cores, so an 8-core system shouldn't go much above 8.0 load average. Anything higher means that applications are probably being limited by CPU (absent %wa issues). More info on load averages linked here
Mem and Swap (usually lines 4 and 5) - If you're running out of RAM, you'll see that here. A warning sign here would be the combination of a low number for buffers, a low number of Mem free, and a high amount of Swap used. Low/high here is relative to your total Memory.

If you want to measure network access real-time, I suggest starting with something like bmon (linked here) to see just how much traffic is being generated.
One question though: Are you running storage and client access over a single network interface? You may want to separate those two out if you are.
Comment: I've watched `top` on both of the servers for 20 minutes the %wa didn't even hit 1%, usually sitting at 0.3 - 0.7. The max I saw the CPU usage jump was to 7.9% and the load averages are always below 1.0. The memory is interestingly showing as ~8GB even though it has 32GB, there is barely any swap usage.

The severs have bonded NICs so there are two phyiscal links but just the one logical.
Answer: Don't worry! All of your problems can be solved!  All you need to do is upgrade to XenServer 5.6 sp2.  The only downside of upgrading to this release is that you will not be able to use mdadm software local raid.  From what you said, it seems that you aren't using this.
Citrix has introduced their "intellicache" technology with XenServer 5.6.  This technology has personally revolutionized my XenServer infrastructure, removing all the slowness in my VM's.  Intellicache works by caching reads from network attached storage on a local disk.  The first time you boot the VM it will be just as slow as normal, but the next time you reboot, all of the reads will hit the local storage, saving IOPS on your OpenFiler.  If you shut the VM down, and start on a different XenServer host, a new read-cache is built automatically in the background.
If you really want to see your VM's scream, I would suggest installing a SSD inside each XenServer host and configuring it for intellicache.  This will give you excellent performance.
For install instructions, see:
http://support.citrix.com/article/CTX129387  pages 21-24 of the pdf.
For more information see:
http://www.youtube.com/watch?v=i-6ojYDdrLA
http://support.citrix.com/article/CTX129052

XenServer Performance


When are $3$ vectors associative in triple cross products?

Question: The question I am trying to show under what conditions
$$\vec{A}\times(\vec{B}\times\vec{C}) = (\vec{A}\times\vec{B})\times\vec{C}.$$
I have found that right-hand side of the above equation is equal to
\begin{align}
(\vec{A}\times\vec{B})\times\vec{C}
&=-\vec{C}\times(\vec{A}\times\vec{B})\\
&= \vec{C}\times(\vec{B}\times\vec{A}).
\end{align}
This is similar to the left-hand side of the original equation.
The conclusion I arrived at was, in order for the equality to be true, either $\vec{A}, \vec{B}$ or $\vec{C}$ have to be zero, or $\vec{A}$ must be equal to $\vec{C}$.
Is this correct? If not, or if I am missing anything, please let me know.
Comment: What do you think happens when $\vec A = \lambda \vec C$ for some scalar $\lambda$?
Comment: I think that since multiplication by scalar would be distributive, it would also be an additional condition for equality. Is that correct?
Answer: Your reasoning is correct. The reason is that the cross product is anti-commutative. Therefore, the only situations wherein you will find the desired properties are when one of the vectors is zero, or when $A \propto C$
To prove this, note that $A\times (B\times C) = (A\cdot C)B-(A\cdot B)C$ and likewise $(A\times B)\times C = -(C\cdot B)A+(C\cdot A)B$
Assume the two are equal, and note that the dot product commutes, and we find
$$ (A\cdot B) C = (C\cdot B) A $$
Then, $$\frac{A}{A\cdot B} = \frac{C}{C \cdot B} \implies C = kA.$$
So $C$ is some scalar multiple of $A$.
Comment: Thanks. I have a few quick followup questions.

1) Is there any way to derive the AX(BXC) = BAC-CAB or is it just a shortcut that cuts down on tedious algebraic manipulation?
2) Where does the $k$ come from at the very end, that says that another possible solution is that $C$ is a scalar multiple of $A$ and not just equal?
Comment: @achacttn Since $k = \frac{A\cdot B}{C\cdot B}$. The dot product is just a scalar number, so the ratio of dot products is a scalar number. In this case, we're looking for conditions of equality; $A,B,C$ aren't variables, so there's nothing lost by saying $C = \frac{A\cdot B}{C\cdot B} A$. The triple product relation I used can be found by doing out the algebra; it's called the "triple product expansion", see also http://en.wikipedia.org/wiki/Triple_product
Comment: @Emily I think there's another possibility when the triple product is associative: when the vector $B$ is orthogonal to both $A$ and $C$. In your prove you assumed that $A\cdot B$ and $C\cdot B$ are both non-zero; which might not be the case.


Infer a proportion CI from the difference of 2 incidence rate CI

Question: In an article, one could read the value of 2 incidence rates per 100,000, with their confidence intervals:

Treatment (A): $7.3\ [6.1 - 8.5]$
Control (B): $14.2\ [12.2 - 16.1]$
Difference (B-A): $7.0\ [4.6 - 9.2]$

The value I'm interested in is the ratio of $\frac{B-A}{B}$, which is estimated at 48.6% here.
Is there any way I could infer the confidence interval of this value with only this information?
Answer: Yes, there is. Assuming you know the variance and covariance for A and B, you can use the delta method to compute variances of functions of these two statistics.
Let $\beta = [a,b]^T$, and let $h(\beta) = (\beta_1 - \beta_2)/\beta_1$
Then $$\begin{aligned}
\operatorname{Var}(h(\beta)) =\nabla h(\beta)^{T} \cdot(\Sigma / n) \cdot \nabla h(\beta)
\end{aligned}$$
Comment: Very interesting! A and B are supposed to be independent and I think I can infer the variance from somewhere in the article. Could you provide a link where I could learn more about this?
Comment: @DanChaltiel https://en.wikipedia.org/wiki/Delta_method.  Also, there is almost certainly an example similar to your question in Biostatistical Methods 2ed by Lachin.
Comment: The quality of the approximation will depend on the sample sizes.
Comment: @MickyboYakari This is so for everything in statistics.
Comment: First, I didn't think I'd need to bring attention explicitly to the difference between exact and approximate methods, Secondly, I'm sure you know how bootstrapping compares with asymptotic methods for relatively small sample sizes.
Comment: @MickyboYakari Bootstrapping requires actual data, doesn't it? I stated that I only had confidence intervals, read from an article.
Comment: @DanChaltiel I just realised I mistranslated the question to 'how would you calculate a confidence interval for this particular function of the parameters?'


$f^{-1}(V)=U$ imply $V=f(U)$ if $V\subset f(X)$

Question: Let $X,Y$ be topological space. Suppose  $f: X\rightarrow Y$ and $V\subset f(X)$, if $f^{-1}(V)=U$, is it true that $V=f(U)$?

I encounter this question when studying quotient topology, I think the answer is yes but I am not sure
Answer: Note that $U \subseteq X$.
the inverse image (or preimage) is defined as follows:
$x \in U$ iff $f(x) \in V$.
$f(U) \subseteq V$ by definition, since $f(u) \in V$ for all $u \in U$.
We show that $V \subseteq f(U)$. Suppose not. then there is some $v \in V$ so that there does not exist some $u \in U$ s.t $f(u) \in V$. But $V \subseteq f(X)$, so by definition there is some $x \in X$ s.t $f(x)=v$. But then $x \in U$ by definition, and we have a contradiction.
Answer: Let $y \in f(U)$. Then there exists $x \in U$ such that $f(x) = y$ and since $U = f^{-1}(V)$, $x \in f^{-1}(V) \implies y = f(x) \in V$. So $f(U) \subset V$.
Let $v \in V$. Since $V \subset f(X)$, there exists $x \in X$ such that $v = f(x) \implies f(x) \in V \implies x \in f^{-1}(V) = U$, which shows $v \in f(U)$. So we have $V \subset f(U)$.
Answer: True for any function $f$ and with $V\subset X.$
(1). If $v\in V$ then $\exists x\in X$ with $f(x)=v$ because  $V\subset f(X).$ So $x\in f^{-1}V=U$ by def'n of $f^{-1}V,$ so $v=f(x)\in f(U).$
(2).If $v\in f(U)=f(f_{-1}V)$ then $\exists y\in f^{-1}V$ with $f(y)=p.$ But $y\in f{-1}V$ implies $f(y)\in V$ by def'n of $f^{-1}V$,  so  $p=f(y)\in V.$
(3). By (1) we have $V\subset f(U).$ By (2) we have $f(U)\subset V.$
(4). Remarks .Note that (2):$f(f^{-1}V)\subset V$ always holds because $f(f^{-1}V)=\{p\in V:\exists x\;(f(x)=p)\}.$ Note that topology is irrelevant to the Q. It is a Q about  functions and sets.


unpack syntax in python 3

Question: I am trying to convert hex numbers into decimals using unpack.
When I use:
from struct import *
unpack("<H",b"\xe2\x07")

The output is: 2018, which is what I want.
The thing is I have my hex data in a list as a string in the form of:
asd = ['e2','07']

My question is is there a simple way of using unpack without the backslashes, the x? Something like so:
unpack("<H","e207")

I know this doesn't work but I hope you get the idea.
For clarification I know I could get the data in the form of b'\x11' in the list but then it's interpreted as ASCII, which I don't want, that's why I have it in the format I showed.
Answer: You have hex-encoded data, in a text object.  So, to go back to raw hex bytes, you can decode the text string.  Please note that this is not the usual convention in Python 3.x (generally, text strings are already decoded).
>>> codecs.decode('e207', 'hex')
b'\xe2\x07'

A convenience function for the same thing:
>>> bytes.fromhex('e207')
b'\xe2\x07'

Now you can struct.unpack those bytes.  Putting it all together:
>>> asd = ['e2','07']
>>> text = ''.join(asd)
>>> encoded = codecs.decode(text, 'hex')
>>> struct.unpack("<H", encoded)
(2018,)

Comment: will there be any ascii interpretation using codecs.decode()? because in my case i have a huge list containing of hundreds of hex strings and i don´t want them to be interpreted at any point
Comment: I'm not sure what you mean by "ascii interpretation". Ideally, you should locate the code that mistakenly saves binary data as text data, and change that code to just save binary data in the first place.
Comment: what i mean is that if i use print() to show the hexa numbers, python shows them allready interpreted in ascii
Comment: As long as they are all 0-9 a-f then it doesn't matter.

unpack syntax in python 3


HAProxy not detecting backend with non-standard http port

Question: I have a HAProxy setup where the backend server is running a webserver non standard port.The config is as follows
global

    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    tune.ssl.default-dh-param 2048
    user        haproxy
    group       haproxy
    daemon

    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats

    ssl-default-bind-options no-sslv3
    ssl-default-bind-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS

    ssl-default-server-options no-sslv3
    ssl-default-server-ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:RSA+AESGCM:RSA+AES:!aNULL:!MD5:!DSS

    tune.ssl.default-dh-param 2048

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    stats enable
    stats auth someuser:somepassword
    stats uri /hpstats
    option forwardfor       except 127.0.0.0/8
    option http-server-close
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

frontend  http-in
    bind *:80
    bind *:443 ssl crt /etc/ssl/certs/test.pem no-sslv3

    #define hosts
    acl host_demo       hdr(host)       -i testing.domain.com

    #usage rules
    use_backend demo    if      host_demo
    default_backend             demo

backend demo
    server      demo 192.168.XX.XX:4873 check verify none

With this setup HAProxy always complained with the following error.
Message from syslogd@localhost at Nov  2 12:31:58 ...
 haproxy[24266]: backend demo has no server available!

When I changed the backend server to port 80 from 4873 everything works fine
backend demo
    server      demo 192.168.XX.XX:80 check verify none

I am running Centos 7 on both machines. I have tried to enable the http_t_port using semanage on both machines but that didn't have any effect. Only when I changed the port from 4873 on the server to 80 or 443 that the HAProxy load balancing worked and it stopped complaining.
Can anyone point out why is that and how should I resolve it so that I can run the HTTP on non-standard port?I am pretty sure I am missing setting somewhere considering I am using SELinux.
Comment: This is because there is probably no service listening on port 4873.
So the health check fails, and HAProxy considers the server as down.
Comment: There is a service listening. I can go to `192.168.XX.XX:4873` in the browser directly and it works but `testing.domain.com` returns 503 which is ofcourse because HAProxy thinks that the server is down. I even tried adding `tcp_check` to add a custom check for port `4873` in the `backend` configuration but that didn't work either.
Comment: If you suspect sellinux, check the audit log and/or use semanage to switch haproxy_t to permissive.
Answer: @andthereitgoes
It seems that the service is not listening on the port 4873 or its being blocked by iptables at source or destination.
Comment: There is a service listening because I can go to 192.168.XX.XX:4873 in the browser directly and it works.


Triangle inequality for Schatten norms

Question: For a matrix $A\in\mathbb R^{n\times m}$, we consider the vector of its singular values $\sigma = [\sigma_1,\dots,\sigma_{\min\{m,n\}}]^T$. We define the $p$-Schatten norm
$$
\|A\|_{S,p} := \|\sigma\|_p
$$
as the usual $p$-norm of $\sigma$. How do I show that this $p$-Schatten norm satisfies the triangle inequality?
Everywhere, where Schatten norms are used it is implied that it's actually a norm, but I don't have a good idea on how to prove this.
There is this related question on Ky Fan norms, but I actually don't understand the answer.
Comment: Maybe it helps to know that $\DeclareMathOperator{\tr}{tr}$
$$
 \|A\|_{S,p} = \Bigl(\tr\bigl((A^TA)^{p/2}\bigr)\Bigr)^{1/p}. $$
But I still don't know where to go from there.
Comment: Pedersen's "Analysis Now" proves this (for arbitrary trace-class operators on a Hilbert space) with polar decomposition; I imagine something similar can be done with singular value decomposition.  Bhatia's "Matrix Analysis" has a proof that handles arbitrary unitarily invariant norms.  I've been looking at both but I have failed to fined a quick, punchy justification of subadditivity.
Comment: If you can access Bhatia's Matrix analysis, Theorem IV.2.1 will do the job
Answer: If we adapt the proof of IV.2.1 from Bhatia's Matrix Analysis, it suffices to prove the following facts:

The Ky-Fan norms satisfy the triangle inequality
For vectors $x, y \in \Bbb R^n_+$: if
$$
\sum_{j=1}^k x_k^\downarrow \leq \sum_{j=1}^k y_k^\downarrow, \quad k = 1,\dots,n
$$
then $\|x\|_p \leq \|y\|_p$
For vectors $x, y \in \Bbb R^n_+$: if $x \leq y$ (entrywise), then $\|x\|_p \leq \|y\|_p$

The result regarding the Ky-Fan norms can be proven as follows:

If $A$ is a Hermitian matrix and $\lambda^{\downarrow}_j$ denotes the eigenvalues in decreasing order, then
$$
\sum_{j=1}^k \lambda_k^\downarrow =  \max \sum_{j=1}^k x_j^TAx_j
$$
where the maximum is taken over orthonormal $k$-tuples of vectors $\{x_1,\dots,x_k\} \subset \Bbb R^n$ (known as Ky-Fan's max principle).
Consequently: for Hermitian matrices $A,B$, we have
$$
\sum_{j=1}^k \lambda_k^\downarrow(A + B) \leq \sum_{j=1}^k \lambda_k^\downarrow(A) + \sum_{j=1}^k \lambda_k^\downarrow(B)
$$
The same applies to the block matrices
$$
\tilde A = \pmatrix{0&A\\A^T & 0}, \quad \tilde B = \pmatrix{0 & B\\B^T & 0}
$$
and notably, $\lambda_j^\downarrow(\tilde A) = \sigma_j^\downarrow(A)$.
Comment: That's nice. What do the little downwards arrows mean?
Comment: @Wauzl it means that the relevant quantities are listed from greatest to least.  Of course, this is the usual convention for singular values.
Comment: @BenGrossmann Could you explain why the last equality holds? Can't the eigenvalues of $\tilde A$ even be negative?
Comment: @ludoer If $A = U \Sigma V^T$ is a singular value decomposition, then we have
$$
\tilde A = \pmatrix{U & 0\\0 & V} \pmatrix{0 & \Sigma\\ \Sigma^T & 0} \pmatrix{U & 0\\ 0 & V}^T.
$$
With that in mind, consider the eigenvalues of
$$
 \pmatrix{0 & \Sigma\\ \Sigma^T & 0}.
$$
Comment: @ludoer Sorry, I misunderstood your question at first. The last equality holds only for $j = 1,\dots,\min\{m,n\}$. After that, we reach zero-eigenvalues and then the negative eigenvalues $-\sigma_j(A)$.

Triangle inequality for Schatten norms


Computing Fast Fourier Transform in R

Question: This is a math question with programming application. So I am trying to find 3 things given a certain function in the x domain when transformed into the spectral domain.
1) the Amplitude
2) The Frequency
3) The Phase
In R (statistical software) I have coded the following function:
y=7*cos(2*pi*(seq(-50,50,by=.01)*(1/9))+32)
fty=fft(y,inverse=F)
angle=atan2(Im(fty), Re(fty))
x=which(abs(fty)[1:(length(fty)/2)]==max(abs(fty)[1:(length(fty)/2)]))
par(mfcol=c(2,1))
plot(seq(-50,50,by=.01),y,type="l",ylab = "Cosine Function")
plot(abs(fty),xlim=c(x-30,x+30),type="l",ylab="Spectral Density in hz")

I know I can compute the frequency by taking the bin value and diving it by the size of the interval(total time of the domain). Since the bins started at 1, when it should be zero, it would thus be
frequency=(BinValue-1)/MaxTime, which does get me the 1/9'th I have in the function above.
I have two quick questions:
First) I am having trouble computing the phase. the density function peaks at 12 (see bottom graph), shouldn't the value then the value of the phase be 2*pi+angle[12]
but I am getting a value of

angle[12]
  [1] -2.558724

which puts the phase at 2*pi+angle[12]=3.724462. But that's wrong the phase should be 32 radians. What am I doing wrong?
Second) How do I convert the amplitude which is abs(fty) into the constant in my consine function above, which is 7? abs(fty)[12]=34351.41  , how do I convert this number to 7?. I appreciate your help!
Comment: So, by $R$, you mean the statistical analysis package, and not the real numbers?
Comment: Yes, the stat program. sorry about the confusion, i'll make the edit. thought this forum would be more apt than stack overflow because it really is more of a math question.
Answer: There are several reasons why you don't get what you expect to see:

Your definition of the phase is not what you get from the FFT. The phase you get from the FFT is the phase of the first point of the time domain signal, which in your case is
$$\frac{-2 \pi\cdot 50}{9}+32=-2.91\text{ rad}$$
The reason why you only get an approximate value is answered in the next point.
The frequency you chose does not lie exactly on one of the FFT bins. This is why you get spectral leakage and neither the magnitude nor the phase will be exact estimates of your original signal. Another way to see this is that within the time window there is not an integer number of periods of your signal.
The magnitude of the output of the FFT for a sinusoidal input signal is scaled by $N/2$ (where $N$ is the length of the FFT). So your magnitude estimate is $34351\cdot 2/10001=6.87$ which is a reasonable value given the spectral leakage mentioned above.

If you want to generate a sinusoidal signal which lies exactly on an FFT bin you can use the following formula:
$$y(n)=A\cos\left(2\pi n\frac{i}{N}+\phi\right),\quad n=0,1,\ldots,N-1;\quad i=0,1,\ldots N/2$$
where $n$ is the discrete time index, $N$ is the FFT length (assumed even), $i$ is the desired bin index (note that the index starts at $0$), and $A$ and $\phi$ are some arbitrary constants. In this case the FFT will give you exact values for $A$ and $\phi$. Note that for $\phi$ you get of course the principal value $\phi\in [-\pi,\pi)$.

Computing Fast Fourier Transform in R


How to read the following mathematical notation?

Question: How to read the following mathematical expression?

$$\text{For any strictly increasing function } f:\Bbb R\to \Bbb R, v(x)=f(u(x))$$
Answer: The key bits:
"Strictly increasing function" means that for any $x_1 < x_2$, $f(x_1) < f(x_2)$.
"$f:\mathbb{R} \rightarrow \mathbb{R}$": $f$ is a function whose input is a real number, and whose output is a real number.
"$v(x) = f(u(x))$": The transformation $v$, applied to a real number $x$, gives the same result as applying the transformation $u$ to $x$ and then using the transformed value as the input to $f$.
Comment: Sounds correct.
Comment: It would help if there was a bit of context around what u and v are, but what I've written is a translation of as much as the snippet you posted covers.
Comment: u represents a utility function. This is from microeconomics. So, u(x) is utility obtained from consuming good x.
Answer: "For any strictly increasing function whose name is $f$ which is a function from the reals to the reals, you have that the result of $v(x)$ is the same as the result of $f(u(x))$"


Connect to Android Bluetooth socket

Question: Hi I am trying to create an Android app which will connect to a Blue SMiRF Bluetooth dongle which I want to send data to. I have read over the developer pages and looked at multiple different examples however I am currently having trouble creating a connection to the socket. The Bluetooth portion of the code is pretty much from an example that I was able to find. When trying to connect to the Bluetooth dongle the app gets a force close because there is some error I am not handling correctly. However I have also tried to use the app just to connect to another PC and the connection wont get established correctly for some reason even though I am already paired with the device through the Bluetooth settings before I even run the app. I have posted some of the more important code below for where I think my issue may be. Any help will be very appreciated, please let me know if I should post any additional code.
protected void connect(BluetoothDevice device) {
    //BluetoothSocket socket = null;
    try {
        //Create a Socket connection: need the server's UUID number of registered

        socket = device.createRfcommSocketToServiceRecord(UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));

        socket.connect();
        Log.d("EF-BTBee", ">>Client connectted");

        InputStream inputStream = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write(new byte[] { (byte) 0xa0, 0, 7, 16, 0, 4, 0 });

        new Thread() {
            public void run() {
                    while(true)
                    {
                    try {
                        Log.d("EF-BTBee", ">>Send data thread!");
                        OutputStream outputStream = socket.getOutputStream();
                        outputStream.write(new byte[] { (byte) 0xa2, 0, 7, 16, 0, 4, 0 });
                    } catch (IOException e) {
                        Log.e("EF-BTBee", "", e);
                    }
                    }
            };
        }.start();

    } catch (IOException e) {
        Log.e("EF-BTBee", "", e);
    } finally {
        if (socket != null) {
            try {
                Log.d("EF-BTBee", ">>Client Close");
                socket.close();
                finish();
                return ;
            } catch (IOException e) {
                Log.e("EF-BTBee", "", e);
            }
        }
    }
}`

I have also tried using
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});

        socket = (BluetoothSocket) m.invoke(device, 1);

instead of just the "socket =" line from above and still had no success.
Comment: Thanks for the comment. Unfortunately still having no luck with it creating the Bluetooth socket connection correctly.
Comment: Having reread it (must have read it wrong initially), but is able to create the Bluetooth socket correctly, still having a problem having it actually connect.
Answer: if the device is already paired , then you can use
if(device.getBondState()==device.BOND_BONDED){
    Log.d(TAG,device.getName());
    //BluetoothSocket mSocket=null;
    try {
        mSocket = device.createInsecureRfcommSocketToServiceRecord(MY_UUID);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        Log.d(TAG,"socket not created");
        e1.printStackTrace();
    }
    try{
        mSocket.connect();
    }
    catch(IOException e){
        try {
            mSocket.close();
            Log.d(TAG,"Cannot connect");
        } catch (IOException e1) {
            Log.d(TAG,"Socket not closed");
            e1.printStackTrace();
        }
    }

for the MY_UUID use
private static final UUID MY_UUID = UUID.fromString("0000110E-0000-1000-8000-00805F9B34FB");

Comment: Thank you for the response, it hadn't worked properly for me still for some reason, but I had found a work around. Posted the link as another answer.
Answer: Assuming (check!) the UUID you are using is indeed the server UUID, your best
bet would be to attempt insecure mode of Bluetooth communication. Some devices simply
do not support secure mode.
To do that duplicate your flow, replacing createRfcommSocketToServiceRecord
with createInsecureRfcommSocketToServiceRecord and "createRfcommSocket" with
"createInsecureRfcommSocket".
Or better still: use BTWiz which encapsulate this flow inside. BTWiz also provides a simple async API for Bluetooth IO.
Finally - socket creation & connect()ion must never be performed on UI thread. Your
code is not conclusive here but I suspect you will need to fix this as well.
Gilad Haimov
www.mobileedge.co.il
Answer: Have finally found a work around. Don't know why this has to be done to work properly.
IOException: read failed, socket might closed - Bluetooth on Android 4.3

Connect to Android Bluetooth socket


Liquibase use added function in index does not work for postgres database

Question: i use the following changeset in liquibase to add a function to an existing database
<changeSet author="me" id="2">
<sqlFile encoding="UTF-8"
         path="add_function_my_timestamp.sql"
         relativeToChangelogFile="true"
         splitStatements="false"
         stripComments="false"
         endDelimiter=";"/>

The content of the sql file (add_function_my_timestamp.sql) is
CREATE FUNCTION public.my_timestamp(some_time timestamp without time zone) RETURNS timestamp with time zone
LANGUAGE sql IMMUTABLE
AS $_$
select date_trunc('day', cast($1 AT TIME ZONE 'Europe/Berlin' as date))
       $_$;

Liquibase tells me migration was successful
SQL in file add_function_my_timestamp.sql executed
ChangeSet update_20220223::2::me ran successfully in 13ms

But when i try to use the function created by this changeset in annother changeset when creating an index
<changeSet author="me" id="3">
<preConditions onFail="MARK_RAN">
    <sqlCheck expectedResult="0">
        select count(*)
        from information_schema.constraint_column_usage
        where table_name = 'myEntity'  and constraint_name = 'description_day_timestamp_unique'
    </sqlCheck>
</preConditions>
<sql>
    CREATE UNIQUE INDEX description_day_timestamp_unique ON myEntity."myProperty" USING btree (description, public.my_timestamp(insertdate));
</sql>

then i get the error
Reason: liquibase.exception.DatabaseException: ERROR: function
public.my_timestamp(timestamp with time zone) does not exist
Hint: No function matches the given name and argument types. You
might need to add explicit type casts.

Answer: I found error. Parameter type was wrong
Comment: Please accept your answer, so that your question is marked as resolved.


Why is my `Label` renderer font style not being applied to Label's Text in Xamarin Forms?

Question: I created a Label renderer that uses the UIFontTextStyle.Body font style.
public class LabelBodyCustomRenderer : LabelRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs e)
    {
        base.OnElementChanged(e);
        if (Control != null)
            Control.Font = UIFont.GetPreferredFontForTextStyle(UIFontTextStyle.Body);
    }
}

I then use it in  XAML like below:
<local:LabelBodyRendererClass x:Name="numberAnswerLabel" />
And populate the Label's text in C#:
numberAnswerLabel.Text = App.noa.Text() + " correct";
Doing it this way, the UIFontTextStyle.Body is not applied to the label but if I populate the Label's text in the XAML side like:
<local:LabelBodyRendererClass Text="sample text" />
then font style is applied.
Anyone has any idea why is it working like this? And how to fix it so it will use the style?
Answer: For some reason the font is reset whenever the text or any associated property of the font is changed.
So you would have to handle the TextChanged event and then reapply the font. Have a look at my code for this in the FormsCommunityToolkit.
Whenever one of these properties change, I apply the new font again:
if (e.PropertyName == Label.TextColorProperty.PropertyName
                    || e.PropertyName == Label.FontProperty.PropertyName
                    || e.PropertyName == Label.TextProperty.PropertyName
                    || e.PropertyName == Label.FormattedTextProperty.PropertyName)
{
    control.Font = UIFont.FromName (_effect.FontFamilyName, control.Font.PointSize);
}

It has something to do with the 'bug' reported here.


Decomposing Countable Union of Measurable Sets

Question: Why can every set $E$ in the real numbers with $\mu^{*}(E)=\infty$ be realized as the disjoint union of countably many measurable sets, each of which has finite outer measure?  I'm trying to see this without the approximation properties by open, closed sets.
Edit: Perhaps one can cover by countably many intervals, intersect each with $E$, and then inductively define the $k$th set by subtracting off the union of the previous $k-1$?
Answer: You can write $E$ as a disjoint union of countably many sets with finite outer measure, because you can divide the entire space as such union. Namely, for each $n\in \Bbb Z$, let $I_n = [n,n+1)$. Then
$$\Bbb R = \bigcup_{n\in \Bbb Z} I_n.$$
Now given such $E$, define $E_n=E\cap I_n$. Then $E$ is the disjoint union of those $E_n$ and each $E_n$ has finite outer measure because $E_n\subseteq I_n$.
Bit in general we can't ensure measurability of the subsets involved in the decomposition. See Ats's answer.
Answer: I didn't think this claim would be true. But would appreciate it if someone would correct me if I am wrong. See a proposed counterexample below:
Define $E$ the union of the following two sets:
1) a Vitali set on $[0,1)$
2) $[1,\infty)$
The set in (1) is non-measureable, so and thus prevents this part of $E$ as being expressed by a countable union of measureable sets.
The set in (2) ensures that $E$ has infinite outer measure.
Comment: You're right. I didn't see the _"countably many measurable sets"_ and focused in the _"finite outer measure"_ part.


MySQL, SELECT e JOIN em tabelas com colunas NULL

Question: Pessoal gostaria de ajuda com meu BD.
Tenho 3 perguntas para responder e consigo responder apenas a 3ª, como respondo as outras?
1) Quais transportadoras estão presentes em apenas 1 categoria?
2) Quais transportadoras estão presentes em 2 categorias?
3) Quais transportadoras estão presentes nas 3 categorias?
Obrigado!
Comment: O que você já tentou que não deu certo?
Answer: Essa resposta não é de uma linha mas funciona:
1) Quais transportadoras estão presentes em apenas 1 categoria?
SELECT t.Transportadora, v.Preferencia_Nacional_Voto, v.Transporte_Internacional_Voto, v.Transporte_Rodoaereo_Voto
FROM transportadora t
JOIN voto v on t.Id_Transportadora = v.Id_Transportadora
WHERE

(v.Preferencia_Nacional_Voto is null
and
v.Transporte_Internacional_Voto is not null
and
v.Transporte_Rodoaereo_Voto is not null)

OR

(v.Preferencia_Nacional_Voto is not null
and
v.Transporte_Internacional_Voto is null
and
v.Transporte_Rodoaereo_Voto is not null)

OR

(v.Preferencia_Nacional_Voto is not null
and
v.Transporte_Internacional_Voto is not null
and
v.Transporte_Rodoaereo_Voto is null);

2) Quais transportadoras estão presentes em 2 categorias?
SELECT t.Transportadora, v.Preferencia_Nacional_Voto, v.Transporte_Internacional_Voto, v.Transporte_Rodoaereo_Voto
FROM transportadora t
JOIN voto v on t.Id_Transportadora = v.Id_Transportadora
WHERE

(v.Preferencia_Nacional_Voto is null
and
v.Transporte_Internacional_Voto is null
and
v.Transporte_Rodoaereo_Voto is not null)

OR

(v.Preferencia_Nacional_Voto is null
and
v.Transporte_Internacional_Voto is not null
and
v.Transporte_Rodoaereo_Voto is null)

OR

(v.Preferencia_Nacional_Voto is not null
and
v.Transporte_Internacional_Voto is null
and
v.Transporte_Rodoaereo_Voto is null);

Comment: Obrigado Edney! Depois que postei minha dúvida consegui resolver com apoio do meu irmão que é de TI, seguimos o mesmo raciocínio alterando apenas algumas coisas mas a resposta foi a mesma.


How to change crystal report template

Question: I have created a custom report template and used this custom template in many report.
Now I have added a new formula and text field in report header of custom report template,
the problem is that this new changes in custom template not showing any of the reports using this custom template.
If any changes done is custom report template it should auto refresh all other reports using this custom template...how it can be done??plz help
Thanks
Answer: Templates don't work the way that you describe--they are NOT linked to any of the other reports.
Instead, a template is applied to a target report; its settings (fields, fonts, etc.) are thus transferred to the target.
You will need to re-apply the templates to the desired reports (as painful as that may be).
** edit **
A template can be applied programmatically using the report-application server (RAS) SDK.  Review the CrystalDecisions.CrystalReports.TemplateEngine assembly.  I believe that RAS is a feature of BusinessObjects Enterprise and Crystal Reports Server only.
** edit 2 **
You could skip the template approach entirely, enumerates a report's object hierarchy, and apply formatting as appropriate.  You could adapt RptToXml's code for your needs.
Comment: Thanks for reply can we re-apply the template from vb6/vb.net code, before preview.
Comment: Will CrystalDecisions.CrystalReports.TemplateEngine.dll can work with vb6??
Comment: The RAS SDK does include COM support.  See http://devlibrary.businessobjects.com/businessobjectsxi/en/en/RAS_SDK/rassdk_com_doc/doc/rassdk_com_doc/Template_Lib9.html#1255434

How to change crystal report template


Erdos' Arithmetic Progression of Polignac's Numbers

Question: Paul Erdos has proven that there is an infinite arithmetic progression of Polignac's numbers - odd numbers that cannot be represented as a sum of a prime and a power of $2$, in Erdos, Paul. "On integers of the form $2^k + p$ and some related problems." (Summa Brasil. Math 2 (1950): 113-123.)
What is the explicit form ($a + b n$) of this arithmetic progression?
This web site seems to give the answer:
$$2^{241} \times 3 \times 5 \times 7 \times 13 \times 241 \times n + 1622548664997933681386229593857244360639853287017787157811814622833051518894081$$
without references, and it is not clear how the solution was derived.
I checked and found that the large integer is $459165 \times 2^{241} + 1$.
Comment: Welcome to Math.SE.  You've picked an interesting topic for your first Question.  A couple of suggestions for improvements.  First, you can use mathematical expressions in your posts using $\LaTeX$ thanks to [MathJax software](http://math.stackexchange.com/help/notation).  Second, the crucial details of your Question are presented only as a link to another website.  It might disappear and make your Question unintelligible to Readers.   Better to quote and/or paraphrase the most important material at that link in your post, and then the link can likely be reconstructed should things change.
Comment: I went ahead with the first part of hardmath's suggestion as it's no problem for me, but the second part is something that has to be done by *you*.
Answer: The answer is in the paper itself, page 8 of the link you gave:

Proof of Theorem 3. Clearly every integer satisfies at least one of the following congruences: $0(mod 2),0(mod 3),1(mod 4),3(mod 8),7(mod 12),23(mod 24)$. Therefore, if x is congruent to: $1(mod 2), 1(mod 7), 2(mod 5), 2^3(mod 17), 2^7(mod 13), 2^{23}(mod 241)$,
then for any k, $x-2^k$ is a multiple of one of the primes $3,5,7,13,17,241$. This proves theorem $3$.

The reason this works is Zsigmondy's theorem (there exists $p\mid2^n-1,p\nmid2^m-1$ for $m<n$ if $n\neq6$). These congruences can be converted into the form $a+bn$ by Chinese remainder theorem. The paper itself doesn't mention $2^{241}$ anywhere.


What makes a popular and insightful question?

Question: One of the things that will probably continue to grow our site are popular questions that get many views that contribute to a valuable conversation.
Questions range from topics such as monetary policy to obesity policies. Technical mathematical questions, while valuable, don't tend to get the attention that practical policy questions get. The difference in attention is really quite notable. How do you think people come to find questions they are interested in, and how should that inform the type of content we should encourage on this site?
Answer: I enjoy (some) practical questions. However the SE format is more or less for clear answers. Practical questions often do not have those. For example leading economists disagree on the medium term effects of austerity policies. If we do not wish to fall back to referencing authority as an argument, real answers have to contain some technical information about the model.
I would also argue that attracting visitors is not really our job. There are also some practical questions that would no doubt attract a large number of visitors but the same can be said of the 'news' items of Buzzfeed. The survival of Economics.SE should not be paramount to what the content of Economics.SE is.
Comment: Also read: 10 Ways To Get More Upvotes On Your Question
Comment: Also read: The Economics of Keeping Up With The Kardashians
Comment: well u certainly got my upvote! :^OO
Comment: I agree with this answer.


How can I have "Table view" as the default result instead of "Submissions"

Question: When viewing the results page of a webform node, the submissions views is the default one.
We then have the possibility to display data using these secondary links:
Submissions, Analysis, Table, Download, and Clear.
I'm looking for a way to thange the order of these links.
I would like the table view to be the one displayed by default, instead of the submissions view.
The first part of the screenshot shows how the module works; the other half shows the order I would like for the tabs.
Answer: You may need to do this in a custom module. In your custom module, implement hook_menu_alter:
<code>/**
 * Implements hook_menu_alter.
 */
function MYMODULE_menu_alter(&$items) {
   // Node webform results.
  $items['node/%webform_menu/webform-results'] = array(
    'title' => 'Results',
     // the page callback here is what will display the table.
    'page callback' => 'webform_results_table',
    'page arguments' => array(1, '50'),
    'access callback' => 'webform_results_access',
    'access arguments' => array(1),
    'file' => 'includes/webform.report.inc',
    // Change the weight to change the order
    'weight' => 0,
    'type' => MENU_LOCAL_TASK,

  );
}
</code>
Once you added that to your custom module. Make sure the module is enabled, and clear all caches.

How can I have "Table view" as the default result instead of "Submissions“


how to implement react js pagination

Question: i am using react-js-pagination.
i am able to fetch the data and can show the list of data. but i am trying to impelment paggination using react-js-pagination. i am able to show paggination bar in button but not able to get functionality.
here i am trying show 3 records per page.
UI

    <Pagination
        activePage={this.state.activePage}
        itemsCountPerPage={3}
        totalItemsCount={this.state.projectList.length}
        pageRangeDisplayed={5}
        onChange={this.handlePageChange}
    />

Method
handlePageChange(pageNumber) {
    this.setState({ activePage: pageNumber });
}

Constructor
constructor(props) {
    super(props);

    this.state = {
        activePage: 1,
        projectList: [],
        originalProjectList: []
    };

    this.handlePageChange = this.handlePageChange.bind(this);
}

FUll Component
import React, { Component } from 'react';
import ReactDOM from "react-dom";
import Pagination from "react-js-pagination";
import {
    BrowserRouter as Router,
    Route,
    IndexRoute,
    Link,
} from 'react-router-dom';

import ProjectDetails from './ProjectDetails';
import DashboardContainer from '../UIcomponent/DashboardContainer';

const pannelWidth = {
    width: '90%'
};

const pannelHeader = {
    color: 'white'
};

const pannelFooter = {
    float: 'right'
};

class ProjectList extends Component {
    constructor(props) {
        super(props);

        this.state = {
            activePage: 1,
            searchText: '',
            isdiagram:true,
            isMax:false,
            projectList: [],
            originalProjectList: []
        };

        //this.handlePageChange = this.handlePageChange.bind(this);
        this.projectDetails = this.projectDetails.bind(this);
        this.deleteMessage = this.deleteMessage.bind(this);
        this.updateInputValue = this.updateInputValue.bind(this);
        this.setSize=this.setSize.bind(this);
    }

    componentDidMount() {
        let d = '';
        $.get("http://localhost:8008/api/navigation/all", function (data) {
            d = data;
            this.setState({
                projectList: d,
                originalProjectList: d
            });
        }.bind(this));
    }

    handlePageChange(pageNumber) {
        this.setState({ activePage: pageNumber });
        console.log(this.state.projectList);
    }

    projectDetails(item, index) {
        console.log(index);
    }

    deleteMessage(item, index) {
        showconfrim("Do you want to delete this Project?", this.deleteProject(item, index));
        console.log('delete');
    }

    deleteProject(item, index) {
        $("#confirmwindow").modal('hide');
        console.log('delete');
    }

    setSize(){
        this.setState({
            isMax:!this.state.isMax
        });
        if(!this.state.isMax){
            //clear style for jquery animate;
            $(this.refs.selfdiv).attr("style",null);
            setTimeout(()=>{
                $(this.refs.selfdiv).animate({
                      top:'0px',
                      right: '0px',
                      bottom: '0px',
                      left: '0px'
                },500);
            },100);
        }
        console.log(this.props.children);
        if(this.props.children[1].props['data-event']){
            var self=this;
            setTimeout(()=>{
                self.props.children[1].props['data-event'].call();
            },700);
        }
    }

    updateInputValue(event) {
        this.setState({
            searchText: event.target.value
        }, function () {
            let textToSearch = this.state.searchText;
            let originalData = this.state.projectList;

            if (textToSearch != undefined || textToSearch != '') {
                let searchData = [];
                for (var i = 0; i < this.state.projectList.length; i++) {
                    if (this.state.projectList[i].name.indexOf(textToSearch) != -1 || this.state.projectList[i].description.indexOf(textToSearch) != -1) {
                        searchData.push(this.state.projectList[i]);
                    }
                }

                this.setState({
                    projectList: searchData
                });

            }

            if(textToSearch == '') {
                this.setState({
                    projectList: this.state.originalProjectList,
                });
            }
       });
    }

    render() {

        var listItems = this.state.projectList.map((item, index) => {
            return

                 this.projectDetails(item, index)}>{item.name}
                {item.description}
                 this.deleteMessage(item, index)}>

        });

        return (

Project List

                                <input type="text" className="form-control" placeholder="Search" value={this.state.searchText} onChange={this.updateInputValue}/>

                                <button className="btn btn-sm btn-success">Create New Project</button>

                                    Project Name
                                    Description
                                    Action

                                {listItems}

                        <Pagination
                            activePage={this.state.activePage}
                            itemsCountPerPage={3}
                            totalItemsCount={this.state.projectList.length}
                            pageRangeDisplayed={5}
                            onChange={this.handlePageChange.bind(this)}
                        />

        );
    }
}

export default ProjectList;

Comment: why are using this `this.handlePageChange = this.handlePageChange.bind(this);`. I believe you simply need to call `this.handlePageChange.bind(this);` in your constructor instead of equating it to `this.handlePageChange`
Comment: let me try this
Comment: after removing from constructor it is showing error `Uncaught TypeError: this.setState is not a function`
Comment: hmmm. Remove it from the constructor and use this code `onChange={this.handlePageChange.bind(this)}`
Comment: let me try this
Comment: no, not working
Comment: it would be better if u shared the entire component. Maybe the issue is somewhere else
Comment: i am following this https://www.npmjs.com/package/react-js-pagination
Comment: can u share some more code?
Comment: @AbdulSamad sorry for the late reply. can you check now. i have updated the question
Answer: var indexOfLastTodo = this.state.activePage * this.state.itemPerPage;
var indexOfFirstTodo = indexOfLastTodo - this.state.itemPerPage;
var renderedProjects = this.state.projectList.slice(indexOfFirstTodo, indexOfLastTodo);

var listItems = renderedProjects.map((item, index) => {
    return

         this.projectDetails(item, index, e)}>{item.projectName}
        {item.description}
         { if (window.confirm('All its related data will be deleted. Are you sure you want to delete?')) this.deleteMessage(item, index) } } >
         this.editProject(item, index, e)}>

});

Here need to update the projectList array using slice(firstIndex, lastIndex). then subarray should be use for rander purpose.
and paggination tag should be like below
<Pagination
    activePage={this.state.activePage}
    itemsCountPerPage={this.state.itemPerPage}
    totalItemsCount={this.state.originalProjectList.length}
    pageRangeDisplayed={5}
    onChange={this.handlePageChange.bind(this)}
/>

Answer: Take advantage of ES6.
So, insted of doing something like this:
this.handlePageChange = this.handlePageChange.bind(this);

You can do this:
handlePageChange = (pageNumber) => {
    console.log(`active page is ${pageNumber}`);
    this.setState({activePage: pageNumber});
  }

Hope this helps.
Comment: this is not the problem. i have answer. i'll post this

how to implement react js pagination


Unquote splice without unquote splice?

Question: Is it possible to write the following without using backquote?
(defmacro while (test &rest body)
  `(do ()
       ((not ,test))
     ,@body))

Thought I'd try this as an experiment to understand benefit of backquote.
I got as far as this:
(let* ((test '(> 10))
       (x 0)
       (body '((princ x) (incf x))))
  (list 'do nil (list (list 'not test))))

Which successfully generates:
(DO NIL ((NOT (> 10))))

To finish this I need a way to spread the n elements of the list body into the generated form.
I know that's the entire purpose of the unquote splice ,@ but is this actually impossible without it? Curious... It's similar to what apply does but I don't want to call a function at this point obviously.
Answer: In your case the body contains the remaining forms to be evaluated, and can be added with LIST*:
(let* ((test '(> 10))
       (x 0)
       (body '((princ x) (incf x))))
  (list* 'do
         nil
         (list (list 'not test))
         body))

Another example, where the spliced list is not at the end:
`(,x ,@y ,z)

The above can be written without backquotes as:
(list* x (append y (list z)))

Unquote splice without unquote splice?


Can I use paired sample t-test for the following problem?

Question: Suppose there is an experiment that in each trial returns a two vector of probabilities of size $n$. In each trial I select uniformly a number from ${1,\cdots, n}$ and compare that element in this two vectors. I want to see if in a large number of trials, that randomly selected element of first vector is greater than the second one. For this I am planning to compare their mean values according to paired sample $t$-test. I know that the vector elements have specified variance and expected value. With this information is it justified to use this hypothesis test?
Thank you.
Comment: Is there an expectation that the two values will be positively correlated?
Comment: See also this question http://stats.stackexchange.com/q/47453/16974 on the impact of using a paired t-test for unpaired data (which is your situation), and the impact of using an unpaired t-test for paired data.
Comment: Exactly the opposite is the expectation.
Answer: If there is no expectation of a correlation between the values in the 'pairs' then using a paired t-test will reduce the power of the t-test by reducing the degrees of freedom without a compensating reduction in the variance of the measure.
(If the values are probabilities then they cannot be normally distributed and a standard t-test may be inappropriate. However, if the sample size is large enough the t-test is quite robust to departures from normality, particularly if the actual distribution is symmetrical and the two distributions have similar widths.)


C# Alter add column on excel with OLEDB

Question: I am trying to add a column in Excel sheet with OLEDB by using ALTER statement. I am not able to do it.
I tried with following code but its not working out
sqladd = "ALTER TABLE Data ADD NewRow varchar(50)";

Here, Data is the Sheet name in Excel and NewRow is column name to be added.
Answer: Sadly OleDb does not support the Alter Table statement but you can add columns by using a Create Table statement on an existing sheet with the new column defined at the end. This will actually only add the new column and leave the existing data untouched.
You must open the workbook in ReadWrite mode and not include an IMEX=1 in your extended properties. So first define and open your connection...
string sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                           "Data Source=e:\\Test.xlsx;Mode=ReadWrite;" +
                           "Extended Properties=\"Excel 12.0;HDR=Yes\"";

OleDbConnection oConnection = new OleDbConnection();
oConnection.ConnectionString = sConnectionString;
oConnection.Open();

This spreadsheet already has Col1, Col2 and Col3 on Sheet1, so then...
string sSql = "Create Table [Sheet1$] (Col1 Int, Col2 Int, Col3 Int, NewColumn Int)";

using (OleDbCommand oCmd = new OleDbCommand(sSql, oConnection))
{
    oCmd.ExecuteNonQuery();
}

You can also copy data into a new sheet and drop the original BUT the drop only clears the contents as you cannot actually delete sheets using OleDB...
Select [Sheet1$].*, Null As NewColumn Into [Sheet2] From [Sheet1$]

Drop Table [Sheet1$]

C# Alter add column on excel with OLEDB


Spreadsheet fails to export to Access database when folder is not empty

Question: I'm trying to export a spreadsheet to an Access database. I have three routines: one which creates an Access file with today's date, one which creates a connection with the database and another which imports the data to the Access file.
Everything works correctly when the folder of the dashboard is empty beforehand. However, when there is already another Access file in the folder, it does not export, displaying the following error message:

I couldn't find anything online which was relevant to this particular issue. Does anyone know why this is happening?
'creates an Access database
Sub CreateAccessDB()

    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")

    'the path to create the new access database
    Dim strPath As String

    'an Access object
    Dim objAccess As Object

    date1 = Format(Date, "DD")

    Year = Format(Date, "YYYY")

    Month = Format(Date, "MM")

    MonthChar = MonthName(Month, False)

    'states the directory for our database
    strPath = "Z:\Danny Tool Test Folder\Database\" & Year & "\" & _
    Month & ". " & MonthChar & " " & Year & "\WeeklyActivity_" & date1 & Month & Year

    'if the file already exists, we open it
    If fso.FileExists(strPath & ".accdb") Then
        Call OpenAccessDatabase(strPath & ".accdb")

    'if the file does not exist, we create it
    Else

        Set objAccess = CreateObject("Access.Application")
        Call objAccess.NewCurrentDatabase(strPath)
        objAccess.Quit
        Call OpenAccessDatabase(strPath & ".accdb")

    End If

End Sub

Sub ADO_Connection_1()

    date1 = Format(Date, "DD")

    Year = Format(Date, "YYYY")

    Month = Format(Date, "MM")

    MonthChar = MonthName(Month, False)

    'Creating objects of Connection and Recordset
    Dim conn As New Connection, rec As New Recordset

    Dim DBPATH, PRVD, connString As String
    'Declaring fully qualified name of database. Change it with your database's location and name.
    DBPATH = "Z:\Danny Tool Test Folder\Database\" & Year & "\" & _
    Month & ". " & MonthChar & " " & Year & "\WeeklyActivity_" & date1 & Month & Year & _
    ".accdb"

    'This is the connection provider. Remember this for your interview.
    PRVD = "Microsoft.ace.OLEDB.12.0;"

    'This is the connection string that you will require when opening the connection.
    connString = "Provider=" & PRVD & "Data Source=" & DBPATH

    'opening the connection
    conn.Open connString

End Sub

'exports the data from the spreadsheet to the Access database
Sub ExportReport()

    'declaring our files, directories, tables and field names
    Dim strPathFile As String, strFile As String, strPath As String, FileName As String
    Dim strTable As String
    Dim blnHasFieldNames As Boolean
    Dim MasterDB As Worksheet

    date1 = Format(Date, "DD")

    'Declaring year value of 1 month
    'This is important to compare datasets from last month
    If date1 > 26 Then
    Year_1M = Format(Date - 37, "YYYY")

    Else
    Year_1M = Format(Date - 27, "YYYY")

    End If

    'Declaring month value of 1 month
    'This is important to compare datasets from last month
    If date1 > 26 Then
    Month_1M = Format(Date - 37, "MM")

    Else
    Month_1M = Format(Date - 27, "MM")

    End If

    'This translates the current month from number to character format
    MonthChar_1 = MonthName(Month_1M, False)

    'setting Account sheet as MASTERFILE & the date
    sheet = "Overall Activity" & "!"

    Application.DisplayAlerts = False

    blnHasFieldNames = True

    strPath = "Z:\Danny Tool Test Folder\"

    strTable = "ActivityTable"

    strFile = Dir(strPath & "Weekly Activity Reporting Tool.xlsm")
      strPathFile = strPath & strFile
      DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
            strTable, strPathFile, blnHasFieldNames, sheet

    Application.DisplayAlerts = True

End Sub

'function opens the Access database
Public Function OpenAccessDatabase(strPath As String)

    If Not IsNull(strPath) Then Shell "MSACCESS.EXE """ & strPath & """", vbNormalFocus

End Function

(It fails at DoCmd.TransferSpreadsheet)
Comment: Please include the code you are using in your question.
Comment: Do you want to export the spreadsheet or import it? **DoCmd.TransferSpreadsheet acImport**
Comment: Code is in Access to create another Access file and import Excel data to that other file? Year and Month are intrinsic functions - I would not use them as variable names. Should use Option Explicit at top of module and explicitly declare all variables.
Comment: What is code for OpenAccessDatabase() procedure?
Comment: Yes I meant to say I'm looking for import spreadsheet to Access database. I have publicly declared these variables in a different module as I use them throughout the project - Option Explicit is included at the top of this module.
Comment: @June7 I have updated code to include this
Comment: Okay, so code is in EXCEL which creates an ACCESS file and attempts to import spreadsheet into that new db file. I have to ask - why is EXCEL used as data repository?
Comment: Still seems to be some code missing. The conn variable is local to procedure. CreateAccessDB creates and opens file which stays open and ExportReport fails because file is locked. Nothing in ExportReport procedure establishes Access object or connection.  Don't see how code works at all. Also, RangeToRecordset and ADO_Connection_1 procedures are not even called, why provide irrelevant code and omit relevant? I changed Year and Month to Yr and Mn.
Answer: Try using the correct spreadsheet format:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, _
    strTable, strPathFile, blnHasFieldNames, sheet

AcSpreadSheetType enumeration (Access)
Comment: This code line is confirmed and verified here, so something else is going on. Double-check everything.
Answer: This simple procedure creates database if doesn't already exist then transfers data from Excel to database.
Sub ExcelToAccess()
    Dim strDbPath As String
    Dim strExFile As String
    Dim strTable As String
    Dim Sht As String
    Dim objAccess As Object
    Dim date1 As String, Yr As String, Mn As String
    date1 = Format(Date, "DD")
    Yr = Format(Date, "YYYY")
    Mn = Format(Date, "MM")
    Sht = "Sheet1!"
    strTable = "Test"
    strExFile = "C:\Users\Owner\June\MyStuff\Condos.xlsm"
    strDbPath = "C:\Users\Owner\June\Forums\WeeklyActivity_" & date1 & Mn & Yr & ".accdb"

    Set objAccess = CreateObject("Access.Application")
    objAccess.Visible = False

    If Dir(strDbPath) = "" Then
        objAccess.NewCurrentDatabase(strDbPath)
    Else
        objAccess.OpenCurrentDatabase (strDbPath)
    End If

    objAccess.DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, strTable, strExFile, True, Sht
End Sub


Features versus Configuration Management

Question: I am building a Drupal 8 site and I have done some research whenever I should use Configuration Management or Features to export configuration and include it in my profile.
In order to export and import configuration with Configuration Management from one site to another, sites have to have the same UUID. It means that they have to be a clone of each other.
There are two ways to achieve this:

Clone the DB from one site to the other in order to have the same UUID
Programmatically modify the UUID to match the one in configuration

The site I am going to build will have its own profile installation and my intention is to keep configuration or features within the profile.
I have already created my profile installation but now I have to include configuration in a way it does not depend on UUID so I can:

Spin up a fresh development environment using profile installation
Develop new functionalities
Export new functionalities in the profile
Deploy it to prod and apply new functionalities without re-installing the site

How can I export configuration and add it to my profile?
Should I use Configuration Management and manually remove the UUID or should I use Features and add it to my profile?
Using the Features module, I am not able to set the destination for my features. I would like to put all my features under profiles/my_profile/feature. When I create it I am not able to set a destination so it gets saved under modules/custom. I manually moved it from modules/custom to profile/my_profile/features. If I update it, it will not update the features I moved but instead it will create a new feature with the same name under modules/custom. How can I setup a custom destination?
Answer: I would advise using Features. This would allow not only exporting functionality and compartmentalizing it a lot easier, but you could also have the ability to bring in features later from an external source, and/or, use certain features here on a different site that didnt start with your installation profile.
Currently there is an issue to support custom path generation for Features:  https://www.drupal.org/node/2693123
When that is addressed, you can generate the features to the path of your choice. For now, you can generate the result, grab it out of modules/custom and place the generated feature module into your profile.
Comment: I am trying to use features but it generates it on modules/custom while I would like to keep it under profile/my_profile/my_feature. I can generate it and move it but then when I want to update it puts it again under modules/custom. How can I set a custom destination?
Comment: I would file a issue in the Features queue (I think one already exists for the destination override). In the meantime, generate them and place them in your profile. I had done this previously for Drupal 7.
Comment: I have found the issue: https://www.drupal.org/node/2693123 . At the moment I am generating it under modules/custom and leaving it there, when I have my first release I will move it into profile/_myprofile_name/feature. If you expand your answer with more instructions I can give you the bounty.
Answer: Adding some counter-argument info for assimilation/consideration:
Features module maintainer Mike Potter is encouraging folks to stop using the features module as ...

Features module is really no longer needed and shouldn't be used to
  deploy configuration. However, Features still provides a powerful UI
  and plugin system for managing configuration and in combination with
  new modules such as Config Actions it might finally achieve its dream
  of packaging reusable functionality.


How to use find to append a string to every file in a directory

Question: I'm trying to append a fixed string to each file in a folder (and its subfolders) whilst skipping the .git directory.
I expected that something like this would work
 find . -type f ! -path "./.git/*" -exec "echo \"hello world\" >> {}" \;

and if I run it with an additional echo it does generate commands that look right
bash> find . -type f ! -path "./.git/*" -exec echo "echo \"hello world\" >> {}" \;
echo "hello world" >> ./foo.txt
echo "hello world" >> ./bar.txt
...

and those commands do what I want when I run them directly from the shell but when I run it from find I get this:
bash> find . -type f ! -path "./.git/*" -exec "echo \"hello world\" >> {}" \;
find: echo "hello world" >> ./foo.txt: No such file or directory
find: echo "hello world" >> ./bar.txt: No such file or directory
...

but those files do exist because when I list the directory I get this:
bash> ls
bar.txt  baz.txt  foo.txt  subfolder/

I guess that I'm not quoting something I need to but I've tried everything I can think of (double and single quote, escaping and not escaping the inner quotes etc...).
Can someone explain what is wrong with my command and how to achieve the the addition of the fixed string to the files?
Comment: @user000001. I tried this before posting and it doesn't work for me. It creates a file called {} and puts "hello world" in it once for each file that find returns
Comment: Try without the quotes: `find . -type f ! -path "./.git/*" -exec echo "hello world" >> {} \;`
Comment: BTW, no such thing as "bash find".  `find` is a separate program unconnected to the shell you are using it from, just like `ls`, `cp`, `cut`, `rm` etc.  It is not a shell built-in.  You can test this with `type find`.
Comment: @cdarke Thanks, title amended.
Answer: You need to instruct the shell to do it:
find . -type f ! -path "./.git/*" -exec sh -c "echo hello world >> {}" \;

Comment: Great, that works. Can you explain what the problem with the original command was. It seems to be building the correct command string but for some reason it can't find the files.
Comment: To handle file that can contains special chars, you can use: `find . -type f ! -path "./.git/*" -exec sh -c 'echo "hello world" >> "$1"' _ {} \;` (the filename come as a argument)


O que é uma Engine?

Question: Li um artigo sobre o Razor. Esse artigo diz que se trata de uma engine view, mas o que vem a ser uma engine? De acordo com o artigo o Razor é uma nova sintaxe que trabalha com CSS e HTML para facilitar a vida dos programadores com um código mais limpo e legível.
Answer: Engine neste contexto pode ser um framework, uma biblioteca, um conjunto de ferramentas, um SDK, ou seja, softwares que estão disponíveis para desempenhar uma tarefa específica e que fora desenvolvido, provavelmente, por especialistas.
O desenvolver provavelmente tratará como SDK ou algo semelhante quando está desenvolvendo. Mas quando estiver rodando não terá o conjunto completo, mas só o executável que fará o trabalho que ele precisa, portanto é só a biblioteca que fará o trabalho.
O termo é muito usado em jogos e outras formas de renderização, mas pode ser usado para diversas qualificações. O Google é um engine de busca.
O engine que costuma ser traduzido como motor, mas eu prefiro mecanismo. Ele é o que faz funcionar algo, é a parte principal de algo maior e sem ele o maior não serviria para nada ou seria muito difícil criá-lo.
O Razor é um renderizador de páginas HTML, assim como o navegador tem um renderizador, mas no navegador o engine pega o HTML e outros componentes e desenha na tela o que for preciso, enquanto o Razor pega um código, que pode ter HTML padrão, HTML que só ele entende, e programação mesmo (em geral C#), e cria o código HTML puro padrão que será entregue para o servidor HTTP.
Ele pode ser usado em qualquer aplicação que precise gerar HTML. O primeiro uso dele é renderizar a view do ASP.NET MVC. Ainda é muito usado no ASP.NET Core. Mas há gente usando isoladamente, ou seus próprios engines de MVC ou outra forma de lidar como interação via HTML.
O Razor permite que misture códigos de linguagens diferentes (HTML/C#), e que partes destes códigos sejam executados no momento da renderização. As primeiras versões dele só o C# podia fazer isto, mas agora é possível até mesmo usar HTML personalizado que ele entende e transforma no que é padrão, desde que tenha um componente que o ensine fazer a transformação de forma adequada, e este componente será escrito em C# em outro local. Então ele é tão poderoso que você pode criar suas próprias tags HTML (que os navegadores não entenderão, por isso é feita a transformação para um código padrão W3C.
Neste caso todo o código controle da view, o controller, o model, o tratamento de rotas, todas essas coisas do MVC não as outras "partes do carro". O MVC não deixa de ser um engine. Você pode ter motores dentro de motores, ou motores auxiliares trabalhando em conjunto com o principal.
O .NET não deixa de ser um engine, embora não seja comum usar esse termo.
Um computador é o dispositivo (device), mas raramente se usa este termo. Engine é um termo bastante geral que tomou mais forma em algumas áreas da computação.
Coloquei no GitHub para referência futura.
Comment: Então EngineCode seria um conjunto de códigos? por exemplo um código que faz diversas funçoes?
Comment: @MariaClara desconheço o termo.
Answer: Engine é como se fosse um motor com suporte a varias funções especificas para cada tipo de uso no caso do Razor como citado é uma engine View que trabalha com modo  gráfico HTML e CSS, as engines são feitas para facilitar a vida em geral de um programador trazendo com ela funções pré-programadas facilitando seu uso no  dia a dia.
Existem  vários tipos de Engines como Game Engine : Uma  muito  conhecida é a Nvidia, Unity entre outras.
As engines são  nada mais nada menos do que bibliotecas que facilitam  sua compilação, e descomplica grandes syntaxs, Imagine só  se você precisasse criar uma função para exibir uma tabela de  produtos ? Poderia ser um pouco  trabalhoso  no caso do Razor você pode utilizar o DataGrid apenas passando os parâmetros necessários, e deixando  que o Razor faça o trabalho por você de buscar e organizar os dados na tabela.

Bom saber

O termo "game engine" originou-se no meio da década de 1990, especialmente em conexão com jogos 3D, como tiro em primeira pessoa. O primeiro motor 3D a ser usado para criar jogos de computador foi o Freescape Engine, desenvolvido pela Incentive Software em 1986, usado para criar jogos de tiro em primeira pessoa a partir de 1987. Preços de motores de jogos variam muito, desde a gratuidade e valores baixos, como US$ 99,00, até valores exorbitantes como US$ 31.200,00 ou mais caros. O processo de desenvolvimento de jogos é frequentemente agilizado, quando um mesmo motor de jogo é usado para criar diferentes jogos
Fonte
WikiPedia
Comment: No caso do Razor, por que dizer que é um "motor"? Quando você disse uma biblioteca, eu consigo imaginar várias classes com suas funções, etc. Mas um motor...
Comment: sim, necesse caso é uma classe com varias funções, que agrupadas geram  esse motor; vamos imaginar um motor de um carro  com suas varias peças juntas fazendo ele funcionar, no caso  do  razor creio  que ele é em c#, ele não  deixa se ser uma biblioteca, mas  ganha esse titulo de engine por porssuir  muitas funcionalidade que de ante-mão te facilitando  assim poupando-o tempo e escrevendo menas  linhas de  codigo o conceito de engine pode parecer um pouco complexo mas  tendo  em mente q ela é uma junção de funcionalidades que poderiam ser escritas por vc mas  q por conveniencia já estão prontas
Comment: O Razor é o motor, pois ele faz as coisas funcionarem. Consegue me dar exemplos do que seriam as outras partes do "carro"?

O que é uma Engine?


remove this useless assignment to local variable c#

Question: var  stopFyon = new StopFYON();
IEnumerable<CarOnline> carOnlineData = (IEnumerable<CarOnline>)vehrep.GetCarOnlineDetail(maintainStopFactoryOrderNo.VehicleDetail).Result;

if (carOnlineData.Any())
{
    stopFyon = vehtran.CreateStopFactoryOrderNo(carOnlineData, maintainStopFactoryOrderNo, lastUpdatedBy);
}
else
{
    stopFyon = vehtran.CreateStopFactoryOrderNo(null, maintainStopFactoryOrderNo, lastUpdatedBy);
}
return gen.GetResponse((Int16)ResultCode.Success, (Int16)MsgType.Ok, null, vehrep.StopFactoryOrderNo(stopFyon));

I got warning error when using sonar code analysis:

remove this useless assignment to local variable stopFyon
Comment: `StopFYON stopFyon;`? you don't need to assign a new instance because in the if/else you always assign a new value to it overwriting it.
Comment: So How can I create a global variable for this?
Comment: @RameshRajendran just look at the first comment
Comment: By not using `var` if you give it a value of the expected type. or of a parentclass/interface type that the StopFYON inherits of you can declare it anywhere without the need to use `var`.
Answer: Don't use var:
StopFYON stopFyon;

The reason for the warning is that you initialize the variable with the default constructor(which could theoretically be a very expensive call, at least it's confusing). But this assignment gets overridden in all branches(the if and the else). So it's useless.
If you would use stopFyon before the if the warning would also disappear.
Comment: @RameshRajendran: you're right. But that costs nothing. You don't create an instance which you don't use. The warning is not about unassigned variables but about useless instances. It wants to protect you from careless mistakes.
Comment: But why it's not showing an error while am giving `StopFYON stopFyon = null`?? this is also a useless assignment.
Answer: The message is pretty straightforward:
IEnumerable<CarOnline> carOnlineData = (IEnumerable<CarOnline>)vehrep.GetCarOnlineDetail(maintainStopFactoryOrderNo.VehicleDetail).Result;

StopFYON stopFyon;

if (carOnlineData.Any())
    stopFyon = vehtran.CreateStopFactoryOrderNo(carOnlineData, maintainStopFactoryOrderNo, lastUpdatedBy);
else
    stopFyon = vehtran.CreateStopFactoryOrderNo(null, maintainStopFactoryOrderNo, lastUpdatedBy);

The first assignment:
var stopFyon = new StopFYON();

is useless since another assignment is performed within the if statement right after.
Comment: Because you initialize your variable with `null`. A `null` assignment is not expensive; the assignment that uses a constructor, on the opposite, could be expensive.
Comment: But why it's not showing an error while am giving `StopFYON stopFyon = null`?? this is also a useless assignment
Answer: It is working, when I change the code StopFYON stopFyon = null instead of var  stopFyon = new StopFYON();
Comment: `= Null` don't think you need that part since it should default to null. `StopFYON stopFyon;` should suffice.
Comment: @Dragonvil . Yeah! That's better than me. But don't know why it's not showing an error while am giving `StopFYON stopFyon = null`?? this is also a useless assignment?
Answer: just assign the type using StopFYON stopFyon; no need to initialize as your if condition blocks are assigning the actual values.


Why there is an issu

Question: I'm trying to create a simple while loop but there is a problem:
import random

weekDays = ["Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday"]

n = 0
while n < len(weekDays):
    diceRoll = random.randint(4, 6)
    print("On {} perform {} reps per set".format(weekDays[n]))
    n = n + 1

The issue I do not get it:

print("On {} perform {} reps per set".format(weekDays[n]))
IndexError:Replacement index 1 out of range for positional args tuple

Will be appreciate for any suggestions.
Comment: In accordance with [ask], please research your issue before posting here. This is a duplicate of [IndexError: Replacement index 1 out of range for positional args tuple](https://stackoverflow.com/questions/63655115/indexerror-replacement-index-1-out-of-range-for-positional-args-tuple)
Comment: How many placeholders do you have in the string? How many values are you giving to `format`?
Comment: Why use a `while` loop at all? `for day in weekDays: print("...".format(day, diceRol))`. You aren't using `n` except to index the list, so you can get rid of it altogether.
Answer: Your format call is missing an argument (diceRoll).  It's easier to keep track of the values you're formatting if you use f-strings rather than format, since they go straight in the string:
import random

weekDays = ["Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday", "Sunday"]
for day in weekDays:
    diceRoll = random.randint(4, 6)
    print(f"On {day} perform {diceRoll} reps per set")

Answer: There needs two arguments in print("On {} perform {} reps per set".format(weekDays[n])).
You could change it to print("On {} perform reps per set".format(weekDays[n])). It will be worked.

Why there is an issu


In an array of strings, how can I print the first element x number of times, and then print the second element x number of times, and so on?

Question: For example:
myArray = ["Dave", "Tom", "Pete"];
I would like to print each element in myArray 3 times in the following format:
Dave
Dave
Dave
Tom
Tom
Tom
Pete
Pete
Pete
forEach loops (at least on their own) don't seem to handle what I'm trying to do. I'm going back and forth between different array and string methods but I feel there's a combination I'm missing. Any help would be appreciated. Thanks.
Comment: This isn't a code writing or tutorial service. You are expected to show your own attempts to solve your issue yourself...or any research that you have done. Then people help you correct any specific  problems  in ***your code***
Comment: use a nested for loop plz
Comment: Think about a function or two you could write to do this. (Then try it and post your attempt)
Answer: All you really need to do here is have two nested for loops. The first one looping through your array of strings; the second loop contains whatever mechanism you want to use for "printing" your strings. I have two variables here, one being your array, and the second a simple x to choose how many times you want your string printed.
In addition, as the comment above said, this isn't a code writing service. Next time you really need to give an example of what you did that didn't work, and asked a question based on that. A forEach loop should actually work just fine as well.
let myArray = ["Dave", "Tom", "Pete"];
let x = 3;
for (let i = 0; i < myArray.length; i++) {
    for (let j = 0; j < x.length; j++) {
        // whatever printing mechanism you would like goes here. let's make some html
        let element = document.createElement('p');
        element.textContent = myArray[i];
        document.body.appendChild(element);
    }
}

Answer: If you want to do something "x times" for every element, that's exactly what you should do: "for every element": "do ... x times"

["Dave", "Tom", "Pete"].forEach((e) => {
  for (let i = 0; i < 3; i++) console.log(e);
});


bash Split string to array

Question: How to split
"DB-ConnString","Server=tcp:database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;User ID=DATAFACTORY;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication='Identity';"
to an array?
IFS="," read -r -a my_array <<< "$line" doesn't seem to do the trick.
Comment: it looks like you want split the string based on a comma but you appear to have a comma as part of the data in the `"Server..."` portion; how many elements are you expecting in the array? 2 or 3?  do you also want to split on `;`? it would help if you update the question with what you expect the array contents to look like
Comment: Strings can be split in thousands of ways. You need to explain how you want it split.
Comment: comma is the delimiter. key being "DB-ConnString", value is the rest of it - "Server=tcp:database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;User ID=DATAFACTORY;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication='Identity';"
Comment: I'd suggest using perl or python or another language with a real CSV parser available.
Answer: Reset your delimiter to something not embedded in your data.
$: echo "$line"
"DB-ConnString","Server=tcp:database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;User ID=DATAFACTORY;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication='Identity';"

$: IFS="|" read -r -a my_array <<< "${line//\",\"/\"\|\"}" # change "," to "|"
$: printf "[%s]\n" "${my_array[@]}"                        # show the results
["DB-ConnString"]
["Server=tcp:database.windows.net,1433;Initial Catalog=db;Persist Security Info=False;User ID=DATAFACTORY;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;Authentication='Identity';"]

bash Split string to array


Client/Server - Server may change, how to abstract that away?

Question: I have to implement a Client/Server architecture in Java, where the Server may change in the future (well, it definitely will at some point).
What would be the best way to take that possible change into account? Here's what I thought of:
1.) Create an interface for the Server:
public interface AlarmService {
  ...
}

2.) Create a concrete implementation:
public AlarmServiceImpl implements AlarmService {
  ...
}

3.) Write a static factory to get some sort of implementation
public AlarmServiceFactory {
  private AlarmService AlarmServiceFactory() {
    return new AlarmServiceImpl();
  }
}

Is this a good idea? Or is this too much? Or maybe I shouldn't take that change into account?
Answer: There is a mantra that goes something along the lines of "always program to an interface, not an implementation". While true in many cases, a lot of time it is overkill. In your case though, since you KNOW the implementation will change, and you won't have as much control over the clients as you do the server, you should definitely program to an interface.
The factory will only really be useful in two cases. The first being that you have several Implementations available, and you need to decide which one to produce at run-time. The other is if you are creating new AlarmServiceImpl() all over the place, and the implementation changes. Otherwise, I see the use of a factory as overkill.
So.. definitely build an interface for the client/server to communicate through. Only use a factory if swapping or choosing implementations isn't one or two lines of code, and is scattered in various places.
Answer: Consider Business Delegate pattern. It's more powerful than just interface/implementation suggested already.
Answer: the question is not cent percent clear to me . but i think that you want to make your server flexible so that you can easily change the server without changing other parts. am i right? if yes then i think you should take a look over IOC pattern.
regards
arefin.


Executar função no app android quando um botão em uma página web for pressionado

Question: tenho um app (desenvolvido no android studio) que obtém dados do acelerômetro. Agora preciso fazer uma página web com um botão, quando o botão da página web for clicado, os dados do acelerômetro que já tenho em uma variável, serão exibidos na página web. Preciso de dicas de como posso implementar a parte web e a parte de escuta do app android. Obrigado desde já!
Comment: Só conheço um método usando Javascript Interface, porém essa interface só se comunica com  `webView`, caso queira dar uma olhada tem o link do google https://developer.android.com/guide/webapps/webview#java
Comment: Bruno, obrigado pela resposta. Acho que me expressei errado. A parte da WEB não rodará dentro do app android.
Comment: Olha acho que você vai ter que criar uma api.
Comment: Vai ter que criar uma api sim, com json vai ficar mais fácil para adaptar a linguagem do app, mais porque você não fez um app hibrido? seria mais fácil lidar com essas coisas e você poderia ir na onda dos "apps lite e apps go". Se você quer uma sugestão alternativa a api, utilize um pwa na web e link ele no aplicativo com webview, eu não consigo testar aqui, pois o meu pc não aguenta o visual studio e o android studio juntos, então não vou mostrar como se faz, mais alguém deve saber como fazer. *O pwa permitirá que as coisas funcionem sem internet então use pwa e não uma pagina da web simples,
Answer: Quando você quer integrar web e app. Use uma api. Para construir api em php tem laravel, slim framework e tem muitos outros framework que você pode usar.


How to refresh a JPanel at runtime?

Question: I have found that there are many similar topics here, but my problem is something more complicated.
Background of my problem:-
I have a JFrame called Main. On this JFrame I have two buttons and one JPanel called WorkingPanel. Then I have another JPanel(called PlayerPanel) but this one is a seprate file (as a class).
Now I want that when I click a button, it should change WorkingPanel to PlayerPanel. I have wrote following code.
private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
    // TODO add your handling code here:
    WorkingPanel = new PlayerPanel();
    System.out.println(WorkingPanel.getName());
    WorkingPanel.revalidate();
    WorkingPanel.repaint();

    WorkingPanel.setVisible(true);

    Window.revalidate();
    Window.repaint();
}

Please guide me, Thanks.
Comment: Use standard Java naming conventions is you want people to read your code. Variable names do NOT start with an upper case character. Class names DO start with an upper case character, so you code is very confusing to read. Follow guidelines from your text book or tutorials. Don't make up your own!
Answer:
I have found that there are many similar topics here, but my problem is something more complicated.

On the contrary, your description is of a rather basic problem that is very easily solved by using a CardLayout. I suggest that you do this now.  If you had it in place your method could be as simple as:
private void MenuButtonPlayerViewMouseClicked(java.awt.event.MouseEvent evt) {
   cardLayout.show(cardPanel, WORKING_PANEL);
}

where cardLayout is your CardLayout variable, cardPanel is the JPanel that displays the "cards" that displays the swapping JPanels, and WORKING_PANEL is a String constant that you used when you added your WorkingPanel instance to the cardPanel.

Point 2:
Don't use a MouseListener on a JButton as it won't behave correctly. For instance, if you disable the button via setEnabled(true) the button won't truly be disabled. Instead use an ActionListener with JButtons as the tutorials will show you. That is what they are for.

Edit
For examples of CardLayout-using GUI's, please check out:

getting Jcomponent from other class changes frame size
Java CardLayout Main Menu Problem
Change size of JPanel using CardLayout
Java CardLayout JPanel moves up, when second JPanel added
Java swing; How to toggle panel's visibility?
Clear components of JFrame and add new componets on the same JFrame
gui multiple frames switch
JLabel displaying countdown, java

This one is unusual in that it uses a CardLayout and has one panel fading into the other panel:

CardLayout showing two panels, flashing.
Answer: You could use CardLayout instead of that approach. You will be able to switch between different panels very easy and efficiently. It's also wort of mentioning that use of CardLayout is less verbose approach.
Comment: By writing your code with hands. **EDIT:** Actually, you can set it very easy in NetBeans for some `JPanel`. Just right-click on desired `JPanel` > Set Layout > CardLayout
Comment: @ZulfiqarJunejo: I strongly urge you not to use a code generator until you understand the library that you're using. Code by hand first to gain that understanding.
Comment: @ZulfiqarJunejo: case in point, you're using a MouseListener on a JButton, something that you should almost never do, and something that the Oracle Swing tutorials will tell you. Please have a look at the tutorials.
Comment: I am using Netbeans visual designer, how can I use CardLayout with it?
Answer: Use a CardLayout, containing your two panels, but only showing one at a time. The CardLayout is documented, with examples, in the Swing tutorial.

How to refresh a JPanel at runtime?


Solving for a matrix in equation form

Question: Solve for $X$ assuming all matrices are n x n and invertible as needed.
$$B(X+A)^{-1}=C$$
I solved this the following way:

Multiply both sides by $(X+A)$
Multiply both sides by the inverse of $C$
Isolate $X$

My final answer is $$X=BC^{-1}-A$$ but the book got the answer $$X=C^{-1}B-A$$
I know that this problem comes from multiplication.  When I multiply both sides by any value, how do I know which side to put it on?
For instance, if I have the equation:
$A=XB$ and I want to solve for $X$, I have to multiply both sides by $B^{-1}$
But which side do I put the new multiple on?  Is it $AB^{-1}=XBB^{-1}$ or is it $B^{-1}A=B^{-1}XB$
Comment: If you want to solve $AX=B$ for $X$ and you multiply both sides on the right by $B^{-1}$, you get $AB^{-1}=XBB^{-1}$ and because $BB^{-1}=I$, it follows that $X=AB^{-1}$. If you multiply on left, however, you get $B^{-1}A=B^{-1}XB$ and there's no way to simplify this unless you go back to the original equation.
Comment: Ah okay I understand now, since in matrix multiplication $ABA \ne AAB$, so then both are valid, I just have to carefully pick which one to use based off of the question because one may have no answer, while the other will?
Answer: Since matrix multiplication is non-commuting, you can only say $AA^{-1}=1$, but you can't say $AXA^{-1}=X$ since $AXA^{-1}$ isn't generally the same as $AA^{-1}X$.
In the case of $B(X+A)^{-1}=C$, we get:
$B(X+A)^{-1}=C\\
B(X+A)^{-1}(X+A)=B=C(X+A)\\C^{-1}C(X+A)=X+A=C^{-1}B\\X=C^{-1}B - A$
Answer: You can multiply by $B^{-1}$ you can multiply left or right side. $AB^{-1}=XBB^{-1}$ is correct and $B^{-1}A=B^{-1}XB$ is also correct. But if you want to use identity $B^{-1}B=BB^{-1}=I$ you should have $B$ "near" $B^{-1}$. You can simplify $XB^{-1}B=XI=X$, but not $B^{-1}XB$.
Answer: If the matrix you are 'getting rid of' is the left factor, multiply on the left by its inverse (since the inverse and the matrix must be next to each other).  Similarly if the matrix you're getting rid of is the right factor, multiply by its inverse on the right.
There is another thing going on in your problem, which is for invertible matrices $M, N$, we have $(MN)^{-1}=N^{-1}M^{-1}$, since $MNN^{-1}M^{-1}=MIM^{-1}=MM^{-1}=I$.
So in your problem you would get $X+A=(B^{-1}C)^{-1}=C^{-1}(B^{-1})^{-1}=C^{-1}B$

Solving for a matrix in equation form


Como atualizar um  de 30 em 30 segundos JAVA WEB

Question: Estou tentando criar um painel mostrando o resultado dos vendedores em um telão na minha empresa.
preciso temporizar o painel de 30 em 30 segundos para que os resultados estejam sempre atualizados.
não tenho ideia de como fazer, toda ajuda é bem vinda.
Comment: Está usando algum framework tipo primefaces ou rich faces?
Comment: Estou usando Primefaces
Answer: Tenta isso Brother?
<code><h:form id="formulario">
    <p:poll interval="3" listener="#{bean.atualizaForm}" update="formulario" />
    ...

</h:form>
</code>
Comment: To testando aqui, mas já vi que vai dar certo! Muito obrigado!


Posterior predictive regression lines in Bayesian linear regression seem to exclude most data points

Question: I have some data to which I'd like to fit a GLM using PyMC3. Here are the posterior predictive regression lines plotted along with the data points:

Domain knowledge indicates that a linear model with zero-intercept is appropriate and the data looks linearly related. However, it feels as though the fit is not OK since the post predictive lines seem to cover only a few data points, leaving most out. Is this intuition correct? In this case, 64% of the data points are not covered by the regression lines.
The PyMC3 model I used is really simple:
<code>with pm.Model() as geom_glm:
    pm.GLM.from_formula('delta_p ~ 0 + sot', dados)
    step = pm.Metropolis()
    trace = pm.sample(draws = 10**4, step = step)

plt.plot(dados['sot'], dados['delta_p'], 'x', label='dados')
pm.plot_posterior_predictive_glm(trace,
                                 lm = lambda x, sample: sample['sot'] * x,
                                 eval = dados['sot'],
                                 label = 'posterior predictive check',
                                 samples = 1000)
</code>
If I allow the intercept to be inferred from the data as well (even though domain knowledge indicates this is inappropriate), the situation doesn't seem to improve much:
Answer: It's nothing to worry about. Bayesian linear regression is (to simplify) just linear regression with random variables for the slope and intercept.
Now, the posterior distributions for the slope and intercept random variables permit many credible values for those parameters. And those combinations of credible parameters permit many credible regression lines to be drawn:

(Source for stylised image: http://www.indiana.edu/~jkkteach/WorkshopUWM2012.html)
The shaded area on your chart is simply a similar representation for the above.
I would argue the opposite to you: I would argue that you want the shaded region to be as tight as possible, to have more "confidence" (in the non-technical sense of the word) in the fitted regression line.
Comment: The regression line represents the conditional ***mean*** of $Y$ given $X$. There's no reason that the credible area for such means should cover any data points at all. Again, it's not a problem.
Comment: I'd like the shaded region to be as tight as the data (and the prior) allow, but no further. My question is whether the data allow it to be as tight as it currently is. In fact, the chart you showed also strikes me as "too tight" (lots of data points outside the shaded area). I'd like to know if my intuition about this is off.
Comment: No, you're already predicting $Y$ using the conditional mean: $\hat{Y} \approx E[Y|X]$. This is the very core of the linear regression model - it's a model for the mean, and this is how it is defined in statistics and machine learning textbooks. But there's nothing wrong with this because the conditional mean is the optimal predictor for $Y$ under a squared loss. This is why it is so widely used. Here's a proof: https://stats.stackexchange.com/a/71869/190932
Comment: Yes, I understand it can be confusing. Sure, you are indeed trying to model the relationship between the dependent variable $Y$ and covariate(s) $X$. But linear regression can only *predict* $E[Y|X]$. Have a look at this stylised representation: http://www.fao.org/docrep/w5449e/w5449egf.gif  If you want a model between $Y$ and $X$ that works in a different way, then you should pick a different model.
Comment: It should theoretically be possible to do without changing the model, like in this rJAGs example: http://doingbayesiandataanalysis.blogspot.co.uk/2015/10/posterior-predicted-distribution-for.html  But I'm not sure how you'd do it in PyMC3, as I'm no expert in this particular library. Perhaps you can try asking a follow-up question about this, quoting this link, and see if anyone has any better ideas?
Comment: I see. So if I understand correctly, in order to make predictions about the value of $Y$ (instead of the mean of $Y$) given new data, I have to include the error distribution in my posterior predictive, yes? Instead of `lm = lambda x, sample: sample['sot'] * x`, I should use something like `lm = lambda x, sample: scipy.stats.norm(loc = sample['sot'], scale = sample['sigma']).rvs(1) * x`?
Comment: I don't follow. $Y = \beta \, X + \epsilon$ is a model for $Y$, not just the mean of $Y$. If $\epsilon$ is normally distributed with zero mean, then $\beta \, X$ represents an estimate for the mean of $Y$, but the model purpose (any model, not just the linear one I'm using here) is to model $Y$, not the mean of $Y$, no?
Comment: I understand that, going back to my original question, the shaded area is an estimate of $E[Y|X]$, not of $Y|X$. But can't I use the error term $\epsilon$ to get an estimate on $Y|X$? So if there is a vector `trace['sot']` with $n$ simulated values of the angular coefficient, and another vector `trace['sd']` with $n$ simulated values of the standard deviation of the error term, then I can get $Y|X$ by plotting the histogram of $n$ draws from normal distributions, each with mean `trace['sot'][i]` $\cdot X_{n+1}$ and standard deviation `trace['sd'][i]`, no?
Comment: Your answer clarified my original question and the article you linked answers my new question exactly, so thank you!
Comment: Glad I could help!