Computing distance on a sphere

Question: Let's say I want to compute the distance between two far points on Earth, say Toronto and Brazil. I can do this by getting in my car, setting my odometer to zero and then driving to Brazil. For me, the distance between these two points is the number my odometer spits out.
An observer outside Earth would be using the metric $dx^2+dy^2+dz^2$ and would calculate a different distance between these two points. From what I know, the induced metric on the sphere is the round metric which is $r^2(d\theta^2+\sin\theta^2 d\phi^2)$. I'm not sure what this is in terms of $x,y,z$.
If the person outside the Earth were to put Toronto and Brazil into the round metric, would they get the same answer the odometer gave? Is the round metric suppose to be the metric a person on the sphere would use?
Suppose this person in Toronto takes herself as the origin of the $xy$-plane she lives on. Her metric would be $dx^2+dy^2$. If the person observing the Earth also put their origin at Toronto, would the round metric coming from $dx^2+dy^2+dz^2$ coincide with this i.e.  $g_{\text{round}}=dx^2+dy^2$? The person on the Earth is calculating the length of this part of a great circle using the metric $dx^2+dy^2$ not the round metric? Are the round metric and the surface's metric the same objects; is there a difference between spitting out the same lengths and being the same thing?
Comment: I don't understand your second question. By the straight line to Brazil do you mean the straight line in $\mathbb{R}^3$, or the great circle along the surface of the earth?
Comment: Sorry, I meant the straight line between Toronto and Brazil from the perspective of a person on Earth's surface. So this would be an arc of a great circle to a person outside the Earth. I'm asking if the round metric, which can only be calculated if you know of the third dimension, is the same as the metric a person on the $2$-dimensional surface is using, who doesn't know of the third dimension. Also if their origins need to coincide for this to happen.
Answer: The round metric on the sphere is the metric induced from the Euclidean metric $dx^2 + dy^2 + dz^2$ on $\mathbb{R}^3$ (in fancy language, the round metric is the pullback of the Euclidean metric under the inclusion of the sphere into $\mathbb{R}^3$). This means in particular that if the person "outside the earth" decided to measure the length of a path on the surface of the earth (using the Euclidean metric), he would measure exactly the same length as you would (using the round metric). But the person outside the earth is allowed to travel along a larger class of paths than you are (e.g., the path through the earth) because his ambient manifold (all of $\mathbb{R}^3$) is larger than yours (the sphere/surface of the earth, which is a submanifold of $\mathbb{R}^3$). That's why in this case he can find a shorter path than you can.
To address your second question, the person on the earth does not live on an $xy$-plane, she lives on a sphere. I think this is an important point. The plane and the sphere are not only geometrically distinct (i.e., not isometric), but they are also topologically distinct (not homeomorphic). You seem to be suggesting that there are coordinates $(x,y)$ on the sphere in which the metric is $dx^2 + dy^2$, but this is incorrect. First of all, note that such coordinates could only possibly be defined locally, on some proper subset of the sphere, since the sphere is not homeomorphic to any subset of the plane. More fundamentally, even if you choose local coordinates $(x,y)$ on a subset of the sphere, the metric could never be the flat metric (in those or any other coordinates), since the sphere is not flat, which can be seen, for example, by computing that its Gauss curvature is nonzero.
Now, you could also, after choosing local coordinates $(x,y)$ on the sphere (in some neighborhood of Toronto), consider the flat metric $dx^2 + dy^2$. This would be a valid Riemannian metric in the coordinate neighborhood, but it would be completely irrelevant to this situation, because it would have nothing to do with the actual ("round") geometry of the earth, which, again, is induced from the flat metric on the ambient $\mathbb{R}^3$. It would be possible to choose the coordinates in such a way such that the distance between Toronto and Brazil is the same in either metric, but chances are that the distances between most points not on that great circle would be different in the two metrics.
Let me say a word about distances. It's important not to conflate the Riemannian metric, call it $g$, on a manifold $M$ and the metric, call it $d$, that makes $M$ a metric space. $g$ computes the inner product between pairs of tangent vectors, whereas $d$ computes the distance between pairs of points. Of course, $g$ detemines $d$, since the distance $d(p, q)$ is the infimum of lengths of curves joining $p$ and $q$ (and the length of a curve is computed by integrating the $g$-length of its velocity vector). The question of whether $d$ determines $g$ (which I think is related to your last question) is a harder one, and I'm not sure what can be said.
Comment: Thanks very much for your answer! In regards to your second paragraph, I am assuming the person on the surface does not know of the third dimension. They think the Earth is flat and so they are using the standard metric $d=dx^2+dy^2$. Here I identify the points Toronto and Brazil as vectors. This person has no idea of a $z$-coordinate. But to them this metric $d$ is a real thing they use. I'm starting to think this is a silly question, but the round metric isn't the same as $d$, right? It just spits out the same numbers.
Comment: I think you are confused. If she thinks the earth is flat, then she's completely wrong! The earth is not flat, it is curved. Even if someone on the earth has no knowledge of the third dimension, her world is still fundamentally curved. One could detect this, for example, by measuring the angles in geodesic triangles, which would sum to more than 180 degrees because a sphere is positively curved:
http://en.wikipedia.org/wiki/Differential_geometry_of_surfaces#Geodesic_triangles
Comment: To elaborate on my comment, every surface (including the sphere) is locally diffeomorphic to $\mathbb{R}^2$. Because of this, and because the curvature of the earth is relatively small (but definitely nonzero), a person on the surface of the earth might guess (incorrectly) that she lives on a flat plane. But being _diffeomorphic_ to $\mathbb{R}^2$ (smoothly topologically equivalent) is _not_ the same as being _isometric_ to $\mathbb{R}^2$ (geometrically equivalent).
Comment: Haha, I am very confused! I'm going to post another question now about the Theorema, which is where my confusion in this question is coming from. Thank you for your help.

Computing distance on a sphere


É possível, no MySQL, atualizar as informações de uma coluna em diversas linhas duplicadas, deixando apenas uma linha com o valor original?

Question: Por exemplo, tenho diversas linhas em uma tabela (que puxei de um XML de produtos do google, então o mesmo produto é apresentado diversas vezes, cada um para um tamanho da roupa/produto) que são referenciadas para o mesmo item, apenas com diferença de tamanho do produto, quantidade e código SKU. Nesta tabela, tenho uma coluna que determina se o produto é apresentado ou não no sistema (0 ou 1). O que eu gostaria de fazer era rodar uma query que verificaria os itens duplicados, alterando o parametro dessa coluna que disponibiliza a visulização, escondendo todos os itens duplicados, deixando apenas um com o valor original.
Eu consegui atingir algo similar com a query abaixo:
DELETE a FROM sis_produto_xml a LEFT JOIN ( SELECT MIN(ID) id, sku, nome FROM sis_produto_xml GROUP BY nome) b ON a.id = b.id AND a.sku = b.sku AND a.nome = b.nome WHERE b.id IS NULL;

O problema dessa query é que ela deleta o produto, e depois o XML baixa o mesmo produto novamente, ou seja, é um trabalho que não tem fim. Escondendo o produto através do parametro da tabela (com um update ao invés do delete) isso não iria ocorrer.
Valeu galera.
Answer: Percebi que a query do post deleta itens únicos, mas consegui resolver o problema da seguinte maneira:
DELETE table FROM table INNER JOIN ( SELECT MAX(id) AS lastid, column FROM table WHERE column IN ( SELECT column FROM table GROUP BY nome HAVING COUNT(*) > 1 ) GROUP BY column ) duplic ON duplic.column = table.column WHERE table.column2 < duplic.lastid;

Valeu comunidade!
Answer: É só você alterar a query que você havia citado para UPDATE:
UPDATE a
   SET apresentado = 0
  FROM sis_produto_xml a
       LEFT JOIN (SELECT MIN(ID) id,
                         sku,
                         nome
                    FROM sis_produto_xml
                   GROUP BY nome) b ON a.id = b.id
                                   AND a.sku = b.sku
                                   AND a.nome = b.nome
 WHERE b.id IS NULL;

Comment: Depois de penar fazendo alguns testes entendi essa alteração e consegui fazer funcionar.
Comment: @thierryrene deu certo usando essa resposta então?
Comment: + / -, acabei precisando fazer outra query, porque esta deletava resultados únicos, postei a resposta! Obrigado @Sorack !


How should a self-taught musician study and reach his goals?

