Por que fracionar um insert melhora sua performance?

Question: Supondo que eu tenha uma tabela order_items que registra os itens de um pedido, e neste exemplo, o usuário estaria fazendo 1 pedido de 10.000 computadores. No momento de registrar os itens do pedido no banco dados, gostaria de entender porque ambas as operações de insert:
insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400)
insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400)
insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400)
insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400)
outros 10.000 inserts

insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                +10.000 registros

Apresentam uma performance muito inferior em relação ao modelo abaixo?:
insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                +2497 registros
insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                +2497 registros
insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                +2497 registros
insert into order_items (user_id, order_id, name, price) values (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                (1, 1, 'Computer', 3.400),
                                                                +2497 registros

Resultados obtidos em teste com um banco MySQL:
Para o 1º exemplo `1.04 sec`
Para o 2º exemplo `0.234 sec`
Para o 3º exemplo `0.094 sec`
A impressão que tive é que a diferença de tempo fica mais clara quando se tem mais registros sendo inseridos, mas é apenas uma impressão mesmo
Comment: *"Apresentam uma performance muito inferio"* e como chegou a essa conclusão? pode adicionar esses dados na pergunta? E pf também informe o banco de dados, isso faz **toda a diferença nas respostas**. De qualquer forma, é muito mais performática fazer uma única operação, pq se valida e transaciona de uma vez, mas, dependendo do banco, isso envolve gravar log para fazer rollback, atualizar índices, etc, por isso precisa ver o plano de execução e entender melhor.
Comment: Seria interessante juntar a pergunta os dados de análise das consultas em questão.
Comment: Se a opção AUTOCOMMIT estiver ligada então os procedimentos referentes ao COMMIT serão realizados a cada comando. Tais procedimentos tem custo alto e por isso quando você coloca diversas tuplas a cada INSERT tem uma performance melhor. Avalie definir uma única transação `BEGIN` (lista dos comandos INSERT) `COMMIT` e observe o tempo de execução.
Comment: Atualizei a pergunta com o banco utilizado e o tempo que levou para a execução de cada operação. Como disse ali, a impressão que **eu** tive é que essa diferença no tempo parece mais clara quando você está fazendo a inserção de uma quantidade maior de registros.
Answer: Isso pode ser muito relativo. Pode depender da configuração e estrutura do próprio banco. No Sql Server normalmente isso pode estar relacionado com o fato de o banco precisar reservar recursos para a estrutura de log e transação. Fazendo com que o SGBD sofra uma certa lentidão enquanto todos os inserts estão sendo processados em uma mesma transação e o banco ainda não realizou o commit para liberar estes recursos que consomem memória e processamento.
Mas, para uma análise mais exata é preciso verificar o plano de execução.


What is wrong with this code?

Question: Sorry that I'm asking such a question, but I'm tryin to make this one run for hours, and I'm not finding the mistake...
public class Main extends ListActivity {
/** Called when the activity is first created. */

ProgressDialog dialog;

@Override
public synchronized void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    new WebLoader().doInBackground("http://sample.sample.com/sample.xml");
}

public class WebLoader extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        String result = "";

        try{
            URL url = new URL(params[0]);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(2048);

            int current = 0;
            while((current = bis.read()) != -1)
            {
                baf.append((byte)current);
            }

            result = new String(baf.toByteArray());
        }

        catch(Exception e)
        {
            Log.e("gullinews", e.getMessage());
        }

        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        dialog.dismiss();
    }

    @Override
    protected void onPreExecute() {
        dialog = ProgressDialog.show(getApplicationContext(), "",
                "Loading. Please wait...", true);
    }
  }

}
Running with a debugger shows, that the xml data is downloaded, but there's just black screen. When I tried "setContenView(R.layout.main);" with main.xml:
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:id="@android:id/list" />
</LinearLayout>

//Edit:
okay, I solved one error, didn't solve the rest. Source updated.
My Main problem now is, that I ain't got an Idea why the ProgressDialog doesnt show up. rest should be black, that's right.
Comment: What did you expect? Your layout is empty.
Comment: What errors?  Please cut-and-paste stack traces, etc
Answer: new WebLoader().doInBackground("http://sample.sample.com/sample.xml");
That's not how you use an asynctask. Did you read any documentaton at all?
Comment: Mh. Sorry. Old sourcecode, did that mistake, but now I'm not doing this anymore. I know it's .execute("");

What is wrong with this code?


Azure API gateway reroute urls based on tenant Id to backend application which are not hosted on Azure?

Question: I have following requirement.
Currently we have different back-end public facing API applications(Not in Azure) for each tenant. We are building a mobile application. for that we have come up with an integration layer where we invoke multiple API's from our Back-end API applications using Azure Logic Apps.
This Logic APP url's further will be integrated with API Management.
But we are looking for the possibility to reroute the request to relevant back-end api application based on tenant id which we pass as query string parameter.
Can any one please help me to understand whether Azure API gateway supports urls rerouting to external public facing applications (Backed applications are Not in Azure) ?
We are planning to move back-end applications to AZURE but not near by. Mean while we wanted to have a solution.
Currently we are storing the Host name and Tenant ID in Table storage. So based on tenant id we are fetching the host name and forming the URL with relevant host name.
Any other alternative ideas are also highly appreciable.If you need any other information please let me know?
Answer: I believe the approach you are taking is the right one. The same in APIM can be achieved by using the set-backend-service policy.
You could either fetch the hostname from table storage in APIM using the send-request policy (cache the result for better latency).
Or you can use Named Values to store the mappings and remove table storage altogether.
Comment: currently we are doing all these tenant based mapping's in a separate logic app and calling it as part of the base API which will do multiple back-end request's and aggregate the result and send it back.  But all these things we can do in APIM Policies level. So My question is will there be any advantage doing all these things in APIM Policies rather than doing it in a logic app/Durable functions? Is it just a flavor?
Comment: While it may be possible, APIM is best as a front for your APIs with features like security, rate limit, etc. and shouldn't really have lots of processing involved like aggregates. If the intention is just to route based a key like your original post, should be OK. But if you require processing of any kind, better to use Function/Logic Apps and just front them with APIM if required.


Quiz Module - Restart (Reload) the question of the quiz when it has been wrongly attempted four times

Question: I am trying to reload the page (or) make the user attempt the question again if he/she has made four wrong attempts.
I am using Quiz module in Drupal 7.
Is there a possibility?
Thanks
Answer: It's kind of hard to decide what to do without code samples, but the following ideas come to my mind:
First, you could try a simple PHP command in the form of header("Refresh:0");, calling that once the wrong answer count reaches 4.
My second idea is that you try to integrate some JavaScript into the module itself, attaching a click event to the buttons or whatever it your user is clicking on. The click event could keep track of how many wrong answers they've gotten, and once they hit the threshold, have the JavaScript call location.reload().


Is it possible to schedule posts with Jekyll?

