Explanation of min_child_weight in xgboost algorithm

Question: The definition of the min_child_weight parameter in xgboost is given as the:

minimum sum of instance weight (hessian) needed in a child. If the
  tree partition step results in a leaf node with the sum of instance
  weight less than min_child_weight, then the building process will give
  up further partitioning. In linear regression mode, this simply
  corresponds to minimum number of instances needed to be in each node.
  The larger, the more conservative the algorithm will be.

I have read quite a few things on xgboost including the original paper (see formula 8 and the one just after equation 9), this question and most things to do with xgboost that appear on the first few pages of a google search. ;)
Basically I'm still not happy as to why we are imposing a constraint on the sum of the hessian? My only thought at the minute from the original paper is that it relates to the weighted quantile sketch section (and the reformulation as of equation 3 weighted squared loss) which has $h_i$ as the 'weight' of each instance.
A further question relates to why it is simply the number of instances in linear regression mode? I guess this is related to the second derivative of the sum of squares equation?
Answer: For a regression, the loss of each point in a node is
$\frac{1}{2}(y_i - \hat{y_i})^2$
The second derivative of this expression with respect to $\hat{y_i}$ is $1$. So when you sum the second derivative over all points in the node, you get the number of points in the node. Here, min_child_weight means something like "stop trying to split once your sample size in a node goes below a given threshold".
For a binary logistic regression, the hessian for each point in a node is going to contain terms like
$\sigma(\hat{y_i})(1 - \sigma(\hat{y_i}))$
where $\sigma$ is the sigmoid function. Say you're at a pure node (e.g., all of the training examples in the node are 1's). Then all of the $\hat{y_i}$'s will probably be large positive numbers, so all of the $\sigma(\hat{y_i})$'s will be near 1, so all of the hessian terms will be near 0. Similar logic holds if all of the training examples in the node are 0. Here, min_child_weight means something like "stop trying to split once you reach a certain degree of purity in a node and your model can fit it".
The Hessian's a sane thing to use for regularization and limiting tree depth. For regression, it's easy to see how you might overfit if you're always splitting down to nodes with, say, just 1 observation. Similarly, for classification, it's easy to see how you might overfit if you insist on splitting until each node is pure.
Comment: Hi @maw501: No problems, I can. Good answer Hahdawg !
Comment: So in a case of highly imbalanced data, what is your proposed range for the min_child_weight?
Comment: Thank you for the reply, I can't upvote you due to low reputation.
Comment: When in unbalanced dataset, should min_child_weight include weights as well? Thank you! @hahdawg
Comment: @MahdiBaghbanzadeh, it is difficult to give recommendations on such starting parameters, as differences in the datasets being used are vital to this determination. I would recommend performing a cross-validated grid search over either 1) randomly generated sets of hyperparameters or 2) a hand-picked set of probably useful hyperparameters to determine what helps from a predictive PoV.
Comment: Notice that min_child_weight doesn't work quite well in lightgbm (nor in xgboost AFAICS) https://github.com/microsoft/LightGBM/issues/5236
Comment: What is your recommendation value for the min_child_weight for classification case, I didn't see it so clear as in the regression case.
Answer: When there is little information, gradients of the loss function will tend to change slower, hence a smaller hessian. In the MLE framework, the negative of the hessian is known as the observed Fisher information. Ignoring the sign, a larger hessian will mean that more information is available. You don't want splits to happen when there is too little information. This shortage of information manifests in different ways for different loss functions, some of which were already described in another answer: smaller sample size for ordinary least squares regression and similar for logistic regression but now also weighted by the impurity $p(1-p)$ expected by the current model (so smaller and purer samples will be the less informative ones). Also notice that since the score of a leaf is related to $\frac{\sum grad}{\sum hess}$, a very small $\sum hess$ will make the ratio unstable, which is another way this lack of information manifests.
Comment: What is the score of a leaf? and why it is related to sum(grad)/sum(hess)


Define ranges for nested cross validation in SVM parameter tuning

Question: I have a question regarding parameter tuning in nested cross validation. I have already found some excellent questions and comments on the topic (Training with the full dataset after cross-validation?). However, they do not really address my question.
Suppose I want to do model checking for a SVM classification with a k-fold outer cross validation. For each fold of the outer cross validation I use a nested cross validation for parameter tuning (cost, gamma) of the respective SVM using grid-search. My question is how and where to define the parameter ranges for the grid-search then.
The options I see:
Option 1: Define a range once and use this same range to tune the method in each fold. If this is a valid approach, how would you set the parameter values ex-ante if you have no idea how they perform?
Option 2: Define an individual range in each fold and refine the search manually several times. Also, what would be a good criterion to stop refining the grid-search in each fold? This would also imply that you would need to do the tuning manually in each fold.
Any thoughts on what is a good approach here?
Answer: Assuming you're sticking with grid search (there are many other options, such as Random Search, Particle Swarm Optimization, CMA-ES, Simulated Annealing, etc).

Option 1: Define a range once and use this same range to tune the method in each fold. If this is a valid approach, how would you set the parameter values ex-ante if you have no idea how they perform?

This is the most common approach and quite robust to overfitting (with a coarse grid) when compared to more complex options. The a priori choice though is subject to heuristics. If you have the computational resources to investigate a huge parameter space, try to space well the evaluation points. Usually, both $C$ and $\gamma$ are searched in logarithmic scale.

Option 2: Define an individual range in each fold and refine the search manually several times. Also, what would be a good criterion to stop refining the grid-search in each fold? This would also imply that you would need to do the tuning manually in each fold.

This is much more laborious and quite prone to overfitting, but would save you from the issue in grid search when the optimal parameters fall out of your grid. A good stop criterion depends on your objective function, but you could define an a priori target objective, or stop when no improvement higher than a threshold is made between iterations.

Overall, if sticking to grid search, Option 1 sounds better because it's reproducible.


Symfony2 Assetic Image Path

Question: I'm new to Symfony and I'm trying to include and image (stored in the bundle Resources/public/images dir) in a twig template. When I render the page however the img tag has a non existent src path. I'm not sure what is going on, any advice would be appreciated greatly.
My Twig File
{% image '@XYZTestBundle/Resources/public/images/bananas.jpg' %}

{% endimage %}

The rendered image tag

There are no files in the /myproject/web/images/ directory
Comment: Relevant: http://stackoverflow.com/questions/9500573/path-of-assets-in-css-files-in-symfony2
Answer: found my answer here. I hope this helps someone else in the future
https://github.com/symfony/AsseticBundle/issues/41
Comment: Make sure that you accept this when the system allows you too.
Answer: have you try the command php app/console assetic:dump or php app/console assets:install ?
otherwise I think this link can help you :
http://symfony.com/doc/current/cookbook/assetic/asset_management.html
Answer: As Tali says, you should execute the command "assetic:dump" to dump your bundle assets in web folder.

Symfony2 Assetic Image Path


Thinking about the notes when playing a piece

