What approach for Commerce products, attributes and product displays?

Question: I am building a site, based on Commerce Kickstart. At this moment the site will not have a store, but it's on the horizon and I am trying to prepare the site beforehand for a store launch with as little developer hassle as possible.
My concern is now to wisely design the products and their attributes.
First off, I have two taxonomies. One is obviously a Product tree. A Commerce Product will have only one term reference value possible.
Then I have a Usage tree (or call it Applications), which describes how and where a product can be used. A Product Display will have multiple possible term references from that tree.
Now let's say, I have a brand of nails in different sizes: 1,2,3,4. The sizes 1 thru 3 would be tagged as "Home Use" and the size #4 - only as "Construction Use". All sizes would share some fields, such as main description, PDF attachments, images. And these could be fields on the Product level.
For sizes 1 thru 3, I would basically need one product display, speaking in terms of the page contents. And the Add to Cart button would have a list of models next to it: 1 thru 3. I would expect the button to add to cart SKUs mapped for each size.
For model #4 I would like to have perhaps additional set of images and a slightly different description, and the Add to Cart button would not need any select.
This is one possible approach. Another that comes to mind would be to treat all sizes as separate 4 product displays. Then, the Add to Cart buttons would have no attribute selector. This scenario could seem quite simple and obvious - a typical one-to-one relationship. However I am allergic to solutions that are not scalable, I hate cloning data as a slightest text correction would later require N edits on each product display.
So, which way would you go based on your experience? I don't know Commerce well enough but still want to be prepared for it beforehand.
I assume that SKU is something crucial for a successful and non-ambiguous order. But maybe my assumption is wrong.
I'm open for your insights.
Answer: When thinking about how to handle products and product displays, I prefer to model it after how Commerce Kickstart handles it's data organization:
Product displays are used to organize and present products to the customer and product entities are used to store product-specific information.

First off, I have two taxonomies. One is obviously a Product tree. A Commerce Product will have only one term reference value possible. Then I have a Usage tree (or call it Applications), which describes how and where a product can be used. A Product Display will have multiple possible term references from that tree.

Product catalog taxonomies influence how products are organized and subsequently found on your site. This would mean this information would go into the Product Display.

Now let's say, I have a brand of nails in different sizes: 1,2,3,4. The sizes 1 thru 3 would be tagged as "Home Use" and the size #4 - only as "Construction Use". All sizes would share some fields, such as main description, PDF attachments, images. And these could be fields on the Product level.

The "Use" field is a little tougher but I think you're on the right track. Anything that is specific to the individual SKU (product images, PDFs, diagrams, descriptions, sizes, etc.) would be on the product entity. You can still provide filtering using these attributes via Faceted search, but you end up not duplicating information across multiple entities.
I'm not sure how big your product catalog is, but there are some modules you can explore to ease your data management pain:

Inline Entity Forms: With IEF, you can create and manage product SKUs within your Product Displays. It makes 1:1 product relationships much easier to manage. It can also do 1:N relationships, but sharing those across multiple products can be painful with this.
The opposite of this is Commerce Auto Display, which allows you to create a Product display from within the Product entity form.

If you want to go down this road, IEF is preferred, since it tends to want you to keep only the SKU, price, and other bare minimum fields on the product entity.


Is there a difference between degrees of freedom and independent variables?