Question: I am a self-taught pianist. I have been playing piano for about 7 years. I can play all my major and melodic minor scales comfortably. I excelled in AP Music Theory in high school.
My goals are to improve my piano technique, eventually compose music, and learn my music theory well. I eventually want to compose for a wide variety of genres, including some subgenres of EDM such as future funk, synth-pop, and the experimental/glitch genre.
I am not going to school to study music, but on the side of my schoolwork I have been meeting with music professors to get some guidance. I have been looking at counterpoint textbooks and learned the five species of basic counterpoint. I have also read the first 200 or so pages of Mark Levine's Jazz Theory, up to the part about sequences.
I am not sure how to proceed next - I don't know if going further into the jazz textbook will give me the skills I want, since I myself do not consider jazz my favorite genre, but found the seventh chords and smooth voice leading interesting.
I don't know what to do at this point. I don't know what textbooks to study next, or what techniques to practice. I can always practice my scales on the piano, but I want to grow my knowledge that will help me compose better music as well.
I could compose some more music, but I'm not sure that would expand my skills - only reinforce my existing ones. I could improvise, but I don't know exactly how to improvise in a way that will help me improve, as I have no formal knowledge of improvisation. I could also pick up another textbook on harmony.
However, I don't really know what to do next. Does anyone have any suggestions?
Answer: I'm sure I don't have a definitive answer to offer, but I'll share what I've learned being in a similar position -- self-taught pianist and composer for about 14 years. I haven't read as much as you, but I've probably improvised more.
One of the most valuable things has been alternation between book learning and finger learning. My method for the longest time was to just play and play and try new things and see what I liked and investigate anything that was interesting. Often, songs would develop through improperly or accidentally struck keys! For example, the bridge in one song started from meaning to hit D, A/C#, F#m/C#, which was part of the standard melody line, but for that last chord played C#sus4 instead, transitioned into F#m/C#, and from there back to D. Sounded nice so I developed it further!
But now and then I will take the time to learn theory. Sometimes experimentation leads to original composition, but very often it just chips away (slowly!) at well-established and basic principles. Since it's possible to digest a principle in an hour with a good chapter instead of over weeks of experimentation, I now try to get these "infusions" of knowledge and start playing on a structure. Similarly, I will play other people's songs, sometimes spending a day or a week working through new songs or sheet music, or more if I'm finding it helpful, and notice what I like best in it and intentionally improvise on the base that they've given (whether it's their song structure, their rhythmic pattern, their chord progression, their melody line, their harmony style). This provides fresh "mutations" for my basic compositional DNA.
In the same way, many of my new songs actually start the same as an existing one; they'll have the same intro, for example (say, 4 or 8 bars of a rhythm or a chord progression). This provides a runway for the creative process. Then I just go in a different direction, again experimenting, whether it's physically or mentally: Physically, shall I try this higher octave to see what it has to offer? Mentally, shall I try a new minor chord where my instincts tell me to use the familiar major one? Physically, shall I do a chromatic trill on this note to open up the scale? Mentally, shall I modify the harmony to work in snatches of counterpoint?
Now, sometimes I do work intentionally instead of experimentally. I sit at my score without my hands on the keyboard and think, "What is needed for this climax is a modulation up a major third. I'll need to use two measures to transition that, which I can give to the flute and accent the key 'pivot' notes with the horn," or something. But because I'm not as well-read, I do more discovering the other way. You might find that your scale is balanced more to this sort of thing.
Also, I record a lot of content to make sure I don't lose ideas, but I do at least 3 or 4 times as much playing without recording. This playing is other people's music or favourite songs of mine I know aren't quite developed. Like a toddler babbling, this is the exploration, the dwelling and steeping in the piece until something new emerges. As the cliché goes, the voice of inspiration comes when you least expect it, but I find it rarely comes if I'm not listening. How I "listen" is often just to spend that patient time with a piece.
Happy composing!

Appendix on improvising
In reply to your comment: I'm not sure the process is easily transferable, and I wouldn't say that I improve "in order to learn something new". What I meant above is that I learn either by intentionally reading or by accidentally playing, usually after getting to the point of boredom with whatever framework I started with. Instead, I improvise to get ideas for new pieces. :)
That said, for what it's worth, here's the sort of thing I do.

Pick a key — whether this is done by fingers finding familiar keys or intentionally choosing one doesn't matter.
Start with a few chords or a more or less random melody line.
Whichever was chosen first, add the other.
Develop! Repeat one element while testing the other. (One thing I really enjoyed about Miles Davis' ensembles was how they would sometimes have a constant rhythm while changing the melody, and sometimes change the rhythm while having a constant melody!)
Make your way gradually towards a melody that you would call the central idea, and a consistent chord progression for it. Play around in the space between. New chords, new melody fragments within the key. Make your way back to that central idea every now and then, taking longer ways round till you return to it, which immediately lends it new thematic interest as the context around it changes. Try to build an arc so that you remain emotionally interested.

This process is generally enough to get an idea that I can then begin to more intentionally elaborate.
Here's a snippet of the opening of an old improvisation of mine (nowadays I hope my timing is better... my recording quality certainly is!). I don't know if it'll be your cup of tea, and it may not actually be representative of the above since it's 3 minutes of what became 26 and doesn't even begin to touch on the main theme, but for me it represents a good balance between a repeating structural element and playing minor variations to get somewhere new and find something I like. Towards the end of this clip you can hear where I found it got samey and decided to throw in a non-diatonic change in the hopes of slightly changing the game.
2015 improv bite
Comment: How do you improvise exactly? When I improvise I sometimes feel like it's either too unstructured so I don't learn anything, or I follow the rules too much and so I'm not learning anything new. What I have done before is take ii V I progressions and just improvise over them, or take a melody and try to develop it and transform it in some ways - sequence, extensions, etc. Are there other useful tips you would suggest for improvising?
Comment: @reincarnationofstackexchange Added some more to the end of the answer that might be useful. It's probably similar to what you do. Feel free to ask any more follow-up questions — if you think I might have anything more you don't already know :p
Answer: Study the map
Continue with things like the counterpoint and the jazz theory books. It's always good to devote a small amount of your time to learning new things even if they don't apply directly to your goal because you'll never know if you do want to go in that direction unless you wander down that path for a while.
If nothing else it's a matter of understanding the topology of what you don't yet know. Maybe skim the table of contents of books that you think might be over your head or not directly applicable to you. If you see a topic you've never heard of read a little bit about it. Don't waste a lot time but at least read enough that you'll know it exists and whether you might want to revisit it at some point later in your studies. In other words, it's good to be a little bit familiar with the entire map even if you're not going to those places anytime soon.
Pick a path
But spend the majority of your practice time directly chasing your goals. Decide what that goal is and start working toward it. Find the shortest distance between where you are now and your goal by figuring out which topics and techniques you'll need to know. Then take each topic and break it down into smaller and smaller chunks until it's something you can work on right now. As for how to find those topics, let the music itself inform you.
Learn the rules of the road
For instance you said you'd like to compose "some subgenres of EDM such as future funk, synth-pop, and the experimental/glitch genre". So start breaking apart and analyzing those types of songs. Learn how to play some of them. Then learn what makes that genre sound like that genre. Analyze the melody, harmony, rhythm, timbre, song form, etc. Which aspects of those things in a particular genre seem similar from song to song and which are different? What techniques do you need to work on to be able to play these genres? What aspects of the theory are most important to the genre or which ones don't you understand yet? Let these type of questions inform your practice and study time.
Then try recreating some of these songs or write new ones in the same style. Dive right in and let your failures guide you. Get a MIDI keyboard and DAW and record the song trying to recreate the sounds as closely as you can. The act of doing this will teach you the genre-specific theory, technique, and timbre that it takes to make that kind of music. So not only are you learning something but it's tailored specifically to your goal.
Break the rules
Once you've learned the "rules" of that genre, try breaking them. Write your own tune and put your own spin on it. Try to apply what you're learning in your counterpoint or jazz piano studies and see what works and what doesn't.
Then rinse and repeat with a new genre or song.
Answer:
My goals are to improve my piano technique, eventually compose music, and learn my music theory well.

I'm not sure that these goals are specific enough to help you plot a path. (I know you mentioned a bit more detail in the post, but I was still left with this impression).
if you improve your piano technique, how do you know when that goal is 'achieved'? if you improve, you'll be better than you were a while back - but there will still be room for improvement. So you may still feel you need to improve... in other words, you may be back where you started.
It's the same with learning music theory. There is always more to learn - there are always more ways to describe and analyse music; always more methods to try. So although learning music theory is an interesting activity, it isn't really a goal that can be achieved, as such. (Of course you could set an intermediate specific goal, like passing a particular exam - if that is, indeed, a genuine goal).
"eventually compose music" is another interesting one. Why eventually? Why not now? With the genres you mentioned, there's no real low bar to entry - generally, people get the equipment they need (which could probably just be the computer you're using now) and get started. You could set a more specific goal, like "get an album uploaded to bandcamp in the next 2 years", or even just to complete one plausible-sounding track in the next X months.
I guess some people are in music because they actually want to contribute something (however small) to the world of music, or a particular musical community. If you want to do this, you could work out if there's something specific that you want to contribute. If not, perhaps you could just have a look around in communities accessible to you and see what opportunities there are.
On the other hand, it seems that some people just want to explore. If this sounds like you, don't pressure yourself to find a particular path - just keep picking something that seems interesting to learn about now, and you'll naturally learn and develop your ideas. You may eventually come up with some ideas about making a more active contribution... or not! Not a problem either way.
Comment: @reincarnationofstackexchange I think that's a hard question to answer. In a subject like music, I'm not sure that focusing on efficient learning would always be healthy; you don't necessarily want to take short cuts or deny yourself the opportunity to explore, especially when you enjoy that exploration. "Follow your bliss" might be an idea worth thinking about - focus on the thing that you are feeling most passionate about at a given moment, so that the time you put in gives you the maximum pleasure. That may be a healthier kind of efficiency IMO.
Comment: Well, I do compose and arrange some music already. I also enjoy simply exploring music. Both are fun for me. My main concern is, how do I explore in a way that is efficient for learning?
Answer: "I could compose some more music..."
Yes.  That's your aim, isn't it?  So do it.  Do it LOTS.
And why not take some piano lessons?  Whatever you're studying, I'm sure the Arts department offers some outreach classes.


seq2seq in pytorch

Question: I have an encoder LSTM whose last hidden state feeds to the decoder LSTM. If i call backward on the loss for the decoder lstm, will the gradients propagate all the way back into the encoder as well, so that when i do decoder.step() and then encoder.step() both parameters are updated?
I feel as the same hidden state is being used, it should automatically take care of backpropagation to the encoder as well.
Answer: Yes, you need to do decoder.step() and encoder.step(): have a look at
https://pytorch.org/tutorials/intermediate/seq2seq_translation_tutorial.html
that's what they're doing.
Backpropagation is indeed taken care with a single call to loss.backward()
But backprop just computes the gradients everywhere.
Then decoder.step() uses these gradients to update the decoder parameters (only).
In the above example, you'll also find a useful trick (see the "detach" part) to prevent the model from backpropagating too far away in the past, because the gradients then are too small and it may lead your model to become excessively slow and memory hungry.

seq2seq in pytorch


Matrix invertibility proof?

Question: Can it be proven that $A^\top  A$ is invertible given just the fact that:

if $A^\top Ay = \theta$
  then $Ay = \theta$?

Here $y$ is a vector and $\theta$ is the vector zero.
Comment: What it does mean is that $A^T A$ is invertible if the null space of $A$ contains only $\theta$.
Comment: The property "for all $y$, if $A^TAy=0$ then $Ay=0$" holds for *any* matrix (even not square).
Answer: No, this is not enough to show $A^T A$ is invertible.  The claim holds when $A$ is the zero matrix (since $Ay=0$ is automatically true), yet in this case $A^T A$ is not invertible.
In fact the highlighted fact is true for every matrix, whether square or rectangular, singular or invertible (assuming we work over the reals and not some other field).  If $A^T A y = 0$ then $y^T A^T A y = 0$, but the LHS is just $\| Ay \|$, so $Ay$ must be the zero vector.
Answer: If $A$ is injective then $A^TA$ is invertible. Maybe that's what you mean?