Question: I've been a guitar player for 8 years now and I've started to learn the piano recently, which led me to get more into music theory and score reading.
When I learned a song on the guitar, I used to learn it by ear or from a tab and not from an actual music score. I've been trying to change this habit lately to get to know my instrument better.
Now when I learn a piece for the piano, I first dig into the score, learning the notes and chord progressions and how to play them on the piano. However as I get used to play the piece on the instrument I tend to use only my muscle memory and I don't think about what notes and chords I'm playing anymore.
My question is : would thinking about the notes and chords of a piece while I'm playing them make me a better musician ? Would it help me to learn music theory faster or to play the instrument better ? How do experienced musicians approach this kind of thing ?
Thank you for your answers !
tl;dr : would thinking about the notes and chords of a piece while I'm playing them make me a better musician ?
Comment: I don't see how distracting yourself with note names and chord types while performing will make you perform better, but knowing what is going in your practice will give you a better understanding of the music in general.
Comment: Shortest answer: It depends. Are you a concertmaster? Yes you need to think about theory in advance. Are you a bluesman? Definetely not.
Answer: This is a good question but I think a hard one to pin down a short answer for.  There reason is that there are several things going on in a musician's mind and body during the learning process and performance.
First I would say that you want to use muscle memory.  And, you do not want to be "thinking" about anything.  When you get to that level performance is like an out of body experience.  Thinking ruins this. So I'd say that if you are relying on muscle memory you are evolving to a better place.
At the end of the day music is about the pitches and rhythms.  I think that a musician, after much training, will be able to hear what they want to play in their head and then play it.  If this is what you mean by "thinking" about the notes then perhaps this is okay, it will be necessarily for improvisation.  For me I don't consider a piece performance ready until (1) I can play it through w/o the sheet music, (2) I can improvise a little without getting lost (even on classical pieces), and (3) I can sit quietly and hear the piece in my head without forgetting any part of it.
I should say that I am a guitarist but I think some of these points apply to any instrument.  I typically start with sheet music or by ear, if I know a song by ear then I can usually play it without much effort.  I have used TAB but prefer sheet music.  After enough time you get to the point where you can hear the music in your head as you read the sheet music (w/o touching the instrument).  This is valuable as it helps speed up the learning process.  Once you know what you expect to hear then figuring out the correct fingering is important.  Even piano players have multiple options for shifting as the move across the key board.  At some point you commit one to memory but as the song gets deeper into your subconscious you body will loosen up and you'll find that you can execute the piece with alternate fingering.  Now you are not relying so much on muscle memory but on the connection between your inner ear and your hands.
As for music theory, it's great and I recommend all musicians learn as much as they can.  But again, thinking ruins the process of performing.  Some of the greatest musicians (and very educated ones) have said in interviews about this topic that they don't think of anything when improvising, they just play what they hear in their head.  Knowing the structure of music helps again with figuring out the structure of a piece and the more you know the faster you pick up new pieces.  But when it comes to playing you can't be held down by the thought process.  It's more for analyzing what you see in a score or for making good decisions in your own compositions.  Not useful in performing a song.
Comment: "thinking ruins this": makes me think when you play something perfectly and then you start to think about what you're doing and suddenly everything fails and you're off tempo trying to figure out where fingers go next :)
Answer: The immediate job of playing THIS piece well ultimately comes down to muscle memory.
But learning the piece is a lot quicker when you recognise patterns.  Same difference whether they're heard or read.  And that's all 'theory' is really - codifying patterns that work.
Answer: Everything sounds fine. I just finished recording a fairly complex guitar piece and realised I was thinking about all kinds of everyday stuff while I was doing. I try to bring my mind more or less back to the job at hand, but not in a " concentration with effort" way. Relaxed mindfulness is great but if the mind briefyly pops off here and there it's OK. Kind of like in meditation.
The point of all the endless scales, chord patterns and general repetition of just about everything we do in pratice is to make things more or less automatic under performance conditions. You do not want to be trying to do musical maths while performing. Leave that for the practice room (as a stepping stone to actual music).
So the answer to your question is "probably not".
Answer: Yes, you become a better musician, if you are able to add the conceptual side of music into your thinking more closely. Having concepts and abstractions is essential for reasoning, and notation, notes and other theoretical tools can provide those. You need some kind of "objects" that have locations and names, for thinking about what things there are, where they are relative to each other, and how they interact with each other. And how things you do affect what you hear, and how both of them relate to the conceptual objects.
Location, composition, structure, behavior, ... What notes there are in a chord, where the notes are in notation and on your instrument, what happens if you change one of the notes, and what it would sound like if you did that. A lot of practice can help you build these concepts even without theory, but having e.g. names for the notes and their relationships can help a lot in reasoning and communication. How well do you think you might be able to calculate things, if you didn't know the names for numbers? When calculating in your head without tools, don't you think about numbers a lot?
The piano keyboard can provide an excellent locational map, because it's kind of a linear one-dimensional map of all pitches. Each pitch is only in one place, and they're spatially arranged in a logical way. The guitar is much more challenging in that regard, because every note or pitch is in many different locations at the same time. To make things worse, even the strings aren't placed at equal pitch distances - the G - B interval is four frets/semitones, when all the others are five!? Who thought that was a good idea!?! ;) (joking of course)
We can think that there are three dependent "axis": (1) what you hear, (2) what you do on the instrument, and (3) how this is written in notational concepts. All these things are interconnected.
Some people only need 1 and 2. They can operate their instrument and they know what it sounds like, so well that they can predict what any imagined actions would sound like before actually performing them... But they might not be able to read music, so they can only play by ear. Then again, someone else may only handle aspect 2: they are told what keys to press and strings to pluck, but it's mechanical and they cannot relate the actions with the sound or predict what sounds their actions might produce. Another person might be able to read music and obey the mechanical commands, so they would be operating in 2 and 3 only, but if they imagine sounds without having it in writing, they won't be able to perform it. In other words, they cannot play by ear. If you can handle all three, at least it won't make you a worse musician!
(A composer or arranger could be thinking purely in terms of notation and what it sounds like ... maybe in that case you could consider notation itself as the instrument.)
Answer: All answers you've got until now are great. In summary they are my learning concept even if they seem to be somehow contradictions.
Analyzing leads to understanding, concentration leads to meditation, muscle memory helps you play in the flow and and 150% of knowledge and improvisation helps you to take up the thread when you're dropped out.
Learning patterns, chord progressions, triads  and scales,  motives and abstractions in form of a lead-sheet will support sight reading, recognition and memory.

would thinking about the notes and chords of a piece while I'm playing them make me a better musician ?

You will be a better musician, but not necessarily a better performer.
Answer: An extremely thought-provoking question!
Starting with the actual wording: what constitutes a 'better musician'? One who can play a piece perfectly with no mistakes? Not necessarily. For me a 'better musician' is one who will do that anyway, but also be capable of bringing that music to life, putting their own slant to it, and obviously understanding where it's going, and why. So being able to play a piece flawlessly is only a part of this, and in itself not indicative of a 'better musician'. That muscle memory is of paramount importance, yes, but being able to 'bark at print' really doesn't belie much as far as 'musician' is concerned.
There are various levels concerned in learning and performing, and that's where the theory etc. comes in. If one understands the 'geography' of a piece, it becomes not only easier to learn, but easier to remember and also becomes better to perform with understanding.
In the early stages of learning a piece, there's a lot of importance in knowing what the notes are, how long they last, and where they all fit together. That's the time for conciously knowing the names, etc. Not during a performance, though. That's at a completely different level, where perhaps muscle memory has been established, so quoting note names at that stage is unneccessary. Muscle memory is all very well, but during a performance, it still needs to be controlled.
That's where the 'better musician' comes in - controlling the music and how it's played at that moment. Changing the dynamics on the fly - obviously in a lot of styles of music the notes themselves are sacrosanct - so that's all that can be changed (that's why I prefer jazz...).
Referring back to tab for guitar - that's more restricting, as it tells where each note must be played: not allowing the player to have the control that being a guitarist affords. That's one big difference between guitar and piano - on the latter, there's little choice!
So - in the practice section of learning a piece, yes, get to know notes, chords and their interactions. In performance, a few of those facets may be useful to you - as landmarks, if you like - but at that point, you should be at a completely different (higher) level, and mentally be in a very different place.
Think about it - how many times when you read this answer did you actually think - oh yes, that's three consonants together in that word, for example?
Answer: You talk about what happens when you become familiar with the piece, but I know a professional pianist who is a good sight-reader, and she doesn't even work out the individual notes when she first sees them. She sees a chord, a harmony, a rhythm, not a collection of 120 dots on the page that need to be analysed and deciphered. I wish I could do that! But I think that should be your aspiration. After all, that's the way we read English text.
Answer: "Muscle memory."  I hate the phrase.  It is misused.  Your muscles have no memory, it is your brain which controls them.  When you play the piano, it is not the muscles in your hand playing, it is your brain. If we swapped brains, once our brains figured out the new measurements, our techniques would transfer, too.  Like a teenager who trips over the floor or falls walking up stairs, it is because their tendons and bones grew and the brain needs to figure out the new measurements.  The kid isn't clumsy, his brain is in a new body.  That is why you never forget how to ride a bike despite not "practicing" for years, getting fat, getting skinny, growing or getting old.  Your brain has hardwired how to balance and commands the ancillary muscles to make it so.  The muscles are just the engine.  If you stood on one leg and looked at your foot, ankle and calves, you will see (if you have low body fat) your brain using your muscles and tendons to maintain balance.  Your muscles are not doing that, your brain is.  Likewise, when you play the piano, it is not your finger nor hands but, your brain. This is where many teachers and students go wrong because they focus on brow beating the fingers into submission when really they should cultivate proper movement from the mind where movement originates.
When you first touch a piano, your brain hard wires or makes neural pathways  on how to use the muscles and tendons to make a bone strike a key.  That is so called muscle memory. So, you had better make that first movement correct and ergonomic because if there are any improper movements associated, those are hard wired, too. This is why some people have lifetime technique issues because it is already in the brain to be that way.  Improper movement is very difficult to eradicate for other reasons I won't get into.
Never "memorize" a song by playing it mindlessly a hundred times.  That is not memory at all.  It is your ear, patterns and guesswork.  If you don't maintain playing it, it will atrophy and you will forget it.  It is not memory because if you get nervous or loose your place, it is gone and so is your performance.  I've seen it a hundred times.  It literally means you do not know what you are doing and are guessing.
So per your question what to think about. There are several skills which must come together and crosspollinate with one another.  I advocate that you forget your letter training and instead think of notes in the scale as numbers.  CEG is absolute but 135 can be anything.  It can be FAC, GBD, BbDF . . . . Playing by numbers is also the secret to sight transposing, improvisation and "memorization." NONE of it is thoughtless or hocus pocus.  It is all in the brain.
Then, train your ear to know what the numbers sound like.  For instance, I just know that Blue Moon starts on the 5th.  Mary had a Little Lamb on the 3rd.  Joy To the World 8th.  Happy Birthday on the 5th. Star Wars is 1 to 5. Once you train your brain and ear to know any degree of the scale, you will just know what the notes are and never have to memorize or see the music because your brain's ear will know how the song goes and your brain will just know what the notes are. Like spelling.  You know the basic rules, how to sound out, the alphabet and if you heard the word cat, catacomb, catatonic . . . you will either just know how to spell it or your brain can figure it out based upon crosspollinated knowledge and calculations. You use your ear and knowledge of the alphabet to figure it out. You don't have memorized every word but you can figure them out.
When I memorize a classical piece, I will listen to it once to get it into my mind's ear, then play it to assess trouble spots, then go sit by the pool with the score and study the melody and its intervals.  I'll study the chord progression, voicing's, key changes and counterpoints.  Then after a few hours, without the score I will go play it.  Then back to the pool to study problem parts.  After doing this for a few days, the piece is mine.  It isn't memorized but, combined with my ear, being able to read in my mind what my brain hears, knowing where every note is going and I admit, a dab or rote tossed in, I will never forget the song. I can also sit down with pen and paper and write it out because my ear can see the score.
Pop songs or standards are different.  Since I "fake" those, all I need to do is hear the melody for my mind's ear to just know what the notes are.  Pick any note on the keyboard and play Happy Birthday. That first note will by your 5th so you now know what key you are in.  Then, don't guess, hear each note in your head and calculate what the notes are.  It is amazing how when people "hunt and peck" that melody, how many miss the octave. You should never make a mistake playing a melody that simple if you know what you are doing. If you make a mistake, you are guessing.
There is nothing mystical about making music. You can close your eyes and let rote take over and just regurgitate patterns or, know what you are doing, craft a solo, build to a climax, develop a mood or a variation. That is why the greatest musicians seemingly don't make mistakes because they know what they are doing and are not guessing.
When I teach students this method, I start with just focusing on 135 and use Mary Had a Little Lamb. Sing it.  It starts on the third.  Don't guess, know. Even if it takes time.  Do it away from the piano.  You should get these numbers: 3212333 222 355 3212333322321. Now go to the piano and start on the third tone of ANY key.  Don't guess, know.  YOU will know if you are cheating. One hour away from the piano is worth more than five hours at it.
Congratulations. You are combining brain, ear and fingers to "memorize" music and you are on your way not to be a musician but, an artist.
Also, the issue of technique is also in the brain, not a Hanon exercise or hours of practice.  You only need to hard wire proper movement unless you've already hardwired improper movement.
Answer:
...thinking about the notes and chords...

