MySQL select from second row where

Question: I have three tables containing books and tags. A book can be tagged with several tags.
What I want to do is to filter out books whish are tagged with both 'Comics' AND 'Romance'.

The first book is tagged only with 'Comics'.
The second book is tagged with both 'Comics' AND 'Romance', so this row should be returned in our example.
The third book is tagged only with 'Fantasy'.

How do I construct a proper query to find the second book?

..select book_id from books b, tags t, tags_to_books tb where ((FIND
  TAGS_ID '7' AND '8'))..

Table design:
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
  `book_id` int(11) NOT NULL AUTO_INCREMENT,
   PRIMARY KEY (`book_id`)
 );

 INSERT INTO `books` (`book_id`)
 VALUES
(1),
(2),
(3);

 DROP TABLE IF EXISTS `tags`;
 CREATE TABLE `tags` (
   `tag_id` int(11) NOT NULL AUTO_INCREMENT,
   `tag_name` varchar(11) NOT NULL DEFAULT '',
   PRIMARY KEY (`tag_id`)
 );

 INSERT INTO `tags` (`tag_id`, `tag_name`)
 VALUES
(7,'Comics'),
(8,'Romance'),
(9,'Fantasy');

 DROP TABLE IF EXISTS `tags_to_books`;
 CREATE TABLE `tags_to_books` (
   `book_id` int(11) NOT NULL,
   `tag_id` int(11) NOT NULL
 );

 INSERT INTO `tags_to_books` (`book_id`, `tag_id`)
 VALUES
(1,7),
(2,7),
(2,8),
(3,9);

Comment: Nicely asked question.  Just the right amount of detail to let people work on the answer.
Answer: You could use something like this, which is effectively the same as an intersection operator in other SQL languages:
select comics.book_id from (
select b.book_id
  from books b
  join tags_to_books tb on tb.book_id = b.book_id
  join tags t on tb.tag_id = t.tag_id
  where t.tag_id = 7) as comics
join (
select b.book_id
  from books b
  join tags_to_books tb on tb.book_id = b.book_id
  join tags t on tb.tag_id = t.tag_id
  where t.tag_id = 8) as romance
on comics.book_id = romance.book_id

This could probably be optimized a little, but the idea is to get all the books that are in Fantasy and then all the books that are in Romance, and then return the books that are in both.  If you needed to create a query where you needed more criteria, you would just copy the JOIN clause and update the tag id.
Link to SQL fiddle
Answer: If I understand you correctly, the same book can appear in the tag table multiple times. Correct?
Have you tried something like
SELECT book_id
FROM books
WHERE book_id NOT IN
    (SELECT comics.book_id
    FROM tags_to_books as comics
    JOIN tags_to_books as romance
      ON comics.book_id = romance.book_id
    WHERE comics.tag_id = 7
      AND romance.tag_id = 8)

?
Comment: I think you got it backwards.  All you need is the inner query - SELECT comics.book_id
    FROM tags_to_books as comics
    JOIN tags_to_books as romance
      ON comics.book_id = romance.book_id
    WHERE comics.tag_id = 7
      AND romance.tag_id = 8  - SQLFiddle - http://www.sqlfiddle.com/#!2/fc556/21
Comment: Wasn't the request to filter **out** the books where it was tagged with both comic and romance?
Comment: "Filter out" was used to basically mean "returns"  Requirement #2 was where the OP said that she just wanted book #2
Comment: This would probably be the fastest solution. As @BrianHoover noted - to get the inverse set of books just remove the outer query.
Comment: Thank you so much Jonny and Brian! I ended up using Jonny's version with Brian's modification in the first comment above. Thank you! :)
Answer: Hard to read that but:
SELECT B.book_id
FROM Books B INNER JOIN tags_to_books ttb
ON ttb.book_ID = b.book_ID
JOIN tags t
ON t.tag_ID = ttb.Tag_ID
WHERE t.tag_ID IN(7) AND t.tag_ID IN(8)

I haven't compiled this but it should work
OR as Joe described, you can do this by tag_name
SELECT B.book_id
FROM Books B INNER JOIN tags_to_books ttb
ON ttb.book_ID = b.book_ID
JOIN tags t
ON t.tag_ID = ttb.Tag_ID
WHERE t.tag_name = 'Comics' AND t.tag_name = 'Romance'