Question: They seem to represent the same idea as far as I can tell - can they be used interchangeably?
Comment: Beware of different uses of the same word or phrase in different disciplines. In a physical system "degrees of freedom" is used in the same sense that "number of identifiable parameters" would be used in a statistical model. (Technically, it's the dimension of a real manifold whose points correspond to "states" of a system under investigation.) Although the concepts are related, they are not exactly the same. I suspect the thread at http://stats.stackexchange.com/questions/16921/how-to-understand-degrees-of-freedom may answer this question.  Does that help?
Comment: I don't understand in what sense you can think they are interchangeable
Comment: Please explain yourself better
Comment: Wikipedia [makes a distinction](http://en.wikipedia.org/wiki/Degrees_of_freedom), though I can't say the article on [degrees of freedom in statistics](http://en.wikipedia.org/wiki/Degrees_of_freedom_%28statistics%29) is particularly clear. One paper I did find very useful in clarifying my own notions in some of the more complex contexts was Ye's 1998 paper (which is in the references at the latter article). See also the discussions of df under regularization or smoothing in Hastie et al (Elements of Statistical Learning 2ed, which is - legitimately - available for download at the book site).
Comment: Sorry for the lack of clarification; the Wikipedia page for degrees of freedom (physics & chemistry) says "In physics, a degree of freedom is an independent physical parameter in the formal description of the state of a physical system." So if you have a single monatomic gas molecule moving in 3D space, wouldn't we say the d.o.f. = # independent variables = 6 (x, y, z coordinates and velocities)? This leads me to believe they are describing the same thing - I would like to know whether this is generally the case, and if not, what an exception would be.
Comment: Thanks for your help, although I'm still not able to see in what case they would be different. Can you give a concrete example?
Comment: [My answer](http://stats.stackexchange.com/a/17148/919) in the thread I referenced gives a simple, concrete example where the two senses of "degrees of freedom" are distinctly different.
Answer: In discipline of Statistics, the terms "Independent Variables" and "Degrees of Freedom" are two distinct concepts.
Independent variables generally refers to a set of variables (traits) that are independent from one another. For example, if we take a random sample of people and record their age, we do not expect that the age of one individual influences the age of another individual.
Per wikapedia degrees of freedom refers to "the number of values in the final calculation of a statistic that are free to vary." For example, in a situation where we are trying to calculate the variation of the age of our sample, this would be $(n-1)$.
This may not be the case in another discipline, but you would need to ask someone of that field to know for sure.


Where is the documentation for the ACL module/api?

Question: Has anyone successfully created a client module that controls node access via the ACL module? I installed this module and created my own UIs to populate the three tables: acl, acl_node and acl_user. I started to create nodes, which populated acl_node with appropriate values, but nothing else seemed to happen. No new realm in the node-access table, nothing. Have I missed a step? I can't find any documentation on this module/api. Thanks.
The notes in the nodes_access_example module (part of the examples project) encourage you to 'read up' on the ACL module. I can't find anything to read though.
Answer: If you take a look at the ACL project page, you'll see that the following four client modules exist:

Content Access
Flexi Access
Forum Access
Image Gallery Access

You'll find plenty of examples on how to use the API in the source code of those.
The documentation for ACL are the comments embedded inside acl.module.  They are quite helpful when you try to create a client module.
P.S.: I am the maintainer of Flexi Access.  I can verify that ACL works as advertised.
Comment: Thanks. This is helpful. I didn't think about looking in another module that successfully uses this api. In the end I built my own thing with hook_node_grants and hook_node_access_records - perhaps a better way to go for a node-access table first-timer - but I may come back to this api in the future and have another go.


Display formresults in same block as form

Question: I have created a module that writes a block with a very simple form in it.
The purpose of it is calculating distance between 2 adresses, one fixed, one asked by the form.
Now I can't get the results to show in the block itself, and I can't find any tutorials on how to do that.
The block shows, the form is in it also, but when i submit a postalcode it seems that nothing happens (also the postcode input field resets to emtpy).
the code that retrieves the distance works also (tested on commandline).
How can I approach this?
The code for my block:
  function reiskosten_block_view($delta = '') {
  $block = array();

  switch ($delta) {
  case 'reiskostenblock':
  $block['subject'] = t('Reiskosten berekenen');
  $block['content'] = drupal_get_form('reiskosten_form');
  break;
  }

The code for submitting the form:
function reiskosten_form_submit($form, &$form_state) {

/* REPLACE SPACE OR HTTP-REQUEST WILL BE MALFORMED */
  $postcode = str_replace(' ','',$form_state['values']['postcode']);

  $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=1024AA|Netherlands&destinations=$postcode|Netherlands&mode=car&language=nl-NL&sensor=false&key=myKey";

$data = drupal_http_request($url);
$result = drupal_json_decode($data->data);

$destination = $result['destination_addresses'][0];
$distance = $result['rows'][0]['elements'][0]['distance']['text'];
$duraton = $result['rows'][0]['elements'][0]['duration']['text'];

  drupal_set_message("$distance");
  $form_state['rebuild'] = TRUE;
}

I want the output of $result to be shown below the submitbutton. It is driving me crazy this one, been puzzling for 5 hours now on this thing (what should be relatively simple imho).
edit:
I also validate on postalcode with:
function reiskosten_form_validate($form, &$form_state) {
  if ( empty($form_state['values']['postcode']) ) {
    form_set_error('postcode', t('Postcode mag niet leeg zijn'));
  }
}

It doesn't throw an error so it seems to me that 'postcode' has a value and the function should be processed.
Answer: Solved it. Solution:
in function reiskosten_form_submit($form, &$form_state) {
$result = drupal_json_decode($data->data);
$form_state['results'] = $result;

in function reiskosten_form($form, &$form_state) {
$form['destination'] = array(
  '#markup' => '
' . $form_state['results']['destination_addresses'][0] . '
',
);

Wrap t in an if to see if the values are there and the magic happens. Pfff


Qual a diferença entre Compile e Implementation no arquivo build.gradle do Android Studio?

Question: Sempre que vou adicionar alguma biblioteca manualmente no arquivo build.gradle (Module: app) do Android Studio 3, eu utilizo o formato implementation, porque essa é a forma que o próprio Android Studio utiliza por padrão, mas sempre que pesquiso na internet qual biblioteca é necessária para um recurso especifico, encontro exemplos utilizando o formato compile.
Em todos os casos eu sempre substitui compile por implementation e nunca deu problema, mas estou curioso pra saber qual a diferença entre os dois formatos de adicionar bibliotecas ao projeto.
dependencies {
    //...
    implementation 'com.google.android.gms:play-services:11.8.0'
    //...
}

dependencies {
    //...
    compile 'com.google.android.gms:play-services:11.8.0'
    //...
}

Comment: Até onde eu sei, implementation passou a ser suportado na versão mais nova do gradle
Answer: A configuração compile está obsoleta
É uma das grandes mudanças vindas do gradle:3.0, que a google anunciou em 2017 na google I/O
A configuração compile está agora depreciada e você deve substituir por implementation ou api. Onde a configuração api deve ser usada para declarar dependências na qual serão exportadas pela biblioteca da API, enquanto que implementation deve ser usada para declarar dependências na qual são de uso interno do componente. Exemplo:
dependencies {
   api 'commons-httpclient:commons-httpclient:3.1'
   implementation 'org.apache.commons:commons-lang3:3.5'
}

As dependências que aparecem nas configuração de api serão expostas transitivamente aos consumidores da biblioteca e, como tal, aparecerão no caminho de compilação dos consumidores.
As dependências encontradas na configuração de implementation, por outro lado, não serão expostas aos consumidores e, portanto, não serão vazadas no classpath de compilação dos consumidores. Isso permite uma compilação mais rápida graças ao tamanho reduzido do classpath.
Se você tiver um conhecimento de inglês, aqui esta a documentação.
Substitua no seu código:

compile por implementation
testCompile por testImplementation
debugCompile por debugImplementation
androidTestCompile por androidTestImplementation

Este artigo fala um pouco dessa e de outras mudanças que ocorreram na nova versão. Além de mostrar como migrar de uma versão mais antiga do gradle para a mais atual.

A configuração Compile ainda existe, mas não deve ser utilizada por não oferecer as garantias que as configurações api e implementation fornecem.
Answer: Encontrei a resposta no Stackoverflow em inglês, segue a tradução:
É uma das mudanças de ruptura que vem com gradle: 3.0 que o Google anunciou no IO17 gradle: 3.0
A compile configuração está agora obsoleta e deve ser substituída por implementation ou api
Dos documentos do gradle :

dependencies {
    api 'commons-httpclient:commons-httpclient:3.1'
    implementation 'org.apache.commons:commons-lang3:3.5'
}

As dependências que aparecem nas configurações api serão expostas
  transitivamente aos consumidores da biblioteca e, como tal, aparecerão
  no classpath de compilação dos consumidores.
As dependências encontradas na configuração implementation, por
  outro lado, não serão expostas aos consumidores e, portanto, não serão
  vazadas no classpath de compilação dos consumidores. Isso vem com
  vários benefícios:
Dependências não vazam no ranking classpath de consumidores mais,
  então você nunca irá depender acidentalmente de uma dependência
  transitiva compilação mais rápida graças ao tamanho reduzido do
  classpath menos recompilações quando as dependências de implementação
  mudam: os consumidores não precisariam ser recompilados publicação
  mais limpa: quando usado em conjunto com o novo plugin maven-publish,
  as bibliotecas Java produzem arquivos POM que distinguem exatamente o
  que é necessário para compilar contra a biblioteca e o que é
  necessário para usar a biblioteca no tempo de execução (em outras
  palavras, não Misture o que é necessário para compilar a própria
  biblioteca e o que é necessário para compilar contra a biblioteca).
A configuração de compilação ainda existe, mas não deve ser usada, pois não oferecerá as garantias oferecidas pela api e
  implementation.

Basta substituir:

compile por implementation
testCompile por testImplementation
debugCompile por debugImplementation
androidTestCompile por androidTestImplementation

Esclarecendo: O consumidor é o módulo que usa a biblioteca. No caso do Android, é o aplicativo Android.
implementation x api:  Se seu aplicativo depende da biblioteca x, que depende de y, z. Se você usar implementation apenas x api será exposto, mas se você usar api y, z também será exposto.
Fonte: What's the difference between implementation and compile in gradle
Ask


19" Rackmountable fast storage arrays

Question: I'm in need of some fast (15+ GB/s write) and somewhat large (50+ TB) storage subsystem with a standard storage interface.  I'd prefer something other than Fibre Channel, which admittedly appears to be the most standard interface.  RAID 5 is sufficient.
What should I be looking for when purchasing a storage system of this size? How does one build a system that can handle this sort of throughput.
We'd prefer it to fit into less than 20U and cost under a million US dollars.
Comment: Deleting question as per owner's request as it contained priveliged information
Comment: +1 for having a realistic budget.
Comment: RAID 5 for a large array will take forever to rebuild.  RAID6 is the way forward.
Answer: So you want 15GB/s of throughput, and 50TB of capacity, and you want to do it cheaply? Well, I guess that depends on your definition of cheap.
Have a look at this Cost Per Petabyte (about 1/3 of the way down the page). Now, that's of raw SATA storage. SATA is not fast. It's certainly not 15Gb/sec fast. So feel free to, say, double or triple those costs (before you get excited about backblaze, the IO on those boxes is going to be rubbish. They stream their data out over the internet and probably don't even need to saturate a 1Gb/link for each of their arrays).
All disk arrays of this calibre are 19" rack mountable. All of them. And what is "small"? 3U? 30U?
Your other challenge is getting 15GB/sec of throughput. 15GB/sec is 120Gb/sec, and given that the fastest interface I've seen on a single disk array is 10Gb/sec, you're going to need a minimum of 12 arrays to get that kind of throughput. That's assuming that a single array can saturate a 10Gbps link. At 3U per array you're already up to 36U, or almost a full rack.
This is totally unofficial, but based around some pricing that we recently received:
12x 16 Disk storage array (10Gbe, 16x600Gb 15k SAS drives configured in RAID-10) will roughly get you what you need. That's accounting for redundancy and whatnot. They're 3U and let's say $50,000 each. That's $600,000 and 36U of space. Plus the costs of the rest of the 10Gb infrasturcture, expect to be spending closer to $700,000.
Comment: @Erude - the big ones are Dell Equallogic and HP Lefthand, there's also EMC and a multitude of them. For a system of this size, I would budget for at least 80 hours of full-time research, just sitting at the vendor websites, plotting all their details, and then spending a very long time on the phone to each vendor and sitting through fun tech meetings, followed by shoot-me-in-the-face management meetings.
Comment: Thanks for the response, I updated my question.  Where did you see the fastest interface on an array at 10 Gb/s? An array is free to have more than one interface card or connection.  e.g. http://www.ramsan.com/products/3
Comment: @Eruditass - the arrays we've looked at generally have 2x or 4x 10Gbps connections, but you have to plan as if only 50% of them are working, in case you lose some of your infrastructure and your MPIO routes all fall back to sharing the same interfaces. Having 40Gb/sec is great (although you can't share all 40Gb to the one destination, but that's another topic), but the day you lose one of your SAN switches and you're trying to squeeze 40Gb of traffic through a 20Gb connection is the day you wish you had more arrays.
Comment: @Erud - You can also make the arrays much, much cheaper by using RAID6 (or RAID5) and increasing the usable capacity vastly, but then you're potentially sacrificing speed and availability. But if "cheap" is < $1M, then you'll have absolutally no trouble filling that solution.
Comment: great points.  I'm new to the storage arena so all this is very helpful in deciding what to ultimately go with.  Do you recommend any specific vendors that would be good to look at for building me such a solution?
Answer: Consult with a reseller who has solution architects on site and who will come up with a solution for you. If you've never done this before, you want to engage experts.
A good VAR will save you money and make sure your solution fits - they want you to come back again.
Disk throughput is only one component of getting such high throughput to the servers that require it.
Answer: Check out
Isilon unified scale-out storage.
Comment: Definitely fast, also very configurable, secure and robust, but not necessarily cheap.
Answer: You may want to check this out http://www.nexsan.com/sasbeast.php. It is high density solution so you will definitely fit into rack space. It should reach your performance goals too, I believe they support up to 2 dual-port FC cards per enclosure. Pricing is competitive too.

19" Rackmountable fast storage arrays


Exercises for quickly identifying intervals on the staff

Question: In order to improve my piano sight-reading, I want to practice recognizing intervals on the staff faster. If possible I'd like to combine the interval-recognition with a training for my finger memory, i.e. combine recognizing intervals and playing them.
Is there a good exercise for doing this? Aside from, well, taking a score and identifying the intervals? I thought about preparing a practice score with only two intervals at a time, going up and done randomly. But perhaps there is a better way of doing this.
Comment: Sounds like a mobile app (in waiting?) to me!
Comment: I would recommend http://www.musictheory.net - they have great customisable exercises for lots of this kind of thing.
Answer: Flash cards can be an effective method.  You can make up a few dozen cards with different intervals, with multiples of each interval starting on different notes and so on.  On the back you can have the name of the interval or something like that.  Then you'd play/name the top card, flip it and confirm, play/name the next card, flip it and confirm, and so on.  Then shuffle them up again and switch your playing and flipping hands.
There are some examples here.
Comment: @FlorianBrucker It's true that flash cards remove you from the context of real sight reading. But as a targeted exercise for increasing the speed at which you can recognize intervals, they are effective.
Comment: I guess that would work fine for identifying the intervals. However, it takes them out of the "flow" of a piece. My problem is rather this: Given a spot in a piece with the fingers on the corresponding keys, recognize the following interval and play the right key. Hence I'd prefer to train several intervals one after the other. But I guess I simply could put more than one interval on each flash card.
Answer: I've tested many different things in years, from online flashcards to books to some sort of software and so one. By far one of the best options is Notable by The Noteable Software Company.
It analyses your reaction time and gives you the charts and statistics of your real ability of recognizing the notes, as well as a road map to the progress. It has some unique features for defining your own tests and drills. One of the great things here is, It constantly re-exam you with your mistakes to improve your ability.
Although I own an old version of it [version 5.2], but in their website they offer few options ranging from free-to-try to professional edition.
Comment: Looks interesting. Only available for Windows, though, which won't work for me.
Comment: Unfortunately, this does not appear to work on Windows 10 (an error message pops up when trying to launch the program). On the other hand, it doesn't claim that either (it says Windows 95 / 98 / 2000 / NT / XP / VISTA / 7).
Answer: If you're running Windows then I would suggest RandomABC (SightRead.zip) which generates random ABCNotation tunes within a a given range.  Then you could just print out random scores to test your sightreading.  You could also manipulate the ABC files for any interval range you were interested.
So spend a couple of hours generating the tunes you want, print them, shuffle them up and there's your practice sheets.


Como puxar os emails do meu servidor com PHP

Question: Gostaria de saber como posso fazer para puxar os meus emails do meu servidor POP3 para uma página PHP, ou seja, puxar e exibir todos os emails, como o outlook faz, só que gostaria de saber como fazer isso com PHP. Procurei muito sobre isso e não encontrei. Eu irei puxar de um domínio meu.
Comment: Imap seria melhor. Não tem esta possibilidade ?
Comment: Na resposta do Marcelo Bezerra tem algo bem simples. Mas para criar um cliente de email é bem complexo. Você pode querer usar algo já pronto como  o roundcube: http://roundcube.net/.
Answer: Você pode usar as funções de Imap
Exemplo: http://garrettstjohn.com/entry/reading-emails-with-php/
<code><?php
class Email_reader {
    // imap server connection
    public $conn;
    // inbox storage and inbox message count

    private $inbox;
    private $msg_cnt;

    // email login credentials
    private $server = 'yourserver.com';
    private $user   = 'firstname.lastname@example.com';
    private $pass   = 'yourpassword';

    private $port   = 143; // adjust according to server settings

    // connect to the server and get the inbox emails
    function __construct() {
        $this->connect();
        $this->inbox();
    }

    // close the server connection
    function close() {
        $this->inbox = array();
        $this->msg_cnt = 0;

        imap_close($this->conn);
    }

    // open the server connection
    // the imap_open function parameters will need to be changed for the particular server
    // these are laid out to connect to a Dreamhost IMAP server

    function connect() {
        $this->conn = imap_open('{'.$this->server.'/notls}', $this->user, $this->pass);
    }

    // move the message to a new folder
    function move($msg_index, $folder='INBOX.Processed') {

        // move on server
        imap_mail_move($this->conn, $msg_index, $folder);
        imap_expunge($this->conn);

        // re-read the inbox
        $this->inbox();
    }

    // get a specific message (1 = first email, 2 = second email, etc.)
    function get($msg_index=NULL) {
        if (count($this->inbox) <= 0) {
            return array();
        }
        elseif ( ! is_null($msg_index) && isset($this->inbox[$msg_index])) {
            return $this->inbox[$msg_index];
        }
        return $this->inbox[0];
    }

    // read the inbox
    function inbox() {
        $this->msg_cnt = imap_num_msg($this->conn);
        $in = array();

        for($i = 1; $i <= $this->msg_cnt; $i++) {
            $in[] = array(
                'index'     => $i,
                'header'    => imap_headerinfo($this->conn, $i),
                'body'      => imap_body($this->conn, $i),
                'structure' => imap_fetchstructure($this->conn, $i)
            );
        }

        $this->inbox = $in;
    }
}

?>
</code>


What's the probability of picking up a certain board game tile?

Question: In a game like Carcassonne, sometimes you need a specific type of tile to score points.

All tiles are face down so everyone selects the tiles at random.
All tiles will eventually be selected by someone playing the game.
The number of players can vary from 2 - 5 players.

In a 2 player game, if I need a certain tile (Tile M) and there is only 1 left I have 50% chance of getting it.
<code>| Player A | Player B       |
|----------| ---------------|
| 0        | Tile M         |
| Tile M   | 0              |
</code>
Player A gets at least 1 certain tile 1/2 of the time = 50% chance.
In a 2 player game, if I need a certain tile (Tile M or Tile N) and there are 2 left (Tile M & Tile N) I have 75% chance of getting one of them.
<code>| Player A   | Player B       |
| -----------| -------------- |
| Tile M & N | 0              |
| Tile M     | Tile N         |
| Tile N     | Tile M         |
| 0          | Tile M & N     |
</code>
Player A gets at least 1 of the certain tiles 3/4 of the time = 75% chance.

Solving these using truth tables doesn't scale well.
Is there a general solution to solve for X tiles with Y players?
Comment: Usually these problems (and their solutions) are phrased in terms of drawing cards.  Is there a way for you to make that analogy?  After all, "...if I need a tile (Tile M) and there is only 1 left I have 50% chance of getting it..." sounds to me like drawing the last card in the deck (which should be 100% chance if you draw next).  Unless you mean "there's 1 'M' left in the deck" in which case the odds of drawing are going to be proportional to the number of cards left in the deck - not precisely the 50% you've formulated.
Answer: The odds of "picking a specific kind of tile out of a pile of tiles" is perfectly analogous to "picking a specific kind of card out of a deck of cards".  As such, you should look into "The Hypergeometric Distribution" as a tool for solving these sorts of problems.
(I'm going to switch over to calling them 'cards' in 'decks' from here on so that it is easier for everyone reading along to be able to more easily bounce around to other reference materials.)

Firstly, I'm going to start off by explaining that your "If there are two players, the odds I draw the last X is 50%!" instinct is actually incorrect.  Let's create the simplest counterexample:

There is one X left in the deck; the deck is down to it's final card; Alice is the next person to draw.

What are the odds that Alice draws the X? Since it is the last card in the deck, and she is guaranteed to draw the last card in the deck, then she is 100% going to draw that X.
Let's look at a few more examples to see if we can figure out why our general intuition didn't mesh with the example...

There is one X left in the deck; the deck is down to 2-cards; Alice is the next person to draw.

What are the possibilities this time? Well, if we call the other non-X cards "O" then there are only two ways that the deck could be shuffled: XO, or OX.  This means that Alice would draw the X in the first case, but not in the second.  So we're back to a 1/2 (50%) chance as our intuition expects.

There is one X left in the deck; the deck is down to 3-cards; Alice is the next person to draw.

This time the deck could be shuffled as: XOO, OXO, or OOX.  If it is only Alice and Bob playing then Alice wins in the first or third cases - meaning 2/3 odds.  If you keep doing more examples you find the odds for 4-cards are 2/4, then 5-cards are 3/5, then 6-cards is 3/6, then 7-cards is 4/7... so what is the pattern?
Whenever there was an even number of cards in the deck (call it $2N$) then Alice and Bob would draw $N$ cards each, but whenever there was an odd number ($2N+1$) Alice would be the one to draw the final card.  So, that means Alice's odds are $N/2N$ for even decks, or $(N+1)/(2N+1)$ for odd decks.
And just like that, things jive with out intuition again!  For a deck with lots of cards left, Alice's odds are basically 50% whether the deck has an odd or even number of cards left.

But how does this connect to the Hypergeometric Distribution? And how do we generalize?
Above we were considering all possible shuffle orders and counting up the successful outcomes versus the total outcomes.  But it would be tedious to count up all possible shuffle orders and manually check whether Alice wins or loses for each possibility.  Is there another way to get to the same result?
In the 3-card deck example, the three possible shuffle-orders were XOO, OXO, and OOX.  This means the cards Alice specifically drew would have been XO, OO, and OX.  These are effectively 2-card hands drawn from a 3-card deck.  And the Hypergeometric Distribution can calculate those kinds of possibilities for any hand-size and deck-size we want!
So, how does it work? Well, let's start off by saying that there are $D$-many total cards left in the deck, and we will be drawing a $d$-many card hand.  There are specific cards we want to draw, with a total of $X$-many left in the deck, and we'll say that we want to know the likelihood of drawing $x$-many of them.  The hypergeometric distribution says that the odds of of drawing $x$ special cards in a $d$ card hand (with $D$ and $X$ fixed) are:
$$
P(x,d) = \frac{\binom{X}{x}\binom{D-X}{d-x}}{\binom{D}{d}}
$$
where the parentheses symbols represent the binomial coefficients: $\binom{n}{k}$ is called "n choose k".  But what does it all mean?  Well, the phrasing "n choose k" is actually really the crux of it - this little symbol is a way of accounting for the number of possible ways to draw exactly k-many somethings from out of x-many somethings.
Think about playing cards; how many ways are there to draw exactly one ace?  Since there are four aces and we want to choose one, that means "n choose k" is $\binom{4}{1}$.  If we plug that into WolframAlpha ("Choose[4,1]") or Excel ("COMBIN(4,1)") or something we'll see $\binom{4}{1}=4$.  Does that make sense? Yes! We would have drawn either the ace of hearts, diamonds, spades, or clubs.  What about drawing two aces? $\binom{4}{2}=6$. Does this still make sense? Sure.  We'd get pairs of either HD, HS, HC, DS, DC, or SC if we list them all out. $\binom{4}{3}=4$? The possible trios are HDS, HDC, HSC, DSC. $\binom{4}{4}=1$? The only possibility for all four is HDSC!
Okay, that makes sense for the first $\binom{X}{x}$ bit in our problem, but what about the other stuff?  The $\binom{D-X}{d-x}$ is just counting out all the different ways for drawing the other unimportant (non-X) cards in our hand.  And how do we turn that into a probability? We divide it by the total number of different possible d-card hands from a D-card deck $\binom{D}{d}$!
So, if we want to know the odds of drawing the very last X-card... then the probability must be:
$$
P(1,d) = \frac{\binom{1}{1}\binom{D-1}{d-1}}{\binom{D}{d}}
$$
Oh wait, we need to know the deck size and hand size!  We could figure out those numbers and then plug it into WolframAlpha or whatever... but actually with the mathematical identity $\binom{n}{k}=\frac{n!}{k!(n-k)!}$ and a little elbow grease we can actually get a little further with this...
$$
P(1,d) = \frac{\frac{1!}{1!(0)!} \cdot \frac{(D-1)!}{(d-1)!(D-d)!}}{\frac{D!}{d!(D-d)!}} = \frac{(D-1)!}{D\cdot(D-1)!} \cdot \frac{d \cdot (d-1)!}{(d-1)!} = \frac{d}{D}
$$
Wow, It's surprising how nicely that all cancelled out!  What does this tell us? Well, if we know how many cards each person would draw from the remaining deck, we can calculate Alice's win percentage super easily.  A two-player game with 3-cards in the deck? Alice's draw odds are d/D = 2/3 just like from before! Five-player game with a 50-card deck? Alice's odds are 10/50! Six-player game with 61-cards? Alice's odds are 11/61 (since she gets an extra card in her draw)!
But what if it wasn't just a single X that could have won the game?  What if there were two or three X's left?  In that case it's mostly the same, except you have to make sure to account for the odds of drawing hands with multiple winners.
So, the number of ways to draw an Ace from a deck would be:
$$
P(x\neq 0,d)=P(1,d)+P(2,d)+P(3,d)+P(4,d)
$$
(assuming d>4)

So how do we generalize this further, if we want to draw an X or Y or Z?  There are two basic ways: the simple way and the hard way.
In the simple method, you just count the good cards all together in one group separate from the "other" cards.
$$
P(g,d) = \frac{\binom{G}{g}\binom{D-G}{d-g}}{\binom{D}{d}}
$$
There are two Xs, four Ys, and zero Zs left in the deck and you need to draw one of any of them? Then $G=6$ and you work through it just like before (once again making sure to count up the possible multi-winner hands).
Or, in the more complicated method, we can use the same counting ideas used in the Hypergeometric Distribuion and extend them into a Multivariate Hypergeometric Distribution!
$$
P(x,y,z,d) = \frac{\binom{X}{x}\binom{Y}{y}\binom{Z}{z}\binom{D-X-Y-Z}{d-x-y-z}}{\binom{D}{d}}
$$
This is a way of accounting specifically for the number of each individual x, y, or z card that gets drawn (or not drawn).  It can be a pretty powerful tool, but you also have to be extra careful about what you are tracking.  For example:
$$
P(1,1,1,d) = \frac{\binom{X}{1}\binom{Y}{1}\binom{Z}{1}\binom{D-X-Y-Z}{d-1-1-1}}{\binom{D}{d}}
$$
represents the odds of drawing exactly one of each kind of card.  Getting exactly one of any of the cards would have to be accounted properly by the following:
$$
P(\mbox{exactly one winner},d)=P(1,0,0,d)+P(0,1,0,d)+P(0,0,1,d)
$$
In other words, things can get very tedious once again depending upon how overly-specific we want to get.
Answer: Probability(Get atleast one tile) = 1 - Probability(Getting no tiles)
This second probability is easier to calculate : for each tile among the $X$ tiles, it can go to any of the other players with probability $\frac{Y-1}{Y}$
As these tiles seem independent of each other, the total probability of all tiles going to other players, or you getting no tiles is $\left(\frac{Y-1}{Y}\right)^X$, so the final probability of you getting at least one tile is $1-\left(\frac{Y-1}{Y}\right)^X$
Note : Also if $Y\gg X$, $1-\left(\frac{Y-1}{Y}\right)^X = 1-\left(1-\frac{1}{Y}\right)^X \approx 1-\left(1-\frac{X}{Y}\right) = \frac{X}{Y}$ which is kinda what you would expect


Query ActiveRecord for first object with match to partial attribute

Question: I have a scaffold for a user model which has a name attribute. Index looks like this:

I want to do a query that finds only the first record whose name begins with the two letters of: "Mr".  So for above it would return the record with the name "Mr Bar".
I know it might look something close to this:
User.find_by(name: name[0].concat(name[1]) == "Mr") # doesn't work

Comment: I upvoted you back. I think you have a good question, and I edited the title.
Answer: You can use a where method to obtain Users whose name starts from "Mr" and first to obtain the first result of the query
User.where("name like ?", "Mr%").first

Comment: Just made an edit to the answer. I just removed the '%' symbol before "Mr" which ensures that names starting with "Mr" are picked


index position in ORDER BY (ranking position)

Question: I have a table enemies, one of the fields is a named "Power". I have power as index.
To list the enemies based in power I would use
SELECT * FROM enemies ORDER BY Power DESC

Now I want to show for a certain enemy the position based in power. Also I want to show the five enemies that are over and under that value.... but I dont want to read all the registers until the one that i'm interested (millions of registers).
something like:
enemy1 1111
enemy2 1100
...
enemysearched 1000
enemyn1 995
.....

One option is to generate a rank column and calculate it often, but I just want to know if it is possible just with mysql at good speed.
Is it possible without reading the full table?
Comment: When the table isn't that big 1 or two million it should be that slow, to get the position, the only way i could think of is to save the rank and always keep it up-to-date, but that would cost much more that calculating it when it is needed.
Comment: I have in mind one idea... look for the value of the row, then another select order ASC with limit 5 and then  same query but changing the > for <= and limit 5 DESC
Comment: not in mysql that would cost more power performance than ranking it once., have you thought about  keeping the table in memory for the rank, it would cost because you must insert and update two tables the physical and that in memory, but it will be fast
Comment: I have tested the method that i told you before... 3 querys, first to search the value, then Select * FROM enemies WHERE Power>=$val ORDER BY Power ASC LIMIT 6 and another with Select * FROM enemies WHERE Power<$val ORDER BY Power DESC LIMIT 5.  It took 0.0005s to make the full table in my computer. Solution found.
Comment: It is not the solution :(, with that i know the table but not the position. To know the position you need to make a count with the number of values over your value
Answer:
...I want to show for a certain enemy the position based in power.

To find the position you can do:
select count(*) + 1
from enemies e
join enemies f on f.power < e.power
where e.id = 1000 -- the base enemy

This query will perform an *Index Range Scan" engine operation so it should be faster than reading the table. Still it can be time consuming if there are many rows in the table.

Also I want to show the five enemies that are over and under that value...

You can do:
select f.*
from enemies e
join enemies f on f.power < e.power
where e.id = 1000 -- the base enemy
order by f.power desc
limit 5
union all
select f.*
from enemies e
join enemies f on f.power > e.power
where e.id = 1000 -- the base enemy
order by f.power
limit 5

This query will perform two Index Range Scan. Both should be pretty fast all the time, since each will read 5 rows max.
Comment: I will check it later, but i dont know if it will be faster than the method that i wrote in comments. Anyways if it works and it is not too slow i will accept it. The position is not indexed and with 3 millions of register it can be slow, i will check and tell you. My solution is under 1ms.
Comment: @hamboy75 - Sounds the same.  Impaler's solution uses `PRIMARY KEY(id)` 3 times for a point query (very fast).  And `INDEX(power)` 3 times -- once will scan some portion of the index; the other two stop after 5 rows.


Interpretation of mutual information

Question: I remember having read the following interpretation of Shannon's information somewhere
"For discrete variables, I(X;Y) quantifies how well we can discriminate among the outcomes of Y by looking at the outcomes of X (and viceversa)"
This interpretation is fundamental for my paper cause it stresses the "discrimination" aspect. However, I cannot find a reference for it anymore.
So I am wondering whether:

whether this is a well-accepted interpretation of MI and
whether anyone could point out a reference for it.

Addendum: I think that the property that MI is zero if either one of the variables has only one single outcome is actually a fundamental property of MI. In particular it speaks against some common interpretations of MI, for example:

MI quantifies the predictability of $Y$ from $X$ (or viceversa): wouldn't $I(X;Y)$ then be maximal and not minimal when either $Y$ has only one outcome, since it is perfectly predictable?
MI quantifies the statistical relationship between $X$ and $Y$. But then I would expect $I(X;Y)$ to be maximal when both $X$ and $Y$ have only one possible outcome, since in this case the statistical relationship is deterministically defined.

I am looking for an interpretation that stresses the fact that for MI to make sense both $X$ and $Y$ must have at least two outcomes each.
Comment: Can you define what it means to 'discriminate among the outcomes of Y'?
Answer: This is what I have learned while studying information theory, but I do not know a reference for a quote stressing the discrimination aspect.
I think of mutual information as measuring the size of the intersection in the Venn diagram between the information "contained" in two sources (random variables). If that is a complete overlap, then the outcome of $X$ will exactly be the outcome of $Y$. If there is no overlap, then the outcome of $X$ will not say anything about the outcome of $Y$. If there is partial overlap, the outcome of $X$ should sway your belief in some direction about the outcome of $Y$ (helping to discriminate between which outcomes are likely/unlikely) but will not guarantee the outcome of $Y$.
Comment: The point I want to make is that MI is intrinsically a non point-wise quantity. Indeed, if X or Y have only one possible outcome then MI is zero. This conflicts with the interpretation of MI in terms of "predictability", since if that interpretation were to hold you would expect MI to be maximal and not minimal if Y has only one outcome. Instead, discrimination can only be done between at least two items.
Comment: I suppose in the case of both $X$ and $Y$ having only one outcome, there is no information or "surprisal" contained in observing either outcome, i.e., the area of the circles in the Venn diagram would be $0$, and so whether you consider maximal or minimal overlap, the area of that overlap is still $0$. Have you ever heard of thinking of entropy and mutual information in terms of "surprisal"? This is a nice analogy.
Comment: I think surprisal goes well with your idea of discrimination, because there needs to be possible uncertainty (i.e. at least two items) to be surprised, or to talk about discriminating between them.
Comment: Yes. I know the surprisal interpretation. Surprisal is well defined for variable with only one possible outcome and is equal to zero. Also, for MI to be zero it is enough that either X or Y are singleton.

Interpretation of mutual information


Is the minimal polynomial also minimal over the closure of the base field?

Question: Let $V$ be a finite dimensional vector space over the field $F$ and $T: V\to V$ be linear. Then the minimal polynomial of $T$ is the least degree monic polynomial in $F[x]$ that annihilates $T$. Is it true that it must also be the least degree monic annihilator of $T$ in $\overline F[x]$?
I know this is true when $F=\mathbb{R},\overline F =\mathbb{C}$, but only by seeing  $\mathbb{C}$ as a finite dimensional vector space over $\mathbb{R}$, but I wouldn't know how to extend that to arbitrary fields.
I also tried applying the division algorithm on the minimal polynomial $m_A \in F[x]$ and a hypothetical minimal polynomial $\mu_A \in \overline F[x]$ of lesser degree to reach a contradiction.
Comment: What have you tried? Any examples?
Comment: @markvs I know that this is true when $F=\mathbb{R}, \overline F= \mathbb{C}$, where I would use the fact that $\mathbb{C}$ is a finite dimensional vector space over $\mathbb{R}$. But I don't know how that could extend to general fields and their closures. I've tried to use the division algorithm on the minimal polynomial $m_A \in F[x]$ and a hypothetical annihilating polynomial $\mu_A \in \overline F[x]$ of lesser degree to reach a contradiction, with no results.
Answer: Yes, it is true. Take a standard basis $e_1,...,e_n$ and let $e=e_i$. Consider the sequence of vectors $e, Te, T^2e,...,T^m e,...$, all these vectors have coordinates in $F$.
There exists the minimal $m=m(e)$ such that the vector $T^me$ is a linear combination $\lambda_0e+\lambda_2Te+...+\lambda_{m-1}T^{m-1}e$ (over $\bar F$). Note that since $T^k e$ have coordinates in $F$, all $\lambda_i$ are in $F$. Let $p_e$ be the polynomial  $t^m-\lambda_{m-1}t^{m-1}-...-\lambda_0$. Note that it is in $F[t]$. Then $p_e(T)e=0$.
If $e_1,...,e_n$ is the standard basis then the minimal polynomial $p(t)$ of the matrix $T$ is the lcm of all $p_{e_i}(t)$, and so it belongs to $F[t]$ too.
Comment: Coefficients of the linear combination can be read off the RREF of the matrix where columns are $e,Te,...,T^m e$ (these are the entries of the last column of the RREF. The matrix has entries from $F$; when you make RREF, you do not need to get out of $F$, so all $\lambda_i$ are in $F$.
Comment: I'm sorry could you explain with a little more detail why the $\lambda_i$ have to be in $F$? I remember having done something similar but getting stuck on that point.


Verificar qual dos 'guards' está loggedin

Question: Eu tenho uma aplicação laravel 5.2, com multiautenticação, os guards configurados em config/auth.php são:
...
'admin' => [
    'driver' => 'session',
    'provider' => 'admin',
],
'user' => [
    'driver' => 'session',
    'provider' => 'user',
],
...

Ou seja admin e user.
O meu problema é na view, pois estes dois guards quando loggedin partilham algumas mesmas views, aí é que é gerado o problema:
Olá {{Auth::guard('admin')->user()->name}}

Neste caso o guard correspondente está hardcoded para ser sempre o admin (dando erro caso seja o guard user que esteja loggedin), depois de alguma pesquisa não encontrei nada de me indicasse a solução, ou seja, de saber se o guard que está loggedin é user ou admin, dessa maneira escusava de outra view igual só com essa alteração. EX de solução:
Olá {{Auth::guard(<GUARD QUE ESTEJA LOGIN>)->user()->name}}

PS: Sei que daria indo pelo segmento do url correspondente ao guard, ex: www.site.com/pt/user/dasboard, neste caso seria o segmento 2, mas perdia-se um da escalabilidade da app pois nada garante que no futuro o segmento correspondente do url continue a ser o 2.
Answer: Cara, já pensou nisso:
$guard    = $this->getGuard();
$provider = $guard->getProvider();
Auth::guard($provider)->get()->name;

Senão, algo que você pode fazer:
if(Auth::guard('admin')->check()){
   Auth::guard('admin')->admin()->name;
}
elseif(Auth::guard('user')->check()){
   Auth::guard('user')->user()->name;
}

Comment: Obgado pela resposta Diego, mas isso ia tirar escalabilidade à app, no futuro teria de mexer nisso se os guards não fossem os mesmos... Queria saber o guard loggedin independentemente do nome que possa ter. O facto de ter de escrever o nome do guard é que estou a tentar evitar. Daria um voto por tentar ajudar mas só posso amanha, já gastei os meus votos hoje mas não me vou esquecer
Comment: Mas o primeiro código da minha resposta pega o Guard Logged. Não é isso que deseja ? Não fixar um, mas sei pegar o que está na ativa ?
Comment: Nop, fazendo `dd($this->getGuard());` no controller dá: `Method [getGuard] does not exist.` Também perguntei no SOen mas sem sucesso: http://stackoverflow.com/questions/38999725/checking-which-guard-is-loggedin/39002084?noredirect=1#comment65357513_39002084
Comment: Eu alterei o código. Não está mais assim.
Comment: Sim, mas o problema é `$this->getGuard();` dá o erro que escrevi
Comment: Entendi. Vou pensar mais nisso.


Finding approximation of root

Question: AS for newton approximation of the reciprocal of the square root of 5. Does the function f(x)=1/x - 5^1/2 apply for newton method
Answer: No, because if you know $5^{1/2}$ to use in $f(x)$, then clearly you know
$\dfrac{1}{5^{1/2}}$.
The usual Newton's method for finding the reciprocal of the square root of $a$ uses $f(x)=\dfrac{1}{x^2}-a$, which has the advantage that the Newton iteration does not need a division:
$$
x_{n+1} = \frac{x_{n}(3-ax_n^2)}{2}
$$

Finding approximation of root


The size of an array created from np.random.normal

Question: I am using the numpy's random.normal routine to create a Gaussian with a given mean and standard deviation.
array_a = an array of len(100)

gaussian = np.random.normal(loc=array_a,scale=0.1,size=len(2*array_a))

So I expect the gaussian to have a mean=array_a and stddev=0.1 and the size of the gaussian array to be 2 times array_a.
However the above returns me an array with the same size as that of array_a !
How do I get the len(gaussian) to be 2 times len(array_a) with the given mean and standard deviation?
Answer: you have to multiplicate len(array_a) * 2 instead of len(array_a * 2) and loc=array_a.mean()
Try:
import numpy as np

array_a = np.arange(100)
gaussian = np.random.normal(loc=array_a.mean(), scale=0.1, size=2 * len(array_a))

Now gaussian.size is 200 and gaussian.mean() is equal to array_a.mean().
Comment: Actually I now understand! What I wanted was to create a single gaussian array, but with the `mean` being each value of `array_a`. But this is not possible if I want a final array of a size greater than `array_a`. So your method will work, provided I give a single `mean` value.
Comment: I would like the mean to be each value in `array_a`, and not the mean of `array_a`.
Comment: so you want 100 gaussians as output?
Comment: NO!! Each value in `gaussian` will have a different mean. i.e. the first `gaussian` value will have the `mean = first object` of `array_a` and so on.
Comment: i don't understand. can you explain with a quantitative example what do you expect?


How to remove paragraph tag from ckeditor?

Question: When we insert content into ckeditor field,suppose we write single line of text into ckeditor without any html tag, why its wrapping paragraph  tag automatically around the text .
I need to remove this paragraph tag because its breaking my page's structure.
Comment: That's how ckeditor works, it always has at least one block level tag at the root of the document. Have a look through their docs and see if you can find an option to disable the behaviour, if you can it'll be fairly straightforward to apply that option via drupal. Last time I checked there was no good way to do it, you had to abuse a combination of other features to get it to work which may have undesirable side-effects
Comment: Why are you using CKEditor if you don't want any html on that field? Furthermore why does a P tag break your layout? The easy solution here is disable the format or recreate the field as a textarea only.
Comment: I am also having the same issue
Answer: In Drupal 8, you may need to implement hook_editor_js_settings_alter and set config.autoParagraph to false, as referenced here: CKEDITOR.config.autoParagraph
/**
 * Implements hook_editor_js_settings_alter().
 */
function MODULE_editor_js_settings_alter(array &$settings) {
  foreach ($settings['editor']['formats'] as $name => $value) {
    $settings['editor']['formats'][$name]['editorSettings']['autoParagraph'] = FALSE;
  }
}

Comment: Also there's a module for that (TM): [CKEditor custom config \| Drupal\.org](https://www.drupal.org/project/ckeditor_config)
Answer: In Drupal 7 you have to go to admin/config/content/ckeditor press "edit" in the profile you want to change, and then  under "Custom JavaScript configuration" (in advanced options section) add this line:
config.enterMode = CKEDITOR.ENTER_DIV;

more info in the original answer by alex-petrov:
https://drupal.stackexchange.com/a/30995/55010
Answer: In Drupal 8 using CKeditor if you change the text format to 'Full HTML' (to allow div tags) then click on source and then change the
 for a
 then it will not automatically add the
 tags in next time it loads the content.
This is because, (as clive mentioned) there needs to be a block level element at the root of the document.
This is often less problematic than wrapping it in a p tag as sites are less likely to have global div styles, spacing, borders, padding, etc.
Answer: If you don't have a
 in the Allowed HTML tags and enable the "Limit allowed HTML tags and correct faulty HTML" filter no
 will be output on the front end. These settings appear per text format seen at /admin/config/content/formats.
The Allowed Formats module allows you to restrict certain fields to specific formats. This way you can restrict this field to a specific text format, a format that does not allow
 tags, but allow them everywhere else.


What is the function of this ♯V major chord (or ♭VI major)?

Question: From this video, just listen to the first 8 bars (first 15 seconds). It sounds like the chord progress is I III7 II7 V# V7 in F major scale to me.
I don't know how to explain the function of this V# major chord. It sounds so natural but I don't know why.
The closest thing I can think of is the backdoor progression which is VIb VIIb I. But in this case, there is no VIIb after VIb. The fifth chord is not even close to it.
Answer: Assuming your transcription is correct, that is not a ♯V chord, but a ♭VI chord. This is borrowed from the tonic minor and provides a powerful pull to the dominant (because of the half-step-downward resolution of the ♭VI chord's root, third, and fifth).
Answer: With Roman numeral analysis you can put sharps and flats on the Roman numerals to show the root is altered.
But, while a altered dominant root with ♭V makes sense a ♯V does not. Here is why...
Usually such alteration are about borrowed chords or mode mixture. For example, in a major key you can have a ♭VII borrowed from mixolydian. When you add flats in the order of the circle of fifths the sequence of altered tones, starting from C major, is B♭ E♭ A♭ D♭ G♭ and the corresponding modes in sequence are mixolydian, dorian, aeolian, phrygian, locrian. When you add sharps the sequence is short, it's just one move F♯ taking it to lydian.
When you get to the end of either the flats or sharps direction the next step would alter the tonic from C to either C♭ or C♯. Things get weird, but if we keep going up with sharps to finally get to the alteration of a ♯V, which starting from C major would be a G♯, we first move through C# locrian, then C# phrygian.
So, alterations like ♭II or ♭VII, for example, make sense, because you can say they are borrowed from the parallel modes phrygian or mixolydian. From C major those would be C phrygian or C mixolydian. But for ♯V you wouldn't even get the root of the chord until C# phrygian. Where the tonic has changed! But, even then the chord would be #vo, a diminished triad. To get a diatonic #V you have to add seven sharps to get to C# major!
All this becomes a whole lot clearer when you use the key signature which should be given with Roman numeral analysis. This... C: I III7 II7 C#:V C:V7 ...uses a G# major triad but requires a really clumsy tonic change in the middle of the progression. But, if you spell the root as something diatonic to a C tonic, it will be enharmonic equivalent A♭ or ♭VI. This... C: I III7 II7 ♭VI V7 ...is so much clearer! One tonic with borrowed chords.
Basically, you shouldn't normally see root alterations on I or V. They don't make sense. A ♭V would be a diatonic chord in locrian, but locrian is pretty unusual. Not at all common.
Alteration on dominants that you will commonly see are not alterations to the root, but on the chord's fifth and ninth, ex. G7♭9 or G7♭5. I've seen that combined with Roman numerals like V7♭9.
Answer: (Expanding on Dekkadeci's answer) the bVI7, sometimes with a few modifications, is a common predecessor to the V chord (perhaps through the I64 to avoid parallels or just to extend the piece). These are the "Augmented Sixth" chords (with geographic names).
There are two musical "tricks" happening, the half-step move from the b6 to the 5 stands out; using the bVI7 also resolves by a half step. (Usually, the flat seventh is written as an augmented sixth, Ab-C-Eb-G#, to show the direction of motion.) The usual modifications (which do not change the function, just alter the color and voice-leading) are to drop the fifth (Eb in this case) or to lower that fifth which gives two tritones. This is all covered in (Common Practice Period) harmony books.


How to prove that a function from N to Z is bijective

Question: I'm trying to prove that $\mathbb{N}\sim\mathbb{Z}$ and I think that by proving a function exists between $f(\mathbb{N})=\mathbb{Z}$ (that is onto and $1$-$1$) would show this.
However I'm at a loss at how to show this and I am wondering if someone could help me get a proof form of this?
Comment: Closely related, maybe duplicates http://math.stackexchange.com/questions/795234/showing-the-integers-has-the-same-cardinality-to-the-natural-numbers http://math.stackexchange.com/questions/873927/how-to-show-the-integers-have-same-cardinality-as-the-natural-numbers
Comment: @Pp.. after your edit the question is quite difficult to understand. As the symbol ~ between $\mathbb{N}$ and $\mathbb{Z}$ is not visible anymore. Please, try to be more careful in te future.
Answer: Consider $k \mapsto k/2$ for $k$ even, and $k \mapsto -(k+1)/2$ for $k$ odd.
Comment: with a slight adjustment if $\mathbb N$ starts at $1$
Comment: Yes in that case replace the $+1$ by $-1$.


Can I Systematically Determine the Cardinality of a Finite Set?

Question: Is there any way you can, in a systematically manner, determine the cardinality of a finite set in which the elements are given by a specific expression?
For example, let $A$ be a set $\ A=\{1,2,3,4,5,6\}$ and $B$ be a set defined by:
$$B=\biggl\{\frac{a-b}{a+b} : a,b\in A\biggl\}$$
My question is thus, can I somehow determine the cardinality of the set without having to calculate and enumerate each and every element?
Comment: This depends heavily on what sorts of expressions you allow. Effectively you are asking to determine the range of a function; it depends heavily on what kinds of functions are allowed, and in what form they are given.
Comment: I don't think there is any general method for this ... but given a specific problem (like yours) you may find some specific ways to avoid having to go through all of them. For example, you know that $\frac{a-b}{a+b}=0$ for any $a=b$, and that $\frac{a-b}{a+b}=- \frac{b-a}{b+a}$, so that already cuts it down to less than half of all $36$ pairs.
Answer: It's really dependent on how you built the set. The thing that can make things rather tricky is when things in the set are equal only one thing is counted. So usually inequalities are easy but the exact number you'd have to eleminate all equivalent elements.

For example, let $A$ be a set $\ A=\{1,2,3,4,5,6\}$ and $B$ be a set
  defined by: $$B=\biggl\{\frac{a-b}{a+b} : a,b\in A\biggl\}$$

Since $|A|=6$ and you are taking two elements in $A$ you get the easy inequality $|B| \le 6^2=36$.
Since $B$ has at least $1$ element $1 \le |B|$
Enumerateing through the values and evaluating them in the function $\frac{a-b}{a+b}$ i get the following table
$\begin{array}{rrrrrr}
0 & \frac{1}{3} & \frac{1}{2} & \frac{3}{5} &
\frac{2}{3} & \frac{5}{7} \\
-\frac{1}{3} & 0 & \frac{1}{5} & \frac{1}{3} &
\frac{3}{7} & \frac{1}{2} \\
-\frac{1}{2} & -\frac{1}{5} & 0 & \frac{1}{7} &
\frac{1}{4} & \frac{1}{3} \\
-\frac{3}{5} & -\frac{1}{3} & -\frac{1}{7} & 0 &
\frac{1}{9} & \frac{1}{5} \\
-\frac{2}{3} & -\frac{3}{7} & -\frac{1}{4} & -\frac{1}{9}
& 0 & \frac{1}{11} \\
-\frac{5}{7} & -\frac{1}{2} & -\frac{1}{3} & -\frac{1}{5}
& -\frac{1}{11} & 0
\end{array}$
So sorting out all the duplicates:
$$B=\left\{0, \pm\frac{1}{2}, \pm\frac{1}{3}, \pm\frac{2}{3}, \pm\frac{1}{4}, \pm\frac{1}{5}, \pm\frac{3}{5}, \pm\frac{1}{7}, \pm\frac{3}{7}, \pm\frac{5}{7}, \pm\frac{1}{9}, \pm\frac{1}{11}\right\}$$
So it looks like in your example $|B|=23$
Comment: When you calculate $(3-2)/(3+2)$, you get $-1$ or $0$? I think you have erred in your calculation...  Are you using a spreadsheet that's rounding numbers off to integer values?
Comment: hmm. Yeah you're certainly right I ran this as a table in sage. I bet my function didn't work. I'll fix it :)
Comment: @GTonyJacobs You were right about the roundoff it was caused by type from the native python iterator being
 and when I'd type them in by hand . The 'sag.rings.integer.Integer' type casts into an exact rational field as necessary while the 'int' type does integer floored arithmetic :)
Answer: In a systematic manner, determine the cardinality of any finite set? No. For this one, without having to calculate and enumerate each and every element? Ok. The possible combinations of values for $a$ and $b$ are $36$. From them, we must remove duplicates. That is, when we get the same result for different values of $a$ and $b$. When this happens, we have:
$$\dfrac{\alpha-\beta}{\alpha+\beta}=\dfrac{\gamma-\delta}{\gamma+\delta}
 $$
We cross multiply them to get: $\left(\alpha-\beta\right)\left(\gamma+\delta\right)=\left(\gamma-\delta\right)\left(\alpha+\beta\right)
 $. Multiplying and eliminating common terms gives:
 $$\alpha\delta=\beta\gamma$$
 Now, either $\alpha=\beta
 $ (and thus $\gamma=\delta
 )$, or not. There are $6$ cases for which $\alpha=\beta
 $ (and thus $\dfrac{\alpha-\beta}{\alpha+\beta}=0
 $). We count only one, so we subtract $5$ from $36$ $(=31)$. We need to find how many duplicates are there for $\alpha\neq\beta
 $. If we write the number $\left(\alpha\delta\right)
 $ as a product of prime numbers, it will have the form: $$p_{1}^{n_{1}}p_{2}^{n_{2}}...p_{k}^{n_{k}}$$
 But because $\alpha\delta\leq36
 $, only in one case we have more than two distinct primes: $2\cdot3\cdot5=30
 $. This gives $3$ products of the form $\alpha\delta
 $: $2\cdot15=30
 $, $6\cdot5=30
 $ and $3\cdot10=30
 $. Since $10$ and $15$ are not in $A$, we have no duplicates. So, $\left(\alpha\delta\right)
 $ must be of the form:
$$p_{1}^{n_{1}}\cdot p_{2}^{n_{2}}$$
 Now, $\left(\alpha\delta\right)
 $ can be writen as a product of two numbers in several ways:
$$\alpha\delta=p_{1}^{n_{1}}\cdot p_{2}^{n_{2}}=p_{1}^{n_{1}-1}\cdot\left(p_{1}\cdot p_{2}^{n_{2}}\right)=p_{1}^{n_{1}-2}\cdot\left(p_{1}^{2}\cdot p_{2}^{n_{2}}\right)=...=p_{1}^{0}\cdot\left(p_{1}^{n_{1}}\cdot p_{2}^{n_{2}}\right)
 $$
We notice that, in order to have $p_{1}^{k}\cdot p_{2}^{l}\in A
 $, that is: $p_{1}^{k}\cdot p_{2}^{l}\leq6
 $, we must have $k,l\leq2
 $. Thus,
$$n_{1},n_{2}\in\left\{ 0,1,2\right\}$$
Since $\alpha,\delta\leq6$, we must have $p_{1},p_{2}\leq5
 $. But if one of the primes is $5$, for example, $p_{2}=5
 $, then it is not possible to express $\left(\alpha\delta\right)
 $ as a product in two different ways: $p_{1}^{k}\left(p_{1}^{l}\cdot5\right)=p_{1}^{m}\left(p_{1}^{n}\cdot5\right)
 $. So $l=n=0
 $, else $\left(p_{1}^{l}\cdot5\right),\left(p_{1}^{n}\cdot5\right)\notin A
 $. But then, $\left(p_{1}^{l}\cdot5\right)=\left(p_{1}^{n}\cdot5\right)
 $ and thus $p_{1}^{k}=p_{1}^{m}
 $. So: $$p_{1},p_{2}\in\left\{ 2,3\right\}$$
If one of the primes is $3$, then its exponent must be always $0$ or $1$, because for $k>1 , p_{l}^{k}>6 $. So we have the following possible cases:

$1\cdot2^{2}=2\cdot2$. Then,
$a=1\wedge b=2$ or $a=2\wedge b=1$ $(1)$
or $a=4\wedge b=2$ or $a=2\wedge b=4$ $(2)$
$2\cdot3=1\cdot\left(2\cdot3\right)$. Then,
$a=2\wedge b=1$ or $a=1\wedge b=2$, same as $(1)$,
or $a=2\wedge b=6$ or $a=6\wedge b=2$ $(3)$,
or $a=3\wedge b=1$ or $a=1\wedge b=3$ $(4)$
or $a=3\wedge b=6$ or $a=6\wedge b=3$ $(5)$.
$2^{2}\cdot3=2\cdot\left(2\cdot3\right)$. Then,
$a=4\wedge b=2$ or $a=2\wedge b=4$, same as $(2)$,
or $a=4\wedge b=6$ or $a=6\wedge b=4$ $(6)$,
or $a=3\wedge b=2$ or $a=2\wedge b=3$ $(7)$,
or $a=3\wedge b=6$ or $a=6\wedge b=3$, same as $(5)$.

Putting these valus in $\dfrac{a-b}{a+b}
 $, for all possible cases, we get:

$\dfrac{2-1}{2+1}=\dfrac{1}{3}
 $ and $\dfrac{1-2}{1+2}=-\dfrac{1}{3}
 $
$\dfrac{4-2}{4+2}=\dfrac{1}{3}
 $ and $\dfrac{2-4}{2+4}=-\dfrac{1}{3}
 $
$\dfrac{6-2}{6+2}=\dfrac{1}{2}
 $ and $\dfrac{2-6}{2+6}=-\dfrac{1}{2}
 $
$\dfrac{3-1}{3+1}=\dfrac{1}{2}
 $ and $\dfrac{1-3}{1+3}=-\dfrac{1}{2}
 $
$\dfrac{6-3}{6+3}=\dfrac{1}{3}
 $ and $\dfrac{3-6}{3+6}=-\dfrac{1}{3}
 $
$\dfrac{6-4}{6+4}=\dfrac{1}{5}
 $ and $\dfrac{4-6}{4+6}=-\dfrac{1}{5}
 $
$\dfrac{3-2}{3+2}=\dfrac{1}{5}
 $ and $\dfrac{2-3}{2+3}=-\dfrac{1}{5}
 $

These are $14$ cases from which - to remove duplicates - we must count only $6$. So we subtract $8$ from $31$ and we get $23$ possible different values for $\dfrac{a-b}{a+b}
  $. Thus
$$\left|B\right|=23$$
Answer: Let's solve the general problem.Consider $A=\{ 1,2,....,n \}$ and    $B=\{ {a-b\over a+b}:a,b\in A \}$.
Now, note that ${a\over b}= {c\over d}\Leftrightarrow {a-b\over a+b} ={c-d\over c+d}$ i.e there is a one to one correspondence between $B$ and $S= \{ {a\over b} : a,b\in A \} $.So, we have to find cardinality of $S$.
Now, if the least form of $a\over b$ is $c\over d$ then $gcd(c,d)=1$ and $c,d\in A$ as $1\le c\le a\le n$ and $1\le d\le b\le n$.So, the problem is to find the pair of numbers $c,d\in A$ such that $gcd(c,d)=1$.
Now, for $c>d$, for a chosen $c$, $d$ can be chosen in $\phi (c)$ ways. So, the number $c\over d$ s.t. $c\over d$ $>1$ and $gcd(c,d)=1$ are $\sum_{r=2}^{n} \phi (r)$ in numbers.Now for the numbers $e\over f$ $<1$ and $gcd(e,f)=1$, we just interchange the position of $c$ and $d$ in $c\over d$ $>1$ and $gcd(c,d)=1$.So, they are also in $\sum_{r=2}^{n} \phi (r)$ in numbers.
Now at last just consider ${1\over 1}=1$, which was not in previous cases.Hence the required result is $2 \sum_{r=2}^{n} \phi (r) +1$=$2 \sum_{r=1}^{n} \phi (r) -1$.
For particularly your problem, $n=6$ and $2 \sum_{r=1}^{n} \phi (r) -1=23$


Pesquisar no banco de dados e retornar o valor - Laravel

Question: Estou fazendo uma pesquisa no meu banco de dados, quero pegar o valor encontrado e salvar ele (São duas tabelas diferentes) mas o valor não está sendo salvo, está sendo salvo o numero 0.
Utilizo esse comando na minha controller:
$clientes = DB::table('cliente')->pluck('razaosocial', 'id');
return view('admin.contrato.create', ['cliente' => $clientes]);

E na minha view eu utilizo esse:

     {{ Form::label('cliente', 'Cliente') }}
         <select class="js-example-basic-multiple" name="cliente_id" >
            <option selected>Selecione o cliente</option>
              @foreach ($cliente as $cli)
                <option value="{{ $cli }}">{{ $cli }}</option>
              @endforeach
         </select>

Quando utilizo isso me aparece os dados, mas não consigo salva-los. Não manjo muito de PHP, alguém sabe como posso solucionar?
Comment: Lei um pouco sobre Eloquent nesse link: https://laravel.com/docs/5.6/eloquent  e veja como os objetos são persistidos no banco
Answer: Você está passando a razaosocial para o banco de dados,
Tente dessa maneira:

     {{ Form::label('cliente', 'Cliente') }}
         <select class="js-example-basic-multiple" name="cliente_id" >
            <option selected>Selecione o cliente</option>
              @foreach ($cliente as $key => $value)
                <option value="{{ $value }}">{{ $key }}</option>
              @endforeach
         </select>

Comment: Não deu o valor passado ainda foi 0.
Answer: Tente:
@foreach ($cliente as $cli)
    <option value="{{ $cli->id }}">{{ $cli->razaosocial }}</option>
@endforeach

Comment: Deu certo, muito obrigado !
Comment: Legal. Agora vi que coloquei um sinal de igual antes de '->'.
Mas deves ter ajustado. Vou editar.
Uma dica, use o Eloquent.


Route issue? Rails 'depot' issue with save_order redirecting

Question: I am making a rails depot application. After a user selects items to buy and proceeds to checkout, I want to save their order. If the save fails, I want to redirect to the index.
If the save succeeds, it should clear the session[:cart]. Then redirect the user to the catalogue page (the index action).
def save_order
  @cart = find_cart
  @order = Order.new(params[:order])
  if @order.save
    session[:cart] = nil
    redirect_to :action => "index"
  else
    redirect_to :action => "index"
  end
end

If the line redirect_to :action => "index" is included if the order saves, I simply get "We're sorry, but something went wrong" on chrome.
If I take it out, I get
Template is missing

Missing template store/save_order, application/save_order with {:locale=>[:en],
:formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "I:/13141-
CSC340A/Rails/Sites/depot3/app/views"

Any ideas?
Depot::Application.routes.draw do
  resources :orders

routes.rb
  get "store/index"
  post "store/add_to_cart"
  post "store/empty_cart"
  post "store/save_order"
  post "store/checkout"

  resources :products

Answer: I have a feeling the smart quotes around "index" could be a problem. Replace them with normal quotes.
Comment: Agreed. Try substituting the smart quotes for double or single quotes. Smart quotes almost uniformly causes snafus upon compilation.


Computing Hermite Normal Form using Extended Euclidean Algorithm

Question: I am trying to calculate the Hermite Normal Form of a square $n \times n$ matrix using the Extended Euclidean Algorithm to compute the columns of the HNF matrix, rather than the standard (column) matrix operations.  According to Cohen [Coh93], this is possible using the following algorithm, where $a_{i,j}$ denotes the coefficients of a matrix $A$, $A_{j}$ denotes column $j$ of the matrix $A$, and $B$ is an auxiliary column vector.
$
\def\indent{\qquad}
  k \leftarrow n\\
  j \leftarrow n\\
  \text{for } i \leftarrow n \text{ to } 1 \text{ do}\\
    \text{    compute } (u, v, d) \text{ such that } ua_{i,k} + va_{i,j} = d = \gcd(a_{i,k},a_{i,j}) \text{ with } |u| \text{ and } |v| \text{ minimal (see below)}\\
    \text{    while } j \geq 1 \text{ do}\\
    \indent\text{    if } a_{i,j} \neq 0 \text{ then}\\
    \indent\indent B \leftarrow uA_{k} + vA_{j}\\
    \indent\indent A_{j} \leftarrow (a_{i,k}/d)A_{j} - (a_{i,j}/d)A_{k}\\
    \indent\indent A_{k} \leftarrow B\\
    \indent\text{    end if}\\
    \indent\text{    } j \leftarrow j - 1\\
    \text{    end while}\\
  \text{end for}\\
  \vspace{5mm}\\
  \text{// Final Reductions}\\
  k \leftarrow n\\
  \text{for } i \leftarrow n \text{ to } 1 \text{ do}\\
    \text{    } b \leftarrow a_{i,k}\\
    \text{    if } b < 0 \text{ then}\\
      \indent\text{    } A_{k} \leftarrow -A_{k}\\
      \indent\text{    } b \leftarrow -b\\
    \text{    end if}\\
    \text{    if } b = 0 \text{ then}\\
      \indent\text{    continue}\\
    \text{    end if}\\
    \text{    for } j \leftarrow k+1 \text{ to } n \text{ do}\\
      \indent\text{    } q \leftarrow \lfloor a_{i,j} / b \rfloor\\
      \indent\text{    } A_{j} \leftarrow A_{j} - qA_{k}\\
    \text{    end for}\\
    \text{    } k \leftarrow k - 1\\
  \text{end for}\\
$
Note that the Euclidean step requires that $|u|$ and $|v|$ be minimal.  This means that of all the possible unique pairs, $(u,v)$ be chosen such that
$
  -\frac{|a|}{d} < v \text{sign}(b) \leq 0 \text{ and } 1 \leq u \text{sign}(a) \leq \frac{|b|}{d}
$
My algorithm for calculating the minimum $|u|$ and $|v|$ therefore consists of the following:
$
  d, u, v = \text{XGCD}(a_{i,k},a_{i,j}); \text{// Using XGCD defined by Magma} \\
  x_{0} \leftarrow u \\
  y_{0} \leftarrow v \\
  \text{if } (u = 0) \text{ or } (v = 0) then \\
    \text{    for } i \leftarrow 0 \text{ to } d \text{ do} \\
      \indent\text{    } u \leftarrow x_{0} - i \times b / d \\
      \indent\text{    } v \leftarrow y_{0} + i \times a / d \\
      \indent\text{    if } -\frac{|a|}{d} < v \leq 0 \text{ and } 1 \leq u \leq \frac{|b|}{d} \text{ then} \\
        \indent\indent\text{    break} \\
      \indent\text{    end if} \\
    \text{    end for} \\
  \text{end if} \\
  \text{return } d, u, v \\
$
Here, I have assumed that Magma's XGCD returns the correct sign for $u$ and $v$.
To test this algorithm, I am using the following sample matrix as input to the procedure:
$
  \left(
    \begin{array}{rrrr}
      10 &  0 &  1 &  0 \\
      -2 & -2 &  2 &  1 \\
      -2 & -1 &  3 & -1 \\
       4 & -6 & -2 &  0 \\
    \end{array}
  \right)
$
I expect the following output from the procedure:
$
  \left(
    \begin{array}{rrrr}
      33 & 12 & 12 & 11 \\
       0 &  6 &  5 &  1 \\
       0 &  0 &  1 &  0 \\
       0 &  0 &  0 &  2 \\
    \end{array}
  \right)
$
Instead, I am getting the following output:
$
  \left(
    \begin{array}{rrrr}
      3168 & 48 & 48 & 47 \\
         0 & 24 & 22 & 18 \\
         0 &  0 &  2 &  1 \\
         0 &  0 &  0 &  2 \\
    \end{array}
  \right)
$
It is apparent that there is no problem with the steps that reduce the lower triangular part of the matrix, nor with the steps that reduce the upper triangular part of the matrix (that is, the Final Reduction steps).  The problem therefore lies with the Euclidean step.
My question is whether or not I have correctly implemented the procedure that returns the minimal values for $|u|$ and $|v|$?  Note that I have used the following link relating to Linear Diophantine Equations as a guideline to implementing this procedure: Euclidean Algorithm. Is this correct, or should I be tackling this problem a different way?  Either way, where could I find a better reference that better describes the problem at hand?  Any help/advice would be appreciated.
References:
[Coh93] H. Cohen, A Course in Computational Number Theory, Vol. 138 of Graduate Texts in Mathematics. Springer, Heidelberg, 1993.
Comment: I hope you won't find me too presumptuous for putting `\def\indent{\qquad}` in the top of the first math mode block. I hope this is acceptable and works for everyone.
Comment: Are the `\indent` and `\vspace` commands working for you?! I just see the command sequences in red instead (Firefox 10.0.2 and Safari 5.1.2).
Comment: Unfortunately, the `\indent` and `\vspace` commands are not working for me either.  I am also seeing them in red.  I figured it's the best that I can do under the LaTeX style mathematics mode.  Note that I have a running version of this algorithm written in Magma.  I refrained from copying and pasting it with this question since I do not want the group to think that I am asking them to debug the program for me.
Comment: @bgins - Yes, it works for me!  Thanks for that!
Answer: I think there are some errors, either book errata, or transcription errors, in the stated algorithm. Firstly, $k$ is the pivot column, so it needs to be decremented (whenever nonzero) at the end of the first $i$ loop. Secondly, for each iteration in this loop, $j$ needs to be looped over from $k-1$ down to $1$, to clear out the lower triangular part, so it must be initialized inside the $i$ loop.
I am also skeptical of whether special handling of xgcd is necessary or even helpful, since it always returns a "positive definite" $d$ (an admitted abuse of terminology), in the sense that it is (or should be) always positive given $(a,b)\ne(0,0)$. Because of this, we also do not need the $b < 0$ test (and correction) in the final (partial upper triangular) reductions. In fact, it seems rather artificial to require $v \le 0$ and $u > 0$ of xgcd. What about $(a,b)=(3,1)$ and $(-1,3)$? Also, your pseudocode for the xgcd wrapper only alters its output if $u$ or $v$ is zero, and I think the loop could be avoided by solving some linear equations (taking quotients and a minimum).
However, I don't have Cohen's book, and don't know how or why he requires $|u|$ and $|v|$ to be minimal. Does he mean that $\max\{|u|,|v|\}$ must be minimal? I admit, I haven't thought deeply about it. But I think it is a fact that xgcd already does a good job of producing a minimal solution.
I have corrected the algorithm, as I think it should be, and implemented it in sage. After testing described below, I also made a correction.
m = Matrix(ZZ,4,4,\
    [[10,-2,-2, 4]\
    ,[ 0,-2,-1,-6]\
    ,[ 1, 2, 3,-2]\
    ,[ 0, 1,-1, 0]]).transpose(); m

def hnf(m,show=False):
    nr = m.nrows()
    nc = m.ncols()
    if show:
        print 'hnf: '+str(nr)+'x'+str(nc)
    assert(nr == nc)
    c = m.columns()
    M = Matrix(ZZ,nc,nr,c).transpose()
    if show:
        print M
    def r(n):
        s = range(n)
        s.reverse()
        return s
    k = nc - 1
    pivots = []
    for i in r(nr):
        for j in r(k):
            ck = c[k]; a = ck[i]
            cj = c[j]; b = cj[i]
            if b != 0:
                d,u,v = xgcd(a,b)
                if show:
                    print 'row '+str(i+1)+', cols '+str(j+1)+','+str(k+1)\
                        , 'xgcd:',u,'(',a,') + ',v,'(',b, ') = ',d
                ad = a/d
                bd = b/d
                c[k] = ck*u  + cj*v  # if a != d:
                c[j] = cj*ad - ck*bd
                M = Matrix(ZZ,nc,nr,c).transpose()
                if show:
                    print M
        if c[k][i] != 0:
            pivots.append((i,k))
            if c[k][i] < 0:  # this is necessary for the last pivot column,
                c[k] = -c[k] # since it cannot utilize xgcd to "normalize"
            k = k - 1
    for (i,k) in pivots:
        b = c[k][i]; assert(b > 0)
        if show:
            print 'pivot('+str(i+1)+','+str(k+1)+')='+str(b)
        for j in [k+1..nc-1]:
            q = floor(c[j][i] / b)
            c[j] -= c[k]*q
        M = Matrix(ZZ,nc,nr,c).transpose()
        if show:
            print M
    pivots.reverse()
    #if show:
    #    print 'pivots:', pivots
    return M, pivots

h,p = hnf(m,True)

Here is the output (for your test case):
hnf: 4x4
[10  0  1  0]
[-2 -2  2  1]
[-2 -1  3 -1]
[ 4 -6 -2  0]
row 4, cols 3,4 xgcd: 0 ( 0 ) +  -1 ( -2 ) =  2
[10  0  0 -1]
[-2 -2  1 -2]
[-2 -1 -1 -3]
[ 4 -6  0  2]
row 4, cols 2,4 xgcd: 1 ( 2 ) +  0 ( -6 ) =  2
[ 10  -3   0  -1]
[ -2  -8   1  -2]
[ -2 -10  -1  -3]
[  4   0   0   2]
row 4, cols 1,4 xgcd: 1 ( 2 ) +  0 ( 4 ) =  2
[ 12  -3   0  -1]
[  2  -8   1  -2]
[  4 -10  -1  -3]
[  0   0   0   2]
row 3, cols 2,3 xgcd: -1 ( -1 ) +  0 ( -10 ) =  1
[12  3  0 -1]
[ 2 18 -1 -2]
[ 4  0  1 -3]
[ 0  0  0  2]
row 3, cols 1,3 xgcd: 1 ( 1 ) +  0 ( 4 ) =  1
[12  3  0 -1]
[ 6 18 -1 -2]
[ 0  0  1 -3]
[ 0  0  0  2]
row 2, cols 1,2 xgcd: 0 ( 18 ) +  1 ( 6 ) =  6
[33 12  0 -1]
[ 0  6 -1 -2]
[ 0  0  1 -3]
[ 0  0  0  2]
pivot(4,4)=2
[33 12  0 -1]
[ 0  6 -1 -2]
[ 0  0  1 -3]
[ 0  0  0  2]
pivot(3,3)=1
[33 12  0 -1]
[ 0  6 -1 -5]
[ 0  0  1  0]
[ 0  0  0  2]
pivot(2,2)=6
[33 12 12 11]
[ 0  6  5  1]
[ 0  0  1  0]
[ 0  0  0  2]
pivot(1,1)=33
[33 12 12 11]
[ 0  6  5  1]
[ 0  0  1  0]
[ 0  0  0  2]

Sage also has a hermite_form function for matrices,
var('s')
for s in ['pari', 'pari0', 'pari4', 'padic', 'default', 'ntl']:
    print str(s)
    print m.hermite_form(algorithm=s)

but each implementation produces the matrix below
$$
\left(\matrix{
 2 & 0 & 2 & 27 \\
 0 & 1 & 1 & 64 \\
 0 & 0 & 3 & 45 \\
 0 & 0 & 0 & 66
}\right)
$$
which, as you point out in a comment, is arrived at by iterating over $i$ increasing, i.e. by pivoting from the top (left) downwards,
rather than decreasing from the bottom (right) upwards.
We can verify this directly using the transformation
$\phi:m\mapsto rm^tr=(rmr)^t$, for $r$ the (orthogonal) matrix
$$
r=
\left(\begin{array}{rrrr}
0 & 0 & 0 & 1 \\
0 & 0 & 1 & 0 \\
0 & 1 & 0 & 0 \\
1 & 0 & 0 & 0
\end{array}\right),
$$
which transposes $m$ by both diagonals. Our (Cohen's) HNF (which pivots from the bottom right upward) and sage's (which forms pivots from the top left downwards) can be obtained from each other by pre- and post- transforming by $\phi:$
r = Matrix(ZZ,4,4,[\
    floor(1/(1+n%3))*\
 (1-floor(1/(1+n%15)))\
    for n in range(16)])
print r
phi = lambda x: (r*x*r).transpose()
print phi(m).hermite_form() == phi(h)
phi(phi(m).hermite_form())

By the way, if you don't mind a little excursion, the group of transformations of square matrices generated under composition by the (self-inverse) maps $\color{red}{t}:m\mapsto m^t$ (transpose) and $\color{blue}{r}:m\mapsto rm$ (row reversal) or $\color{green}{c}:m\mapsto mr$ (column reversal) is in fact isomorphic to the dihedral group $D_4$ of the square. We can visualize this group by its action on a fixed matrix, e.g. the $2\times2$ matrix $e=\left(\matrix{1&2\\3&4}\right)$.

Each transformation is represented by, or identified with, (a path from $e$ to) a vertex. The order two elements consist of the surface edges and interior diagonals, while the rotations consist of the face diagonals, and their cosets partition the vertices into two intersecting tetrahedra. This is also a commutative diagram, and thus can be used to verify such formulas as $rc=cr$, $rt=tc$, and $ct=tr$ as well as $\phi=rtc=trc$ from above.
Anyway after some testing
ranks = [0,0,0,0,0]
passed = 0
failed = 0
for n in range(10^2):
    m = random_matrix(ZZ,4)
    h, p = hnf(phi(m))
    if phi(h) == m.hermite_form():
        ranks[len(p)] += 1
        passed += 1
    else:
        print 'Test FAIL!'
        print 'm:'; print m
        print 'm"=phi(m):'; print phi(m)
        print 'hnf(m"):'; print h
        print 'hnf(m")":'; print phi(h)
        print 'm.hnf():'; print m.hermite_form()
        failed += 1
        break
print n+1,'tests performed:',passed,'passed,',failed,'failed.'
print 'rank counts:',ranks

I found that I needed to sometimes correct the sign of the first pivot column, and incorporated this into the hnf function above (along with making the print output optional but off by default). In any case, in sage, you can just define $\phi$ and use phi(phi(m).hermite_form()); after all, why should we add a seventh implementation (except to learn!)?
Comment: My pleasure! I also discovered an error after some testing: the first pivot column could have a negative pivot value, since it couldn't benefit from xgcd to normalize itself. Please see the improved post for the new code.
Comment: Thanks for that!  I also suspected that the minimal condition isn't necessary since, like you mentioned, XGCD does a good job of returning the minimal solution anyway!  Note that the algorithm that I have provided is an *interpretation* of that provided by Cohen, and is therefore likely to contain errors.  I have, however, discovered and compensated for errors in this and other algorithms provided.  Also, thank you for providing the Sage source code and, especially its output. It will give me a guideline to correcting the source code which I have written in Magma.
Comment: Discovered the problem!  At the step where I was reducing the lower triangular portion of the matrix, I was forgetting to divide the coefficients by the GCD of both a and b.  I would have NOT realised this if I had not looked at your coding example, which divides the coefficients by the GCD on separate lines.  You are also right about XGCD returning the *minimal* values.  As for the implementation of HNF provided by Sage, you'll find that it commences from the top-left corner of the matrix, rather than the bottom right corner of the matrix described by Cohen's algorithm.
Comment: If you transpose the matrix on the diagonal commencing from bottom left to top right, and take the HNF of the resulting matrix under Sage, you'll find that this produces the same results as this algorithm (but, you probably knew that).  Once again, thank you for your help!  Really appreciated.
Comment: Transposing on that diagonal is a similarity transformation by the permutation (and therefore orthogonal) matrix $r$ which reverses columns (rows) upon right (left) multiplication: `r = Matrix(ZZ,4,4,[1 if n%3==0 and n%15!=0 else 0 for n in range(16)]); print latex(r); r` $$\left(\begin{array}{rrrr}
0 & 0 & 0 & 1 \\
0 & 0 & 1 & 0 \\
0 & 1 & 0 & 0 \\
1 & 0 & 0 & 0
\end{array}\right)$$ To get our matrix, we have to perform a pair of transpositions, the regular one, and the conjugate by $r$ (which commute with each other): `r*(r*m.transpose()*r).hermite_form().transpose()*r` Thanks for the fun Q!
Comment: Thank you for your support!
Comment: Thanks for the update!  Probably more than I needed to know, but invaluable nevertheless.  I've actually moved on to implement the next HNF Algorithm in Cohen's text that reduces the calculations modulo the determinant of the matrix.  This method was contributed by Hafner and McCurley.  While the methods of reduction remain pretty much the same, it handles the pivot in a slightly different way.  If you're up to the challenge, I can email you the Magma code of what I have done thus far.  I can also email you my solution to this exercise.
Comment: Unfortunately, my version of Sage is a bit outdated: it's using version 2.6 of Python, whereas I've upgraded to version 2.7.  While I have no problems writing code, I do have a problem loading the sample solution that you posted.  As this is not a priority at the moment, upgrading Sage is on my list of things to do.
Comment: If you're on Linux (and not using one of those VM's), just type `sage -upgrade` from the command line an hour before you go to bed and then make sure you hit the confirmations (I don't think there were many). If you want, you can back up all your worksheets at once, first, and make sure you remember your password and that all your worksheets are stopped so their pickle files don't get corrupted.
Comment: Thanks for that! I'll give it a try later today.  Note that I am running my software on a MacBook Pro with the latest OS X 10.7.3 Lion.
Comment: Just to let you know that I have posted a similar question concerning the computation of HNF using the Extended Euclidean Algorithm modulo the determinant of the matrix.  Unlike the other two algorithms that I have successfully implemented, this does not specifically cater for the zero case, nor does it specifically reduce the lower triangular portion of the matrix.
Comment: As for the Sage upgrade, I did not do this as there are issues with the upgrade under the latest version of Mac OS X 10.7.3.  I did manage, however, to upload the worksheet using Sage's notebook().  Not sure why it didn't allow me to do this on the command line.  Either way, all works well.


Solving inequality for convex functions.

Question:
A function $f : I \rightarrow \mathbb R$ on an interval $I$ is convex
  if $f((1-t)x+ty)\le (1-t)f(x)+tf(y), \forall x,y \in I, t \in [0,1]$
Assume now that $I$ is an open interval.  Show that if $f$ is convex
  then for each $c \in I$ there exists $m \in \mathbb R$ such that $m(x
 − c) + f(c) \le f(x)$ for all $x \in I$ , and if in addition $f$ is
  differentiable at $c$ then $f'(c)$ is the unique $m$ that works. In
  general, must m be unique?

(we have previously shown that convex functions are continuous)
I think I'm missing the point of the question.
Surely, by the mean value theorem
we can find some $y\in (x,c)$ such that $f'(y)=\frac{f(x)-f(c)}{x-c}$ setting $m=f'(y)$ we get $m=\frac{f(x)-f(c)}{x-c}, m(x-c)+f(c)=f(x)$ such an $m$ fits the inequality we're asked to show.
I also cannot see why $f'(c)$ would be the unique solution or why we would ever get a unique solution of the inequality at all for that matter.
Clearly I'm misunderstanding the question, so if someone could point out where I would appreciate that.
Thank you
Comment: Have you tried drawing a picture? Try drawing a line $m(x-c)+f(c)$ (i.e. a line through $(c,f(c))$) that is never above the graph of $f$ on the interval $I$. If $f$ is differentiable at $c$ then there is only one such line: the tangent line (i.e. the line with $m=f'(c)$).
Comment: I should have said: "if $f$ is differentiable and convex then there is only one such line". You did not use convexity yet in your attempt at answering the question.
Comment: Actually now that I think about it more, what's the significance in it being convex?
Comment: The question is really getting you to show that for differentiable functions convexity can be characterized as the property that the graph of the function never lies below its tangent lines.
Answer: Here is an idea how you can prove it.

Take a point $a\in I$ to the left of $c$ and a point $b\in I$ to the right, i.e. $a<c<b$, and draw two secant lines: $\ell_a$ through $(a,f(a))$ and $(c,f(c))$ and $\ell_b$ through $(c,f(c))$ and $(b,f(b))$. Denote $k_a$ and $k_b$ their slopes respectively.
By convexity, the graph of $f$ is under $\ell_a$ on $[a,c]$ and above it outside $[a,c]$. Similarly, the graph of $f$ is under $\ell_b$ on $[c,b]$ and above it outside $[c,b]$. It gives, in particular, that $k_a\le k_b$.
Let $a\to c^-$. The slope $k_a$ is monotonically increasing (again by convexity) and bounded from above by $k_b$, hence, converges to some value $M_1$. Similar argument when $b\to c^+$ gives that $k_b\to M_2$.
Since $k_a\le k_b$ we get $M_1\le M_2$. Now any $m\in[M_1,M_2]$ works.
If $f$ is differentiable at $c$ then $M_1=M_2$.

P.S. The interval $[M_1,M_2]$ is called subdifferential of $f$ at $c$.

In your argument, the point $y\in (x,c)$ you found by the mean value theorem depends on $x$, hence $m=f'(y)$ also does. However, the value of $m$ in the claim must be the same for all $x\in I$.
Answer: As smcc pointed out in their comment, we can think of $y=m(x-c)+f(c)$ as a line passing through the point $\left(c, f(c)\right)$ with slope $m$. Let's assume that this line has a second intersection with the curve $f(x)$: $\left(b, f(b)\right)$. Then, we can rewrite the equation of the line using the fact that $\displaystyle m=\frac{f(b)-f(c)}{b-c}$: $$y=\frac{f(b)-f(c)}{b-c}(x-c)+f(c)$$
We can use the substitution $\displaystyle t=\frac{x-c}{b-c}$ to get the following equation:
$$y=tf(b)+(1-t)f(c)$$
Now, we use the convex condition which was mentioned in the OP:
$$f(tb+(1-t)c)\leq tf(b)+(1-t)f(c) \text{ for }t\in[0,1] $$
Unless $f(x)$ is the equation of a line, equality is only reached when $t=0$ or $t=1$. Ergo, for all $x$ between $b$ and $c$, $m(x-c)+f(c)>f(x)$. The only case in which this does not happen is when the line $y=m(x-c)+f(c)$ has no second intersection. In other words, $m=f'(c)$.
In the case that $f(x)$ is a line, the question should be easy to answer.
Comment: @user307463 Although I appreciate that you've accepted my answer, I would like to point out that I never proved that such an $m$ exists for general convex functions. You should read through A.G.'s answer for a proof of existence.


Python - Flask method call inside if statement

Question: I am new to flask. I am trying to call a method defined in the python file. The method is setup properly in the python file (tested). Now in the HTML file I want to call this method within an if statement. So in Java it would be something like:
if (allowAccess() == 1){...}

I have tried the following:
{% if {{ allowAccess() }} == 1 %}

test message

{% endif %}

But i get the error:

expected token : got }
Comment: Can you try to with `{% if allowAccess()  == 1 %}` ?
Comment: @RajaSimon I thought I tried this earlier, anyway I tried it again and it worked thanks. If you put it as answer ill accept it
Answer: You don't have to put {{ inside the conditional templates statement... So answering the question would be...
{% if allowAccess() == 1 %}


Call to a member function set_charset() on a non-object

Question: Here is the source of my connect_database.php file.
<?php
include($_SERVER['DOCUMENT_ROOT'].'/material_wallpaper/includes/variables.php');
$connect->set_charset('utf8');
?>

and here is the error that comes.

Fatal error: Call to a member function set_charset() on a non-object in /home/yukioo/public_html/wallo/includes/connect_database.php on line 3
Comment: you need to post your full code, including your db connection and query. This isn't a PHP issue, it's a database issue.
Comment: you edited your question from utf7 to utf8 without marking it as an edit.
Comment: there isn't enough code to support the question/error to reopen
Answer: N.B.: OP edited the question from utf7 $connect->set_charset('utf7'); to utf8 without marking it as an edit.
Querying a MySQL database does not use utf7, it uses utf8. (see my edit below)

mysqli_set_charset($link, "utf8");

http://php.net/manual/en/mysqli.set-charset.php

mysql_set_charset('utf8',$link);

http://php.net/manual/en/function.mysql-set-charset.php

PDO

$pdo = new PDO("mysql:host=localhost;dbname=world;charset=utf8", 'my_user', 'my_pass');

http://php.net/manual/en/mysqlinfo.concepts.charset.php

UTF-7 related link:

$str = mb_convert_encoding($str, "UTF-7", "EUC-JP");

http://php.net/manual/en/function.mb-convert-encoding.php

Edit:
$connect and the error you received suggests the querying of a database and isn't a PHP issue as you tagged your question with.
The error may be related to your connection and/or a non-object in your query that you did not post.
If you're mixing MySQL APIs such as PDO to connect with and mysqli_ or mysql_ to query with, those do not intermix.

You need to use the same API from connection to query.

In order for me to help any further, you will need to post your full code, including the connection method and its related query.
Comment: whoever thinks MySQL uses utf-7, comment and show me a link
Comment: The issue is that `$connect` isn't an object; the character set isn't an issue.
Comment: @EdCottrell I had that feeling also. I will make another edit
Comment: I first created a database using mySQL database wizard of Godaddy. Then i went to file manager uploaded my files and when i opened it. It gave me this error. Please can we contact over something like teamviewr so that you can help me fix the problem otherwise tell me what else you need to know ?
Comment: @OsamaBaloch I don't have/use teamviewr. why can't you post more relevant code?
Comment: @Fred-ii- Ok Here is the other code.
Close_Database.php`close();
?>`

and here is the Variables.php
``
Comment: @OsamaBaloch you need to post that in your question and edit it http://stackoverflow.com/posts/32833059/edit rather than putting code in comments. including for `variables.php` if relevant.
Comment: @OsamaBaloch and this `mysql_error()` that needs to be `mysqli_error($connect)` you cannot mix `mysqli_` with `mysql_`. Keep it all the same being `mysqli_`. Those do not intermix together. Not to mention the query itself if you are querying the database.
Comment: @OsamaBaloch by the way, in your pasted code above, there are two unicode characters following `variables.php')` <= being `` and right before the `;` semi-colon, if that is part of your real code and that will break your code. copy/paste this exactly from here `close(); ?>` and add error reporting ot your code and to display them. visit http://php.net/manual/en/function.error-reporting.php
Comment: Seems like someone crapped in someone's corn flakes this morning. I hope it was me. Thanks for the drive-by downvote, and have a very beautiful day.


How to set remoteId and server certificate check Strongswan IKEv2 ubuntu 18.04

Question: I am a new member in IKEv2, and I want to install an IKEv2 vpn on an ubuntu 18.04 server,  I did it through this tutorial , but I have a couple of questions.
First how can I configure its remote id, because I use a url for that in my ios app.
The second is how can I omit using cert file in client, I mean I want to login it with just username and password and I don't want to trust the cert in client devices, because I use it in an app.
Comment: What remote ID exactly? What do you mean with "use a url for that"? The tutorial you referenced doesn't use client certificates, so not sure if your second question aims at the self-signed server certificate. Just create your server certificate via Let's Encrypt if you want to avoid installing your CA on the clients.
Comment: Just configure that domain name as identity in _leftid_ then (must match a subjectAltName extension in the certificate). And as I said, get your certificate for your server from [Let's Encrypt](https://letsencrypt.org/) (or a commercial CA) so you don't have to install anything on the client.
Comment: @ecdsa thanks for comment, I installed it on an ubuntu server and I use Ip for vpn server address, but I want to use a url ( ex: sec.mydomain.com) for all my servers remote id. And the next I don't want to install the cert in clients, because I must use it on an ios app
Comment: @ecdsa thank you so so much, if you can put it on the answer for allowing me to accept as the right answer.
Answer: To use a domain name instead of an IP address as identity of the server, just configure it as local identity there (e.g. in leftid in ipsec.conf). The configured identity has to be contained in a subjectAlternativeName extension in the server certificate.
To avoid having to install any certificates on the clients, use a server certificate issued by an already trusted Certificate Authority (CA). You can do so for free via Let's Encrypt, or alternatively buy a certificate from a commercial CA.