You didn't say it explicitly, but I assume you mean naming the tones/pitches and chords.
There are two basic naming approaches: absolute and relative.
Absolute names are letter and accidental for tones, like: Ab or C#. For chords the names are letter and accidental for the root and a chord quality, like Ab dominant seventh or C# minor.
Relative names place tones and chords into a tonality, a key and then use special terms and symbols to indicate relationship and functions. These relationships and functions are at the heart of harmony theory, and the terminology is much more complex that mere absolute names. For example:

Ab dominant seventh is the dominant chord in the key Db major.
C# minor is the relative minor of E major.
in the key of Db major the tone Db is the first scale degree and is named the tonic, the tone a half step below the tonic is the seventh scale degree and is named the leading tone, in the key of Db the leading tone is C, etc. etc.

To the extent that music works with harmonic patterns, knowing the relative relationships is helpful.

Thinking about the notes when playing a piece...

If you practice harmonic patterns, or analyze the harmony of pieces you are working on, I can't imagine how you would not think about the relative relationships. But, gradually, as things become familiar you will probably stop thinking about it.


Nuxt app cannot access one REST API, but all others work

Question: I have a web app with a Django backend and a NuxtJS front end running in docker. The backend uses REST framework JWT Auth to control access. My URLs file looks like this:
urlpatterns = [
path('api/auth/login/', obtain_jwt_token),
path('api/auth/verify/', verify_jwt_token),
# other urls here
]

Docker-compose.yml
version: '3'

services:
  backend:
    build: ./backend
    env_file:
     - deploy.env

    volumes:
      - .:/backend
    expose:
      - "8000"

  nginx:
    image: nginx:latest
    container_name: ng01
    ports:
      - "8000:8000"
    volumes:
      - ./backend:/backend
      - ./nginx:/etc/nginx/conf.d
    depends_on:
      - backend

  web:
    build: ./web
    ports:
      - "80:3000"
    links:
      - backend

These two methods work perfectly from postman when the docker container is running - I'm able to obtain and verify JWT tokens as expected.  However, when the front end calls these methods the login method still works perfectly, but the verify method fails. There are a number of other methods in my app that also work fine when called from the front end. Here is the error i get:
Error: connect ECONNREFUSED 127.0.0.1:8000

I find this really weird, as normally this message would imply some sort of CORS issue, but as the other methods work, I don't see how it can be the case here.
Here's how I'm calling the login/verify APIs from my front end (in my store.js file). Whenever the user refreshes the page, nuxtServerInit retrieves the token from the browser cookie and attempts to call the verify method to get the user profile in the front end
export const actions = {
  async refreshAuth ({ dispatch, commit }, token) {
    try {
      var { data } = await axios.post(endpoint + '/auth/verify/', { token })
      console.log(data)
      if (data.token) {
        commit('setUser', { token: data.token, profile: data.profile })
      }
    } catch (err) {
      console.error('refreshAuth failed due to an error')
      console.log(err)
    }
  },
  async nuxtServerInit ({ dispatch, commit }, { req }) {
    if (req.headers.cookie) {
      let { token } = cookie.parse(req.headers.cookie)
      await dispatch('refreshAuth', token)
    }
  },
async login ({ commit }, { email, password }) {
    try {
      var { data } = await axios.post(endpoint + '/auth/login/', { email, password })
      await commit('setUser', {
        token: data.token,
        profile: data.profile
      })
    } catch (err) {
      if (err.response.status === 401) {
        commit('setError', 'Invalid credentials')
      }
      if (err.response.status === 403) {
        commit('setUnverified', true)
      }
    }
  }
}

Comment: Can you post your docker-compose file, please? Maybe you're missing a `network_mode: "host"` in the compose file, because you need that to access from inside container to the host using the same localhost interface.
Comment: Have added it above now
Comment: Try adding `network_mode: "host"` to `backend` service
Comment: that breaks nginx: ``ng01       | 2018/05/24 12:32:52 [emerg] 1#1: host not found in upstream "backend:8000" in /etc/nginx/conf.d/nginx.conf:3
ng01       | nginx: [emerg] host not found in upstream "backend:8000" in /etc/nginx/conf.d/nginx.conf:3
ng01 exited with code 1.``
Comment: here's my nginx config: ``upstream backend {
  ip_hash;
  server backend:8000;
}

