Java Bloomberg API - how to generate a Request without a Service

Question: I am using the Bloomberg API to grab data.  Currently, I have 3 processes which get data in the typical way as per the developers guide.  Something like:
Service refDataService = session.getService("//blp/refdata");
Request request = refDataService.createRequest("ReferenceDataRequest");
request.append("securities", "IBM US Equity");
request.append("fields", "PX_LAST");
cid = session.sendRequest(request, null);

That works.  Now I would like to expand the logic to be something more like an update queue.  I would like each process to send their Request to an update queue process, which would in turn be responsible for creating the session and service, and then sending the requests.  However, I don't see of any way to create the request without the Service.  Also, since the request types (referenceData, historical data, intraday ticks) are so varied and have such different properties, it is not trivial to create a container object which my update queue could read.
Any ideas on how to accomplish this?  My ultimate goal is to have a process (I'm calling update queue) which takes in a list of requests, removes any duplicates, and goes out to Bloomberg for the data in 30 second intervals.
Thank you!
Comment: If that is of any interest, I have developed a [Java wrapper](https://github.com/assylias/jbloomberg) around the Bloomberg API which could make this easier (i.e. all types of request are created via builders which all implement the same interface and all results are put in a RequestResult object). You can see a sample code at the bottom of [the javadoc page](http://assylias.github.com/jBloomberg/apidocs/index.html).
Comment: Great, I should be able to use this, thank you!  Any chance you've been working on a IntraDayTickBuilder class?
Comment: Not yet - I will probably include one in the next few days.
Comment: Great!  Thank you, I will keep looking out for it.  By the way, what is the Stack Overflow etiquette - this is definitely what I needed but I can't accept a comment as the answer
Comment: I have added an answer with more details. I have completely revamped the API to include tick data and it looks much better now. Thanks for the little push!
Answer: I have updated the jBloomberg library to include tick data. You can submit different types of query to a BloombergSession which acts as a queue. So if you want to submit different types of request you can write something like:
RequestBuilder<IntradayTickData> tickRequest =
    new IntradayTickRequestBuilder("SPX Index",
        DateTime.now().minusHours(2),
        DateTime.now());

RequestBuilder<IntradayBarData> barRequest =
    new IntradayBarRequestBuilder("SPX Index",
        DateTime.now().minusHours(2),
        DateTime.now())
        .period(5, TimeUnit.MINUTES);

RequestBuilder<ReferenceData> refRequest =
    new ReferenceRequestBuilder("SPX Index", "NAME");

Future<IntradayTickData> ticks = session.submit(tickRequest);
Future<IntradayBarData> bars = session.submit(barRequest);
Future<ReferenceData> name = session.submit(refRequest);

More examples available in the javadoc.
If you need to fetch the same information regularly, you can reuse a builder and use it in combination with a ScheduledThreadPoolExecutor for example.
Note: the library is still in beta state so don't use it blindly in an black box that trades automatically!
Comment: This is great, thank you!  By the way, I am using blpapi-3.4.8-1.jar and I have no issue using the `includeNonPlottableEvents` flag (in fact I have to in order to see the trades I'm interested in).  Your variable is called `includeNonPlottable` so it might just be a typo.  In the developer's guide it breaks it down on two lines so maybe that is why.  Thanks again, this is exactly what I needed!
Comment: @Marianna Well spotted! I had missed the second line indeed. I've just pushed an update. Thanks!
Comment: @assylias is there any place we can find more examples for the  jBloomberg
 library
Comment: Bottom of [this page](http://assylias.github.io/jBloomberg/apidocs/index.html)


How to finger-pick guitar boogie?

Question: I'm trying to play the following (part of Guitar Boogie) without a pick. What's a reasonable way to pick with the right hand? So far I've only been using the thumb, but trying to speed up, I think it's keeping me back.
I've got the feeling that this is some "standard" pattern with a "standard" way of picking? I recognize the 12-bar blues (it continues D7 A7 E7 A7E7) and I've been trying to search for advice on how to pick, but didn't manage to find anything useful.
Comment: Are you playing the bass line and the chords on the guitar or are you playing along with another musician (e.g., bass player or pianist) who is playing the bass line? The bass line is an important aspect of the boogie feel, so it makes a big difference whether you're solo or in an ensemble.
Comment: @ToddWilcox I just found these notes, tried to play it and felt that it sounded pretty nice and swingy. I have no idea how it fits into a larger context or how it's supposed to be played. I'd like to be able to play it myself with no other instruments and have it sound good. Maybe I should have read up a bit on boogies before asking!
Comment: @ToddWilcox I heard this which I believe is played on a single guitar, right? https://www.youtube.com/watch?v=8tNYyoelXTw I think the notes in my question are the start of this song, Guitar Boogie by Arthur Smith.
Answer: You can use your thumb, or alternating i-m-i-m, and it will be a good exercise, but you'll never get much speed that way, especially if you're going to swing it (if you want to play it straight, i-m-i-m will work). I'd suggest p-i-p-i (thumb-index) if you want speed. You could also just play quarter notes with the thumb, in which case you could add a melody above it with the fingers.
If you watch this, you'll see Arthur Smith is using a flat pick:
Comment: A pick would help with speed, though with practice, i-m could work too. Use i-m OR m-i on the same note (try them both). Long note with thumb.
Comment: Thank you! So you'd suggest a pick for speed? Also, with i-m-i-m, would i-m go on the same note or m-i?
Comment: Also with p-i-p-i, I didn't understand: I'm trying to swing it (play the first note longer and second shorter), do you suggest playing the long note with the thumb and the short one with the index finger, or the other way around?
Answer: The music you have shown would likely be played as a bass line in a fingerpicking style. For this, I would probably just use the thumb with all down strokes. This line would likely be combined with one or more other parts to be played by the other fingers, but the bass line is important to the feel of the boogie.
Here is an example 12 bar blues in tablature using the boogie bass line from the question. It is important to maintain some independence between the bass line and the upper voices. The fingerings here are not complicated, but it may take some practice to be able to execute them smoothly.
<code>$E 5 $E.5.$G.6.$B.8 $A 4 $A.4.$G.6.$B.8 $A 7 $A.7.$G.6.$B.8 $D 4 $D.4.$G.6.$B.8 | $D 7 $D.7.$G.6.$B.8 $D 4 $D.4.$G.6.$B.8 $A 7 $A.7.$G.6.$B.8 $A 4 $A.4.$G.6.$B.8 |
</code>
<code>$E 5 $E.5.$G.6.$B.8 $A 4 $A.4.$G.6.$B.8 $A 7 $A.7.$G.6.$B.8 $D 4 $D.4.$G.6.$B.8 | $D 5 $D.5.$G.6.$B.8 $D 4 $D.4.$G.6.$B.8 $A 7 $A.7.$G.6.$B.8 $A 4 $A.4.$G.6.$B.8 |
</code>
<code>$A 5 $A.5.$B.7.$e.8 $D 4 $D.4.$B.7.$e.8 $D 7 $D.7.$B.7.$e.8 $G 4 $G.4.$B.7.$e.8 | $G 5 $G.5.$B.7.$e.8 $G 4 $G.4.$B.7.$e.8 $D 7 $D.7.$B.7.$e.8 $D 4 $D.4.$B.7.$e.8 |
</code>
<code>$E 5 $E.5.$G.6.$B.8 $A 4 $A.4.$G.6.$B.8 $A 7 $A.7.$G.6.$B.8 $D 4 $D.4.$G.6.$B.8 | $D 7 $D.7.$G.6.$B.8 $D 4 $D.4.$G.6.$B.8 $A 7 $A.4.$G.6.$B.8 $A 5 $A.6.$G.6.$B.8 |
</code>
<code>$A 7 $A.7.$G.7.$B.9 $D 6 $D.6.$G.7.$B.9 $D 9 $D.9.$G.7.$B.9 $D 6 $D.6.$G.7.$B.9 | $A 5 $A.5.$G.5.$B.7 $D 4 $D.4.$G.5.$B.7 $D 7 $D.7.$G.5.$B.7 $D 4 $D.4.$G.5.$B.7 |
</code>
<code>$E 5 $E.5.$G.6.$B.8 $A 4 $A.4.$G.6.$B.8 $A 5 $A.5.$G.6.$B.8 $A 6 $A.6.$G.6.$B.8 | $A 7 $A.7.$G.7.$B.9 $A 9 $A.9.$G.7.$B.9 $D 5 $D.5.$G.7.$B.9 $D 6 $D.6.$G.7.$B.9 | $E.5.$G.6.$B.7 ||
</code>
It is also worth noting that when tempos speed up (or even at slower tempos), it is not uncommon to omit some of the bass notes when the upper voices help to establish the rhythmic feel:
<code>$E 5 $G.6.$B.8 $A 4 $G.6.$B.8 $A 7 $G.6.$B.8 $D 4 $G.6.$B.8 | $D 7 $G.6.$B.8 $D 4 $G.6.$B.8 $A 7 $G.6.$B.8 $A 4 $G.6.$B.8 |
</code>
Concerning these Chord Voicings
The OP has asked about why I played these chords starting at the fifth fret, noting that this could also be played with open strings. This is true, at least in part. It is good to play things in different positions and with different fingerings, and there is no reason that the above must be played exactly the way I wrote it.
To start, note that the chords used above are shell voicings, which are chords composed of the root, third, and seventh of a chord. These notes contain the essential skeleton of a seventh chord, and are very handy to have in your hip pocket. There is plenty of interesting stuff to say about shell voicings, but that would take us too far afield, I fear. Here are some shell voicing chord diagrams (apologies for the chord diagram rendering-- I couldn't get jTab to render more than two diagrams side-by-side):
<code>%5/1.X/X.X/X.6/2.8/4.X/X[A7]

%X/X.5/1.X/X.X/X.7/3.8/4[D7]
</code>
<code>%X/X.7/1.X/X.7/2.9/4.X/X[E7]
</code>
Note that the D7 here is voiced with the F♯ and C (the 3rd and the 7th) on the top two strings. If this chord is moved to use an open D, those top notes won't be reachable, so either you would have to move up to the fifth fret after playing the A7, or you could voice the chord differently.
If you are paying attention, you will notice that there are two voicings for a dominant 7th chord with the root on the fifth string shown above. Here are three such possibilities for C7:
<code>%X/X.3/1.X/X.X/X.5/3.6/4[C7]

%X/X.3/1.X/X.3/2.5/4.X/X[C7]
</code>
<code>%X/X.3/2.2/1.3/3.X/X.X/X[C7]
</code>
And here are four possibilities for a G7 rooted on the sixth string (the first one requires a stretch, and may be easier higher on the fingerboard):
<code>%3/1.X/X.X/X.X/X.6/3.7/4[G7]

%3/1.X/X.X/X.4/2.6/4.X/X[G7]
</code>
<code>%3/1.X/X.3/2.4/3.X/X.X/X[G7]

%3/2.2/1.3/3.X/X.X/X.X/X[G7]
</code>
So, I voiced the D7 starting in the fifth bar the way I did because the bass line ends up on the G string, and I didn't want it to interfere with the top notes of the chord. I couldn't do this with an open D in the bass; since I needed to put the D7 at the fifth fret, I also put the A7 at the fifth fret to reduce movement around the fretboard.
You may want to try different ways to play this, and note that there isn't really a "right" way. You might not like the way it sounds, and prefer the sound of some open strings, or might prefer the sounds of some different voicings. You might like to change some of the chords to 6th chords (the final chord is an A6). You might find this too difficult to finger in the written positions. Any of these would be a good reason to find new ways to play this.
Experiment. Hopefully the above chord diagrams will help you to see other ways to play with the original bass line.
Comment: Thanks a lot! I had some suspicion that this was just a "base", thanks for showing me context with chords over the bass line!
Comment: I second the idea of using the thumb and all downstrokes. That's what I would do. That's what delta blues players did.
Comment: @Anna -- there was a mistake in the first tabulature I provided; I had shifted some fingerings to the wrong strings towards the end. I have corrected and reformatted the tabulature. Hopefully there are no more mistakes ;)
Comment: Thank you again! I played the same notes at frets 0,2,4, what's the reason you're playing on 5,7,4? To have space for the voicings I suppose, anything else?
Comment: Just realised you could move the "chords"/voicings a string as well, so why not play closer to the nut?
Comment: @Anna -- that is a good observation. You can play wherever you like, and I would encourage you to experiment; one of the most important things to develop is flexibility (I don't just mean physical flexibility, but the ability to play things in different positions and with different fingerings). I have added some comments to my answer about the voicings I used.

How to finger-pick guitar boogie?


Why do CNNs work on regression problems such as finding bounding boxes in images?

Question: My understanding is that each layer in a CNN computers specific features, depending on the type of filter that has been trained, like edges/nodes/patterns etc. I can understand how this can be used in classification problems, but how is this information used when predicting bounding boxes around images.
I've come across multiple tutorials online, where the process of adding bounding box locations to the output layers, and loss function are discussed, but none explained why this approach works. Are there any resources that could help explain this?
Comment: Do you understand how bounding boxes work for other models, such as MLP or RNN?
Comment: No I don't I wanted to start with CNN's before moving onto other models, would it help?
Answer: Let me answer with an opposite question: Why shouldn't they work?
Look at it from the math perspective: What does a convolution operation do? It looks at a bunch of local activations from its preceding layer and computes their weighted sum. The same operation is applied in a sliding window fashion over multiple spatial locations. The "usual" fully connected layers for regression do actually the same, except they don't use the same weights everywhere and they do not look only at activations in a small local neighborhood, but as far as arithmetic goes, they compute the same stuff.
When predicting bounding boxes, one usually regresses parameters of "anchor boxes", predefined bounding boxes at various locations. These parameters are often offset and scaling, controlling how should an anchor box be perturbed to match some object.
Convolutions are actually great in this case: it does not matter where in the image an object is located, the offset and scaling of an anchor box in that location should be the same.


using chained automount to mount home directory

Question: I have set up two directories mounted with automount (via LDAP):
in the /home directory automount is configured to mount
*   -fstype=bind :/network/srv/home/&

in the /network directory one entry
srv    -fstype=nfs4,rw,sec=sys,noatime,hard,intr,proto=tcp,timeo=100,port=2049 myserver:/

when i try to access the home directory of a user (e.g. ls /home/myuser), automount fails with the error message:
>> mount: special device /network/srv/home/myuser does not exist
failed to mount /home/myuser

when i do the 'bind mount' manually using
mount -o bind /network/srv/home/myuser /home/myuser

automount for /network/srv is triggered and the myuser home directory is successfully mounted to /home/myuser.
Any ideas how to tell automount to load the /network/srv before trying to mount /network/srv/home/myuser???
Answer: I've encountered the same problem on Ubuntu 16.04 (autofs 5.1.1), but this doesn't happen on CentOS 7 (autofs 5.0.7, but with many patches).  I worked around this by mounting directly rather than bind-mounting, with entries of this form in /etc/auto.home:
* -fstype=nfs4,rw,sec=sys,noatime,hard,intr,proto=tcp,timeo=100,port=2049 myserver:/home/&

I'd also much rather use a bind mount into a /net directory on both Ubuntu and CentOS, but this workaround works OK on both.
(I've not yet run through the large number of patches to see if there is something which was added to the CentOS version to fix the bind mounting, or if it's something which broke in a version of autofs after 5.0.7)


Rotating an N-dimensional vector in a plane

Question: Suppose that I have two vectors $\mathbf{z} \in \mathbb{R}^{N}$ and $\mathbf{v} \in \mathbb{R}^{N}$. I want to find a $\mathbf{\tilde{z}}$ that is closer in angle by a preselected $\Delta \theta$ to $\mathbf{v}$ in the plane defined by the column space of $A = [\mathbf{z}, \mathbf{v}]$.
In other words, I want to find a new $\mathbf{{z}}$ that reduces the value:
$$ \theta = cos^{-1}\Big( \frac{\mathbf{z}^T \mathbf{v}}{ ||\mathbf{z} || \cdot || \mathbf{v} || } \Big) $$
Obviously, I can simply choose my new $\mathbf{z}$ as $\mathbf{\tilde{z}} = \mathbf{z} + d\cdot(\mathbf{v} - \mathbf{z}) $ for some $d \in \mathbb{R}_{++}$. However, I don't know how I can choose $d$ analytically such that I can reduce $\theta$ by a given amount $\Delta \theta$ that I have pre-selected -- ie other than by a line search.
Another approach I thought of is to define find a vector $\mathbf{w} \in N(A)$ via QR decomposition of $A^{T}$. Then, I will just need to rotate $\mathbf{z}$ about $\mathbf{w}$ by $\Delta \theta$ to get my result. However, I don't know how to actually do this.
Can anyone provide some insight on my two approaches, or another way to do this all-together?
Answer: Let me write the dot products of the vectors instead of your notation, instead of transposes, etc, so then
$$
 \mathbf{z}    \cdot \mathbf{v} = | \mathbf{z} |   \cdot |\mathbf{v} |\cos \theta.
$$
If I understand you right, you are seeking   $\mathbf{\tilde{z}}$  in the plane of the original vectors with $$
 \mathbf{z}    \cdot \mathbf{\tilde{z}} = | \mathbf{z} |   \cdot |\mathbf{\tilde{z}} |\cos \Delta \theta.$$
Note
$$
 \mathbf{u}\equiv    \mathbf{v}~\frac{| \mathbf{z}|}{| \mathbf{v}|\cos\theta} -  \mathbf{z}
$$
dictates
$$ \mathbf{u}\cdot \mathbf{z} =0 .
$$
You may then take
$$
\mathbf{\tilde{z}}\equiv \mathbf{z}+   \mathbf{u } \frac{\tan\Delta\theta}{\tan\theta} .
$$
It follows that
$$
\mathbf{\tilde{z}}\cdot\mathbf{z}= |\mathbf{z}|^2,
$$
and
$$
|\mathbf{\tilde{z}}|^2=\frac{|\mathbf{z}|^2} { \cos^2\Delta\theta },
$$
to yield your
$$
 \mathbf{z}    \cdot \mathbf{\tilde{z}} = | \mathbf{z} |   \cdot |\mathbf{\tilde{z}} |\cos \Delta \theta.$$
Comment: Hmm, but how do I know that $\tilde{z} \cdot v =  |\tilde{z}| |v| cos( \theta - \Delta \theta)$ and not $\tilde{z} \cdot  v =  |\tilde{z}| |v| cos( \theta + \Delta \theta)$?
Comment: ? It's easiest if you diagram it, at least for both θ and Δθ  smaller than  π/2. In that case, both for   θ > Δθ   and for  θ < Δθ, the evenness of the cosine enforces the  cos(θ - Δθ) solution. For very large angles, you have to triage and adapt. It would actually be helpful for you to diagram the ***v*** and ***z*** on their plane, and "see" the solution.


Debug iPython Parallel engines in cluster

Question: Related to this thread...
I am trying to track down a bug in which the results from processing on an iPython cluster do not match what happens when the same process is run locally.  Even when the iPython cluster is entirely local, and the CPU is simply running multiple engines.
I cannot seem to figure out how to log data as it is being processed on the engines.  Print statements don't work and even when I try to have each engine write to a separate file, the file is created but nothing is written to it.
There must be a way to debug code running on the iPython parallel engines.
Answer: Not sure why, but I narrowed the problem and workaround down to the fact that I am using cython and compiling the .pyx files before running the program.
For some reason the cython cdef init of my float variables was not being done properly on the engines however it was being done correctly when I ran outside of the Client() queue.
Changing these variables to be normal python variables solved the problem.  Though it does not seem like this should have to happen.  Anyone that can shed more light on this?


inequality of norm for compact operators on Banach space

Question: I have to prove the following statement:

Let $(X,||\cdot||_X),(Y,||\cdot||_Y),(Z,||\cdot||_Z) $ Banach spaces, let $T \in L(X,Y)$ be compact and let $J \in L(Y,Z)$ be injective. Prove that for every $\epsilon \in (0,\infty)$, there exists $C\in [0,\infty)$ such that
$$||Tx||_Y\leq \epsilon ||x||_X + C||JTx||_Z \quad \forall x\in X$$

My proof:

Assume the opposite in order to derive a contradiction: I.e.$\exists \epsilon >0, x\in X $ such that $\forall C>0 \quad ||Tx||_Y> \epsilon||x||_X + C||JTx||_Z$
It is easy to see $x$ can not be an element of the kernel of $T$. Hence assume $x\notin \ker(T)$ Since the above inequality holds for abitrarily large $C$ it holds $||JTx||_Z = 0$ otherwise pick $C> \frac{| ||Tx||_Y-\epsilon||x||_X|}{||JTx||_Z}$
But since $J$ is injective $\ker J = \{0\}$ hence $Tx = 0$ which is a contradiction to $x\notin \ker T$

My Question: Is this proof correct? I am asking because I did not require the compactness of $T$.
So where does it fail?
Answer: You negated the statement incorrectly. The correct negation is "there exists an $\varepsilon > 0$ such that for all $C > 0$ there exists an $x > 0$..." which means that as you vary $C$ in your argument, you must vary $x$ also.
In particular, as $C$ gets large, it may be that $\|JTx_C\|$ gets small.
Comment: yeah well it's always those small things.
Thanks a lot. This clears it up
Answer:
 Assume $\|x_n\|=1$ and $$\|T\|\ge \|Tx_n\|\ge \varepsilon +n\|JTx_n\|$$ By compactness the sequence $Tx_n$ contains a convergent subsequence $Tx_{n_k}.$ Let $y=\lim_k Tx_{n_k}.$ Then $JTx_{n_k}\to Jy.$ If $Jy\neq 0,$ then $n_k\|JTx_{n_k}\|$ tends to $\infty.$ Therefore $Jy=0,$ i.e. $y=0.$ On the other hand $\|Tx_{n_k}\|\ge \varepsilon,$ which implies $\|y\|\ge\varepsilon,$ a contradiction.


CentOS 7 guest fails to boot on Xenserver 7.0

Question: I have a CentOS 7 guest installed on a Xenserver 7.0 Host with all the up to date patches.  I have tried 2 installation sources, the minimal.iso and the DVD, and installed the 64bit version.  I have no installation issues but upon reboot I get the following screen.

At this point the VM just sits there.  I have allotted 1 CPU with 2 cores to this VM.  The first core is pegged at 100% utilization. I have allocated 4GB RAM, so I don't think its a memory issue.  I have looked around on the net for similar issues and have found none.  There is a possibility this is related to the bochs video driver.  I had to disable it to get the graphic install working but I am unable to access the boot loader to modify the kernel options as I am with the installer.  Any assistance would be greatly appreciated.  Cheers!
Answer: I solved the issue by using a "Misc" vm template in xenserver and not using the CentOS 7 template.


Trying to understand morphisms between varieties

Question: Let $k_1, k_2, k_0 \in \mathbb{N}$.
If $k_1 = k_2 = k_0$ then the map
which sends $[x_0:x_1:x_2] \in \mathbb{P}^2$ to $[x_0^{k_0}:x_1^{k_1}:x_2^{k_2}] \in \mathbb{P}^2$ is well defined, but
it's not well defined if all of the $k_i$'s are not the same.
(My understnading of morphisms between projective varieties:
$\varphi: V \to W$ is a map between projective varieties $V \subset \mathbb{P}^n$ and  $W \subset \mathbb{P}^m$ given by $\varphi([x_0 : \ldots : x_n]) = [\varphi_0([x_0 : \ldots : x_n]): \ldots : \varphi_m([x_0 : \ldots : x_n])]$, where the $\varphi_i$ are homogeneous polynomials of the same degree that don't vanish simultaneously at any point of $V$.)
I was just wondering when not all of the $k_i$'s are equal, does the map 'make sense' if I change the domain to affine space?
i.e. if I define a map from $\mathbb{A}^3 \backslash \{ \mathbf{0} \}$ to $\mathbb{P}^2$ by $(x_0, x_1, x_2) \rightarrow [x_0^{k_0}:x_1^{k_1}:x_2^{k_2} ]$, is this just a weird map that sends of $\mathbb{A}^3 \backslash \{ \mathbf{0} \}$ to $\mathbb{P}^2$? or does this become a morphism in an appropriate category (something that generalizes affine varieties and projective varities maybe?) Any comments would be appreciated. Thank you.
ps for simplicity I'm only thinking the affine space and the projective space over $\mathbb{C}$
Comment: The map as you define from affine space outside zero is indeed a morphism. The map you define can also be interpreted as a morphism of weighted projective spaces.
Answer: You can consider the affine map $(x,y,z)\mapsto (x^a,y^b,z^c)$ from $\mathbb{A}^3-\{0\}$ to itself. (Observe that this map does not contain $0$ in its image). Hence you can compose it with the quotient map $(x,y,z)\mapsto [x:y:z]$ (which is well-defined on non-zero points) to obtain the map you give.
Comment: It is a morphism of varieties :)
Comment: No it is not. Note that finite morphisms necessarily have finite fibers, where as for example when $k_0=k_1=k_2=1$ all fibers are infinite.
Comment: The key concept here, as Mohan mentions above, is weighted projective space.
Comment: Is there a terminology for this composed map? (as in it's not a morphism between affine varieties or morphism between projective varieties. So do we still say this is a morphism between something?)
Comment: Thank you very much. I was wondering if I could ask you just one more question: is this (this composed map)  a finite morphism?
Comment: Thank you very much for your help today! Greatly appreciated! :)
Comment: You are welcome :)


Exact terminology of types in Scheme

Question: I am looking for a standard doc about types of Scheme. For instance, As types don't need to be explicitly specified in a program, I do not know it is bool or boolean; LIST or LISTE; int or Nat; List[] or List[alpha]...
Another small question is how "atomic expression" is defined. For instance, is '(1 2 3) an atomic expression?
Answer: As you point out, types don't need to be explicitly specified. The notion of types is therefore largely missing from the language specification.
There are some languages--Typed Racket comes to mind--that build a typed language on top of (what used to be) Scheme, and these languages define their own types.
There are also contract systems that give names to classes of values. Again, I can point to Racket as an example of this, but I'm confident that there are similar systems out there.
Without being more specific, then, there's no general answer to your question.
The term "atomic" is generally used to refer to values that are not "compound data"; that is, values that don't contain other values. A list is definitely not atomic, and a symbol is.
Answer: Here is the standards manual for MIT scheme.  Depending on which scheme you're using, this might or might not be what you're looking for.

Exact terminology of types in Scheme


creating images slide show on mysql database

Question: i have a table images in my database which can store say like four images for one product. i want when a user search for a particular product an image slide show is generated for that product using the respective image id of that product.thanks.
<code>$sql5="create table if not exists product(
prdid int(10) not null auto_increment primary key,
type enum('house','car') not null,
p_date timestamp not null DEFAULT CURRENT_TIMESTAMP,
amount varchar(15) not null,
description varchar(255) not null,
status enum('1','0') not null default '0',
prdfk int(10) not null,
foreign key(prdfk) references users(usid) on delete cascade
)DEFAULT CHARACTER SET utf8 ENGINE=InnoDB";

$sql6="create table if not exists images(
imgid int(10) not null auto_increment primary key,
name varchar(50) not null,
mimetype varchar(55) not null,
fielddata mediumblob not null,
imgfk int(10) not null,
foreign key(imgfk) references product(prdid) on delete cascade
)DEFAULT CHARACTER SET utf8 ENGINE=InnoDB";
</code>
Comment: Can you please be more specific of what is not working for you?
Comment: @AndriusNaruševičius, He wants his products to be searched and place images of those products in a one image slider.
Comment: ..and of course, without showing his slider code --by thinking we would code for him.
Comment: @mwas: do you only need a sql to get the images?
Comment: @jens yeh...i wont mind even if its sql only.thanks
Answer: This should work:
<code>select fielddate, mimetype, name from image join prduct on imgfk = prdid where prdi=<your productid>
</code>


iPhone time comparison

Question: I am trying to figure out how to create an 'if' statement that uses a time value as a condition.  For example:
if (time <= 10:00) {
    score = 3;
} else if (time <= 20:00) {
    score = 5;
} else {
            score = 9;
}

I know that a string of "5:23" cannot be compared this way but I don't think I can just turn a string value like that directly into an integer.  Any thoughts?
Comment: This makes me wonder if 00:00 < 24:00.  You probably would like to check `NSDateFormatter` & `-[NSDate dateFromString:]` out?
Answer: Try this:
NSString *realTimeString = @"10:00";
NSString *someTimeString = [realTimeString stringByReplacingOccurrencesOfString:@":"
                                           withString:@"."];
float time = [someTimeString floatValue];

if (time <= 10.00) {
    score = 3;
} else if (time <= 20.00) {
    score = 5;
} else {
    score = 9;
}

iPhone time comparison


Glassfish Daemon with Apache and Mod_proxy

Question: I am trying to setup an ubuntu web server which utilizes both Apache's httpd web server for some content and glassfish for Java-based applications. I figured I would use mod_proxy as I've had luck playing with that a little in the past. I got it so the basic setup works. However, in trying to get glassfish running as a daemon, I found that the apache/glassfish connection breaks down anytime I stop/restart glassfish without restarting apache.
The basic directions I followed for setting up glassfish as a daemon can be found here: http://220.127.116.11/foo/entry/how_to_run_glassfish_v3.
A) Is there a way to configure things so I can restart glassfish without needing to restart apache?
B) If not, is there a simple way to setup my glassfish daemon to restart apache at the same time without relying on glassfish and apache to both be on the system? In other words, it would be nice to have a script which would work cleanly on a server with glassfish and apache as well as a server with just glassfish.
Answer: How long have you waited after the Apache connection breaks?
It will try again on the failed backend (the Glassfish server) after 60 seconds by default; you can speed that up by modifying your configuration:
<code># Retry this backend immediately instead of waiting
ProxyPass /location http://127.0.0.1:8080/location retry=0
</code>