Question: I suspect that one of the limitations of running a static site is that the administrator has to be at the helm when he wants a post to be published. I'm looking for that suspicion to be validated. Is there a method I haven't discovered?
Answer: Set the site.future option to false on the _config.yml file.
Then set up a cron task in your server to regenerate your blog every day (or hour, whatever you fancy).
Posts set up in the future will be automatically published when the correct date is reached.
Comment: Thank you. I deleted my own answer because some independent testing gave results that made me think I was wrong, but I will experiment later to see what happened.
Comment: Can I do it with GitHub pages?
Comment: @VladimirStarkov probably, but I have not tried. If you do, please report your findings.
Comment: Probably not working with github pages because AFAIK, github only regenerate the pages when your push something. So if you want to do this you'll have to push something and, hopefully, github will regenerate your site
Comment: In order for this to work you need to have some sort of automatic scheduler in your own machine which periodically publishes the jekyll site to the server - github or otherwise. I suggested [cron](http://en.wikipedia.org/wiki/Cron), but there are others.


Which files should be commited in SVN for an Angular project?

Question: creating a project based on Angular as the frontend results on many automatically generated files. I would like to commit the project into SVN including the necessary files only.
Below is the current frontend folders structure:

Which files/folders should be commited in SVN ?
Comment: except node_modules and node  all others folders you can commit
Comment: Also `dist` don't need commit.
Comment: I've just tried to create a new project (IDE eclipse) with the same files as displayed above but without the folders "node_modules", "node" and "dist", but each time a dependency error is displayed.
Comment: @Sakr did you run `npm install` to install all the dependencies specified in package.json?
Comment: @Jette yes indeed, so the node_modules folder doesn't have to be checked in. The depencies will be installed via npm install using the definded packages from "package.json"
Answer: Indeed as mentioned by Robert and Stepan, the following folders could/should be ignored:

node
node_modules
dist


Is knowing music theory really necessary for those who just want to play songs they hear?

Question: I'm kinda new to music theory.
I am always told that to play songs on, for example, a piano I should:

know which key is responsible for which note.
adapt my ears to notes, so that I know which note I should play to produce the right sound in a piece of music.

And I am always told that knowing music theory is necessary for those who want to:

read music from a sheet.
be a composer.

I know it's better to know music theory for those who just want to play songs they hear on, for example, radio. My question is that is it necessary here?
Comment: Fair enough. The answer also hinges on what "counts" as music theory. We tend to think of it as the analysis of the chords and patterns that make up a piece. Is this necessary to simply read sheet music? No. But if you count knowing the difference between a quarter note and half note, understanding sharps and flats, or reading key and time signatures as "music theory" then yes, these are necessary.
Comment: I'm voting to close the question because it needs more details. What is specifically the theory you consider to learn or not? When you learn to play an instrument you inevitably learn _some_ theory. If you don't learn it from someone else, you end up figuring it out by yourself. What is your dilemma? Are you worried about the time you need to devote to learning theory, or perhaps you don't know how to apply some theory you learned doesn't apply to the music you know, or is it something else?
Comment: Welcome! I'm afraid that, as it stands, this question is subjective and likely to get opinion-based answers, which is [not covered here](https://music.stackexchange.com/help/dont-ask). As the linked page suggests, you could edit it into a question that will get better answers, perhaps by focusing on *how* theory might help in reading and composing. Good answers would be more than the binary "yes, it's necessary" or "no, it isn't."
Comment: Technical proficiency is the only thing you need to do covers. So what's necessary is ten thousand hours of practice. For improvisation you've either 'got it' or you don't, if you don't then I'd assume theory would help immensely with faking it.
Comment: You don't have to do anything you don't want to do. Will studying music theory help you understand music? Absolutely. So the question for you is, how well do you want to understand music?
Comment: You might even find it helpful to move away from the word "necessary"; maybe that's not what you want to emphasize. Are seats necessary in a car? Well... not *strictly*, technically, but no one can argue that they're not helpful.
Comment: Personally I think it's a fair question for the site, and will be reasonably interpreted as asking "to what extent is it necessary." I'd expect answers to talk about advantages theory provides for playing songs by ear, limitations in trying to do so without theoretical knowledge, and the processes involved in playing by ear. All of those are pretty objective and seem within the bounds of the site to me.
Comment: Idk how you can read music and not have been taught theory. I taught theory for 10 years and the most important and also the first thing I taught children was how to read music notation.
Comment: @user1079505, respectfully, I think I disagree that we should expect someone who is new to theory to be able to enumerate specific parts of theory that will help them. That doesn't seem like a fair requirement IMO.
Comment: @jdjazz OK, fair point, retracting my close vote... but I'm still in doubt. I like your answer for instance, but I'm not sure if it actually answers the OP's question.
Comment: @user1079505, I do see what you mean there. I'm interpreting it more as "to what extent is it necessary/ helpful," which I tend to think is a fair take on what's being asked.
Comment: @Mazura, I'm of a very different mind. Improvisation is cultivated, not innate. Improv skills are acquired through practice, exercises, transcriptions, listening, and playing with other people. Virtually none of the great improvisers were simply innately good from the beginning. There are tons of stories about e.g. Parker, Coltrane, Mehldau, etc. emerging as great improvisers only after spending months/years shedding. Knowing *some* theory is a very big help, & 10,000 hours is way too high. I assume you're quoting Gladwell; one doesn't need to be an expert musician to play covers successfully.
Answer: tldr; there is a commensurate level of theoretical knowledge that will help immensely with what you want to do. It may not be much theory, but if you know it, you'll be much better off (unless you're a musical genius, as Michael Curtis points out).
When someone with extensive theory knowledge listens to a song, they might think, e.g., "Oh, that's a I-vi-IV-V progression in G." Thanks to their theoretical knowledge, they've recognized the pattern more quickly, they've expended less mental effort, and they'll remember that song for a longer time.
You can probably already see how theory helps. When you play by ear, you're decoding sounds into notes. Theory provides structures and frameworks for conceptualizing both of those things (the sounds and the notes). Theory identifies patterns in music. It's like the teacher standing over your shoulder saying "look at the chords you just played--it's the same sequence as the song you learned two weeks ago." When you spend time thinking about those patterns, learning them, and naming them, you will be faster at identifying them and better at remembering them. Knowing those patterns is essentially a "shortcut" that reduces the required mental effort.
In essence, when you learn a song off the radio instead of reading sheet music, you're transcribing. The less intensive your transcription work, the less theory you'll require. If your desired task is to play melodies and chords, then you don't need to go buy 10 books on theory. Focus on diatonic chord progressions and the major/minor scales, and that will probably be enough.
On the other hand (to use a more extreme example), if you want to transcribe exact chords/notes on a solo jazz piano track, then you'll want to know a wide range of theoretical voicings (upper structure triads, combination triads, quartal voicings, stacked thirds, drop-2, shell voicings, type A/B voicings, etc. etc. etc.).
Theory is nothing more than a collection of statements describing patterns in the music. Knowing those patterns helps. But you only need to study the patterns you care about.
Comment: The theory also gives you creative license and technical know-how to fill in parts that you did not perfectly replicate. Most people know the song "Amazing Grace", but if you know the theory behind harmony, you don't have to memorize the base lines and just make it up as you go along.
Comment: Knowing the language is a communication tool – even with yourself. E.g. many popular songs can be entirely described by something like "It's a 4-chord-song in D with an ABACAB form and half-step modulation at the end." If you know the language (and music *is* a language), then that is literally you need to know to play that song, at least in a basic version.
Comment: I've actually found the opposite when it comes to my transcribing (admittedly rock- and metal-leaning) video game music: the more intensive my transcription, the more I actually need to throw my existing theory knowledge out the window. I've ended up transcribing some key ambiguity and rapid-fire key changes in my most difficult transcriptions, and solos tend to refuse to fit cleanly in any meter and go MIDI-style instead. (On another note, the latest piece I transcribed the melody of spammed a Cm-Em-Cm-Em chord progression, so time to throw determining a key out the window.)
Comment: @Dekkadeci, that's a great example. So, I think the point I was attempting to make is that the patterns still exist, but just become more complex. Classical harmony of course won't be the relevant theory to help with super complex video game/rock-metal music, because the harmonic structures and patterns are different. I know enough about those genres to appreciate the incredible complexity that can be involved, but not enough to be able to describe the relevant patterns intelligently.
Answer: Necessary? No.
Helpful? Yes.
You can compare it to just about any other endeavor. The more you know about the concepts and terminology of any given field, the more effective you will be in the study.
Being illiterate in any topic is not a benefit.
Some like to cherry pick examples of the supposedly unstudied geniuses, like Einstein failed math, Bill Gates dropped out of college, and the Beatles couldn't read music, as justification for avoiding any study. The problem with that is: extraordinary examples don't make the norm, those examples are often untrue, those examples overlook the people gained the same studied knowledge through non-traditional paths, and copying one characteristic will not give you the whole package. Not learning to read music will not make you as good a musician as The Beatles.

Is knowing music theory really necessary for those who just want to play songs they hear?