# portal
server {
  location / {
        proxy_pass http://backend/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  listen 8000;
  server_name localhost;
}
``
Comment: ok, i managed to get nginx going again. But the issue still occurs
Answer: Turns out it was a CORS issue. The login action was being called from a nuxt page, whereas the refreshAuth call was being made from nuxtServerInit. Bizarrely, django-cors-headers rejects the call when made by nuxtServerInit. The solution was to move the call to refreshAuth to the created() method of the default layout in nuxt


meteor 0.9 "list" shows "new versions of these packages are available" but "update" doesn't

Question: $ meteor list
...
 * New versions of these packages are available! Run 'meteor update' to update.
$ meteor update
Figuring out the best package versions to use. This may take a moment.
This project is at the latest release which is compatible with your
current package constraints.

See the simplified version below:

It seems like there's a "conflict" between the list command which is saying there are updates available, and the update command which can not find any... how can this happen?
Comment: My guess is you were using Meteorite and the migrate-app screwed up a bit, remove all packages and add then back and I think you'll be fine. make sure you add the right packages though (iron:router instead of cmather:iron-router and etc).
Comment: have you tried removing the packages and adding them back in?
Comment: Yup - I did that... but perhaps I didn't remove all of them...  I can try doing each.

but still, why the conflicting messages?
Comment: having a new version for an individual package is not the same as having a satisfying solution with all those packages upgraded. And remove `cmather:iron-router` and replace it with `iron:router`
Comment: @ErezHochman I think you're exactly right...  2 problems, outlined below.
Answer: The problem seemed to be the mrt migrate-app script, which was a convenient way to upgrade... but apparently left some cruft around.
problems

some of the packages were guessed a bit wrong... with cmather:iron-router when it should be iron:router and more like that... (wish there was some way to filter/vote/identify "good" packages)
some of the packages were added with version suffixes like @2.0.0 and those were holding up the update... so list says "there are updates available" but update doesn't get them...  <-- answer

solution
remove all packages, update, add them all back, without the version suffix...
pretty easy to do now that they are all single lines in the .meteor/packages file:
sed -e 's/^[a-zA-Z0-9]/meteor remove &/' .meteor/packages | sed 's/\@[0-9\.]*//g' > packages-rm.sh
sed -e 's/ remove / add /' packages-rm.sh > packages-add.sh
bash packages-rm.sh
meteor list  # should be empty
meteor update
bash packages-add.sh
meteor list


Interpolation of a 3D curve in space

Question: I'm doing a curious exercise. I have to find a method to interpolate the following ballistic trajectory in 3D space:
$$\left\{\begin{matrix}
x(t) = \frac{v\cos(\phi)\cos(\beta)}{k}(1-e^{-kt})+d_{0}\cos(\alpha)\\ y(t) = \frac{v\sin(\phi)\cos(\beta)}{k}(1-e^{-kt})+d_{0}\sin(\alpha)
\\
z(t) = \left ( \frac{v\sin(\beta)}{k}+\frac{g}{k^2}\right)(1-e^{-kt})-\frac{g}{k}t
\end{matrix}\right.$$
provided a set of data $(x_n,y_n,z_n)$.
Therefore, to do this I think that I should eliminate the dependence on the parameter 't' and write the curve in the form of two different functions of one variable: $z(x)$ and $z(y)$:
$$z(x)= \left( v\sin(\beta)+\frac{g}{k}\right )\frac{x-d_0\cos(\alpha)}{v\cos(\phi)\cos(\beta)}+\frac{g}{k^2}\log\left(1-k\frac{x-d_0\cos(\alpha)}{v\cos(\phi)\cos(\beta)}\right) $$
$$z(y)= \left( v\sin(\beta)+\frac{g}{k}\right )\frac{y-d_0\sin(\alpha)}{v\sin(\phi)\cos(\beta)}+\frac{g}{k^2}\log\left(1-k\frac{y-d_0\sin(\alpha)}{v\sin(\phi)\cos(\beta)}\right) $$
Is this actually the best way to set the problem before interpolating?
Comment: Generally not. Everything depends on which variables you retain (relatively ) "precise" and which subject to "fluctuations" (errors).
Comment: Could you give me an example, please?
Answer: This is just a preliminary note, following the comment above.
Start with a simple case.
Suppose the model is
$$
\left\{ \matrix{
  x(t) = a\,t + b \hfill \cr
  y(t) = c\,t + d \hfill \cr}  \right.
$$
and that you collected a sample $\{(x_n, \, y_n\}$ of data, supposedly synchronuously , at the same time $t_n$, but you do
not know the values of the $t_n$'s.
So you are going to interpolate  with a straight line.
But which linear regression model are you going to choose ?
Do you choose $x(y)$ ( $x$ dependent variable, $y$ explanatory variable) , or   $y(x)$ ?
Depending on the choice you'll get in general two different results.
Which model to choose depend on the amount (variance) of the "error" expected on $x$ and $y$, and on many other assumptions
you can make about its distribution (scedasticity in primis).
Refer to the Assumptions paragraph in the cited Wikipedia article.
If you can assume $y$ to be quite more "precise" than $x$, then you take $x(y)$, and viceversa.
But if they are comparable, then you shall pass to Total Least Squares Regression.
Refer for instance to this related post and various other on the subject.

Interpolation of a 3D curve in space


Which one is the cache value in the output of top?

Question: On the book I'm reading, DevOps Troubleshooting, it says that the last value in the swap line is the memory used for file caches (yellow) in Linux so subtracting it to the used memory (green) gives you the actual used RAM:

but when I run it on my machine, the output looks like it has a different format with buffer and cache now being on the same line:

Did the output of top change? Why is there a difference here?
I'm familiar with the concept of the Linux file cache, is buff/cache how much it's being used for the file cache in the latter screenshot?
Answer: The output format of top changed many years ago, in a major rewrite published in 2011, almost before DevOps was even a widely used term. It's strange that your book would use such an ancient output, unless it too is ancient. Now, the actual memory available to applications is called out in its own field. The memory used for cache and buffers is combined here.
Comment: 2011... wow... suddenly my trust in that book went down. Is 2153.5 the actual RAM available to apps in that screenshot? Why is it on the Swap line?
Comment: @Pablo Yes, it's the actual memory available to apps. And it's on that line because it wouldn't fit on the previous line.  Notice that the entry before it has a full stop instead of a comma.
Comment: Ahhh... I didn't notice the full stop. Thank you.


Include sonata menu in custom link with controller view

Question: I have this code in config.yml to add a item to the menu:
<code>    # PrestaSonataNavigationBundle
    presta_sonata_navigation:
        menu:
            with_description: false
            items:
                report:
                    roles:
                        - ROLE_SONATA_ADMIN
                    children:
                        reportes:
                            route: informe_show
</code>
This route, "informe_show", it's a route to an action on a controller with a custom view, it isn't display the menu with the  services items.
How can i do to include the sonata menu?
Answer: If you want to use the Sonata administration menu on your front layout, please have a look on this answer: show sonata admin menu in every page if the admin is logged in


What does C++11 consider to be a "thread"?

Question: C++11 has some notion of threads. For example, it defines a new storage specifier thread_local, and specifies that for variables with this storage specifier, "there is a distinct object or reference per thread" [basic.stc.thread].
What is considered to be a "thread" for this purpose? Is it only threads created using the standard thread library (i.e. those represented by std::thread objects)? What about threads created by other means (for example, by using pthreads directly on Linux)? What if I use a library that provides user-space threads - does each of those get its own copies of thread_local objects (I don't really see how that could be implemented)?
If the answer is "it's implementation-defined what is considered to be a thread for purposes such as thread_local", could someone give an example of how one well-known implementation defines this?
Comment: @texasbruce Java *now* has those keywords? They were there 17 years ago.
Answer: Only components from the thread support library count because of these quotes, or main which the standard states runs in its own thread of execution.

1 The following subclauses describe components to create and manage threads (1.10), perform mutual exclusion, and communicate conditions and values between threads, as summarized in Table 148.

The link to 1.10 implies that the threads being spoken about are these.

1 A thread of execution (also known as a thread) is a single flow of control within a program, including the initial ...

Therefore it seems to me threads only refer to the stdlib threads (meaning std::thread and anything the thread support library does internally). Of course thread_local in many cases could end up working with the native threads (especially when you consider on a specific system you don't usually have more than one choice for implementing threads)  but as far as I can tell the standard makes no guarantee.
Comment: @bames53 not sure how this applies to my answer, all I'm saying is that a "thread" in c++ is something that is implemented internally, `std::thread` is obviously one of those, but other things internally could produce a "thread". Even if internally the implementation uses lets say pthreads, and they end up working as a "thread" the standard makes know guarantee of this IMO.
Comment: Although this is true and implementations could technically get away with supporting `thread_local` only with `std::thread` this would be a poor quality implementation. So far the existing implementations are ensuring `thread_local` works with system threads and having `std::thread` pick up support for `thread_local` through the system thread library support.
Comment: Yes, I'm agreeing with you that the standard makes no guarantee about anything other than `std::thread`. I'm just adding that in practice and on implementations I know of `thread_local` will work with the system's threads and not just `std::thread`.
Comment: @bames53 yes most likely, as I said in my edit you don't exactly have a wealth of options when implementing this type of stuff
Comment: It's also understood that `main` is run on its own thread, wih its own `thread_local` variables even though there's no `std::thread` object for it.
Comment: 1.10/1 seems to make the opposite point from what you claim. It defines a thread as a single flow of execution *regardless* of why that execution is happening. It does not reference the library.
Comment: @Potatoswatter unless you have a different copy than me IT you added the " regardless of why that execution is happening.", anyway I feel like it's possible to interpret it either of our ways, at no point does the standard definitively say it either way, feel free to post an answer if you want
Comment: What part of my comment was a quote? Everything the standard says is without reference to other things unless it references them. That is invariably the correct way to interpret it.
Comment: @Potatoswatter I thought some of your comment was intended to be a quote for some reason
Answer: C++11 §1.10/1 defines the terms:

A thread of execution (also known as a thread) is a single flow of control within a program, including the initial invocation of a specific top-level function, and recursively including every function invocation subsequently executed by the thread. [ Note: When one thread creates another, the initial call to the top-level function of the new thread is executed by the new thread, not by the creating thread. — end note ]

The italicized terms indicate that this is definitive. You could argue that this definition is mathematically deficient, because each function invocation defines a new thread, but that's just obviously wrong. They mean maximal single flow of control, otherwise the non-normative note would cancel the effect of the normative "recursively including" text.
From the standpoint of the core language, it is merely incidental that std::thread causes such a thing to exist.

What if I use a library that provides user-space threads - does each of those get its own copies of thread_local objects (I don't really see how that could be implemented)?

There is no way to write such a library without kernel calls. In all likelihood all threads in your process are already represented a high-level abstraction such as pthreads, just to satisfy the kernel. The C++ standard library is likely written against the native threading library to "just work" without additional glue.
For example, thread_local objects are initialized at first access rather than when each new thread starts, so the compiler just has to insert a query based on pthread_self to access and perhaps initialize. Initialization would register a destructor with the pthread_cleanup facility.
What is implementation-defined here is whether the pre-existing native library is compatible with C++. Supposing they provide that, and it's something customers would tend to want, all other threading libraries built atop it will be automatically compatible barring some other conflict.
Comment: So to actually answer the OP's question you believe anything that creates a new flow of control in a program is guaranteed to work with something like `thread_local` by the standard
Answer: The standard does not describe how threads produced by other libraries and system calls behave.  They are, as far as the standard is concerned, undefined in their behavior.  There is no other way, within C++ proper, to create multiple threads: such libraries or system calls do things that are not standardized by the C++ standard.
Now, each such library and system call will behave in ways defined by its own specs.  Quite often, the C++ std::thread will even be built on top of such libraries or system calls.  How exactly the interaction works is not specified.


Javascript, ou outra forma, de apagar certa linha do codigo html, e criar outra no mesmo local da anterior

Question: Preciso saber se existe alguma função que selecione uma linha específica do código HTML para apagar ela, quando disparar à função, e algo para criar uma nova linha de código no mesmo lugar dá linha apagada, meio que para substituir mesmo.
OBS: Literalmente é só uma linha de code :)
Comment: O que você quer dizer com linha? Poderia colocar um exemplo ?
Comment: Você quer apagar um texto/conteúdo ou quer apagar um elemento HTML?
Answer: O HTML é constituído por elementos do DOM (Document Object Model) e não por linhas. É representado por uma árvore de nós, onde um nó pode ser selecionado e modificado via script (ex., JavaScript). Cada nó possui uma posição hierárquica dentro da árvore. O que você precisa fazer é saber qual a posição do elemento que você quer manipular.
Por exemplo, se houver vários elementos
 (parágrafo) no HTML, cada um possui um índice (index) na árvore (também possui um elemento-pai, pode ter elementos-irmãos e até elementos-filhos).

Todos os elementos da página possuem um avô e um pai:  e ,
  respectivamente.

Caso o elemento que você quer modificar possua um id, fica bem mais fácil selecioná-lo, porque um id é único na página e você pode ir direto a ele sem se preocupar com a posição dele na árvore.
O exemplo abaixo mostra a substituição de um parágrafo
 por um link :

function funcao(){

   // apagar e substituir o primeiro parágrafo

   // por um link
   var el = document.querySelectorAll("p")[0]; // seleciono o primeiro

   var a = document.createElement("a"); // crio o elemento
   var t = document.createTextNode("Novo link"); // crio o texto do
   a.appendChild(t) // adiciono o texto no
   a.setAttribute("href","pagina.html"); // adiciono href no
   el.parentNode.insertBefore(a, el.nextSibling); // insiro o novo elemento  após o

   el.outerHTML = ''; // apago o

}

Lorem Ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed laoreet odio at interdum euismod. Praesent suscipit tortor ex, vel vestibulum eros rhoncus eu. Phasellus efficitur dui ut tincidunt eleifend.

Nullam finibus facilisis risus, nec vehicula tortor efficitur a. Suspendisse at nibh elementum elit volutpat fermentum vel a eros. Vestibulum at condimentum nisi.

<input type="button" onclick="funcao()" value="Clique aqui para substituir">


Use Lagrange multipliers to find the minimum distance from the curve or surface to the indicated point

Question: The given curve is the parabola $y=x^2$ with point $(0,9)$.
We want to minimize $D^2=f(x,y)=x^2 + (y-9)^2$ with the constraint equation $g(x,y) =y-x^2=0$.
After finding the gradients of both $f$ and $g$ we get:
$$2x = -2 \lambda x$$
$$2y-18=\lambda$$
Plugging in $\lambda$ from our $y$ equation into the $x$ equation yields that either
$$x=0$$
$$y=\frac{19}{2}$$
When $x=0$ then $y=0$ and when $y=\frac{19}{2}$ then $x=\pm\sqrt{\frac{19}{2}}\space$,we cant to minimize the equation so we use the negative.
Using these values of $x$ and $y$ in $D^2$ gives
$$D^2=\sqrt{\frac{39}{4}}$$
$$D=\frac{\sqrt{39}}{2}$$
But this is where I run into a problem because $\frac{\sqrt{39}}{2}=9.75$ which is greater than my original point. So how do I find the lowest point then?
Comment: solving the equations gives you, $y=0, x=0, \lambda=-18$. Also, note that because this is not a convex problem, then KKT conditions are not sufficient. So, you should check all the KKT points to find the minimum.
Comment: I haven't learned what KKT points are.
Comment: I meant all the solutions of that satisfy your equations.
Answer: $f(x,y,\lambda) = x^2 + (y-9)^2 - \lambda (x^2 - y)\\
\frac {\partial f}{\partial x} = 2x (1-\lambda) = 0\\
\frac {\partial f}{\partial y} = 2(y-9) +\lambda = 0\\
\frac {\partial f}{\partial y} = y-x^2 = 0$
From the first equation, either $x = 0$ or $\lambda = 1$
If $x = 0$ then $(0,0)$ is a critical point.  It looks like this is a local maximum.
If $\lambda = 1$
$2y - 17 =0\\
y = \frac {17}{2}$
This appears to be your error.
$(\sqrt \frac {17}{2} , \frac {17}{2})$ is a critical point.
Plugging this into:
$D^2 = x^2 + (y-9)^2\\
D^2 = \frac {17}{2} + (\frac {17}{2}-9)^2\\
D^2 = \frac {35}{4}$
$D$ is slightly less than $3$


Android NDK pulling in JNI stuff with gradle and jitpack?

Question: I have an android library project on github. Its basically the java port of the popular BASS library: http://www.un4seen.com/
All the .so files are in a directory /jniLibs found in src/main.
I can make a release and am able to grab it using jitpack. I know its working because I have a test class in the library and I can see it in autocomplete after adding the library to my project via gradle.
Though that class works, I can seem to use any of the BASS methods. Why would this be?
Answer: Apparently when using the ndk you need to be careful of package names.
It was undocumented, but for my case, the BASS library on needed to have a specific package name to be accessed. In this case it was com.un4seen.bass


Repetição de Números

Question: Bom pessoal eu tenho a seguinte situação: tenho uma conta na tela tipo assim: x + 1 = 2, o usuário tem que clicar na opção que completa a conta e eu tentei usar este código da minha pergunta anterior, porém não funcionou. No caso, tenho que deixar um valor de x "fixo", e acaba repetindo este número.
Segue o código que tenho:
            esc = numeroDecisao();
            res1 = numeroAleatorio();
            res2 = numeroAleatorio();
            resultadoConta = res1 + res2;

            Set<Integer> respostas = new HashSet<>();
            respostas.add(resultadoConta);

            while (respostas.size() < 4) {
                int a = numeroAleatorio();
                int b = numeroAleatorio();
                respostas.add(a + b);
            }
            respostas.remove(resultadoConta);
            Iterator<Integer> iterator = respostas.iterator();

            if (esc == 1) {
                op1.setText(Integer.toString(res1));
                op2.setText(Integer.toString(esc == 2 ? resultadoConta : iterator.next()));
                op3.setText(Integer.toString(esc == 3 ? resultadoConta : iterator.next()));
                op4.setText(Integer.toString(esc == 4 ? resultadoConta : iterator.next()));

            } else if (esc == 2) {
                op1.setText(Integer.toString(esc == 1 ? resultadoConta : iterator.next()));
                op2.setText(Integer.toString(res1));
                op3.setText(Integer.toString(esc == 3 ? resultadoConta : iterator.next()));
                op4.setText(Integer.toString(esc == 4 ? resultadoConta : iterator.next()));

            } else if (esc == 3) {
                op1.setText(Integer.toString(esc == 1 ? resultadoConta : iterator.next()));
                op2.setText(Integer.toString(esc == 2 ? resultadoConta : iterator.next()));
                op3.setText(Integer.toString(res1));
                op4.setText(Integer.toString(esc == 4 ? resultadoConta : iterator.next()));

            } else if (esc == 4) {
                op1.setText(Integer.toString(esc == 1 ? resultadoConta : iterator.next()));
                op2.setText(Integer.toString(esc == 2 ? resultadoConta : iterator.next()));
                op3.setText(Integer.toString(esc == 3 ? resultadoConta : iterator.next()));
                op4.setText(Integer.toString(res1));
            }
            num2.setText(Integer.toString(res2));
            txt_resultado.setText(Integer.toString(resultadoConta));

No caso, o res1 seria o x da conta x + 1 = 2.
Comment: Coloque o seu código atual aqui ao invés de apenas apontar para a pergunta anterior como se nada tivesse mudado. O link era para ser apenas um detalhe para explicar melhor o contexto para quem fosse responder a pergunta, não era para o link ser praticamente a pergunta inteira.
Comment: @VictorStafusa, obrigado pela a dica, estou aprendendo ainda a como realizar as perguntas devidas no stack. Realizei a edição e coloquei o trecho do código.
Answer: Primeiro, você não precisa desses if (esc == 1), if (esc == 2), etc. O código da resposta da pergunta anterior foi feito para que você tivesse o mesmo código não importando qual é o esc. Vou repetir o que coloquei na minha resposta da pergunta anterior:

Sempre prefira usar matemática do que um monte de ifs com código copiado e colado.

Segundo, vamos simplificar um pouco a geração de números aleatórios:
private static final Random RND = new Random();

private int numeroAleatorio() {
    return RND.nextInt(set * 10) + 1;
}

Terceiro, que você tem resultadoConta = res1 + res2 porque você estava fazendo uma conta do tipo X = A + B, onde A e B eram os números dados e X era o valor desconhecido a ser respondido. Agora você tem contas no formato X + A = B, e provavelmente vai ter outras tais como X - A = B, A + X = B, A - X = B, A * B = X, X * A = B, A / X = B, AX = B, etc.
Para modelar todas essas contas possíveis, primeiro use uma classe para representar a operação:
public class Conta {
    private int a;
    private int b;
    private int x;
    private String formato;

    public Conta(int a, int b, int x, String formato) {
        this.a = a;
        this.b = b;
        this.x = x;
        this.formato = formato
                .replace("A", String.valueOf(a))
                .replace("B", String.valueOf(b));
    }

    public int getA() {
        return a;
    }

    public int getB() {
        return a;
    }

    public int getX() {
        return x;
    }

    public String getFormato() {
        return formato;
    }

    public String getTermo1() {
        return formato.split(" ")[0];
    }

    public String getOperacao() {
        return formato.split(" ")[1];
    }

    public String getTermo2() {
        return formato.split(" ")[2];
    }

    public String getResultado() {
        return formato.split(" ")[4];
    }
}

E então use uma interface para representar todos os tipos de operações possíveis e gerar os números de acordo:
public interface ModeloConta {
    public Conta fazerConta();
}

E com essa interface, gere um monte de implementações em um array:
ModeloConta[] modelos = {
    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int a = numeroAleatorio();
            int b = numeroAleatorio();
            int x = a + b;
            return new Conta(a, b, x, "A + B = X");
        }
    },

    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int a = numeroAleatorio();
            int x = numeroAleatorio();
            int b = a + x;
            return new Conta(a, b, x, "A + X = B");
        }
    },

    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int a = numeroAleatorio();
            int x = numeroAleatorio();
            int b = a + x;
            return new Conta(a, b, x, "X + A = B");
        }
    },

    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int a = numeroAleatorio();
            int x = numeroAleatorio();
            int b = a * x;
            return new Conta(a, b, x, "A * X = B");
        }
    },

    new ModeloConta() {
        @Override
        public Conta fazerConta() {
            int x = numeroAleatorio();
            int a = numeroAleatorio();
            int b = x - a;
            return new Conta(a, b, x, "X - A = B");
        }
    }
};

