SNMP (UDP) Proxy

Question: I'm Working in a mid-sized telco, and I'm in charge of network monitoring solutions. We have a custom monitoring system, that is using mostly SNMP to communicate with devices. The problem is that every once in a while, we need to add additional polling servers, and everytime we have to update 10k+ ACL's on each SNMP enabled device to add the new IP addresses. They cannot be predicted in advance, because servers are situated in different data centers, with different L3 connectivity.
This is hard work for our operations, and they're not happy with it.
My question: Is there any SNMP or UDP proxy that we can just install on one host, and forward every SNMP request to the devices through it?
Comment: How custom is your monitoring system?  Built completely from scratch, or something built on top of Nagios or some other monitoring system?
Comment: Entirely from scratch.
Comment: Is IPv6 not an option?
Comment: IPv6 is no help for this problem
Answer: I don't think you want an SNMP proxy as much as you want an SNMP NAT gateway.
A proxy requires parsing the results of each query, which by-nature is slower than merely performing NAT on the queries. AFAICT, you only care about reducing the number of source-addresses for snmp ACL updates on your routers / switches / firewalls.
Simple solution: deploy a special Cisco router or linux server as a NAT gateway for your SNMP queries.  You should restrict the source addresses to be NAT'd as well as the protocol (161/udp).  Your best option for routing snmp queries through this device is to build a GRE tunnel to it and ensure that all SNMP queries get routed from the snmp pollers through the tunnels to the NAT gateway.
Keep in mind that no matter which way you go, centralizing snmp queries through a NAT gateway or proxy will slow down your pollers a bit due to the incrementally longer round-trip times for each SNMP get / get-next operation.
Comment: Thanks, Do you know if this is possible via some iptables rules on the polling hosts? Because these hosts do other things, but we want only the SNMP requests to be routed through this router? How can this be done?
Comment: There is no simple, host-level NAT configuration, because even if you NAT to a single subnet across several geographically-distributed pollers the queries must somehow get back to the poller (which is impossible if you hide the IP source-address with an essentially bogus NAT entry)
Comment: re: "centralizing snmp queries through a NAT gateway or proxy will slow down your pollers", the fact that @Jacket already has some IP's in the ACLs means he can have a couple of NAT devices to split the load across. that should help somewhat.
Comment: I agree with Mike's post. NAT/Forwarding is done on kernel level and it's more common, bug-less and optimized too. Proxy will work on higher level and require more CPU resources. ex: HAproxy
Answer: put all of your pollers in a dedicated subnet, or a couple of dedicated subnets. make sure you have room for growth. updated your ACL's to allow polling from any host in those subnets. if you leave enough room, this will be the last ACL update you'll ever need.
Comment: +1 I agree with this approach as long as he's willing to physically relocate his pollers.  That is not implied in his question though
Comment: changing or dedicating subnets doesn't require a physical move; it could be as simple as a small network configuration change. for example, in my infrastructure all this would take is adding a new vlan to a couple of switches and an entry in a routing table.
Comment: I have trouble believing the OP is so naive that he's asking the question when it's as simple as changing vlans... remember, he is talking multiple pollers and multiple data centers
Comment: it's not a question of naivete. it's likely this started out as one server, then two, then another server in another data center. with that type of piecemeal, organic growth, a dedicated subnet is not the obvious answer. simple, but not obvious. sometimes it's the simplest stuff that gets overlooked!
Answer: The larger shops I've seen have often gone in the direction of setting up a single common polling infrastructure.  A set of dedicated pollers pulls the data and then makes it available for various consumer apps.  This scales a lot better and ends up leading to a lot less traffic and reduction in control plane usage on monitored devices.  This sounds like what you're after.
There's actually an SNMP proxy MIB out there that's built into Net-SNMP.  You may be able to set up a couple of machines in such a way, although it will likely require some customization.  There are also commercial packages like this that may provide additional value in terms of protocol translation, support, etc.

SNMP (UDP) Proxy


Čech-Stone compactification of $\mathbb N$ and ultrafilters on $\mathbb N$

Question: I have found in the literature that the Čech-Stone compactification $\beta\mathbb N$ of $\mathbb N$ (or more generally, of any discrete topological space) can be identified with ultrafilters on $\mathbb N$. Under this identification, the natural numbers correspond to the principal ultrafilters.
How exactly does the identification of $\beta\mathbb N$ and the ultrafilters on $\mathbb N$ look like? How does the topology on the set of ultrafiltres on $\mathbb N$ look like?
Comment: The topology on the set $\mathcal U$ of ultrafilters has basic open sets of the form $B_X=\{u\in\mathcal U:X\in u\}$ where $X\subseteq\mathbb N$.
Answer: The object $\beta\mathbb N$ is notoriously elusive. It is an important object of study in set theoretic topology and is highly sensitive to various set-theoretic axioms. Generally speaking, non-principal ultrafilters are very elusive, their general existence requiring a weak form of the axiom of choice. Thus, it is very difficult to imagine what ultrafilters look like, let alone what $\beta \mathbb N$ looks like.
One thing for instance is that one can extend the addition of natural numbers to an addition operation on $\beta \mathbb N$ but it is not commutative. You can further read quite a lot on $\beta \mathbb N$ on any text on set-theoretic topology, but as said, this is far from conclusive or straightforward.
Comment: See http://www.math.toronto.edu/~drorbn/classes/9293/131/ultra.pdf

Comment: Thank you, but this does not answer my question...
Comment: Do you mean then to ask how to prove that $\beta \mathbb N$, as the set of ultrafilters on $\mathbb N$, is the required compactification? if so, then any text introducing it will answer that.
Comment: I would like to see the prescription of the mapping which identifies $\beta\mathbb N$ with the ultrafilters.
Answer: It might look like an obvious reference, but I consider the wikipedia survey on Stone–Čech compactification one of the best mathematical articles in wikipedia. In particular, it gives four different constructions of the Stone–Čech compactification, where most textbooks give only one or two constructions.


Correlating various diagnoses with amount of weight gained

