how optimize small linux server?

Question: I have a small Ubuntu server with 512MB of ram and several php/mysql websites (~20) running on it.It's been running for 2 years now without a single issue. But I think the moment for some optimization has just came.
The website that are hosted on this server are very low traffic, but I see some CPU issues now that they started to get more visits.
First thing I'm trying to do is identify the issues, so I've been playing around with several command line tools, like top htop apachetop, etc, and workbench for monitoring MySQl.With these tools is kind of difficult to know what i's going on exactly. For example, I created a PHP daemons that is running in the background, and every 10 seconds stores in the database the CPU usage.If the CPU is bigger than 60%, it stores the output of apachetop. This hasn't been specially useful since many of the records are OPTIONS * HTTP/1.0. Also, it looks like it's a list of the last requests, not the current request.
Seconds, when I try to monitor what's going on using htop, I always see ~15 records like:
6905 mysql     20   0  462M  110M  5220 S  0.0 21.6  5:29.79 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-external-locking --port=3306 --socket=/var/run/mysqld/mysqld.sock

and another ~15 like:
23382 www-data  20   0  259M 27384  4820 S  1.3  5.2  0:01.00 /usr/sbin/apache2 -k start

here's an screenshot:
I'm not sure if that amount of processes should be open all the time or this is something I can optimize.
I can upgrade this server now, I just want to optimize it as much as possible before doing it.
Also, I turned on the log for slow queries, the only queries there are all queries that are executed when the server's CPU is over 90%. This just confirms that php/mysql I'm using (custom) is not very heavy. I can say that the website with more traffic has 300 unique visitors and 1200 page views a day. All other websites have much lower traffic.
1) I'm looking for suggestions of how to monitor this server efficiently.
2) I need suggestions for MySQl and PHP. For example, how much memory should I assigned to them or which options should I turn on/off.
Thanks!
here's a link to a screenshot of htop: http://www.flickr.com/photos/58586868@N02/5375745794/
Answer: As one might guess from some of my previous posts I run collectl everywhere.  Download/install the rpm from sourcefore and '/etc/init.d/collectl start' and you're done.  It will collectl over a couple of hundred performance metrics every 10 seconds at <0.1% of the cpu.  It will also take a snapshot of process data every minute since that's a heavier weight operation.  You can then play back the collected data in a variety of formats or even plot it with a web-based tool called colplot, which is part of the collectl-utils package.
Looking at brief and/or incomplete pictures of what the system is doing ain't going to get  you there.  You need a longer-term picture of fine-grained data that you can drill down into to see what's really happening.
-mark
Answer: How to tune a Linux server, running Apache, PHP and MySQL would fill at least 4 large books - and there is no magic solution.
However in my experience, most of the performance problems on LAMP stacks are poorly written database queries. You have discarded this as a possible contribution to what you perceive as a performance issue - I'd recommend you go back and have a very long look at this.
Once you've sorted out the database, the other things to check are that you are compressing all html, javascript and css (mod_gzip) and PHP generated HTML (output buffering). Do make sure you've got an opcode cache running (like APC). And do make sure you're serving all your static content with sensible caching info.
System metrics are good for telling you how the performance of a system is constrained - NOT what the performance actually is. Start recording %D in your apache logs, and analyse the results to find out how your system really is performing. Prioritize the URLs with the highest totals for optimization.
The details you've provided from your system are very contextual - assuming that you're webserver is setup correctly, they suggest that the CPU usage is mostly down to mysql - but if the webserver config is wrong then the opposite may be the case.
It would have been helpful if you'd provided the apache config (less the comments).

I turned on the log for slow queries, the only queries there are all queries that are executed when the server's CPU is over 90%

Sounds like you set too high a threshold. Try setting the threshold to 0 and write a parser to strip the parameters out of the queries to identify which ones really are slow. Or just find one already written.
Answer: Propably your table are growing with time and you're losing on the mysql buffers. Try to find out if your tables are of type MyISAM or InnoDB and tweak buffer settings accordingly.
If you don't use InnoDB turn it off. InnoDB has buffer set up with innodb_buffer_pool_size and MyISAM uses key_buffer. You can find both values at mysqladmin variables.
As the tables grow the same queries that were previously fast work slower and slower.
Regarding knowing what the computer does? It's difficult. Try to find the exact times when the system was busy and check out both system and user crontabs and webserver access logs.
Answer: You need more information.  Is the server always running slow?  Or only during certain time periods?  What time periods?  What does the box look like during those time periods?
I would look into something like Munin.  It's a very simple system monitoring app that collects data and presents it as graphs.  They have plugins for Apache and MySQL to track accesses, processes, and volume.
The best thing about Munin is that you get a lot of information for almost no work.  Installation and setup is a 5 minute job if you do it manually, and a 30 second job if you are using Debian/Ubuntu.  The only thing you have to do after dropping it in is add an entry for the host to the config and make sure the plugins you want are enabled.
Munin, or something similar, will help give you some current baselines for what your system looks like.  This will be very useful as you start making changes, tweaks, and tuning the applications, as you'll have better feedback on what's working.  Then, you can start digging into the more specific places, and improve the MySQL performance, and the Apache performance, and anything else running on the box.

how optimize small linux server?


The email address is already taken

Question: I'm trying to edit a user, but I keep getting the following error:

The email address [their email] is already taken.

I'm not editing their email. In fact, I could open the edit page, just hit Save without changing anything, and I would still get the error.
Why am I getting this error message?
Comment: It appears that email is already in the database on another row. Could it be you've accidentally created or imported your users twice? If you do not use the forms but modify users programmatically, it can appear that the same email is added to the database twice (the form validation does not run in those cases). Then later when you try to edit one such user, it cannot save because that account should not have existed in the first place. Double check that you have no modules that can create accounts from email addresses (eg. Drupal commerce) and check your database `users` table.
Comment: @Neograph734 Boy do I feel dumb. I forgot I had imported users for testing on this dev version. One of those happened to share my admin email.
Comment: I've added a slightly more complete answer. Glad to be able to help you :)
Comment: You can use the module at https://github.com/Fonata/shared_email - thanks to @burgdawg!
Answer: It appears that email is already in the database on another row. This can have many causes; importing users twice, creating accounts in a custom module without proper checking if the email address is available, etc.
If you do not use the account edit forms, but modify users programmatically, it can appear that the same email is added to the database twice (the form validation does not run in those cases). Then later when you try to edit one such user, it cannot save because that account should not have existed in the first place. This measure is there so users cannot sign up with the same email twice, but since the user creation and edit form are in fact the same form (and as such have the same validation), it can happen during the save of a user as well.
Double check that you have no modules that can create accounts from email addresses (eg. Drupal commerce) and check your database users table to identify the duplicate account.
Answer: I got this same error because an external integration was creating user accounts with duplicate email addresses.  The solution for me was to use drush to add the role:
drush urol "role" --name=username

The email address is already taken


Freeswitch set variable using Perl

Question: There is a dialplan that bridges the incoming call to three users in a round robin fashion.
It makes a webservice request using curl as follows :
<action application="curl" data ="http://10.0.0.90:3081/routecall?number='${caller_id_number}' " inline="true" />

The curl service adds the number of the caller as get param and returns following parameters separated by a special character "-" :
Ex : Tonny_Stark-1002-1003-1005

Display Name of caller (Tonny_Stark)
The first client to be bridged (1002)
The second client to be bridged (1003)
The third client to be bridged (1005)

I have to parse this response into different variables and pass it to the bridge action.
I have written the Perl script for parsing as follows :
#!/usr/bin/perl5.8.8

$curl_response= $ARGV[0]; // The response from webservice.
$param_name = $ARGV[1] ;  // The parameter that is needed.

@params = split(/-/, $curl_response);

print  getParamValue();

sub getParamValue(){

if($param_name eq  "display_name")
{

  return $params[0];
}
elsif($param_name eq "first_cce")
{

  return $params[1];
}
elsif($param_name eq "second_cce")
{

  return $params[2];
}

}

The call to this perl script is made as follows :
  <action application ="perl" data= "firstcce.pl '${curl_response_data}' 'first_cce'}" />

Need the response from the perl script to the bridge action :
<action application="bridge" data="{sip_contact_user ='${display_name_from_perl}'} [myid=bridge1]user/${first_cce_from_perl} | [myid=bridge2]user/${second_cce_from_perl}"/>

**But don't know how the perl script will return the values and assign it to the variables, which can be used in the bridge action. **
Comment: You got down voted, because it's unclear what you want. You give `Perl`, but I don't see any Perl. You don't explain what that line where the data comes from, what the data looks like, or what your Perl script does.
Comment: Please provide a reason for down-vote
Comment: The response from webservice along with which parameter is needed is passed to the perl script as extra arguments. The script spilts the above response into array. Don't know how it will return a value and assign to a variable in freeswitch
Comment: Have added the required information. Please suggest a way forward.
Answer: Got it. Answering my own question.
Declare a variable in dial-plan.
<variable name="var_name" value =""/>

Access it from Perl script :
 $session->setVariable("var_name",  $value);

Modified the Perl Script as follows :
#!/usr/bin/perl5.8.8

$curl_response= $ARGV[0];

@params = split(/-/, $curl_response);

getParamValue();

sub getParamValue(){

 $session->setVariable("display_name",  $params[0]);

 $session->setVariable("first_cce", $params[1]);

 $session->setVariable("second_cce",$params[2]);

 $session->setVariable("third_cce" , $params[3]);

}
1;

Thanks.

Freeswitch set variable using Perl


Learning an alternative moving-do solfege -- futile?

Question: Here's a fairly specific but multi-faceted question.
I can only solfege (moving-do) in my head up to about Allegro 16ths at best, and I've been thinking if maybe there are ways to speed up. I noticed that I can solfege faster in a Japanese accent than I can in English (sol is so, ti is shi etc., all with stiff vowels), which might suggest that my solfege speed is limited by the length of the syllables.
On the other hand, I have no way of distinguishing certain pitches in chromatic solfege, which I've been meaning to learn: re and le are the same to me when I'm thinking in Japanese. So there seem to be pros and cons to solfeging in English and solfeging in Engrish.
Also, my friend, a composer, pointed out that perhaps people should be taught to think in chromatics rather than in diatonics -- which would be quite difficult to do, even with the standard chromatic solfege, since the consonants still imply a diatonic note.
With all that in mind, I thought maybe I could come up with my own solfege syllables that would be just as fast if not faster than do re mi in Japanese, but would also allow me to discern accidentals.
Interestingly, the ordering of consonants in the Korean alphabet seem perfect for the purpose -- there are 14 basic consonants:
ㄱ   ㄴ   ㄷ   ㄹ   ㅁ   ㅂ   ㅅ   ㅇ   ㅈ   ㅊ   ㅋ   ㅌ   ㅍ   ㅎ
g   n   d   r   m   b   s   -/ng    j   ch  k   t   p   h