Se você estiver usando Java 8 ou superior, esse código acima pode ser simplificado para isso:
ModeloConta[] modelos = {
    () -> {
        int a = numeroAleatorio();
        int b = numeroAleatorio();
        int x = a + b;
        return new Conta(a, b, x, "A + B = X");
    },

    () -> {
        int a = numeroAleatorio();
        int x = numeroAleatorio();
        int b = a + x;
        return new Conta(a, b, x, "A + X = B");
    },

    () -> {
        int a = numeroAleatorio();
        int x = numeroAleatorio();
        int b = a + x;
        return new Conta(a, b, x, "X + A = B");
    },

    () -> {
        int a = numeroAleatorio();
        int x = numeroAleatorio();
        int b = a * x;
        return new Conta(a, b, x, "A * X = B");
    },

    () -> {
        int x = numeroAleatorio();
        int a = numeroAleatorio();
        int b = x - a;
        return new Conta(a, b, x, "X - A = B");
    }
};

Coloque no array todos os modelos de contas que você tiver. Tendo então os modelos, você adapta o código anterior para escolher um modelo e usar. Também vou usar um array de TextView caso você queira mais do que quatro respostas e também para ter certeza que você não vai querer substituir isso por um monte de ifs horrorosos:
TextView[] opcoes = {op1, op2, op3, op4 /*, op5, op6, op7, quantas você quiser */};
ModeloConta modeloEscolhido = modelos[RND.nextInt(modelos.length)];
Conta conta = modeloEscolhido.fazerConta();
int respostaCerta = conta.getX();
esc = RND.nextInt(opcoes.length);