Comment: I think you're nearly there, in that OP probably wants `SELECT B.book_id
FROM Books B INNER JOIN tags_to_books ttb
ON ttb.book_ID = b.book_ID
JOIN tags t
ON t.tag_ID = ttb.Tag_ID
WHERE t.tag_ID IN( SELECT tag_id FROM tags WHERE tag_name = 'Comics' OR tag_name = 'Romance')`
Comment: The first query will return both book 1 and book 2 since book 1 is also IN(7,8).  A correct query will only return book 2.
Comment: @Joel My apologies, I did not see that. The OP code was not formatted when I answered so was difficult to read what values were needed!
Answer: If you only need the book_id (rather than, say, the name of the book or the text of the tag, this works (but requires changing the magic number described below if you have a different number of tags in your query):
SELECT book_id FROM tags_to_books
WHERE tag_id IN (7, 8)
GROUP BY book_id
HAVING COUNT(DISTINCT book_id, tag_id) = 2;

That 2 is the number of tags, which you'd have to change if there were more or fewer tags in your query.
Answer: Here is the sql: http://sqlfiddle.com/#!2/fc556/6
Hope this helps

MySQL select from second row where


Help with complex sql query

Question: To make story short, i'm building self-learning banner management system. Users will be able to insert these banners to their site when banners will be shown based on sales/impressions ratio.
I have 4 tables
Banners
bannerID      int
bannerImage   varchar....

SmartBanners
smartBannerID      int
smartBannerArrayID int
bannerID           int
impressionsCount   int
visibility         tinyint (percents)

SmartBannerArrays
smartBannerArrayID int
userID             int

Statistics
bannerID          int
saleAmountPerDay  decimal...

Each night i need to generate new "visibility" for each SmartBanner based on whole SmartBannerArray that same user has. So i need to get sum of impressions and sales for each bannerID in SmartBannerArray.
All comes to my mind is to use double cursor, one will loop thought SmartBannerArrays get needed values for sum of impressions and sales and then inner loop which will access each SmartBanner and change it's "visibility" percentage based on
(sales/impressions)/(sumOfSales/sumOfImpressions)*100
Hope you get the picture...
Is there any other way to design better tables or not to use double cursor to avoid server overload ?
MORE INFO
Each sale written to Statistics table , field is updated because i need daily sum per banner rather each sale. User create BannerArray, choose products he would like to promote. Each product he chose is written to Banners table with proper image and other info.
SmartBanners table stores bannerID so as Statistics table, while BannerArray table assigns this group of banners to certain user.
Comment: If you are really after design improvements you will have to further describe the process that you are trying to model (how is the Statistic table populated, what is the relation between the smartbanners and banners, what are smartbannerarrays, etc...). Assumptions can be made, but analysis based on assumptions is not a good thing.
Comment: did so, if you still have a question free to pm me...
Answer: The picture is not so clear. Why cursors at all, what is wrong with this?
SELECT SUM(saleAmountPerDay)/SUM(impressionsCount)
FROM SmartBanners sb        INNER JOIN
     SmartBannerArrays sba  ON sb.smartBannerArrayID = sba.smartBannerArrayID INNER JOIN
     Statistics ss          ON sb.bannerID = ss.bannerID
GROUP BY smartBannerArrayID

Which can be used then as subquery to calculate 'visibility' directly.
EDIT2:
Illustrating the principle (SQL not optimized), why not:
UPDATE SmartBanners
SET visibility =
    ROUND( 100. *
    (
    SELECT SUM(saleAmountPerDay)/SUM(impressionsCount)
    FROM SmartBanners sb        INNER JOIN
         Statistics ss          ON sb.bannerID = ss.bannerID
    WHERE sb.smartBannerID = SmartBAnners.smartBannerID
    ) /
    (
    SELECT SUM(saleAmountPerDay)/SUM(impressionsCount)
    FROM SmartBanners sb        INNER JOIN
         SmartBannerArrays sba  ON sb.smartBannerArrayID = sba.smartBannerArrayID INNER JOIN
         Statistics ss          ON sb.bannerID = ss.bannerID
    WHERE sb.smartBannerArrayID = SmartBAnners.smartBannerArrayID
    ) )

if you want to avoid the cursors.
Also, not to forget - if this data is not going to be updated often (what's the frequency of the update of the statistics table? I imagine you update it periodically from some logs) AND if you are looking to maximize read performance you might consider triggers.
EDIT3:
In MS SQL you can also use OVER clause, just a short example
SELECT
       SUM(saleAmountPerDay) OVER(PARTITION BY BannerID) AS 'TotalSalesByBanner',
       SUM(impressionsCount) OVER(PARTITION BY BannerID) AS 'TotalImpressionsByBanner',
       SUM(saleAmountPerDay) OVER(PARTITION BY smartBannerArrayID) AS 'TotalSalesByArray',
       SUM(impressionsCount) OVER(PARTITION BY smartBannerArrayID) AS 'TotalImpressionsByBanner'
FROM
       SmartBanners sb INNER JOIN
       Statistics ss   ON sb.bannerID = ss.bannerID
GROUP BY
       SmartBannerID

This SQL is not tested.
Comment: I will still be needed to have on cursor to set "visibility" otherwise each time new banner will be generated this huge query will run...
Thanks for saving one cursor....
Comment: If you find things useful, feel free to upvote. Now, I edited the answer. If you would like exact example of the query can you explain further (edit your question) the relation between the SmartBanners and Banners tables?
Comment: Problem is that i need to update each banner, thus to loop thought them. Think of that, i have lots of stats, lots of banners and lots of banner arrays. I need to update visibility of each banner so next time user accesses the array it will show most sold product..
Comment: Ok, I've updated the answer, pls check if it works on MS SQL server - there could be issues with locking (2notes: I did not specially emphasize but the queries use INNER JOIN which means that if there will be no statistics for certain banner then the the stats would not be updated, turning join on ss to LEFT JOIN solves that, also you should take special care for impressionsCount = 0)
Comment: @augeneK, correlated queries used in the above are similar to loops/cursors; the query should update the stats for all of the SmartBanners with no cursors.
Comment: Will this update list of 1000 banners at once with the same value ?
Comment: lol we you answered before i've asked... thanks i will try that
Comment: @eugeneK, no, not with the same value - each banner gets its own value (basically subqueries are performed for each row and are filtered on the values of the external query).
Answer: In many cases the use of cursors can be avoided by using derived tables. An introduction to the use of them can be found here.
Answer: I think you're asking the right question, which is: is there any way to design better tables?  My advice: worry less about server overload, which is no big deal if you're running queries only once a day.  Worry more about a comprehensible structure.
What about this:
 Banners
 -------
 BannerID
 UserID
 BannerImage
 BannerVisibility  // A denormalized value that you calculate once a day

 Banner_Sales
 ------------
 BannerID
 Date
 SalesAmt

 Banner_Impressions
 ------------------
 BannerID
 Date
 Impressions

You don't need a SmartBannerArray - or cursors - to sum the sales and impressions.  Just use a SQL sum:
 SELECT SUM(bs.SalesAmt) AS SALES, SUM(bi.Impressions) AS IMPRESSIONS,
   SUM(bs.SalesAmt) / SUM(bi.Impressions) AS RATIO
 FROM Banners b
 JOIN Banner_Sales bs
 JOIN Banner_Impressions bi
 WHERE b.UserID = ? // For total sales / impressions for a user

Substitute
WHERE b.BannerID = ? // For total sales / impressions for a banner

Good luck!
P.S. tinyint's are for integers, not percentages.  You'll need a double.  :)
Comment: He chose to store percentages as integers, in the question formula is multiplied by 100. If such precision is satisfactory there's no problem with tinyints.
Comment: I need to have SmartBannerArray because the advertisement module i have will show group of banners for each user.

++ Thanks Unreason, i don't need more than that

Help with complex sql query


WPF - Updating Label Content During Processing

Question: Well I've tried several methods of getting this to work, background worker, Dispatcher.Invoke, threading within the called class and nothing seems, to work. The best solution so far is an Extension method which calls the invoke of the control. Also I've tried avoid passing the data for the label through my event classes and simply invoking within my processing code, however this made no difference.
In regards to the background component, I kept getting exceptions saying the background worker was busy, so I instantiated the class several times, however the label only visibly changed once the entire operation had been complete.
I've removed my previous code, here's everything that is relevant, as it seems the issue is difficult to resolve.
Method Being Called
 private void TestUris()
        {
            string text = new TextRange(rtxturis.Document.ContentStart, rtxturis.Document.ContentEnd).Text;
            string[] lines = Regex.Split(text.Remove(text.Length - 2), "\r\n");

            foreach (string uri in lines)
            {
                SafeUpdateStatusText(uri);
                bool result;
                string modUri;

                if (!uri.Contains("http://"))
                {
                    modUri = uri;
                    result = StoreData.LinkUriExists(new Uri("http://" + modUri));
                }
                else
                {

                    modUri = uri.Substring(7);
                    result = StoreData.LinkUriExists(new Uri(uri));
                }

                if (!result)
                {
                    Yahoo yahoo = new Yahoo();
                    yahoo.Status.Sending += (StatusChange);
                    uint yahooResult = 0;

                    yahooResult = yahoo.ReturnLinkCount(modUri);

                    if (yahooResult > 1000 )
                    { results.Add(new ScrapeDetails(Guid.NewGuid(), modUri, 1000, "Will be processed", true)); }
                    else
                    { results.Add(new ScrapeDetails(Guid.NewGuid(), modUri, (int)yahooResult, "Insufficient backlinks", false)); }

                }
                else
                {
                    results.Add(new ScrapeDetails(Guid.NewGuid(), modUri, 0, "Previously been processed", false));
                }
            }

            foreach (var record in results)
            {
                dgvresults.Items.Add(record);

            }

            EnableStartButton();

        }

Yahoo Class
public class Yahoo
    {

        ///
        /// Returns the amount of links each Uri has.
        ///
        public uint ReturnLinkCount(string uri)
        {
            string html;
            Status.Update(uri, false); //this is where the status is called
            try
            {

                html = client.DownloadString(string.Format("http://siteexplorer.search.yahoo.com/search?p=http%3A%2F%2F{0}&fr=sfp&bwm=i", uri));

            }
            catch (WebException ex)
            {
               ProcessError(ex.ToString());
               return 0;
            }

           return (LinkNumber(html));

        }

Status Classes
public class StatusEventArgs : EventArgs
    {
        private string _message;
        private bool _isidle;

        public StatusEventArgs(string message, bool isidle)
        {
            this._message = message;
            this._isidle = isidle;
        }

        public bool IsIdle
        {
            get { return _isidle; }
        }

        public string Message
        {
            get { return _message; }
        }
    }

   public class Status
    {
        public Status()
        {
        }

        // Declaring an event, with a custom event arguments class
        public event EventHandler<StatusEventArgs> Sending;

        // Some method to fire the event.
        public void Update(string message, bool isIdle)
        {
            StatusEventArgs msg = new StatusEventArgs(message, isIdle);
            OnUpdate(msg);
        }

        // The method that invokes the event.
        protected virtual void OnUpdate(StatusEventArgs e)
        {
            EventHandler<StatusEventArgs> handler = Sending;

            if (handler != null)
            {
                handler(this, e);
            }
        }
    }

Method That Changes the labels Content
 private  void StatusChange(object sender, StatusEventArgs e)
        {

            if(!e.IsIdle)
            {
                lblstatus.Content = e.Message;
                lblstatus.Foreground = StatusColors.Green;
                lblstatus.Refresh();
            }
            else
            {
                lblstatus.Content = e.Message;
                lblstatus.Foreground = StatusColors.Grey;
                lblstatus.Refresh();
            }

        }

The Refresh static method called:
 public static class ExtensionMethods
    {
        private static Action EmptyDelegate = delegate() { };

        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(DispatcherPriority.Render   , EmptyDelegate);
        }

Another EDIT: Staring at my code for a bit longer, I've realised, that the foreach loop will execute really quickly, the operation which takes the time, is
yahooResult = yahoo.ReturnLinkCount(modUri);

Therefore I've declared the status class (which handles the event and invokes the label etc) and subscibed to it. I've gotten better results, although it still feels random, sometimes I see a couple of label updates, and sometimes one even though the exact same URI's are passed, so weird.
Comment: Is this code executing inside a different thread?
Comment: Nope, sadly I still haven't managed to fix it yet :(.
Answer: I hope there is sth. helpful...
 private void button1_Click(object sender, RoutedEventArgs e)
{
    ThreadPool.QueueUserWorkItem(o =>
    {
        int result = 0;
        for (int i = 0; i < 9999999; i++)
        {
            result++;
            Dispatcher.BeginInvoke(new Action(() =>
            {
                this.label1.Content = result;
            }));
            Thread.Sleep(1);
        }
    });
}

Comment: I tried to put this into a method and call it in my WPF app, but the label still never updates.  So I resorted to using the Forms.Application.DoEvents() method.  Can someone tell me why my method fails:        private void SetStatus(string status)
        {
            ThreadPool.QueueUserWorkItem(o =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    lblStatus.Content = status;
                }));
            });
        }
Answer: SOLVED IT YES WOOHOOOOOOOO 3 days of testing, testing, testing.
I decided to start a new project just with the extension method above and a simply for loop to test UI update functionality. I started testing different DispatchPrioraties (tested them all).
Weirdly, I found the highest priorities were the worse, for example using Send didn't update the label at all, Render updated it twice on average. This was the weird behavior I was experiencing as I tried different priorities. I discovered Background:
The enumeration value is 4. Operations are processed after all other non-idle operations are completed.
Now this sounded exactly what I didn't want, as obviously the label should update during processing, hence why I never tried it. I'm guessing that once one of my method has been completed, before the next it called, the UI is updated. I'm find of guessing, but it 100% consistently updates correctly on two separate operations.
Thanks all.
Comment: im so confused.... i seem to be experiencing the same issue, and cant figure it out... only updates at the end of method instead... when the line is hit in the debugger... please assist.
Comment: The code that you finally got to work would sure be great to get to see. : )
Answer: Well this is going to sound stupid but you could just reference the forms namespace, and then you can do this
     using System.Windows.Forms;

     mylabel = "Start";
     Application.doEvents();

     myLabel = "update"
     Application.doEvents();

now the problem using this would be you are using wpf but you can still reference forms and use this namespace. The other issue is what ever is in the que would execute directly to the ui. However this is the most simplistic way of doing label updates i could think of.
Comment: only works when i do this, what is the correct way to do this?
Answer: would it be easier/better to add the status info as a property on this object, and have it just fire property change notifications?
that way the label text (or whatever) could be bound to the property instead of having the async work try to update a label?
or add a method like this to update status if you have to update it?
    void SafeUpdateStatusText(string text)
    {
        // update status text on event thread if necessary
        Dispatcher.BeginInvoke(DispatcherPriority.Background, (SendOrPostCallback)delegate
        {
            lblstatus.Content = text;
        }, null);
    }

otherwise, i don't think we have enough details to help yet....
Comment: Thanks for the method and advice John. I've tested that method five times with different Dispatch priories and the label doesn't change until the method I posted above is complete, it then displays the last uri processed.
Comment: Feels so random, sometimes it shows one update, sometimes more, so weird considering I'm passing the same data and the conditions are also identical.
Comment: are you doing it in another thread?  you could put breaks in and make sure your UI is getting updated properly.

If you are not doign it in another thread, you might never be giving the UI time to change?
Comment: Yeh the UI is being given time, I've just tried my update with an operation which takes around 12 seconds, I've only ever seen 1 label update throughout this period.

I'm sure I never had this problem with winforms.
Answer: I hope this helps:
private delegate void UpdateLabelDelegate(DependencyProperty dp, object value);

public void UpdateLabelContent(Label label, string newContent)
{
    Dispatcher.Invoke(new UpdateLabelDelegate(label.SetValue), DispatcherPriority.Background, ContentProperty, newContent);
}

Usage:
while (true)
{
    UpdateLabelContent(this.lblStatus, "Next random number: " + new Random().Next());
    Thread.Sleep(1000);
}

Comment: why wouldn't this work if called from MainWindow : Window, any ideas, only update after the full method as finished rather than wen the line is executed.


Assign property of second object to every element in first object(JS)

Question: I have code to merge two objects
Here is code

var obj1 = [{ food: 'pizza', car: 'ford'},
{food:'apple',car:'volvo'}];
var obj2 = { animal: 'dog' }

var allRules = Object.assign({}, obj1, obj2);

console.log(allRules);

It works, but in new object I have two elements and dog as 3 element. You can see it from snippet.
But I need two elements with dog as property. So it will be food, car and animal as key in every element of object.
How I can do this?
UPDATE
For merging array with object property I understood, but if I have two arrays like this
    var arr1 = [{
    food: 'pizza',
    car: 'ford'
  },
  {
    food: 'apple',
    car: 'volvo'
  }
];

var arr2 = [{
  animal: 'dog'
},
{animal:'cat'}
];

And want to merge it to one array with 1 element of 1 array, get's 1 element of 2 array , etc.
How I can do this?
Answer: obj1 is an array, so you need to iterate this array and assign the obj2 to each item in the array.
Use map
var allRules = obj1.map( s => Object.assign({}, s, obj2) );

Demo

var obj1 = [{ food: 'pizza', car: 'ford'},
{food:'apple',car:'volvo'}];
var obj2 = { animal: 'dog' }

var allRules = obj1.map( s => Object.assign({}, s, obj2) );

console.log(allRules);

Edit
For the updated question
var allRules = obj1.map( (s,i) => Object.assign({}, s, obj2[i]) );

Comment: Thank's. It help. I've updated post.
Answer: You need to iterate the array and then assign Object.assign() on item's of the array.

var obj1 = [{
    food: 'pizza',
    car: 'ford'
  },
  {
    food: 'apple',
    car: 'volvo'
  }
];

var obj2 = {
  animal: 'dog'
}

obj1.forEach(x => Object.assign(x, obj2));
console.log(obj1);

As per comment

var arr1 = [{
    food: 'pizza',
    car: 'ford'
  },
  {
    food: 'apple',
    car: 'volvo'
  }
];

var arr2 = [{
    animal: 'dog'
  },
  {
    animal: 'cat'
  }];

arr1.forEach((item, index) => Object.assign(item, arr2[index]));
console.log(arr1);
Comment: Thank's. It help. I've updated post.
Comment: @Yevhen, slightly different logic. Updated answer with snippet
Comment: Thank's, it works
Answer: Your first object is actually an array. You can map over it and assign your object into object into array. See this snippet:

var myArray = [{ food: 'pizza', car: 'ford'},
               { food: 'apple', car: 'volvo'}];
var obj = { animal: 'dog' };
var newArray = myArray.map((o) => Object.assign({}, o, obj));

console.log(newArray);

I changed variable names to myArray and newArray for you to see what you're dealing with.
Comment: Thank's. It help. I've updated post.


VMWare Workstation configure network adaptor to be 'Intel Networking'

Question: I am trying to create a Windows VM with VMWare Workstation that has requirement to "Use an Intel networking adaptor and not 'Virtio'" I am putting this in quote as this is one of the requirement listed, not cmming from me and I can not say why it is needed.
But I can not find where and how to configure the network adaptor type with VMWare Workstation.
The more I read about this the more I understand that Intel network adaptor is the default for VMWare, but I can not say for sure.
Does someone has info on how to do this?
Comment: Is there a good reason for this requirement?
Comment: I have zero control over the requirement
Answer: You need to edit the .vmx file directly.
Set ethernet0.virtualDev to the value e1000 to get an Intel E1000 network adapter.
ethernet0.virtualDev = "e1000"


Recording latency is keeps changing. How can I fix this

Question: This is more of an audio recording question but I'm looking for any help I can get. I have a shitty setup, audacity and snowball microphone, but they haven't had this problem anytime since I've used them.
The latency changes with every recording, so it is impossible to correct for. This makes it impossible to record.
EDIT: So exactly what is happening. I'll go to record something and there is latency. So I go through the typical motions of how to correct that based on what little I know. I create a click track, play the click track out loud and record it, then measure the distance between when the click occurs and when it was recorded in milliseconds with the cursor. I then go to edit>preferences>playback and set the latency correct to negative the number I measured.
If the latency is roughly the same every time then this fixes it to a certain degree of precision. Recently, and I've never had this problem with this setup before, the latency has been varying by about .1 seconds every time I do the click track thing. I'll measure .145, plug in -.145, then still notice latency so I do the test again and now it is .245. Then is goes to .128 or something. It makes it impossible to record coherently.
Comment: If you've got an existing multitrack recording that you've made and you're trying to mix it and running into trouble, you should describe that in detail instead of what you have here. Also note that your click track playback and record test does **not** measure recording latency. It measures playback and recording latency at the same time. Playback latency is affected by many things including any plugins present, even on other tracks. You're probably worrying about something that's not actually a problem. Actual problems you might have can likely be fixed with better gear.
Comment: We will need a lot more information before we can help. What is happening and what are you hearing and seeing that is leading you to say the latency is changing?
Comment: Can you explain what you mean by "there is latency"? Are you monitoring the mic through the software? Are you using a dedicated audio interface? If so, which one?
Comment: Just audacity and a usb mic. If I play one track out loud with the mic right next to it simultaneously recording another the two tracks do not end up synced during subsequent playback.
Comment: That's pretty common. I guess with dedicated interfaces and better software the software reads the one way latency through the interface and adjusts the recorded samples to line up automatically. Back in the day we would nudge stuff to the left the necessary number of samples after making a recording pass if necessary. But if you are separately tracking to a click, each pass of a different track will be exactly the same amount of time behind the click, so there no need to do anything to correct for the recording latency.
Comment: This kind of latency is only a problem if for some reason you are playing audio out through converters and recording it back in through converters, which is usually not done in home studios. In other words, don't do that.
Comment: Right, it would be easy to correct if the latency was always the same but for some reason it is varying by about .1 s each time I do the process I described. The latency varies with each take so I can't anticipate how much to correct it by and I can't just drag the samples because the mix is too complex at this point, that's the issue.
Comment: Install Asio4All drivers and/or make sure Audacity actually uses those drivers
Answer: We managed perfectly good multi-track digital recordings not THAT long ago when all soundcards had high latency - over half a second.  The only restriction was that we couldn't monitor an input through the audio engine while recording.  You either looped the input straight back to your headphones BEFORE it reached the audio interface and got digitised for the computer, or you didn't bother with monitoring the input at all.
Your Snowball isn't THAT crappy, but its USB connection isn't going to be low latency.  And, unlike some USB mics, it doesn't seem to offer a direct analogue output, for input monitoring while recording (which would require an external mixer anyway).  So, for a start, don't have the speakers on while recording, don't let the mic 'hear' the existing tracks.  Use headphones.
I don't think the latency figure is jumping around.  But your recording method is flawed.  You shouldn't be having to make manual offsets to compensate for latency.  You need a method that avoids the latency problem altogether.
Answer: For anyone googling this in the future I found a temporary fix. I set the audio to buffer, located right above the latency correction, as low as possible. From what I understand this gives the computer less time to process each sample of audio. So the CPU has to work harder and faster. This means less inherent latency because samples don't take as long to be handled by the computer, but the computer has to work harder. It seems like my computer can handle the extra load. Doing this gets the latency variance down to .01s, which is less perceptible.
This is just a band-aid and isn't really addressing the core problem. If anyone else has a solution please post it.
Comment: I guess the interesting question is how to apportion the blame between hardware and OS.  For what it's worth, I have found hardware can be a factor.  I have a Zoom H4n that also functions as an audio interface and have measured latencies varying by as much as 200ms.  On the other hand a Scarlett 2i2 hooked up to the same laptop predictably gives me the same latency to within 1ms.
Answer: ok this things are easy to do and really common. the audiobuffer is growing with my projects.
here some general steps for tighter recordings
1.Powersupply
Always play with your Macbook connected to the power supply, especially when using an external audio interface and / or MIDI controller. To make sure your Macbook is properly grounded, use the extension cable of the power supply.
2.USB HUBS
Use only USB hubs that draw power from their own external power supply. They are known in the market as powered USB hubs.
If you use an external USB audio interface, do not connect it to the USB hub. Audio interfaces require the largest amount of power and computation, thus they must be connected directly to a free USB port. You may connect all other external devices to the USB hub into another free USB port.
3. wifi and bluetooth
switch it off!
4.energie saver
go to sys.settings and switch to never sleep
5. Updates
make sure all is up to date
7 Graphics
switch off auto graphic switching
8 now we go to the audiopref.
Depending on the processing power of your computer and the amount of calculations needed for your application, a buffer size of 256 or 512 samples at a sample rate of 44100 Hz (on Mac OS X 10.9 or higher, we recommend 48000 Hz instead). A buffer size of 256 samples is a good starting point. Decrease this value to 128 samples if there is a noticeable lag between your actions and the audio, for example when playing virtual instruments live with a keyboard. Increase to 512 samples if you experience dropouts in the audio playback. so if your recordings are done you can go to higher sizes with no problems.
but if you want the real shit use a really good interface for ultra low audiolatency and apples iac bus and mtc to sync your daw perfect in time.
trigger only start without clock can be useful with midi, it is no problem to adjust the right startpoint. it becomes an issue, if the record is drifting in time.
In your case it sounds like your monitoring setup is not right. try some pre and post records to find the bug. your soundcard is also a possible point... drivers and firmware up to date?    cheerz


Why the length of the weight vector is likely to grow?

Question: In the book Michael Nielsen's Neural Networks and Deep Learning, in chapter 3 he writes: "Heuristically, if the cost function is unregularized, then the length of the weight vector is likely to grow, all other things being equal". Can you explain why the length of the weight vector is likely to grow?
Comment: It may be helpful to see it expressed in eqaution form. The length of the weight vector is given by $\Vert w \Vert$ (Euclidean norm). Using regularization, we want to minimize a cost function $C$ that is given by: $$ C = C_1 +\lambda \Vert w \Vert^2$$ where $C_1$ represents the unregularized cost function. In that equation we can see that big values of $\Vert w \Vert$ are penalized as they would make $C$ big $\Rightarrow$ with regularization, lower values of $\Vert w \Vert$ are achieved
Answer: Intuitively, all other things being equal, increasing the number of connections (increasing the weight vector) allowed to be searched will never lead to a worse model on trained data, as in the aggregate of the new neural net simply setting the added connections to weights $0$ will at least be able to perform as well as the old model.
And there is always some probability of some combination of non-zero weights performing better. Hence trained models when given the chance will seek to add these combinations.
This is the core of the overfitting problem however and is why regularisation is used regularly in practice.
Comment: the length of the weight vector is likely to grow means norm of weight is increasing. Is that right?
Comment: Um I haven't read the book you're referring to but I'm assuming he means vectors as the actual row vector, not the Euclidean sense. So the number of elements is what is actually increasing.
Comment: This is the rest of the paragraph "Over time this can lead to the weight
vector being very large indeed. This can cause the weight vector to
get stuck pointing in more or less the same direction, since changes
due to gradient descent only make tiny changes to the direction,
when the length is long. I believe this phenomenon is making it
hard for our learning algorithm to properly explore the weight
space, and consequently harder to find good minima of the cost
function."
Comment: Okay sure he's just interpreting the vector in Euclidean space then in a geometrical interpretation. They represent the same thing though hence the argument still applies. As you go from a specific n-dimensional vector to an n+1 dimensional vector, the Euclidian-norm can never decrease and always increases for new non zero elements. He's actually making another point though about the length of a vector and its relationship to the inertia of its direction when new elements are added, but im not sure thats the same question as your original.
Comment: You said 'the Euclidian-norm can never decrease and always increases for new non zero elements'. Why is that? Is it based on the weight update formula using gradient descent or something else?
Comment: No its a geometrical argument. Intuitively adding dimensions shouldn't reduce the length. But algebraically the euclidian norm is the square root of the inner product. Adding elements is always positively increasing.
Comment: Oh, I see. One more thing. Why we add dimension to the weight vector? I thought regularization is used to reduce the magnitude of weights and without regularization magnitude of weights increases, leads to overfitting, there is no relation to add dimension to weight vector.
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/116598/discussion-between-duy-phan-and-dale-c).


Segmentation fault when running PHP script from CLI, works fine as cron job

Question: I have a PHP script that reads a file via fgetcsv through PHP's zip:// wrapper. This works fine locally and when run on production via a cron job. It fails with a Segmentation fault when invoked on production via CLI though, apparently at the end of reading the file when fgetcsv returns false.
The relevant parts of the script:
#!/usr/local/bin/php5
<?php

...

$i = 0;
while (($row = fgetcsv($file)) !== false) {

    // processing ...

    if (++$i % 50000 == 0) {
        echo '.';
    }
}

printf("\nFinished reading %s records.\n\n", number_format($i));
fclose($file);

And its output:
-bash-3.00$ ./script.php
Reading file.zip................Segmentation fault

It appears to segfault before the printf but after having read all the records, so I suspect it fails when it reaches the end of the file.
-bash-3.00$ /usr/local/bin/php5 -v
PHP 5.2.8 (cli) (built: Apr 14 2010 16:08:06)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
    with the ionCube PHP Loader v3.1.31, Copyright (c) 2002-2007, by ionCube Ltd.

What could be the cause for this and is there a way to fix it?
Answer: strace the proc to figure out where it's dying. A segfault is by definition an unhandled error, so the logs are unlikely to tell you much interesting. Speculation: php compiled against a shared library that's not available on the host to which it has been deployed.
Alternatively, since you say you're running on shared hosting, why not get the vendor's support channel involved? They can do a lot more with root access to figure out what's going on, and it's the environment they're providing that's broken.
Comment: I'll see if I can achieve anything via support, but I'm skeptical. The weird thing and what's really bothering me is, *it works* via cron, but not via CLI on the same machine.
Comment: Does your cron job call php_cli, or does it make an HTTP request to invoke the PHP script? php and php_cli are different compiled binaries, and may exhibit different behaviours.
Comment: AFAIK the file is just executed directly, hence the need to put the shebang on the first line and make it executable. I'm doing basically the same in my CLI invocation. I only have a minimal web interface to configure cron jobs on this host, so I can't know for sure what exactly is done, but I can say for sure that there's no HTTP involved.
Comment: Cron may be running the script in a different environment (jailed, maybe?), or conceivably even on a different system across a shared filesystem. I definitely recommend stracing to find out what system call makes php dump core.
Comment: Would love to `strace` it, but `strace` is not available on that host. Very little is, in fact. -_-;; Interesting idea with running on a different system though.
Comment: Well, your next best bet is to analyze the core dump (assuming you have access to where it gets dumped); if nothing else, this will help you assess your level of dedication to determining the nature of the problem.
Comment: This helped me out, but I had to put a sleep at the top of my script to be able to strace it since it would die within a second.
Answer: You need to check your logs and have a look at where in the process it is erroring. You may need to enable logging in your php.ini file.
Likely you don't have proper permissions on the file. When you run it as cron you are likely running the script as root (assuming you have the cron in the system crontab).
So check your logs and check to make sure your file has the correct permissions to be run as the user you are logging in as through the CLI.
Comment: I don't think it's the fault of incorrect permissions or the script doing anything wrong. Seems to be something more low-level. I added the relevant piece of the script to the question for illustration. Also, unfortunately I don't have any PHP logs on this host. :(
Comment: This is running on a shared host, most certainly not as root. ;) Also, it's reading the file just fine; it's outputting a `.` every so many records read, and as you can see in the output, it's reading a lot of records. In fact, it appears that it's reading the whole file and segfaults at the *end* of this operation.
Comment: Does the user that you are running as have permission to write the zip file in the location that you are trying to save it in?
Comment: Also if you add a .htaccess file and then in there specify the php.ini file you want to use which has logging enabled you could enable logging to a location of your choosing. Most hosting allows this.
Comment: I'm not writing any file, just reading one. Also, this host is very restrictive, I don't think I can override this setting. Furthermore, it's not executed in a web server context, only as a cron job.
Comment: Right.. assumed web, sorry bout that.
Comment: No problem. PHP just has that image, doesn't it? ^_^;;
Answer: I had the same problem when using #!/usr/bin/php -q. I was creating a pdf using fpdf.org.  I was reading the whole file (with 39000 lines) with the
PHP file command and I was getting the same segfault. I replaced the file command with fopen and fgets reading line by line and the problem went away. I suspect that this segfault came from bash because when I was running manually php -q mpdf.php instead of ./mpdf with #!/usr/bin/php on the first line the problem did not occur. By the way I am running PHP 4.3.1 on SUSE linux 8.2. I have to stay there and not move to later releases for a number of reasons.
I hope that was of some help.
Regards from sunny Athens (in Greece)
Christos Michail


How to find all the possible combinations of a range in Python?

Question: This is more of a math question than Python, but if I have a range 0-100, how would I go about iterating through all the possible ranges in this? For example, the lower bound could be 0, and then the upper could be anything from 1-100 and so on.
Would something like this cover all possibilities?
lower = 0
upper = 100
for i in range(0,100):
    for z in range(lower,upper):
       print("Range is" + str(z) +":" + str(upper))

    lower = lower +1
    upper = upper -1

Comment: Does this answer your question? [How to get all possible combinations of a list's elements?](https://stackoverflow.com/questions/464864/how-to-get-all-possible-combinations-of-a-list-s-elements)
Answer:
Checkout itertools.combinations:

itertools.combinations(iterable, r)

Return r length subsequences of elements from the input iterable.
The combination tuples are emitted in lexicographic ordering according to the >order of the input iterable. So, if the input iterable is sorted, the >combination tuples will be produced in sorted order.
Elements are treated as unique based on their position, not on their value. So >if the input elements are unique, there will be no repeat values in each >combination.

import itertools
arr = list(itertools.combinations(range(1,100), 2))

Using For loops

For loops
arr = []
for i in range(1,101):
    for j in range(1,101):
        arr.append((i,j))
print(arr)

Answer: Your questions is essentially all combinations of 2 numbers from 0-100. So 100 choose 2 different combinations. Python has itertools.combinations for this:
for lower, upper in itertools.combinations(range(100), 2):
    # do something with lower and upper

Answer: If you want to implement something of your own (don't want to use itertools as mentioned in other answers), here's something that will work:
lower = 0
upper = 10

for range_size in range(1, upper-lower+1):
  for j in range(lower, upper):
    if j+range_size > upper:
      break
    print(f"Range: [{j}, {j+range_size}]")

The outer loop iterates all the possible size of ranges, which can be as small as 1 and as large as upper - lower. Note that the +1 is to make sure upper limit is also included in range.
The inner loop then starts from the lower bound and print all the ranges of size range_size.
Edit: If you want to get a sorted output like itertools.combinations, you can store the values in an array and sort that array.


Multiple IP address in a machine

Question: I am running an opennebula instance with opensuse as a guest machine. There is only one network interface eth0 with ipaddress 192.168.122.155 . However I can ping the machine 2 IP address 192.168.122.155 and 192.168.122.160. I dont want to assign 192.168.122.160 to the vm. How can I figure out what is going on?
Comment: If you didn't explicitly set .160 to the VM, it sounds like you have bridged your virtual network, and the VM has picked up a DHCP address.
Comment: can you provide ifconfig -a output from the VM?
Comment: Can you explain the setup you'd like to have?
Comment: Yes the virtual netowrk is bridged. How do I check if it has obtained the address from DHCP?
Comment: Thanks I checked the /var/log/messages and it seems vm obtained address from dhcp.  eth0: renewing lease of 192.168.122.160. But ifconfig still shows .155 then why am I able to ping .160?
Comment: It would seem that if the connection is bridged, and you have DHCP, the server has obtained .155, and the VM obtained .160. You can ping both addresses because there is one machine on each... if you don't want the VM to have an IP, configure it to **not** lease from DHCP.
Answer: 192.168.122/24 is a common subnet used for the VM Host's NAT'd network. Perhaps you're pinging one of the other guests?
Comment: Not really. I tried ssh-ing to the machine and it is the same one.
Comment: If you already know that, such information really ought to be included in the initial question.

Multiple IP address in a machine


Grammatically confused: $\omega=4dV$ for 3-form $\omega$ and volume in $\Bbb R^4$?

Question: Background: Against the advice I should have been given but wasn't, I'm taking a Lie theory course with no background in differential geometry. We finally made it into the part of the course where we need to know differential forms, and I'm not taking it very well.
I ran into a notational confusion on a homework problem. I'm also not really sure how to start it, but I'd like to think on that for myself a while.
Here is the full homework problem (it is exercise 4.10 from Kirillov's Introduction to Lie Groups and Lie Algebras.)
$$\DeclareMathOperator{\SU}{SU}
\DeclareMathOperator{\su}{\mathfrak{su}}
\DeclareMathOperator{\tr}{tr}
\newcommand{\homeo}{\cong}
\newcommand{\ss}{\subseteq}
$$

Let $G = \SU(2)$. Recall that we have a diffeomorphism $G\homeo S^3$ (see
  Example 2.5).

Let $\omega\in \Omega^3(G)$ be a left-invariant 3-form whose value at $1\in G$ is defined by $\omega(x_1, x_2, x_3) = \tr([x_1, x_2]x_3), x_i\in\mathfrak{g}$. [...] Show that $\omega = \pm 4dV$ where $dV$ is the volume form on $S^3$ induced by the standard metric in $\Bbb R^4$ [...]
Show that $\frac{1}{8\pi^2}\omega$ is a bi-invariant form on $G$ such that for appropriate choice of orientation on $G$, $$\frac{1}{8\pi^2}\int_G\omega=1.$$

The first question doesn't make sense to me; isn't $dV$ supposed to be a 4-form $dx^1\land dx^2\land dx^3\land dx^4$?
(This is probably a different question, but I'm having a hard time understanding what does the wedge product mean at the pointwise level? For instance, with the volume form: $x^i$ is the function that picks out a coordinate $x\mapsto x_i$. Okay, so take the derivative of that; this is supposed to basically be the vector $x_ie_i\in\Bbb R^4$ at all $p$, yes? Is it then reasonable to get $(dV)_p(x)$ by shoving vectors into a matrix and... taking the determinant? This "works", in that it gets the right answer and explains the antisymmetry, but I'm worried that this intuition breaks for more general top-dimensional differential forms.)
Answer: In $\Bbb{R}^4$, certainly, the volume form is $dV = dx^1 \wedge dx^2 \wedge dx^3 \wedge dx^4$.  However, confined to the sphere $S^3$ in $\Bbb{R}^4$, you need a $3$-form.  (It is perhaps easier to see this for the sphere $S^2$ in $\Bbb{R}^3$, where what is wanted is for areas, not volumes.)
Answer: Hint: the form $dV$ is defined on the Lie algebra $su(2)$ of $SU(2)$ you can represent the elements of $su(2)$ by complex $2\times 2$-matrices and use the trace and Lie bracket.
$su(2)=\pmatrix{ia & -c+id\cr c+id & -ia}a,b,c\in R.$ thus $su(2)$
$SU(2)=\pmatrix{a & -\bar b\cr b & \bar a}, a,b\in C \mid a\mid^2+\mid b\mid^2=1$ thus it is a subspace of $R^4$ to evaluate $dV$ on $u,v,w$ tangent to $x\in SU(2)$, it is $dv(x,u,v,w)$
Comment: Okay, so I just missed the intermediate step of "...induced by [the subspace metric on the sphere that is induced by] the standard metric...". I think I could now do the first part, at least, by just bashing coordinates together with Pauli matrices (I omitted a hint that I now understand to be suggesting this). Thank you :)
Comment: Hmm, I don't understand the newest addition: $u,v,w\in\mathfrak{su}(2)$, but $x\in\SU(2)$?
Comment: u,v,w are orthogonal to x. SU(2) is $S^3$, if $x$ in $S^3$, the tangent space $T_xS^3$ are elements orthogonal to $x$, to compute $dV$ you need 4 elements of $R^4$, x and three vectors tangent to x in fact x represents here the unit vector orthogonal to its tangent space. In the case of $S^n$, if $x\in S^n$ it is also orthogonal to its tangent space.
Comment: Okay, so it looks like we are abusing the fact that $\Bbb R^4$ is its own tangent space, then, so that we can identify both the elements in $T_xS^3$ and the basepoint in $S^3$ with the elements in $\Bbb R^4$.


install pygobject3 for python3 in windows?

Question: i need to write a python3 program for windows and i want to use gtk+3 for GUI
in GNU/Linux i use python3 and pygobject3 and now i cant find any binary version of pygobject3 for windows x86 or x86_64 !!!
only i find last windows version is 2.28 !!! in this link http://ftp.gnome.org/pub/GNOME/binaries/win32/pygobject/
where i can find pygobject3 for windows and how to install and use that
thanks for all,
Answer: It looks like someone made a win32 binary for pygobject3.03 here (download link on page).
Also from a blog comment here, it looks like the current maintainers aren't interested (or have time) to port to windows.


How to sight read rhythm (note durations) and maintain rhythmic accuracy?

Question: I understand the concept of rhythm and different note durations, I can play them well (when I pause and use the "1 e + a" system)
But, in many piano pieces there are different rhythms that are difficult to count while sight reading. I find it difficult to play different note durations that are combined with other note durations. For example:- A quarter note with two sixteenth notes next to it.
To be exact, right now I am just "guessing" the rhythms and note durations, I know I do have the option of listening to the music and then playing, but I would like to learn how to play with exact rhythmic accuracy and being sure of the rhythm"
My exact question is that "How to maintain accuracy in rhythm" , basically how do you perfect the note durations, if the answer is "Practice" then  please suggest me some things that you have practiced that have helped you.
How do I fine-tune my rhythmic execution to align to the beats with more precision?
Comment: Hehe, seems like your edits brought in *both* ideas—the idea of quickly *understanding* a rhythm, and the idea of playing it with precise timing even when you know what it should be. It seems your main focus is the first idea, so the duplicate of this question would be [Tools to improve sight-reading of rhythms](https://music.stackexchange.com/questions/30599/tools-to-improve-sight-reading-of-rhythms). for precision, see ["Is it possible to play an exact rhythm"]. ...
Comment: This post will probably get you some answers you're looking for in terms of general practice techniques, but I recommend opening two additional questions: 1) A question about how rhythm was taught before metronomes (and remove that part of the present post), and 2) A question about a specific rhythm you've run into trouble with, including a picture of the rhythm plus a couple measures on either side.
Comment: I also suggest you move mention of the history of the metronome into a separate question. The emphasis on a steady, regular, recurring beat existed for centuries before the metronome (Jean-Baptiste Lully died of a beat-marking accident), and so did the division of that beat into equal parts (thus rhythm). It's sight reading that was not heavily emphasized in earlier history.
Comment: Meanwhile, I'm not sure whether you're looking for help with *reading* rhythms—with being able to decipher in time, from the dots on the page, what to play—or for help with rhythmic *accuracy*. Your mention of metronome and making "note durations perfect" suggests that you might simply be looking for help fine-tuning your rhythmic execution to align to the beats with more precision. Please use the "edit" button to clarify some more.
Comment: For working on precision, see [Is it possible to play an exact rhythm?](https://music.stackexchange.com/questions/62544/is-it-possible-to-play-an-exact-rhythm). I have a few ideas myself that I'll add to those two questions.
Answer: The secret is pattern recognition. For most musical styles, there are at maximum few dozens rhythmic patterns. Once you are familiar with the style, you should be able to recognize these as well as common melody and chord patterns at a glance. When you're sight reading you should also be reading a bit ahead of what you are playing so you have some time for thinking if something unfamiliar comes along. So unfortunately it boils down to "practice".
Regarding Beethoven, Wikipedia mentions that "Ludwig van Beethoven was perhaps the first notable composer to indicate specific metronome markings in his music. This was done in 1815, with the corrected copy of the score of the Cantata op. 112 containing Beethoven's first metronome mark". Those without metronome practiced the same way as now: counting in their head, tapping a foot, counting with fingers or something similar. In classical music maintaining a strictly constant tempo is frowned upon anyway.
Comment: Huh, I thought the only places in classical music where maintaining a strictly constant tempo is frowned upon are in gradual tempo changes (e.g. ritardando, rallentando, accelerando) and in rubato.
Comment: We seem to have had different exam boards.
Comment: Yes, pattern recognition! Not any sort of instantaneous cerebral analysis. :)
Answer: Some general tips about sight reading:

Play slowly. How slowly? At a tempo that allows you to play accurately, or at least with minimal mistakes.
Choose music appropriate for your current level. You will need to invest some time into finding the right music. My suggestion is to start looking at imslp.org. If you are working in a "classical" style, IMO the simple exercise collections of Czerny are one of the best resources.
Concentrate on not stopping and not replaying when you make a mistake. Try to keep a steady tempo and keep going, and reapply point number one to make sure you aren't trying to play too fast.
While not specifically about rhythm, make sure your fingering technique is good. If you're sight reading, you want your eyes on the page, and if your eyes stay on the page, they aren't looking at your hands. If your fingering technique is good, you won't need to look at your hands constantly, and that will allow you to concentrate on the page of music... including the rhythms.
For piano music separate the hands. Sight read right hand only first, then left hand only, then try both together. Obviously this wouldn't be a sight reading test, but it's a way to build up your skill gradually.

Specifically about rhythm...

Spend some time with basic rhythm exercises away from actual sight reading. You can play little scale/chord patterns at the piano for those exercises, if you like, or you can simply pat your two hands on your lap.
Think about categories of beat subdivision: even subdivision, dotted rhythms, division by three (triplets), and syncopation. Each has a different "feel." Practice and count each type separately. And then as you get the feel of each try switching between them to "feel" the contrast of rhythms.
Count beats out loud as you play and practice. Do this with everything, sight readying, scale practice, etc.

I find it difficult to play different note durations...A quarter note with two sixteenth notes next to it...right now I am just "guessing" the rhythms...

That is a simple rhythm. If it's hard for you to play, that's fine, that's your current level. But I think it suggests you need to work on basic rhythm exercises away from sight reading. If you can't count mixed durations like that, how can you expect to sight read them? You don't need to stop working on sight reading, but put in extra time on rhythm exercises, and maybe lessen the time on sight reading until you sharpen your basic rhythm skills. There are many books devoted to the topic. Most of them take a progressive approach from easy to hard.
Comment: @AmanKaushik This hits the themes that I'm planning to add to the two duplicate questions: 1) "scaffold your learning"—that is, build bit by bit on what you already know—2) decontextualize (practice rhythms in ways that isolate and simplify them) and 3) contextualize (sometimes the challenge is in the "real-world" ways they're used, so don't forget to practice those too)
Answer: Sight reading with good rhythm is not easy because the mere fact that the music is unfamiliar tends to make you want to focus on small details and play slowly. If you do focus on details and play slowly, you are no longer sight reading; "focusing on small details and playing slowly" equals "learning the music". When you call it "sight reading", that term can only mean immediately performing the music rather than taking time to learn it - i.e. sight reading requires you to play at or near a suitable speed for a concert.
One very good way to make sure you are sight reading, and not simply learning new music, is to play along with someone who already plays or sings the music confidently. Perhaps the best of all is being part of a group that has a leader or conductor - they won't stop if you get lost, because it's boring and irritating to interrupt every time someone makes a mistake. Being forced to keep up, and not having a chance to stop or slow down, is IMO the real key to sight reading.
Comment: That's weird - I was taught (for Royal Conservatory of Music exam boards) to sight-read at *half tempo*.
Comment: Going at a slower speed is fine, if that's an option.
Comment: In Royal Conservatory exams (and other similar ones), the sight reading test is an artificial game. Following a good teacher's advice about how to win that game is a smart move, for the purpose of your exam. But real sight reading is quite a different thing from the exam game, and to improve your real sight reading, it really does help to be forced to keep up - even if you're being forced to keep up with only half speed. (If they're at half speed and you still can't keep up, it means choose easier music for now.)


Conditions for expressing matrices as polynomials

Question: Some matrices can be expressed as polynomials of each other.

For example inverses: if $AB=I$ then $A$ can be expressed as some polynomial
$p(B)$ and also it exists such polynomial $r(A)$  that $B=r(A)$.
(standard procedure of deriving such polynomials is based on Cayley-Hamilton
theorem).

Not always we have situation that if for example some matrix can be expressed as  others polynomial then the reverse statement is true.

For example  $A^2=I$ doesn't mean that it exists such polynomial
$p(I)$ that $p(I)=A$
(a polynomial of identity matrix must be a
scalar matrix but $A$ in the case $A^2=I$ doesn't necessarily be a
scalar matrix - the same can be said if instead of $I$ we would have on RHS above equation a scalar matrix).

From these considerations the question follows:

what are exactly conditions that allow for a pair of matrices $A,B$
to be expressed as polynomials of each other?

One could say that they must have exactly the same eigenvectors but I'm not sure whether it is equivalent to the condition mentioned above or that conditions can be expressed in other, more universal way..
Let entries of considered matrices be real.
Comment: Note: $A$ and $I$ have the same eigenvectors, but as you said, $A$ is not necessarily a polynomial in $I$
Comment: @Exodd The set of eigenvectors of $A$ is subset of eigenvectors of $I$ ( which are all possible vectors)
Comment: See https://math.stackexchange.com/questions/57308/commutation-when-minimal-and-characteristic-polynomial-agree
Comment: @lhf here the question is more general
Answer: A partial answer is the following:

If $A,B$ are diagonalizable matrices, and each eigenvalue of each matrix has multiplicity 1, then
  $$ A=p(B), \, B=q(A) \iff AB=BA $$
Comment: How do you know  $AB=BA $   -->  $ A=p(B), \, B=q(A)$? My counterexample with $I$ shows that there is no such implication for every pair of commuting matrices.
Comment: $AB=BA$ implies the matrices are symultaneously diagonalizable, so you can work with diagonal matrices. If all the entries of diagonal matrices are distinct, you can find an interpolation polynomial that bring them in any value you want
Comment: @Widawensen In fact in the hypothesis, every eigenvalue has multiplicity 1, while $I$ has eigenvalues with multiplicity $n$
Comment: I see, the answer is valid for matrices with n distinct eigenvalues. Probably we can say also in this case that the sets of eigenvectors for both matrices are the same (up to the scale).
Comment: Maybe commuting, diagonalizable matrices with distinct eigenvalues are the only possibility? What do you think ?
Comment: @Widawensen No, take $B=A$ for any $A$
Comment: Good example. More generally we could take $B=kA+sI$.


Searching whole word in vim - is there another way than using \<\>

Question: There are several answers on how to search a whole word in vim. For example, this link How to do whole-word search similar to "grep -w" in Vim answers it.
I am wondering is there any alternative to \<word\> in vim to search whole word? The reason I am asking is out of curiosity. I am wondering an alternative in vim. I know some other tools, such as grep can do the same task, but I am interested in finding way in vim.

What's wrong with \<\>?

I am asking alternative.
Comment: What's wrong with `\<` `\>`?
Comment: What do you mean by "not good"? What are your criteria for "good"? (We can't suggest good alternatives without knowing how to rate them.)
Comment: Sorry to miss that information. This is for interactive use.
Comment: @melpomene I think you are confusing. `pi` is defined as constant ratio, where as in computer software, there are, in general, multiple ways of achieving things. If there is no alternative, please put `no` in answer. I am surprised why there is no direct answer for this question.
Comment: I think the downvotes are because it is not clear what you are looking for. Is there another way to search for words? Yes, of course. For example, you can use `:!grep -w word`. But why? Just to be different? Or do you have some actual purpose?
Comment: What exactly do you mean by "alternative"? And why are you looking for one? What are the constraints you're operating under?
Comment: @melpomene, :alternative" literally means other way (to find whole word in `vim`. And why are you looking for one? Curiosity (which, I believe, led mankind to progress up to current level). Is there anything wrong to find alternative?
Comment: Is this for interactive use, or for syntax highlighting, or for a script, or for macros, or ...? Should it respect the `iskeyword` option (like `\<` `\>` does) or is that not important?
Comment: @iod93883 Your question is like "I'm looking for an alternative to the number `pi` (3.14159...). I'm asking because I personally did not find it good to use `pi`."
Comment: Are you looking for things like `*` and `#`?  (search forward/backward for the word under the cursor)
Comment: Not word under cursor. I am looking something like grep -w "word"
Answer: Technically yes, you can place your cursor over that word in the document and press * which will automatically populate the search with the word surrounded by word boundaries. Same with # for searching backwards.
Also, Vim's default search escaping is quite verbose and poorly designed. I highly recommend starting all searches with the "very magic" switch, \v, so that you don't have to escape special characters. As in you can type /\v<word> and the < and > will be treated correctly as word boundaries without having to escape them.
A mapping to "enable" very magic for all searches:
nnoremap / /\v
nnoremap ? ?\v