Leaving out ng and h, there's just enough for an entire octave of chromatics. Then, unlike standard solfege, I could think of each note as an independent chromatic note rather than as a raised or lowered diatonic.
If I opt for a diatonic solfege, I could use just the first 7 consonants and distinguish accidentals by vowel. It could be faster that way since then I could omit more time-consuming consonants like m and n. "pa pa pa pa pa" seems to be easier to say than "ma ma ma ma ma", though they both use the same parts of the mouth.
The problem though is that unlearning solfege is like trying to rewire my brain with a fork, so if it's not a worthwhile endeavor, I'd rather not. Is there anything wrong with this idea that maybe a linguist can point out?
To summarize, my questions are:

Is my ability to solfege limited by how fast I can think the syllables in my head?
Is my ability to think syllables in my head limited by how fast I can say them out loud?
Am I right in assuming that some syllables are inherently faster to pronounce than others?
Would there be any benefits to a style of chromatic solfege that doesn't suggest any diatonic aspects? Would I be better off if anything just using 7 consonants and distinguishing accidentals by vowel?
Comment: Miyasaka San: i am from Indonesia and i solfege using the Indonesian language. My country uses a numbering system for writing the solfege, so "re" will be written as "2".
Answer: You say

I can only solfege (moving-do) in my head up to about Allegro 16ths at best

Congratulations. That's more than enough for most purposes. I am not sure you need more but adapting the note names to your language basic phonemes is a very good idea. Constructing a dodecaphonic version of note names is fun too. But good luck to evangelize it. I would reserve it to practice singing dodecaphonic music such as the Vienna School.

Is my ability to solfege limited by how fast I can think the syllables in my head?

Depends on what you put behind this verb, but I would say no. The basic use of solfège syllables is to help memorization and vocalization of scales, to be a support for musical notation training -- it is also convenient when discussing a musical point with another musician. When you practice an instrument you end up after some time to directly pass from the sheet-music notation to the playing action, without passing systematically through a note name intermediary. When you sing, you use the syllables of the song.
There is some notable difference in the way solfège notes are used between, say France, Italy, and anglo-saxon countries. Classical italo-french note names are taught as fixed not moving as early as their first music course. They don't do "moving-do", they don't have to transition to "fixed" letters. Most students encounter letter names much later as a weird and foreign alternative based on la instead of do and have to adjust if they want to read chords on song music written with this notation. Most musical reading material they will have will not require any knowledge of letter names. It is just fun to check the translation of piece tonalities on international sheetmusic or on CD covers. They miss the fun to play between notes and their letters, but they learn about it later if they read about Bach's fugue for instance.

Would there be any benefits to a style of chromatic solfege that doesn't suggest any diatonic aspects? Would I be better off if anything just using 7 consonants and distinguishing accidentals by vowel?

I do not think that suppressing the diatonic aspect would be a good thing. So much music is written as a mono or poly-diatonic frame of mind. It would just reverse complexity between the two aspects. This is not only about diatonic: this is also about the position of the note on the stave.  If you want to modify the emphasis on diatonic, it would be logical to change the staff system as well. It has been made several times, and many softwares can display music this way.
But I agree that it is a little strange that more people have not been using a full chromatic system derived from solfege. The best version would certainly depend on the phonetic background. Some US Teachers using the movable do system extend it with vowels modifications, using "e" mainly for flat and "i" for sharp:
do di=ra re ri=me mi fa fi=se so si=le la li=te ti do
you can see that there is no place for mi sharp, that the original si has been renamed ti to leave place for sol sharp = si, etc.
Using the consonant/vowel opposition is not the only way. I can show you a full accidentalized version which is at least 30 years old. This is a system we played with when I was a teenager but was frowned upon by our teachers despite its qualities.
The principle is to use the (soft) z sound for sharp (= dieze in french) and the m sound for flat (=bémol in french). It's convenient because they can be prolonged as long as needed when singing.
do ré mi fa so la si do  (suppressing the sol's l for homogeneity, ré and la are quite contrasted in french prononciation)
gives
do doz=rem re rez=mim mi=fam miz=fa faz=som so soz=lam la laz=sim si=dom siz=do
C C#=Db D D#=Eb E=Fb E#=F F#=Gb G G#=Ab A A#=Bb B=Cb B#=C
It allows you to pronounce and sing uniformly any classical partition (without double accidentals) and is cumulative compared to the original note names.

Am I right in assuming that some syllables are inherently faster to pronounce than others?

Yes, but it is not strictly music, it is linguistics and phonetics. What we imperfectly write down as "sol" is not the same across languages. The troubles you have with the 'r/l' distinction is typical. Inside countries, accents can change considerably the way some sounds are pronounced.
I expect that if you practiced a system where the syllables of the solfege were strictly equal to some of the unit syllables of the hiraganas with the native accent, it would help you going faster.

Is my ability to think syllables in my head limited by how fast I can say them out loud?

Interesting and subtle question but not so easy to answer, and off-topic here I am afraid. Some people cannot read without mentally vocalizing. Some people can't even read without mumbling. So the latter would certainly be hard pressed to think those syllables faster than they can say them out loud. But the main purpose of reading music that fast is to either play it or sing it. You can easily play with your fingers faster than you can speak. And you are not singing faster than yourself, do you? Also if you practice humming when looking at a score, you can sing in your head without that phonetic limitation. I am not full capable of it but I have musician friends that are able to 'sing' chords in their head when they look at a score. And with the way they can spot a typo in orchestral sheetmusic, I believe them.
To me the overall conclusion is that you should not obsess on only one aspect of musicianship.
Comment: A bit off topic, but perhaps interestingly, the German B and H keys have the same root history as the flat and natural signs: the use of "round B" and "square B" in different scales to avoid the *diabolus in musica* interval of the tritone in sacred music.
Comment: This is great! But how do you distinguish "re mi mi" from "rem mim mi"? German languages do actually call their notes differently depending on the accidental. Ab is Ass (sorry, it is!).
C Ciss=Dess D Diss=Ess E Eiss=F Fiss=Gess G Giss=Ass A Aiss=B H .
Note the last notes, a source of confusion (you never know if B is meant as H or as Bb).
Drawback: Eiss and Aiss are hard to say, and the consonant s is not tonal (still you could stay on e or i). On the other hand, several languages lack the french z...
Comment: And to add to the enharmony discussion, how'd you say F##?
Comment: @Gauthier: In addition to implicit glottal stops, "ré mi mi" would be pronounced differently from "rem mim mi" because the "e" of "rem" would be an open "e" in french (think "rèm" like in "monotrème" or "raie" instead of "rez-de-jardin"). At high speed, solfege is more for you than for listeners, anyway.
Comment: @Gauthier: good question that never really came up at the time. I presume that if you plan to read aloud the whole Well-Tempered Keyboard (especially P&F 13 or 18), we could decide to use 2 new consonants for the bb and ## cases.
Comment: @Gauthier: German Ab is not very polite in english and german Fb=Fess is funny in french, that may be one of the reason for its lack of adoption outside germany :-).
Comment: @Gauthier: I agree that the H convention can be a source of confusion and misinterpretation.  There have been further systems to map the whole alphabet for sentence or music encoding purposes.
Comment: @ogerard: actually, I never say B anymore. Only H or Bb, so there is no possible confusion. Note also that the H convention is used in other countries with germanic languages. I'm thinking about Sweden, for example.
Comment: @ogerard: I never realised that the german Fb in french and Ab in english had very related meanings... what are the odds??
Comment: @Gauthier : depends on the set of choices you choose to draw from. A side remark is that Fb - Ab is major so that's ok, but where is the third?
Comment: @ogerard: I'm not with you, care to rephrase?
Comment: @Gauthier: the odds depend on the base set of possibilities (how many possible vowels to use in the system, how many words in the vocabulary of each language, etc.) This is not easy to make precise.
Comment: @Gauthier: I would prefer explaining my side remark in chat.
Comment: Yeah, I definitely wouldn't bother trying to popularize something like this. The main reason I'm really interested in this myself is because I've developed a compulsive habit of solfeging everything I hear, so it's frustrating when I can't keep up. I do admire your friends who can "sing chords in their head" -- that would be a really practical skill to have. Thanks for the great discussion yet again!
Answer: I have constructed my own solfege system at toneme.org which simultaneously handles relative and absolute solfege, including accidentals.
Each utterance consists of a consonant phoneme followed by a vowel phoneme.
The consonant represents the pitch class, and the vowel represents the function.
Please note that the website is several years out of date; it is threadbare, reflecting the fact that I have had to develop it in my free time.  However, it does communicate the basic idea.
I have been training my brain to comprehend this system on and off for three years and more.
I believe it will be of value to my musical development, and help me reach places that I would not be able to by using conventional systems.
Comment: Wow.  Someone marks this guy down, when he has a whole website devoted to an alternative solfege that he's invented.  Seems like what the OP was asking for.  I don't know whether I like the invention or not, but I'm marking this back up to zero.  Not liking it doesn't mean that it doesn't answer the question.  It does, and in a pretty original way.
Comment: Agreed. Another +1 from me. An invention could be interesting or odd, but if it is useful, it is worth an appreciation.
Answer: I think the key to this question is: what is your reason for using solfege?
The commonest use for solfege, is as an aid to learning vocal parts - especially harmony parts where the main melody can distract you from the harmonising pitches you're supposed to be singing.
Having trained yourself to associate the words do,re,mi... with intervals, the words help you get the pitches right, and tell you when you get the pitches wrong.
However, once you've learned the melody of the part, you stop using solfege, and start using the real words of the song.
So the question becomes -- how often do you need to perform allegro 16ths, while learning the melody of a part? If the answer is "often", then yes, you have a reason to need a solfege-equivalent that is suited to singing at that speed.
But my guess is that for most people, the answer is "not often". By the time they need to sing the melody at full speed, they have moved on from the learning-the-tune-supported-by-solfege stage.
Comment: And when those 16th notes are combined with a legato, in the end you would find that singing the actual lyrics is easier than the solfege. (Though i am not sure if allegro songs really have (m)any legatos. )
Answer: Friedmann's Ear Training for Twentieth-Century Music outlines a super simple to learn chromatic, single-syllable solfege based on the numbered pitch classes.
oh one two three four five six sev eight nine ten lev (though personally I like el instead of lev)
An advantage to tying it to numbers is that modular arithmetic can be leveraged for reasoning about and constructing scales and chords and intervals especially if you're math-oriented.


C# Entity Framework Multiple Results

Question: I'm trying to consume multiple results from a stored procedure, which returns 3 result sets.

The first result is a single column called 'Message'.
The second result set is a table
The third result set is another table

My main problem is that every other example on how to approach multiple result sets from a stored procedure implies that every result set can be mapped to an entity, which is not the case with my first result.
First result:
Select @Message

Second result set returned from this query:
Select SecondId, SecondName
From T_Table1

Third result set returned from this query:
Select ThirdId, ThirdName
From T_Table2