Set<Integer> respostas = new HashSet<>();
respostas.add(respostaCerta);

while (respostas.size() < opcoes.length) {
    respostas.add(modeloEscolhido.fazerConta().getX());
}
respostas.remove(conta.getX());
Iterator<Integer> respostasErradas = respostas.iterator();

for (int i = 0; i < opcoes.length; i++) {
    int valor = esc == i ? respostaCerta : respostasErradas.next();
    opcoes[i].setText(Integer.toString(valor));
}

num1.setText(conta.termo1());
sinal.setText(conta.operacao());
num2.setText(conta.termo2());
txt_resultado.setText(conta.resultado());

As variáveis res1, res2 e resultadoConta devem então ser deletadas. O método numeroDecisao() também deve ser deletado. Acrescentei uma TextView chamada sinal para guardar o sinal desejado para a operação.
Aliás, se preferir, você pode substituir num1, sinal, num2 e txt_resultado por uma única TextView que vai ter a conta inteira. Nesse caso, você trocaria isso:
num1.setText(conta.getTermo1());
sinal.setText(conta.getOperacao());
num2.setText(conta.getTermo2());
txt_resultado.setText(conta.getResultado());

Por isso:
txtConta.setText(conta.getFormato());

Onde txtConta seria a TextView na qual você mostraria a conta (vai aparecer como also semelhante a 5 + X = 12). Tendo essa nova TextView, você deletaria as TextViews num1, num2, sinal e txt_resultado. Também poderia deletar os métodos getTermo1(), getOperaco(), getTermo2() e getResultado() da classe Conta.

Repetição de Números


Is an irreducible component of analytic set just a connected component of its regular points?

Question: Say $Z$ is an anlatyic set in a complex manifold $M$. Is it true that irreducible components of $Z$ are just the closures in $Z$ of connected components of $Z\backslash sing(Z)$?
I need this for a paper I am reading. In general, can someone reccomend a good book about complex analytic geometry? I don't want to waste a lot of time figuring these things out without at least a good source for all these notions.
Thank you!
Benny
Answer: [1] The answer to your question is yes.There is an important theorem on decomposition analytic subset into irreducible components:(Cf E.M.Chirka's Complex Analytic Sets chapter 5.4.Theorem1)
Let $A$ be an analytic subset of a complex manifold $\Omega$.Then:
(1)every irreducible component $A$ has the form $\overline S$,where $S$ is a connected component of $regA=A\backslash singA$;
(2)if $reg A=\bigcup_{j\in J}S_j$ is the decomposition into connected connected components ($J$ is finite or countable, $S_j\bigcap S_k=\varnothing$ for $j\not= k$),then $A=\bigcup_{j\in J}\overline {S_j}$,and this is the decomposition of $A$ into ieeducible components;
(3) the decomposition of $A$ into irreducible components is locally finite,i.e.for every compact $K\subset\Omega$ there is only a finite (or empty) set of indices $j\in J$ such that $K\bigcap\overline {S_j}$ is nonempty.
[2] Demailly's Complex Analytic and Differential Geometry is wonderful.I also recommend E.M.Chirka's Complex Analytic Sets,it's worth reading and full of a lot of classical theorems.
Comment: Thank you for your answer! In the case $Z$ is an algebraic set in $M=\mathbb{C}^{n}$, is it true that the irreducible components in the sense of zariski topology coincide with the irreducible components in the analytic sense?


Optimize shuffling an ArrayList

Question: The problem is this: I need to shuffle a deck of cards (ArrayList or array of 52 ints (0 to 51)). I also need to do this 200,000 times and on an Android processor. Please help me optimize this, because this is taking an unbelievable amount of time even on a more high end device (up to 10 seconds).
Methods I've tried:
1) Shuffle the deck with Collections.shuffle and draw first cards I get (takes 8 seconds on Nexus7):
for(long i = 0; i < ITERATIONS; i++) { // ITERATIONS is 200,000
        ArrayList<Integer> fakeDeck = (ArrayList) deck.clone(); // deck is the sorted deck.
        Collections.shuffle(fakeDeck);
        int card1 = fakeDeck.get(0);
        int card2 = fakeDeck.get(1);
        int card3 = fakeDeck.get(2);
        int card4 = fakeDeck.get(3);
        int card5 = fakeDeck.get(4);
        // do something with cards.
}

2) Randomly pick cards from an unshuffled deck (a bit better, takes 5 secs):
for(long i = 0; i < ITERATIONS; i++) { // ITERATIONS is 200,000
        ArrayList<Integer> fakeDeck = (ArrayList) deck.clone(); // deck is the sorted deck.
        int card1 = pullCardFromDeck(fakeDeck);
        int card2 = pullCardFromDeck(fakeDeck);
        int card3 = pullCardFromDeck(fakeDeck);
        int card4 = pullCardFromDeck(fakeDeck);
        int card5 = pullCardFromDeck(fakeDeck);
        // do something with cards.
}

// pullCardFromDeck is:
private int pullCardFromDeck(ArrayList<Integer> deck) {
    int randomNumber = new Random().nextInt(deck.size());
    int card = deck.get(randomNumber); // get a random card.
    deck.remove(randomNumber); // remove the card from the deck.
    return card;
}