Matrix invertibility proof?


T-SQL: querying table while populating it in a second session

Question: There is a table:
CREATE TABLE T1 (
  id int NOT NULL PRIMARY KEY CLUSTERED
, some_column varchar(10)
, some_other_column datetime
, yet_another_one bigint
, bazillion_other_columns_goes_here ...
);

Now, in SSMS I execute:
BULK INSERT T1 FROM 'E:\blahblahblah.dat';

blahblahblah.dat is relatively large; the BULK INSERT takes over 40 minutes to complete.
While the BULK INSERT is still running, I open a second session to the same server.database and go:
SELECT * FROM T1;

As expected, I get no rows on the output.
Then I try:
SELECT * FROM T1 WITH(NOLOCK);

This time I get a full set of rows imported so far, also as expected.
However, when I run:
SELECT id FROM t1 WITH(NOLOCK);

... I get no rows.
If I select any columns other than id, I get partial results. But when I select id only, I get nothing. Unless of course the BULK INSERT completes and everything is back to normal.
Any ideas why this is happening? Why does SELECT * return rows while SELECT id does not?
Comment: To confirm, `id` is not an IDENTITY column?
And table `t1` is not the same as `T1` in case sensitive collations
Comment: Indeed, it is not an identity, and this is all the same table.
Answer:
Any ideas why this is happening?

Because you're using NOLOCK.  So there are absolutely no guarantees about what you'll get.  Perhaps the non-leaf levels of the index haven't been built yet.  But who knows.  The results are undocumented, and could change at any time.


Primefaces dynamic tree

Question: I am developing a Tree component, which might be prunned according to the user input. I have all nodes cached, in order to provide primefaces component model with always the same instances of the tree nodes.
But the problem is, that when I change the parent-child relationships (remove some of them), I always get a concurrent modification exception... Is there any way to workaround this issue or am I missing something.
Thanks in advance.
/**
 * Recursively copy the hierarchy and prune not visible branches
 * @param originalNode node of the original hierarchy to be copied
 * @param parent parent node in the new hierarchy
 * @param visibleNodes set of nodes, which should be included in the new hierarchy
 * @param model model of the TreeAutompleteComponent
 * @return copy of the oroginal node in the new hierarchy
 */
private TreeNode copyHierarchy(TreeNode originalNode, TreeNode parent, Set<TreeNode> visibleNodes, TreeAutocompleteModel model) {
    if (!visibleNodes.contains(originalNode)) {
        return null;
    }

    TreeNode newNode = model.getCompleteModelNodes().get(((NodeWrapper)originalNode.getData()).getIri());//new DefaultTreeNode(originalNode.getData(), parent);
    for (int i = 0; i < originalNode.getChildCount(); i++) {
        copyHierarchy((TreeNode) originalNode.getChildren().get(i), newNode, visibleNodes, model);
    }
    newNode.setParent(parent); //when removed, no exception occurs
    return newNode;
}

The exception
ava.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)
    at java.util.ArrayList$Itr.next(ArrayList.java:791)
    at org.primefaces.component.tree.TreeRenderer.encodeTreeNodeChildren(TreeRenderer.java:284)
    at org.primefaces.component.tree.TreeRenderer.encodeTreeNode(TreeRenderer.java:274)
    at org.primefaces.component.tree.TreeRenderer.encodeMarkup(TreeRenderer.java:165)
    at org.primefaces.component.tree.TreeRenderer.encodeEnd(TreeRenderer.java:96)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:875)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:304)
    at com.sun.faces.renderkit.html_basic.GroupRenderer.encodeChildren(GroupRenderer.java:105)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at com.sun.faces.renderkit.html_basic.CompositeRenderer.encodeChildren(CompositeRenderer.java:78)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:845)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1779)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1782)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:424)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:125)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:182)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:722)

Answer: You are trying to modify the collection you are currently iteratring over.
Have you tried working on a copy of the collection in question instead of modifying the current one?
From http://docs.oracle.com/javase/7/docs/api/java/util/ConcurrentModificationException.html:

Note that this exception does not always indicate that an object has
  been concurrently modified by a different thread. If a single thread
  issues a sequence of method invocations that violates the contract of
  an object, the object may throw this exception. For example, if a
  thread modifies a collection directly while it is iterating over the
  collection with a fail-fast iterator, the iterator will throw this
  exception.