Update
I'm updating my EDMX directly, I'll include an Example from Microsoft and explain my problem.
<FunctionImport Name="s_GetAllData">
      <ReturnType EntitySet="CustomerNames" Type="Collection(Model.CustomerName)" />
      <ReturnType EntitySet="CustomerOrders" Type="Collection(Model.CustomerOrder)" />
      <ReturnType EntitySet="CustomerShippings" Type="Collection(Model.CustomerShipping)" />
</FunctionImport>

According to this I'd need a Collection Type to reference (meaning I'd need a existing table within the Database that reflects the columns coming in).
You can't add a Table to EF Model unless it has a Key field, which I don't have in my Results.
Comment: Your first result is still an Entity, with one property defined as something like `public string Message { get; set; }`
Comment: @entropic Check out my update to the Post.
Answer: I don't use EDMX files and have not touched them in years, but here is an example of how you can consume multiple resultsets from a stored procedure call using POCO.  In the example below the stored procedure dbo.MyProc has two select statements, assume the first select simply selects a string as in your example.
using (DbContexts.MyDbContext db = new DbContexts.MyDbContext())
        {
                using (var cmd = db.Database.Connection.CreateCommand())
                {
                    db.Database.Connection.Open();
                    cmd.CommandText = "EXEC dbo.MyProc @param1=@param1";
                    cmd.Parameters.Add(new SqlParameter("@param1", SqlDbType.Int) { Value = 1 });

                    using (var rdr = cmd.ExecuteReader())
                    {
                        using (var objectContext = ((IObjectContextAdapter)db).ObjectContext)
                        {
                            List<string> listSTring = objectContext.Translate<string>(rdr).ToList();
                            rdr.NextResult();
                            List<MyClass> listMyClass = objectContext.Translate<MyClass>(rdr).ToList();
                        }
                    }
                }
        }

Comment: Couple of Minutes ago I was able to get something working using this method, still thank you, I can mark this as the answer since I don't think most people prefer editing the EDMX directly.

C# Entity Framework Multiple Results


Need some hint on design a dynamic chart showing wavelength(s) for element

Question: I have a very interesting challenge here for my Flex project development.
I now have an experiment wizard which consists of three states.In the first state,the user selects the elements they are interested from the periodic table.
When it comes to the second stage,now we have the elements that the user selected in the first state,and these elements are shown on a list A',the user can select an element from there. Also,I have an xml file that has the intensities and wavelengths for all the elements in the periodic table
Now my challenge is ,I need to build a dynamic image there to show the intensity of the element that the user selects from list A',and also show the neighboring(in terms of the wavelength) 10 elements' intensities on this image too.The X-axis is the wavelength while the Y-axis is the intensity.So the neighboring here basically means the elements has neighboring wavelength values.
All the intensity and wavelength data can be read from the xml file,but I am really stuck here from what kind of component to use and how to make it shown dynamically,so please offer some suggestions please,thanks in advance.
Answer: Clarify your flex version 3 or 4.
Read this to use core components
http://livedocs.adobe.com/flex/3/html/help.html?content=Part7_DataVis_1.html
In other cases you could try to use raw Graphics component to draw lines/points/plots etc. as you want or look for some 3rd party components like http://flare.prefuse.org/
Comment: do you have some images if interface you want?
Comment: yes,I do.But I don't know how to post it on here,could you help me,thx.
Comment: @Robert, of course: for images - http://imageshack.us, for source code - http://pastebin.com, for video - http://vimeo.com


Grade 1 Music Theory

Question: I have 2 questions:

I have learned some topics of grade 1 but I think there are a lot of topics in it. Can anyone provide a list of topic which come under the Grade 1 syllabus and which I must go through prior jumping to Grade 2?
Is there website or any channel on Youtube which covers all the topics?
Comment: Go straight to the examination board websites. In UK it's Trinity, LCM or ABRSM. The syllabi are all set out, including sample questions.
Comment: Which methods??
Comment: @NeilMeyer - no one mentioned any methods.
Comment: When i say method i mean ABRSM, trinity, UNISA. All will have different answers.
Answer: Note values of semibreve, minim, crotchet, quaver and semiquaver, and their equivalent rests
(candidates may use the terms 'whole note', 'half note', etc.). Tied notes. Single-dotted notes and rests.
2 Simple time signatures of 24 34 4 , bar-lines and the grouping of the notes listed above within these times.
3 The stave. Treble (G) and bass (F) clefs. Names of notes on the stave, including middle C in both clefs.
Sharp, flat and natural signs, and their cancellation.
4 Construction of the major scale, including the position of the tones and semitones. Scales and key signatures of the major keys of C, G, D and F in both clefs, with their tonic triads (root position), degrees (number only), and intervals above the tonic (by number only).
5 Some frequently used terms and signs concerning tempo, dynamics, performance directions and articulation marks. Simple questions will be asked about a melody written in either treble or bass clef.
Answer: I took Theory I and II at a university and basically the material below i what they gave me (minus the Youtube channel)
Beginner Level Theory:
Read Tonal Harmony by Kostka (Buy the workbook as well - this is a separate book) (Buy an older version - way cheaper and same info)
The main things to focus on is circle of fifths, voice leading (avoiding parallel octaves and fifths), counterpoint, analysis, figured bass, roman numerals,intervals, interval inversion and chord construction... really, you can skip chapters later on in the book, but don't do it early in the book. Theory is much like math where as you miss one part and you're doomed to understanding anything past that.
Work on aural skills out of a new approach to sight singing by Berkowitz
musictheory.net for aural skill mastery (kind of like flash cards except online)
Youtube Channel: Rick Beato - this channel may be a little advanced, but he does have simple stuff too.
That will carry you to Theory III if not further.
It's been years and I'm still working on this same material; music theory is a huge topic - you can get your PHD in music theory alone.
Also notice that this focuses on classical theory. If you are a jazzer, you still want to learn the classical theory first, then move to jazz theory.
Comment: Syllabi vary in different parts of the world - where are you ( and where's the OP?)

Grade 1 Music Theory


How could I stop the script without having to wait for the time set for interval to pass?

Question: In this script I was looking to launch a given program and monitor it as long as the program exists. Thus, I reached the point where I got to use the threading's module Timer method for controlling a loop that writes to a file and prints out to the console a specific stat of the launched process (for this case, mspaint).
The problem arises when I'm hitting CTRL + C in the console or when I close mspaint, with the script capturing any of the 2 events only after the time defined for the interval has completely ran out. These events make the script stop.
For example, if a 20 seconds time is set for the interval, once the script has started, if at second 5 I either hit CTRL + C or close mspaint, the script will stop only after the remaining 15 seconds will have passed.
I would like for the script to stop right away when I either hit CTRL + C or close mspaint (or any other process launched through this script).
The script can be used with the following command, according to the example:
python.exe mon_tool.py -p "C:\Windows\System32\mspaint.exe" -i 20
I'd really appreciate if you could come up with a working example.
I had used python 3.10.4 and psutil 5.9.0 .
This is the code:
# mon_tool.py

import psutil, sys, os, argparse
from subprocess import Popen
from threading import Timer
debug = False

def parse_args(args):
    parser = argparse.ArgumentParser()
    parser.add_argument("-p", "--path", type=str, required=True)
    parser.add_argument("-i", "--interval", type=float, required=True)
    return parser.parse_args(args)

def exceptionHandler(exception_type, exception, traceback, debug_hook=sys.excepthook):
    '''Print user friendly error messages normally, full traceback if DEBUG on.
       Adapted from http://stackoverflow.com/questions/27674602/hide-traceback-unless-a-debug-flag-is-set
    '''
    if debug:
        print('\n*** Error:')
        debug_hook(exception_type, exception, traceback)
    else:
        print("%s: %s" % (exception_type.__name__, exception))
sys.excepthook = exceptionHandler

def validate(data):
    try:
        if data.interval < 0:
            raise ValueError
    except ValueError:
        raise ValueError(f"Time has a negative value: {data.interval}. Please use a positive value")
def main():
    args = parse_args(sys.argv[1:])
    validate(args)

    # creates the "Process monitor data" folder in the "Documents" folder
    # of the current Windows profile
    default_path: str = f"{os.path.expanduser('~')}\\Documents\Process monitor data"
    if not os.path.exists(default_path):
        os.makedirs(default_path)

    abs_path: str = f'{default_path}\data_test.txt'

    print("data_test.txt can be found in: " + default_path)

    # launches the provided process for the path argument, and
    # it checks if the process was indeed launched
    p: Popen[bytes] = Popen(args.path)
    PID = p.pid
    isProcess: bool = True
    while isProcess:
        for proc in psutil.process_iter():
            if(proc.pid == PID):
                isProcess = False

    process_stats = psutil.Process(PID)

    # creates the data_test.txt and it erases its content
    with open(abs_path, 'w', newline='', encoding='utf-8') as testfile:
            testfile.write("")

    # loop for writing the handles count to data_test.txt, and
    # for printing out the handles count to the console
    def process_monitor_loop():
        with open(abs_path, 'a', newline='', encoding='utf-8') as testfile:
            testfile.write(f"{process_stats.num_handles()}\n")
            print(process_stats.num_handles())
        Timer(args.interval, process_monitor_loop).start()
    process_monitor_loop()

if __name__ == '__main__':
    main()

Thank you!
Answer: You can try registering a signal handler for SIGINT, that way whenever the user presses Ctrl+C you can have a custom handler to clean all of your dependencies, like the interval, and exit gracefully.
See this for a simple implementation.
Comment: Maybe add a second handler for CTRL_C_EVENT? [Check this link](https://docs.python.org/3/library/signal.html#signal.CTRL_C_EVENT). I think it wouldn't be necessary to check the OS, the uncompatible signal would just not trigger.
Comment: Thank you! I got so far this working example: https://pastebin.com/6dQwThLZ , where the loop runs every 10 seconds. But, unfortunately, this solution only works for Unix systems. I'll keep looking for a windows solution.
Answer: I think you could use python-worker (link) for the alternatives
import time
from datetime import datetime
from worker import worker, enableKeyboardInterrupt

# make sure to execute this before running the worker to enable keyboard interrupt
enableKeyboardInterrupt()

# your codes
...

# block lines with periodic check
def block_next_lines(duration):
    t0 = time.time()
    while time.time() - t0 <= duration:
        time.sleep(0.05) # to reduce resource consumption

def main():
    # your codes
    ...

    @worker(keyboard_interrupt=True)
    def process_monitor_loop():
        while True:
            print("hii", datetime.now().isoformat())
            block_next_lines(3)

    return process_monitor_loop()

if __name__ == '__main__':
    main_worker = main()
    main_worker.wait()

here your process_monitor_loop will be able to stop even if it's not exactly 20 sec of interval
Comment: ahh thanks for the feedback, I've updated my code above right away. Can you please check it?
Comment: btw I just achieved the goal that you want by declaring the `@worker` and `process_main_loop()` outside the `main()`. I just updated my code. Please check it, I hope you got the result that I got.

For the `from worker import worker` resolve issue, are you already installed on your interpreter Sir? Its fine in my side.
Comment: I'm using windows 10. `python-worker==1.10.1` , `python 3.8.10`. What python version do you have?
Comment: Hey I've just added `main_worker.wait()` at the code above and you can see it in here https://imgur.com/a/B5uGT9p, it works perfectly fine. Btw thanks for giving me some insight from this thread. The previous code didn't work because we are running out of lines after `main()` and the interpreter is already exiting the main thread, thats why we can't stop it.
Comment: and you have to change it into `while loop` because we if you want to do recursive scenario (like before), you have to add a state checker to block the interpreter from exiting.
Comment: why you have to repost  it ?
Comment: ohh okay no probs
Comment: I think it's already the fastest execution for checking `is_alive`
Comment: you can do `if not all([w.is_alive for w in ThreadWorkerManager.allWorkers.values()])` but this code will check the whole registered worker
Comment: Thanks! Can you please check out this code that I had modified based on your suggestion: https://pastebin.com/ThWtgx4H , maybe it's something that I did wrong? I got to run the code, but I still can't stop the script right away.
Comment: Thanks for the code! Unfortunately it still doesn't work :( I recorded a video to see how it works for me, using a simplified version: https://imgur.com/a/YwMPuJu . I hit CTRL + C in the console at 07:49:43 and the script only stopped at 07:50:12, taking it 29 seconds to stop. The time I used for the interval was 20 seconds.

Also, "worker" shows with a squiggly line where it says "from worker", saying "Import "worker" could not be resolved", could that be the problem?
I get the same kind of error with the "psutil" module, but it still works.
Comment: Thank you! It still doesn't work :( Yes, I have installed the worker module using "pip install python-worker" and its version is 1.10.1 . I had also tested the code on a different computer, but it still doesn't work. I'm using Windows 10, more specifically: 101.0.1210.53 (Official build) (64-bit) . The other computer is on the latest updates of Windows, unlike mine. What operating system are you using?
Comment: I'm using python 3.10.4 . But now I have tried the script with 3.8.10, as well, but it still doesn't work. I really have no idea what I could do next. I ran the script within Visual Studio Code terminal and then I ran it in cmd as Administrator, but still nothing.
Comment: Would you mind recording a very short video to see how it works for you, using this script: https://pastebin.com/LBygz7LP?
Comment: Thank you very much! This time it has worked for me! You are awesome! :D
Today I was really busy and I didn't have time to test this with my real code. In case any problems come up during testing, can I let you know?
Comment: Also, can I use your code below to post it as an answer in case I figure out how to implement immediate script stopping once the launched program stopped, I already go an idea? Don't worry, your solution will still have the mark :P
Comment: I wanted to add my solution on top of your solution as a separate answer to this thread so that if a beginner programmer like me come across this thread it will be much easier for them to understand what we have done. They will see your solution as dealing with part 1 of the problem, where you have solved the CTRL + C problem. While my solution, part 2, deals with checking every second if the launched program exists.
Comment: I implemented my solution over here: https://pastebin.com/U6tBgEDd , where, as I described above, the sub_process function checks every second if mspaint exists, if it doesn't exist, it will stop the script. Now, a new problem came up, if I hit CTRL + C, only the thread created by you will stop, while mine keeps running. Can you please see how I can stop both threads when hitting CTRL + C?
Comment: I solved the problem I had above. One last thing, could you tell me please if there's a better way of getting the value of "w.is_alive" than what I used on lines 61 and 62 for this code: https://pastebin.com/aeedxgX7?
Comment: Thank you! Then I'll stick to ```is_alive```.
Answer: This is the solution for the second part of the problem, which checks if the launched process exists. If it doesn't exist, it stops the script.
This solution comes on top of the solution, for the first part of the problem, provided above by @danangjoyoo, which deals with stopping the script when CTRL + C is used.
Thank you very much once again, @danangjoyoo! :)
This is the code for the second part of the problem:
import time, psutil, sys, os
from datetime import datetime
from worker import worker, enableKeyboardInterrupt, abort_all_thread, ThreadWorkerManager
from threading import Timer

# make sure to execute this before running the worker to enable keyboard interrupt
enableKeyboardInterrupt()

# block lines with periodic check
def block_next_lines(duration):
    t0 = time.time()
    while time.time() - t0 <= duration:
        time.sleep(0.05) # to reduce resource consumption

def main():

    # launches mspaint, gets its PID and checks if it was indeed launched
    path = f"C:\Windows\System32\mspaint.exe"
    p = psutil.Popen(path)
    PID = p.pid
    isProcess: bool = True
    while isProcess:
        for proc in psutil.process_iter():
            if(proc.pid == PID):
                isProcess = False

    interval = 5
    global counter
    counter = 0

    #allows for sub_process to run only once
    global run_sub_process_once
    run_sub_process_once = 1

    @worker(keyboard_interrupt=True)
    def process_monitor_loop():
        while True:
            print("hii", datetime.now().isoformat())

            def sub_proccess():
                '''
                Checks every second if the launched process still exists.
                If the process doesn't exist anymore, the script will be stopped.
                '''

                print("Process online:", psutil.pid_exists(PID))
                t = Timer(1, sub_proccess)
                t.start()
                global counter
                counter += 1
                print(counter)

                # Checks if the worker thread is alive.
                # If it is not alive, it will kill the thread spawned by sub_process
                # hence, stopping the script.
                for _, key in enumerate(ThreadWorkerManager.allWorkers):
                    w = ThreadWorkerManager.allWorkers[key]
                    if not w.is_alive:
                        t.cancel()

                if not psutil.pid_exists(PID):
                    abort_all_thread()
                    t.cancel()

            global run_sub_process_once
            if run_sub_process_once:
                run_sub_process_once = 0
                sub_proccess()

            block_next_lines(interval)

    return process_monitor_loop()

if __name__ == '__main__':
    main_worker = main()
    main_worker.wait()

Also, I have to note that @danangjoyoo's solution comes as an alternative to signal.pause() for Windows. This only deals with CTRL + C problem part. signal.pause() works only for Unix systems. This is how it was supposed for its usage, for my case, in case it were a Unix system:
import signal, sys
from threading import Timer

def main():
    def signal_handler(sig, frame):
        print('\nYou pressed Ctrl+C!')
        sys.exit(0)

    signal.signal(signal.SIGINT, signal_handler)
    print('Press Ctrl+C')
    def process_monitor_loop():
        try:
            print("hi")
        except KeyboardInterrupt:
            signal.pause()
        Timer(10, process_monitor_loop).start()
    process_monitor_loop()

if __name__ == '__main__':
    main()

The code above is based on this.


Why is there a small gap between an absolutely positioned child and parent with borders? All margins, padding, and positions are set to 0

Question: I recreated a stripped down version of the phenomenon I'm facing. So the parent has position: relative and the ::after child has position: absolute with all directional values set to zero. This would normally stretch out the child directly over the parent perfectly in my experience. Also * { margin: 0; padding: 0 } is set to be safe and make sure this isn't a margin or padding issue.
I know I can just set the child element -1px left and up... but I'm kind of curious to why this is happening if the child is stretched out directly over it's parent? Is there a way to make it work with top, right, bottom, and left set to 0?

* {
  margin: 0;
  padding: 0;
}
body {
  padding: 5rem; /* to push div closer to center of snippet window */
}
div {
  transform: scale( 5 ); /* added so displacement can be easily seen */
}
div, div::after {
  display: block;
  position: relative;
  width: 1rem;
  height: 1rem;
  background-color: #eee;
  border-style: solid;
  border-width: 0.1rem;
  border-color: #666;
  border-radius: 0.25rem;
  content: '';
}
div::after {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
}

Notice from the snippet ( In Chrome atleast ) The div's are offset from each other by about 1 px.
Picture of what I'm seeing in Chrome on desktop:

Why aren't these div's placed directly on top of each other?
Answer: The reference is the padding-box and not the border-box

If the element has 'position: absolute', the containing block is established by the nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed', in the following way:
... the containing block is formed by the padding edge of the ancestor. ref

Then the values are over-constrained since you are defining left/right/width and the width is equal to the parent (containing block) width so the right will be ignored (use any value for it and nothing will happen)

If the values are over-constrained, ignore the value for 'left' (in case the 'direction' property of the containing block is 'rtl') or 'right' (in case 'direction' is 'ltr') and solve for that value. ref

Same logic with top/bottom/height where the bottom is ignored.
If you replace your border with a box-shadow for example you will have a perfect overlap:

* {
  margin: 0;
  padding: 0;
}
body {
  padding: 5rem; /* to push div closer to center of snippet window */
}
div {
  transform: scale( 5 ); /* added so displacement can be easily seen */
}
div, div::after {
  display: block;
  position: relative;
  width: 1rem;
  height: 1rem;
  background-color: #eee;
  box-shadow:0 0 0 0.1rem #666;
  border-radius: 0.25rem;
  content: '';
}
div::after {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
}

Comment: @devsandbox probably you never had borders. This behavior is quite old and clearly defined in the Spec
Comment: So this behavior is normal? I don't think I've had this issue in the past when stretching absolutely positioned children over their parents
Comment: @devsandbox added more details
Comment: You're right. Usually I don't have borders on my elements like this. And removing them seems to fix the problem... smh...weird at the least


How to fetch value based on div class and ID combination

Question: I have a div as shown below

Additions:  0

Additions:  0

I am trying to fetch the Text of additions , i was trying this way
var vendoritemsid = '707' ;

var addtions=$(".lastItm_Wrap #lastwrap"+vendoritemsid).find('.Itm_right_aside . additions').text();

alert(addtions);

but i am getting blank alert
THis is my fiddle
https://jsfiddle.net/fpzeLa0a/8/
could you please let me know how to fetch te additions class text ??
Answer: It's because the selector $(".lastItm_Wrap #lastwrap707") wasn't selecting any elements.
The element .lastItm_Wrap actually has an id, therefore you would remove the space between the selectors (because you aren't trying to select a descendant).
Updated Example
var vendoritemsid = '707';
var addtions= $(".lastItm_Wrap#lastwrap" + vendoritemsid).find('.Itm_right_aside .additions').text();

alert(addtions);

Since you're selecting an element with a unique id, you could just omit the class selector.
Example Here
$("#lastwrap" + vendoritemsid + ' .Itm_right_aside .additions').text();

The .find() method can also be removed.


How to generate an ear file from a maven-archetype-webapp artifact?

Question: I currently have a project built with maven-archetype-webapp artifact. The default packaging for this project is war.
Is it possible for me to insert the maven-ear-plugin in this webapp pom.xml generate an ear file that contains this project war? I tried that, but the war file doesn't get embedded in the generated ear file. It has everything except the war file.
I read many Maven related articles, and perhaps I could use maven-archetype-j2ee-simple artifact. However, I'm reluctant to this use for 2 reasons:-

This artifact handles ejbs and all the extra features that I don't use. It makes my project looks bloated.
Second, it seems like it requires me to install the web module into the repository first before I can create the ear file. Is this the preferred way to create an ear file?

How do I create an ear file that contains the war file using maven-ear-plugin from my webapp's pom.xml? If this way is not possible, what's the preferred way?
I'm sorry if my questions sound a little novice, I realized I have whole lot more to learn about Maven.
Thanks much.
Answer: The maven ear plugin assumes that any WAR is a separate project, so you need two projects, plus a parent project. It's a function of how maven does packaging. Each project produces one artifact. There are ways around this, but Maven will punish you for your sins. This question goes into some detail on this topic.


Override a specified method of an object with new functionality while still maintaining all of the old behaviour

Question: Is it possible to create a Spy function that returns an object that keeps track of how many times a function is called like shown below?
var spy = Spy(console, 'error')

console.error('calling console.error')
console.error('calling console.error')
console.error('calling console.error')

console.log(spy.count) // 3

Comment: maybe you need to implement a `console.log` by yourself
Answer: You could wrap the call to the method

class Spy {
  constructor(obj, method) {
    this.count = 0

    const _method = obj[method] // save the ref to the original method
    const self = this

    // wrap the method call with additional logic
    obj[method] = function () {
      self.count++
      _method.call(this, ...arguments)
    }
  }
}

var spy = new Spy(console, "error")

console.error("calling console.error")
console.error("calling console.error")
console.error("calling console.error")

console.log(spy.count) // 3
Comment: beautiful , thanks
Answer: you can use javascript Clouser property.
// Initiate counter
let counter = 0;

// Function to increment counter
function add() {
  counter += 1;
}

// Call add() 3 times
add();
add();
add();

// The counter should now be 3

reference https://www.w3schools.com/js/js_function_closures.asp


Use regular expressions to edit an entire column with MySQL

Question: I have a table like so:
| link  |
----------
http://dev.example.com/images/image1.png
http://dev.example.com/images/image2.png
http://dev.example.com/images/image3.png
http://dev.example.com/images/image4.png

But, I need to change it so it contains:
| link  |
----------
http://example.com/resources/images/image1.png
http://example.com/resources/images/image2.png
http://example.com/resources/images/image3.png
http://example.com/resources/images/image4.png

There are lots of entries that need changing. What would be a good way to go about updating all of the entries?
Right now I'm using this to find the entries:
SELECT column
FROM table
WHERE link REGEXP '^dev'

I was updating the links by hand first, but there are so many left. I can't update them all myself.
Any ideas?
Answer: You call it "Field" in the table listing but then "link" in the SQL so i'm going to use "Field"
update table
set
    Field = replace(Field, 'dev.site.com', 'site.com/resources')
where
    Field rlike '^dev.site.com'


Hide Certain Values from an Apache Solr Facet

Question: I have some Apache Solr facet blocks i'd like to filter certain values from. For example if a facet contains the values:
Filter by letter:
 - a (6)
 - b (4)
 - c (2)
I want to hide the facet "c" so visitors won't be able to use it as a filter. Is it possible? i'm ok with editing/creating modules and also it would be okay a solution that uses CSS to hide the row from view (which was my first thought but then i realized there isn't a unique identifier for facet values)
Answer: I kept looking for a solution on the internet after posting this question and i have discovered this module:  https://www.drupal.org/project/facetapi_bonus which does exactly what i need
Comment: You should select this as the correct answer once the system allows you to do so.
Comment: Yes i was planning to, it'll require 2 days, thanks for the reminder


Ranking Interval Complexity

Question: I would like to know the order of complexity of musical intervals (in a melody). I learned about this from an exposure to the Kodaly method, although I have not learned from nor taught using the Kodaly method. But in my reading it introduced the concept of how the intervals are naturally learned. Seems like there should be an ordering. I can see that the first two are a minor third down and a perfect fourth up, but I don't know where to find a list of the intervals from easiest to hardest, treating the up direction and the down direction as separate things.
I realize that this ordering of interval difficultly or learning order may be dependent on the culture of the student. It would be extra cool to understand how this ordering of interval difficulty related to the harmonic series, since the first interval there is a perfect fifth up, which does not match my understanding that a minor third down is the easiest interval.
Comment: I have a feeling this may be quite subjective, as I may find some intervals easier or harder than you based on my experiences to date. What is the underlying issue you are trying to solve?
Comment: @Dr Mayhem - I am interested in writing a computer program that would take a set of music and order it according to the rules set. So sure, part of this is likely to be cultural or perhaps tied to individuals experiences, but I think there must be established orders of difficulty. Otherwise Kodaly could not have done what he did.
Comment: I wouldn't take it for totally impossible that someone could have done some cognitive study on this.
Answer: Interval complexity is a direct function of the distance between the lowest note of the interval as compared to the highest note of the interval with the closest note in the harmonic series of the lowest note of the interval. (Phew!)
Let me explain:
Poor Man's Harmonic Series: For the sake of this explanation, let's pretend the harmonic series represents these relationships. It does not, but this linear representation is roughly based on the harmonic series:
Octave, fifth, major third, minor 7th, major second, flat fifth, minor sixth, major seventh, minor 9th, minor third.
So if the root note is C, its sympathetic vibrations along the harmonic series are as written above. (Our musical system is not perfectly mapped to the harmonic series, so this is just an approximation.)
As you can see, the harmonic series firmly supports the simplest intervals (octave, fifth and major third), as well as those a whole step up or down from the fundamental (minor 7th, major second). As we climb further into the stratosphere we encounter dissonances not well supported within the fundamental, which would represent the more complex intervals of the flat fifth, minor sixth, major seventh and minor ninth.
Harmonics also imply their own inversions: Note that each interval also implies its own inversion, though, meaning that the perfect fourth, although not even present in this particular example, is well enough supported by the perfect fifth, the two intervals being so closely related.
Higher harmonics are also weaker: Also note that the higher the harmonic is, the more weak it is. So the fundamental is alot less related to the minor 9th than it is to the perfect fifth.
The 12-tone musical system does not relate perfectly to the harmonic series: many notes in the harmonic series are not too closely related to their closest relative in the actual musical scale. For instance, the flat 5th in the harmonic series is 49 cents more flat than the one in the 12-tone scale, a significant difference. This difference has meant that many harmonies which would be perfectly valid in a microtonal system are completely unusable in the 12-tone system we most commonly use.
Answer: In my experience as a musician and using Kodaly, it's less about the intervals themselves as it is about the notes of the scales used. For instance, a minor third down is represented as "sol mi" and a perfect fourth up is "sol do". You can see how these are the notes of the tonic triad.
Therefore, in ranking the complexity of an interval, I believe it would be more useful to consider which step of the scale it is. So, if the note lies outside of the key, yet is a perfect fifth up, it would be listed as more complex.
Comment: Most of my experience is modal. Certainly the meaning of an interval will depend on the context, including where the interval occurs in the scale being used and the current tonal center. Maybe my question is outside of Kodaly, strictly speaking, since I am more concerned with comparisons of bodies of music than the education of the musician, but I assumed that this must have been thought through.
Comment: wouldn't that be the sub-dominant triad?
Comment: @Ygg "do mi sol" are the notes of the tonic triad when using "movable do."
Answer: No experience with the method you mentioned, but I learned from the center out.
P5, P4, M3, m3, M6, m6, M2, m2, M7, m7, TT (tritone).
Ascending and descending in all cases.
In this sense, you are starting with more consonant, "stable" intervals, then working your way into the more dissonant, "complex" intervals, if you will.
Comment: Are minor 2nds considered more consonant than a major 7th?
Comment: I would argue that a major 2nd and a minor 7th are roughly "equivalent" in dissonance, same with a minor 2nd and major 7th. M2/m7 less dissonant than m2/M7.  My point in laying them out that way was to illustrate the concept of interval inversion pairing in terms of consonance/dissonance. (4ths and 5ths, 3rds and 6ths, 2nds and 7ths, etc..)

Ranking Interval Complexity


LINQ: How to filter list based on values from array

Question: I would like to filter array of users, based on that if roles that exists in array are already contained in user roles list.
Here is my code which won't compile:
var roles = role.Split(','); // admin, basic, super-admin

listOfUsers = listOfUsers.Where(user => user.Roles.Contains(roles.Select(x => x)).ToList();

I want to do if admin and basic are contained in roles which is array of string to return only those users which have the same value in Roles array. (Roles is also list of strings) but this reproduces an error:
Comment: Assuming that `user.Roles` is an `IEnumerable`, then `user.Roles.Contains(roles.Select(x => x))` expects a `string` as a parameter.  We know that `roles` is a `string[]` (assuming that `role` is a string containing stuff separated by commas).  Then, `roles.Select(x=>x)` is an `IEnumerable` not the `string` the function expects. This would be much easier if you gave us more code (as suggested by @DavidFox) and if your variable names weren't `role`, `roles` and `Roles`
Comment: can you add more code? what's the type of `listOfUsers`, etc? what type is `user.Roles`?
Comment: Are you looking for users that have ALL roles specified in `roles`, or users that have ANY role in `roles`?
Comment: @DavidFox It states that `Roles` is also a `List`
Comment: @RufusL For users that have any roles specified in roles.. Yes `Roles` is also a `List`
Comment: Please post actual text for error messages. Bitmaps are not searchable, and for some people are also not even readable at all.
Answer: To find a match in one list of strings from another, you can check if Any user roles are contained in the roles list:
listOfUsers = listOfUsers.Where(user => user.Roles.Any(roles.Contains)).ToList();

If you want to do a case-insensitive comparison, you can pass a comparer to the Contains method:
listOfUsers = listOfUsers
    .Where(user =>
        user.Roles.Any(role => roles.Contains(role, StringComparer.OrdinalIgnoreCase)))
    .ToList();


Divided Differences expanded form definition.

Question: From definition of divided differences we have that $$f[x_0,\cdots,x_n]=\sum_{j=0}^n\frac{f(x_j)}{\Pi_{{k\in\{0,\cdots,n\}-\{j\}}}(x_j-x_k)} $$
It makes completely sense to have $k\neq j$ otherwise the denominator becomes zero.
In many articles I've seen the following notation,
let $q(\xi)=(\xi -x_0)\cdots(\xi-x_n)$, then we can re-express the expanded form of the divided difference as
$$f[x_0,\cdots,x_n]=\sum_{j=0}^n\frac{f(x_j)}{q'(x_j)}$$
In this link there's a brief explanation but I don't really get the point.
why is it that $$q'(x_j)=(x_j - x_0)\cdots(x_j -x_{j-1})(x_j-x_{j+1})\cdots(x_j - x_n)$$ ?
Maybe I am messing up things, but if I apply the product rule for derivatives I don't obtain that result.
Thanks in advance
Comment: Note that $q'$ is being computed at point $x_j$... All the other terms that you obtain deriving the product have a factor $(x-x_j)$ that vanishes when $x=x_j$.
Answer: First, look at the following equivalent expression of $q$:
$$q(\xi)=(\xi-x_j)\cdot \prod_{i\neq j}{(\xi - x_i)}$$
Take the derivative of $q(\xi)$ with respect to $\xi$, using the product rule (no need to expand the rightmost expression):
$$q'(\xi)=1\cdot \prod_{i\neq j}{(\xi - x_i)} + (\xi-x_j)\cdot \left( \prod_{i\neq j}{(\xi - x_i)}\right )'$$
Now substitute $\xi=x_j$, to get
$$q'(x_j)=\prod_{i\neq j}{(x_j - x_i)} + (x_j-x_j)\cdot \left( \prod_{i\neq j}{(x_j - x_i)}\right )' = \prod_{i\neq j}{(x_j - x_i)}$$
I don't think you should view it as anything more meaningful than a shorthand for the longer expression on the right hand side.


Send out a unique (unprotected) link per user

Question: I'm the admin of a Drupal site with 200+ users. I sort of rolled into this, so I'm no Drupal expert, although I've managed to understand it quite good now.
This is a site for our sportsclub and we want all existing users to confirm their membership (of the club) for the coming season.
We would prefer to do this a simple as possible and send each user a link which, when clicked on, will confirm their renewal. Ideally, this is a link with some sort of unique hash, so we can make it unprotected. This would avoid having to log in.
We want to do this (keep it as simple as possible) because we've noticed a lot of members just don't do it (go to the site, log in, go to the correct page if not already there, and click the yes link to renew).
Is this possible (with a module perhaps)? Sending out a unique link per user?
Answer: I don't know of a single module to do this.
I would suggest looking at the VBO module (views bulk operations).
You can create an "email" action and customize what gets sent.
From your description I think the biggest barrier you're facing is that users don't want to login,  and you'd like them to just click a link.
To do that you're going to have to roll your own module and likely make use of a menu hook with a callback to a function that processes the link.
Answer: This is how I solved it, although jdu is right, it should be in a custom module. I have made one before, so I will look into that, but for now, here's my solution:
I created a computed field for my users where I calculate a hash based on their username.
I created a new unprotected page, and I will send them the link with their unique hash by mail. As an aside: I'm using Mailchimp which integrates with Drupal so I should be able to take out that hash and use it in the mail.
Once on the page, I execute custom PHP where I check if the necessary query string parameters are present. Then I set the necessary field, confirming their new membership, save the user and notify them on the page.
The mail hasn't been sent out to all our users yet, but I've tried it myself, and it works.
As jdu said, the biggest barrier is the fact that they won't login, because this also means a lot of them will only half-read the mail, won't click on the link, will postpone it,... But it can't be any more simple than this.


Java: Why is it called bitwise exclusive or?

Question: I am trying to understand how bitwise exclusive or works in java. So for example:
false ^ false = false
true ^ true = false
true ^ false = true
false ^ true = true

According to some posts here at SO, the size of boolean in java is jvm dependant. Typically boolean may be represented with 32 bits on the stack, 8 bits in the array.
So, why is it called bitwise if it is more than a byte on the stack?
Comment: You can compare any bits, not only booleans. E.g. int 10 ^ int 5
Comment: It is not called "bitwise exclusive OR". When applied to boolean operands, it is called "boolean logical exclusive OR".
Comment: @user3111525 If your question is answered? Please consider marking one of the answers as accepted.
Answer: According to the Java Language Specification there is no boolean bitwise operator but only boolean logical operators and integer bitwise operators.

15.22.1. Integer Bitwise Operators &, ^, and |
When both operands of an operator &, ^, or | are of a type that is
  convertible (§5.1.8) to a primitive integral type, binary numeric
  promotion is first performed on the operands (§5.6.2).
The type of the bitwise operator expression is the promoted type of
  the operands.
For &, the result value is the bitwise AND of the operand values.
For ^, the result value is the bitwise exclusive OR of the operand
  values.
For |, the result value is the bitwise inclusive OR of the operand
  values.
15.22.2. Boolean Logical Operators &, ^, and |
When both operands of a &, ^, or | operator are of type boolean or
  Boolean, then the type of the bitwise operator expression is boolean.
  In all cases, the operands are subject to unboxing conversion (§5.1.8)
  as necessary.
For &, the result value is true if both operand values are true;
  otherwise, the result is false.
For ^, the result value is true if the operand values are different;
  otherwise, the result is false.
For |, the result value is false if both operand values are false;
  otherwise, the result is true.
Answer: The ^ operator is overloaded. So if you use it on primitive boolean values, it work like logical XOR.
Answer: Bitwise refers to the size of the information they represent. Booleans represent 1 bit of information even though they might need more than 1 bit to be saved in memory.
Answer: It's called "bitwise" because it applies the logical XOR operation to each pair of bits on corresponding positions in the bit representation of the two operands (which can be any number, not only booleans).
Here's a simplified example for 8 bits:
9 (00001001) ^ 14 (00001110) = 7 (00000111).


Different approach to tackle $\int_{0}^{\infty}{2\sqrt{x^2+1}+\gamma(x^2-x)\sqrt{x^2+2}\over \sqrt{(x^2+1)(x^2+2)}}\cdot{\mathrm dx\over (x^2+1)^2}=1$

Question: $$\int_{0}^{\infty}{2\sqrt{x^2+1}+\gamma(x^2-x)\sqrt{x^2+2}\over \sqrt{(x^2+1)(x^2+2)}}\cdot{\mathrm dx\over (x^2+1)^2}=1\tag1$$
Where $\gamma=0.577...$ it is Euler's Constant
$$\int_{0}^{\infty}{2\over \sqrt{x^2+2}}\cdot{\mathrm dx\over (x^2+1)^2}+\gamma\int_{0}^{\infty}{x^2-x\over (x^2+1)^{5/2}}\mathrm dx\tag2$$
$$\gamma\int_{0}^{\infty}{x^2-x\over (x^2+1)^{5/2}}\mathrm dx=\gamma\int_{0}^{\infty}{x^2\over (x^2+1)^{5/2}}\mathrm dx-\gamma\int_{0}^{\infty}{x\over (x^2+1)^{5/2}}\mathrm dx\tag3$$
Enforcing $u=x^2$ then $du=2xdx$
Recalling from the beta function
$$\int_{0}^{\infty}{u^m\over (u+1)^{m+n+2}}du=B(m+1,n+1)\tag4$$
$${\gamma\over 2}\int_{0}^{\infty}{u\over (u+1)^{5/2}}\mathrm du-{\gamma\over 2}\int_{0}^{\infty}{1\over (u+1)^{5/2}}\mathrm du={B(3/2,1)\over2}-{B(1,3/2)\over2}=0\tag5$$
So this part of integration must be 1! It is ready done my @Marco on my previous post.
$$\int_{0}^{\infty}{2\over \sqrt{x^2+2}}\cdot{\mathrm dx\over (x^2+1)^2}\tag6$$
Can anyone prove this integral $(1)$ via another method? Thank you! Sorry for not addressing the question properly.
Comment: What is your question, really? Could you clarify?
Comment: Euler's constant is usually $e$. The constant you have is usually called the Euler-mascheroni constant.
Comment: This looks like very similar to a previous post: http://math.stackexchange.com/questions/2090206/help-to-prove-that-int-0-infty-sqrtx21x2-sqrtx22-over-sqrtx
Comment: @SimpleArt [See this](http://numbers.computation.free.fr/Constants/Gamma/gamma.pdf)
Comment: @Simple Art: I have seen $\gamma$ called "Euler's constant" about ten times more frequently than "the Eueler-Mascheroni constant."  Perhaps I am biased by work in mathematical physics, where $\gamma$ plays a crucial role in the $\overline{MS}$ renormalization prescription. Also, $e$ is rarely referred to as "Euler's constant" since it is so familiar, most literature just uses "$e$".
Comment: The last integral is just $2$ times the integral $I_2$ in Marcos answer to your previous question. Thus, it equals $1$. For the part with $\gamma$, it is zero (just do $x\mapsto 1/x$), independent of the factor $\gamma$.
Comment: Hm, ok.  Thanks, I never knew that.  Guess I'm biased since I use $e$ more, and it's just more natural for me.
Comment: @SimpleArt The subtlety is Euler's number versus Euler's constant.
Answer: In fact, letting $x=\tan t$ and $u=\sin t$, one has
\begin{eqnarray}
&&\int_{0}^{\infty}{2\over \sqrt{x^2+2}}\cdot{\mathrm dx\over (x^2+1)^2}\\
&=&2\int_0^{\pi/2}\frac{1}{\sqrt{1+\sec^2t}}\frac{1}{\sec^4t}\sec^2tdt\\
&=&2\int_0^{\pi/2}\frac{\cos^3 t}{\sqrt{1+\sec^2t}}dt\\
&=&2\int_0^{\pi/2}\frac{\cos^3 t}{\sqrt{2-\sin^2t}}dt\\
&=&2\int_0^{1}\frac{1-u^2}{\sqrt{2-u^2}}du\\
&=&u\sqrt{2-u^2}|_0^1\\
&=&1.\\
&&\int_{0}^{\infty}{x^2-x\over (x^2+1)^{5/2}}\mathrm dx\\
&=&\int_{0}^{\pi/2}{\tan^2t-\tan t\over (\sec^2t)^{5/2}}\sec^2t\mathrm dx\\
&=&\int_{0}^{\pi/2}(\tan^2t-\tan t)\cos t\mathrm dx\\
&=&\int_{0}^{\pi/2}\cos t\sin t(\sin t-\cos t)\mathrm dx\\
&=&\int_{0}^{\pi/2}\cos t\sin^2 t\mathrm dx-\int_{0}^{\pi/2}\cos^2 t\sin t\mathrm dx\\
&=&0.
\end{eqnarray}


How do I redisplay Ticker Text?

Question: For my app, I use one notification ID as to not clutter the Notifications Menu of a user. Each of my notifications have Ticker Text. When there are no notifications from my app, and the user gets notified, the ticker text displays. When a notification already exists, and is merely updated, the ticker text does not get displayed.
I made a work around which is very hacky, where I cancel the notification before I notify, but this ultimately causes a very obvious lag with vibrations.
Currently how I have notifications happening:
    mBuilder.setSmallIcon(R.drawable.actionbar_logo)
            .setContentTitle(extras.getString("title"))
            .setContentText(extras.getString("summary"))
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(extras.getString("extended_text"))
            )
            .setLights(Color.WHITE, NOTIF_LIGHT_INTERVAL, NOTIF_LIGHT_INTERVAL)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("mainActivityNotification", true);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 424242, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(contentIntent);

    final Notification notification = mBuilder.build();
    notification.tickerText = extras.getString("title") + "\n" +
                              extras.getString("summary") + "\n" +
                              extras.getString("post_body");

    mNotificationManager.notify(GATE_NOTIFICATION_ID, notification);

Comment: Looking at the answer here http://stackoverflow.com/a/16435330/2841101.
It says you have to use the same builder next time. And using that builder you just update the tickertext.
Comment: You probably are messing up with `PendingIntent`s, but for anyone to be able to answer the question you should provide the minimum relevant code.
Answer: Android does not display the ticker text again if it is the same as before.
You may use setTicker  API of  Notification.Builder class instead of constructing ticker text using Notification object, as  Notification.Builder has been added to make it easier to construct Notifications.
And if your intentions is to re-display the same ticker text(from previously posted notification), then just add this Line :
mNotificationManager.notify(GATE_NOTIFICATION_ID, mBuilder.build());

After you posted your first notification.
So, your complete code looks like as below:
mBuilder.setSmallIcon(R.drawable.actionbar_logo)
            .setContentTitle(extras.getString("title"))
            .setContentText(extras.getString("summary"))
            .setAutoCancel(true)
            .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(extras.getString("extended_text"))
            )     //consider using setTicker here
            .setLights(Color.WHITE, NOTIF_LIGHT_INTERVAL, NOTIF_LIGHT_INTERVAL)
            .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);

    Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("mainActivityNotification", true);
    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 424242, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder.setContentIntent(contentIntent);

    final Notification notification = mBuilder.build();

    //consider using setTicker of Notification.Builder
    notification.tickerText = extras.getString("title") + "\n" +
                              extras.getString("summary") + "\n" +
                              extras.getString("post_body");

    mNotificationManager.notify(GATE_NOTIFICATION_ID, notification);
    //second time onward, add your changed content like setContentText, setTicker etc.
    mNotificationManager.notify(GATE_NOTIFICATION_ID, mBuilder.build());

How do I redisplay Ticker Text?


Transformar um array de objetos em outro array de objetos com campos diferentes

Question: Preciso transformar este array de objetos:
const produto = [
  {
    produto: "Teclado",
    price: 12,
  },
  {
    produto: "mouse",
    price: 23,
  },
];

Especificamente, transformar em:
const produtoTransform = [
  {
    descricao: "Teclado",
    valor: 12,
  },
  {
    descricao: "mouse",
    valor: 23,
  },
];

Esse ultimo valor é para poder passar para uma API. Tentei instanciar um objeto com new Object.
Comment: Recomendo aprender o básico do JavaScript, principalmente de arrays, recomendo aprender pela documentação da MDN, sugestões: [for..of](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) e [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)
Answer: Basta você utilizar a função map da sua lista, que, por definição, tem o objetivo de converter cada item da lista em outro:
const produtoTransform = produto.map(item => ({
    'descrição': item.produto,
    'valor': item.price
}));

Answer: O laço de iteração for...of percorre objetos iterativos chamando uma bloco de código contendo instruções a serem executadas para o valor de cada objeto distinto.

const produto = [{
  produto: 'Teclado',
  price: 12

}, {
  produto: 'mouse',
  price: 23

}];

let resultado = [];

for (let e of produto) {
  resultado.push({
    descrição: e.produto,
    valor: e.price
  });
}

console.log(resultado);

console.log(resultado[0].descrição);


PHP js alert message when form was sent ok

Question: I'm trying to show an alert box saying the message was sent OK, but for some reason I don't see the message. I do get the one saying to check the captcha. What am I doing wrong? All the code is in my index.php file. All the if's at the beginning where attempts to avoid resubmitting the form.
Also, how can I style the alert box? I've tried using Bootbox. Js but without luck.
<?php
$message['message'] = "";
if (($_SERVER['REQUEST_METHOD'] == 'POST') && isset($_POST['submit'])) {

    if (isset($_POST['g-recaptcha-response']) && ($_POST["g-recaptcha-response"] != '')) {

        $url = 'https://www.google.com/recaptcha/api/siteverify';
        $privatekey = "--secretKey--";

        $response = file_get_contents($url."?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
        $data = json_decode($response);

        if (isset($data->success) AND $data->success==true) {

            $to = "firstname.lastname@example.com";
            $email = $_POST['email'];
            $name = $_POST['name'];
            $subject = $_POST['asunto'];
            $subject2 = 'Copia de: '.$_POST['asunto'];
            $message = $name . " " . "escribio lo siguiente:" . "\n\n" . $_POST['message'];
            $message2 = "Le enviamos una copia de su mensaje " . $name . "\n\n" . $_POST['message'];
            $headers = "From:" . $email;
            $headers2 = "From:" . $to;
            mail($to,$subject,$message,$headers);
            mail($email,$subject2,$message2,$headers2);
            //echo "La consulta se envió correctamente. Gracias! " . $name . ", lo contactaremos a la brevedad.";
            echo '';
            //Back to the web
            header('Location: index.php');

        }

        } else {
            echo '';
            //$_SESSION['form'] = filter_input_array(INPUT_POST);

    }
}

?>

Form:

Escribinos

    <form class="contact-form" action="" method="post" id="form">

            Nombre
            <input type="name" class="form-control" name="name" id="name" placeholder="Nombre y Apellido" required title="Por favor ingrese su nombre y apellido" value="<?php echo (isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''); ?>" />

            Asunto
            <input type="asunto" class="form-control" name="asunto" id="asunto" placeholder="Asunto" required title="Por favor ingrese el asunto" value="<?php echo (isset($_POST['asunto']) ? htmlspecialchars($_POST['asunto']) : ''); ?>" />

            Email
            <input type="email" class="form-control" name="email" id="email" placeholder="Email" required title="Por favor ingrese un mail" value="<?php echo (isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''); ?>" />

            Mensaje
            <textarea class="form-control" id="message" name="message" rows="7" placeholder="Mensaje" required title="Por favor ingrese un mensaje mayor a 10 caractéres."><?php echo (isset($_POST['message']) ? htmlspecialchars($_POST['message']) : ''); ?></textarea>

            <input type="submit" name="submit" id="btn-submit" class="btn btn-send-message btn-md" value="ENVIAR">

    </form>

Answer: Don't know about the rest of the code or if this is the problem - but you shouldn't use "AND" in the 'if' statetement - it should be :
if (isset($data->success) && $data->success==true) {...

Answer: After removing the
header('Location: index.php');

The message appears. Don't ask me why.


Mesclar uma feature com vários commits ao longo do tempo em outro projeto

Question: Tem uma feature tenis com vários commits ao longo do tempo, e preciso mesclar essa feature em outro projeto.
Considere esta linha do tempo:
- x - tenisV1 - x - x - tenisV2 - x - tenisV3 - x - x - x - x - x - tenisV4

Eu quero:
- tenisV1 - tenisV2 - tenisV3 - tenisV4

Como posso resolver isso?
Estava pensando em exibir o diff de todos esses commits mesclados pra me ajudar, mas não sei como fazer isso sem mostrar alterações de commits não relacionados no meio da linha do tempo (commits x).
Estou usando git e também bitbucket então se tiver algo visual lá seria legal também.
Answer: Você pode usar o comando cherry-pick. Você escolhe os commits que deseja e traz eles pra branch que está.
No commit que você deseja replicar faça
- git log

A resposta será algo assim:
- "9ce8fa5a3d3a3555222eabb234df93cc5447####"

O resultado será o ID do commit, copie-o, faça um checkout pra sua nova feature.
- git checkout "nome da branch".
- git cherry-pick "9ce8fa5a3d3a3555222eabb234df93cc5447####"

Repita o processo para todos os commit em ordem.
Answer: Complementando a resposta do @MarcelFelipe
É possível também usar o cherry-pick com vários commits caso eles estejam em ordem, por exemplo, considere os commits A - B - C - D, sendo A mais velho e D o mais novo:
git checkout branch-destino
git cherry-pick A^..D

Observação: ^ serve para incluir também o commit A
Caso a ordem esteja bagunçada, o comando irá falhar silenciosamente, fonte.


Can a single GAN learn multiple probability distributions

Question: Assuming for example to generate a sentence, I have two distributions i.e one that represents a semantic  and another a syntactic structure of a sentence. This distributions are different. Can I train GAN to learn learn the two distributions. My simple question is: it possible for GAN to learn two distributions at the same time?
Comment: Is this not a joint distribution?
Answer: I think you're misunderstanding the concept of the true distribution of the data. When we're taking about the (true) distribution of the data or about learning it, we have in mind a mathematical model where the data we observed is a realization of some underlying distribution (or data generating process). Data comes from a distribution, there's no "distributions".
Going back to your example, if there were two distinctions "semantic" and "syntactic", this would mean that you are assuming that some of the sentences are generated only based on semantic rules, other only based on syntactic rules. Unlikely your data consists of meaningless, nonsense sentences, that are valid according to syntactic rules. The distribution of the data are all the process that lead to generating your data.
You can have a model that is designed to learn something like syntactic rules based on the data, but you would need to specifically guide the model to do so. Within GAN, or any other neural network, you can have components, or subnetworks, learning different tasks. For this you would usually need data labeled according to all the tasks and different loss functions.


What is the distribution of $x^p$ if x is chi-squared distributed?

Question: I am thinking about the powers of random variables that have a skewed distribution to construct confidence intervals for these.
So, consider x follows a chi-squared distribution. What is the distribution of $y=x^p$? Lets say p=2.
The density function of x, given degrees of freedom df, is:
$$ \frac{X^{df/2-1} e^{-X/2}} {2^{df/2} \Gamma(df/2) }$$
if x>0.
So it seemed to me that the density of $y=x^2$ should equal the chi-squared density of $\sqrt{y}$, ie.
$$ f_{df}(y) = \frac{\sqrt{y}^{df/2-1} e^{-\sqrt{y}/2}} {2^{df/2} \Gamma(df/2) }$$
for y>0. This would work for discrete random variables, it seems. But for continuus variables the integral of the density will be greater than 1.
So, what did I do wrong?
Answer: Welcome to MSE.
For the sake of abbreviation, I will write $\text{df} = \nu$.
The result you're looking for is this:

Suppose $X$ is a continuous random variable with PDF $f_{X}$ and $Y = g(X)$ is a monotonic transformation of $X$ (which is also a continuous random variable). Then
  $$f_{Y}(y) = f_{X}(g^{-1}(y)) \cdot \left|\dfrac{\text{d}}{\text{d}y}[g^{-1}(y)] \right|\text{.}$$

For a proof of this theorem, see my answer here.
Essentially, your work is correct, but you forgot to multiply by the absolute value of the derivative of $\sqrt{y}$ with respect to $y$, which would be $\dfrac{1}{2\sqrt{y}}\text{.}$ For continuous random variables, this adjustment needs to be made; not necessarily the case for discrete random variables.
Thus, let's compute
$$\int_{0}^{\infty}\dfrac{(\sqrt{y})^{\nu/2-2}e^{-\sqrt{y}/2}}{2^{\nu/2+1}\Gamma(\nu/2)}\text{ d}y$$
Plugging this into Wolfram Alpha shows that the integral is equal to $1$.
Comment: Thanks a lot for the quick response and clear answer.


Solving a system of $2n-2$ equations, what is the relationship of solutions between successive $n$?

Question: I am trying to solve a system of $2n-2$ equations.  The first two rows of the matrix representation are always $$\begin{pmatrix}-8&1&1&0&\ldots& 0\end{pmatrix} \text{and}\begin{pmatrix}-1&-7&2&1&0&\ldots& 0\end{pmatrix}.$$
The bottom rows are always $$\begin{pmatrix}0&\ldots& 0&1&2&-6&2\end{pmatrix} \text{and}\begin{pmatrix}0&\ldots& 0&1&2&-6\end{pmatrix}.$$  The rows inbetween are $\begin{pmatrix}1&2&-6&2&1&0&\ldots& 0\end{pmatrix}$ and shifting over one to the right as you go down.  In my particular problem $x_1$ is always $1$ so that's why there's only $2n-2$ variables.
For example, for $n=4$, the matrix equation is:
$$
M_4\bf{x}=\begin{pmatrix}-8 & 1 & 1 & 0 & 0 & 0   \\
  1 &-7&  2&  1 & 0 & 0   \\
  1 & 2 &-6 & 2 & 1 & 0   \\
  0 & 1 & 2 &-6 & 2 & 1   \\
  0 & 0 & 1 & 2 &-6 & 2  \\
  0 & 0 & 0 & 1 & 2 &-6 \\
\end{pmatrix}_{6\times6}\begin{pmatrix}x_2\\x_3\\x_4\\x_5\\x_6\\x_7\end{pmatrix}=\begin{pmatrix}a\\b\\c\\d\\e\\f\end{pmatrix}
$$
For $n=5$
$$
M_5\bf{x}=\begin{pmatrix}-8 & 1 & 1 & 0 & 0 & 0 & 0 & 0 \\
  1 &-7&  2&  1 & 0 & 0 & 0 & 0 \\
  1 & 2 &-6 & 2 & 1 & 0 & 0 & 0 \\
  0 & 1 & 2 &-6 & 2 & 1 & 0 & 0 \\
  0 & 0 & 1 & 2 &-6 & 2 & 1 & 0\\
  0 & 0 & 0 & 1 & 2 &-6 & 2 & 1\\
  0 & 0 & 0 & 0 & 1 & 2 &-6 & 2\\
  0& 0 & 0 & 0 & 0 & 1 & 2 &-6\\
\end{pmatrix}_{8\times 8}\begin{pmatrix}x_2\\x_3\\x_4\\x_5\\x_6\\x_7\\x_8\\x_9\end{pmatrix}=\begin{pmatrix}a\\b\\c\\d\\e\\f\\g\\h\end{pmatrix}
$$
My particular problem is that I want to solve thousands (possibly millions) of such systems but it is computationally demanding.  You'll notice that you can step down - the $6\times6$ is simply the $8\times8$ matrix with the last two rows and columns removed.  Similarly, you can step up by extending the previous matrix with two rows and columns.
The core of my question is this: is there some relationship between the inverse of $M_n$ and $M_{n+1}$ that I can utilize to make solving more efficient?
Specifically, I am interested in $x_n$ for each $n$ if it is more efficient to only calculate that.
Answer: Consider the following symmetric "pentadiagonal" Toeplitz matrix:
$$T=\begin{pmatrix}-6 & 2 & 1 & 0 & 0 & 0 & 0 & 0 \\
  2 &-6&  2&  1 & 0 & 0 & 0 & 0 \\
  1 & 2 &-6 & 2 & 1 & 0 & 0 & 0 \\
  0 & \ddots&\ddots & \ddots &\ddots & \ddots & 0  & 0 \\
  0 & 0 &\ddots & \ddots &\ddots & \ddots & \ddots & 0\\
  0 & 0 & 0 & 1 & 2 &-6 & 2 & 1\\
  0 & 0 & 0 & 0 & 1 & 2 &-6 & 2\\
  0& 0 & 0 & 0 & 0 & 1 & 2 &-6\\
\end{pmatrix}_{n \times n}$$
The matrix you describe in the general case is a so-called "rank-2 perturbation" of matrix $T$, which is
$$M=T-UU^T$$
where
$$U^T=\begin{pmatrix}1&1&0&0&\cdots 0 \\ 1&0&0&0&\cdots 0
\end{pmatrix}$$
Such a perturbation of a matrix gives rise to a similar formula giving the induced perturbation on their inverses in terms of Woodbury Matrix Identity whose general form is
$$\displaystyle{M^{-1}=\left(T+UCV\right)^{-1}=T^{-1}-T^{-1}U\left(C^{-1}+VT^{-1}U\right)^{-1}VT^{-1}}$$
which boils down in our case, taking $C=-I_n$ (identity matrix of order $n$) and $V=U^T$ :
$$M^{-1}=\left(T-UU^T\right)^{-1}=T^{-1}-\underbrace{T^{-1}U\left(-I+U^TT^{-1}U\right)^{-1}U^TT^{-1}}_{\text{perturbation term } P}$$
Dealing with inversion of matrix $T$ itself, see for example this article "An explicit formula for the inverse of a pentadiagonal
Toeplitz matrix".
Edit : In fact, even for moderate values of $n$, $T^{-1}$ is close to $M^{-1}$. Take a look at the following picture displaying $T^{-1}$ on the left and $M^{-1}$ on the right ($50 \times 50$ matrices) under a colored form with a conversion bar on the left. We see that the differences are very tiny, which is confirmed by precisely comparing the different entries (no entry in the perturbation matrix $P$ is larger than $10^{-4}$).
Comment: For inversion of symmetric Toeplitz matrices, see Trench algorithm [here](https://dl.acm.org/doi/pdf/10.1145/321541.321549)
Comment: @Rócherz Thanks for the stylistic corrections that really improve this answer.


Sending SIGINT signal to child process that uses execvp in C

Question: I'm making a shell in C and I need to be able to handle SIGINT and SIGTSTP signals so as the shell doesn't terminate but it passes the signals to the child processes. I've found various posts here saying I should either use kill(child_pid,SIGINT) in the signal handler with child_pid being the id fork() returns, or to use killpg() while setting a new groupid to the child process before it uses execvp(). None of these work for me, as the handler only displays what I've put in it using the write() function, but doesn't terminate the child.
// global variable
__pid_t child_pid = 0;

int main(){
// init variables
pid_t childpid;
char *commandp;

 while(1){
   ignore();

   //do stuff that read input

   // do stuff that end up using fork to make child process

   if ( ( childpid = fork() ) == -1 ){
            perror ( " Fork fail " );
            exit (2); // exit value 2 means fork fail inside the shell
    }
    if (childpid == 0){
        setpg(getpid(), getpid());
        child_pid = getpgrp();

        commandp = argv[0];

        execvp(commandp, argv);

    }
    else {
        ignore_child();
        waitpid(childpid, &status, WUNTRACED);
    }
 }
}

//signal handling functions
void ignore(){
    struct sigaction act;

    act.sa_handler=SIG_IGN;

    // sigaction()
    sigaction(SIGINT, &act, NULL);
    sigaction(SIGTSTP, &act, NULL);
}

void ignore_child(){

    struct sigaction act;
    act.sa_handler = &handle_signal;
    sigaction(SIGINT, &act, NULL);
    sigaction(SIGTSTP, &act, NULL);
}

void handle_signal(int sig){

    if (child_pid != 0)
        if (sig == SIGINT){
            write(STDERR_FILENO, "Received SIGINT (CTRL-C) signal. Exiting...\n", 46);
            killpg(child_pid, SIGINT);
        }
        else if (sig == SIGTSTP){
            write(STDERR_FILENO, "\nReceived SIGSTP (CTRL-Z) signal. Stopping...\n", 47);
            killpg(child_pid, SIGTSTP);
        }
}

Any idea why the child process doesn't terminate but the message from the signal handler displays fine?
Answer: The first thing your loop does is ignore(), setting the SIGINT disposition to SIG_IGN. This disposition is inherited by the child, and travels accross exec. Quoting man execve,

Signals set to be ignored in the calling process are set to be ignored in the new process.

so no surprise it is not killed. The first instinct is to restore the SIGINT disposition back to SIG_DFL in the child. This would appear to work, but...
... the SIGSTOP problem will sill be unresolved, because SIGSTOP cannot be caught. The right solution is not to tinker with the signals at all. Instead, let the shell detach from  the controlling terminal. This way, no signal generated by the tty driver will reach the shell. In a very broad strokes,
    signal(SIGTTOU, SIF_IGN);
    if ((child = fork()) < 0)
        perror("fork")
    if (child == 0) {
        setpgid(0, 0);
        tcsetpgrp(0, getpgrp());
        execve(...);
        perror("exec");
    } else {
        setpgid(child, child);
        tcsetpgrp(STDIN_FILENO, child);
        waitpid(...);
    }

I don't know the scope of your project, but keep in mind that job control is far from easy.
Comment: The problem was that I wasnt restoring the default signal handling in the child...
Now about the SIGSTOP, i'll probably remove it since we weren't asked to implement something like that


loopback3 coffeeshop tutorial: use curl to get reviewers

Question: In the above tutorial, reviews do not require an authorization token but coffeeshops do. And this works; I use curl to send email/password for a reviewer, receive an auth token and then send the auth token to get a list of shops.
But when I try the same to get a list of reviewers, I get an error of authorization required. Even though I sent an auth token.
Can someone explain this? What have I not understood?
Regards,
John
Answer: I'm unable to test this, but I'm pretty sure I'm correct.
Reviewer is based on User, and nobody is allowed to find all Users.
In your scenario, you are authorized in the sense you have a token.  This gives you the $authenticated role, and if you have created anything probably the $owner role for those things.
If your next question is "Can I allow $authenticated or $everyone to find all Users/Reviewers?"  See this question.
Details
You can see the difference in the access controls by looking at the Review  model's ACLs
And look at User's ACLs
Notice the User model uses the $owner role often (indicating you must be that User) and that it doesn't allow find, only findById for the $owner
Comment: Thanks, that makes sense. Though it would be nice (from an admin perspective) to list all of the users.
Comment: Just to add I tried Reviewers/id-number and that worked.
interestingly, Reviewers/id-number/exists gave a requires auth error.
And trying with a different user id, having listed directly from the database, also gives an auth req error.
Comment: If you want to see exactly how loopback is deciding you can launch your server like `DEBUG=loopback:security:* node .` on linux/macos or `set DEBUG=loopback:security & node .` or windows


How to add css properties to canvas

Question: I am using canvas in my html page.
Adding css properties directly to the drawn canvas is not possible,But How to add css property to html5 canvas.
Comment: http://stackoverflow.com/questions/22757825/applying-css-on-drawn-canvas-elements
Answer: In general you can use some CSS like width and height via id or class.
http://www.w3schools.com/html/html5_canvas.asp
All other colors of objects you draw on it you have to define in your drawing script. Have a look in the documentation above.
Answer: You can add CSS in Canvas like .

 <canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"></canvas>

You can not add css properties on drawn canvas instead , you can use Javascript to add effects into it.
For More refer this SO Link

How to add css properties to canvas