Comment: Is it really necessary to clone the deck each time?
Comment: `Random`s aren't cheap to make. Move it outside of your loop and pass it in to your method.
Comment: `I also need to do this 200,000 times and on an Android processor.` Why? Also, why shuffle the whole deck? Can't you just pull random cards from it when they're needed?
Comment: *"as the cloning takes a meager amount of time compared to the shuffling"* And you determined this through benchmarks...right?
Comment: In the 1st example it's not necesssary, I can reshuffle the same deck, in the second example I need to, becuase I remove cards. But that really is not the issue, as the cloning takes a meager amount of time compared to the shuffling.
Comment: I need to calculate poker odds exhaustively. I can pull random cards, the second example does just that.
Comment: @VM Maybe explain why 200,000 times. Unless you are running a Monte Carlo simulation of something I can't find a reason.
Comment: Well yes, I'm dealing 200,000 different community boards to figure out how many times a player wins.
Comment: I'm using System.nanoTime(); and compounding the time my method is taking, removing the cloning from the timing does not impact time taken severely.
Comment: Can you show what exactly you're measuring? Perhaps you could get rid of cloning the array and removing the elements. It may be faster just to keep a list of up to 5 indices of the cards used in a single draw (effectively marking the cards as already in use instead of actually removing them from the deck).
Answer: If you only need five cards you could just shuffle 5 cards each time. This could be 20x faster.
public static void main(String... ignored) {
    int[] cards = new int[52];
    for (int i = 0; i < cards.length; i++) cards[i] = i;

    long start = System.currentTimeMillis();
    int runs = 1000000;
    for (int i = 0; i < runs; i++) {
        shuffleN(cards, 5);
        int card1 = cards[0], card2 = cards[1], card3 = cards[2], card4 = cards[3], card5 = cards[4];
    }
    long time = System.currentTimeMillis() - start;
    System.out.printf("Took %.3f seconds to shuffle %,d times%n", time / 1e3, runs);
}

private static final Random RND = new Random();

public static void shuffleN(int[] numbers, int count) {
    for (int i = 0; i < count; i++) {
        int r = RND.nextInt(numbers.length - i) + i;
        if (i == r) continue;
        int tmp = numbers[i];
        numbers[i] = numbers[r];
        numbers[r] = tmp;
    }
}

prints
Took 0.115 seconds to shuffle 1,000,000 times

Comment: I need to draw 5 cards from a 52 card deck.
Comment: @VM So you would make the count 5, and the top 5 cards would be random, even if the rest of the cards would not be very random. i.e. this is like a shuffle but stops after the first 5 cards, making it possibly 10x faster.
Comment: I don't understand, won't you be interchanging the same 5 cards every time? That would yield the same result. Sorry I'm slow.
Comment: @VM This is swapping five random cards from the whole deck and placing them at the top.  The first five will be random, the remaining 47 will be the rest.  The next time, 5 are chosen at random from the 52, etc.
Answer: You may be better off considering using an index of int[] that you shuffle and use to access a single deck.  For getting random entries, set the chosen index to zero (and when you get a random number see if the value is already zero, if it is choose a new card)

Optimize shuffling an ArrayList


Reference a MovieClip symbol later on in the timeline

Question: I've been trying to reference a Library symbol that is introduced in Frame 2 by using it's instance name, using timeline scripting only.
I am trying to make a colour picker MC symbol for multiple re-use. Firstly I drew a small button on the stage and converted to MC, called textColourPicker of type CPicker (the class used to represent this MC in the Library). Inside this MC I have a button symbol called pickerButton
In the first frame of textColourPicker MC, I placed the code:
stop();
pickerButton.addEventListener(MouseEvent.CLICK, doShowPicker);

function doShowPicker(e:MouseEvent):void
{
    gotoAndPlay(2);

}

In Frame 2 of the textColourPicker instance, a new MC appears with instance name pickerBox of type pickerBMP. The pickerBox is then scale tweened until Frame 14 at which point I have the code:
stop();
pickerBox.addEventListener(MouseEvent.MOUSE_OVER, cursorToColour);

Except I always get the error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at CPicker/frame14()[CPicker::frame14:10]

Here CPicker is referring to the Linkage name of the textColourPicker MC.
So textColourPicker is Instance of: myPicker, linkage name: CPicker
and pickerBox is instance of: pickerBMP, linkage name: pickerBMP (residing inside the textColourPicker MC.
Why is not allowing me to reference the pickerBox MC on Frame 14 of the textColourPicker?
This graphic of the textColourPicker timeline may help illustrate all this (note the two timeline scripting icons, first in Frame 1 last in Frame 14:
Comment: in the 14th frame what is the the pickerBMP layer object name?
Comment: @Jevgenij Dmitrijev - it is the same name that it always is which is `pickerBox`
Answer: I wrote a post for InsideRIA about doing exactly what you are trying to do.  It is archived here:
http://www.developria.com/2010/04/combining-the-timeline-with-oo.html
It seems to me that your best bet is the getter/setter method, since you have a keyframe at the end where a new value will be inserted into the variable's value.
Example code for both methods is available here http://flexdiary.blogspot.com/2010/04/sample-code-for-oop-timeline-insideria.html
HTH;
Amy
Comment: Hi Amy, thanks for the answer...! :) So what I'm doing is deciding if I should use the document class or a class file I am using separately from all this (in the main application). My main concern is re-usability; I guess I can import a .swf instead. Thanks again!
Comment: If you look at the example code, you will see that the way it works is that the main fla file uses the .as file as its document Class-- this can be reused by any fla file that has the same needs.

I am not sure how importing a swf file actually relates to any of this.  Can you explain what you were intending to do with the imported swf?
Comment: Simply to have a file that can always be used as a colour picker, without having to code in classes/events for every project that might use the picker.
Comment: Yes, you can use this technique with the Library symbol as well.  Instead of attaching the as file to the fla as the Document Class, attach it to the symbol in its as linkage.  It essentially means the same thing, that you want the Symbol to extend the as file (where using it as the Document class means that you want the whole swf to extend the as file).
Comment: Thanks Amy - so after a few hours spent figuring it out, it all boils down to the *root* of each mc being the best (and often the only) place to put code, i.e. in frame 1. In the end I created a custom event class and used dispatchEvent() from the timeline (but had to resort to using the doc root).


Can I use lightbox for displaying pop up biographies?

Question: I want to achieve this effect. When user clicks on (thumb) image, biography and image should pop up. Can I do this with lightbox, and if answer is yes, how to customize it? Also, is there some other module that is good for this purpose? I am using Drupal 7. Thanks in advance.
Comment: @nattan Per the tooltip on the downvote link: _"This question does not show any research effort; it is unclear or not useful"_. If you'd taken some time to research the problem yourself, and presented a well thought out, interesting question that would be useful to future visitors, and not just solve your immediate, narrowly scoped issue, it probably would have been received a lot better.
Comment: What's wrong or unclear with the question? It would be appreciated to explain what is wrong, every time when you click minus in order to let user know how to fix it. You don't click minuses every time when you don't know the answer.
Comment: This question appears to be off-topic because it is about _Implementing a functionality, or a layout seen in a site, for which only a screenshot or a site URL is provided_, per the [help]
Answer: You might be able to do this by adding your content as the alt text caption and manipulating the lightbox display with css.
Alternatively this question may help: load a node content into a lightbox2 (without headers and footer and other blocks)
Comment: Have just recently found an extension to the colorbox module that does exactly this! https://www.drupal.org/project/colorbox_node
ENJOY! :)


Efficient way to transpose large dataset in R

Question: Suppose I have a table as follows:
A  B
1  2
1  3
1  4
5  8
5  9
5  10

and I want an output as follows:
  B
A 2  3  4 8 9 10
1 1  1  1 0 0 0
5 0  0  0 1 1 1

is there a package that can create an "indicator matrix"? The real data set has about 300,000,000 rows and 2 columns. The value in column B are actually variables, and I want to create a data set to indicate whether than variable is "turned on".
Comment: `table(mydf)` should get you most of the way.
Comment: If I do table(mydf) I get an error: Error in table(train) : attempt to make a table with >= 2^31 elements.
Comment: If it is all integers convert it to the matrix.
Comment: I am using data.table package and I want to keep it that data type
Comment: using `data.table` you can do something like `dcast(dt, A~B, fun.aggregate=length)`
Comment: Why transpose at all? Just `dt[, .N, by = list(A, B)]`.
Answer: You can use this way:
df <- data.frame(A = c(1, 1, 1, 5, 5, 5), B = c(2, 3, 4, 8, 9, 10))
table(df)

If you have other columns in the data frame, and you want to only use columns A, B , you can use this way:
table(df$A, df$B)

You can save this table as its own data frame by converting the table output above to data frame as follows:
df <- as.data.frame(table(df$A, df$B))

Comment: I reach my system's memory limits when I try to create a data frame. Are there any other ways you can do this, or is R not suitable to handle very large datasets.
Comment: It does not seem to be an R problem, but the problem with available memory on your system. Quite possible that you don't have enough for this size data summarization.
Comment: I have 16 GB of RAM. So are there any alternatives or do I need to buy a better computer?
Comment: This is not a good answer since the question states 'efficient way to transpose'; this means that there should be a function or method to transpose without requiring massive amounts of RAM (hence the 'efficient' part). This answer doesn't provide an alternative method to accomplish this nor an explanation why it may be impossible.


Theory on custom loss functions for GBDT and other ML

Question: I'm looking for resources on the theory behind choosing a loss function for ML---I'm interested in GBDT but for deep learning would work as well. I'd like to get a better understanding of how the loss function affects the model, the difference between validation loss and training loss, etc.
I've googled around and most medium articles I find are too superficial, and I don't know where to start looking for more academic resources.
Answer:
the difference between validation loss and training loss, etc.

This is addressed to some extent in chapter 7 of Elements of Statistical Learning which is available for free from the authors online.
There are some other general discussions of loss function properties in other chapters of that book (Chapter 3, Chapter 4, Chapter 10 for example).

I'm interested in GBDT