Comment: WOW!  I **never** new about `*` and `#` - very nifty!
Answer: See also answers here:

https://stackoverflow.com/a/44296906/584940
/\v<yourword>

https://stackoverflow.com/a/71241990/584940
map w /\v<><Left>

The second link is probably (I'm guessing) the most applicable to your question, especially if you put in in your .vimrc
Even if you don't, you only need to do the mapping once.  Of course, mapping w overrides its typical "next word" behavior.  This might be acceptable to you, since W (capital W) has a similar behavior, of "next WORD" (which is typically what I actually want to do).

ASIDE: Vim has between two use cases of "non-blank-characters."
From :help word in Vim (Emphasis mine on '**or**') :

These commands move over words or WORDS.

                                                       *word*
A word consists of a sequence of letters, digits and underscores, **or** a
sequence of other non-blank characters, separated with white space (spaces,
tabs, <EOL>).  This can be changed with the 'iskeyword' option.  An empty
line is also considered to be a word.
                                                       *WORD*
A WORD consists of a sequence of non-blank characters, separated with white
space.  An empty line is also considered to be a WORD.

What this effectively means is that:
a word is        /(w+|[\W]+/   ---   e.g., *foo* is 3 words total (*, foo, *)
But a WORD is /S+/                   ---   e.g., *foo* is 1 WORD total


Solving $x^2 + y^2 = 1 + z^4$ with (x,y,z) = 1 and z < x < y

Question: I have a computer programming problem where I need to find n many sets of integers that meet the condition $x^2 + y^2 = 1 + z^4$ with (x,y,z) = 1 and z < x < y
I can do this relatively easily with a brute force algorithm that increments z, then finds an x which has a GCD of 1 with z, then finds a y that has a GCD of 1 with z & x. Which then checks to see if the co-prime tripple of x, y & z satisfy the above equation.
However the program is unbearably slow making millions of function calls once I've discovered the first 5 - 10 sets.
How can I restate the problem to quickly find a candidate x & y given some z? Is there some other approach I could consider over this slow brute force method?
Comment: Possible hint: rewrite the equation to use difference of squares.
Comment: You can first solve the equation.  $x^2+y^2=z^2+1$ Then make choices when there are $z -$ square.  The formula there.  http://math.stackexchange.com/questions/351491/integral-solutions-of-hyperboloid-x2y2-z2-1/709219#709219  http://math.stackexchange.com/questions/74931/integral-solutions-of-x2y21-z2/789918#789918
Comment: @barrycarter So: $x^2 = 1 + (z^2)^2 - y^2$ ... or $x^2 = 1 + (z^2 + y)(z^2 - y)$ ... ? I appreciate the hint but I'm still lost here.
Comment: @individ Thanks for the links you provided. I read through both answers on those links but don't understand them. Unfortunately you may be overestimating my maths knowledge and experience. Reading through the wiki page on Pell's equation was no help either.
Comment: `x^2 - 1 = z^4 - y^2` is a difference of square on both sides. Not sure if this helps, just a random thought.
Comment: [All solutions below $10^3$](http://i.stack.imgur.com/ChmXj.png).
Comment: [All solutions below $10^4$](http://i.stack.imgur.com/MBL6x.png).
Answer: You don't have to worry about $(x,y,z)=1$  because it's automatic: if $x\equiv y\equiv z\equiv 0 \pmod{p}$ then your condition $x^2+y^2=z^4+1$ becomes $0\equiv1\pmod{p}$.
Edit: Modified code to cater for missed constraint $z<x$ as per @alex.jordan's comment.
This brute force python fragment produces about 380 solutions in a few seconds on my laptop:
<code>for z in range(1,300+1):
  target = z*z*z*z+1
  for x in range(z+1,int((target/2)**.5)+1):
    y = int((target-x*x)**.5)
    if x*x + y*y == target:
      print("Solution: z=",z,"x=",x,"y=",y)
</code>
(Here (blah)**.5 is python for $\sqrt{\text{blah}}$.)  And int(blah) is floor(blah).
Basically, for a given $z$ and viable $x$ values, you test the single plausible $y$ value of $\left\lfloor\sqrt{z^4-1-x^2}\right\rfloor$.
If you need to find even more solutions efficiently, I think you'll have to factor $z^4+1$ into primes of the form $4k+1$ (plus an optional factor of 2), decompose the primes into sums of squares, then recombine those solutions. It'd be faster, but take a lot more code and you'd still be limited by the size of the numbers you can factor which probably won't go much beyond the $300^4+1$ in the code fragment above.
Incidentally, there will always be at least one solution to $x^2+y^2=z^4+1$ for each $z$ since $z^4+1$ is a product of primes of the form $4k+1$, plus an optional factor of 2. For a prime $p$ of the form $4k+3$, $z^4+1\equiv0 \pmod{p}$ has no solutions  since $x^2\equiv-1\pmod{p}$ has no solutions. And $z^4+1\equiv0\pmod{4}$ has no solutions since $x^2\equiv3\pmod{4}$ has no solutions, so there is at most 1 factor of 2 in $z^4+1$. Any number can be written as the sum of two squares iff is it the product of powers of 2 and $4k+1$ type primes and even powers of $4k+3$ primes. The constraint $z<x$ doesn't always hold in the solutions, though (thanks to alex for remark in comment below).
Comment: Regarding the last paragraph, OP is requiring $z<x<y$, so there is not always such a solution for each $z$. For instance with $z=2$, one of $x,y$ must equal $\pm1$. Is the condition $z<x<y$ built in to your search algorithm?
Comment: @alex.jordan: You're absolutely correct. I'd missed that completely. Will update the code and comments!
Comment: $x$ can start at $z+1$ and need only go up to $\left\lfloor\sqrt{\frac{1+z^4}{2}}\right\rfloor$.
Comment: For the most part there will be quite a bit of flexibility in the presentation as a sum of two squares, at least if there enough prime factors. So I am not overly concerned about finding $x,y$ pairs that are both larger than $z$. I guess that prime factorization will prove to be a bottleneck when you go for larger choicec of $z$ though. +1 for the observations that 1) checking gcd's is a waste of effort,2) using prime factorizations
Comment: Great answer, thank you.
Answer: Write for the equation.
$$x^2+y^2=z^4+1$$
One simple formula.
$$x=(\frac{a^4+1}{2}t+2a)ta^2+1$$
$$y=\frac{a^8-1}{4}t^2+a(a^4-1)t+a^2$$
$$z=\frac{a^4+1}{2}t+a$$
If you use the solutions of the equation Pell. Where $t -$ ask yourself.
$$p^2-2(t^2+1)s^2=1$$
Make the change.  $$a=2t(p+ts)s$$
Then decisions can be recorded.
$$x=(2a+1)(t^2+1)-1$$
$$y=2a(a+1)(t^2+1)-2a-1$$
$$z=t(p^2+2tps+2(t^2+1)s^2)$$
Comment: Nice (FWIW I verified this)! Where does this come from? You do need either $a$ odd or $t$ even to get integers, and $a>1$ to get $x<y$.
Answer: Here is a quick and dirty method for generating infinitely many solutions. The idea is very crude in the sense that it only produces solutions, where $y=z^2-3$. Restricting ourselves in this way reduces the problem to a Pell equation, and we can solve those efficiently. My write-up does not assume any knowledge of Pell equations, but those familiar with their theory will be bored.
Given all this the algorithm below only finds a tiny fraction of all the solutions of your equation, but it does find this subset very quickly.

If plug in $y=z^2-3$ we get the following equation
$$
x^2-6z^2=-8.\qquad(*)
$$
So the question is how to find $(x,z)$ pairs satisfying this. To that end let me introduce the following functions:

The function $N:\Bbb{Z}^2\to\Bbb{Z}$ that takes two integers as inputs and spews out a single integer given by $$N(a,b)=a^2-6b^2$$
The function $M:(\Bbb{Z}^2)^2\to\Bbb{Z}^2$ that take two pairs of integers as inputs and outputs one pair of integers. The formula is
$$M((a,b);(c,d))=(ac+6bd,ad+bc).$$

Remark: The function $N$ gives the norm of the element $a+b\sqrt6$ in the ring $\Bbb{Z}[\sqrt6]$, and the function $M$ calculates the product of the numbers $(a+b\sqrt6)(c+d\sqrt6)$ in that ring.
Key Lemma. We have for all integers $a,b,c,d$ the identity
$$
N(M((a,b);(c,d)))=N(a,b) N(c,d).
$$
Proof. You do this. Or you can just take my word for it.
The Key Lemma allows us to find solutions to $(*)$, i.e. $N(x,z)=-8$, if we can locate one solution to it, and if we can also locate a solution to the equation $N(a,b)=1$. These are easy to find with a bit of searching among small integers. Namely
$$
N(4,2)=4^2-6\cdot2^2=-8
$$
and
$$
N(5,2)=5^2-6\cdot2^2=1.
$$
Here is then an algorithm for producing infinitely many solutions to $(*)$.
We first produce a sequence of pairs $(a_n,b_n)$, $n=1,2,\ldots$, such that
$N(a_n,b_n)=1$:

The starting point is $(a_1,b_1)=(5,2)$, i.e. the solution found above.
Given a solution $(a_k,b_k)$ we then find another solution $(a_{k+1},b_{k+1})$ with the recurrence formula
$$
(a_{k+1},b_{k+1})=M((a_k,b_k),(5,2))=(5a_k+12b_k,5b_k+2a_k).
$$

The point is that the Key Lemma implies that if $a^2-6b^2=1$ and $c^2-6d^2=1$, then also $(ac+6bd)^2-6(ad+bc)^2=1$.
On with producing solutions to $(*)$. We saw that $(x_0,z_0)=(4,2)$ is one solution. We can then use the pairs produced above to get others by setting
$$
(x_k,z_k)=M((x_0,z_0);(a_k,b_k))=(4a_k+12b_k,2a_k+4b_k)
$$
for $k=1,2,\ldots$. Observe that $(*)$ implies $x\approx z\sqrt6$, so the inequality $x_k>z_k$ is more or less given. Because $y_k=z_k^2-3$, the other requirement $y_k>x_k$ is also more or less clear, at least for all sufficiently large values of $k$. A quadratic polynomial grows faster than a linear one.

Here's how the list of solutions begins. I also present the calculations using $\Bbb{Z}[\sqrt6]$ arithmetic. The numbers will grow very quickly here. That is a price for it being deterministic and fast (no search is involved). You have to judge whether this is a problem.

The starting solution to $(*)$, namely $(x_0,z_0)=(4,2)$ must be discarded because here $y_0=z_0^2-3=1$ is too small.
Because
$$
(4+2\sqrt6)(5+2\sqrt6)=44+18\sqrt{6}
$$
we get as the next solution $(x_1,z_1)=(44,18)$. This gives $y_1=z_1^2-3=321$ and a valid solution $(x,y,z)=(44,321,18)$.
Because
$$
(44+18\sqrt6)(5+2\sqrt6)=436+178\sqrt6
$$
we get $(x_2,z_2)=(436,178)$. Here $y_2=178^2-3=31681$, and we have the solution $(x,y,z)=(436,31681,178)$.

Essentially we keep multiplying by $5+2\sqrt6$ to get to the next solution of this form.
Comment: We see that for all the solutions produced here both $x$ and $z$ will be even. As is evident from Frentos' answer this is not a problem, because $y$ will always be odd, and thus $\gcd(x,y,z)$ can be $1$ (and, in fact, is $=1$ as per Frentos' argument).
Comment: It would not surprise me, if there were another method for producing a lot of smaller solutions as well. If you can factor the numbers $z^4+1$ produced here, and there are several factors, you can probably find more $(x,y)$ pairs for the given $z$ using the idea described in the last paragraph of Frentos' answer.
Comment: Each iteration here multiplies $x$ and $z$ by roughly ten, and $y$ by a factor of roughly one hundred. Exponential growth cannot be avoided, when we reduce the problem to a specific Pell equation with a choice like $y=z^2-3$.


How can I add an existing field to a new content type?

Question: I have a content type with name category and have title and an image under it. I want a select option with this category name while for my new content type "videos". How can I do this?
Comment: Take a look at entityreference. With that you can reference entites to other entities. BTW: I wonder why you have chosen a nodetype "category" and not an taxonomy vocabulary.
Answer: You can find an option to select existing field under the list of fields :
Comment: Changing the widget will work, I think?


Arabic in PyroCMS

Question: When I save some arabic data using phpmyadmin, Its showing correctly in my CMS. But if I save some data using my CMS admin its showing something like below.
Ú©ÙÙدگا٠خدÙات اسÙاد بÙا Ø¯Ø§Ø±Ø ÙجرÛا٠سÛست٠اÙتÙا٠اسÙاد بÙا دار ٠ساÛØ±Ø§Ø´Ø®Ø§ØµÛ Ú©Ù Ø¯Ø§ÙغاÙستا٠باÙÚ© ÙÛتÙاÙد Ùطاب٠احکا٠ÙاÙÙ٠آÙÙا را Ùظارت ÙÙاÛØ.
I converted my data using utf8_encode() before saving. But still its giving same result. Any master configuration or encoding to set ?
Answer: Make sure

Page header  contains <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
db Collation is utf8_general_ci

Then use
mb_convert_encoding($str,'UTF-8');

PHP docs for mb_convert_encoding

Arabic in PyroCMS


jQuery UI images don't work unless leading slash is added (I'm using Symfony2)

Question: I have a jQuery UI dialog on a page of mine. The "X" in the upper right-hand corner is made possible by this little piece of CSS:
<code>.ui-widget-header .ui-icon {
  background-image: url(images/ui-icons_222222_256x240.png)/*{iconsHeader}*/;
}
</code>
The problem is that that doesn't work. It only works when I add a leading slash like this:
<code>.ui-widget-header .ui-icon {
  background-image: url(/images/ui-icons_222222_256x240.png)/*{iconsHeader}*/;
}
</code>
I know I could just make this change in the jQuery UI files, but that doesn't seem right. It seems hacky, plus, when I upgrade to a newer version of jQuery UI, all my changes will get blown away.
How can I make my jQuery UI images work? I don't know if the fact that I'm using Symfony2 matters here but I thought I'd mention it just in case.
Comment: Update: I've found a more specific issue that I think is causing the problem: http://stackoverflow.com/questions/10397450/symfony2-is-trying-to-feed-my-css-paths-through-routing-but-i-dont-want-it-t
Answer: Should work provided your files are in the right place.  This other SO Question has lots of references that illustrate how relative urls in stylesheets are relative to the stylesheet itself, not the containing document.
So as long as you aren't breaking up the jQuery UI download and moving the assets around, should work with a relative URL just fine.
Answer: I had the same problem.
The problem was the name to the CSS-file in my index-file.
First I had jquery-ui-1.10.1.custom.css. I changed it to jquery-ui-1.10.1.custom.min.css and then it worked.


How can I dump changes corresponding to list using ruamel.yaml?

Question: I am using solution in the related answer for How to auto-dump modified values in nested dictionaries using ruamel.yaml
 with RoundTripRepresenter.
If I make chnages on a list, ruamel.yaml is able to make changes on the local variable, but it does not dump/write the changes into the file. Would it be possible to achive it?
Example config file:
live:
- name: Ethereum
  networks:
  - chainid: 1
    explorer: https://api.etherscan.io/api

For example I changed name into  alper and tried to append new item into the list.
my code:
class YamlUpdate:
    def __init__(self):
        self.config_file = Path.home() / "alper.yaml"
        self.network_config = Yaml(self.config_file)
        self.change_item()

    def change_item(self):
        for network in self.network_config["live"]:
            if network["name"] == "Ethereum":
                network["name"] = "alper"
                print(network)
                network.append("new_item")

yy = YamlUpdate()
print(yy.config_file.read_text())

output is where name remains unchanged on the original file:
{'name': 'alper', 'networks': [{'chainid': 1, 'explorer': 'https://api.etherscan.io/api'}]}
live:
- name: Ethereum
  networks:
  - chainid: 1
    explorer: https://api.etherscan.io/api

Answer: I think you should look at making a class SubConfigList that behaves like a list but notifies its parent (in the datastructure), like in the other answer where SubConfig notifies its parent.
You'll also need to make sure to represent SubConfigList as a sequence into the YAML document.
If you ever going to have list at the root of your data structure, you'll need to have list-like alternative to the dict-like Config. (Or document for the consumers/users of your code that the root  always needs to be a mapping).
Comment: Would it be possible to make a hybrid class that can handle `list` and `dict`?
Comment: If I do your advice on your answer could `SubConfig` still used for `dict`?
Comment: Yes you can make a hybrid class, but IMO more difficult than necessary, I would not attempt it if I were you. I don't understand why you need to as about `Subconfig`, of course you still need that as intelligent `dict`, but you might need to make it `SubConfigList`  aware and vv.
Comment: I may have think more complex to solve this. I just wanted to inser an item in to a list using ruamel, actually it shouldn't be `dict-like`


lookup with 2 values to transpose rows to columns

Question: I have a table that looks like this:

I would like to make it so that each Name has its own distinct row, with the subjects changed to column headers and the grade being the value, like below

I'm currently using INDEX and MATCH to look up the grades, but I have to re-write the formula every time which will be time consuming considering my actual data is a few hundred rows
Here is my formula
=INDEX(C2:C8,MATCH(G2&H1,A2:A8&B2:B8,0))

Is there a way to do this so I don't have to re-write the formula for every cell?
Answer: Well, done the first for you, needed a helper column though, see:
=IFERROR(INDEX($C$2:$C$8,MATCH($F2&G$1,$D$2:$D$8,)),"")

Added iferror() to hide missing values.
Shown the formula for cell G2 which you can drag or use control enter to enter in all cells selected.
Answer: A non-array formula option and without helper column
Assume your "Source" table put in A1:C8
And, "Output" table put in G1:J4
In "Output" H2, formula copied across right to J2, and all copied down :
=SUMIFS($C:$C,$A:$A,$G2,$B:$B,H$1)

Then, select H2:J4 >> Format Cell >> click Accounting >> Decimal places, choose : 0 and Symbol, choose: None


Why does this pattern in powers happen?

Question: Hopefully this not to frivolous to ask, I have wondered it for years. There is a strange pattern in the differences between various power.
For example if you square the numbers from one to ten the difference between each square is the odds numbers. Similar patterns exist in the powers through 6 (and I assume all of them).
I attached an example of what I mean. Does anyone know why this occurs. For each higher power the pattern moves one to the "right"
Comment: $a^2-b^2=(a-b)(a+b)$
Comment: [What a coincidence.](https://www.youtube.com/watch?v=LkIK8f4yvOU)
Comment: 1) in any consecutive pair of integer $m, m+1$, one of them is even, the other is odd. 2) an even number raised to power is an  even number, an odd number raised to power is an odd number. 3) the difference of an odd and even number is odd.
Comment: By the way, your question is **not** frivolous! As shown in the above linked post, the mathematics underlying this leads not only to an explanation of the pattern your observed, but also to a very efficient algorithm to compute the closed form for any polynomial based on the pattern, which is more efficient than almost all other techniques.
Comment: I had this thought as a school student and thought it was a big deal.
Comment: @yunzen: I think it is good if you thought of it without being taught about it. Keep up that curiosity, and you'll eventually think of some things that nobody happened to think of. =)
Answer: This is a well-known result analogous to the formula $$ \frac{d^k}{dx^k} x^k = k!$$for differentiation of polynomials. I'll try to state it clearly:

Definition: Let the discrete derivative of a sequence $a_1, a_2, a_3, \ldots$ be the sequence $a'_n := a_{n+1} - a_n$.

Remark: The discrete derivative is a linear operator. That is, if $c_n = \alpha a_n + \beta b_n$ for some constants $\alpha$ and $\beta$, then $c'_n = \alpha a'_n + \beta b'_n$.
Notation: Iterated discrete derivatives can be notated by parenthesized superscripts; thus $c^{(2)}$ for $c''$, $c^{(3)}$ for $c'''$, and so on.

Theorem: For some $k \geq 1$ fixed, let $a_n = n^k$ be the sequence of $k$th powers. Then $a_n^{(k)} = k!$ for all $n$.

Remark: An immediate consequence of this statement, used in a proof by induction, is that $a_n^{(\ell)} = 0$ if $k < \ell$.
Proof. The result is obvious for $k = 1$. Now assume that the result holds for all integers less than $k$. Then $$a'_n = (n+1)^k - n^k = k n^{k-1} + \binom{k}{2} n^{k-2} + \binom{k}{3} n^{k-3} + \cdots + kn + 1.$$
We know that $a_n^{(k)}$ is the $k-1$th discrete derivative of the RHS. Because the discrete derivative is a linear operator, we can take the derivative of each term on the RHS by itself. We know by the induction hypothesis that the $k-1$th derivative of $n^{k-1}$ is $(k-1)!$ and that the $k-1$th derivative of every other term in the RHS is zero, and we're done.
Note that the $k$th discrete derivative is a constant $k!$ for any polynomial with leading term $n^k$, not just the $k$th powers, as terms of smaller order drop out.
Comment: Very nice answer. I'd just like to point out that $a_n^{(k)}$ is also known as the forward difference operator and, in my experience, is usually notated as $\Delta^k a_n$.
Answer: You are computing the finite differences of order $n-1$ of the $n^{th}$ powers of the integers.
Observe that by the binomial theorem, if we compute the first order difference of $m^n$ we have
$$(m+1)^n-m^n=nm^{n-1}+\frac{n(n-1)}2m^{n-2}+\cdots$$
Two important facts:

we started from a polynomial in $m$ of degree $n$, and obtained a polynomial of degree $n-1$;
the leading term has been multiplied by $n$.

If we iterate $n-1$ times, we obtain a polynomial of degree $1$ (linear) and the leading term has been multiplied by $n!$. Hence the general term is
$$n!m+c.$$
The $n^{th}$ column is the constant $n!$

The top values in every column have a closed form given here: https://oeis.org/A028246 (consider all subsequences starting with a $1$).
$$a(n,k) = \frac1k\sum_{i=0}^k (-1)^{k-i}\binom kii^n$$
Answer:
Square numbers n*n are just the area of a square with side n.  Notice how as you increase n (each side of the square) by one, you need an odd number of new squares.
These are the squares from $1^2$ to $5^2$  You can see that going from $4^2$ (orange) to $5^2$ (green) you need to add 9 squares.  The same as 25 - 16.
Answer: If we have a polynomial $p(x)$ of degree $n$ with coefficients $a_i\in\mathbb{R}$ then we can write this as
$$p(x)=a_0+a_1x+a_2x^2+\dots+a_nx^n$$
We can use the backward difference operator on this polynomial to get
$$\nabla p(x)=p(x)-p(x-1)=\sum_{k=0}^n(a_kx^k-a_k(x-1)^k)$$
The resulting polynomial is always of degree one less than the intial polynomial which can be proven by noting that
$$\begin{align}
\sum_{k=0}^n(a_kx^k-a_k(x-1)^k)
&=\sum_{k=0}^n(a_kx^k-a_k\sum_{j=0}^k\binom{k}{j}x^j(-1)^{k-j})\\
&=-a_k\sum_{j=0}^{k-1}\binom{k}{j}x^j(-1)^{k-j}+\sum_{k=0}^{n-1}(a_kx^k-a_k\sum_{j=0}^{k}\binom{k}{j}x^j(-1)^{k-j})\\
\end{align}$$
Hence this process can be repeated on the resulting polynomial which will once again reduce the degree by one. Eventually the polynomial degree is reduced to zero in which case each term is constant and equal to the difference between the terms in the previous polynomial.
In your case you have an initial polynomial of
$$p(x)=x^k$$
for some $k\in\mathbb{N}$ and hence we can see that
$$\nabla p(x)=x^k-(x-1)^k=x^k-\sum_{j=0}^k\binom{k}{j}x^j(-1)^{k-j}=kx^{k-1}-\frac{k(k-1)}2x^{k-2}+\dots$$
which means that this repeated process will both reduce the degree of the polynomial by one and multiply the highest power term of the polynomial by the highest power of the previous polynomial leaving a degree zero polynomial of $k(k-1)(k-2)\dots(1)=k!$ as a 'constant difference' between the corresponding degree one polynomial before it.
Answer: Another way to look at this is breaking down the operations:
For squares:

Going from 3x3 to 4x4, you need to add 3 and 4.
Going from 4x4 to 5x5, you need to add 4 and 5.
Going from 5x5 to 6x6, you need to add 5 and 6.

You always need to add an odd number and an even number, each one higher than the last time, to get to the next square.
For cubes, it is similar:

Going from 2x2x2 to 3x3x3, you need to add 2x2, 2x3, and 3x3: 4 + 6 + 9.
Going from 3x3x3 to 4x4x4, you need to add 3x3, 3x4, and 4x4: 9 + 12 + 16.
Going from 4x4x4 to 5x5x5, you need to add 4x4, 4x5, and 5x5: 16 + 20 + 25.

Always an odd number and two even numbers.
The 2nd iteration of cubes is:

Going from (4 + 6 + 9) to (9 + 12 + 16), you need to add (5 + 6 + 7).
Going from (9 + 12 + 16) to (16 + 20 + 25), you need to add (7 + 8 + 9).

Each time, you are adding the same three sides as the last 'layer' you added, plus the extra 'sides' (note these are

(5 + 6 + 7) is  (2+3) + (3+3) + (3+4)
(7 + 8 + 9) is  (3+4) + (4+4) + (4+5)

The extra 'sides' you add to the cube are each one bigger than the last time.

Why does this pattern in powers happen?


How do I make the staff for dynamics affect the MIDI output?

Question: I've written a lovely[citation needed] piece of piano music using Denemo (a graphical front-end to Lilypond), consisting of a piano staff with a staff for dynamics inserted in between them.
The dynamics appear in the PDF output, but do not appear in the MIDI output. If I apply the dynamics separately to each piano staff, the dynamics appear in the MIDI output but are doubled in the PDF output.
I would like to keep the dynamics appearing once in the PDF output (using the staff for dynamics) but still get a reasonably representative MIDI output. How can I accomplish this?
Answer: I make separate score-blocks for the PDF and the MIDI. Something like the following. Note that, for the PDF, the dynamics in \pianoCentredDynamics are separate from the Staffs containing the notes in \rhMusic and \lhMusic. By contrast, for the MIDI for the RH, the dynamics and \rhMusic are put in parallel and all assigned to the Staff -- and similarly for the LH.
\version "2.18.2"
\include "english.ly"

\header {
} % header

\paper
{
    #(set-paper-size "a4")
}

global =
{
    \key bf \major
    \tempo 4=96
    \time 3/4
}

rhMusic =
{
    d'2. ef'4 f'g'a'bf'c''d''2.
}

pianoCentredDynamics =
{
    s2.\pp s\< s s\ff
}

lhMusic =
{
    \clef bass
    bf,2. c4 d ef f g a bf2.
}

pedalling =
{
}

% Score-block for the layout
\score
{
    \new GrandStaff
    <<
        \new Staff = "rh" << \global \rhMusic >>
        \new Dynamics = "dynamics" \pianoCentredDynamics
        \new Staff = "lh" << \global \lhMusic >>
        \new Dynamics = "pedal" \pedalling
    >>
    \layout
    {
    }
}

% Score-block for the MIDI
\score
{
    \unfoldRepeats %\articulate
        \new PianoStaff
        <<
            % See http://lilypond.1069038.n5.nabble.com/Sustain-in-midi-output-of-Piano-Centered-Dynamics-Template-td31012.html
            \new Dynamics = "dynamics" \pianoCentredDynamics
            \new Dynamics = "pedal" \pedalling
            \new Staff = "rh" << \global \rhMusic \pianoCentredDynamics \pedalling >>
            \new Staff = "lh" << \global \lhMusic \pianoCentredDynamics \pedalling >>
        >>

    \midi
    {
        % See http://lilypond.1069038.n5.nabble.com/Sustain-in-midi-output-of-Piano-Centered-Dynamics-Template-td31012.html
        \context {
            \type "Performer_group"
            \name Dynamics
            \consists "Piano_pedal_performer"
        }
        \context {
            \PianoStaff
            \accepts Dynamics
        }

        \context
        {
            \Staff
            \consists "Dynamic_performer"
        }
    }
} % score for MIDI


Do you need a CAL if I'm only logging in as a local administrator on Windows Server 2008 R2

Question:
Possible Duplicate:
Can you help me with my software licensing issue?

Here the scenario:  I'm helping a really small business (less than 10 employees, no real administrators) migrate over an older sql server installation to a new box with new hardware.
We would like the new box to run on Windows Server 2008 R2, either Foundation or Standard (whichever is most cost-effective).  I know that for the Sql Server install we could either buy a per processor license or just use Sql Server Express.  The box would only have Sql Server running on it and maybe one other non microsoft service with non microsoft login.
There are a few services that connect to the Sql Server instance from other boxes (web server, other services, employees directory connecting using sql server manager).  All of the services connect using sql server accounts, not windows accounts.  In fact there are not any "domain" accounts at all.
Since the company doesn't have any real administrators the only way any person would login to the Server box is either directly onto the box as a local administrator or within the companies own internal network using RDP.
I'm trying to determine how many CALs (if any) we need to purchase.
Comment: sorry this is one of those licensing questions best answered by the vendor
Answer: To clarify, I assume your question includes Windows Server AND Windows SQL Server licensing.
For Windows Server Licensing, you need to have license for Windows Server OS and CAL for device/user connecting.Whether you choose to select per device or per user licenses depends on your environment.
For SQL 2008, similarly, you need to have server and CAL licenses.However, you have a choise of buying expensive per processor licence that covers unlimited amount of CALs, or you can buy reasonably cheap base license and add as many client CALs for users/devices as you need.Normally per processor license is aquired for the envirnment exposed to the internet where amount of users is unknown.
As for SQL Express, I suggest you check it's limitations (i.e. database size) to see if they could be limiting your application growth.