Question: I have a processed dataset where patients took a medication and gained a varying amount of weight. In addition, any ICD9 diagnoses the patients have received while being monitored are recorded. I'd like to see if certain diagnoses are correlated with amount of weight gained. What would be a good procedure for testing this?
Of note, each patient has several diagnoses: there are about 1,200 unique diagnoses in total.
Currently, I have calculated the average wt. gain along with the standard deviation, then grouped the patients by standard deviation, say:
<code>(not actual #'s)
Total Patients: 420
Total in Group 1 (>2 Sd weight Loss): 10
Total in Group 2 (>1 Sd weight Loss: 50
Total in Group 3 (<1Sd weight gain or loss): 300
Total in Group 4 (>1 sd weight Gain): 50
Total in Group 5 (>2 sd weight Gain): 10
</code>
I was thinking of looking at how many patients have x,y,z (etc.) diagnoses in each group. Say Type II DM:
<code>Total: 200 Pts Have it
In Group 1: 4/10 Pts have it
Group2: 15/50 Pts have it
etc...
</code>
With this method, would simply computing a Pearson correlation on the absolute count of Diagnoses vs. wt change group be appropriate?  Additionally, if by the time I get to Group 2 or Group 4 0 patients carry a particular diagnoses, how will this impact running the analysis this way?
Appreciate any pointers.
Comment: Do you have the actual weight measurements, or is it just those categories? (I hate this medical approach of taking an arbitrary threshold and classify everybody either above or below it; economists, at least, would've broken this down to five quintiles and have the same number of people in each group.) I would think that the s.d. needs to be computed within the diagnosis, rather than across everybody, to make the standardized effect sizes comparisons meaningful.
Comment: Yes, have the original weights; can break them down into as many groups as necessary.
Answer: Before proceeding you need to make sure that the subject was free of all the diseases at the time of the initial weight.
If you were just considering one diagnosis you could estimate the probability of the diagnosis as a function of a smooth function of the initial weight and a smooth function of the final weight.  It is important not to assume that weight change is an adequate summary of the two weights.
With multiple diagnoses a complex but general solution may possibly be had by turning the problem backwards: predict the optimum linear combination (first canonical variate) of the weight variables and nonlinear transformations of them from 1200 binary dummy variables, using lasso-like penalization of the disease effects.
For a few diagnoses (especially diabetes using glycohemoglobin as a continuous outcome measure) you can turn to NHANES data to model patient disease outcomes or medical test results, starting with a cohort that is disease-free with respect to that disease.
Comment: Why would they need to be free of disease before the initial weight? To be clear, I'm interested in the 1) The disease indication for the medication as affecting the outcome 2) Any co-morbidities that may affect the outcome. Additionally, the intial diagnoses is sometimes incorrect or very general, with the specific, accurate diagnoses coming out further into treatment.
Comment: A drug may be given because someone has a disease.  You can't claim then that the drug caused the disease; it's the other way 'round.  Also, some diseases may predispose patients to gain weight regardless of their meds.  There may be a way around this but you need to very explicitly lay out all your assumptions, then critically assess them.
Comment: I'm sorry, let me clarify - indications for the drug are wide - pt may be given the drug based on diagnosis A,B,C, etc... In addition, they may also have other diagnoses un-related to the one they were given the medication for. I'm interested in both sets.
Comment: That makes sense, but how do you make sense when you have a mixture of patients who already had the disease before the weight gain, and those who developed the disease after gaining weight?


Does it matter if a digital piano has a large cabinet?

Question: I am looking at purchasing a digital piano for a young student. We do not need it to be mobile.
From pictures, many of the digital pianos in our budget (sub $3000) look like they have the same form-factor as a small upright piano (e.g. the Kawaii CA- range). But on close observation of assembly/unboxing videos, I notice the bottom part below the keyboard isn't actually a box/cabinet, but just a simple board.
<code>               | <-- sheet music stand
              _______________ <-- top of piano
keys -->   --/               |
           \_\______________ |
                            ||
                            ||
                            ||
                            ||
                            || <--back wall is just a piece of wood
                            ||
                            ||
                            ||
                            ||
================================= <--floor
</code>
When purchasing a digital piano, is there some advantage gained if the piano actually has a cabinet space below the keyboard?
Comment: None. It's purely to make it look more like an acoustic piano.
Comment: I'd add that the price difference is more on the actual mechanics parts (how they are, behave and *feel* similar to an actual piano), and less on the electronic components that are responsible of synthesizing the sound (both hardware and software), and, finally, the PA system, which is usually way less considered than the above aspects.
Comment: One minor point: if your young student uses a pedal stool, consider the position of the piano pedals relative to that back wall. If it's not the same as the position of the pedals on an acoustic piano relative to the cabinet, you may have trouble finding a stool that works well with your piano...
Comment: My digital doesn't even have a back wall, just 2 legs on the sides and a bar across the bottom for the pedals.  The sound is nearly as good as the acoustic one in the other room - better in some cases since it never needs tuning.  (And 1 person can transport it without assistance in a regular car, while the acoustic requires at least 4 and a moving van.)
Comment: If that were a desk, the "back wall" would be termed a modesty panel. I leave it to other readers to consider whether that makes players of grand pianos immodest.
Answer: Not really.
Most of the resonance in a digital piano is "faked" - that doesn't mean it can't be good, just that it doesn't rely on actual cabinet resonance to make it sound "real". Some pianos have really complex algorithms to make up this virtual space; others don't. A lot of this is dependant on price point. Three grand ought to be getting into the realms of 'pretty darn good'.
Having said that - there are many degrees of realism provided by different models & manufacturers and ultimately you need to go play them & hear them to see which you prefer.
Comment: _Most of the resonance in a digital piano is "faked"_? This confuses me--what resonance is meant here, and what parts of that are fake and what real?
Comment: MDF doesn't have much useful resonance when it comes to piano bodies. These instruments are all dependant on getting the piano sound pretty much directly from a set of samples played over speakers, so all the "real" piano resonance must either be sampled or calculated on the fly. The more you pay, the better it's calculated.
Comment: Agreed. I think your answer would be much clearer if it started with "no, all the resonance in a digital piano is fake".
Answer: I'm going to post a counter-opinion and say it does matter. Not because that fake structure makes any difference to the sound of the piano - it doesn't. But...

We do not need it to be mobile

I'm not sure that's necessarily a good call. If your young student is going to grow up and might want to take the piano with them, a digital piano with all the fake woodwork is not very practical to move around. And if your reason for buying a digital piano instead of a real one is to save space in your house, it's way more convenient if you can fold the piano down and stash it under a bed if you've got guests.
A stage piano without all the fake woodwork bolted to it, and with a good collapsible stand and collapsible piano stool, is likely to be a better call generally in both those cases. By all means leave it assembled and ready to play, most of the time. But it gives you options which the "cabinet" one doesn't.
For a young student, a stage piano also has a massive advantage ergonomically. The piano and stool can be lowered to their height - they don't have to perch on a too-high stool with their feet dangling in mid-air. Don't underestimate the impact of simple physical discomfort on how much someone wants to practise.
And lastly of course, with a stage piano you're only paying for the actual instrument. With digital pianos in cabinets, a significant chunk of your money is paying for the cabinet. If you're looking for a musical instrument and not a piece of furniture, spend the money on the musical instrument.
Comment: By sacrificing the large non-portable case, you are also sacrificing sound quality. A stage piano is designed to sound its best over a PA, not in a living room. Second point, a stage piano set up in a living room will attract the "when are you going to put that away?" mother-questioning. It's not furniture that suits the environment, it's a piece of work machinery best out of sight.
Comment: Adequate doesn't seem to be what the OP is looking for. The OP wants a piece of furniture & stated it didn't need to be mobile. I'd be pretty sure the OP's pupil's mother will want a piece of furniture too. Add a combo to the stage piano & X-frame combination… & see how long it stays in the living room.
Comment: @Tetsujin You actually made me laugh imagining that situation.
Comment: @Clockwork - as a result of which [partner rather than mother at my age] I have a portable in the workroom & a 'piece of furniture' in the living room ;)
Comment: @Tetsujin As for the second point, that was where I intentionally asked whether they wanted an instrument or furniture. TBH though, if their mother sees an instrument as just clutter then the kid likely has bigger obstacles to learning than just the choice of instrument. My own house is full of guitars and other instruments though, so perhaps I'm not your typical parent. :)
Comment: The only practical advantages I can see to having a cabinet vs a stage digital piano, are the lower chance of having a relative "accidentally" knocking over the piano, and the fact that the pedals are fixed so they will always be at the same position as on an acoustic.
Comment: @Tetsujin Many stage pianos have adequate speakers built in. For those that don't, a suitable practise amp is the size of two shoeboxes, and those are substantially better than the speakers in any digital piano.
Answer: If the space is just a void, then as per Tetsujin's answer, there's unlikely to be a major benefit to it. However, some pianos with  the same form-factor as a small upright piano will have speaker systems with some drivers in the body of the instrument below the keyboard - Pictured below as just one example is the Roland LX708. It's possible that you may like the sound of some of the instruments with this kind of speaker configuration.
Comment: The space being "just a void" doesn't really imply that it's unlikely to matter.
Comment: Some of these "acoustic upright" alike actually have speakers integrated to a soundboard too, if I remember.
Comment: @leftaroundabout maybe we need another answer explaining why..?
Answer: I have a Kawai CE-200, it doesn't have a cabinet but it sounds and plays amazingly. It's very stable and I've never had a problem with it!


How to create custom loading spinner in Ionic 3

Question: I have an app which has been built with Ionic 1. I upgraded this app to Ionic 3. While doing this, I am having a trouble. It is that I can't create the same spinner as Ionic 1 in Ionic 3.
Here is a video with the spinner in Ionic 1.
Loading spinner video for Ionic 1
Also, here is a video  which I created in Ionic 3.
Look forward to receiving a good solution.
Answer: You can use this code to create custom loader.

presentLoadingCustom() {
  let loading = this.loadingCtrl.create({
    spinner: 'hide',
    content: `

`,
    duration: 5000
  });

  loading.onDidDismiss(() => {
    console.log('Dismissed loading');
  });

  loading.present();
}

after that create a animated gif for loading purpose. For now, you can use this site to download the gif loader gif. Place this gif to the assets folder and link the gif in the .custom-spinner-box class.
Comment: Hello Ajoy,
Thank you for your help. Can you help me for one more thing?
Comment: Yes, what's that?
Comment: I want to solve this problem. https://stackoverflow.com/questions/52628778/how-to-use-custom-svg-as-refreshingspinner-in-ion-refresher-ionic-3/52653847?noredirect=1#comment92264272_52653847
Comment: you want a loader.. while scrolling and loading new data?
Comment: I want a custom refreshingIcon for ion-refresher.
Answer: While you are want a custom refresher, for this you need some modification in the code. Here it is -
Add this in app.scss file

  [ng-reflect-name="customcircles"]{
    background-image: url('https://loading.io/spinners/cutiefox/lg.cutie-fox-spinner.gif');
    background-size: cover;
  }
 <ion-refresher (ionRefresh)="doRefresh($event)">
    <ion-refresher-content
    refreshingSpinner="customcircles">
    </ion-refresher-content>
  </ion-refresher>

 and Add this in .HTML file.
I hope you got it.
Comment: thank you so much. i will try it. and let you know about the result.
Comment: i tried it and it works. but you don't have any idea to keep the pullingIcon?
Comment: If you want to use icon.. then you can go with ionic default icons or made it your own loader icon with HTML SCSS.


How do you use the hash_ptr() function from

Question: According to hash.h, hash_ptr() looks like this:
static inline unsigned long hash_ptr(const void *ptr, unsigned int bits)

I'm just a little confused on what the parameters do. Obviously, the void *ptr points to the data to be hashed. My question is that how does the function know where the data ends? If it was assuming cstrings (ie. looking for the NULL byte), then why make it a void pointer?
This is where the next parameter comes in: does the int bits parameter tell you how long the data goes on for? I thought that the int bits parameter specifies the return value, for example, if my hash array has only 63 slots, then I may specify 5 as the number of bits. I'm just a little confused as to how this functions works and there aren't any man pages or anything...
Edit:
OK since hash_ptr only hashes the pointer address, how would one go about hashing an array of bytes, or a c string within the kernel programming space?
Answer: It doesn't hash the value at the pointer; it hashes the pointer itself, in a platform- (size-) independant way.
Mine looks like this:
static inline unsigned long hash_ptr(const void *ptr, unsigned int bits)
{
    return hash_long((unsigned long)ptr, bits);
}

Comment: It hashes the pointer itself? I mean I have the body part in mine too, but I really don't know what `hash_long()` does either. So if I want to hash an array of bytes, how would I go about doing that? And also, what's the `unsigned int bits` for?
Comment: @RuolinFan: `hash_long`, as the name implies, just hashes a `long`. `bits` does what you guessed it did: it determines the number of bits in the output. Since the result before slicing of `hash_long` *is* a `long`, it might seem unnecessary, but the first `bits` bits aren't necessarily evenly distributed enough to be considered a proper hash.
Comment: Oh ok, thanks. Do you have any suggestions as to how to hash an array of bytes then?
Comment: @stakSmashr: Try [one of these](https://programmers.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed) (but read the answer, of course).
Comment: I see, so I guess I'll have to implement my own, no built in functions. Thanks


Using intellisense and Vsvim

Question: I recently installed vsvim and was wondering how to do a few things.
When intellisense pops in and gives you a few options,
Is there a way to choose them without using the arrow keys?
Kind of breaks the vim flow.
Thanks in advance.
Answer: If you use ALT, intellisense doesn't turn transparent!
Go to Tools, Options, Keyboard and edit these commands
Edit.LineUp : Alt+p
Edit.LineDown : Alt + n
Comment: I had given up on keeping the Vim flow with intellisense, but this was a really neat trick! Thanks for sharing.
Answer: In general no.  Once Intellisense is displayed VsVim essentially backs off and lets Visual Studio key handling take over.  Intellisense is one of the more important features of Visual Studio and VsVim attempts to integrate into that experience instead of taking it over.
But it's an area that's open to improvement if there is a good design around it.  Is there a particular way you would like to interact with it?
Comment: C-n to go down the list, C-p to go up basically. I kind of got it to work changing Vs bindings but the intellisense popup dissapears (becomes transparent) when I press Ctrl.
Answer: You can bind the keys in visual studio for intellisense like in Jason Mirks answer, but there are problems with various other popups, especially if you have things like resharper.   Quite a lot of these plugins don't respect the VS keybindings in their popups.  A good way to work around this is with an autohotkey script ( which does Caps Lock as the ESC key as well)
<code>SetTitleMatchMode, RegEx
#IfWinActive .*Microsoft Visual Studio|ReSharper
<!k::Send {Up}
<!j::Send {Down}
Capslock::Esc
#IfWinActive
</code>

Using intellisense and Vsvim


Vector equation of a line (or something)

Question: So there's something in my linear algebra textbook that I just can't understand.
Here goes: (keep in mind that the textbook is not in English so my translation may not be the best):
The line l can be determined by a point P that's on that line and a vector n that's orthogonal to the line.
Let's pick the point O as the center of a coordinate system. Then, the point X is on the line l if and only if PX is orthogonal to n, meaning:
$PX \cdot n = 0$
Using the position vectors of the points P and X we get:
$r \cdot n = p$
where p is a constant and $r = OX$
And here's the illustration that follows (redrawn by me, but it's exactly like in the textbook):

Things I don't get here:
What the hell is p (the small one)?
How is this even an equation of a line?
There is also a part about parametric representation of the line which, oddly, comes before this part in the textbook, and that one I understood. But this I just can't get through my head, it seems as if there is a lack of information or something.
Anyhow I've googled this a lot and couldn't find anything like it. In the textbook this is called 'vector equation of a line' whereas when I google that I get the parametric equation of a line which is a different thing I suppose.
I would gladly skip this but the problem is that it is used as a step in a proof for the distance between a point and a line. So if anyone could explain me what's going on here, that would be nice.
Thanks in advance.
Comment: Consider [Vector addition](https://en.wikipedia.org/wiki/Euclidean_vector#Addition_and_subtraction) and consider the triangle $OPX$ : we have that vector $PX$ is the difference of vectors $OP$ and $OX$.
Comment: Thus $PX \cdot n= (OP-X) \cdot n=0$. An thus : $OP \cdot n= X \cdot n$.
Answer: This is indeed the equation of a line.
$$r\cdot n=p$$
(note the little dot $\cdot$ is a dot product, a product of vectors that gives a scalar)
in coordinates, let $r=(x,y),n=(n_x,n_y)$, this equation is
$$x n_x+y n_y=p$$
If the normal vector to the line, $n$, has length 1, $|n|=\sqrt{n_x^2+n_y^2}=1$, then $p$ represents the distance from the line to the origin. We can see this by considering the point $Q$ on the line which is closest to the origin (it would be close to $P$ in your figure). For this point, $OQ$ is parallel to $n$, so $OP \cdot n=|OP||n|=|OP|=p$.
Comment: Okay so I  haven't redrawn the figure exactly as it is, but I've taken a quick look at the original one now. It seems that OP is NOT parallel to n. It just turned out to look so in my figure by accident, sorry about that.
Comment: My guess is that P is supposed to be an arbitrary point on the line.
Comment: There is some point, say $Q$, on the line such that $OQ$ is parallel to $n$, and this is the closest point to the origin. It seemed like $P$ was intended to be this point. I'll edit.


need you advice how to build this website has the same views

Question:
The search info module is the common feature for all the airport1,2,3
with this requirement do I need to set up multisite for ariport1,2 ,3?
Could any give some tips what should I use (drupal) to develop this kind of website.
Thanks
Answer: With the info that you've given,
I'll suggest you to use quicktabs module for tabs and views exposed filter for search.
If you're asking what CMS to use I guess that is inappropriate to ask here.
Comment: How about we implement airport1,2,and ariport3


Integral of an exponential multiplied by a line

Question: Experimental Physics graduate student looking for some assistance with an integral. I'm attempting to find a new solution to a beam propagation integral, and it has me evaluating an integral of a form
$$\int_{b}^{\infty} e^{Ar^{2} + Br}r dr$$
Where $b$ is some real valued scalar.
I've found solutions to equations that are very similar , but are for lower limits of integration that go to infinity. Apologies if this is something very basic and I'm missing something, I haven't done real calculus in a couple of years.
Comment: Complete the square in the exponent and notice that its derivative is "almost" $r$. (Okay, after fixing it, you might still need special functions such as Erf to express your final answer)
Answer: Hint
Start completing the square
$$Ar^2+Br=A\left(r+\frac{B}{2 A}\right)^2-\frac {B^2}{4A }$$ Now, let $r=x-\frac{B}{2 A}$ to make
$$\int r\,e^{Ar^2+Br}\,dr=\frac{e^{-\frac {B^2}{4A } } }{2A }\int (B-2Ax)\,e^{A x^2}\,dx$$ which are very simple.
Solve for $x$, go back to $r$ and use bounds (hoping that $A<0$)


VAR Impulse response with a dummy variable

Question: I have a var model :
y=dummy + other variables
where dummy =1 if the firm is having a negative return on stock and 0 otherwise. Y is the return on stock. Is it appropriate to use the VAR model to study the impact of the dummy on y and other variables in this context?
Answer: You might want to look at threshold VARs (TVAR). Lo and Zivot, 2001 They allow you to estimate seperate Var equations depending on the state of the economy/firm. source
Here is variable c the threshold variable. In principle, an infinite number of boundaries are conceivable. In practice, however, it is usually no more than 2-3, which is also due to the limited number of degrees of freedom.
There are also TVEC Models, which can deal with cointegrated data.
Comment: This is a little brief as an answer by our standards; you could improve it by saying a little about what TVARs are and why you think they would help in this case.


$(A+B)^2\|B^{-1}\|-A$ is positive definite?

Question: If $A$ and $B$ are positive definite matrix, $(A+B)^2\|B^{-1}\|-A$ is also positive definite?
When $A$ and $B$ are real numbers, then $(A+B)^2\|B^{-1}\|-A$ is obviously positive definite. When $A$ and $B$ are matrix, I can use orthonormal matrix $O$ to transform $B$ an diagonal matrix $\operatorname{diag}\{\lambda_1,\cdots,\lambda_n\}$. Then I can transform the original problem to  $(A+B)^2-\lambda_n A$ is it also positive definite?
Comment: The norm of a matrix. i.e.  the largest singular value of A.
Comment: I think the $\| \cdot \|$ should be denoting any norm
Comment: Does $\| \cdot \|$ denote the determinant here?
Comment: If you are acquainted with operator theory, you can apply continuous functional calculus, then there is immediate.
Answer:
If $A\ge 0$, $B\ge 0$ then $$A^{\frac12} B A^{\frac12} \ge 0$$

PS: this property does not require $A$ and $B$ to commute.

Since $B \ge \lambda_n I$ then $A+B \ge \lambda_n I$ then,
\begin{align}
\left(A+B\right)^2 &= \left(A+B\right)^{\frac12}\left(A+B\right)\left(A+B\right)^{\frac12}\\
&\ge \lambda_n \left(A+B\right)^{\frac12}\left(A+B\right)^{\frac12}\\
&= \lambda_n (A+B)\ge \lambda_n A
\end{align}

By the way, using the same idea you can prove the stronger result:
$$(A+B)^2\left\|\left(A+B\right)^{-1}\right\| - (A+B) \ge 0$$


Text inside  tag is not wrapping

Question: response.text contains the html source of google.com.
How come the html source is not wrapping inside my
?
Current solution creates a huge side scrolling area, I'd like any text that goes beyond the window width to wrap.
I want to avoid use of innerHTML in this solution.
var pre = document.createElement('pre');
pre.appendChild(document.createTextNode(response.text));
document.getElementById("body").appendChild(pre);

css for
 ..
pre {
    outline: 1px solid #ccc;
    padding: 5px;
    margin: 5px;
    white-space: -moz-pre-wrap;
}

Comment: http://stackoverflow.com/questions/248011/how-do-i-wrap-text-in-a-pre-tag
Answer: Use like below,
pre {
    outline: 1px solid #ccc;
    padding: 5px;
    margin: 5px;
    white-space: pre-wrap;
    white-space: -moz-pre-wrap;
    white-space: -pre-wrap;
    white-space: -o-pre-wrap;
    word-wrap: break-word;
}

Text inside  tag is not wrapping


How to pass token to custom Login Module for Glassfish 3

Question: I have a web application deployed on Glassfish 3.0  which uses my custom web application realm to authenticate user based on username and password (using servlet 3.0 request.login()). I have written a custom login module which extends "AppservPasswordLoginModule" and does the authentication .The password is stored encrypted in db.
Now i have a scenario where i need to generate token based urls for users which they can use to login to the site (without any username or password ) . The token can be valid for certain time or valid for one time login .
How do i pass my token to my login module or How do i handle such login flow and authenticate a user based on token ?
Answer: Since no one answered .
I tried having server auth module , however it didn't work for passing information to realm . I ended up parsing the token in one of my servlet, retrieving the credentials over there and then passing those to my login module (and realm) . In my realm i did the validation against DB values to authenticate the user .


Searching a value using for-loop and

Question: The question is this:
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Input: [4,1,2,1,2]
Output: 4
my code is:
public static int singleNumber(int[] nums) {
     int answer = 0;
        for (int i =0; i<nums.length-1; i++) {
            for(int j = i+1; j<nums.length; j++) {
                if(nums[i] != nums[j]) {
                 answer = nums[i];      //this should be where I am wrong.
                }
            }
        }
        return answer;
    }

I know that the output was 4 and then now it will be changed to 1.
I'm trying to figure out how not to change the found value once found.
Comment: I'd use a HashBag (or implement such a bag using a Map) and then return the set of all objects which have a count of one.
Comment: Are there any limitations on the input numbers? (example: they are between 1-10 etc)
Comment: I have not learned Hash yet.. so this is the way I'm doing right now.
And there is no limit on input numbers (sorry about not being specific for it earlier)
Comment: is this an algorithm problem or an implementation problem?
Answer: The logic is wrong - your inner loop finds every number that isn't the only number in the array.
I'd maintain a Set to track the numbers I've encountered. The first time you encounter a number, you add it to the Set. The second time you encounter it, you remove it from the Set. Once you're done going over the array, you'd have a Set with a single element, which is your answer:
public static int singleNumber(int[] nums) {
    Set<Integer> unique = new HashSet<>();
    for (int num : nums) {
        // add returns true if num is indeed new to unique
        if (!unique.add(num)) {
            unique.remove(num);
        }
    }

    return unique.iterator().next();
}

Answer: For this problem, i would to bitwise XOR of the numbers. The numbers that are equal will cancel one another and only single integer will be the final value.
public static int singleNumber(int[] nums) {
     int answer = 0;
        for (int i =0; i<nums.length; i++) {
           answer = answer ^ nums[i];
        }
        return answer;
 }

Answer: below changes to your method will give you the expected answer
public static int singleNumber(int[] nums) {

    int temp = 0;
    int answer = 0;

    for (int i = 0; i < nums.length; i++) {
        boolean flag = true;
        temp = nums[i];
        for (int j = 0; j < nums.length; j++) {
            if (temp == nums[j]) {
                if (i != j) {// if a match found then the loop will terminate
                    flag = false;
                    break;
                }
            }

        }
        if (flag == true) {
            answer = temp;
        }
    }
    return answer;
}

Answer: Here is another solution using Collectors.groupingBy from Java 8 :
public static int singleNumber(int[] nums) {
    return Arrays.stream(nums).boxed()
            .collect(Collectors.groupingBy(a -> a, Collectors.counting()))
            .entrySet().stream().filter(e -> e.getValue() == 1).findFirst().get().getKey();
}

the idea is :

group by number of occurence
then find the one which is repeated just one time

Note I assume that your array contain at least one element, else you can check the length before you search then throw an exception like this :
public static int singleNumber(int[] nums) throws IllegalArgumentException{
    if(nums.length == 0){
        throw new IllegalArgumentException("empty array");
    }
    return Arrays.stream(nums).boxed()
            .collect(Collectors.groupingBy(a -> a, Collectors.counting()))
            .entrySet().stream().filter(e -> e.getValue() == 1).findFirst().get().getKey();
}

More deeper, If you want to avoid the situation where there are multiple number repeated just one time you can use :
public static int singleNumber(int[] nums) throws IllegalArgumentException {
    if (nums.length == 0) {
        throw new IllegalArgumentException("empty array");
    }
    Map<Integer, Long> grouping = Arrays.stream(nums).boxed()
            .collect(Collectors.groupingBy(a -> a, Collectors.counting()));
    if (grouping.values().stream().filter(c -> c == 1).count() > 1) {
        throw new IllegalArgumentException("more than one element is repeated one time");
    }

    return grouping.entrySet().stream()
            .filter(e -> e.getValue() == 1).findFirst().get().getKey();
}

Answer: Here is a solution which uses ArrayList.indexOf and ArrayList.lastIndexOf. If they are same, you have your answer.
public static int singleNumber(int[] nums) {
    int answer = 0;
    //ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(nums));
    ArrayList al = new ArrayList();
    for (int i =0; i < nums.length; i++) {
        al.add(nums[i]);
    }

    for (int i =0; i < nums.length; i++) {
        int test = nums[i];
        if(al.indexOf(test) == al.lastIndexOf(test)){
            answer = nums[i];
        }
    }
    return answer;
 }

Answer: Try this:
    int[] nums = new int[] {4,2,1,2,1};
         int answer = 0;
            for (int i =0; i<nums.length-1; i++) {
                int times = 0;
                int target = nums[i];
                for(int j : nums) {
                    if(j == target) {
                        times++;
                        if(times == 2) {
                            break;
                        }
                    }
                }
                if(times == 1) {
                    answer = target;
                    break;
                }
            }
            System.out.println(answer);

you have to go into every number and count how meny are in the array if there is only 1 you instantly stop the loop end set the answer
Comment: Instead of checking if `times==1`, why not just break as soon as `times` becomes 2, then you can set `answer = target` if it managed to reach the end without exiting

Searching a value using for-loop and


Slow query with large dataset in Drupal views — better to process in SQL or PHP?

Question:
I have a large userbase in my Drupal 7 site, which contains a People finder. Because there are hundreds of thousands of users and we want to display the People finder in a random order on page load, I presume it's transferring the whole userbase into memory at some point, in order to paginate (using the Pager built-in to Views2 Views3).
I've noticed that the query has no limit against it (presumably because of pagination) and that it joins against users_roles twice (because I need to include all users of rôle A but not also of rôles B, C and D). I've added comments against the WHERE clause to explain each line's purpose and how it's come out of my View.
SELECT
    users.uid AS uid,
    'user' AS field_data_field_user_image_user_entity_type,
    'user' AS field_data_field_user_field1_user_entity_type,         -- custom field
    'user' AS field_data_field_user_field2_display_user_entity_type, -- custom field
    'user' AS field_data_field_user_field3_user_entity_type,         -- custom field
    'user' AS field_data_field_user_field4_user_entity_type,         -- custom field
    RAND() AS random_field
FROM
    users users
    INNER JOIN users_roles users_roles ON users.uid = users_roles.uid
    LEFT JOIN users_roles users_roles2
        ON users.uid = users_roles2.uid
        AND (users_roles2.rid = :views_join_condition_0
          OR users_roles2.rid = :views_join_condition_1
          OR users_roles2.rid = :views_join_condition_2)
WHERE
    (( (users.status <> :db_condition_placeholder_3)      -- Active users only
    AND (users_roles.rid = :db_condition_placeholder_4)   -- Must be in rôle A
    AND (users_roles2.rid IS NULL)                        -- Must not be in rôles B, C, D
    AND (users.uid != :users_uid OR users.uid IS NULL) )) -- Must not be current user
ORDER BY random_field ASC

So I have two questions:

Is there some way in Views2 that I can somehow get this query to be limited (potentially by sorting on something else that pretends to be random)? I assume that transferring fewer result rows per query is likely to help with the memory overhead?
If I want to mess with that WHERE clause, am I better off using a PHP Filter Criterion to do the roles messing (which would presumably remove the double-join) or should I move the whole damn query into either a custom module[a] or a mySQL view[b]?

a: I assume that would attach to the relevant hook with a function modulename_views_pre_execute(&$view).
b: I assume a mySQL view would still need to do both joins, one inner join and one left join, so I can't quite see how this would help.
For what it's worth, I know that my site will have a relatively short lifetime and, during that, the rôles won't change, so I am happy to hardcode the rôle IDs, if that will give me a performance benefit.
Comment: Are you using Views 2 or Views 3?  I didn't think that Views 2 ever had a version for Drupal 7.
Comment: I realise I could also avoid the `join`ing by doing something like `SELECT * FROM users WHERE uid IN (SELECT DISTINCT uid FROM users_roles WHERE rid = 5) AND uid NOT IN (SELECT DISTINCT uid FROM users_roles WHERE rid IN (6,8,9))`. But that's still going to pull back all the rows; would that be better than the double-join?
Comment: @MPD You're right, it's Views3; I thought it was Views2. My bad.
Comment: Just a thought: Do you use indexes on the joined ids and the fileds in the where clause? Using indexes in the right way can speed up your queries dramatically.
Comment: @BetaRide Yes, the `JOIN`ed IDs are both indexed (indeed there are only two columns in `users_roles` and they are a clustered primary key together). I'll double-check `users.status` but I'm pretty sure it's the ugly join that's the problem here.
Comment: I have also opened a question on DBA.SE with the mySQL specific part of this question: [mySQL query optimisation — multiple joins or select … where not in (select distinct…)?](http://dba.stackexchange.com/q/15794/7755)
Answer: Are you 100% positive that the view isn't paging in SQL?  When you turn on Query Logging in the Devel module, what do you see for your view?  I just double checked a site and am seeing (formatting is mine):
SELECT node.nid AS nid, RAND() AS random_field
FROM node node
WHERE (( (node.status = :db_condition_placeholder_0)
  AND (node.type IN (:db_condition_placeholder_1)) ))
ORDER BY random_field ASC LIMIT 10 OFFSET 0

when I am on paged view with a random sort.
I have seen the double JOIN thing before.  Often, you can adjust the view parameters and get the query to run the way you want.
Optimizing these queries is really use dependent.  I am pretty sure that both of the columns you are joining on are both indexed already.  You can try to replicate the query manually, and then EXPLAIN it, and manually improve it.  You can then implement this query in a custom block, but then you lose out on other things that Views give you.  Caching the view results, may help, too.
Keeping everything in MySQL is likely best (it is meant for heavy lifting like this), but only profiling will give the true results for your system.  Just make sure you have MySQL, Apache, and PHP tuned so that you aren't chasing the wrong problem.
Comment: We've been looking at the site performance for a couple of days and it's definitely this query (and one other) on this page that are our main problem. The query in the question is copy/pasted from the output of Devel and then reformatted for legibility. I did look at `EXPLAIN`, but part of my problem is that I can't see how to improve it dramatically from the status quo — I've also [listed the mySQL bit on DBA.SE](http://dba.stackexchange.com/q/15794/7755) and the subquery option looks slightly better, but I don't quite follow how to use the output of `EXPLAIN`, tbh.
Comment: I just find it very odd that the ORDER/LIMIT is missing from yours, but I see it when I use "Global: Random" as a views sort.
Comment: Yeah, I'm sure that `ORDER`/`LIMIT` would help here dramatically. But I can't see how to enable them. Is there something I need to do to the Query settings, perhaps? (I have all three checkboxes unticked, so SQL rewriting is _not_ disabled)
Comment: No clue.  Another module may be rewriting the query.  I would try remaking the view w/ a new name and see what happens.  Other than that, you can take @Countzero's advice and try to alter the query.
Comment: Remaking the View with a new name doesn't make any difference, it would seem. It takes pretty much the same time to run each query and there's still no `LIMIT`/`OFFSET` appearing in the SQL query being shown me by [Devel](http://drupal.org/project/devel).
Answer: You can easily modify the query in hook_views_query_alter, add your LIMIT clause and see what you get.
But MPD advice above is very sound and is widely applicable.


How does one know the implied difference in time signatures?

Question: I have always been told that the difference between 3/4 time and 6/8 time is that 6/8 time has two beats per measure and 3/4 has three beats per measure.
However, other than people telling me this, I see no way that someone would be able to know this just by looking at the music.
For instance the following example seems like it should be played identically:

Is there any way to distinguish the difference between how 6/8 and 3/4 music should be played other than just somehow knowing that one usually has the emphasis on two beats in a measure and one on three beats per measure?
Similarly, with  cut time and common time, is there anything identifiable (without listening to the song) that differentiates the time signatures?

Looking at the above, I would think:

2/2 means two half notes per measure, which can be divided to four quarter notes per measure, which could be reduced to eight eighth notes per measure, so we are still in 4/4.

I personally count quarter notes and eighth notes and 16th notes as (1 2 3 4) vs (1 + 2 + 3 + 4) vs (1 e + a 2 e + a...) respectively but unless someone were hearing me count while playing, how would he or she know that the piece is in 2/2 vs 4/4 vs 8/8?
When looking at the second image, I would think one of two things:

The bar line should be after the first four eigth notes (because that would be two beats) or
"Okay, I see 8 eighth notes. I am going to count each measure as "1 + 2 + 3 + 4 +" (because that is how I count eighth notes)"

but this would be wrong because I should only count up to 2 (two beats per measure) so instead I'll count (1 e + a 2 e + a) but this is also wrong because that would imply we are in 8/16 time.
So I do not see a valid way to count this. I would very appreciative if someone could explain this to me.
Comment: @Startec I know that it's not obvious, but you can't expect a completely immediate and absolutely unambiguous system from something that evolved through centuries and is mostly based on "common practice". Also, having an obvious or completely instinctive system doesn't imply that that system is the better. The rule, anyway, is not that complex, and there's not a lot of ambiguity: if the numerator can be divided by 3 there are numerator/3 beats (unless it's exactly 3), if it's even then there are numerator/2 beats, otherwise it's an additive rhythm (5/4, 7/8, etc).
Comment: @musicamente there are typically said to be 4 beats in a 4/4 measure, not 2.  If you're going to answer the question you should post an answer, not comments, so incorrect information can be corrected.
Comment: "Is there any way to distinguish the difference between how 6/8 and 3/4 music should be played other than just somehow knowing that one usually has the emphasis on two beats in a measure and one on three beats per measure?" Well, the point is that you *should* know that, as that is exactly what 6/8 and 3/4 refers to. While I admit that 6/8 might be less instinctive, the convention says that meters that can be divided by 3 should consider beats as groups of 3 subdivisions (which are indicated by the denominator). Besides that, when you count, you don't count quarters or eights, you count beats.
Comment: @musicamante "meters that can be divided by 3 should consider beats as groups of 3 subdivisions (which are indicated by the denominator)". I assume you mean numerator as neither 4 nor 8 are divisible by 3.  To me it seems odd when looking at 6/8 that someone would think: "yes, six, of course that means two".
Comment: This answer may also be helpful: [How should I understand time signature?](https://music.stackexchange.com/questions/28484/how-should-i-understand-time-signature/106794#106794)
Comment: As well, this question: [How simple time and compound time signatures change the rhythm](https://music.stackexchange.com/questions/103586/how-simple-time-and-compound-time-signatures-change-the-rhythm/103587#103587).
Comment: There are many things in music that one has to learn the hard way and remember. This is merely one example. Your last sentence in the second to last paragraph is incorrect. 1 e + a is not only for 16th notes.
Comment: There have been attempts to try to simplify certain aspects or study alternative notation systems, but all of them just failed for a reason or the other: common usage was the most common, but the complexity required for *writing* something so complex as music is probably even more important. Music is a very complex language, and like any other language there aspects that are not always obvious, and there are *rules* (and exceptions) which might not follow strict logic, as their existence is exactly what allows that complexity.
Comment: @phoog you're right, while reviewing the comment I made some changes and then forgot to correct that part, but my comment wasn't intended to be an answer, as I wanted to point out those "obviousness" aspects, which doesn't directly answer the question.
Answer: The answer to both questions is the same: it's understanding meter
Meter is the pattern of accented and unaccented beats.  Both music and poetry have meter: RO-ses are RED has a pattern of one accent followed by two unaccented syllables; i WISH i KNEW how LONG has a pattern of one unaccented and one accented syllable.
In poetry there are lots of different meters with fancy names like dactyl (roses are red) or iamb (i WISH).  In music they're simpler: we group beats into twos or threes, and we divide beats into twos or threes.
Grouping beats in twos is duple meter.  2/4 and 6/8 are both duple meters, with two beats per measure.  Grouping  in threes is triple meter, as in 3/4 or 3/8.
The way we divide the beats can be into two equal parts (one quarter note beat = two eighth note divisions), which is called simple meter, or we can divide beats into three equal parts (one dotted quarter beat = three eighth note divisions) which is called compound meter.
Meters are represented by the time signature.  If the top number is a multiple of three (6,9,12,15) it's compound meter, and the bottom number is the type of note that represents a beat division, or one third of a beat.  If the top number is anything else it's showing you a simple meter - then the bottom number tells you the type of note that represents one full beat.
Groups of beats can be combined: 4/4 is two groups of two beats.  The first group's accent is slightly stronger than the second group's accent: ONE two THREE four, and because 4 is not a multiple of 3 each beat will naturally divide into two parts.  We call this quadruple simple meter.
The time signatures you ask about are 3/4, which is a triple simple meter and 6/8, which is a duple compound meter.  3/4 has three beats per group with beat one accented, and each beat naturally divides into two parts; 6/8 has two beats per group, with each beat divided into three equal parts.
4/4 is quadruple simple, with two groups of two beats.  2/2 is duple simple, with one group of two beats.  The difference here is subtle: the second group in 4/4 is accented a little less than the first group's.  In 2/2 all accents are the same.
You didn't ask, but we can also combine unlike groups: 5/4 time is one group of three beats and one group of two.  Meters like this are called asymmetric.  For an asymmetric meter we don't know if it's grouped 3+2 or 2+3 - either will have the same time signature.  For those we usually use the beams within divisions to tell the musician how the notes are grouped.
Answer: 'I see no way that someone would be able to know this just by looking at the music.' The simple answer here is to look at the time signature.
3/4 and 6/8 look very different from each other! That's because they are! And playing your example would come out differently. The 6/8 tune would have an empahsis on the first note (like most music) but also on the fourth. That's implicit in the time signature. Very different from 3/4.
A point of order: there should not be six separate quavers written, they should be beamed. 3/4 can be as three pairs, 6/8 as two groups of three. That's how they get played - that's how they get written.
How do we know? By learning it. We learn that to, too and two, or you, yew and ewe all sound the same, but very quickly as kids realise that they are actually all different. So we learn in music that there are different ways in which to designate timings.
Comment: Funnily enough, 3/4 time is beamed as all 6 8th notes beamed together more often than 6/8 time is.
Answer:
However, other than people telling me this, I see no way that someone
would be able to know this just by looking at the music.

Of course! Musical notation is written language used by people in a culture. Did you learn to read text by looking at text, without actually speaking any language or meeting any speakers of any language?
It's about culture, conventions, tradition and language. Not natural science, laboratory measurements and math. If you ask yourself "should I write this in 3/4 or 6/8" and cannot tell the difference, then you lack cultural, not theoretical knowledge.
You can't calculate it, you need to get involved in the culture to understand it.
(Paraphrasing my own answer from here How does the bottom number of the time signature affect counting?)
Comment: Sorry but you don't know Japanese if you don't know what people do there, how they live, what happens in situations. Language is not text, syntax and grammar, it is part of culture, a way of how people live. To get this knowledge, you should live there as part of the society, or at least somehow get lots of descriptions about the culture.
Comment: @Dekkadeci I tried to say that written language, including musical notation, is not mathematics that could be logically deduced. It's a tool used by people in a particular culture, for communicating and working with music-related ideas. It wasn't designed. For every detail in notation there's an "etymology", and to explain the features "why is this notation like so", the explanations will inevitably refer to cultural history. How people in some country at some point used to do something. Though it's not only historical - classical music has preserved a lot of those practices and traditions.
Comment: Ironically enough, I learned all my Japanese from reading it (and reading about it - e.g. what their "l/r" sound *really* sounds like, the rumour that the Japanese will cut out ending vowel sounds given the opportunity). I also learned pretty much all my German from reading it (I have heard non-native German speakers say "Ich liebe dich" and "Ich ben ein Berliner", but not "Sehr rasch" or "Die forelle").
Comment: @Dekkadeci that's fundamentally notionism, not "knowing a language". Transcription of a language is only the result of its practice, always comes *after* (a *lot* after) it, and is only a slightly precise (but often inaccurate or insufficient) attempt to transform a medium into a completely different one. An image can be a good representation of reality, but it can also be misleading, it's incomplete and open to interpretations. Knowing a language just by reading it and about it is insufficient, just like you can't learn music just by listening it (or reading it) if you don't play it.
Answer:
Let's assume your problem is not the differentiation of the six  8th-notes in a 3/4 time compared with the six  8th-notes in 6/8. (3/4 you count 1 a 2 a 3 a, 6/8 time you will count 1 2 3 4 5 6.

I think the question is how to differentiate a  6/8 from to bars of3/4, isn't it?

The problem is actually that it isn't always clear even to the composer to decide which time signature is more adequate, as we have mostly motifs of a double bar or even 2 double bar phrases which would fit as well in a 6/8 time signature (I think I have also seen a Waltz notated in 6/8).
So if the 1st point is clear to you I wouldn't bother to much about the difference of these 2 kind of notations. It is not so much crucial as you may think.
Concerning the 1st point:
I have written a long time ago a piano piece (polyrhythmic): l.h. 3/4, r.h. 6/8 and vice versa. In this case it is possible to mix the 2 rhythms resp. times, but the notation would be complicated. The solution is to notate the piece in 3/4 and add accents on 1 and 4 of the 6 8th notes. Note the Cello part in this arr. for Strings:

or the first bars for Piano: (unfortunately the software doesn't allow 2 different times in the upper and lower staff ...)
Comment: Bars 7&8 should be 6/8, right?
Comment: Maybe it's my ear buds and how it's hard to hear bass with them, but I was completely convinced by listening to it that the first page of your strings version of your piece is *entirely in 6/8 time*. Your piano sheet music and its accents didn't get transferred to strings well at all.
Comment: I agree this is a bad "performance". Too much hall! But even if I listen to the piano version, or play it myself with the accents ... my auditive perception is similar to a visual tilt figure: sometimes I hear it 6/8 sometimes 3/4 ... the same version. ;)
Comment: @ Tim: yes - the l.h. only, (r.h. 3/4)
Answer: It's not an implied difference.
It's specified in the time signature.
You have to learn to read time signature. They are not self explanatory.
The tip off is in the top number. Basically, if it's a multiple of 3, and the bottom number is 8, then it's compound time and the beat is subdivided by three.
The problem with your example of eighth notes in 6/8 and 3/4 is that it is written poorly. Good  rhythmic notation will put beam on eighth notes grouped into beats, 3 notes in 6/8 and 2 notes in 3/4.
Again, you need to read a book of music fundamentals about rhythm and time signatures to get the full picture.
Answer: One difference with compound time signatures is that instead of counting beats, we count pulses.
In compound time signatures there are three pulses to a beat, whereas in simple time signature there are only two.
This is represented as a clear visual difference by the beaming; we connect the flags of the pulses into beams, and we do this according to the beats.

The compound duple time signature 6/8 says that there are 6 pulses to the bar and they are eighth notes (quavers). (As already mentioned) we count the pulses in compound time signatures.
Below is two bars in 6/8, the first showing beats, and the second showing pulses:
X: 1
K: C clef=perc stafflines=1
M: 6/8
L: 1/8
V:1 stem=up
B3 B3| BBB BBB||
w: ~1 ~4 ~1 ~2 ~3 ~4 ~5 ~6

The simple triple time signature 3/4 says: there are 3 beats to the bar, and they are quarter notes (crotchets). We count beats in simple time signatures.
Below is two bars in 3/4, again the first bar shows beats, and the second shows pulses:
X: 1
K: C clef=perc stafflines=1
M: 3/4
L: 1/4
V:1 stem=up
B B B| B/2B/2 B/2B/2 B/2B/2||
w: ~1 ~2 ~3 ~1 ~& ~2 ~& ~3 ~&

Comment: Two down votes; no explanation.  Please leave a comment if you know what is wrong with my answer.
Answer: Y0u are quite correct that, shown just this, we have no way of telling whether 2+2+2 (3/4) or 3+3 (6/8) grouping is required.

And that is why we have time signatures!
The 3/4 time signature tells us it's 2+2+2.

And, to make it even clearer, we'd normally write it like this.

For 3+3 we use a 6/8 time signature and this grouping.


Does "made cut, did not finish" also count as a "missed cut?"

Question: At the 2014 Farmers Insurance Open, Tiger Woods finished with a result of "MDF" or "made cut, did not finish." This is the first time in his career he has had a MDF.
A missed cut (MC) is the result when a player finishes outside the top-70 and ties after the second round.
When more than 78 players advance into the third round, a MDF is the result when a player finishes outside the top-70 and ties after the third round. This is also known as a "54-hole cut."(1)
Does "made cut, did not finish" also count as a "missed cut?"
Answer: It does not.
Notice from the leaderboard of this year's Farmers Insurance Open that Tiger Woods finished "MDF." Next, take a look at the consecutive cuts made statistics, where Tiger is currently T4 with 24 consecutive cuts made.


Varnish - how to check JWT signature using digest Vmod?

Question: I have a DockerFile based on Varnish 7.0 alpine, I have a custom vcl file to handle JWT authentication. We pass the JWT as a Bearer in the header.
I am based on this example: https://feryn.eu/blog/validating-json-web-tokens-in-varnish/
    set req.http.tmpPayload = regsub(req.http.x-token,"[^\.]+\.([^\.]+)\.[^\.]+$","\1");
    set req.http.tmpHeader = regsub(req.http.x-token,"([^\.]+)\.[^\.]+\.[^\.]+","\1");
    set req.http.tmpRequestSig = regsub(req.http.x-token,"^[^\.]+\.[^\.]+\.([^\.]+)$","\1");

    set req.http.tmpCorrectSig = digest.base64url_nopad_hex(digest.hmac_sha256(std.fileread("/jwt/privateKey.pem"), req.http.tmpHeader + "." + req.http.tmpPayload));

    std.log("req sign " + req.http.tmpRequestSig);
    std.log("calc sign " + req.http.tmpCorrectSig);

    if(req.http.tmpRequestSig != req.http.tmpCorrectSig) {
        std.log("invalid signature match");
        return(synth(403, "Invalid JWT signature"));
    }

My problem is that tmpCorrectSig is empty, I don't know if I can load from a file, since my file contains new lines and other caracteres ?
For information, this Vmod is doing what I want: https://code.uplex.de/uplex-varnish/libvmod-crypto, but I can't install it on my Arm M1 pro architecture, I spent so much time trying...
Can I achieve what I want?
Comment: I saw your comment on my Youtube video and I'll provide the necessary information on how to validate HS256 & RS256 JWTs. It might take me a day or 2, stay tuned.
Comment: @ThijsFeryn Would be amazing, I try everything to install the libmod-crypto on the alpine version, but impossible, the libmod-digest works. But I don't think you can do it with RS256 algo? we don't use H256 but RS256 for the JWT
Comment: @VincentDecaux should one of the answers flagged as correct?
Comment: @NilsGoroll no, still doesn't work for me on M1 architecture. For now, I just a different Dockerfile to disable Varnish for M1 users for local development.
Comment: in https://gitlab.com/uplex/varnish/libvmod-crypto/-/issues/3 you said it worked. So if it does not, let's reopen the issue.
Answer: Firstly, the configure error was caused by a missing -dev package, see the gitlab issue (the reference is in a comment, but I think it should be more prominent).
The main issue in the original question is that digest.hmac_sha256() can not be used to verify RS256 signatures. A JWT RS256 signature is a SHA256 hash of the subject encrypted with an RSA private key, which can then be verified by decrypting with the RSA public key and checking the signature. This is what crypto.verifier(sha256, ...) does.
In this regard, Thijs' previous answer is already correct.
Yet the code which is circulating and has been referenced here it nothing I would endorse. Among other issues, a fundamental problem is that regular expressions are used to (pretend to) parse JSON, which is simply not correct.
I use a better implementation for long, but just did not get around to publishing it. So now is the time, I guess.
I have just added VCL snippets from production code for JWT parsing and validation.
The example is used like so with the jwt directory in vcl_path:
include "jwt/jwt.vcl";
include "jwt/rsa_keys.vcl";

sub vcl_recv {
    jwt.set(YOUR_JWT);  # replace YOUR_JWT with an actual variable/header/function

    call recv_jwt_validate;

    # do things with jwt_payload.extract(".scope")
}

Here, the scope claim contains the data that we are actually interested in for further processing, if you want to use other claims, just rename .scope or add another jwt_payload.expect(CLAIM, ...) and then use jwt_payload.extract(CLAIM).
This example uses some vmods, which we developed and maintain in particular with JWT in mind, though not exclusively:

crypto (use gitlab mirror for issues) for RS signatures (mostly RS256)
frozen (use gitlab mirror for issues) for JSON parsing

Additionally, we use

re2 (use gitlab mirror for issues) to efficiently split the JWT into the three parts (header, payload, signature)
and taskvar from objvar (gitlab) for proper variables.

One could do without these two vmods (re2 could be replaced by the re vmod or even regsub and taskvar with headers), but they make the code more efficient and cleaner.
blobdigest (gitlab) is not contained in the example, but can be used to validate HS signtures (e.g. HS256).
Comment: First, thanks really for sharing your jwt implementation, we saw the JWT Vmod on the varnish plus , but prices are ridiculous. Then, as I wrote on Gitlab issue, your lib works perfectly, just not on Alpine distri, or with 6.6 Varnish too. And I need to up the version for the Arm architecture. Will take a big look to your code, thanks again
Answer: I have a valid solution that leverages the libvmod-crypto. The VCL supports both HS256 and RS256.
These are the commands I used to generated the certificates:
cd /etc/varnish
ssh-keygen -t rsa -b 4096 -m PEM -f jwtRS256.key
openssl rsa -in jwtRS256.key -pubout -outform PEM -out jwtRS256.key.pub

I use https://jwt.io/ to generate a token and paste in the values from my certificates to encrypt the signature.
The VCL code
This is the VCL code that will extract the JWT from the token cookie:
vcl 4.1;

import blob;
import digest;
import crypto;
import std;

sub vcl_init {
    new v = crypto.verifier(sha256,std.fileread("/etc/varnish/jwtRS256.key.pub"));
}

sub vcl_recv {
    call jwt;
}

sub jwt {
    if(req.http.cookie ~ "^([^;]+;[ ]*)*token=[^\.]+\.[^\.]+\.[^\.]+([ ]*;[^;]+)*$") {
        set req.http.x-token = ";" + req.http.Cookie;
        set req.http.x-token = regsuball(req.http.x-token, "; +", ";");
        set req.http.x-token = regsuball(req.http.x-token, ";(token)=","; \1=");
        set req.http.x-token = regsuball(req.http.x-token, ";[^ ][^;]*", "");
        set req.http.x-token = regsuball(req.http.x-token, "^[; ]+|[; ]+$", "");

        set req.http.tmpHeader = regsub(req.http.x-token,"token=([^\.]+)\.[^\.]+\.[^\.]+","\1");
        set req.http.tmpTyp = regsub(digest.base64url_decode(req.http.tmpHeader),{"^.*?"typ"\s*:\s*"(\w+)".*?$"},"\1");
        set req.http.tmpAlg = regsub(digest.base64url_decode(req.http.tmpHeader),{"^.*?"alg"\s*:\s*"(\w+)".*?$"},"\1");

        if(req.http.tmpTyp != "JWT") {
            return(synth(400, "Token is not a JWT: " + req.http.tmpHeader));
        }
        if(req.http.tmpAlg != "HS256" && req.http.tmpAlg != "RS256") {
            return(synth(400, "Token does not use a HS256 or RS256 algorithm"));
        }

        set req.http.tmpPayload = regsub(req.http.x-token,"token=[^\.]+\.([^\.]+)\.[^\.]+$","\1");
        set req.http.tmpRequestSig = regsub(req.http.x-token,"^[^\.]+\.[^\.]+\.([^\.]+)$","\1");

        if(req.http.tempAlg == "HS256") {
            set req.http.tmpCorrectSig = digest.base64url_nopad_hex(digest.hmac_sha256("SlowWebSitesSuck",req.http.tmpHeader + "." + req.http.tmpPayload));
            if(req.http.tmpRequestSig != req.http.tmpCorrectSig) {
                return(synth(403, "Invalid HS256 JWT signature"));
            }
        } else {
            if (! v.update(req.http.tmpHeader + "." + req.http.tmpPayload)) {
                return (synth(500, "vmod_crypto error"));
            }

            if (! v.valid(blob.decode(decoding=BASE64URLNOPAD, encoded=req.http.tmpRequestSig))) {
                return(synth(403, "Invalid RS256 JWT signature"));
            }
        }

        set req.http.tmpPayload = digest.base64url_decode(req.http.tmpPayload);
        set req.http.X-Login = regsub(req.http.tmpPayload,{"^.*?"login"\s*:\s*(\w+).*?$"},"\1");
        set req.http.X-Username = regsub(req.http.tmpPayload,{"^.*?"sub"\s*:\s*"(\w+)".*?$"},"\1");

        unset req.http.tmpHeader;
        unset req.http.tmpTyp;
        unset req.http.tmpAlg;
        unset req.http.tmpPayload;
        unset req.http.tmpRequestSig;
        unset req.http.tmpCorrectSig;
        unset req.http.tmpPayload;
    }
}

Installing libvmod-crypto
libvmod-crypto is required to use RS256, which is not supported by libvmod-digest.
Unfortunately I'm getting an error when running the ./configure script:
./configure: line 12829: syntax error: unexpected newline (expecting ")")

I'll talk to the maintainer of the VMOD and see if we can figure out someway to fix this. If this is an urgent matter, I suggest you use a non-Alpine Docker container for the time being.
Comment: Looking, thanks for your time, I have the same error on the libvmod-crypto... I talked to the maintainer of the Vmod already (see gitlab issues), I tried with a non alpine (6.6), see here: https://github.com/opositatest/varnish-jwt/issues/13
Comment: Thread of the configure error: https://gitlab.com/uplex/varnish/libvmod-crypto/-/issues/3
Comment: By the way, missreaded your answer, but using libvmod-crypto, it works (see https://github.com/opositatest/varnish-jwt/blob/main/varnish/default.vcl, we copied this code), just I can't update the crypto vmod to varnish 6.6 or 7


What is the need of assumptions in linear regression?

Question: In linear regression, we make the following assumptions

The mean of the response,
$E(Y_i)$, at each set of values of the predictors, $(x_{1i}, x_{2i},…)$, is a Linear function of the predictors.
The errors, $ε_i$, are Independent.
The errors, $ε_i$, at each set of values of the predictors, $(x_{1i}, x_{2i},…)$, are Normally distributed.
The errors, $ε_i$, at each set of values of the predictors,  $(x_{1i}, x_{2i},…)$, have Equal variances (denoted $σ2$).
One of the ways we can solve linear regression is through normal equations, which we can write as
$$\theta = (X^TX)^{-1}X^TY$$
From a mathematical standpoint, the above equation only needs $X^TX$ to be invertible. So, why do we need these assumptions? I asked a few colleagues and they mentioned that it is to get good results and normal equations are an algorithm to achieve that. But in that case, how do these assumptions help? How does upholding them help in getting a better model?
Comment: Normal distribution is needed to calculate coefficient confidence intervals using usual formulas. Other formulas of CI calculation (I think it was White) allow non-normal distribution.
Comment: See https://stats.stackexchange.com/q/16381/35989
Comment: @Alexis The independent variables being i.i.d. definitely isn't an assumption (and the dependent variable being iid also isn't an assumption - imagine if we assumed that the response was iid then it would be pointless to do anything beyond estimating the mean).  And the "no omitted variables" isn't really an additional assumption although it's good to avoid omitting variables - the first assumption listed is really what takes care of that.
Comment: @Dason I think my link provides a pretty strong example of "no omitted variables" being requisite for valid interpretation. I also think i.i.d. (conditional on the predictors, yes) is necessary, with random walks providing an excellent example of where non-i.i.d. estimation can fail (ever resorting to estimating only the mean).
Comment: You do not always need those assumptions for the model to work. In neural networks you have linear regressions inside and they minimize rmse just like the formula you provided, but most likely none of the assumptions hold. No normal distribution, no equal variance, no linear function, even the errors can be dependent.
Comment: You are missing the i.i.d. assumption of the dependent and independent variables (indirectly kinda captured by independent errors), and the [no omitted variables assumption](https://stats.stackexchange.com/questions/89547/what-is-the-purpose-of-precision-variables/337129#337129).
Answer: You are correct - you do not need to satisfy these assumptions to fit a least squares line to the points. You need these assumptions to interpret the results. For example, assuming there was no relationship between an input $X_1$ and $Y$, what is the probability of getting a coefficient $\beta_1$ at least as great as what we saw from the regression?
Answer: Try the image of Anscombe's quartet from Wikipedia to get an idea of some of the potential issues with interpreting linear regression when some of those assumptions are clearly false:  most of the basic descriptive statistics are the same in all four (and the individual $x_i$ values are identical in all but the bottom right)
Comment: I made a illustration following Anscombe [showing what violating the no omitted variables assumption can look like](https://stats.stackexchange.com/questions/89547/what-is-the-purpose-of-precision-variables/337129#337129). Still working on an Anscombe-like illustration of a [violation of the i.i.d. assumption](https://stats.stackexchange.com/q/313119).
Answer: You don't need those assumptions to fit a linear model. However, your parameter estimates could be biased or not having the minimum variance. Violating the assumptions will make yourself more difficult in interpreting the regression results, for example, constructing a confidence interval.
Answer: Ok, the answers so far go like this: If we violate the assumptions then bad things can happen. I believe that the interesting direction is: When all assumptions that we need (actually a little different from the ones above) are met, why and how can we be sure that linear regression is the best model?
I think the answer to that question goes like this: If we make the assumptions as in the answer of this question then we can compute the conditional density $p(y_i|x_i)$. From this we can compute $E[Y_i|X_i=x_i]$ (the factorization of the conditional expectation at $x_i$) and see that it is indeed the linear regression function. Then we use this in order to see that this is the best function with respect to the true risk.
Answer: The two key assumptions are

Independence of observations
Mean is not related to the variance

See The discussion in Julian Faraway's book.
If these are both true, OLS is surprisingly resistant to breaches in the other assumptions you have listed.


Somar valores de itens iguais em uma tabela

Question: Estou usando JAVA, JPA, Wildfly e PrimeFaces, mas gostaria mesmo de que alguem me ajudasse em um método java para somar  valores de itens iguais de um dataTable.Exemplo:
Tenho duas colunas PRODUTO  e QUANTIDADE, no item BORRACHA  existem dois valores de 50, gostaria de somar de acordo com o nome do produto, no caso a resposta seria 100, esta resposta eu vou colocar em outra coluna da tabela, mas gostaria de saber como faço pra somar os valores de nomes iguais deste Arraylist.

MINHA VIEW:
                    <h:panelGrid style="margin-top: 10px">

                                <h:outputText value="#{est.produto}" style="width:100%" />

                                    <f:facet name="output">
                                        <h:outputText value="#{est.quantidade}" />
                                    </f:facet>
                                    <f:facet name="input">

                                    </f:facet>

                                <h:outputText value="#{est.setor}" />

                                <h:outputText value="#{est.pontoAtendimento}" />

                                <h:outputText value="#{est.autor}" />

                                <h:outputText value="#{est.data}">
                                    <f:convertDateTime pattern="dd/MM/yyyy HH:mm"
                                        timeZone="GMT-03:00" />
                                </h:outputText>

                    </h:panelGrid>
                </h:panelGrid>
            </h:panelGrid>
        </h:panelGrid>
    </ui:define>
</ui:composition>

MEU MB
package br.com.sicoob.ManagedBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import org.primefaces.context.RequestContext;
import org.primefaces.model.UploadedFile;

import br.com.sicoob.Service.EstoqueService;
import br.com.sicoob.entidades.Estoque;

@ManagedBean(name = "estoqueMB")
@ViewScoped
public class EstoqueMB implements Serializable {

    /**
     * CLASSE DE CONTROLE DE ESTOQUE; Criada por: Pedro Henrique
     */
    private static final long serialVersionUID = 1L;

    @Inject
    private EstoqueService estoqueService;

    private Estoque estoque = new Estoque();

    private List<Estoque> listaEstoque = new ArrayList<Estoque>();
    private boolean edicao = true;

    private int contBobinas, contBomAr, contBoracha, contCaixaPag, contCaixaReceb, contCaneta, contCanetaPon,
            contCarbono, contCera, contClipsG, contClipsM, contClips04, contCola, contColher, contCopo, contElast,
            contEnvCarta, contEnvePardo, contEnvPlast, contExtrator, contFitaAdes, contFitaERC, contGrampeador,
            contGrampo, contLacre, contLapis, contMarTexto, contMataIns, contMolhaDedo, contMovNum, contPapelA4,
            contPastaPapel, contRegua20, contRegua30, contTesoura, contTintaCarimbo;

    @PostConstruct
    public void init() {
        contBobinas = 0;
        contBomAr = 0;
        contBoracha = 0;
        contCaixaPag = 0;
        contCaixaReceb = 0;
        contCaneta = 0;
        contCanetaPon = 0;
        contCarbono = 0;
        contCera = 0;
        contClipsG = 0;
        contClipsM = 0;
        contClips04 = 0;
        contCola = 0;
        contColher = 0;
        contCopo = 0;
        contElast = 0;
        contEnvCarta = 0;
        contEnvePardo = 0;
        contEnvPlast = 0;
        contExtrator = 0;
        contFitaAdes = 0;
        contFitaERC = 0;
        contGrampeador = 0;
        contGrampo = 0;
        contLacre = 0;
        contLapis = 0;
        contMarTexto = 0;
        contMataIns = 0;
        contMolhaDedo = 0;
        contMovNum = 0;
        contPapelA4 = 0;
        contPastaPapel = 0;
        contRegua20 = 0;
        contRegua30 = 0;
        contTesoura = 0;
        contTintaCarimbo = 0;
        listaEstoque = estoqueService.listarTodos();
        contarRegistros();
    }

    /**
     * Método responsavel por adicionar cadastro
     */

    public void salvar() {

        if (validarEstoque(estoque) == false) {
            return;
        }
        estoque.setData(new Date());
        estoque.setAutor(System.getProperty("user.name"));
        estoqueService.salvarEstoque(estoque);
        listaEstoque = estoqueService.listarTodos();
        estoque = new Estoque();
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(FacesMessage.SEVERITY_INFO, "", "cadastro realizado com SUCESSO!"));
    }

    private UploadedFile file;

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public void upload() {
        if (file != null) {
            FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

    /**
     *
     * @return
     */
    public String redirecionar() {
        return "perfil";
    }

    /**
     *
     * @param cadastro
     * @return
     */
    private boolean validarEstoque(Estoque estoque) {

        if (estoque.getProduto() == null || "".equals(estoque.getProduto())) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Favor preencher o campo: PRODUTO"));
            return false;
        }
        if (estoque.getQuantidade() == 0 || "".equals(estoque.getQuantidade())) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Favor preencher o campo: QUANTIDADE"));
            return false;
        }
        if (estoque.getPontoAtendimento() == null || "".equals(estoque.getPontoAtendimento())) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Favor preencher o campo: PA"));
            return false;
        }
        if (estoque.getSetor() == null || "".equals(estoque.getSetor())) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "", "Favor preencher o campo: SETOR"));
            return false;
        }
        return true;
    }

    public void contarRegistros() {
        for (int x = 0; x < listaEstoque.size(); x++) {
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Bobinas 2 vias de caixa-Caixa ( 30uni)")) {

                contBobinas = listaEstoque.get(x).getQuantidade();

            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Bom ar")) {
                contBomAr = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Borracha")) {
                contBoracha = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Caixa Pagamento-Bloco")) {
                contCaixaPag = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Caixa Recebimento-Bloco")) {
                contCaixaReceb = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Caneta-Caixa (50 uni)")) {
                contCaneta = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Caneta ponta fina")) {
                contCanetaPon = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Carbono-Pacote (5 uni)")) {
                contCarbono = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Cera para limpeza")) {
                contCera = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Clips 4/0 -Caixa")) {
                contClips04 = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Clips  Grande")) {
                contClipsG = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Clips Médio")) {
                contClipsM = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Cola Bastão")) {
                contCola = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Colher Descartavel-Pacote (25uni)")) {
                contColher = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Copo Descartável-Pacote (100uni)")) {
                contCopo = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Elástico Dinheiro-Pacote 500g")) {
                contElast = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Envelope Carta - Pacote (100 uni)")) {
                contEnvCarta = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Envelope Pardo A4-Caixa (250 uni)")) {
                contEnvePardo = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Envelpe Plastico  4 furos-Pacote ( 100 uni)")) {
                contEnvPlast = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Extrator de Grampo")) {
                contExtrator = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Fita adesiva (Larga)")) {
                contFitaAdes = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Fita ERC03-Caixa (10 uni)")) {
                contFitaERC = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Grampeador")) {
                contGrampeador = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Grampo-Caixa")) {
                contGrampo += listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Lacre Malote -Pacote (100 uni)")) {
                contLacre = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Lápis-Caixa( 12 uni)")) {
                contLapis = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Marca texto")) {
                contMarTexto = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Mata Insetos")) {
                contMataIns = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Molha Dedo")) {
                contMolhaDedo = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Movimento Numerario -Bloco")) {
                contMovNum = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Papel A4 -Caixa (10 pct)")) {
                contPapelA4 = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Pasta Papel A4")) {
                contPastaPapel = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Régua 20 cm")) {
                contRegua20 = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Régua 30 cm")) {
                contRegua30 = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Tesoura")) {
                contTesoura = listaEstoque.get(x).getQuantidade();
            }
            if (listaEstoque.get(x).getProduto().equalsIgnoreCase("Tinta Carimbo")) {
                contTintaCarimbo = listaEstoque.get(x).getQuantidade();
            }
        }
    }

    /**
     * Método responsavel por adicionar cadastro
     */
    public void atualizar() {
        estoqueService.salvarEstoque(estoque);
        estoque = new Estoque();
        edicao = false;
    }

    /**
     *
     * @param u
     */
    public void remover(Estoque c) {
        estoqueService.remover(c);
        listaEstoque = estoqueService.listarTodos();
        c = new Estoque();
    }

    public void reset() {
        RequestContext.getCurrentInstance().reset("form:panel");
    }

    /**
     *
     * @param u
     */
    public void editar(Estoque e) {
        this.estoque = e;
        edicao = true;
    }

    public List<Estoque> getListaEstoque() {
        return listaEstoque;
    }

    public void setListaEstoque(List<Estoque> listaEstoque) {
        this.listaEstoque = listaEstoque;
    }

    public Estoque getEstoque() {
        return estoque;
    }

    public void setEstoque(Estoque estoque) {
        this.estoque = estoque;
    }

    public boolean isEdicao() {
        return edicao;
    }

    public void setEdicao(boolean edicao) {
        this.edicao = edicao;
    }

    public int getContBobinas() {
        return contBobinas;
    }

    public void setContBobinas(int contBobinas) {
        this.contBobinas = contBobinas;
    }

    public int getContBomAr() {
        return contBomAr;
    }

    public void setContBomAr(int contBomAr) {
        this.contBomAr = contBomAr;
    }

    public int getContBoracha() {
        return contBoracha;
    }

    public void setContBoracha(int contBoracha) {
        this.contBoracha = contBoracha;
    }

    public int getContCaixaPag() {
        return contCaixaPag;
    }

    public void setContCaixaPag(int contCaixaPag) {
        this.contCaixaPag = contCaixaPag;
    }

    public int getContCaixaReceb() {
        return contCaixaReceb;
    }

    public void setContCaixaReceb(int contCaixaReceb) {
        this.contCaixaReceb = contCaixaReceb;
    }

    public int getContCaneta() {
        return contCaneta;
    }

    public void setContCaneta(int contCaneta) {
        this.contCaneta = contCaneta;
    }

    public int getContCanetaPon() {
        return contCanetaPon;
    }

    public void setContCanetaPon(int contCanetaPon) {
        this.contCanetaPon = contCanetaPon;
    }

    public int getContCarbono() {
        return contCarbono;
    }

    public void setContCarbono(int contCarbono) {
        this.contCarbono = contCarbono;
    }

    public int getContCera() {
        return contCera;
    }

    public void setContCera(int contCera) {
        this.contCera = contCera;
    }

    public int getContClipsG() {
        return contClipsG;
    }

    public void setContClipsG(int contClipsG) {
        this.contClipsG = contClipsG;
    }

    public int getContClipsM() {
        return contClipsM;
    }

    public void setContClipsM(int contClipsM) {
        this.contClipsM = contClipsM;
    }

    public int getContClips04() {
        return contClips04;
    }

    public void setContClips04(int contClips04) {
        this.contClips04 = contClips04;
    }

    public int getContCola() {
        return contCola;
    }

    public void setContCola(int contCola) {
        this.contCola = contCola;
    }

    public int getContColher() {
        return contColher;
    }

    public void setContColher(int contColher) {
        this.contColher = contColher;
    }

    public int getContCopo() {
        return contCopo;
    }

    public void setContCopo(int contCopo) {
        this.contCopo = contCopo;
    }

    public int getContElast() {
        return contElast;
    }

    public void setContElast(int contElast) {
        this.contElast = contElast;
    }

    public int getContEnvCarta() {
        return contEnvCarta;
    }

    public void setContEnvCarta(int contEnvCarta) {
        this.contEnvCarta = contEnvCarta;
    }

    public int getContEnvePardo() {
        return contEnvePardo;
    }

    public void setContEnvePardo(int contEnvePardo) {
        this.contEnvePardo = contEnvePardo;
    }

    public int getContEnvPlast() {
        return contEnvPlast;
    }

    public void setContEnvPlast(int contEnvPlast) {
        this.contEnvPlast = contEnvPlast;
    }

    public int getContExtrator() {
        return contExtrator;
    }

    public void setContExtrator(int contExtrator) {
        this.contExtrator = contExtrator;
    }

    public int getContFitaAdes() {
        return contFitaAdes;
    }

    public void setContFitaAdes(int contFitaAdes) {
        this.contFitaAdes = contFitaAdes;
    }

    public int getContFitaERC() {
        return contFitaERC;
    }

    public void setContFitaERC(int contFitaERC) {
        this.contFitaERC = contFitaERC;
    }

    public int getContGrampeador() {
        return contGrampeador;
    }

    public void setContGrampeador(int contGrampeador) {
        this.contGrampeador = contGrampeador;
    }

    public int getContGrampo() {
        return contGrampo;
    }

    public void setContGrampo(int contGrampo) {
        this.contGrampo = contGrampo;
    }

    public int getContLacre() {
        return contLacre;
    }

    public void setContLacre(int contLacre) {
        this.contLacre = contLacre;
    }

    public int getContLapis() {
        return contLapis;
    }

    public void setContLapis(int contLapis) {
        this.contLapis = contLapis;
    }

    public int getContMarTexto() {
        return contMarTexto;
    }

    public void setContMarTexto(int contMarTexto) {
        this.contMarTexto = contMarTexto;
    }

    public int getContMataIns() {
        return contMataIns;
    }

    public void setContMataIns(int contMataIns) {
        this.contMataIns = contMataIns;
    }

    public int getContMolhaDedo() {
        return contMolhaDedo;
    }

    public void setContMolhaDedo(int contMolhaDedo) {
        this.contMolhaDedo = contMolhaDedo;
    }

    public int getContMovNum() {
        return contMovNum;
    }

    public void setContMovNum(int contMovNum) {
        this.contMovNum = contMovNum;
    }

    public int getContPapelA4() {
        return contPapelA4;
    }

    public void setContPapelA4(int contPapelA4) {
        this.contPapelA4 = contPapelA4;
    }

    public int getContPastaPapel() {
        return contPastaPapel;
    }

    public void setContPastaPapel(int contPastaPapel) {
        this.contPastaPapel = contPastaPapel;
    }

    public int getContRegua20() {
        return contRegua20;
    }

    public void setContRegua20(int contRegua20) {
        this.contRegua20 = contRegua20;
    }

    public int getContRegua30() {
        return contRegua30;
    }

    public void setContRegua30(int contRegua30) {
        this.contRegua30 = contRegua30;
    }

    public int getContTesoura() {
        return contTesoura;
    }

    public void setContTesoura(int contTesoura) {
        this.contTesoura = contTesoura;
    }

    public int getContTintaCarimbo() {
        return contTintaCarimbo;
    }

    public void setContTintaCarimbo(int contTintaCarimbo) {
        this.contTintaCarimbo = contTintaCarimbo;
    }

}

MEU DAO:
package br.com.sicoob.DAO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.sicoob.Service.EstoqueService;
import br.com.sicoob.entidades.Estoque;

@Stateless
public class EstoqueDAO implements Serializable, EstoqueService {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    @PersistenceContext
    private EntityManager em;

    /**
     *
     * @param u
     */
    public void salvar(Estoque e) {
        if (e.getId() == 0) {
            em.persist(e);
        } else {
            em.merge(e);
        }
    }

    /**
     *
     * @param u
     */
    public void remover(Estoque e) {
        e = em.merge(e);
        em.remove(e);
    }

    /**
     *
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<Estoque> listarTodos() {
        List<Estoque> estoque = new ArrayList<Estoque>();
        Query q = em.createQuery("select obj from Estoque obj");
        estoque = q.getResultList();
        return estoque;
    }

    /**
     *
     * @param id
     * @return
     */
    public Estoque buscarPorId(Long id) {
        return em.find(Estoque.class, id);
    }

    /**
     *
     * @param u
     * @return
     */
    public Estoque carregarEntidade(Estoque e) {
        return em.merge(e);
    }

    @Override
    public void salvarEstoque(Estoque estoque) {
        if (estoque.getProduto() == null) {
            em.persist(estoque);
        } else {
            em.merge(estoque);
        }
    }

    @Override
    public void salvarCadastro(Estoque estoque) {

    }

    @Override
    public List<Estoque> listarDescricao() {
        // TODO Auto-generated method stub
        return null;
    }
}

Comment: Poste algum código de como você está preenchendo a tabela, forneça mais informações para que possamos te ajudar.
Comment: editado com MB,View e DAO
Answer: Acho que a melhor forma é fazer isso na query, usando sum e group by:
select sum(quantidade), minhaTabela.produto
from MinhaTabela minhaTabela
group by minhaTabela.produto

Essa query deve te retornar uma List contendo dois elementos, a soma como um long e a descrição.
De uma olhada nas funções na documentação.