The original paper on Boosting by Schapire might be useful, as well as this later paper by the same author.
Some other general papers on loss functions:
The original paper on L1 regularization by Tibshirani
A comparison of L1 and L2 regularization by Andrew Ng
The original paper on L2 regularization by Tikhonov is in Russian, you might be able to find a translation somewhere.


Erro em tempo de execução "5852"

Question: Utilizei esse código para gerar uma mala direta e separar os arquivos.
Porém está dando o seguinte erro:

Erro em tempo de execução "5852"
O Objeto solicitado não está disponível.

Uso o office 365

<code>Sub SalvarComoDOCIndividual()
    Dim a As Integer
    Dim registro As Integer
    Dim nomeArquivo As String

    'Define o primeiro registro da mala direta
    ActiveDocument.MailMerge.DataSource.ActiveRecord = wdFirstRecord

    'Contador de registros
    a = ActiveDocument.MailMerge.DataSource.RecordCount

    'Salva todos os registros
    For registro = 1 To a
        'Atribui o valor de cada registro da coluna nome para a variável nomeArquivo que dará nome ao novo arquivo. Caso queira outro nome, basta colocar o nome da coluna desejada
        nomeArquivo = ActiveDocument.MailMerge.DataSource.DataFields("ALUNO").Value
        ActiveDocument.SaveAs2 FileName:= _
            "C:\Users\usuario\arquivos\" & nomeArquivo & ".docx", FileFormat:= _
        wdFormatXMLDocument, LockComments:=False, Password:="", AddToRecentFiles _
        :=True, WritePassword:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts _
        :=False, SaveNativePictureFormat:=False, SaveFormsData:=False, _
        SaveAsAOCELetter:=False, CompatibilityMode:=15
        ActiveDocument.MailMerge.DataSource.ActiveRecord = wdNextRecord
        Next registro
    End
End Sub
</code>
Comment: Faltou explicar em que linha o erro ocorre.
Answer: Esse erro normalmente aparece quando objeto MailMerge não existe e quando você está tentando executar a mala direta.
Experimente primeiro, criar uma mala direta em seu documento do Word.
Basta usar o assistente e esse objeto será preenchido automaticamente e depois execute o código

Erro em tempo de execução "5852"


Boost.Asio - using std::move on handler before calling it

Question: I'm confused by a Boost.Asio idiom I frequently see - calling a handler (function object) like this:
std::move(handler)(param1, param2);

What is the reason for writing it this way? My understanding is that this is exactly the same as
handler(param1, param2);

unless handler's operator() method is ref-qualified with && (for info about ref-qualification see "member functions with ref-qualifier" here). Is this something to be expected? I've never actually seen this idiom paired with a ref-qualified operator(), so this seems like an unlikely explanation.
Examples:

official Boost.Asio documentation
official Boost.Asio example - look for std::move(op) in function test_deferred()
cpp alliance blog - multiple examples
Answer: In general, the explicit support for move-only handlers ties in with allocation order guarantees that Asio makes:

As noted previously, all resources must be released prior to calling the completion handler.
This enables memory to be recycled for subsequent asynchronous operations within an agent. This allows applications with long-lived asynchronous agents to have no hot-path memory allocations, even though the user code is unaware of associated allocators.

Specifically, the second example
auto op = async_write_messages(socket, "Testing deferred\r\n", 5, asio::deferred);

defines a packaged operation op with type
asio::deferred_async_operation<
    void(boost::system::error_code),
    asio::detail::initiate_composed_op<void(boost::system::error_code),
                                              void(asio::any_io_executor)>,
    async_write_messages_implementation>

Looking at the implementation thereof, indeed we see exactly what you surmised:
template <BOOST_ASIO_COMPLETION_TOKEN_FOR(Signature) CompletionToken>
auto operator()(
    BOOST_ASIO_MOVE_ARG(CompletionToken) token) BOOST_ASIO_RVALUE_REF_QUAL;

template <BOOST_ASIO_COMPLETION_TOKEN_FOR(Signature) CompletionToken>
auto operator()(
    BOOST_ASIO_MOVE_ARG(CompletionToken) token) const &;

Which, removing macro noise, in c++20 becomes:
template <asio::completion_token_for<Signature> CompletionToken>
auto operator()(CompletionToken&& token) &&;

template <asio::completion_token_for<Signature> CompletionToken>
auto operator()(CompletionToken&& token) const&;

The &&-qualified overload optimizes execution. This makes intuitive sense when you realize that a deferred handler may merely represent deferred_values, to be passed to the user-handler. These - effectively callback arguments - may also be expensive to copy, or move-only.
In this case, deferred_async_operation implements a function object that delays initiation of another asynchronous operation. The initiation function takes arguments that might, again, be expensive to copy, or be move-only.
Indeed the rvalue-ref-qualified version supports those move-semantics, where the const-qualified version does not (again Asio code heavily redacted for legibility, and assuming C++14 or up):
template <typename CompletionToken, std::size_t... I>
auto invoke_helper(CompletionToken&& token, std::index_sequence)
{
    return asio::async_initiate<CompletionToken, Signature>(
        std::move(initiation_), token, std::get(std::move(init_args_))...);
}

template <typename CompletionToken, std::size_t... I>
auto const_invoke_helper(CompletionToken&& token, std::index_sequence) const&
{
    return asio::async_initiate<CompletionToken, Signature>( //
        initiation_t(initiation_), token, std::get(init_args_)...);
}

Does It Matter?
Arguably in code that doesn't support ref-qualification, the unqualified version of operator() would successfully pass lvalue-references to the initiation function. This would even work for move-only types IFF the arguments were taken by lvalue-reference. If mutable, those could even be moved from.
The more precise forwarding allows initiation where move-only ("sink") parameters are taken by value, too.
In cases where copies are avoided, this has the important benefit of optimizing the (de)allocation patterns of the application. Consider what would happen if one of the arguments involved contains a reference-counted resource (e.g. shared_ptr). Even if the wrapping type (e.g. deferred_async_operation) goes away "immediately" after invocation, there's can be a difference in the order of allocation/de-allocations when the ref-counted resource temporarily has a non-unique refcount.
TL;DR
I'd like to boil it down to expressive code: callables that are meant to be invoked only once should express that "unique invocation" much like any other move-only type signal "unique ownership" by the presence of std::move().
There are places where it matters, and a general-purpose library like Asio should not impose unnecessary overhead.
Illustrating - Live Demos
In the example, the async_write_messages_implementation is move-only because it contains unique_ptr members. Therefore, going through the const_invoke_helper would not be able to compile: https://godbolt.org/z/KTc5ooPhT
You can fix it by changing the unique_ptr to shared_ptr, but now you run into the issue described where ownership of the resources will not be unique during the call to async_write_messages_implementation::operator() which leads to the reset()s not releasing their resource until later: https://godbolt.org/z/n9fbE3zxh
Comment: Because no answer should go without live code illustration, added one.
Comment: wow what a Lovely answer !!
Comment: @ante.ceperic Hardly. In terms of public interface, it might be - as far as I can tell `asio::deferred_t`  is also exceptional in that it seems to assume [c++11 support](https://think-async.com/Asio/AsioStandalone.html). However, in library implementations this pattern comes up quite a bit - when implementing stable async operations that do *not* rely on shared ownership. Your [third link](https://cppalliance.org/richard/2021/10/10/RichardsOctoberUpdate.html) is also an example of that in the wild.
Comment: Is `boost::asio::deferred` the only example of this behaviour?


How to start view chart on last page

Question: I am using the Charts module and Google Charts API with Views. I want my view to display with the last page of the view showing on page load. How can this be accomplished? For more explanation of what I'm trying to do: I have a view displaying a line chart with historical data, but I want the view to load with the most recent data first and the pager to allow the user to go back (using previous button) to view data in the past.
Thanks in advance.
Answer: Not familiar with charts, but in views SORT you could try using the opposite.
Ex: Sort Descending instead of Ascending. (so it reverses the order).
Then all you would have to do is override the pager so it says previous instead of next. I found some documentation here.
Comment: @NoSssweat : **some people** (like Drupal *developers*?) spend their time on learning about all the bits and bytes to code whatever from scratch (and keep themselves busy with that for a loooooong time) .... **others**  (like Drupal *site architects*?) just look around and hunt for hidden gems, and think about how to use those gems ...
Comment: Thanks for the suggestion, but sort desc puts the latest data on the left side of the chart so it reads historically from right to left and not left to right as is natural.
Comment: "*Not familiar with charts*" ??? What's stopping you to use the [Ask Question](http://drupal.stackexchange.com/questions/ask) button around here ... eg to "get started with its Views interface"? If you do, make sure it fits the [Rules](https://www.drupal.org/project/rules) around here.
Comment: `What's stopping you to use the Ask Question button` I haven't tried to use the module yet. I would like to implement it for a project, but I am leaning towards using D8 for that project, so looks like I will have to wait.
Comment: @NoSssweat : your comment above was too challenging to me ... have a look at the question with id=198246  ... So if you have a chance to get into charting in D8, make sure to look at it. And if you do: I'd appreciate your feedback, good or bad, about it ...
Comment: @Pierre.Vriens another module I did not know about, I will check it out some time, thx.

How to start view chart on last page