In your recursive function, you have the following scenario:
1 step before you come to the leaf of your tree:
Let's say you have a parent node "5" an only child (leaf) node "11". ("11" has "5" as its parent)
You start iterating over children ("11"), and call copyHierarchy on him.
Since he has no children, the loop is skipped and his parent becomes newNode, "17" for example.
Then you return one level up.
You were supposed to be iterating over nodes who have "5" as their parent, but node "11" actually has had its parent changed from "5" to "17".
Comment: I know what this exception means. If you look at the stacktrace, you will see, that the exception is NOT triggered by my code, but by primefaces. Also this cannot be multithreading issue, as the request should be always handled by one thread...
Comment: Hmz, is there any other code you can provide that might clear out the issue?
Comment: I have tried to investigate the issue using debugger, but it seems, that the collection is for some reason modified by JSF impl (mojarra in my case) while iterating over children of the root node. The modification occurs during EL resolving (finding) of component representing the child node. I honestly think that this is a bug of JSF, but I am trying to find out, if I am not doing something conceptually wrong. I may provide you with more code, just comment which part you need (template?), because this method seems to be the only relevant part...
Comment: I honestly don't know, then. If JSF is causing this, the only thing that comes to my mind is that you did not follow the contract made by JSF while designing your program or JSF has a bug, but in any case, it's no news for you. Sorry :(
Answer: This is solution, that works. I havent localized the actual issue, but I have sucessfully workaround all it...
public TreeNode constructActualModel(TreeAutocompleteMatcherIface matcher) {
    if (prunedModel == null) {
        eraseHierarchyStructure();
        prunedModel = copyHierarchy((TreeNode) completeModel, getVisibleNodes(matcher));
    }
    return prunedModel;
}

/**
 * Recursively copy the hierarchy and prune not visible branches
 * @param originalNode node of the original hierarchy to be copied
 * @param visibleNodes set of nodes, which should be included in the new hierarchy
 * @param model model of the TreeAutompleteComponent
 * @return copy of the oroginal node in the new hierarchy
 */
private TreeNode copyHierarchy(TreeNode originalNode, Set<TreeNode> visibleNodes) {
    if (!visibleNodes.contains(originalNode)) {
        return null;
    }

    TreeNode newNode = this.getCompleteModelNodes().get(((NodeWrapper) originalNode.getData()).getIri());//new DefaultTreeNode(originalNode.getData(), parent);
    List<TreeNode> children = new ArrayList<TreeNode>();
    for (int i = 0; i < originalNode.getChildCount(); i++) {
        TreeNode child = copyHierarchy((TreeNode) originalNode.getChildren().get(i), visibleNodes);
        if(child != null){
            children.add(child);
        }
    }
    ((DefaultTreeNode) newNode).setChildren(children);
    return newNode;
}

private void eraseHierarchyStructure() {
    for (TreeNode node : duplicateCompleteModelNodes.values()) {
        node.setParent(null);
    }
}

It was necessary to do it the opposite way - to get the impl to be able to call the setChildren method and at first erase the hierarchy...

Primefaces dynamic tree


vertical accordion from horizontal

Question: //# jQuery - Horizontal Accordion
//# Version 2.00.00 Alpha 1
//#
//# portalZINE(R) - New Media Network
//# http://www.portalzine.de
//#
//# Alexander Graef
//# firstname.lastname@example.com
//#
//# Copyright 2007-2009

(function($) {
    $.hrzAccordion = {

       setOnEvent: function(i, container, finalWidth, settings){
            $("#"+container+"Handle"+i).bind(settings.eventTrigger,function() {

                        var status = $('[rel='+container+'ContainerSelected]').data('status');

                        if(status ==1 && settings.eventWaitForAnim === true){
                         return false;
                        }

                        if( $("#"+container+"Handle"+i).attr("rel") != container+"HandleSelected"){

                          settings.eventAction;

                            $('[id*='+container+'Handle]').attr("rel","");

                            $('[id*='+container+'Handle]').attr("class",settings.handleClass);

                            $("#"+container+"Handle"+i).addClass(settings.handleClassSelected);

                            $("."+settings.contentWrapper).css({width: finalWidth+"px" });

                            switch(settings.closeOpenAnimation)
                            {
                            case 1:

                        if($('[rel='+container+'ContainerSelected]').get(0)  ){
                        $('[rel='+container+'ContainerSelected]').data('status',1);

                            //current_width = $('[rel='+container+'ContainerSelected]').width();

                            $('[rel='+container+'ContainerSelected]').animate({width: "0px",opacity:"0"}, { queue:true, duration:settings.closeSpeed ,easing:settings.closeEaseAction,complete: function(){

                            $('[rel='+container+'ContainerSelected]').data('status',0);                                                                                } ,step: function(now){
                                                                                width = $(this).width();

                            //new_width = finalWidth- (finalWidth  * (width/current_width));
                            new_width = finalWidth - width;
                            $('#'+container+'Content'+i).width(Math.ceil(new_width)).css("opacity","1");

                            }});

                        }else{
                            $('[rel='+container+'ContainerSelected]').data('status',1);

                            $('#'+container+'Content'+i).animate({width: finalWidth,opacity:"1"}, { queue:false, duration:settings.closeSpeed ,easing:settings.closeEaseAction,complete: function(){
                                                                                $('[rel='+container+'ContainerSelected]').data('status',0);
                                                                                }});

                            }

                            break;
                            case 2:
                                $('[id*='+container+'Content]').css({width: "0px"});
                                $('#'+container+'Content'+i).animate({width: finalWidth+"px",opacity:"1"}, { queue:false, duration:settings.openSpeed ,easing:settings.openEaseAction, complete:
 settings.completeAction                                                                                });

                            break;
                            }

                            $('[id*='+container+'Content]').attr("rel","");
                            $("#"+container+"Handle"+i).attr("rel",container+"HandleSelected");
                            $("#"+container+"Content"+i).attr("rel",container+"ContainerSelected");

                        }

                    });
}
        };

    $.fn.extend({

        hrzAccordionLoop: function(options) {
            return this.each(function(a){

                var container = $(this).attr("id") || $(this).attr("class");
                var elementCount = $('#'+container+' > li, .'+container+' > li').size();
                var settings = $(this).data('settings');

                variable_holder="interval"+container ;
                var i =0;
                var loopStatus  = "start";

                variable_holder = window.setInterval(function(){

                $("#"+container+"Handle"+i).trigger(settings.eventTrigger);

                if(loopStatus =="start"){
                        i = i + 1;
                    }else{
                        i = i-1;
                    }

                    if(i==elementCount && loopStatus  == "start"){
                        loopStatus  = "end";
                        i=elementCount-1;

                    }

                    if(i==0 && loopStatus  == "end"){
                        loopStatus  = "start";
                        i=0;

                    }
                                                },settings.cycleInterval);

                });
            },
        hrzAccordion: function(options) {
            this.settings = {
            eventTrigger            : "click",
            containerClass          : "container",
            listItemClass           : "listItem",
            contentContainerClass   : "contentContainer",
            contentWrapper          : "contentWrapper",
            contentInnerWrapper     : "contentInnerWrapper",
            handleClass             : "handle",
            handleClassOver         : "handleOver",
            handleClassSelected     : "handleSelected",
            handlePosition          : "right",
            handlePositionArray     : "", // left,left,right,right,right
            closeEaseAction         : "swing",
            closeSpeed              : 500,
            openEaseAction          : "swing",
            openSpeed               : 500,
            openOnLoad              : 2,
            hashPrefix              : "tab",
            eventAction             : function(){
                                    //add your own extra clickAction function here
                                    },
            completeAction          : function(){
                                    //add your own onComplete function here
                                    },
            closeOpenAnimation      : 1,// 1 - open and close at the same time / 2- close all and than open next
            cycle                   : false, // not integrated yet, will allow to cycle through tabs by interval
            cycleInterval           : 10000,
            fixedWidth              : "",
            eventWaitForAnim        : true

        };

        if(options){
            $.extend(this.settings, options);
        }
            var settings = this.settings;

            return this.each(function(a){

                var container = $(this).attr("id") || $(this).attr("class");

                $(this).data('settings', settings);

                $(this).wrap("

");

                var elementCount = $('#'+container+' > li, .'+container+' > li').size();

                var containerWidth =  $("."+settings.containerClass).width();

                var handleWidth = $("."+settings.handleClass).css("width");

                handleWidth =  handleWidth.replace(/px/,"");
                var finalWidth;
                var handle;

                if(settings.fixedWidth){
                    finalWidth = settings.fixedWidth;
                }else{
                    finalWidth = containerWidth-(elementCount*handleWidth)-handleWidth;
                }

                $('#'+container+' > li, .'+container+' > li').each(function(i) {

                    $(this).attr('id', container+"ListItem"+i);
                    $(this).attr('class',settings.listItemClass);
                    $(this).html("
"
                                 +"
"
                                 +"
"
                                 +$(this).html()
                                 +"

");

                    if($("div",this).hasClass(settings.handleClass)){

                    var html = $("div."+settings.handleClass,this).attr("id",""+container+"Handle"+i+"").html();
                    $("div."+settings.handleClass,this).remove();

                     handle = "
"+html+"
";
                    }else{
                     handle = "

";
                    }

                    if(settings.handlePositionArray){
                        splitthis               = settings.handlePositionArray.split(",");
                        settings.handlePosition = splitthis[i];
                    }

                    switch(settings.handlePosition ){
                        case "left":
                        $(this).prepend( handle );
                        break;
                        case "right":
                        $(this).append( handle );
                        break;
                        case "top":
                        $("."+container+"Top").append( handle );
                        break;
                        case "bottom":
                        $("."+container+"Bottom").append( handle );
                        break;
                    }

                    $("#"+container+"Handle"+i).bind("mouseover", function(){
                        $("#"+container+"Handle"+i).addClass(settings.handleClassOver);
                    });

                    $("#"+container+"Handle"+i).bind("mouseout", function(){
                        if( $("#"+container+"Handle"+i).attr("rel") != "selected"){
                            $("#"+container+"Handle"+i).removeClass(settings.handleClassOver);
                        }
                    });

                    $.hrzAccordion.setOnEvent(i, container, finalWidth, settings);

                    if(i == elementCount-1){
                        $('#'+container+",."+container).show();
                    }

                    if(settings.openOnLoad !== false && i == elementCount-1){
                            var location_hash = location.hash;
                            location_hash  = location_hash.replace("#", "");
                            if(location_hash.search(settings.hashPrefix) != '-1' ){
                            var tab = 1;
                            location_hash  = location_hash.replace(settings.hashPrefix, "");
                            }

                            if(location_hash && tab ==1){
                                $("#"+container+"Handle"+(location_hash)).attr("rel",container+"HandleSelected");
                                $("#"+container+"Content"+(location_hash)).attr("rel",container+"ContainerSelected");
                                $("#"+container+"Handle"+(location_hash-1)).trigger(settings.eventTrigger);

                            }else{
                                $("#"+container+"Handle"+(settings.openOnLoad)).attr("rel",container+"HandleSelected");
                                $("#"+container+"Content"+(settings.openOnLoad)).attr("rel",container+"ContainerSelected");
                                $("#"+container+"Handle"+(settings.openOnLoad-1)).trigger(settings.eventTrigger);
                            }
                    }
                });

                if(settings.cycle === true){
                    $(this).hrzAccordionLoop();
                }
            });
        }
    });
})(jQuery);

**Given is the code used for the accordion...please check out this Accordion Link. in the link there are four examples of accordions. I want the last accordion, i.e example 4, to be vertical.
Comment: helping someone and doing others job are two different things @Sify Juhy.
Comment: @Sify - what people are trying to tell you is that if you want help here, you need to zero in on the code that's causing you a problem, show that, and ask a specific question about why it's wrong.  Nobody is going to read 5 pages of code and engineer you a solution.
Comment: OP, would you fix the link - it is broken, and I don't have enough rep to fix it without making other changes.
Comment: i know the code is lengthy ...but i had no other options :(
Comment: i understand..but i am not very good at JS.
Answer: I think this is what you want:
http://www.portalzine.de/Horizontal_Accordion_Plugin_2/lib/jquery.hrzAccordion.examples.js
Edit: the JS you've posted is, I think, the whole plugin. Just include the plugin on your page, plus jQuery, plus the relevant snippet from my link above.
Comment: Btw, you may find it easier to google for "vertical accordion jquery", as this is quite likely to find something :)
Comment: i have this file..but i want the accordion to be vertical.
Comment: i already did that..its a horizontal accordion ...i want to change it to vertical accordion.
Comment: Ah, I see. That is a much more significant change, apologies. In that case you'll need to see what part of the plugin code is doing the horizontal animations, and modify them. I'd start off with the `new_width` stuff - this looks like it is setting some div widths, and you want to set some heights instead. Switching those around might work - give it a go?

vertical accordion from horizontal


Add a MetaClass to a library class

Question: In a Python library I'm using, I want to wrap the public methods of a class from the library. I'm trying to use a MetaClass to do this like so.
from functools import wraps
from types import FunctionType
from six import with_metaclass

def wrapper(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        print("wrapped %s" % func)
        return func(*args, **kwargs)
    return wrapped

class MetaClass(type):
    def __new__(mcs, classname, bases, class_dict):
        library_class_dict = bases[0].__dict__

        print(library_class_dict)

        for attributeName, attribute in library_class_dict.items():
            if type(attribute) == FunctionType and \
                    not attributeName.startswith('_'):
                print("%s %s" % (attributeName, attribute))
                attribute = wrapper(attribute)

            library_class_dict[attributeName] = attribute

        print(library_class_dict)

        return type.__new__(mcs, classname, bases, class_dict)

# this is the class from the library that I cannot edit
class LibraryClass(object):
    def library_method(self):
        print("library method")

class Session(with_metaclass(MetaClass, LibraryClass)):

    def __init__(self, profile, **kwargs):
        super(Session, self).__init__(**kwargs)
        self.profile = profile

When you put this into a Python file and run it, you get the error
TypeError: Error when calling the metaclass bases
    'dictproxy' object does not support item assignment

I get that trying to assign directly to __dict__ is a bad idea. That's not what I want to do. I would much rather add the MetaClass to the LibraryClass but I'm not sure how.
I've been through the other StackOverflow questions regarding Python MetaClass programming but haven't come across any that try to add a MetaClass to a library class where you can't the source code.
Comment: 1. What are you actually trying to achieve. 2. What is the full error traceback?
Comment: @Pynchia: well, trying to alter a base class (`LibraryClass` here) every time you subclass is.. redundant. You only need to alter that base class once, and you don't need to use a metaclass for this.
Comment: If my understanding of what you are trying to do is correct, I think class decorators would be more suitable and less complex to accomplish this. Have you given them a shot?
Comment: is [this SO QA](http://stackoverflow.com/questions/11349183/how-to-wrap-every-method-of-a-class) relevant?
Comment: `library_class_dict` is a reference to the class `dictproxy` object, and it doesn't support assignment. Why are you trying to alter a baseclass from the metaclass? Why not add overrides in the new class that call their `super()` method instead?
Comment: @MartijnPieters that is what I wanted to ask and suggest from the very beginning, but I assumed the OP wants to keep code to a minimum and/or has many methods to wrap (and I do not know much about meta's)
Comment: @Pynchia is correct in that there are many methods to wrap and I want to keep the code to a minimum.
Comment: @MartijnPieters I'm totally open to alternative implementations.
Answer: You can't assign to a dictproxy. Use setattr() to set attributes on a class:
setattr(bases[0], attributeName, attribute)

However, you don't need a metaclass to do this, which is entirely overkill here. You can just do this on that base class, and do it once:
for attributeName, attribute in vars(LibraryClass).items():
    if isinstance(attribute, FunctionType) and not attributeName.startswith('_'):
        setattr(LibraryClass, attributeName, wrapper(attribute))

This just does it once, rather than every time you create a subclass of of LibraryClass.
Comment: @Pynchia: anywhere you like, but make sure it is run before you need to use any of those wrapped methods.
Comment: upvoted. But can you please describe more precisely where that code should go? Thank you
Comment: Worked for me. Much less code than using a metaclass too.
Answer: Essentially you want to do this:
LibraryClass.library_method = wrapper(LibraryClass.library_method)

for all methods automatically.
Using your code pieces:
from functools import wraps
from types import FunctionType

class LibraryClass(object):
    def library_method(self):
        print("library method")

def wrapper(func):
    @wraps(func)
    def wrapped(*args, **kwargs):
        print("wrapped %s" % func)
        return func(*args, **kwargs)
    return wrapped

You can write a helper function that does that for all methods:
def wrap_all_methods(cls):
    for name, obj in cls.__dict__.items():
        if isinstance(obj, FunctionType) and not name.startswith('_'):
            setattr(cls, name, wrapper(obj))
    return cls

Now, wrap all methods:
LibraryClass = wrap_all_methods(LibraryClass)

Test if it works:
class Session(LibraryClass):
    pass

s = Session()

s.library_method()

prints:
wrapped <function LibraryClass.library_method at 0x109d67ea0>
library method

The method is wrapped.

Add a MetaClass to a library class


C - Time complexity of nested loops

Question: What should be the Time Complexity of below code?
     int j = 0;
     for(i=0;i<n;i++)
     {
         for(i=0;i<n*n ;i++)
         {
             while(j<n)
             {
                 j++;
             }
         }
     }

My Attempt-

Outer for loop will run just once.
Inner for loop will run n*n times BUT only when i=0, while loop will run for n times.
Now,

My point of confusion is -

If I write  n+(n^2−1)∗O(1) as n+O(1)+O(1)+.........+O(1) then I can ignore lower order terms and complexity will be O(n) but then another reasoning  can be a constant amount of work is being done n^2 time so time complexity should be O(n^2)
Similar thing is happening in this question also-
Time complexity O(N) of nested loops with if-statement: O(N^4)?
According to the reasoning given in above link-
I can rewrite the above code simply like-
  j=0;
    while(j<n)
    {
        j++;
    }

This eliminates those steps in which while loop does not get entered.
So T.C should be O(n).
Both reasoning looks intuitive to me. Please help me.
Comment: "If I write n+(n^2−1)∗O(1) as n+O(1)+O(1)+.........+O(1) then I can ignore lower order terms " Unless the `...` stands for a *constant* number of 1s, you can't remove it. If you could, everything would be `O(1)` (you could just replace any variable with a variable amount of `+1`s and then remove those).
Comment: I can't tell whether the oddities in your code (like the outer for loop being useless and the while loop only executing the first time) are your mistakes or the author's attempt to make the question trickier.
Comment: Author attempted to make it tricker. Thank you so much.
Your this line-
" Unless the ... stands for a constant number of 1s, you can't remove it. If you could, everything would be O(1)
cleared all my doubts. :)
Comment: You (or the compiler) could rewrite the code that way *if* `i` is never used after the loop (and if it is, you could just add `i=n*n+1` though I'm not sure a compiler would do that automatically), but complexity questions aren't about different code that's semantically equivalent or about compiler optimizations. They're about the code as written, so loops that don't affect the output, still count for the complexity (though of course in the real world you'd never write code like that).
Comment: Ok. thank you. I will take care of it while doing complexity questions :)
Answer: The time complexity is O(n^2).
The outer loop runs only once, so it can be ignored. The middle loop runs n*n times, of which only the first iteration takes O(n) steps, the rest is O(1), as j will be n.
So it's O(n^2 + n) = O(n^2).
Comment: I am sorry, It's exponentiation only. Please edit answer according to it.
 I am editing question.

C - Time complexity of nested loops


Bash remove every occurrence after first in string

Question: I'm trying to remove everything after a specific_string in a path string in Bash. I've tried using sed to no avail so far.
variable="specific_string"
input_string="/path/to/some/specific_string/specific_string.something/specific_string.something-else"
output=$(sed 's/$variable//' $input_string)

Output should be "/path/to/some/specific_string/"
Would be better if I didn't have to use commands such as sed!
Answer: The Problems
There are many problems

Variables are not evaluated inside single quotes. 's/$variable//' will be treated as a literal string, which does not contain specific_string
sed can modify text from files or STDIN, but not text given via parameters. With sed 's/...//' $input_string the /path/to/some/specific_string/.../file is opened and its content is read, instead of the path itself.
s/string// deletes only string, not the words afterwards.

Also remember to double quote your variables. cmd $variable is dangerous if the variable contains spaces. cmd "$variable" is safe.
Sed Solution
output="$(sed "s/$variable.*/$variable/" <<< "$input_string")"

GNU Grep Solution
output="$(grep -Po "^.*?$variable" <<< "$input_string")"

Pure Bash Solution
output="${input_string%%$variable*}$variable"

Answer: If you want to remove everything after "specific_string" it will remove the "/" also as it does with the following example:
output=$(echo $input_string|sed "s/${variable}.*$/${variable}/")

Answer: try with simple sed:
variable="specific_string"
input_string="/path/to/some/specific_string/specific_string.something/specific_string.something-else"
output=$(echo "$input_string" | sed "s/\(.*$variable\/\).*/\1/")

Output of variable output will be as follows.
echo $output
/path/to/some/specific_string/


Understand this progression/harmony

Question: I've found a song that uses the next progression. Is a 4 "simple" 4 bars progression:
Cm / D / D# / F
In the first 3 bars, it plays notes from this scale: D D# F# G A A# C
In the last bar, basically is using the notes from the triad: F A C
At the beginning I thought it was a G harmonic minor progression, but the last bar blows my mind because it works so well but I can't fit it all together (it should be F#, not F).
Is the song making a key change? or maybe I'm too tired and I can see the obvious?
Thanks to all the community in advance.
Answer: First, that D# chord is more easily understood as an Eb chord.
Assuming that this chord progression loops, I'd think it's in C minor, with this Roman numeral notation:
i - V/v - VI/v = III - IV
So the D-to-Eb chord progression would be understood as V/v (a secondary dominant) to VI/v, rather like a deceptive cadence. The Eb chord would be a capable pivot chord back into C minor.
IV going to i (a subdominant variant going to the tonic) sounds fine to me.
Alternately, if we treat this as being in G minor, we get this:
iv - V - VI - VII
Shame we don't reach the tonic!
Comment: With the scale notes being G A Bb C D Eb F#, it's highly likely to be G harmonic minor. With it being modal and using C as the root.
Comment: Mmhm, one thing is true and I didn't say it before: the song/progression has a bass making a C note all the time in the 4 bars.

But I don't understand, if it were a C minor, we would have: C D Eb **F** G **Ab** Bb

And not "my" notes: C D Eb F# G A Bb
And D chord should be suspended and the F chord should be minor and not major isn't?
Comment: @Victor it's rather c-[Dorian](https://en.wikipedia.org/wiki/Dorian_mode) than Ionian/major, and (in this answer's interpretation) the D chord is basically borrowed from the dominant key.
Answer: This is best undestood as iv-V-VI-VII in G minor. Sure, D major chords are from G harmonic minor, but that doesn't exclude the rest of the G natural minor's chords.
In light of the new developent in this post, the C pedal over the entire progression, I agree with the other answers that suggest C Dorian with a borrowed D chord from the relative minor, G minor (harmonic minor).
Comment: I see the original post left out the part about bass part playing a `C` pedal. In light of that @leftaroundabout's comment about C Dorian seems probably the best description.
Comment: What is your reason for seeing this as g rather than c, if the "dominant" anyways doesn't resolve to a g chord? What does your answer add over Dekkadeci's?
Comment: @leftaroundabout, all the pitches are from G minor where the only chromatic is the raised ^7 for the dominant `D`, while it's funny the tonic is omitted, everything else is straight forward key of G minor. The dominant truly defines a key. If it were C minor we have two foreign chords - that aren't really functional - and no dominant.
Comment: I conceptualized this as a variation of the VI-VII-i move in minor, that's really really common. Also, in C, it seems to make less sense, with less of a sense of resolution. It could, of course be C minor, I just think it makes more sense in G minor. As for what it adds, in my opinion it is a refutation of his idea.
Comment: Thank you, @MichaelCurtis. I've edited my response. Really interesting piece, should be!

Understand this progression/harmony


Logging with filebeat - extract JSON to ES fileds

Question: We use filebeat + elk to handle our logs.

Our requirement from filebeat is to receive a json object in each line and extract the following fields:

message field: The whole message that was logged.
For each json field, create equivalent field in ES document.

We've tried to use json.keys_under_root: true but then we didn't succeed to store in 'message' field the whole content.

We want to have a multiline support and we didn't succeed to start the filebeat service with both json.keys_under_root: true and multiline.pattern:<pattern>definition.

Do you have any idea regarding these two issues?
We prefer to execute this json parsing using filebeat and not in logstash.
Thanks a lot.
Answer: 1.) Below filebeat.yml configuration helped me to parse one-line Json log file.
filebeat.prospectors:
paths:
     your path.
     input_type: log
     json.keys_under_root: true
     json.add_error_key: true
2.) I haven't tried multiline pattern yet. You can also check https://discuss.elastic.co/t/input-multiline-json/165287 this post and other post in this forum.


VBA Excel Reg-ex

Question: I cant seem to figure out how to put this all into regex. I know there has got to be an easy way but i am stumped. I dont think you can do chaining with vba excel?  Any guidance would be awesome. Thanks!!
Set Myrange = ActiveSheet.Range(Range("A2"), Range("A2").End(xlDown))
<code>For Each Cell In Myrange
        strInput = LCase(Cell.value)
        strInput = Replace(strInput, " ", "-")
        strInput = Replace(strInput, ".", "-")
        strInput = Replace(strInput, ",", "-")
        strInput = Replace(strInput, "'", "")
        strInput = Replace(strInput, ")", "")
        strInput = Replace(strInput, "(", "")
        strInput = Replace(strInput, "---", "-")
        strInput = Replace(strInput, "--", "-")
        strInput = Replace(strInput, "®", "")
        strInput = Replace(strInput, "™", "")
        strInput = Replace(strInput, "%", "-percent")
        strInput = Replace(strInput, """", "-inch")

        Cells(Cell.Row, "F") = strInput
Next
</code>
Comment: What does this have to do with regex? You've dumped a bunch of string replace code, not explained a problem, there's absolutely no regular expression anywhere in your question... What's wrong with the code you currently have?
Comment: You have various replacement strings, you can't merge it into 1 regex replace code.
Comment: Thanks, that was the question. I knew how to do it with replace strings, it's the regex that I was trying to convert it to. Regex confuses me lol. For some reason I can't seem to grasp it
Answer: Brief
You require 4 regular expressions according to the information you've given.
See this post for information on how to run regex in VBA

Code
Expression 1
Regex
<code>([ .,]|-{2,3})
</code>
Replacement
<code>-
</code>

Expression 2
Regex
<code>(['()]|®|™)
</code>
Replacement

Expression 3
Regex
<code>(%)
</code>
Replacement
<code>-percent
</code>

Expression 4
Regex
<code>(")
</code>
Replacement
<code>-inch
</code>
Comment: Thanks ;) this helps much appreciated

VBA Excel Reg-ex


sql server 2008: select substring from a field

Question: here is example data:
 10914_Excel Short Summary.xls
F:\MassHunter\DATA\10921_PAIN\QuantResults\10921_PAIn.batch.bin
10918_Excel Short Summary.xls
10923_Excel Short Summary.xls
10922_Excel Short Summary.xls
10913_Excel Short Summary.xls
10912R_Excel Short Summary.xls
F:\MassHunter\DATA\10907_PAIN\QuantResults\10907_PAIN.batch.bin
F:\MassHunter\DATA\10915_PAIN\QuantResults\10915_PAIN.batch.bin
F:\MassHunter\DATA\10909_PAIN\QuantResults\10909_PAIN.batch.bin
F:\MassHunter\DATA\10914_PAIN_new\QuantResults\10914_PAIN_new.batch.bin
F:\MassHunter\DATA\10911_PAIN\QuantResults\10911_PAIN.batch.bin
C:\Documents and Settings\Michelle Miller\Desktop\10917_PAIN\QuantResults\10917_PAIN.batch.bin
F:\MassHunter\DATA\10913_PAIN\QuantResults\10913_PAIN.batch.bin
C:\Sergey\10923_PAIN\QuantResults\10923_pain.batch.bin
C:\Documents and Settings\Michelle Miller\Desktop\10922_PAIN\QuantResults\10922_PAIN.batch.bin
C:\Sergey\10919_PAIN\QuantResults\10919_pain.batch.bin
D:\Marina\10920_PAIN\QuantResults\10920_PAIN.batch.bin
D:\Marina\10925_PAIN\QuantResults\10925_PAIN.batch.bin
D:\Marina\10937_PAIN\QuantResults\10937_PAIN.batch.bin
10925_Excel Short Summary.xls
10924_Excel Short Summary.xls
10925_Excel Short Summary_100727092302.xls
10925_THC_Excel Short Summary.xls
F:\MassHunter\DATA\10924_PAIN\QuantResults\10924_pAIN.batch.bin
10924_THC_Excel Short Summary.xls
10926_Excel Short Summary.xls
10927_Excel Short Summary.xls
10928_Excel Short Summary.xls
10929_Excel Short Summary.xls
10930_Excel Short Summary.xls
10931_Excel Short Summary.xls
10932_Excel Short Summary.xls
10933_Excel Short Summary.xls
10934_Excel Short Summary.xls
10935_Excel Short Summary.xls
10936_Excel Short Summary.xls
F:\MassHunter\DATA\10926_PAIN\QuantResults\10926_PAIN.batch.bin
F:\MassHunter\DATA\10938_PAIN\QuantResults\10938_Pain.batch.bin
F:\MassHunter\DATA\10928_PAIN\QuantResults\10928_PAIN.batch.bin
C:\LSR Data\10940_PAIN\QuantResults\10940_PAIN.batch.bin
10927_THC_Excel Short Summary.xls
10930_THC_Excel Short Summary_100727112433.xls
F:\MassHunter\DATA\10932_PAIN\QuantResults\10932_PAIN.batch.bin
10926_THC_Excel Short Summary_100727113358.xls
10929_THC_Excel Short Summary.xls
10928_THC_Excel Short Summary.xls
10931_THC_Excel Short Summary.xls
10932_THC_Excel Short Summary_100727114507.xls
F:\MassHunter\DATA\10939_PAIN\QuantResults\10939_PAIN.batch.bin
10947_Excel Short Summary.xls
10948_Excel Short Summary.xls
10949_Excel Short Summary.xls
10933_THC_Excel Short Summary.xls
10934_THC_Excel Short Summary.xls
10937_Excel Short Summary.xls
10938_Excel Short Summary.xls
10939_Excel Short Summary.xls
10942_Excel Short Summary.xls
F:\MassHunter\DATA\10927_PAIN\QuantResults\10927_PAIN.batch.bin
10940_Excel Short Summary.xls
F:\MassHunter\DATA\10931_PAIN\QuantResults\10931_PAIN.batch.bin
F:\MassHunter\DATA\10929_PAIN\QuantResults\10929_PAIN.batch.bin
10935_THC_Excel Short Summary.xls
10937_THC_Excel Short Summary.xls
10938_THC_Excel Short Summary.xls
10950_Excel Short Summary.xls
F:\MassHunter\DATA\10941_PAIN\QuantResults\10941_PAIN.batch.bin
10947_THC_Excel Short Summary.xls
10949_THC_Excel Short Summary.xls
F:\MassHunter\DATA\10934_PAIN\QuantResults\10934_PAIN.batch.bin
10950_THC_EXCEL SHORT SUMMARY.XLS_Final.xls
10940_THC_Excel Short Summary.xls
F:\MassHunter\DATA\10935_PAIN\QuantResults\10935_PAIN.batch.bin
F:\MassHunter\DATA\10942_PAIN\QuantResults\10942_PAIN.batch.bin
F:\MassHunter\DATA\10933_PAIN\QuantResults\10933_PAIN.batch.bin
10941_Excel Short Summary.xls
10939_THC_Excel Short Summary.xls
10941_THC_Excel Short Summary.xls
10942_THC_Excel Short Summary.xls
F:\MassHunter\DATA\10936_PAIN\QuantResults\10936_PAIN.batch.bin
F:\MassHunter\DATA\10950_PAIN\QuantResults\10950_PAIN.batch.bin
F:\MassHunter\DATA\10949_PAIN\QuantResults\10949_PAIN.batch.bin
10950_THC_Excel Short Summary.xls
F:\MassHunter\DATA\10930_PAIN\QuantResults\10930_PAIN.batch.bin
F:\MassHunter\DATA\10948_PAIN\QuantResults\10948_PAIN.batch.bin
10936_THC_Excel Short Summary.xls
10948_THC_Excel Short Summary.xls
10966_THC_Excel Short Summary.xls
10964_THC_Excel Short Summary.xls
F:\MassHunter\DATA\10947_R_PAIN\QuantResults\10947_R_PAIN.batch.bin
10960_THC_Excel Short Summary.xls
10958_Excel Short Summary.xls
10963_THC_Excel Short Summary.xls
10962_THC_Excel Short Summary.xls
10959_Excel Short Summary.xls
10961_THC_Excel Short Summary.xls
10960_Excel Short Summary.xls
10959_THC_Excel Short Summary.xls
10961_Excel Short Summary.xls
10962_Excel Short Summary.xls
10963_Excel Short Summary.xls
10967_THC_Excel Short Summary.xls
10964_Excel Short Summary.xls
10968_THC_Excel Short Summary.xls
10965_Excel Short Summary.xls
10969_THC_Excel Short Summary.xls
10970_THC_Excel Short Summary.xls
F:\MassHunter\DATA\10958_PAIN_new\QuantResults\10958_PAIN.batch.bin
F:\MassHunter\DATA\10964_PAIN\QuantResults\10964_PAIN.batch.bin
F:\MassHunter\DATA\10966_PAIN\QuantResults\10966_PAIN.batch.bin
F:\MassHunter\DATA\10981_PAIN\QuantResults\10981_PAIN.batch.bin
10958_THC_Excel Short Summary.xls
10965_THC_Excel Short Summary.xls
F:\MassHunter\DATA\10963_PAIN\QuantResults\10963_PAIN.batch.bin
10971_THC_Excel Short Summary.xls
10972_THC_Excel Short Summary.xls
10966_Excel Short Summary.xls
F:\MassHunter\DATA\10961_PAIN\QuantResults\10961_PAIN.batch.bin
10967_Excel Short Summary.xls
F:\MassHunter\DATA\10962_PAIN\QuantResults\10962_PAIN.batch.bin
10968_Excel Short Summary.xls
F:\MassHunter\DATA\10965_PAIN\QuantResults\10965_PAIN.batch.bin
10969_Excel Short Summary.xls
10970_Excel Short Summary.xls

the result that i need is:
10914
10921
10918
10923
10922
10913
10912
10907
10915
...
...

how would this select statement look?
select column from table where column...

Comment: Always 4 digits? And do you want the whole column out, or only search on it?
Comment: @gbn im sorry i updated the question. it is not always four digits. i just want to return those values from the column
Comment: @i am a girl, it isn't clear what you want to do - retrieve all numeric data from the columns, retrieve the first five characters of a filename (excluding file path), randomly generate a five-digit number on every row... Please can you clarify what you are trying to achieve.
Answer: Break it down into 2 steps:

discard everything upto the start of the number (here I assumed 3 digists minimum)
then take everything upto the next non-numeric digit

You'll need a CASE for the LEFT is the number is at the end because PATINDEX will return zero
DECLARE @MyTable TABLE (bigstring varchar(200))
INSERT @MyTable VALUES ('F:\MassHunter\DATA\6897_Pan_1\QuantResults\6897_Pan_1.batch.bin')
INSERT @MyTable VALUES ('F:\MassHunter\DATA\6897_Pan_1\QuantResults\6897_Pan_1.batch.bin')
INSERT @MyTable VALUES ('10914_Excel Short Summary.xls')

SELECT  --assumes number not at end of string
    LEFT(startOf, PATINDEX('%[^0-9]%', startof)-1)
FROM
    (
    SELECT  --assumed 3 digits minimum
        SUBSTRING(bigstring, PATINDEX('%[0-9][0-9][0-9]%', bigstring), 8000) AS startOf
    FROM
        @MyTable
    ) foo

Comment: @JNK: Thanks. May I comment on your use of "him" with a user called "i am a girl" :-)
Comment: @gbn - just caught that and fixed it as you typed it :)
Comment: @gbn thanks so much how do i select ONLY bigstrings that are greater than 3 characters?
Comment: i am trying this but not wokring SELECT  --assumes number not at end of string
    LEFT(startOf, PATINDEX('%[^0-9]%', startof)-1)
FROM
    (
    SELECT  --assumed 3 digits minimum
        SUBSTRING(datapath, PATINDEX('%[0-9][0-9][0-9]%', datapath), 8000) AS startOf
    FROM
       select datapath from batchinfo where LEN(datapath)>3
    ) foo
Comment: Extend [0-9][0-9][0-9] as needed. This is only a *minimum* length, not fixed.
Comment: @gbn what does 8000 stand for?
Comment: Msg 537, Level 16, State 5, Line 2
Invalid length parameter passed to the LEFT or SUBSTRING function.
Comment: @gbn - sounds like it's returning a negative for the char in the LEFT on the second line.
Comment: Of course it won't work: why did you add LEN and completely wrong SELECT. Simply substitute batchinfo for @mytable and datapath for bigstring. Do not change anything else
Comment: @i am a girl - length of the expression to return.  he put it at 8000 so you wouldn't have any truncation
Comment: @i am a girl, @jnk: max length of varchar. you need 3 arguments for substring. no need to know the actual length if you use 8000...
Comment: @gbn is that better than calculating length of substrnig
Comment: @gbn thank u very much can u help wtih this one http://stackoverflow.com/questions/4108551/sql-server-2008-combining-two-complex-queries


Show selected element in Telerik MVC TreeView

Question: this is the code where i create tree in telerik mvc. Now i want to create popup which shows the slected node name.
Html.Telerik().TreeView()
    .Name("ZebTree")
    .ExpandAll(true)
    .ClientEvents(events => events.OnSelect("TreeView_onSelect"))
    .BindTo(Model , map =>
            {
                map.For<TreeViewBind.Models.Category>(bind => bind.ItemDataBound((item, category) => { item.Text = category.CategoryName; }).Children(category => category.Products));
                map.For<TreeViewBind.Models.Product>(bind => bind.ItemDataBound((item, product) => { item.Text = product.ProductName;}));
            }
    )

Here is the javascript code.
function TreeView_onSelect(e)
    {
        var treeview = $(this).data('tTreeView');

        alert("You select the node " + ...);

    }

Answer: to get a node's text:
function TreeView_onSelect(e)
{
  var treeview = $(this).data('tTreeView');
  var nodeText = treeview.getItemText(e.item);
  alert("You select the node: " + nodeText);
}

Regards


prove if the function is vector space

Question: Let $\Bbb R^3$ be 3-dimensional euclidean space.
Is the set of all vectors in $\Bbb R^3$ that have the form $v=(v_1,v_2,v_3)$ where $5v_1-3v_2+2v_3=0$ itself a vector space?
so my take:
$v+w = (v_1+w_1,v_2+w_2,v_3+w_3)$ satisfies $5(v_1+w_1)-3(v_2+w_2)+2(v_3+w_3) = 0 + 0 = 0$
and now I know I have to use similar method to use $tv$ has this form but I'm not quite sure how to write this out.
Comment: I'm not sure why you got downvoted. Please don't let that discourage you! I have upvoted this question, since you've showed your efforts and thoughts on the problem.
Comment: Aw thank you :)  I didn't even noticed that, have a good Christmas break @CameronBuie
Answer: If $v:=(v_1,v_2,v_3)$ is in your set, then
$$
5v_1-3v_2+2v_3=0
$$
hence $tv=(tv_1,tv_2,tv_3)$ is such that
$$
5(tv_1)-3(tv_2)+2(tv_3)=t(5v_1-3v_2+2v_3)=t\cdot0=0
$$
So, $tv$ is also in your set.
Comment: beautiful thanks!
Answer: It's a bit cleaner if you write the original equation as $[5,-2,2] \cdot x =0$. Let $v,w$ be two such vectors. Obviously its a nonempty set as the zero vector satisfies this.
Then, you have $[5,-2,2] \cdot (a v + w) = [-5,-2,2] \cdot (a v) + [5,-2,2] \cdot w = a ([-5, -2, 2] \cdot v) + [5,-2,2] \cdot w = a (0) + 0 = 0$.
Thus, the set is a subspace because its closed under addition and scalar multiplication.
Answer: Let $A$ be the $3\times 1$ matrix
$$
A=\begin{bmatrix}5 & -3 & 2\end{bmatrix}
$$
and let $T:\Bbb R^3\to\Bbb R$ be the linear map $T(v)=Av$. Then
\begin{align*}
\ker T
&= \left\{v\in\Bbb R^3:T(v)= 0\right\} \\
&= \left\{v\in\Bbb R^3:Av= 0\right\} \\
&= \left\{(v_1,v_2,v_3)\in\Bbb R^3:5\,v_1-3\,v_2+2\,v_3=0\right\}
\end{align*}
That is, your collection is precisely $\ker T$. Now, since the kernel of a linear map is a subspace of $\Bbb R^3$ we get your result for free!

prove if the function is vector space


What do blank cells mean in the output of prior_summary in the brms package?

Question: The brms package is an R package for fitting Bayesian models using lme4-like syntax using Stan as the back-end. In the package, there is an option to fit a model without specifying any priors. In this case, according to the documentation, the package by default selects noninformative or weakly informative priors. In particular, according to a vignette, for population-level parameters the default is improper, and for group-level parameters the default for the SD is a Student's t.
As I fit a model using the default priors (having no prior information to speak of), I would like to see what the priors used actually were. The function prior_summary prints out the priors used after fitting the model.
However, I'm having trouble understanding the printout. Here it is, and the model is distance | cens(censored) ~ (segdist + tonedist | participant) + (1|variable):
                  prior     class      coef       group resp dpar nlpar bound
1   student_t(3, 4, 10) Intercept
2  lkj_corr_cholesky(1)         L
3                               L           participant
4   student_t(3, 0, 10)        sd
5                              sd           participant
6                              sd Intercept participant
7                              sd   segdist participant
8                              sd  tonedist participant
9                              sd              variable
10                             sd Intercept    variable
11  student_t(3, 0, 10)     sigma

As you can see, there many lines without any prior specified. I have thought of two reasons for this:
Possibility 1: Those blank lines were fit with improper priors. One reason for thinking this is that there is an all parameter in the function, which the documentation describes as follows: 'Logical; Show all parameters in the model which may have priors (TRUE) or only those with proper priors (FALSE)?'. After setting it to FALSE, all lines but 1, 2, 4 and 11 were removed, seemingly implying that they have improper priors.
However, this seems strange as the SDs had an improper prior, which they're not supposed to. Moreover, I'm not sure what lines like 3, 4, 5 and 9 actually are. 4 in particular cannot refer to the model's residual standard deviation since that's sigma, so it's not clear what a population-level sd is doing here.
Possibility 2: Lines 2 and 4 are headers, and imply that all the following lines have that prior. This seems to make sense in the case of line 2. Assuming that L in the class column is a stand-in for cor (correlations; I couldn't find an L class anywhere in the documentation), the prior makes sense. (I only have correlations in the participant groupings since for variable, there is just a random intercept.) Similarly, all the group-level effects have Student's t priors, which is expected behaviour.
This is what I feel is the more sensible guess. It doesn't explain the strange behaviour of all, though, since lines 3 and 5-10 would have proper priors, and should show up even when all=FALSE according to the documentation. Moreover, lines 5 and 9 are still slightly mysterious, although I could explain them away as 'sub-titles' which 'inherit' the prior from line 4 and 'pass them on' to the intercepts and slopes.
Are any of my guesses correct?
P.S. The doc for prior_summary is p.133 of this.
Answer: Possibility 2 is the correct one. Prior specification in brms is hierarchical in the sense that if a global prior for a parameter class is specified, it will be used for all coefficients of that class unless a coefficients has its own prior. In the latter case, the coefficient specific prior is used instead.
You may also extract the stan code from a fitted model object via stancode() to see what kind of priors are actually specified.
With regard to your additional questions:
Indeed L is related to the cor class, where L is the cholesky factor of the correlation matrix, which is what is actually used in the model.
I see that the documation of all is misleading. What it does is basically just to show those rows of the brmsprior object which have a non-empty prior column.
Comment: Thanks! Funnily enough, just a few days before you answered, I realised I could generate the Stan code for the brms models and found all out this my myself, heh :P


How to load jquery cookie library for anonymous user

Question: It seems  with login user jQuery Cookie added and worked correctly but when I visit my website with not login user(anonymous) then  I faced with TypeError: $.cookie is not a function. Why This happened and how can I attach jQuery cookie libraries for not logged in users by default?
Notes:
My theme is based on Drupal Bootstrap theme
Comment: By default no libraries are attached, like in D7 so in your theme or module you have to define library that depends on jquery and/or the cookie library and attach it.
Comment: in drupal 7 I got jquery.cookie.js file and put it in theme file and in theme.info add it.
but it seems it exists here and it is not require I download and add it like drupal 7, is this right? tell me what is the solution for this ?
Comment: https://www.drupal.org/developing/api/8/assets
Comment: @IvanJaros you mean I have to manually add it to my theme?
Comment: @IvanJaros can you provide some snippet as answer for this question?
Answer: For available Jquery and Jquery cookie for anonymous user, it should add it on  theme libraries dependencies , as example if your theme name is themename  on themename.libraries.yml add
in Drupal 8
  dependencies:
    - core/jquery.cookie

(Drupal 9 Deprecated core/jquery.cookie and will remove in D10.)
then in Drupal 9 and 10 use
  dependencies:
    - core/js-cookie

Thanks @Mr.Thi to mention this
Comment: Drupal 9 Deprecated core/jquery.cookie and will remove in D10. use core/js-cookie instead. https://www.drupal.org/node/3104677


Change font size on my website with Javascript

Question: This is my website http://natjecanje.interes.hr/
I want to have big A and small A and by pressing one of this "A" people can change all letters size on website, all elements need change font size, Thank you! :)
P.S. I did google search, but i didn't find what i was looking for.
Comment: What did you try? there is a big problem about that though: do you need to mantain proportions?
Comment: Take a look at this: [Change Text Size On Click With JavaScript](http://davidwalsh.name/change-text-size-onclick-with-javascript) although I would advise against this idea. You should never let the user control the display/layout of your website. Make the font something readable by default, and if the user wants the font to be bigger they can zoom in with their browser.
Comment: I try with this http://davidwalsh.name/change-text-size-onclick-with-javascript
But that code change only few thing on my website... i mean only few texts change their size with that script... i want all elements change size by klicking A+ and A-
Comment: @APAD1 Yeah I agree, but this is for my school project and that needs to be included :/
Answer: You need to use 2 differents CCS Style, a little JS script
Please Find below Javascript :

function setActiveStyleSheet(title) {
var i, a, main;
for(i=0; (a = document.getElementsByTagName("link")); i++) {
if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
a.disabled = true;
if(a.getAttribute("title") == title) a.disabled = false;
}
}
if (navigator.appName == "Microsoft Internet Explorer") {
window.resizeBy(0,-10);
window.resizeBy(0,+10);
}
}
function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

You can save this script in a fil named : changement_css.js
Now you need a first CCS style whit regular font-size, and another one with a different font size, and include it in your HTML file.

<link rel="stylesheet" type="text/css" href="normal.css" title="normal">

<link rel="alternate stylesheet" type="text/css" href="grand.css" title="grand">

Copy those line instead of your regular CSS
Then, use this code to call your JS after your CSS link :

Then, you need a button to change font-size :

Normal

Grand

I hope this could help you.
This message is translate from : http://forum.alsacreations.com/topic-6-11551-1-Boutons-AugmenterDiminuer-la-Taille-du-Texte-.html
Comment: cookies are not necessary, that si more code which I don't need in school project :/ and that JS I can't get till tomorrow :/
Answer: function promjenaVelicineFonta(inc)
{
var p = document.getElementsByTagName('p');
for(n=0; n<p.length; n++) {
if(p[n].style.fontSize) {
var size = parseInt(p[n].style.fontSize.replace("px", ""));
} else {
var size = 14;
}
p[n].style.fontSize = size+inc + 'px';
}

var span = document.getElementsByTagName('span');
for(n=0; n<span.length; n++) {
if(span[n].style.fontSize) {
var size = parseInt(span[n].style.fontSize.replace("px", ""));
} else {
var size = 14;
}
span[n].style.fontSize = size+inc + 'px';
}
}

I found a solution, here it is.
Answer: Do you mean font size (i.e. 10pt, 12pt etc) or font capitalisation (i.e. a or A)? I think you mean capitalisation based on the theme of your question (because you said 'big' and 'small')?
You can control capitalisation with CSS via the 'text-transform' property  (http://www.w3schools.com/cssref/pr_text_text-transform.asp).
You can use JavaScript to add remove CSS classes which is outlined in this thread: Change an element's class with JavaScript
Using this approach you could add remove CSS classes which use text-transform to change the capitalisation of the font.
Comment: I mean on font-size for ex. normal is 16 and by pressing A+ it will become 17px on whole page
Comment: OK, but the approach is still valid - just replace text-transform for font-size


Divergent Alternating Series

Question: Need help in finding an alternating series:

S = $\sum_{n=1}^{\infty}(-1)^{n+1}b_n$

where

$\lim_{n\to \infty}b_n = 0$
$b_n > 0$ but only $\forall n \ge 1$

such that S diverges
Comment: Hint: If $b_n$ is monotonic then $S$ converges, so try looking for non-monotonic $b_n$.
Answer: Try something like $b_{2n}=\dfrac{1}{2n}$ and $b_{2n+1}=\dfrac{1}{(2n+1)^2}$
Comment: wow that's smart.
Comment: but it is not alternating in this case
Comment: @user136895: It alternates when you multiply $b_n$ by $(-1)^{n+1}$.
Comment: @Henry yes, but if I take 2n instead of n as a variable here, I should multiply by $(-1)^{2n+1}$ which makes it not alternating
Comment: Take $b_n=n^{((-1)^n-3)/2}$ if you prefer.
Comment: @Henry and assuming $b_n=n^{((−1)n−3)/2}$ how am I sure the series is divergent?
Comment: Because the terms with one sign are effectively half a harmonic series with infinite sum while the terms with the other sign are effectively half a series with an finite sum
Answer: $$
b_n=\frac{1}{(n+1)^p+(-1)^n},\quad 0<p\le\frac12.
$$
Comment: But if $p=1/2$ then $b_{9} = -1/2 < 0$, contradicting one of the premises of the problem ($b_{n}>0$ for all $n$).
Comment: Thanks for the observation. I have edited may answer.
Answer: $(-1)^n \frac{n!!}{(n+1)!!}$
where
n!!= n(n-2)(n-4)...2 if n is even
n!!= n(n-2)(n-4)...3 if n is odd

Divergent Alternating Series


Fitting distribution to a given data

Question: I have a loss data arising out of Operation risk for some particular bank. The standard procedure for arriving at the capital charge w.r.t. Operational risk needs I fit some continuous distribution to this loss data.
Normally, I am able to fit some standard distributions. Once the distribution is identified, the same distribution is used to simulate future loss amounts.
My question is assuming the data is such that I am just not able to fit any statistical distribution, how do I deal with data to simulate the loss amounts. Is there any non-parametric method available to deal with such situations.
Kindly give me some advice.
Answer: You could resample the observed data (bootstrapping).
The problem with that is the real risk is in the extreme tail... and the sample doesn't really give you any information there (e.g. if you're interested in say a tail value at risk, for $\alpha = 0.005$ but you only have
a couple of hundred observations, then you have no information
about the behavior of the tail out that far.
On the other hand if you had many thousands of observations
it may not be a big problem.
You may get better benefit from investigating extreme value
distributions (which do deal with the extreme tail), but that's not quite nonparametric.
Answer: in banking you have to use also the external data. the regulators will insist that you don't come up with distribution strictly on your own dataset. so this becomes rather complicated. for instance take a look at change of measure approach. the paper also discusses the scenarios - another part of oprisk estimation.
Comment: I am extremely sorry for my late response. Thanks a lot for your guidance and the paper change of measure was really informative. Thanks again.

Fitting distribution to a given data


Fixed: Apache - PHP permissions to create / edit files and directories?

Question: I have a server on rackspace clouds... Fedora 13, and I installed apache, mysql.
Now, I have a piece of code (PHP) that my web app uses a lot. It works on my local machine running XAMPP, but when I upload this code on my rackspace server, it doesn't work:
$myFile = "textfile.txt";
$fh = fopen($myFile, 'w') or die("cant open");
$stringData = "CONTENT";
fwrite($fh, $stringData);
fclose($fh);

Really simple PHP code that should just work. However, it wont create the file, and if I upload textfile.txt file myself, and give the permissions myself to 0666 then it edits the file. However, this isn't ideal, since it still wont create new files (or directories) and chmoding each one by hand isn't feasible.
The owner of the file is root and the group is root
How do I make this work?
UPDATE - Fixed
I changed the owner to apache:apache doing: chown -R apache:apache /var/www/html
That seemed to work :)
Is there any security issues with that, or is that fine to have the user apache and the group apache?
Thanks!
Answer: Giving the apache user write access to your entire DocRoot is not ideal.  I usually configure things a bit differently.
First I identify a place where the files will be written - this can be in the DocRoot, or it can be in a separate location that's brought in with an Alias configuration in apache.
I create a group (usually called www-pub) and add the apache user to it.
Then I do
chown root:www-pub <directory>

and
chmod g+rwxs <directory>

which sets the setgid bit, meaning that any files or directories created under the top will have the same group as the parent directory, as well as being writable (and readable) by that group.
That way, you restrict where and what the apache user can get at, but still allow file creation and so forth in that area only.
Comment: Oh okay, I will try this. Why is having apache user write access not ideal? If I keep it this way, will it come back to bite me in the future?
Comment: Consider a scenario where there's a bug in apache that allows an attacker to write files as the apache user.  If you set your DocRoot and below as writable by apache, then at a minimum, the attacker could delete all your files, or deface your site.  Generally speaking, limiting what files a networked services can write to the bare minimum required is a good idea.
Comment: Oh, and I should also add that once you create the group and add apache into it, you'll need to restart apache to have it pick up the new group membership.
Answer: Put an info.php.
echo -e "<?php \nphpinfo(); \n?>" > info.php with appropriate ownership. Then update here so that I can check whether display_errors is ON or not.
Or simple turn ON display_errors and it will help you to troubleshoot.
Comment: Interesting, my display errors is off, I will try that.


Trying to make changes to page (post/article) text just before they are rendered

Question: I have a module that works fine but I cannot seem to get node_view hook to work - seems to never be called. As a test, I tried adding this to the dennis.module in sites/all/modules/dennis, but nothing happens (Drupal 7).
<code>function dennis_node_view($node, $view_mode, $langcode) {

  echo "AAAAAAAAAA";

  $node->content['body'] = array(
    '#markup' => "bonjour!"
  );

  $node->content['body'][0]['#markup']="hello";
}
</code>
Ultimately I need to find keywords in the content and replace with other text. So easy in WordPress and Joomla but after days of trying I have got nowhere with Drupal. Please help!
Answer: I can't help with the code, but could you use the String Overrides module for this?

Features:

Easily replace anything that's passed through t()
Locale support, allowing you to override strings in any language
Ability to import/export *.po files, for easy migration from the Locale module
Note that this is not a replacement to Locale as having thousands of overrides can cause more pain then benefit. Use this only if you need a few easy text changes.
Comment: Thanks for your reply, but I need to do this within my own module. It's part of how my module should work, not a one-off thing. I dont want to change the articles' text itself as stored in the database, just modify it on display.
Comment: Sadly, seems impossible to do this basic thing in Drupal so looks like I'll have to drop Drupal from supported CMSs.
Answer: you can do this by jQuery also :
<code>$('body').html($('body').html().replace('Original Text','New Text'));
</code>
Comment: Thanks for your reply, but I need to do this server-side.