How to create event listeners using a for loop in JS

Question: I am trying to create event listeners for a pure JS dropdown menu. When I try to create the listeners using a for loop I get an error and it doesn't work, but when I create them manually using arrays they work perfectly. What am I missing?
var dropDown  = document.getElementsByClassName('nav-sub__mobile-dropdown');
var subNavList  = document.getElementsByClassName('nav-sub__list');

for ( i = 0; i < dropDown.length; i++) {

    dropDown[i].addEventListener('click', function() {
        subNavList[i].classList.toggle('nav-sub__list--active');
    });
}

The above doesn't work, but if I create the event listeners manually using the arrays it does work.
dropDown[0].addEventListener('click', function() {
    subNavList[0].classList.toggle('nav-sub__list--active');
});

dropDown[1].addEventListener('click', function() {
    subNavList[1].classList.toggle('nav-sub__list--active');
});

dropDown[2].addEventListener('click', function() {
    subNavList[2].classList.toggle('nav-sub__list--active');
});

When I use the for loop I get the following error code in my console.
Uncaught TypeError: Cannot read property 'classList' of undefined

UPDATE SOLVED PROBLEM
I was able to solve the problem using let thanks to Ben McCormick's comment here:
JavaScript closure inside loops – simple practical example
The solution was to simply use let in the for loop.
for (let i = 0; i < dropDown.length; i++) {

Comment: Put your eventlistener code in function(i) {} block
for ( i = 0; i < dropDown.length; i++) {
function (i) {
    dropDown[i].addEventListener('click', function() {
        subNavList[i].classList.toggle('nav-sub__list--active');
    });
}
}
Answer: Because when the click function is called, the variable i has the last value it held, dropDown.length, not the loop value at the time addEventListener was called.
What you want is something like this:
for ( i = 0; i < dropDown.length; i++) {
    function addListener(n) {
        dropDown[n].addEventListener('click', function() {
            subNavList[n].classList.toggle('nav-sub__list--active');
        });
    }
    addListener(i);
}

Comment: Yes, that is exactly the problem when I console.log it. Is there a way to achieve what I am trying to do?
Comment: Just added more detail.


Custom attributes for a menu item

Question: I have to create a menu that includes items in a variety of languages including Arabic and Farsi.
Something like this:
<code>-LTR item
-LTR item
 RTL item -
-LTR item
 RTL item -
</code>
Is there a way to add the dir='rtl' to specific menu items?
And more importantly, is there a way for me to make this accessible to non-technical users that will be maintaining the site after I have created it?
The site is being built in Drupal 8
Ideas?
Answer: Maybe starting from https://www.drupal.org/project/link_attributes and contributing a dir attribute patch (this way you have a UI for non tech users that will maintain the site)
Answer: You can use hook_preprocess_HOOK
<code>/**
 * Implements hook_preprocess_HOOK().
 */
function hook_preprocess_menu(&$variables) {
  $variables['attributes']['class'][] = 'clearfix';
  if($variables['menu_name'] =='main'){
    foreach($variables['items'] as $item){
      if($item['title'] =='Home'){
        $item['attributes']['dir']='rtl';
      }
    }
  }
}
</code>

Custom attributes for a menu item


Factor loading and corrected item to factor correlation

Question: By definitions seems that both factor loading and corrected item to factor correlation (statistic when calculating Cronbach's alpha) essentially indicate to the same - the correlation of the item with the respective factor, but I get different scores in SPSS on both indicators, therefore presume there are more mathematically sound and grounded explanations of how these differ. Could please someone reason how they differ, as from definitions I got confused that they are kind of the same...
PS: used orthogonal rotation in factor analysis.
Comment: This is too vague. Please explain in details what you did in factor analysis and what you did in reliability analysis, what output you then try to link between the two.
Comment: I did factor analysis with varimax rotation and I report their loadings, as well as did reliability analysis and report corrected item to factor correlation (Cronbach's alpha for each of identified factors). From definitions of what the loading is and what the corrected item to factor correlation is it seems that it is kind of the same, but in that case I do not get the same scores. How could I better explain my question? What other info should I provide?
Comment: E.g. factor loadings on the factor structure matrix indicate the correlation between the particular item and the factor that the item belongs.
Comment: Item-to total factor correlation as I understand also explains the correlation of particular item with the factor. Should then these 2 indicators be of the same value?
Answer: When you write "item-to-total factor correlation" I believe you should remove the word "factor."  Then you will have the term usually used as part of Cronbach's alpha reliability analysis to denote the correlation between one item and the sum of all other items under consideration.  No latent factor is created or computed as part of this relatively simple procedure.  In contrast, a factor loading comes out of more complicated computations from factor analysis that do create a latent factor presumed to be underlying the scores on the various individual items. So the item-total correlations and the factor loadings will differ.


onclick keeps calling javascript function

Question: HTML:

Pseudo Random Quote Machine

      <button id = "getMessage" class = "btn btn-primary">
        Get Random Quote
      </button>
      <button id = "tweet" class = btn btn-primary>Tweet this!
      </button>

Javascript:
function randomQuotes()
{
  //array of quotes
  var quotes = [ "\"Operator! Give me the number for 911!\" - Homer J. Simpson", "\"I\'m normally not a praying man, but if you\'re up there, please save me Superman\" - Homer J. Simpson", "\"I\'m in no condition to drive...wait! I shouldn\'t listen to myself, I\'m drunk!\" - Homer J. Simpson", "\"A Degenerate, Am I? Well You Are A Festizo. See, I Can Make Up Words Too, Sister\" - Peter Griffen", "\"Victory Shall Be Mine!\" - Stewie Griffen", "\"He's a spy, blow him up. I'm gonna go take a shit.\" - Rick Sanchez", "\"Ohh yea, you gotta get schwifty. \" - Rick Sanchez", "\"Yo! What up my glip glops!\" - Rick Sanchez", "\"WUBBA LUBBA DUB DUBS!!! \" - Rick Sanchez", "\"Existence is pain to a meeseeks Jerry, and we will do anything to alleviate that pain.\" - Mr. Meeseeks", " \"It\'s morphine time\" - TheRussianBadger"];

  var randNum = Math.floor((Math.random() * quotes.length));
  document.getElementById("quoting").innerHTML = quotes[randNum];
}

function tweetThis()
{
  var tweetToShare = document.getElementById("quoting").innerHTML;
  var tweetUrl = 'https://twitter.com/share?text=' +   encodeURIComponent(tweetToShare) + ".";
  window.open(tweetUrl);
}

document.getElementById("getMessage").onclick = function(){randomQuotes();}
document.getElementById("tweet").onclick = function(){tweetThis();}

Basically, any time I click either of the two buttons the function keeps being called over and over again. Here is the code running in Code Pen
I guess my questions would be:

why would the functions be called more than one time ?
What should I do to either stop the functions from being called multiple times or prevent it from happening in the first place?
Comment: Working fine on Chrome 58.
Comment: Yep, also fine on firefox 53.
Comment: @cancer, you are absolutely right. Thank you.
Comment: @yuriy636 thanks for checking.
Comment: @Andreas thank you for also checking.
Answer: functions seem to be called only one time on chrome. Maybe your browser cached an old script: maiusc+f5 to clean
Comment: A common way this happens is if you have developer tools open on chrome and your still working on your Js file. Having developer tools open on chrome enables caching, you can fix this by going developertools -> network -> disable cache


How to compare between two different propensity matching done on the same outcome and exposure, but with different baseline covariates?

Question: Suppose I use the same data (outcome and exposure) and I have a list of baseline covariates that I want to adjust for. But I am unsure of which covariates yield the best result from propensity matching.
Let us I decided to opt for two options.

Option A: Use baseline covariates from my prior subject matter knowledge (n = 10 covariates)
Option B: Use all the other baseline covariates (n = 100 covariates)

I used 1:1 greedy matching each time and therefore I have access to covariate balance before and after matching. Both results looks reasonable to me as per the Love plots (Covariate balance plots). However, I want to make an informed decision as to choose which model for my downstream analysis. Option A or Option B? I am looking if there is a quantified approach towards determining which option did the best job.
Answer: In general terms, propensity score matching (PSM) should include all available covariates (excluding those creating logical fallacies) associated with the outcome (see for instance: https://rmdopen.bmj.com/content/5/1/e000953):

However, in your present scenario, you could could compute the mean of all standardized mean differences (SMD) for all covariates based on PSM using list of variables A (PSMA) and then mean SMD from PSM of list of variables B (PSMB), and then check (and pick) which one is smaller.


Why do we not see strong-man athletes competing in olympic weightlifting?

Question: Strong-man is of course filled with quite unique events, but many of its athletes hold world-records in traditional weight-lifting events such as deadlift.
Many of the sport's biggest names are in fact competing at an event (Giants Live) which clashes the Games which suggests there is very little overlap.
Is this due to eligibility (professional/amateur) or some other reason(s)? Have there been many notable athletes who are known to have competed in both?
Comment: I wonder how much of it has to do with doping?
Comment: As a note, [strongman] tag didn't even exist, unless I missed an equivalent tag?
Comment: @Joe an interesting point.
Comment: https://www.espn.com/espn/story/_/id/26522096/got-strongman-champ-admits-steroid-use
Answer: As far as I can tell, weightlifting (as seen in the Olympics) and powerlifting (as seen in strongman competitions) differ in terms of the required techniques and physique.
According to Wikipedia:

In comparison with other strength sports, which test limit strength (with or without lifting aids), weightlifting tests aspects of human ballistic limits (explosive strength); the lifts are therefore executed faster — and with more mobility and a greater range of motion during their execution — than other strength movements.

This suggests that a strongman would lack the speed and mobility to compete in Olympic weightlifting, whereas an Olympic weightlifter would lack the sheer physical strength to compete in strongman contests.
I'm sure there must have been athletes that have attempted to cross over from one discipline to the other, but I haven't been able to find any.
Comment: Here is a link to the [revision of the Wikipedia article](https://en.wikipedia.org/w/index.php?title=Olympic_weightlifting&oldid=1035485847) at the time of posting this answer.  (The quoted paragraph is still present [in the current revision](https://en.wikipedia.org/w/index.php?title=Olympic_weightlifting&oldid=1172464606) - although it is slightly reformulated._)


Getting "ValueError: X has 6 features, but LinearRegression is expecting 7 features as input." probably due to ColumnTransformation (Pipeline) step

Question: titanic = pd.read_csv("train.csv")
titanic_test = pd.read_csv("test.csv")
titanic_train_labels = titanic['Survived'].copy()
titanic = titanic.drop(columns = 'Survived')

# Pipeline

titanic_num = ['Age', 'Fare']
titanic_cat = ['Sex', 'Embarked']

num_pipeline = Pipeline([
        ("imputer", SimpleImputer(strategy='median')),
        ("std_scaler", StandardScaler()),
    ])

cat_pipeline = Pipeline([
        ("enc", OneHotEncoder(drop='if_binary'))
    ])

def full_pipeline(num_attribs, cat_attribs):
    return ColumnTransformer([
        ("num", num_pipeline, num_attribs),
        ("cat", cat_pipeline, cat_attribs)
    ])

titanic_prepared = full_pipeline(titanic_num, titanic_cat)
titanic_clean = titanic_prepared.fit_transform(titanic)

# Here, I'm preparing the test data via the same pipeline

titanic_test_num = titanic_num
titanic_test_cat = titanic_cat
titanic_test_prepared = full_pipeline(titanic_test_num, titanic_test_cat)
titanic_test_clean = titanic_test_prepared.fit_transform(titanic_test)
final_model.fit(titanic_clean, titanic_train_labels)

The code giving the error on the title:
final_model.predict(titanic_test_clean)

Printing useful info that may give hints about the problem:
titanic_clean[0] -> array([-0.56573646, -0.50244517,  1.        ,  0.        ,  0.        ,
        1.        ,  0.        ]) # 7 items
titanic_test_clean[0] -> array([ 0.38623105, -0.49741333,  1.        ,  0.        ,  1.        ,
        0.        ]) # 6 items

From the info above, the problem I assume is with the non matching number of onecodeencoder. What I suspected was that the number of categorical values were not the same for the train and test set. But they actually are.
the link to the dataset -> https://github.com/minsuk-heo/kaggle-titanic/blob/master/input/test.csv
Answer: The error you're seeing is indeed caused by OneHotEncoder.
However, I want to point out a more crucial point: It is not a good practice to put your pipeline into a function.
Usually we assign the pipeline to a variable and then call fit and fit_transform on it:
# Define the pipelines for numerical and categorical attributes
num_pipeline = Pipeline([
    ("imputer", SimpleImputer(strategy='median')),
    ("std_scaler", StandardScaler()),
])

cat_pipeline = Pipeline([
    ("enc", OneHotEncoder(drop='if_binary'))
])

# Combine pipelines in a ColumnTransformer
full_pipeline = ColumnTransformer([
    ("num", num_pipeline, titanic_num),
    ("cat", cat_pipeline, titanic_cat)
])

# Fit and transform the training data
titanic_clean = full_pipeline.fit_transform(titanic)

# Transform the test data using the same transformations
titanic_test_clean = full_pipeline.transform(titanic_test)

# Model fitting and prediction
final_model.fit(titanic_clean, titanic_train_labels)
predictions = final_model.predict(titanic_test_clean)

This approach ensures that the same transformations are applied to both datasets, thereby maintaining a consistent feature set. The OneHotEncoder inside the ColumnTransformer will learn the categories from the training data and apply the same encoding to the test data, resolving the feature mismatch issue.


css3 3d transformation card flip, padding/margin/border will cause the rotation origin to mess up, how to get around this?

Question: http://jsfiddle.net/nicktheandroid/yWKMD/
Look at the example, when you click the element, it rotates around from the front to the back, the problem is that it's not rotating around it's center, it's like it's off balance, hold your mouse at the left border of the front side, click the element, and see how the back side's position is now off. They should be positioned in exactly the same spot.
I noticed that when I removed the padding/margin/border, it would rotate fine, but I need to have the padding and border on there. Is there a way to do this with padding and a border - so that it will rotate around the center, not off to the side?
Comment: is your back also positioned like your front ?
Comment: Yes, check out the jsfiddle. `#card div` is styling for both the front and back.
Answer: The inner divs (#card div) are too wide (235 + 50 + 2) and/or #card too narrow (245). You must add padding and border to total width, or alternatively use box-sizing: border-box (with vendor prefixes).
Answer: You can get around this by using a negative margin on the "flipped" div.
#card .back {
  background: #DDD;
  margin-left: -40px;
  -webkit-transform: rotateY( 180deg );
     -moz-transform: rotateY( 180deg );
      -ms-transform: rotateY( 180deg );
       -o-transform: rotateY( 180deg );
          transform: rotateY( 180deg );
        -webkit-transform-origin: center;
            transform-origin: center;
}

http://jsfiddle.net/gmsitter/jm80wv7b/


How to place Image(plus) to top right of custom frame in swiftui?

Question: I am trying to put a plus sign in a tight frame at a very specific spot (at the right of the title text) but i cannot seem to do so
Do you know how I can do so?
var body: some View {
    VStack {
        HStack {
            Image(systemName: "plus")
            VStack(alignment: .leading, spacing: 15) {
                Text("This Month's Goal")
                    .bold()
                    .frame(width: 300, height: 10, alignment: .center)
                Text("100 out of 2000")
                    .frame(width: 300, height: 10, alignment: .center)

                //replace percent with percent and code percent to move with data
                ProgressBarView(width: 300, height: 30, percent: 50, color1: Color(#colorLiteral(red: 0.01413772535, green: 0.2225477993, blue: 0.9861308932, alpha: 1)), color2: Color(#colorLiteral(red: 0.5605781674, green: 0.9172690511, blue: 0.8047055602, alpha: 1)))
            }
            Spacer()
        }
        .padding()
    }
    .background(Color.white)
    .frame(width: 350, height: 120, alignment: .center)
    .cornerRadius(25.0)
}

This is what it looks like right now, Somewhere in the circled spot in the same frame would be ideal.
Answer: struct TestView: View {
    var body: some View {
        VStack {
            HStack {
                Image(systemName: "plus")
                VStack(alignment: .leading, spacing: 15) {
                    ZStack(alignment: .trailing) {
                        VStack {
                            Text("This Month's Goal")
                                .bold()
                                .frame(width: 300, height: 10, alignment: .center)
                            Text("100 out of 2000")
                                .frame(width: 300, height: 10, alignment: .center)
                        }

                        Button(
                            action: { },
                            label: { Image(systemName: "plus.circle") }
                        )
                    }

                    //replace percent with percent and code percent to move with data
                    ProgressBarView(width: 300, height: 30, percent: 50, color1: Color(#colorLiteral(red: 0.01413772535, green: 0.2225477993, blue: 0.9861308932, alpha: 1)), color2: Color(#colorLiteral(red: 0.5605781674, green: 0.9172690511, blue: 0.8047055602, alpha: 1)))
                }
                Spacer()
            }
            .padding()
        }
        .background(Color.white)
        .frame(width: 350, height: 120, alignment: .center)
        .cornerRadius(25.0)
    }
}

Result:

Incidentally, your progress bar might look better if you calculate the width of the completion bar (the blue-green overlay) as height + (width - height) * percent / 100 instead of as width * percent / 100.


É possivel não permitir duplicidade em combinação de 2 colunas ou mais?

Question: Eu sei que o SQL Server possui as primary Keys e iniques para não permitir duplicidade de registros, mas é possível fazer isso através de 2 colunas ou mais?
Como exemplo, tenho uma tabela com coluna, casa e cachorro, onde os registros podem repetir as casas e também os cachorros, mas não pode repetir a mesma casa com o mesmo cachorro.
INSERT INTO table VALUES ('Laranja', 'Rex') << OK
INSERT INTO table VALUES ('Azul', 'Rex') << OK
INSERT INTO table VALUES ('Laranja', 'Tob') << OK
INSERT INTO table VALUES ('Azul', 'Tob') << OK
INSERT INTO table VALUES ('Laranja', 'Rex') << ERRO - DUPLICIDADE

É possível realizar isso apenas por criação de tabela?
Answer: Sim, é possível, a forma mais simples seria assim:
CONSTRAINT nome_da_chave UNIQUE (coluna1, coluna2)

Se vai alterar uma tabela existente tem que fazer:
ALTER TABLE tabela ADD

antes disto.
Coloquei no GitHub para referência futura.
Isto está criando um índice com essas duas colunas como chave.
Se for uma chave primária então é só determinar que ambas fazem parte da primariedade já que toda primária é única.


Server OS or PowerShell version differences that would affect copy speed?

Question: I recently moved many, many files at 2 different locations. It was exactly the same set of data on each, using an identical robocopy command, but what took under 30 minutes on Windows Server 2012 R2 at our HQ (using PowerShell v4) took roughly 15 hours on Windows Server 2008 R2 SP 1 (PowerShell v2) at our ISP's server center. The latter of the two servers actually has more RAM and CPUs (twice as much, actually: 512 GB memory, 64 cores).
The only other obvious hardware difference (to me) between the two tasks is that on the 2012 server I moved the data from a local disk to a SAN volume whereas on the 2008 R2 server it was across the SAN (from LUN to LUN). I wouldn't expect this to be the culprit though since the iSCSI bandwidth between the source and destination volumes is the same on each machine.
Maybe I'm missing something so I wanted to put this out here and see what insights you all might have.
Answer: (1) DAS -> RAM -> Network -> SAN
Vs.
(2) SAN -> Network -> RAM -> Network -> SAN
Two network hops instead of just one for every single I/O within your second scenario. Network is slow compared to local disk, + you'll have issues building a pipeline as there are too many collision points.
Comment: The only utilized connections between each server / SAN pair is iSCSI - is that what "network" refers to above? (I assume so. My apologies; I'm a DBA not a network engineer). Even with twice as many iops the vast difference between durations still doesn't make sense to me. +1 not only for your input but for having a Dune reference as your user name.
Comment: This is correct statement!


Python module for change point analysis

Question: I'm looking for a Python module that performs a change-point analysis on a time-series. There are a number of different algorithms and I'd like to explore the efficacy of some of them without having to hand-roll each of the algorithms.
Ideally I'd like some modules like the bcp (Bayesian Change Point) or strucchange packages in R. I expected to find some in Scipy but I haven't been able to turn up anything.
I'm surprised that there aren't any facilities in:

statsmodels.tsa: Time series statistical analysis tools
scikits.timeseries: Time series analysis tools to extend scipy
scipy.signal: signal processing tools in scipy

Are there any modules with change point detection algorithms in Python?
Comment: Use the fused lasso in SPAMS http://spams-devel.gforge.inria.fr/ (has Python bindings).
Comment: For online time series data, how does a Change-Point Detection implementation, say [changefinder](https://github.com/shunsukeaihara/changefinder) can scale? This seems to be an inherent problem to me.
Comment: I'm also looking for change-point analysis in Python. Did you find anything useful (e.g. using RPy?).
Comment: anyone found any good changepoint analysis library by now (implementing various algorithms say binary segmentation, segment neighbourhood)?
Comment: The most popular by download: 1. ruptures 2. changefinder 3. changepy 4. baysian-changepoint-detection Source: https://www.microprediction.com/blog/popular-timeseries-packages
Answer: You can try out the changefinder library on PyPI. The description says that it's an online Change Detection Library based on the ChangeFinder algorithm
There are also some Python implementations of Michele Basseville's Statistical Change Point Detection techniques available in tutorial format on this Github repo.
Comment: A Python implementation of Bayesian Change Point Detection can also be found at [this](https://github.com/hildensia/bayesian_changepoint_detection) Github repo.
Comment: looks like the first link in the answer (amanahuja) is incomplete? the other one you posted in the comment is useful!
Answer: There are still some gaps in the Python library for using advanced statistics packages. Have you tried using the RPy module? When using RPy you can load R modules.
brief tutorial on RPy: http://www.sciprogblog.com/2012/08/using-r-from-within-python.html strucchange
Comment: is this still the case? Do I still need to end up using R-Python bridge?
Comment: anyone found any good changepoint analysis library by now (implementing various algorithms say binary segmentation, segment neighbourhood)?
Answer: I just came across a change point detection library in Python named "ruptures" : https://arxiv.org/abs/1801.00826
Maybe this can be of use.
Answer: This implementation of the Python package rpy2 worked for me:
import numpy as np
from rpy2.robjects.packages import importr
import rpy2.robjects as robjects

r = robjects.r #allows access to r object with r.

bcp = importr('bcp') #import bayesian change point package in python

values = bcp.bcp( r.c( r.rnorm(50) , r.rnorm(50,5,1), r.rnorm(50) ) ) #use bcp function on vector

posterior_means = np.array(values[5]).flatten()
posterior_probability = np.array(values[7]).flatten()

Then, you can plot the posterior means and posterior probability against the original vector.  See the bcp function example in R for more detailed information about this example.
Also, hard indexing values with a number (i.e. values[5]) is not ideal, but I was having a hard time using the rx and rx2 extractor.  So if anyone can enlighten me on a less hacky method of extraction, I'd love to know!
Answer: Depending on your requirement for online/offline change point detection, python has the below packages:
1) The ruptures package, a Python library for performing offline change point detection.
2) Calling the R changepoint package into Python using the rpy2 package, an R-to-Python interface.
3) The changefinder package, a Python library for online change point detection.
4) Bayesian Change Point Detection - both online and offline approaches.
Answer: Have you tried ChangeFinder library, you can install it on linux by:
pip install changefinder
also Bayesian_changepoint_detection GitHub code can be found here: GitHub Code

Python module for change point analysis


A topology is closed under finite union and arbitrary intersection of closed sets.

Question: Show that a topology  is closed under finite union and arbitrary intersection of closed sets.
There are some details that are bothering me in this question. Let $(E,\tau)$ be a topological space. We say that $A\subseteq E$ is closed under $\tau$ iff $A^c \in \tau$. I know there are other definitions than that where each element of $\tau$ is open, but this latter is the only which was presented to me so far. So I am supposed to work with it.
In this context, to show the proposition, that 'closed sets' in the question are actually "closed sets under $\tau$" right? For a given choice of $\tau$, I don't know if it is guaranteed that the complement of any closed set in $E$ is in $\tau$.
Furthermore, given $\tau$, is it right to say that $\tau$ is "closed under ... of closed sets"? As far as I understand, I was asked to prove that if $\tau$ is a topology on $E$ then the finite union and arbitrary intersection of closed sets of $E$ under $\tau$ is closed sets of $E$ under $\tau$. It does not mean that $\tau$ is closed under these operations of closed sets. The notion of a set being "closed under intersections or unions" is that for every element in these set, the intersection or unions between them are in the set. But all elements of  $\tau$ is open.
Can you clarify these points to me?
Thanks
Answer: Yes, it is guaranteed that the complement of any closed set in $(E,\tau)$ is in $\tau$, since, by definition, asserting that $A$ is closed means that $A^\complement\in\tau$.
On the other hand, it makes no sense to assert that $\tau$ is "closed under … of closed sets". But that is not the question. There are in fact two questions:

the set of closed subsets of $(E,\tau)$ is closed under finite unions;
the set of closed subsets of $(E,\tau)$ is closed under arbitrary intersections.
Answer: A better formulation would have been: the collection of closed sets of $(X,\tau)$ are closed under finite unions and also closed under arbitrary intersections. Often instead of the complete (and somewhat formal) $(X,\tau)$, the shorthand $X$ (or less commonly $\tau$) is used, which can be confusing, as we see.
The fact itself is immediate by de Morgan's laws for unions/intersections and complements, e.g.:
If $F_1, F_2, \ldots F_n$ are all closed then $F_1^\complement, F_2^\complement, \ldots, F_n^\complement$ are all open and so $\bigcap_{i=1}^n F_i^\complement$ is open by an axiom of topologies and so its complement $\left(\bigcap_{i=1}^n F_i^\complement\right)^\complement$ is closed and this set equals $\bigcup_{i=1}^n F_i$ by de Morgan. The intersections are shown similarly.
Answer: You are correct in your statements about closed sets in $E$ with respect to the topology $t$
Once a topology is defined on $E$ we have the open sets in $E$ and their complements are closed in $E$


Não identifico o erro - |35|error: 'd' undeclared (first use in this function)|

Question: Estou programando em C/C++ e deu um erro que não consigo identificá-lo.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

//Função Principal do Programa
 int main() {

      //Definindo variáveis
      int a = 5, b = 10, c = 15;

      //Maior
      if(a > 2){
         printf("\n %d Eh maior que 2\n", a);
      }

      //Maior ou Igual
      if(c >= b){
        printf("\n %d Eh maior ou igual a %d\n", c, b);
      }

      //Menor
      if(a < 10){
        printf("\n %d Eh menor que 10\n", a);
      }

     //Menor ou Igual
     if(a <= 10){
        printf("\n %d eh menor ou igual a 10\n", a);
     }

     //Diferente
      if(c != 10){
         printf("\n %d nao eh 10\n", c);
     }
      if(d != 'a'){
            printf("\n %c nao eh a", d);
     }

     //Pausa o Programa após executar
     system("pause");

}

Comment: Adicionar conhecimento não é um bom caminho, é melhor aprender e isto se faz de forma estruturada, com informações corretas, em ordem, com base anterior. Pode perguntar o que quiser aqui pra ir aprendendo, desde que seja pontual.
Comment: Não, mas estou pensando no assunto, o problema é que eu acho que a maioria das pessoas não gostariam dos meus cursos, as pessoas gostam dos cursos "me engana que eu gosto", eu faria algo bem puxado, que vai a fundo, que ensina mesmo, que manda o cara ir além, que mostra que é necessário questionar e não apenas absorver, as pessoas não gostam muito disso. Mas adivinha quem gosta bastante? O mercado de trabalho. Mas quem toma decisão sobre que curso fazer é a pessoa, mercado não tem consciência. De qualquer me adiciona em alguma rede, quem sabe saia alguma coisa.
Comment: Não que mude para este caso, mas precisa decidir se está programando em C ou C++, não dá para fazer em ambos.
Comment: Realmente, mas estou fazendo o curso de fundamentos de c e c++ então como sou iniciante não sei o certo se estou programando em c ou c++ (creio que c++), compreendo sua resposta, a cada dia que passa meus conhecimentos aumentam gradativamente, preferia que tivesse uma lista de "comandos" para saber corretamente declarar uma variável ou algo do tipo kkkk.   Mas obrigado pela atenção e sua resposta me ajudou muito.
Comment: Deveria ser a primeira coisa que o curso fala. Alias se o curso fala que é amos, o curso já tem problemas. Não tem uma linha de C++ nesse código, embora compile no compilador C++.
Comment: Verdade, concordo com cada palavra dita acima, e pesquisando listas de comandos percebi isso mesmo a linguagem usada é C. Agora em relação a instituição, não é das melhores mas eu faço Análise e desenvolvimento de sistemas e vou ter programação esse semestre, acontece é que já quis adiantar algumas coisas, só não sei se encontrei a melhor forma. A e além de "adiantar" eu vou adicionando conhecimentos ao meu currículo pois estou precisando trabalhar. Valeu por comentar cara.
Comment: Obrigado, vi o seu perfil e você ministra cursos?
Answer: Como a própria mensagem já indica, falta declarar a variável d. Está usando ela no último if sem que ela exista. Uma solução seria:
int a = 5, b = 10, c = 15
char d = 'a';

Coloquei no GitHub para referência futura.
Comment: Nossa, obrigado cara mas na verdade tinha esquecido do  char d = 'x';                                                       não sei dizer minunciosamente para que serve mas era isso, mas se não fosse você não teria chegado a essa conclusão. Obrigado.
Comment: @dzarm é, eu nem me atentei que o último era diferente, corrigi agora.


React Native SDK runtime permission error

Question: React Native running android says the new target SDK 22 doesn't support runtime permissions. The old target SDK 24 does though, so how do you fix it?
Image of Error
Answer: Uninstall old version of your app.
Answer: Change the following attributes in the file [project path]/android/app/build.gradle ,
<code>........
.........

android {
   compileSdkVersion 23
   buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "com.kotac"
    minSdkVersion 16
    targetSdkVersion 23
    versionCode 1
    versionName "1.0"
    ndk {
        abiFilters "armeabi-v7a", "x86"
    }
}

 .................
 .............
</code>
as needed


Showing $7n^2 + n = \Omega(n)$

Question: I have the intution of how this is true but I am facing problem to write it mathematically. Can someone help me with this?
$$7n^{2} + n=\Omega(n)$$
Comment: What is $\Omega (n)$ ?
Comment: it is asymptotic notation omega representing lower bound of a function
Answer: As $n^2 \geq 0$, we have that $7n^2 \geq 0$. Thus $n + 7n^2 \geq n$ for all $n$, and thus $n + 7n^2 = \Omega(n)$.
If you like to show constants $c$ and $N$ such that $(n + 7n^2) \geq cn$ for all $n > N$, then this shows that you can take $c = N = 1$.

Showing $7n^2 + n = \Omega(n)$


Speed of accessing local vs. global variables in gcc/g++ at different optimization levels

Question: I found that different compiler optimization levels in gcc give quite different results when accessing a local or a global variable in a loop. The reason this surprised me is that if access to one type of variable is more optimizable than access to another, I would think gcc optimization would exploit that fact.
Here come two examples (in C++ but their C counterparts give practically the same timings):
    global = 0;
    for (int i = 0; i < SIZE; i++)
        global++;

which uses a global variable long global, versus
    long tmp = 0;
    for (int i = 0; i < SIZE; i++)
        tmp++;
    global = tmp;

At optimization level -O0 the timing is essentially equal (as I would expect), at -O1 it is somewhat faster but still equal, but from -O2 the version using the global variable is much faster (a factor 7 or so).
On the other hand, in the following code fragments where start points to a block of bytes of size SIZE:
    global = 0;
    for (const char* p = start; p < start + SIZE; p++)
        global += *p;

versus
    long tmp = 0;
    for (const char* p = start; p < start + SIZE; p++)
        tmp += *p;
    global = tmp;

Here at -O0 the timings are close, though the version using the local variable is slightly faster, which doesn't seem too surprising, as maybe it will be stored in a register, whereas global wouldn't. Then at -O1 and higher the version using a local variable is considerably faster (more than 50% or 1.5 times). As remarked before, this surprises me, because I would think that for gcc it would be as easy as for me to use a local variable (in the generated optimized code) to assign to the global one later on.
So my question is: what is it about global and local variables that makes that gcc can only perform certain optimizations to one type, not the other?
Some details that may or may not be relevant: I used gcc/g++ version 3.4.5 on a machine running RHEL4 with two single core processors and 4GB RAM. The value I used for SIZE, which is a preprocessor macro, was 1000000000. The block of bytes in the second example was dynamically allocated.
Here are some timing outputs for optimization levels 0 to 4 (in the same order as above):
$ ./st0
Result using global variable: 1000000000 in 2.213 seconds.
Result using local variable:  1000000000 in 2.210 seconds.
Result using global variable: 0 in 3.924 seconds.
Result using local variable:  0 in 3.710 seconds.
$ ./st1
Result using global variable: 1000000000 in 0.947 seconds.
Result using local variable:  1000000000 in 0.947 seconds.
Result using global variable: 0 in 2.135 seconds.
Result using local variable:  0 in 1.212 seconds.
$ ./st2
Result using global variable: 1000000000 in 0.022 seconds.
Result using local variable:  1000000000 in 0.552 seconds.
Result using global variable: 0 in 2.135 seconds.
Result using local variable:  0 in 1.227 seconds.
$ ./st3
Result using global variable: 1000000000 in 0.065 seconds.
Result using local variable:  1000000000 in 0.461 seconds.
Result using global variable: 0 in 2.453 seconds.
Result using local variable:  0 in 1.646 seconds.
$ ./st4
Result using global variable: 1000000000 in 0.063 seconds.
Result using local variable:  1000000000 in 0.468 seconds.
Result using global variable: 0 in 2.467 seconds.
Result using local variable:  0 in 1.663 seconds.

EDIT
This is the generated assembly for the first two snippets with switch -O2, the case where the difference is largest. For as far as I understand, it looks like a bug in the compiler: 0x3b9aca00 is SIZE in hexadecimal, 0x80496dc must be the address of global.
I checked with a newer compiler, and this doesn't happen anymore. The difference in the second pair of snippets is similar however.
    void global1()
    {
        int i;
        global = 0;
        for (i = 0; i < SIZE; i++)
            global++;
    }

    void local1()
    {
        int i;
        long tmp = 0;
        for (i = 0; i < SIZE; i++)
            tmp++;
        global = tmp;
    }

    080483d0 <global1>:
     80483d0:   55                      push   %ebp
     80483d1:   89 e5                   mov    %esp,%ebp
     80483d3:   c7 05 dc 96 04 08 00    movl   $0x0,0x80496dc
     80483da:   00 00 00
     80483dd:   b8 ff c9 9a 3b          mov    $0x3b9ac9ff,%eax
     80483e2:   89 f6                   mov    %esi,%esi
     80483e4:   83 e8 19                sub    $0x19,%eax
     80483e7:   79 fb                   jns    80483e4 <global1+0x14>
     80483e9:   c7 05 dc 96 04 08 00    movl   $0x3b9aca00,0x80496dc
     80483f0:   ca 9a 3b
     80483f3:   c9                      leave
     80483f4:   c3                      ret
     80483f5:   8d 76 00                lea    0x0(%esi),%esi

    080483f8 <local1>:
     80483f8:   55                      push   %ebp
     80483f9:   89 e5                   mov    %esp,%ebp
     80483fb:   b8 ff c9 9a 3b          mov    $0x3b9ac9ff,%eax
     8048400:   48                      dec    %eax
     8048401:   79 fd                   jns    8048400 <local1+0x8>
     8048403:   c7 05 dc 96 04 08 00    movl   $0x3b9aca00,0x80496dc
     804840a:   ca 9a 3b
     804840d:   c9                      leave
     804840e:   c3                      ret
     804840f:   90                      nop

Finally here is the code of the remaining snippets, now generated by gcc 4.3.3 using -O3 (though the old version seems to generate similar code). It looks like indeed global2(..) compiles to a function accessing the global memory location in every iteration of the loop, where local2(..) uses a register. It is still not clear to me why gcc wouldn't optimize the global version using a register anyway. Is this just a lacking feature, or would it really lead to unacceptable behaviour of the executable?
    void global2(const char* start)
    {
        const char* p;
        global = 0;
        for (p = start; p < start + SIZE; p++)
            global += *p;
    }

    void local2(const char* start)
    {
        const char* p;
        long tmp = 0;
        for (p = start; p < start + SIZE; p++)
            tmp += *p;
        global = tmp;
    }

    08048470 <global2>:
     8048470:   55                      push   %ebp
     8048471:   31 d2                   xor    %edx,%edx
     8048473:   89 e5                   mov    %esp,%ebp
     8048475:   8b 4d 08                mov    0x8(%ebp),%ecx
     8048478:   c7 05 24 a0 04 08 00    movl   $0x0,0x804a024
     804847f:   00 00 00
     8048482:   8d b6 00 00 00 00       lea    0x0(%esi),%esi
     8048488:   0f be 04 11             movsbl (%ecx,%edx,1),%eax
     804848c:   83 c2 01                add    $0x1,%edx
     804848f:   01 05 24 a0 04 08       add    %eax,0x804a024
     8048495:   81 fa 00 ca 9a 3b       cmp    $0x3b9aca00,%edx
     804849b:   75 eb                   jne    8048488 <global2+0x18>
     804849d:   5d                      pop    %ebp
     804849e:   c3                      ret
     804849f:   90                      nop

    080484a0 <local2>:
     80484a0:   55                      push   %ebp
     80484a1:   31 c9                   xor    %ecx,%ecx
     80484a3:   89 e5                   mov    %esp,%ebp
     80484a5:   31 d2                   xor    %edx,%edx
     80484a7:   53                      push   %ebx
     80484a8:   8b 5d 08                mov    0x8(%ebp),%ebx
     80484ab:   90                      nop
     80484ac:   8d 74 26 00             lea    0x0(%esi,%eiz,1),%esi
     80484b0:   0f be 04 13             movsbl (%ebx,%edx,1),%eax
     80484b4:   83 c2 01                add    $0x1,%edx
     80484b7:   01 c1                   add    %eax,%ecx
     80484b9:   81 fa 00 ca 9a 3b       cmp    $0x3b9aca00,%edx
     80484bf:   75 ef                   jne    80484b0 <local2+0x10>
     80484c1:   5b                      pop    %ebx
     80484c2:   89 0d 24 a0 04 08       mov    %ecx,0x804a024
     80484c8:   5d                      pop    %ebp
     80484c9:   c3                      ret
     80484ca:   8d b6 00 00 00 00       lea    0x0(%esi),%esi

Thanks.
Comment: note that when SIZE and global is nonvolatile, compilers might be able to change the whole loop into a single global += SIZE; statement. Using an ancient gcc and only 4MB of ram makes me wonder what hardware you run this on, since any answer is heavily depending on hardware and possibly other things. That said, you might rather want to analyze the generated assembler code yourself, possibly with a good assembler level profiler.
Comment: you sure that's a proper username for this site?
Comment: A useful thing to do would be to grab the resulting machine code generated by the compiler in each case (using `objdump` or similar), and add this to your question.
Comment: @doetoe: look at what the assembler output does. Also note that any "could optimize" is not equal to "is guaranteed to optimize", especially with such ancient compilers. To know what is going on, there is no better thing as to look what is going on.
Comment: The code is certainly not optimal, but this is not a bug in the compilers. Compilers evolve, and especially when using ancient ones as gcc 3.x it is to be expected that things don't optimize as good. We are currently at 4.6.1 ...
Comment: Sorry PlasmaHH, I meant to write 4GB. The hardware is a pretty standard HP xw8200 workstation.
Comment: PlasmaHH is right. For the consecutive increments, gcc 4.5.0 optimizes away the whole loop when using "-O2". Also with that newer gcc, the optimized compiles always showed the same runtime for global and for local variables here. With non-optimized compilations, the versions directly accessing global where a bit faster.
Comment: @PlasmaHH: the question remains why the compiler in that case could change one statement into global += SIZE, but not the other into tmp += SIZE. In fact, I would expect it to be safer to do that with the local variable than the global one.
Comment: Definitely agree with PlasmaHH: Look at the assembly output first thing. Post it here if you want additional input on it.
Comment: @PlasmaHH, Oli Charlesworth, undur_gongor, Kerrek SB: thanks. I'll add some generated assembly to the main post.
Comment: @Mitch Wheat: could you elaborate?
Comment: I don't see what makes you think there is a bug in the compiler?
Comment: @PlasmaHH: I have very little experience with machine code, but I would think that80483fb:   b8 ff c9 9a 3b          mov    $0x3b9ac9ff,%eax
     8048400:   48                      dec    %eax
     8048401:   79 fd                   jns    8048400
     8048403:   c7 05 dc 96 04 08 00    movl   $0x3b9aca00,0x80496dc
     804840a:   ca 9a 3b
Comment: @doetoe: but why do you think this is a bug? what do you think this code does differently than what it should do?
Comment: @PlasmaHH: the previous comment didn't come through correctly, here it comes again: I have very little experience with machine code, but I would think that `80483fb: mov $0x3b9ac9ff,%eax`, `8048400: dec %eax`, `8048401: jns 8048400` executes a loop 0x3b9ac9ff = 1000000000 - 1 times, at the end of which the value of %eax is 0 and nothing else is done. In other words, this looks to me to be equivalent to setting %eax to 0. In the next instruction `movl $0x3b9aca00,0x80496dc` the value 0x3b9aca00 = 1000000000 is loaded into the address of "global". But then again, I hardly know any machine code.
Answer: A local variable tmp whose address is not taken cannot be pointed to by the pointer p, and the compiler can optimize accordingly. It is much more difficult to infer that a global variable global is not pointed to, unless it's static, because the address of that global variable could be taken in another compilation unit and passed around.
If reading the assembly indicates that the compiler forces itself to load from memory more often than you would expect, and you know that the aliasing it worries about cannot exist in practice, you can help it by copying the global variable into a local variable at the top of the function and using only the local in the rest of the function.
Finally, note that if pointer p had been of another type, the compiler could have invoked "strict aliasing rules" to optimize regardless of its inability to infer that p does not point to global. But because lvalues of type char are often used to observe the representation of other types, there is an allowance for this kind of alias, and the compiler cannot take this shortcut in your example.
Comment: @Oli Charlesworth Compared to the version you commented on, I have changed the sentence to "It is much more difficult to infer that a global variable `global` is not pointed to". As you say, it could depend on what `start` is. But the problem of determining whether `*p` aliases with `global` is so difficult in general that the compiler probably does not even try (whereas the "local variable whose address is not taken" criterion is syntactic, local and works very well in practice).
Comment: This answer is exactly right.  For all the compiler knows, you might call `global2` with `&global` lying somewhere between `start` and `start+SIZE`.  So the compiler has to emit memory accesses for every loop iteration.  Using `char *` makes this especially hard to optimize because it can alias anything...  Try writing a similar loop with `*start` and `global` being objects of different types, and then the compiler can easily prove the pointers do not alias.
Comment: Sounds plausible.  But I guess it depends on what `start` is.
Comment: Thanks for your answer. However, when the global variable is non-volatile, isn't it only other threads that could change the value while executing? And isn't it the responsibility of the programmer (rather than the compiler) to provide thread safeness?
Comment: @doetoe: the problem is that a global variable may be manipulated indirectly via pointers. If the compiler can prove that there's no write through a pointer that may alias the global, it can still optimize the access. But that's a big _if_
Comment: @doetoe I can only repeat what I said to Oli Charlesworth: if the variable is not static, its address can always be taken in another compilation unit, and then passed around, and end up where you don't expect it. It would still be possible to predict the absence of alias with a global in some cases: in fact, I work on an analysis that does just that (see my bio). But this kind of analysis is extremely difficult, and the compiler may not even try.
Comment: @Oli Carlesworth, Pascal Cuoq, MSalters, Nemo: Thanks a lot. Until Nemo's comment I didn't understand the other comments, mainly because I couldn't think of any situation (and consequently thought there wasn't any) in which the value of `global` at intermediate stages of the loop could have any influence on the result. Now I realize all of you were explaining me in which situation this could happen.
Comment: @MSalters: It's too bad the Standard doesn't allow extern or global variables to have a "register" qualifier, since if the semantics were such that a compiler could keep its value cached in registers across pointer accesses of any type, that would enable many useful optimizations that would be otherwise unavailable.
Answer: Global variable = global memory, and subject to aliasing (read as: bad for the optimizer -- must read-modify-write in the worst case).
Local variable = register (unless the compiler really can't help it, sometimes it must put it on the stack too, but the stack is practically guaranteed to be in L1)
Accessing a register is on the order of a single cycle, accessing memory is on the order of 15-1000 cycles (depending on whether the cache line is in cache and not invalidated by another core, and depending on whether the page is in the TLB).
Comment: Sounds plausible. But I guess it depends on what `start` is.


Como reutilizar adequadamente as regras de negócio usando EJBs?

Question: Estou refatorando um sistema onde a maior preocupação é diminuir o acoplamento e complexidade dos vários níveis de herança, portanto os exemplos são ilustrativos.
Tenho as entidades Contrato, ContratoUso e ContratoAdesao. Tenho ainda um session bean ContratoRepository com as regras de negócio da entidade Contrato.
As regras de negócio dos contratos mais especializados tem grande parte das regras de negócio do Contrato.
Qual a melhor forma de implementar isto?
Pensei inicialmente em algumas possibilidades:
Herança
O ContratoUsoRepository e os demais herdam as regras de ContratoRepository e implementam suas especifidades.
Exemplo usando generics:
ContratoRepository<M extends Contrato> {
  recuperarContrato(int id) {
    return M;
  }
}

ContratoUsoRepository extends ContratoRepository<ContratoUSO>() {
  //métodos específicos de ContratoUSO
}

Delegação
Uso composição, tendo ContratoRepository injetado em ContratoUsoRepository e delegando para contrato o que é comum?
Exemplo com injeção de dependências:
ContratoUsoRepository {

  @EJB
  ContratoRepository contratoRepository;

  recuperarContrato(Int id){
     return contratoRepository.recuperarContrato(id);
  }

}

Exemplo :
na questão do meu problema os métodos de negócio tem o mesmo comportamento só muda a entidade especifica de retorno,
por ex:
pesquisarContratoPorPeriodo(Date inicio,Date fim);

posso facilmente herdar esse metodo para qualquer contrato, pois o comportamento é o mesmo, só muda o tipo de contrato retornado.
ou ainda assim é preferivel delegar ? mesmo não fazendo nenhuma tarefa adicional.
delegar seria simplesmente:
contratoAdesaoService{
     @EJB contratoService
    pesquisarContratoAdesaoPorPeriodo(date inicio,Date fim){
          return contratoService.pesquisarContratoAdesaoPorPeriodo(inicio,fim);
}

Comment: Java não é a minha praia e não sei se entendi bem a pergunta, mas veja se estas perguntas aqui te ajudam: http://pt.stackoverflow.com/questions/11594/%C3%89-errado-usar-heran%C3%A7a-de-classes-para-agrupar-comportamentos-e-atributos-comuns, http://pt.stackoverflow.com/questions/11378/%C3%89-correto-dar-maior-prefer%C3%AAncia-a-composi%C3%A7%C3%A3o-do-que-heran%C3%A7a
Comment: Bruno, editei sua pergunta para tentar melhorá-la. Por favor, se algo não ficou de acordo com sua intenção original, pode editá-la novamente. Abraço.
Comment: Particularmente eu recomendo herança, por ser mais tradicional. Não possuo um argumento técnico muito forte, apenas por tradicionalismo, e por outra, é mais fácil algum dia mudarem as anotações pra injeção do que mudarem a forma como é a realizada a herança (rsrsr).
Comment: Os detalhes apresentados são insuficientes para uma boa decisão. Por padrão, eu evito a herança até que ela se prove necessária. E depois continuo evitando hehehe. Herança adiciona complexidade e compromete a abstração - geralmente vale a pena gastar um tempo a mais buscando uma solução mais simples. A propósito, por mais detalhes que se apresente, esta tende a ser uma discussão longa onde nenhuma resposta será melhor que a outra (exceto é claro algumas respostas simplórias que com certeza surgirão). Creio que esta seja uma pergunta off topic pro SOpt.
Comment: Obrigado, é exatamente essa a dúvida.
Answer: Basicamente, as duas perguntas linkadas pelo @bfavaretto já responderiam sua questão no que se refere à modelagem de classes. São elas:

É correto dar maior preferência a composição do que herança?
É errado usar herança de classes para agrupar comportamentos e atributos comuns?

Porém, é possível analisar especificamente o seu caso do ponto de vista de um Caso de Uso da aplicação ou da arquitetura EJB.
Casos de Uso
Casos de Usos de uma aplicação podem herdar outros Casos de Uso. Isso inclui a execução de determinadas determinadas regras de negócio do caso de uso herdado. Isso é comum em modelagem de sistemas.
Porém, herança em Casos de Uso não funcionam exatamente como herança em orientação a objetos. Ela consiste em inserir passos adicionais no Caso de Uso principal.
Na prática, isso não dita que você precisa necessariamente usar herança nas classes que implementam esses casos de uso. Existem várias técnicas para criar pontos de extensão no seu código.
Enterprise Java Beans
Não recomendaria usar herança em classes EJBs. Essas classes possuem um contrato de funcionamento com o container. Com herança você pode acabar em situações complicadas em que é difícil saber se algum detalhe deve ou não ser herdado. Isso sem contar possíveis problemas com anotações, transações e visibilidade de acesso aos elementos.
Delegação
Minha sugestão é que a herança de classes de regras de negócio sejam evitadas ao máximo. Usar delegação permite reusar as regras de negócio onde for necessário sem necessidade de gambiarras com hierarquia de classes.
Evite acoplamento
De preferência, faça com que seus métodos de regras de negócio tenham baixo acoplamento para serem mais reaproveitados.
Por exemplo, ao invés de um método que assume várias coisas e acesso o banco para fazer uma validação assim:
public void validarContrato(int idContrato) {
    Contrato contrato = contratoRepository.find(idContrato);
    if (contrato.hasErrors()) {
        throw new RuntimeException("Contrato inválido");
    }
}

Você poderia receber um objeto Contrato:
public void validarContrato(Contrato contrato) {
    if (contrato.hasErrors()) {
        throw new RuntimeException("Contrato inválido");
    }
}

Dessa forma, você pode validar contratos antes de colocar no banco de dados. Isso é muito útil para fazer testes unitários e exibir erros para o usuário antes de cadastrar algo no banco de dados.
Pontos de Extensão usando Interfaces
Uma das formas que considero mais produtiva para implementar pontos de extensão em casos de uso é criar uma interface que funciona como um callback. No Java 8 você pode usar lambdas para fazer isso, porém vou fazer usando o modo tradicional para ilustração.
Imagine que você tem um caso de uso da seguinte forma:
public void criarContrato(Contrato c) {
    recalcularContrato(c);
    validarContrato(c);
    //algo específico aqui
    gravarContrato(c);
}

Caso você queira colocar algum passo ali que varie de acordo com a situação, pode adicionar um parâmetro que funciona como um ponto de extensão.
Para isso, vai precisar de uma interface como a seguinte:
public interface ExtensaoContrato {
    executar(Contrato c);
}

Então você pode alterar o método da seguinte forma:
public void criarContrato(Contrato c, ExtensaoContrato pontoExtensao) {
    recalcularContrato(c);
    validarContrato(c);
    if (pontoExtensao != null) pontoExtensao.executar(c);
    gravarContrato(c);
}

Isso funciona mais ou menos como o Strategy Pattern. A ideia é que você deixa um passo a ser provido pelo cliente que vai chamar o método, podendo haver quantas implementações forem necessárias da interface.
Então o método que chama a criação do contrato pode fazer da seguinte forma:
criarContrato(contrato, new ExtensaoContrato() {
        public void executar(Contrato c) {
            //faz algo depois da validação e antes de gravar
        }
    });

O código acima executa o método criarContrato passando o contrato e também uma classe anônima que implementa ExtensaoContrato. O método executar, implementado nesta classe anônima, vai ser executado então pelo método criarContrato.
Assim você pode "injetar" qualquer código ali, em situações onde vocês tem um Caso de Uso geral e precisa apenas fazer algo específico em um ou mais pontos.
No caso desta pergunta, um dos cenários (dependendo de como é a implementação real) é ter uma implementação para cada tipo de contrato.
Delegação simples
Outra opção é fazer a delegação simples, como já descrito na sua pergunta. Se não houver muitas variações e heranças é a escolha certa, pois é simples e direto.
Considerações finais
Um dos erros mais comuns, principalmente de quem está num nível intermediário em desenvolvimento, é tentar aplicar padrões e hierarquias de classes desnecessariamente no código.
Isso não é de todo ruim. Significa que o desenvolvedor está amadurecendo. Porém, o ponto fraco de todo padrão e da herança é aumentar muito a complexidade do código. É algo difícil de medir, mas seria necessário considerar se essa complexidade adicional traz mais benefícios do que malefícios para o modelo de classes.
Comment: Muito bacana sua resposta, acrescentou bastante, irei dar uma estudada nessa questão de **Pontos de Extensão usando Interfaces** parece bem util em alguns casos, além de sofisticado.
editei minha pergunta , se possível me confirme se nesse último exemplo que dei (que é o meu contexto em algumas situações) delegar ainda seja a melhor opção. obrigado.
Comment: @BrunoSerqueira Se uma única implementação de repositório é capaz de dar conta de todos os tipos de contrato, você não precisa de herança. Pode ter uma única classe com métodos *generic*: https://docs.oracle.com/javase/tutorial/extra/generics/methods.html
Comment: eu quis dizer que os métodos em comum tem o mesmo comportamento, sem nada adicional, porém eles também tem seus comportamentos especificos.


"access ckeditor" permission missing - buttons inactive

Question: I have my ckeditor basically up and running. Users with the role 'editor' however can see and use it, but the buttons appear to be inactive. Just the "Source" and "Format" are accessible for editors.

And in under permissions > ckeditor I have no permission for "access ckeditor". Any help / pointers welcome! Thank you!
ps: Editors do have permission to use the related text format "full html".
Comment: CKeditor doesn't define any such permission...traditionally you would tie CKeditor to text formats, and then set the permissions on those formats. Have you tried that?
Comment: thank you for your reply. but, as mentioned above that's exactly what i've done. The idea with that permission arose from a 'how to' article over here: http://tomandcrystal.com/how_to_set_up_the_ckeditor_module_in_drupal (under [7])
Comment: Yeah I thought as much, but maybe you hadn't set up the CKeditor profile itself and linked it to the text format (guess you have done). Not sure where that article is getting its information, you can see from [`ckeditor_permission()`](http://api.devtrac.org/api/ckeditor/ckeditor.module/function/ckeditor_permission/7.x-1.x) that there's no "access ckeditor". The way text formats and profiles are set up it wouldn't make much sense for there to be. The text format permissions should be all that's required
Answer: Thank you for your suggestions. I suppose it probably is some permissions problem. I now granted the "Customize CKEditor appearance" permission for the role Editor and the toolbar is now functional. However it leaves me puzzled why this paprticular permission would trigger whether a user / role is allowed to use the toolbar buttons or not.

Customize CKEditor appearance
Allow users to customize CKEditor appearance.

Either it's a wording problem and granting that permission is the right thing to to – or I now actually have granted permission to customize the CKEditor appearance (as the wording would suggest) and I have a slight security problem However – i'll submit a request for support issue over at drupal.org and edit this post once I know more…
PS: Even thought this question might leave the impression – it's not my first time on drupal. It's just the first time I'm using CKEditor instead of WYSIWYG.


Avisar quando um tempo for diferente do outro

Question: Olá, tenho duas vars armazenando dois tempos no formato minutos:segundos:milisegundos:
var tempo1 = "11:10:10";
var tempo2 = "11:10:10";

Gostaria de gerar um alerta quando o tempo1 for dirente(maior ou menor) em 0,5seg do tempo2.
Comment: E você terá valores decimais nos segundos? Pode [edit] a pergunta e colocar o que já tentou fazer e escrever a dificuldade encontrada?
Comment: Ainda não fiz nada, estou perdido em como iniciar essa comparação, valores decimais no segundo
Comment: Esses 3 parametros de tempo são horas, minutos e segundos? ou minutos, segundos, milisegundos?
Comment: @DvD eu não percebi como é que se pode detectar diferenças de menos de um segundo se o grau de exatidão do input é 1 segundo (no caso de ser `hh:mm:ss`). Daí a dúvida se o ultimo parâmetro seria  milisegundos...
Answer: Se o teu input é minutos:segundos:milisegundos podes saber se a diferênça entre os dois é menos que 300ms por exemplo assim:

function diff300(a, b) {
  a = a.split(':').map(Number);
  b = b.split(':').map(Number);
  return Math.abs(
      new Date(2000, 0, 1, 1, ...a) - new Date(2000, 0, 1, 1, ...b)
  ) > 300;
}

console.log(diff300("11:10:10", "11:10:10")); console.log(diff300("11:10:10", "11:10:510"));

A ideia é criar duas datas com os minutos, segundos e milisegundos como única diferênça e depois verificar se a diferênça absoluta entre elas é menor que 300ms
Answer: Mais abaixo, segue uma resposta alternativa e mais curta. E também mais econômica em recursos.
A função recebe três argumentos, em ordem:

tempo 1
tempo 2
valor a ser comparado em milissegundos (500, neste caso)

Se não precisar de algo genérico (sempre 500ms), pode-se retirar o 3º argumento. Mas não esquecer de trocar em todos os locais dentro da função, de forma apropriada, por 500 onde havia o nome do terceiro argumento.
Ao converter os tempos para Date, precisa-se colocar dados falsos para zona de tempo e dia, mês e ano. Não interfere com o resultado já que esses dados extras são iguais para ambos os casos.
A função retorna true para casos em que a diferença seja maior ou menor que o valor especificado no 3º argumento. Retorna false caso seja exatamente e igual ao valor especificado.
A forma de lidar com o resultado fica ao gosto do freguês. Se quiser usar alert() ou outros recursos, basta alterar.

// Tempos armazenados a serem verificados
var tempo_1 = "13:12:11";
var tempo_2 = "13:12:12";

// Função verificadora de diferença
function dif_ms(tempo_1, tempo_2, comparativo) {

  // Converte os tempos para o tipo `Date` e obtme tempo total em milissegundos
  var t1 = new Date("2000-01-01T" + tempo_1 + "Z").getTime();
  var t2 = new Date("2000-01-01T" + tempo_2 + "Z").getTime();

  // Verifica a diferença
  var delta = Math.abs(t1 - t2);

  // Retorna 'true' se for diferente (maior ou menor) que 500ms ou 0.5s
  return delta != comparativo;

}

// Exemplo de saída
console.log("- É diferente de 500ms (ou 0,5s)? \n- " + (dif_ms(tempo_1, tempo_2, 500) ? "Sim." : "Não."));
Comment: Haha, respondemos no mesmo minuto, com mais ou menos a mesma ideia :) `+1`
Comment: @Sergio LOL! Acabo de ver, haha! *high five* Mas devo dizer que a sua é mais elegante! ;)
Comment: @Ramos ao fazer a pergunta (ou resposta), vai ter um botão ao lado direito do botão de inserção de imagem. Ele apresenta "< >" desenhado nele. Ao clicar, vai abrir um "modal" (imitação de janela) nos moldes do JSFiddle. Aí é só colocar o código no local apropriado (CSS, HTML e JavaScript).
Comment: Como é que se coloca esses exemplos e execução de código aqui no Stack Overflow?
Comment: @José Obrigado! Vou usar.


The Standard Error in R language lm (linear regression) is Standard Deviation or Standard Error of the Mean?

Question: When we are estimating the coefficient in R, "Std Err" will be produced by "lm". Is it Std or SEM? How is it calculated? Run lm function with any R data set will give an example.
Comment: I think R returns the estimated standard deviation of the sampling distribution of the coefficients.
Answer:            Coefficients:
                         Estimate  Std. Error   t value Pr(>|t|)
             (Intercept)  0.02224    0.03189   0.697    0.486
                 x       -0.03747    0.03214  -1.166    0.244

If you mean the column with header Std. Error, then it is the estimated standard error of estimated regression coefficients.
Comment: Thank you! Is it standard deviation? What is the formula used to determine the Std. Error?
Comment: When you cannot figure out you should call it standard error or standard deviation, you can just call its square as **variance**. The appear of standard error or standard deviation statistics is disaster. For calculation, see https://stats.stackexchange.com/questions/85943/how-to-derive-the-standard-error-of-linear-regression-coefficient