Most likely it will be helpful. Don't use the word "necessary" to load the question. Of course it isn't necessary. There is no question of requirement. The question is whether it is beneficial.
Someone may have a talent for playing by ear and have honed their skill at it through practice. They will know things about how music works... and probably give their own idiosyncratic names to things. Their skill is a combination of ear training and pattern recognition via a personal path.
If that doesn't "just happen for you" then the traditional course of ear training and theory study will help you. It will help you, because it will teach you to hear musical patterns and understand their practical application.
Comment: Not to mention that these geniuses stories are often untrue or exagerated, for example [Einstein being bad at maths is most probably untrue](https://www.ripleys.com/weird-news/did-einstein-really-fail-math/)
Comment: From what I've read, Bill Gates dropped out of college in order to focus more on Microsoft.
Comment: ...indeed these stories are often suggesting quite the opposite of what's really going on. Geniuses often perform worse than one might expect at school because they're _already too good_ at the theory, have already read the deep literature on their own and don't see the point for wasting more time with silly exercises on a level they have long passed. (Then again, of course there are also people who _think_ they know all the theory, but actually have huge gaps in their knowledge...)
Comment: Genius is a tricky word often with poorly defined meaning because we attribute it to so many different things (being a savant, achieving greatness). Someone can be a genius in one field and not another. Einstein was better than most at math but not a mathematical genius--he got help with general relativity from mathematical geniuses like Emmy Noether and David Hilbert. Marcel Grossman pointed out to Einstein that he should use Riemann geometry. Einstein's genius lied elsewhere, in perceiving the deep structures of the universe (light is quantized, gravity is the warping of space-time, etc.).
Comment: Musical savants do exist (and our advice may not apply to them). But they probably don't post questions on this site.
Comment: @Dekkadeci, exactly. The point being Gates didn't drop out first then start Microsoft.
Answer: Yes, you are allowed to play piano without any knowledge of notation or any other 'theory'.  There have been some very accomplished players who were completely untutored.
They learned, by trial-and-error, what notes made what sounds.  They began to recognise patterns, so that what they'd worked out for one song could make it quicker to work out the next one.   They were developing a concept of 'theory'.
It can be a heck of a lot easier to read a book than to wait until someone else decides to recite that story and try to remember it.  Same with music.  'Really necessary'?  No.
Answer: Clearly you can do whatever you want to do.  You could try to learn a bunch of theory all at once, which wouldn't be very much fun for you.  But if you buckle down and study hard it won't be long and you'll start to see things make more sense for you.  You would be working on very simple music, which might frustrate you.  Are you that patient?  If you are, this method pays big dividends down the road.
Or, you could just keep on doing what you're doing if it is enjoyable.  Music was never meant to be a chore.  The day may come when you discover something that you really, really WANT to understand.  Then, you can dig in and learn that thing you really want to know and theory will be just as enjoyable as playing.
My advice, find a private teacher you like.  Tell him/her where you're coming from and let then teach you piano and they'll naturally weave in theory as you go.
Answer: Why do people think western music tradition requires you to learn music theory if it is not necessary? Do people think musicians enjoy exercises in futility? It is taught because it improves every part of your musicianship. Why do people have such a hard time believing this? Do these people also go to Harvard and question why the chemistry department requires you to learn theory and practice or why you cannot graduate a maths degree by doing algebra and not trigonometry?
People think that a great teacher and hard work leads to great musical ability. This is certainly a big part of it but the part that is not discussed nearly often enough is the crucial part that an early, quality music theory education plays in producing musicians of the highest order.
In closing this question is analogous to asking can a blind person find the entrance to your house if he gets to your house? Given enough time and effort, yes. An easier solution would be for the blind person just to be able to see the entrance of your house.
Can you figure out V - I is a good way to end a passage, sure. That is how this piece of knowledge was accrued originally all those centuries ago. It would be easier for someone just to tell you that give you a couple of examples. Show you it is the case and you to go on your merry way.
Comment: I'm with jdjazz here. 'Theory' is often seen to be for elitists - particularly, I've found, amongst guitarists, self-taught ones especially. Several of the guitarists I play with overtly imply that theory is not for them - even with offers of free explanations (!). It doesn't stop them being good players in their own right. It does however stop them being able to speak in a language which a lot of us do understand. It also slows down the rate of learning new material - of that I'm certain. But, as a necessity - no, it's more of a bonus - for those who see the benefit. Not all players, sadly.
Comment: I love studying theory and find it extremely useful. You asked "Why do people think Western music tradition requires you to learn music theory if it is not necessary?" From my interactions, I think some such people view theory as a form of intellectual elitism. It's a gate that keeps out those without the formal education. If there's any truth in that metaphoric gate, then this is a substantial fact to reckon with since many people in music don't have formal education, particularly in non-classical genres.
Comment: In total fairness, I can see where that misperception comes from that theory = elitism / gatekeeping. Sometimes theoreticians convey the belief that (a) music can't be fully enjoyed without understanding the theory, or (b) music is inferior if it *can* be enjoyed without understanding the theory. We don't get a ton of questions on this site about rap or heavy metal, for example, and I don't think that's a coincidence--it conveys something about the established relationship between those genres and theory.
Comment: And a couple of times, those musical forms have been disparaged on this site. (I remember one instance where a very well-established, highly visible community member called the music "noise" and questioned why someone would attempt to analyze it, rhythmically.)
Comment: I don't really understand why people refuse an education. I taught 9 year old this for 5 and a half pecans a month. There really is no reason why anyone cannot learn this subject at a reasonable level.
Comment: @NeilMeyer, I'm certainly not accusing you of being elitist. Just trying to explain where some are coming from. You said "I don't really understand why people refuse an education." But to put ourselves in their shoes, why would someone seek an education they feel has belittled/disparaged their art form & their self-taught methods? Sure, there's probably not any *intellectual* reason why most people couldn't learn music theory at a reasonable level, but we can't overlook those cultural factors. That's an easy thing for me & you to do given our rich, positive experiences with music education.
Comment: @NeilMeyer, there are facts we can't ignore; music theory is based almost exclusively on Western classical theory. Other musical forms have not historically had a place in formal theory education. Elitism, gate-keeping, & the disparaging of non-classical genres all occur. After subjecting a non-classical musician to all of that, is it really fair to characterize them as *refusing* an education? They would say the educational system has rejected them. The sentiment, "why do you refuse to do something children do?" reinforces perceptions of their place within formal institutions of music theory.
Comment: There is no classical theory. A quality melody is good regardless of genre. A minor chord remains the same thing regardless if you play it on electric guitar or church organ. I never gotten to the stage where I ever taught a single  harmony question. All I ever taught was the basics to kids and I never saw it being anything but positive.
Comment: All of this is a tragedy, of course. I agree wholeheartedly with you that theory should be for everyone. I know it's a genuine confusion you feel about this, and because of my particular musical path, I've probably seen those cultural influences play out more than you--they're certainly not always evident. I upvoted your answer, and I agree that theory isn't an exercise in futility. I think the way people like us approach these conversations (people who've had positive experiences with music theory and *don't* subscribe to elitism) can have a big impact on bringing others into the community.
Comment: We're talking about two different things. When you say "There is no classical theory," you're talking exclusively about the intellectual constructs. When I say "Theory is based heavily on Western classical music," I'm talking about the sociocultural institutions of music theory. You're correct, of course, that theoretical constructs apply broadly to other Western genres. But those constructs historically have belonged to institutions that exclusively use classical examples, require students to play classical music, teach the history of classical composers, and refuse to accept other genres.
Comment: This historical bias is changing, thanks to people like you. The students you work with will have positive experiences with music theory. You're sparing them from this historical bias, which is *such* a positive thing. But many people haven't been that lucky. That's the group that we need to work a little harder to bring back in, who deserves some extra empathy. In many fields, primary interventions (preventing the problem from ever occurring, as you're doing with children) tend to be easier than secondary interventions (of repairing the damage from the past).
Answer: A decent percentage of people will just be unable to "play by ear". (Possibly they are deaf, or tone deaf, or just bad at it like me.) For those people, reading some kind of widely available written notation is the most practical way of learning new music, usually sheet music or possibly guitar tablature. But you only need mechanical knowledge of the notation for that--this shape in this position means play this note for this long, this word means play louder, this symbol means play the same section over again, etc.
The alternative for us tonally challenged folks would be some kind of verbal/visual training, but even in these times just learning to read sheet music is a lot easier than trying to find a tutorial for every song you want to play. (Not to mention faster and more convenient.)
Answer: Most of the time I listen to music I'm consciously hearing the scale degrees 1-7 (I-VII) along with some non-scale tones. I play by ear on alto recorder.  I hear the piece and where it lies on the scale, then i play it in one of the two keys I'm best at and that give the best range and comfortable playing. I almost never play things in the original key, but rely on knowing where in the scale its happening. A short section of music by itself can qualify as two different keys; playing major scale you may end up in minor for a little bit (in question). Knowing these overlaps can save you headaches as a by-ear player. In minor and major, notes 4-5-6 in one key is 1-2-3 in another key. I always stumble on this.
+1 jdjazz answer!
Music theory and composing: We could go off about this but its not what you asked :D
Comment: Scale degrees are indeed 1-7. I-vii(o) are usually used for triads (chords).
Answer: Everyone is pretty much free to reinvent the wheel.
That said, a lot of basic "wheels" in the music theory are rather easy to reinvent. I know because I did.
Other things are pretty much easier to read somewhere.
Q: "Do I need to be an engineer in order to hang a picture on the wall" ?
A: "Not really. But it helps a great deal."


Receiving and deleting messages from sqs using boto3

Question: I am trying to receive and delete that message one by one. This is my code for your reference.
def receive_delete(url , attr_names):
    global sqs
    response = sqs.receive_message(QueueUrl=url,
                               AttributeNames=attr_names,
                               MaxNumberOfMessages=10,
                               MessageAttributeNames=['All'])

    delete_msgs = []

    for messages in response['Messages']:

        delete_msgs.append({'Id':messages['MessageId'],
            'ReceiptHandle': messages['ReceiptHandle']})

        while len(delete_msgs) > 0:
            sqs.delete_message(QueueUrl=url , ReceiptHandle=['ReceiptHandle'])

and then i am calling that function like this ...
x = receive_delete("https://sqs.us-east-1.amazonaws.com/266638361021/tony.fifo", attr)

Problem is , when i run this method , it gets stuck and when i check on amazon sqs dashboard , it shows only one message is deleted.
How can i delete message one by one after receiving it ?
Comment: According to boto3 documentation,  `sqs.delete_message`  parameter `ReceiptHandle` only accept string.  For batch delete, use `delete_message_batch`, but again, you need to construct individual "entries"  for each message.
Comment: ok , thanks.This would be helpful.
Comment: You seem to have an infinite loop because delete_msgs is never updated: `while len(delete_msgs) > 0`
Answer: Well this worked for me.
I read a message (one) from a FIFO queue and process in the middle then delete using the receiptHandle. This is the code -
import boto3

queue = "your_queue_url"

def process_message():
    sqs_client = boto3.client("sqs")
    response = sqs_client.receive_message(
        QueueUrl=queue,
        MaxNumberOfMessages=1,
        WaitTimeSeconds=15,
    )
    # print(response)
    # print(response)

    print(f"Number of messages received: {len(response.get('Messages', []))}")

    message = response.get("Messages", [])[0]
    # print(message)
    message_body = message["Body"]
    receipt_handle = message['ReceiptHandle']
    print(f"Message body: {message_body}")
    print(f"Receipt Handle: {receipt_handle}")

    # trying to delete the message
    dlt_response = sqs_client.delete_message(
        QueueUrl=queue,
        ReceiptHandle=receipt_handle
        )

    print("Deleted? ", message_body)
    print(dlt_response['ResponseMetadata']['HTTPStatusCode'])

process_message()

Hope this helps.


Removing Multiple Unique Values from Column in Python

Question: enter image description here
I am trying to remove the last three digits from the date column. There are 1,417 rows in the data frame that have this problem and are all different numbers. I am using Python and Pandas. I have also attached the dataset that I am working on if there is also something I could do in Excel and then re-upload it. enter link description here
Answer: You can use string slicing to remove the last 3 digits as follows.
df['date'] = df['date'].str[:-3]

Example code:
import pandas as pd

date = [
    '2020-01',
    '2020-02',
    '2020-03'
]
df = pd.DataFrame(date, columns=['date'])
df['date'] = df['date'].str[:-3]

print (df)

Output:
   date
0  2020
1  2020
2  2020


Swift generic type check protocol conformance

Question: Let's suppose I have 2 swift protocols:
protocol A: AnyObject {}

protocol B: A {}

On a generic function, I would like to retrieve if the passed type T can conform to the protocol A:
    func method<T>(_ type: T.Type) -> Bool {
        return T.self is A.Protocol // A.Type fails always
    }

On the previous method in case I send a type A everything works fine, but if I try to send a type that conforms B it fails. The expected result will be a true as B already conforms to A.
Is there any way to achieve this logic?
Thanks in advance.
Comment: *...can conform to the protocol...* checking at runtime is pretty *unswifty*. Write `method` then you get the check at compile time
Comment: `A.Type` should work here. Can you show a [mcve] of `A.Type` failing? Anyway, why not check it at compile time (see George's answer)?
Comment: Yup I have already checked Georges's answer (and added a comment) If you try to pass a class type that conforms A or B, everything works fine, but let's suppose you want to pass protocol B as type, George's answer won't work
Answer: Here is a way of achieving this:
protocol A: AnyObject {}
protocol B: A {}

func method<T>(_ type: T.Type) -> Bool {
    return false
}
func method<T: A>(_ type: T.Type) -> Bool {
    return true
}

class Temp1: A {}
class Temp2: B {}
class Temp3 {}

print(method(Temp1.self)) // true
print(method(Temp2.self)) // true
print(method(Temp3.self)) // false

Write two methods the same, apart from one has generics <T> and the other is <T: A>. In this example, Temp1 and Temp2 will use the <T: A> method whereas Temp3 will use just the <T> method since it doesn't conform to A.
Comment: Clever usage of overloading, I like it!
Comment: Thanks for the super fast response @George, but in your example, in case I want to pass `method(B.self)` as parameter, the result will be a `false`. Seems there's no inference on the type when sending a protocol type.
Comment: @AlbertoGarcía It doesn't really make sense that you would give it a type _or_ a protocol. They are different things - there is probably a better way to structure your code than that.
Comment: Note that this is approach is also very fragile. https://gist.github.com/rnapier/638fb92523dc6d0ba8392bc0a62f4030


Two way sync with rsync

Question: I have a folder a/ and a remote folder A/.
I now run something like this on a Makefile:
get-music:
 rsync -avzru server:/media/10001/music/ /media/Incoming/music/

put-music:
 rsync -avzru /media/Incoming/music/ server:/media/10001/music/

sync-music: get-music put-music

when I make sync-music, it first gets all the diffs from server to local and then the opposite, sending all the diffs from local to server.
This works very well only if there are just updates or new files on the future. If there are deletions, it doesn't do anything.
In rsync there is --delete and --delete-after options to help accomplish what I want but thing is, it doesn't work on a 2-way-sync.
If I want to delete server files on a syn, when local files have been deleted, it works, but if, for some reason (explained after) I have some files that aren't in the server but exist locally and they were deleted, I want locally to remove them and not server copied (as it happens).
Thing is I have 3 machines in context:

desktop
notebook
home-server

So, sometimes, server will have files that were deleted with a notebook sync, for example and then, when I run a sync with my desktop (where the deleted server files still exist on) I want these files to be deleted and not to be copied again to the server.
I guess this is only possible with a database and track of operations :P
Any simpler solutions?
Thank you.
Comment: i actually did now. i was trying to achieve a dropbox like experience and came up with a solution that involves unison (that uses rsync) and lsyncd for monitoring file changes. i also found other people with this solution and more tricks on top » http://www.cerebralmastication.com/2011/04/fast-two-way-sync-in-ubuntu/
Comment: I have this same problem. Did you find a solution, mwm?
Comment: @mwm : as I have already noted at https://superuser.com/questions/34697/rsync-two-way-sync-and-syncing-file-deletion-operations , this is a misunderstanding : unison does not "use" rsync in any normally meaningful way. It is surprising how many people keep believing this.
Comment: nowadays I'm using nextcloud locally on my home server. perfect "dropbox" sollution 100% managed by me. has android/ios/win/macos/linux/web clients.
Answer: Try Unison: http://www.cis.upenn.edu/~bcpierce/unison/
Syntax:
unison dirA/ dirB/

Unison asks what to do when files are different, but you can automate the process by using the following which accepts default (nonconflicting) options:
unison -auto dirA/ dirB/
unison -batch dirA/ dirB/ asks no questions at all, and writes to output how many files were ignored (because they conflicted).
Note: I am no longer using Unison (I use NextCloud, which doesn't address the original use case). However, note that rsync is not designed for bidirectional sync, while unison is. unison may have its bugs (as any other piece of software) and its wrinkles. I am surprised it seems to be actively maintained now (last time I looked I think I thought it looked dead), but I'm not sure what's the state nowadays. I haven't had the need to have a two-way file synchronizer, so there may be better options, though.
Note 2: I am now currently using Mutagen for some purposes. It still doesn't cover the question's use case completely, but it does implement two-way sync.
Comment: Unison is pretty easy to use for small directories. But it is excruciatingly SLOW for directories with a large number of files. Where rsync will start copying almost right away, unison sometimes takes over 12 hours to start copying, because it scans every file before copying anything.
Comment: @wener Don't use a hammer for a screw, or a screwdriver for a nail. `:)`
Comment: unison is great - but also very unreliable. In about 50% of my use cases it simple has not worked. The most common cause of this is poor inter version capability. I sync sometimes with content on a computer I have no privileges on. If that server happens to have different version than I do, unison fails. In addition, I've recently had it fail between unison clients of the same version, because they were using different compiled versions of some library (this was between an ubuntu computer and a raspberry pi).
Comment: If you're synchronizing e.g. a music library where you care about 2-way file deletion but don't care about file content conflicts, you can use Unison `-fastercheckUNSAFE` flag to considerably speed up Unison. It will not compute the file content hash but just look at timestamp/length and overwite if newer. See the [manual](http://www.cis.upenn.edu/~bcpierce/unison/download/releases/stable/unison-manual.html)
Comment: It git good at this ?At least git is easy to use and transport is fast.for blob there is an extension developed by github called [Large File Storage](https://git-lfs.github.com).
Comment: Main problems of git are large files and that it is really designed for explicit commits. Both can be reasonably improved upon by add-ons such as git-annex, large file storage, etc.
Comment: The unison version needs to be equal on both sides, otherwise unison revues to work. That's why I am looking for an alternative.
Comment: By the way, if you think, that you're limited to rsync, because you don't have enough privileges to install unison - [you still may install unison using prefix](http://i3vi3v.wordpress.com/2013/01/09/zend-studio-syncing-files-with-remote-server-using-unison/) .
Comment: @ADTC, if hammering the screw in is going to work feel free to hammer away.
Comment: This isn't a scalable solution as there's no `-dryrun` and no man page. It's better to use simpler, known tools such as `rsync` than reinvent the wheel with a million options that you don't need.
Comment: AVOID USING UNISON!! I've had it running for 2 months now on a cron, haven't changed versions on either side. today i noticed some folders were missing. turns out sometimes it fails and simply deletes folders it has issues with. it ended up deleting about half of nested folders. luckily i had backups and restored everything. `rsync` is 100x more reliable than `unison`.
Comment: Unison seems to be written in OCaml. I doubt I could find a binary for my ancient Ubuntu server, and I surely can't compiler there.
Comment: @ierdna I disagree. I'm using unison for a dozen of years, and you have to read docs and not just guess, because there are caveats, but generally it's completely reliable, aside from disk read failures. I'd like to see the logs, just for self-education and out of my curiosity.
Comment: @EdwardFalk unison pre-dates Ubuntu. Unison first released in 1998, while first Ubuntu only in 2004. Which ubuntu do you have? I guess unison might be in the repo.
Answer: Since the original question also involves a desktop and laptop and example involving music files (hence he's probably using a GUI), I'd also mention one of the best bi-directional, multi-platform, free and open source programs to date: FreeFileSync.
It's GUI based, very fast and intuitive, comes with filtering and many other options, including the ability to remote connect, to view and interactively manage "collisions" (in example, files with similar timestamps) and to switch between bidirectional transfer, mirroring and so on.
FreeFileSync can easily sync two computers on the same network and also sync two computers on different and remote networks.

On same network: have FreeFileSync use the local file system on one side and a shared network drive / path on the other. On Windows systems you enable file / disk sharing on one computer and access that share from the other. I use FreeFileSync this way to keep my main development PC source code synced with my 2 laptops.
I have also synced one of these laptops with a Linux server with Samba installed and sharing one of its directories.
Across networks: create a VPN and do the same as above. FreeFileSync will see the remote disk as it was on the local network. Or buy one router that allows you to connect a USB disk to it and share over the internet. I have installed a VPN on a remote Linux server and used it through the OpenVPN Windows client.
Comment: Does not work over a network. Why would I want to sync two local folders?
Comment: @mjs I have added the answer to the above reply
Answer: I'm surprised no one has mentioned Syncthing yet. I have been using it for years to synchronize my phone, my tablet and my two laptops. One time I also used it to send 10 GB of photos to my family ~600 km away, straight from my machine to their machine, and it was incredibly fast (despite the data getting routed through Syncthing's discovery server to work around NAT issues). I also tried OwnCloud/NextCloud at some point but Syncthing has been much more reliable and, also, much faster.
Answer: You could also try bitpocket: https://github.com/sickill/bitpocket
Answer: Try this,
get-music:
 rsync -avzru --delete-excluded server:/media/10001/music/ /media/Incoming/music/

put-music:
 rsync -avzru --delete-excluded /media/Incoming/music/ server:/media/10001/music/

sync-music: get-music put-music

I just test this and it worked for me. I'm doing a 2-way sync between Windows7 (using cygwin with the rsync package installed) and FreeNAS fileserver (FreeNAS runs on FreeBSD with rsync package pre-installed).
Comment: @Lübnah: Correct; this solution is _not a full 2-way sync_: Because rsync with `--delete-excluded` is run with the _server_ as the _source_ first: files _added locally_ will be _deleted_ during that run (whereas files added to the _server_ _are_ copied to the local target). The only local files synced back to the server are files that _also exist on the server_ and were _modified locally_ (more recently than the server versions) - which is not likely in the OP's scenario. As you hint at, methinks true 2-way syncing can't be done with `rsync`, because saving state between runs is required.
Comment: Won't this delete new local files? `-u` skips files newer on the destination, but rsync doesn't have a way of tracking the age of a delete, so how could this script differentiate between when a local file is new (and should be kept) or old (and should be deleted)? Or am I misunderstanding?
Comment: Found the answer:
In addition to deleting the files on the receiving side that are not on the sending side, this tells rsync to also delete any files on the receiving side that are excluded (see --exclude)
http://rsync.samba.org/ftp/rsync/rsync.html
Comment: From what I understand, if I create a file on the destination and then execute the rsync command to copy from source to destination, it will delete the file at the destination, since no time stamps are used. This isn't what you want right?
Comment: Does `--delete-excluded` delete any files that exist on destination directories but not source directories? I thought you had to define `--exclude=` so won't rsync only delete these excludes? Or does rsync treat all non-source directory files as "excludes"?
Answer: You might use Osync: http://www.netpower.fr/osync , which is rsync based with intelligent deletion propagation. it has also multiple options like resuming a halted execution, soft deletion, and time control.
Answer: You could try csync, it is the sync engine under the hood of owncloud.
Comment: As of 2018, csync development appears to have stalled and owncloud look like they are forking it. See: https://github.com/owncloud/client/issues/4661 for more details
Answer: I'm now using SparkleShare https://www.sparkleshare.org/
works on mac, linux and windows.
Comment: as time goes by, I keep updating my solution. I'm now using nextcloud on my home server. syncing with the nextcloud client on linux, mac, windows, android and ios.
Answer: Rclone is what you are looking for. Rclone ("rsync for cloud storage") is a command line program to sync files and directories to and from different cloud storage providers including local filesystems. Rclone was previously known as Swiftsync and has been available since 2013.
Comment: Looks like rclone does support two-way (bidirectional) sync now: https://rclone.org/commands/rclone_bisync/
Comment: It does not support two way sync: https://rclone.org/commands/rclone_sync/
Comment: Aye, `rclone` seems to work quite well in its bidirectional mode! I'm a fan of all those tools — `rsync`, `unison`, `rclone` — but lately I've been using `rclone` most, since I can pretty much do bidirectional sync with basically _any_ protocol out there...
Answer: I'm not sure whether it works with two syncing but for the --delete to work you also need to add the --recursive parameter as well.
Comment: The short equivalent of option `--recurse` is `-r`, which the OP _is_ using (embedded in compressed-options group `-avzru`).

Two way sync with rsync


When laying trominos on an 8x8, where must the empty square be?

Question:
21 tromino tiles (three squares glued together in a row) can together
cover 63 squares on an $8 \times 8$ grid. One of the squares thus
becomes non-covered. What are the possible positions where the empty
square can be?

If this is the relevant object to consider, and it should be arranged only horizontally, being an odd number, it cannot fill any row in the 8x8 grid, without leaving 2 squares unfilled. These two squares are left unfilled for each row, and can be filled by putting a tile on ever row on the empty remaining 2 squares. This leaves one square  of the tile outside the 8x8 grid.  How can the question assume that  63 squares are filled out of 64, when this combination covers all 64?
Thanks
Comment: Why do you think it can only be arranged horizontally? Nowhere in the question does it say it cannot be arranged vertically.
Comment: I suspect they do not intend "row" to mean "horizontal" in this context. "In a row" means, "adjacent and in a line" (rather than some other arrangement that can be made from three squares).
Comment: @Luthier415Hz For me tt's quite obvious, from the question, that the tile can be rotated.
Comment: So you assume that the tromino can go outside of the $8\times 8$ grid? How interesting
Comment: @AdamRubinson because it says "thee squares glued in a row", and since it does not impose you to put it vertically and horizontally as you say,   then why not all horizontally?
Comment: I don't know the rules of the game at all. I see only 3 tiles that have to cover the 64 grid squares. That gives that we are looking for the maximum numbers of time 3 can divide the largest number up to 64. That is 21. The empty square left, can occur any where a row or column cannot accommodate more tiles than can divide that row/column.
Comment: @JaapScherphuis good find.  Ironic that it used the same image for the 8x8 board which I got from the dolphin tour problem nearly ten years ago (makes me feel old).  I'd delete if I could, but accepted answers can't.  That, and my answer here does more to address the issues in problem interpretation the OP here had.
Answer: First, you need to understand the rules of the game.  Tiles must be placed onto the grid such that they do not overlap one another, such that they align with gridlines but may be rotated vertically or horizontally, and such that they do not extend off of the playing area.
Now... consider coloring the board as follows:

Recognize that however you place a tromino piece it must cover exactly one of each color.  There are 22 pictured blue spaces but only 21 each of yellow and green spaces.  It must be then that after you have placed 21 trominos you will have left a blue square open.
Further, if we were to have considered a different coloring of the board like the following, then the same logic applies.

As such, the only possible spaces that could have been left open must have been blue in both images.
The only such spaces that are colored blue in both images are in chess notation: C3, C6, F3, and F6.
It remains to show that there does exist such a tiling covering 63 spaces, such as the following... tiles marked as red bars.

Finally, by rotating the pictured tiling we see that indeed all four of those spaces are possible as the final square remaining.
Comment: @Luthier415Hz the more general technique and takeaway is that when approached with a problem of this type, try to find something which is "always true" regardless what actual arrangement takes place.  Something we call "invariants."  Here, the invariant is that all ways of laying a tronimo involves covering exactly one of each color.
Comment: @Luthier415Hz a mix of that, boardgame and puzzle enthusiasm, and experience.  See for example [this problem](https://math.stackexchange.com/questions/1055087/chess-board-combinatorics/) asking if there exists a sequence of moves for a new type of chess piece called a "dolphin" such that it visits each square.  The same punchline occurs there.
Comment: A problem of this nature?  No.  This is the recommended solution and I know of no others.  You could phrase it in a more dry fashion, but the underlying argument will likely be the same.
Comment: @Luthier415Hz you can "reskin" a coloring argument using modulo arithmetics if you want. It's just that for most people, visualization using coloring is much easier to do (especially for lower to medium difficulty problems like this) than using purely numbers and formulas.
Comment: Is this based on Intuition?
Comment: Thanks..........!
Comment: Thanks,.,Are you aware of an example on MSE where this is solved purely by mathematical formulas?


convergence dominate of Lebesgue-Application

Question: my question is: let $f(x)= \dfrac{\sin (nx)}{x}.\varphi(x)$, where $\varphi \in C^\infty_c(\mathbb{R})$. We can found an function $g \in L^1(\mathbb{R})$ such as $|f(x)| \leq g$? Please.
I think that, supposing that $Supp(\varphi) \subset [-a,a]$ with $a>0$. We have
$$
|\displaystyle\int_{\mathbb{R}} \dfrac{\sin (nx)}{x}.\varphi(x)|\leq C \displaystyle\int_{\mathbb{R}} |\dfrac{\sin(nx)}{x}|dx
$$ where $C=\sup_x|\varphi(x)|$. then since $\displaystyle\int_{\mathbb{R}} |\dfrac{\sin(nx)}{x}|dx=\pi$, there exist $g(x)= \dfrac{\sin(nx)}{x} dx \in L^1(\mathbb{R})$. Is it correct?
Comment: What is the "original" limit problem?
Comment: there is no limit problem, i search if we can majorate $f$ with an function $g \in L^1(\mathbb{R})$
Comment: Then see my answer below!
Answer: You are wrong, $\sin(x)/x\not \in L^1(\mathbb{R})$. We have that
$$\int_{\mathbb{R}} \dfrac{\sin(x)}{x}dx=\pi\quad\text{and}\quad
\int_{\mathbb{R}} \left|\dfrac{\sin(x)}{x}\right|dx=+\infty.$$
See Evaluating the integral $\int_0^\infty \frac{\sin x} x \ dx = \frac \pi 2$? and Improper integral $sin(x)/x $ converges absolutely, conditionaly or diverges? .
On the other hand, since $|\sin(x)|\leq |x|$ for all $x\in \mathbb{R}$, it follows that
$$\left|\dfrac{\sin (nx)}{x}\cdot \varphi(x)\right|\leq n|\varphi(x)|$$
and  $g=n|\varphi|\in L^1(\mathbb{R})$.


Do you know an algorithm for modeling an ellipse by a series of aligned circles

Question: do you know an algorithm for modeling an ellipse by a series of aligned circles? thanks
Comment: It is not clear what you want to achieve. Are you asking how to construct the three-centre curve of a [basket arch](https://www.mdpi.com/2073-8994/11/10/1243/htm)?
Comment: I want to check the intersection between two ellipses. I have a paper which describe collision detection and the recommendation is to do via circles inside ellipse, because the algo should count the number of intersected circles. It means I will check the intersection of circles, which are inside ellipse....
Comment: @user2973395 what do mean? determine if two ellipses collide? Or determine the overlap surface?
Comment: [Like this](https://i.stack.imgur.com/1UDRu.png)? Perhaps you should link or quote the paper and illustrate what you want in the question.
Comment: https://citeseerx.ist.psu.edu/viewdoc/download;jsessionid=E9D1CACDD9068AE83B2012654E4F6C64?doi=10.1.1.369.3763&rep=rep1&type=pdf
Comment: So what you want is described by Figure 2 in that paper. Please edit your post so that it has all relevant information. People who want to answer shouldn't have to trawl the comments to piece together what they need to know. (I suggest to include a snap of Figure 2, since it makes clear what you want.)
Comment: That's probably what you want, section 2.1: https://www.researchgate.net/publication/309434476_A_new_algorithm_for_dense_ellipse_packing_and_polygonal_structures_generation_in_context_of_FEM_or_DEM
Comment: @ Ofek Shilon it looks good, i'll check it...thanks!
Comment: It is possible to calculate intersection of ellipses. Why do you need circles?
Comment: in the paper (see above) was described why circles ;) but anyway, how can i calculate the intersection of two ellipses? I think it is not easy to do it, one soution would be polygon approximation
Comment: [First](https://stackoverflow.com/questions/17213922/finding-intersection-of-an-ellipse-with-another-ellipse-when-both-are-rotated/17214400#17214400), [second](https://stackoverflow.com/questions/15445546/finding-intersection-points-of-two-ellipses-python)
Answer: If your ellipse is centered in the origin with long and short semiaxes a and b, the curvature radius r at the end of the long axis is
   r = b² − a
So, {±(a − r), 0} are the centres of the outer circles with radius r. The circle in the middle is at {0, 0} with radius b. For intermediate circles, you can describe a point on the ellipse with
   {x, y} = {a · cos θ, y = b · sin θ}
The tangent on the ellipse at this point is
   {dx, dy} = {− a · sin θ, dy = b · cos θ}
The intersection of the normal of that tangent and the major axis is the centre {x₀, 0} of a circle with radius r₀ that touches the ellipse.
   x₀ = x + y · dy / dx
    r₀ = hypot(x₀ − x, y)
The problem here is that you start with the angle θ and evnely spaced angles of the ellipse do not yield evenly distributed circles, see figure (a) below.
You can reverse the steps above to get evenly distributed circle centres. Given the centre {x₀, 0} of a circle,
   cos θ = x₀ / (a − b² / a)
    sin θ = sign(x₀) · sqrt(1 − cos² θ)
    r₀ = hypot(x₀ − a · cos θ, b · sin θ)
This will give the distribution in figure (b). You must play with the distances, so that your ellipse is covered by the circles. The flatter the ellipse, the more circles you need.
a
b
Comment: Thanks, @MBo. I agree with you that calculating the intersection of the ellipses is better, but what the OP wanted and what for came out one bit at a time, so I answered the actual question. He who asks an XY quesetion often just gets X. `:)`
Comment: ok, thanks. but what about the rotation? maybe i'll need a rotation matrix in order to rotate the ellipse and the centre of circles.
Comment: You probably know at what angle ϕ the ellipse's major axis is and where its centre {xₑ, yₑ} is. Calculate the circle centres and radii as above, then transform the centres {x₀, 0} to {xₑ + x₀ · cos ϕ, yₑ + x₀ · sin ϕ}. (That's the same as a rotation by a matrix, but the y component of the centres is always 0, so the calculation is simplified.)
Comment: Nice work. (while I believe that direct caclulation of ellipse intersection is really better:)


google home session attribute manage

Question: I am developing google home supported api, here I have facing issue with session. I like to add some object values with response of conversation.
 example : {last_intent: 'sampleintetn'}
I want to get back this data in request while user continue that same conversion.
I'm setting the contextOut parameter with an array of values such as
[
  {
    "name": "my_session_data",
    "lifespan": 0,
    "parameters": {
      "myprop": "sample property",
      "orbitaSession": {}
    }
  }
]

Comment: Can you show the code you're using that you're having problems with? Are you using the Actions SDK or Dialogflow?
Comment: I am using Dialogflow @Prisoner
Comment: I am using Dialogflow my primary expectation is in response body under which property name I want to send my session attribute like alexa @Prisoner
Comment: I have tried with contextOut, but I  did not get from next request:
example:
"contextOut": [
    {
      "name": "my_session_data",
      "lifespan": 0,
      "parameters": {
        "myprop": "sample property",
        "orbitaSession": {}
      }
    }
  ]
Answer: Contexts are slightly different than Alexa properties. The biggest difference that is relevant to what you're trying to do is that a Context can have a lifetime, expressed in number of user responses during the conversation.
A lifespan of 0 means to clear this Context. Sending the context again in your next response resets the lifetime counter.
Parameter values must also be strings, so you can't store another object in there. You can, however, convert that object to a string and store it, and convert it back to an object when you read it again later.
So something like this is more valid and will more likely do what you want:
[
  {
    "name": "my_session_data",
    "lifespan": 5,
    "parameters": {
      "myprop": "sample property",
      "orbitaSession": "{}"
    }
  }
]

Comment: I have tried with lifespan: 5 buat still i did't get back my data in next response
Comment: Can you update your question to add (1) how, exactly, you set this (2) what, exactly, you got on the next call and (3) a screen shot from the simulator illustrating the two parts of this conversation?

google home session attribute manage


handle bar complie issues

Question: uncaught exception: Template was precompiled with an older version of Handlebars than the current runtime. Please update your precompiler to a newer version (>= 1.0.0) or downgrade your runtime to an older version (<= 1.0.rc.2).
I am running handlebar with node and getting this error. not sure whats happing.
Answer: Your precompiler needs to be updated to a version greater than or equal to 1.0.0.  You could alternatively downgrade your runtime to 1.0.rc.2 or less.

handle bar complie issues


Sorting column with anchor tags in jQuery DataTables

Question: I used the jQuery datatable plugin in sort the table data. The sorting works fine if a column contains simple text. If I put any anchor tag condition on a text then the column sorting does not sort properly.
I displayed the values in following manner:
<?php if ($allAptArr[$d][27]['staffinactive'] == 1) { ?>
        <?=ucwords(stripslashes($allAptArr[$d][5]['staff_name']));?>
    <?php } else { ?>
        <a href='#' onClick="redirectToStaff('<?=$allAptArr[$d][10]['staff_id']?>');">
        <?=ucwords(stripslashes($allAptArr[$d][5]['staff_name']));?>

<?php } ?>

with this code the column sorting fails.
Comment: related http://stackoverflow.com/questions/1898621/jquery-datatable-plugin-doesnt-seem-to-sort-columns-with-links-properly
Comment: I have same issue http://stackoverflow.com/questions/41836619/jquery-javascript-sort-a-datatable-on-column-that-has-a
Answer: add this before the ready function:
    //sets up numeric sorting of links
    jQuery.fn.dataTableExt.oSort['num-html-asc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y || isNaN(y) ) ? -1 : ((x > y || isNaN(x)) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['num-html-desc'] = function(a,b) {
    var x = a.replace( /<.*?>/g, "" );
    var y = b.replace( /<.*?>/g, "" );
    x = parseFloat( x );
    y = parseFloat( y );
    return ((x < y || isNaN(x)) ? 1 : ((x > y || isNaN(y) ) ? -1 : 0));
    };

and at the ready function:
        "aoColumns": [
          { "sType": "num-html" },
          null,
          etc. etc.
        ]

This is how it works for me with anchors, integers are getting ordered as they should.
Answer: Set column type to 'html':
$('#example').dataTable( {
    "aoColumns": [
        { "sType": "html" },
        null,
        null,
        null,
        null
    ]
} );

This will strip HTML tags before sorting.
See documentation for sType parameter
Answer: I just had an issue with sorting using datatables when links were involved - I had a column that had links in some cells, and no links in others. It seemed to be sorting, but it was sorting all links and then all non links, as oppose to sorting both links and non links together. The issue turned out to be spacing in the markup. When I took away all non essential spacing around any a tags, my columns sorted perfectly.
Answer: This is quite late for the answer, still it will helpful for other readers, You can take a dummy column. Also hide that dummy column and sort actual column by dummy column. Suppose I have three columns then add fourth coulmn as dummy and put this in datatable call
$('#example').dataTable({ "aoColumns": [null, null, {"iDataSort":3 }, { "bVisible": false}]);

Comment: Jus what i wanted. Thanks!
Answer: I used the DataTables HTML5 data attributes to get the desired function of a sortable date that is wrapped in an anchor tag.
Here is an HTML snippet for one of many table cells:
01/15/2018

Answer: I think its because your are mixing not anchor with anchor data in the same column. You also shouldn't use onClick, aspecialy with jquery, just setup a click() function with the right selector.
Answer: As the guys said, not all content you put in the tds is sortable. Links for example need to be specifically disabled. The code snippet below will disable sorting for the first column in the table.

Answer: My datatable had 2 columns and first column had  element and I was trying to sort my first column but sort was not working. I tried many ways but didnot work so I added a hidden column with @Html.Raw which binded to same model property that I bind to  and sorted on my hidden column. Please see below link
Jquery/JavaScript : Sort a datatable on column that has


Parse from FILE structure to string in C++

Question: I'm trying to capture a system command on Windows with the following code, to return the output as a string.
std::string exec(char* cmd) {
    FILE* pipe = _popen(cmd, "r");
    if (!pipe) return "ERROR";
    std::ifstream ifs(pipe);
    std::string content( (std::istreambuf_iterator<char>(ifs) ),
                         (std::istreambuf_iterator<char>()    ) );
    printf("%s", content);
    return content;
}

When I call the function like this:
char *command = "set";
std::string results = exec(command);
printf("%s", results);
getchar();

The output is just a few random bytes.
    ╝÷:ö°:

I was trying to get all the results appended in 1 long string.  Can anyone tell me what I'm doing wrong?
I tried redirecting the stderr to output with the command but it also gives some random bytes.
Comment: Will that even compile? There are no [`std::ifstream` constructor](http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream) in the standard library which takes a `FILE` pointer.
Answer: Since you're using printf() which knows nothing about C++ std::string values, you need to print the C string representation of the content:
printf("%s", content.c_str());

The printf() function was told to expect that, but it wasn't what you passed to it.
Or, as others pointed out, you should use the native C++ I/O:
std::cout << content;

Answer: Printf expects C string, char*.
Use
printf("%s",results.c_str());

Answer: Don't use printf, use the C++ standard output stream instead:
std::cout << content << '\n';


Find CSS selector for popup menu item for Selenium in Python 3

Question: I'm attempting to make a DeviantART Llamabot for a friend as my first Selenium project with Python 3. I have the bot 99% working except for being able to find the "give llama" button.
The problem seems to be that the menu the button appears under is a popup and I can't just right click and select "copy css selector" in Firefox. As soon as I inspect the element for the give menu the menu closes and the html changes.
I've managed to take a screen shot of a random sample page of the code so I can even see what's there. I've tried learning how CSS selectors work from scratch myself and I've managed to find every nested element EXCEPT the actual items in the list. I've tried looking for Nth child and using the ">" operator. I've attempted searching by class name, name, Xpath, link name, partial link name, nothing has worked.
I've read about this problem inspecting popup elements elsewhere and the suggestions are effectively to write an HTML parser or something to copy the entire html code as it changes and then select it from your copy. I'm not going to do that for this project. It's entirely too much work unless I absolutely have to for some reason.
Honestly at this point I don't even care anymore and I just want someone to outright just tell me what to type in so I can finish this project. This is the screenshot I managed to get. I'm looking for the item highlighted in blue.

Since my code was requested for clarification here it is
<code>from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.action_chains import ActionChains

binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary)

Deviant = browser.get("http://www.deviantart.com/random/deviant")
GiveMenu = browser.find_element_by_css_selector(".i47")
GiveMenu.click()
#GiveLlama = browser.find_element_by_css_selector("")`
</code>
Everything except the last line works which is why the last line is commented out until I can figure out what to put in there. No matter what I've tried, including the examples provided by the people answering this question so far, I either get a no such element error or an illegal syntax error.
Comment: I suggest breaking your question into paragraphs, as it is a little hard to read. Also, copy and paste all your code , which relates to your question, directly in your question.
Comment: I don't understand. You're looking for the item highlighted in blue? Its CSS selector is right there at the bottom of your Firefox inspector, and it has the class `.givellama`.
Answer: You can use the xpath selector, in chrome you can use rigth click and click on copy > copy Xpath, or the element selected on your inspector is "div.popup2 .blockmenu a.f.givellama"
Answer: You should click the "Give" button first, when the pop up dialog opens, inspect the "Give a Llama Badge" element and identify the xpath. Here is a screen shot.
screen shot to locate "Give a Llama Badge" button
You can find that, the xpath to locate "Give a Llama Badge" button(Should click the Give button first to locate this element).
<code>//a[@class='f givellama']
</code>
The xpath to locate "Give button"
<code>//a[@href='#give-give-give']/span[text()='Give']
</code>
As you pointed out, when the popup dialog opens, if you try to test the xpath in the browser console via "Firepath" or others, when you type "Enter" or click the left mouse, the popup windows is closed. But don't worry about this, since you already identified the xpath locator, you can debug it in your scripts.
Comment: Thank you, I thought i was already using Firebug and Firepath but I was mistaken. This appeared to work. It's still giving me a nu such element error but that might be because I need to put in a wait of some kind to guarantee the elements have loaded.
Comment: @Shoopdeboop sounds good. When developing selenium scripts, if the xpath locator is correct and validated via Firebug, if there still is 'no such element' error, just think about the browser loading time. I usually call a 'wait for element' to avoid the loading issue.
Comment: I got it to work, it clicks what I want it to click, now I'm just adding the rest of the code to make it a usable tool for them. Thanks for your help.


Why can't I add a property in a publish function?

Question: I have a frustrating problem:
    Meteor.publish('userBox', function() {
    if (Meteor.user()) {
        if (UserBox.boxId) {
          return UserBox.find({boxId: Meteor.user()._id},
                              {'balance': 1,
                               'myGames': 1,
                               'createdGames': 1
                              });
      } else {
          UserBox.insert({boxId: Meteor.user()._id,
                          balance: 111,
                          myGames: 7,
                          createdGames: {}
                         });

          return UserBox.find({boxId: Meteor.user()._id},
                              {'balance': 1,
                               'myGames': 1,
                               'createdGames': 1
                              });
    }
  }
});

Why doesn't it work? I just want have another collection which will keep some object in advance to Meteor.users and where I can keep any additional property for current users.
Comment: in what way does it not work? Are you getting an error? or does the new object simply not show up in the collection? Please elaborate a little.
Comment: ya new object not show up in the collection. i don't get any errors. Just after restart app i don't see any information in UserBox collection, because it's still empty. even if u logged in app or not.

for example: in addition to the above code i create helpers:

http://plnkr.co/edit/joSx4eoeHekjEfRL9rm0?p=catalogue

and in template i put this cod:
http://plnkr.co/edit/DfXbwgKEOEeYcro31pu4?p=catalogue

but it doesn't work, cuz UserBox don't have any property
Comment: have you verified that manually inserting into the UserBox collection works?
Comment: yes, manually it's work via mongo comand. but what the point
Comment: Have you tried it from the meteor shell?
Answer: The condition is never true, because Meteor.user() doesn't work in publish functions:

Meteor.user()                 Anywhere but publish functions

http://docs.meteor.com/#/full/meteor_user
Use this.userId instead.
Comment: Did you also change anywhere you were using Meteor.user()._id?
Comment: no even this don't work, i have no idea why i cant add property in users object.
http://plnkr.co/edit/HanVlQgyG9xwfWfEznC5?p=catalogue
Comment: you are not trying to add anything to the user's object (at least not in your code above).
Answer: finally... it was so simple... http://docs.meteor.com/#/full/accounts_oncreateuser


Howto limit access to a main menu link using drupal 7 api

Question: in my scenario I am using Drupal 7 with the ubercart modules. I like to add a link in my main menu that is only visible if you are an authenticated user.
For testing purposes I am using the bartik theme.
It is the same function like user/login, which is only visible if you are an anonymus user.
I found some hints and even examples in the drupal api - like
https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_menu_alter/7
The following implements hook_menu_alter to remove access to the "forum" tab for the anonymous user:

/**
* Implements hook_menu_alter.
* Check access for forum menu item.
*/
function MYMODULE_menu_alter(&$items) {
$items['forum']['access callback'] = '_accesscheck';
}

/**
* Callback to disallow access for the anonymous user.
*/
function _accesscheck(){
  global $user;
 return $user->uid;
}

Even after many attempts, I can't catch a value for $items, I was using main_menu.
I like to use the function in the theme template. How can I find the variables that I need to alter one item of my menu - I called it > cart <.
I even tried to print_r($items), but nothing was found.
How can I retrieve $items for the main menue
Comment: Is the /forum path a View by any chance?
Comment: sorry its not a view, I just copied the example and hoped to find the right value for the main menu. I only extended the main menu using the user interface, created a new link and gave it a name [cart] and a path [cart]. All I want is that the link becomes invisible for guests.
Comment: That's a good thing, Views paths can be a pain to work with. Usual suspects: Is the module definitely installed? Have you cleared the caches since implementing the hook? (your code looks right to me BTW)
Comment: @FreeRadical I guess support for this one goes over to you then ;)
Answer: hook_menu_alter only gets executed when you clear cache or rebuilding menu. I would suggest if you want to debug the $items variable, What I used to do
is do print_r($items) and die() after that. Then I would try to clear the cache from the admin UI or save it into watchdog for debugging.
Answer: Please note that in most cases, Drupal will manage menu item visibility and access via menu items just fine by itself.  If you link to "something" in the main menu, and access to "something" is limited, Drupal will hide the menu item from those that shall not have access.  In other words: In most cases, there is no need for you to do anything. (i.e.: there is no need to use hook_menu_alter, etc.)
In some cases, it doesn't work like that.  The example you've copy-pasted from is a remedy for one of these rare case.
I suggest you first try to create the menu link normally in the admin panel, set access control for whatever it is it points to only allow the authenticated user access. Clear caches, and test. If all went well, the menu item will not be visible for the anonymous user when you test it.
And if this is one of those pesky cases where the above don't work, come back here. Edit your question an give us all the details about what you're linking to and how access to that is controlled. Then we need to review other options.
The reason you're unable to "catch" a value for $items by placing a print_r statement inside hook_menu_alter is because this hook is not invoked on page loads. It is only invoked when the menu list is rebuilt. This happens when a module is installed/uninstalled, or when all caches are cleared. However, he output of print_r may be lost if it is output outside of a page load.  Instead, to debug $items, install and enable the Devel module and use the following instead of print_r.
dpm($items, '$items');

The output of dpm, unlike print_r is not output immediately, but queued and will be shown on the next page load.
However, in this case, I don't think you will learn what you need to know in order to block access by looking at $items.  Instead, just figure out what paths of the menu items you want to block access to. It is not what the menu is called that matters, it is the path it leads to.
Comment: I set all permission as usually, especially for the cart I only gave permissions to the admin. But this was not enough. Now for testing purposes I activated the forum and and typed down the code for the access limitation. Beside of that I created another link - to a node [ a basic page] and another to the cart link. I used the code like this. function zuser_menu_alter(&$items) {
$items['forum']['access callback'] = '_accesscheck';
$items['cart']['access callback'] = '_accesscheck';
$items['impressum']['access callback'] = '_accesscheck';. The limitation is working only for the forum
Comment: What is the path to the cart landing page and the impressum landing page?


Stats on categorical variables

Question: For a little background, I have 2 groups in my analysis. I have coded some lab results as 1 = bacterial colonies are red 2 = bacterial colonies are dark and shiny and 3 = bacterial colonies are black and rough. I would like to do some sort of statistical analysis on these so that I can tell if there is a significant difference between the numbers I have recorded for one group compared to the other, but the columns are different lengths as I do not have an equal number of samples in each group. Is there any tests anyone could recommend (have been working on minitab but also happy to use other softwares like prism).
Comment: A simple chi squared test should do this, maybe with an adjustment to account for the small sample size (as for example simulate.p.value in R).
Comment: I think some sort of test on proportions is what you're looking for. You may want to take a look at https://radiant-rstats.github.io/docs/basics/compare_props.html
Comment: @BruceET: What do you mean by "not enough data even for that"? The p-value from this (sim=T) appears in your response, so I gather you don't think it's wrong to run it. (Except it's not significant in this case, but that's just how it is, isn't it?)
Answer: It seems you may not have enough data for a definitive
test. Ordinarily, I would make a $2 \times 3$ table of counts
with groups as rows and colony type as columns. Then do a
chi-squared test for homogeneity on the table:
so = c(5, 3, 1);  sp = c(2, 9, 4)
TBL = rbind(so, sp);  TBL
   [,1] [,2] [,3]
so    5    3    1
sp    2    9    4

chisq.test(TBL)

        Pearson's Chi-squared test

data:  TBL
X-squared = 4.8914, df = 2, p-value = 0.08666

Warning message:
In chisq.test(TBL) :
  Chi-squared approximation may be incorrect

Data are too sparse to give large enough expected counts for
the the "chi-squared statistic" to have nearly a chi-squared
distribution. So the P-value may not be correct.
Here are the expected counts, computed from the row and column
totals of observed counts in TBL.
chisq.test(TBL)$exp
    [,1] [,2]  [,3]
so 2.625  4.5 1.875
sp 4.375  7.5 3.125
Warning message:
In chisq.test(TBL) : Chi-squared approximation may be incorrect

For an accurate P-value all expected counts should exceed $5$ (with
perhaps an exception or two as small as $3.$ But these counts are
clearly too small.
As implemented in R, chisq.test can simulate a useful P-value:
chisq.test(TBL, sim=T)

        Pearson's Chi-squared test
        with simulated p-value
        (based on 2000 replicates)

data:  TBL
X-squared = 4.8914, df = NA, p-value = 0.1304

However, the P-value is not below 5%, so we cannot reject
the null hypothesis of homogeneity at the 5% level.
R also has a version of Fisher's exact test that will
accept tables larger than the traditional $2\times 2.$
But Fisher's test also fails to find a significant result.
fisher.test(TBL)

        Fisher's Exact Test for Count Data

data:  TBL
p-value = 0.1375
alternative hypothesis: two.sided

Note: If the proportions of counts were to persist for
an experiment with twice as many colonies, then you would
have enough data to detect a significant result. But the
only way to know that is to do a larger experiment.
fisher.test(2*TBL)

        Fisher's Exact Test for Count Data

data:  2 * TBL
p-value = 0.01114
alternative hypothesis: two.sided

Comment: Thank you so much for your help, that's really clear and when I repeat the experiment I now have an idea of how many different colonies I need to be able to detect a significant difference. Thanks again :)

Stats on categorical variables


Wordpress Custom loop with two types of posts with different amounts

Question: I have a category names "notes", I need on every page of the loop to show up to 10 notes, and up to 4 regular posts.
They both should be ordered by date.
i.e. I posted 1 note, note A, 1 hour ago and another note, note b, 5 hours ago, I also posted to posts, post A and post B 2 hours ago. in my loop I want to see note A,post A,post B,note B.
I hope it's clear enough.
I'm trying to do this with two custom WP_Queries, but I have hard time with them because of the global $post.
Any help would do!
Thanks
Answer: Well, if anyone is interested, this is how I did it:
<?php
function elis_sortbydate ($a, $b) {
$time_a = strtotime($a->post_date);
$time_b = strtotime($b->post_date);
if ($time_a == $time_b) return 0;
return ($time_a > $time_b) ? -1 : 1;
}
$query = array_merge(array('cat' => 67, 'posts_per_page=10'), $wp_query->query);
    $notes_query = new WP_Query($query);

    $query = array_merge(array('cat' => -67, 'posts_per_page=4'), $wp_query->query);
    $posts_query = new WP_Query($query);
    if ($notes_query->have_posts() and $posts_query->have_posts()) {
        $all_posts = array_merge($notes_query->posts, $posts_query->posts);
        usort($all_posts, "elis_sortbydate");
        foreach ($all_posts as $post) { ... }
            }

the resulting $all_posts array is sorted by date (from high to low)


Combine all columns of a data.frame in a single one (vector concatenation)

Question: I want to convert this data.frame
C1  C2  C3  C4
q   t   p   k
g   l   i   y
f   d   t   r

to a data.frame with a single column with this order of  elements q,t,p,k,g,l,i,y,f,d,t,r. Note that it is not a concatenation of columns.
Result:
C1
q
t
p
k
l
i
y
f
d
t
r

Answer: We can transpose the data.frame to a matrix, concatenate to convert it to vector and create a single column data.frame
data.frame(C1 = c(t(df)))
#   C1
#1   q
#2   t
#3   p
#4   k
#5   g
#6   l
#7   i
#8   y
#9   f
#10  d
#11  t
#12  r

data
df <- structure(list(C1 = c("q", "g", "f"), C2 = c("t", "l", "d"),
C3 = c("p", "i", "t"), C4 = c("k", "y", "r")), class = "data.frame",
 row.names = c(NA, -3L))

Comment: So, the result is the concatenation of the columns of the matrix transposed. But you do the concatenation converting it to a vector and finally to a data.frame. Thank you.


Recursive ARIMA

Question: I am trying to implement recursive ARIMA that would just update the parameters with new data point, rather than re-estimate them from scratch, without taking into account the previous model.
What I have in mind was proposed in:

A. K. Rao, Y. -. Huang and S. Dasgupta, "ARMA parameter estimation using a novel recursive estimation algorithm with selective updating," in IEEE Transactions on Acoustics, Speech, and Signal Processing, vol. 38, no. 3, pp. 447-457, March 1990, doi: 10.1109/29.106863

However, I have difficulties finding in the recent papers that anyone is using a similar approach and how is it performed. I would be grateful to hear from someone who might have tried something like that or some reference.
With increase of the data by adding new data points, execution of reestimation on the whole set increases. That is the main reason why I would aim for parameters update, rather than reestimation.
Comment: You may think of the Bayesian approach also.
Comment: I think the state-space representation that is typically used for estimating ARIMA models has been designed specifically for that.
Answer: I have managed to implement this through Kalman filter, with the help from GitHub:
SARIMAX: incremental Kalman filter (question)

Basically, with initial parameters and covariance of the initial model:
<code>        mod_test = ARIMA(X[t:t+1],order=best_cfg,exog=Xtemp[t:t+1])
        mod_test.ssm.initialize_known(model_fit.predicted_state[..., -1],
                                      model_fit.predicted_state_cov[..., -1])
        model_fit = mod_test.filter(params_training)
        params_training = model_fit.params
</code>
There is also extend function, however, I am not sure if it works the same. My results of forecast MSE for rolling window validation of 1300+ records show the negligible difference:
<code>model_fit = model_fit.extend(X[t:t+1])
</code>

Recursive ARIMA


como faço para inserir variáveis de uma query select em um bancos de um servidor diferente?

Question: Gostaria de fazer uma conexão em php em dois bancos de dados só que estão em redes distintas, nessa conexão precisaria fazer uma busca em um banco em um servidor e inserir numa tabela em um banco em outro servidor
por exemplo:
$conecta = mysql_connect("rede1","usuario","senha");
$db = mysql_select_db("banco1");

e colocar uma segunda conexão :
$conecta1 = mysql_connect("rede2","usuario","senha");
$db = mysql_select_db("banco2");

//aqui é onde eu faço a query para buscar os dados que vai ser armazenados nas variáveis
$serial = $_POST['serial'];

$query = mysqli_query("select coluna1 coluna2... from tabela1 where coluna 1 = '$serial'");

$result = mysqli_fecth_array($query);

$qry = $result['coluna2'];

por exemplo:
$sql = mysqli_query($conecta,"select * from tabela1");
$result = mysqli_fetch_array($sql);

e com resultado dessa query faz um insert em uma tabela no banco no outro servidor
$insert = mysqli_query(conecta1."insert into tabela2 values ('resultado da query acima')");

eu sei que dá pra fazer o insert com o select dentro
por exemplo:
$insert = mysqli_query($conecta1."insert into tabela2(".."select '$qry'... from tabela1) on duplicate key update coluna2 = values(coluna2)...");

eu preciso preencher uma tabela de banco de um servidor tal e esse preenchimento vai ser o vindo de uma query select, onde os dados vai ser armazenados em uma variável vinda de um primeira query select
Fiz dessa maneira mas não deu certo, alguém sabe se existe em php uma maneira de fazer isso ? desde então agradeço.
Comment: Isso é uma questão de rede e não de PHP.
Comment: Não poderia usar um BD por vez?
Answer: Nessa altura do compeonato você não deveria estar usando mysql_*, foi depreciado, php7 já não funciona mais,
você deveria estar usando pdo, mysqli_ ou algum framework.
Mas se você insiste em usar mysql_
$conecta1 = mysql_connect("rede1","usuario1","senha1");
mysql_select_db("banco1", $conecta1);

$conecta2 = mysql_connect("rede2","usuario2","senha2");
mysql_select_db("banco2", $conecta2);

mysql_query('select * from tablenamebanco1', $conecta1);
mysql_query('select * from tablenamebanco2', $conecta2);

Se você não passar o identificador no final a ultima conexão criada será usada.


Para que serve o retorno de um método na linguagem C#?

Question: Para que serve o retorno de um método na linguagem C#? Quando o método recebe os parâmetros ele gera um retorno dependendo do sua estrutura, qual seria a função deste retorno gerado?
Answer: Primeiro precisa entender o que é uma função, e saber que um método é só uma função que tem um parâmetro oculto chamado this que recebe o objeto que está usando, a não ser que ele seja estático. Pode ler mais sobre em Qual a diferença entre funções e procedimentos? que diz que a função recebe parâmetros, executa um algoritmo e retorna um valor (se não retornar um valor é um procedimento). Também pode ver Diferença entre um método com retorno e sem retorno.
Na matemática a função justamente é produzir um resultado. É ter uma fórmula que usa em vários momentos da solução de um problema geral, então em vez de ficar repetindo a mesma fórmula para todo lugar pode só chamar a função e ela produz o resultado que deseja. Além de ficar um código menor tem uma informação canônica, o que deve calcular para chegar naquele resultado, em apenas um lugar só, que provavelmente é o princípio que ajuda organizar código mais importante que a pessoa deve saber, o tal do DRY.
Em função mesmo, que não seja só um procedimento, não faz sentido não produzir um resultado. E produzindo o resultado tem que mandar para quem chamou a função, isso é o retorno.
Por isso que se diz corretamente que a função gera um resultado, o retorno é equivocadamente usado quando isso é apenas o transporte desse valor.
O retorno, a grosso modo, é o mesmo mecanismo do parâmetro, só que inverso, o parâmetro é o meio de transporte para quem chamou enviar um valor para a função e o retorno é o mesmo para transportar de volta da função para quem chamou.
O retorno não acontece dependendo da estrutura, o valor que retorna obviamente pode variar de acordo com o algoritmo executado.
A analogia usada na outra resposta é boa, mas ela está errada. Quem chama a função está fazendo a pergunta. A pergunta pode ser até considerada o cabeçalho da função, ou seja, seu nome e parâmetros, o corpo da função é a solução do problema, ou seja, é como a resposta para a pergunta será realizada, e finalmente gerará um resultado que é a resposta da pergunta. O retorno é o que determina como a resposta será dada.
Outra coisa que pode ser de interesse da pergunta e talvez não tenha sido escrito é sobre o tipo do resultado da função, e portanto o tipo do que retorna. Em geral falamos que o tipo do resultado é o tipo da função. Estritamente não é, uma função não tem tipo, mas todo mundo entende isso como sendo o tipo do resultado.
Mais um ponto que poderia confundir é o comando return. Ele serve para duas coisas, determina o controle de fluxo que encerra imediatamente a execução da função e pode, na maioria das vezes, isto é, em funções de verdade, que um valor seja retornado para quem chamou. Pode ver mais em Por que usar somente return, sem retornar nenhum dado? e O que o "return" faz em Python?.
Em C# é possível simplificar uma função de um algoritmo de uma linha e que só retorne o resultado:
int Soma(int x, int y) => a + b;

Chamando:
var resultado = Soma(1, 2);

Então resultado terá o valor 3.
Coloquei no GitHub para referência futura.
Answer: De forma bem simples, usando uma analogia, imagine que o método é uma pergunta e o retorno do método é a resposta. A implementação do método seria a solução para chegar na resposta.
function somar(a, b) {

    var resultado = a + b;

    return resultado;
}

var resultadoDaSoma = somar(1, 2);
print soma; //3


jQuery time difference of utc offSets

Question: I want to find time difference between user's time and server side time. Like what time is user having on his system and what's the value coming from database. I have got it approximately.
But now my problem is what if user is from difference time zone rather then the value is coming from database? So now I want to find the exact difference by managing their time zones. Here I am putting my sample code and the values I am getting with their formats.
<code>    var studentSessionStartTimeDate = that._sessionData.studentSession.dtStart; //date with time from db
    var splitTimeDate = studentSessionStartTimeDate.split(' ');
    var oldDate = splitTimeDate[0]; // split date different
    var dateold = new Date(oldDate).getTime(); //convert date into milliseconds
    var time=that._sessionData.studentSession.start;  // User's exact start time from db
    var convertedTime=(Number(time.split(':')[0])*60+Number(time.split(':')[1]))*1000;  // converted time in milliseconds
    var sessionStartTime = dateold+convertedTime;  // db time and date value in milliseconds
    var systemTime = new Date().getTime();   // user's system time value in milliseconds
    var timeDiff = sessionStartTime - systemTime; // now i want this time diff to be exact but it is not calculating the zone difference
</code>
Now if someone want I can get utc Offset from db also, but how can I get difference?
Comment: Take a look at this link http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/
Answer: you could get browser time zone with following code:
new Date().getTimezoneOffset();
Comment: yes i know this. but i am not able to write code for this particular part where i can manage there utc calculation

jQuery time difference of utc offSets