Modify Nginx 301 response body

Question: So, when doing curl -i http://example.com on my server, I get this response in body:

<title>301 Moved Permanently</title>

301 Moved Permanently

nginx

It shows that I'm running nginx and I'd like to remove this information.
Here's my nginx.conf redirect to HTTPS (together with my try to change 301 response body):
server {
    listen       80;
    server_name  localhost;
    error_page 301 = /301.html;
    location /301.html {
        return 301 "
use https
";
    }
    return 301   https://$host$request_uri;
}

Any idea how to change 301 response body?
Comment: One option is to edit source code: https://github.com/nginx/nginx/blob/master/src/http/ngx_http_special_response.c#L60
But it seems like an overkill.
Answer: There's a great post about custom error pages at the following URL. The following is an abbreviated version of how it can be used to remove nginx branding from the 301 and 302 HTTP responses.

One NGINX error page to rule them all

nginx.conf
http {

  map $status $status_text {
    301 'Moved Permanently';
    302 'Found';
  }

  server {

    listen 126.96.36.199;
    server_name _;

    error_page 301 302 /error.html;

    location = /error.html {
      ssi on;
      internal;
      auth_basic off;
      root /var/website/custom_error_pages;
    }

    root /var/website/empty_dir;

    location / {
      return 301 https://www.website.com$request_uri;
    }
}

/error.html

  <title>www.website.com</title>

     -

Answer: create file 301.html, this file should contains content which you want to display. If path to file is /usr/share/nginx/html/301.html adjust config to:
server {
    listen       80;
    server_name  localhost;

    location / {
        error_page 301 = /301.html;
        return 301   https://$host$request_uri;
    }

    location /301.html {
        root /usr/share/nginx/html/;
    }

}

And this will return your custom 301.html file
Comment: This actually changes HTTP code to 200 which stops the actual redirect. A very bad solution.
Answer: Try placing your return 301 https://$host$request_uri; directive inside a location / { block. Just that one line.
server {
    listen       80;
    server_name  localhost;
    error_page 301 = /301.html;
    location /301.html {
        return 301 "
use https
";
    }
    location / {
        return 301   https://$host$request_uri;
    }
}

Comment: I dont think this will work since second param to 301 modifies location header instead of body.
Answer: It is a bit hacky and confusing, but it works:
server {
    listen       80;
    server_name  localhost;
    error_page 301 /_use_https_;

    location = /_use_https_ {
        internal;
        # We have to return 200 to modify content.
        # If we would use 301 we would modify Location header.
        # Dont worry, HTTP status will be 301.
        return 200 "
use https
";
    }

    # redirect has to be enclosed in location
    location / {
         return 301 https://$host$request_uri;
    }
}

Answer: The accepted answer by phbits is great, but there are some pitfalls if you want to apply this to a reverse-proxy situation with proxy_intercept_errors on, so that the error page also replaces errors sent by the backend (related mailing list thread).
If your backend replies with a 301 or 302, nginx will drop the Location: header when serving the error page to the client, despite retaining the correct status code. So the client will see a page that says
302 Found
 but will not be redirected. The solution is to re-add the Location header from the upstream response using add_header Location "$upstream_http_location".
This took me some time to figure out, so below is my complete setup, based on Adriaan's Blog, in case someone runs into the same situation in the future.
/etc/nginx/sites-enabled/myserver.conf
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;

    server_name example.com;

    include snippets/errorpage.conf;

    location / {
        proxy_pass http://127.0.0.1:7080/;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

/etc/nginx/snippets/errorpage.conf
error_page 01 302 303 304 307 308 400 401 402 403 404 405 406 407 408 409 410
    411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429
    431 451 500 501 502 503 504 505 506 507 508 510 511 /error.html;

location /redirect-test {        # Just for testing
    return 302 /foo-bar;         # Use nc example.com 80 <<<$'GET /redirect-test HTTP/1.0\r\nHost: example.com\r\n\r\n'
}

proxy_intercept_errors on;
location = /error.html {
  ssi on;
  internal;
  auth_basic off;
  root /var/www/default;
  add_header Location "$upstream_http_location";
}

/etc/nginx/conf.d/status-codes.conf
map $status $status_text {
  301 'Moved Permanently';
  302 'Found';
  303 'See Other';
  304 'Not Modified';
  307 'Temporary Redirect';
  308 'Permanent Redirect';
  400 'Bad Request';
  401 'Unauthorized';
  402 'Payment Required';
  403 'Forbidden';
  404 'Not Found';
  405 'Method Not Allowed';
  406 'Not Acceptable';
  407 'Proxy Authentication Required';
  408 'Request Timeout';
  409 'Conflict';
  410 'Gone';
  411 'Length Required';
  412 'Precondition Failed';
  413 'Payload Too Large';
  414 'URI Too Long';
  415 'Unsupported Media Type';
  416 'Range Not Satisfiable';
  417 'Expectation Failed';
  418 'I\'m a teapot';
  421 'Misdirected Request';
  422 'Unprocessable Entity';
  423 'Locked';
  424 'Failed Dependency';
  425 'Too Early';
  426 'Upgrade Required';
  428 'Precondition Required';
  429 'Too Many Requests';
  431 'Request Header Fields Too Large';
  451 'Unavailable For Legal Reasons';
  500 'Internal Server Error';
  501 'Not Implemented';
  502 'Bad Gateway';
  503 'Service Unavailable';
  504 'Gateway Timeout';
  505 'HTTP Version Not Supported';
  506 'Variant Also Negotiates';
  507 'Insufficient Storage';
  508 'Loop Detected';
  510 'Not Extended';
  511 'Network Authentication Required';
  default 'Something is wrong';
}

/var/www/default/error.html
<!DOCTYPE html>

    <meta charset="utf-8">
    <title>example.com Error</title>

/etc/nginx/conf.d/no-headers.conf
#more_clear_headers Server;   # Remove Server header, or
more_set_headers 'Server: haproxy'; # Or whatever

Modify Nginx 301 response body


First Acoustic Guitar

Question: (Though, this question may have been asked in past. I didn't find it, hence this new question)
I would like to play the guitar for myself as a hobby. But I am not sure, which guitar I need to go with as a beginning step. Will it be acoustic or electrical or any other ?
As this will be my first baby step into "Guitar-world", I need help to choose right guitar for me. Also, to begin with, which books or online/offline self-explanatory study material are available to learn.
(My budget will be approx. Rs. 5000)
All suggestions are welcome.
Answer: It depends on what you're playing.
Typically, I would suggest you get an acoustic guitar. It's simple, portable, and versatile. But, if you're onto rock or metal, I would suggest you to get an electric guitar.
The acoustic guitar itself splits into nylon strings and steel strings. The nylon strings let you enjoy your style on jazz and classical. The steel strings let you enjoy your style on folk (and/or if you want to experiment with jazz or reggae). Some people said that the acoustic guitar got a big body, so it's not really that comfy on your grip. But you can buy a slim guitar, which sometimes costs a bit more.
The electric guitar itself splits into hollow body and solid body. Which you may find hollow body played on jazz and old classic blues and rock songs. While solid body you may find in pop culture, rock and other stuff like that. You may find the electric guitar is smaller and more skinny than the acoustic guitar to you to grip. But on some solid body guitars (like Les Paul), it may have more weigh.
It's up to you, but if you're just learning and want this as a hobby, I'm recommending the acoustic guitar.

First Acoustic Guitar


Porque '08' gera erro dentro do Eclipe?

Question: Tenho uma classe com 'int = dia, mes e ano' e na outra estou chamando eles. Só que me chamou atenção foi que, quando coloco o valor '08' diz

'The literal 08 of type int is out of range'.

mas quando coloco 07, compila sem problemas.
Vou inserir os códigos:
    public class Data {
    int dia;
    int mes;
    int ano;
}

    public class DataTeste {
    public static void main(String[] args) {
        Data nascimento = new Data();
        **nascimento.dia = 08;**--este valor da erro, mas 07 p/baixo valida.
        nascimento.mes = 03;
        nascimento.ano = 1989;
    }
}

Comment: Primeiramente, porque criar uma classe para data se o java tem tantas classes para isso?
Comment: É que estou seguindo um exemplo de uma vídeo aula...
Answer: O problema é que na maioria das linguagens um número inteiro literal iniciado por zero é interpretado como sendo uma valor octal, é por isso que até o 07 dá certo por não existir uma representação em octal desses números o certo seria você nunca iniciar um número com 0.
Comment: Entendi...
MUITO OBRIGADO DE VERDADE
Answer: Qualquer número prefixado com 0 é considerado octal. Os números octais só podem usar os dígitos 0 a 7, assim como o decimal pode usar 0-9 e o binário pode usar 0-1.
// octal to decimal
01  // 1
02  // 2
07  // 7
010 // 8
020 // 16
// octal to binary (excluding most significant bit)
01  // 1
02  // 10
07  // 111
010 // 1000
020 // 10000
Ou seja, internamente o seu "dia" esta tendo 3 números.
Comment: Muito obrigado, entendi o porque do erro

Porque '08' gera erro dentro do Eclipe?


Difference for  in method and variable declaration

Question: Given:
import java.util.*;

public class Hancock {
    //insert code here
        list.add("foo");
    }
}

Which two code fragments, inserted independently at line 5, will compile without warnings? (Choose two)
A. public void addString(List list) {
B. public void addString(List<String> list) {
C. public void addString(List<? super String> list) {
D. public void addString(List<? extends String> list) {

Correct answers are B & C.
Answers A and B are quite clear for me. For the answers C & D i know which way the inheritence is going, however i cannot understand why answer D does not compile in Eclipse while all others do (A with warrning about generic, B & C without warrings).
Error in Eclipse for answer D is The method add(capture#1-of ? extends String) in the type List<capture#1-of ? extends String> is not applicable for the arguments (String).
On the other hand this compiles:
public void addString() {
    List<? extends String> list1 = new ArrayList<String>();
    List<? super String> list2 = new ArrayList<String>();
}

Why? Why <? super String> does not compile in method declaration while it does compile in variable declaration.
I know that String is final class and cannot be extended by any other class but that does not explain to me what is going on here.
Comment: possible duplicate of [difference between  and  in Java](http://stackoverflow.com/questions/4343202/difference-between-super-t-and-extends-t-in-java)
Comment: possible duplicate of [Java Generics: What is PECS?](http://stackoverflow.com/questions/2723397/java-generics-what-is-pecs)
Answer: First, let's see answer C:
public void addString(List<? super String> list) {
    list.add("foo");
}

This method declaration says that you will be allowed to pass List objects which are parametrized by some super class of String, for example String or Object. So:

If you pass List<String> the list.add("foo") will be perfectly valid.
If you pass List<Object> the list.add("foo") will be perfectly valid, because "foo" is a String (and you can add a String to a  List<Object>).

This means that answer C is correct.

Lets now see answer D.
If you have a method declaration like this:
public void addString(List<? extends String> list) {

}

this means that you will be able to pass List objects parametrized by some unknown subtype of String. So, when you do list.add("foo"); the compiler won't be aware if the provided object has a type that matches the unknown subtype of String and therefore raises a compile-time error.

When you have:
public void addString() {
    List<? extends String> list1 = new ArrayList<String>();
    List<? super String> list2 = new ArrayList<String>();
}

This fragment compiles fine, because list1 is defined to hold List objects that are of some unknown subtype of String, including the String itself, which is why it's valid.
The problem is that you won't be able to add anything, except null.
As for list2, the variable can hold List objects which are parametrized by some super-type of String, including the String itself.

More info:

What is PESC?
What is the difference between super and extends in Java wildcards?
Comment: Read carefylly what I've written and tell me where do I say that you can add `Object` instances? I've said that you can pass `List`, not that you can add `Object`s :)
Comment: I disagree. Try `List list = new ArrayList();` and pass it to the `addString` method.
Comment: Actually your explanation of "C" is wrong. You *cannot* add an `Object` to a `list`. The list *could* be a `List` during runtime, so this will fail.
Comment: your example adds a `string` (which is the only valid thing you can add). If you try to add an object, you'll get: `no suitable method found for add(Object) myList.add(new Object());`
Comment: touche. How did you remove that comment about "add" - even from the revision history :P
Comment: I think that PECS is the explanation i was looking for.
Answer: Firstly, generics don't care that String is final. They work the same way for final and non-final classes.
With that in mind, it should be apparent why D is not allowed - if it was, you could do this:
void test() {
    List<Integer> integers = new ArrayList<Integer>();
    addADouble(integers);
    int a = integers.get(0); // ????
}

void addADouble(List<? extends Number> list) {
    list.add(new Double(5.0));
}

List<? extends Number> is a "List of something that extends Number, but you don't know exactly what it's a List of." - it might be a List<Double>, or a List<Integer>, or a List<Number>, or a List<YourCustomSubclassOfNumber>, so you can't add anything to it because you don't know if it's the right type.
Comment: @Dave I guess my explanation sucks then. You can't add a `Double` to a `List`. To use a different example: you can't do `List list = new ArrayList(); list.add(new Double(5.0));`
Comment: now i may complicate it a bit more ;)
`System.out.println(new Double(5.0) instanceof Number);` returns `true`, so why `list.add(new Double(5.0));` does not compile in you code with simmilar error i've described in question?
Does `?` in `` stand for something that returns `true` from above print?


Polymer: Layout screwed up in Firefox, fine in Chrome

Question: So, I am aware the web components, Shadow DOM and such is only implememted natively in Chrome today.
For support in Firefox, Polyfill is needed. According to the website, Polymer has polyfill support in Firefox:
https://www.polymer-project.org/resources/compatibility.html
but when I have made a very simple page, it looks completely screwed up i Firefox. But, if I try the Polymer website in Firefox, it works there without any obvious problems.
Test URL: http://misc.snapcode.se/polymer/
Here is how my test-site looks in Chrome:

and in Firefox:

The code can be seen below.

But they say that Firefox has Polyfill support, and it supports CSS, so why is the layout/design so screwed up?
How come they get their own site to work in Firefox, but a super-simple site I build is screwed up?
If I try out the paper-dropdown in FF, it works fine on their demo site, but if I use the paper-dropdown on my own site, constructed the same way as described on the polymer website, its completely screwed up. Why?

What am I missing?

EDIT 1
I found out that to get the header panel "right" in Firefox, I have to remove the CSS in index.php for the div {...}:

That seems to me like the Shadow DOM isnt working correctly, even though I have imported webcomponents.js, which should be the Polyfill needed.

EDIT 2
I have looked in Firefox using Firebug and I can see the following:

As I can see it, webcomponents.min.js is imported (I tested different js files), and there is some stuff talking about ShadowDOMPolyfill. So, it is even weirder now I think.

EDIT 3
I debugged Firefox using Firebug, image below. As I see it, ShadowDOM using Polyfill is indeed detected and used. Do you agree? =)

index.php

<!DOCTYPE html>

    <title>LEO</title>

    <link rel="import" href="/components/font-roboto/roboto.html">
    <link rel="import" href="/components/core-header-panel/core-header-panel.html">
    <link rel="import" href="/components/core-toolbar/core-toolbar.html">
    <link rel="import" href="/components/core-icons/core-icons.html">
    <link rel="import" href="/components/paper-shadow/paper-shadow.html">
    <link rel="import" href="/components/paper-button/paper-button.html">
    <link rel="import" href="/my-components/logout-button/logout-button.html">
    <link rel="import" href="/my-components/assignment-card/assignment-card.html">

<?php
session_start();
if (!isset($_SESSION['session_userId']))
{
    echo "Not logged in";

}
?>
<core-header-panel flex layout>
    <core-toolbar>

LEO 1

            <logout-button></logout-button>

    </core-toolbar>

      <assignment-card></assignment-card>

</core-header-panel>

assignment-card.html

<link rel="import" href="/components/polymer/polymer.html">
<link rel="import" href="/components/core-icons/core-icons.html">
<link rel="import" href="/components/paper-button/paper-button.html">
<link rel="import" href="/components/core-item/core-item.html">
<link rel="import" href="/components/core-menu/core-menu.html">
<link rel="import" href="/components/core-dropdown/core-dropdown.html">
<link rel="import" href="/components/core-dropdown-menu/core-dropdown-menu.html">

<link rel="import" href="/components/paper-item/paper-item.html">
<link rel="import" href="/components/paper-menu/paper-menu.html">
<link rel="import" href="/components/paper-dropdown/paper-dropdown.html">
<link rel="import" href="/components/paper-dropdown-menu/paper-dropdown-menu.html">

<link rel="import" href="/components/core-ajax/core-ajax.html">
<link rel="import" href="/components/core-tooltip/core-tooltip.html">

<polymer-element name="assignment-card">
    <template>

                <core-label >Starttid: 14:13</core-label>
                <core-label flex></core-label>
                <core-label >Uppdrags-id: 13213241</core-label>

<core-label>Kertin Karlsson,</core-label>

                <paper-dropdown-menu raised label="-Välj" style='background: #fff; padding: 5px; margin: 0px; margin-right: 15px; ' flex>
                    <paper-dropdown class="dropdown" layered="true">
                        <core-menu class="menu">
                            <template repeat="{{assistant in assistants}}">
                                <paper-item name="{{assistant.id}}">{{assistant.name}}</paper-item>
                            </template>
                        </core-menu>
                    </paper-dropdown>
                </paper-dropdown-menu>
                <paper-button raised>Tilldela</paper-button>

        <core-ajax
                   id="coreAjax1"
                   url="http://192.168.1.108/relay.php"
                   method="post"
                   params='{{json}}'
                   handleAs="json"
                   on-core-response="{{handleResponse}}">
        </core-ajax>
    </template>

</polymer-element>

logout-button.html

<link rel="import" href="/components/polymer/polymer.html">
<link rel="import" href="/components/core-icons/core-icons.html">
<link rel="import" href="/components/paper-button/paper-button.html">
<link rel="import" href="/components/core-ajax/core-ajax.html">

<polymer-element name="logout-button">
    <template>

        <core-ajax
                   id="coreAjax1"
                   url="http://192.168.1.108/logout.php"
                   on-core-response="{{handleResponse}}">
        </core-ajax>

        <paper-button raised id="btnLogout" on-click="{{onLogoutClicked}}">Logga ut
                <core-icon icon="highlight-remove"></core-icon>
        </paper-button>
    </template>

</polymer-element>
Comment: It would be far easier for people to answer if you included the URL to the test page.
Comment: URL: http://misc.snapcode.se/polymer/
Answer: The problem persist because firefox does not create shadow DOM but it displays shadow content directly. So the following snippet screws the overall view:

paper-shadow {
    width: 300px;
    background: #FFF;
    margin: 10px;
    padding: 10px;
}
div {
    padding: 10px;
    margin: 10px;
}

If you remove that code and add some specific code then it will work. The snippet for index.php with specific code is given below.

<!DOCTYPE html>

    <title>LEO</title>

    <link rel="import" href="/components/font-roboto/roboto.html">
    <link rel="import" href="/components/core-header-panel/core-header-panel.html">
    <link rel="import" href="/components/core-toolbar/core-toolbar.html">
    <link rel="import" href="/components/core-icons/core-icons.html">
    <link rel="import" href="/components/paper-shadow/paper-shadow.html">
    <link rel="import" href="/components/paper-button/paper-button.html">
    <link rel="import" href="/my-components/logout-button/logout-button.html">
    <link rel="import" href="/my-components/assignment-card/assignment-card.html">

<?php
session_start();
if (!isset($_SESSION['session_userId']))
{
    echo "Not logged in";

}
?>
<core-header-panel flex layout>
    <core-toolbar>

LEO 1

            <logout-button></logout-button>

    </core-toolbar>

      <assignment-card></assignment-card>

</core-header-panel>

Comment: Polyfill support for Shadow DOM means the DOM API methods (querySelector, getElementById, etc) are polyfilled and work as expected. It's impossible to polyfill CSS scoping, so the Shadow DOM polyfill just updates the content in your CSS selectors to replace Shadow DOM bits with your element name. ex: :host .blah gets translated to x-foo .blah.
Comment: I have already discovered that the div-css screws it up, but the question is why? It is supposed to work using polyfill, right?
Comment: div-css screws up because firefox does not put the content in shadow DOM. It puts content out of shadow DOM and that's why the div style is applied to them.
Comment: So I have realized, but then I dont understand the table here:
https://www.polymer-project.org/resources/compatibility.html

There it clearly says that Firefox has Polyfill support for ShadowDOM, right?
Comment: I am getting the same problem with Safari as well.


How to create a menu for current book

Question: I want to create a menu that will be shown on the node if it is part of a book. For example like this:

That menu should be shown for all books, which means I don't want to have a separate block for each book in the system.
I am not interested in how to implement accordion functionality, I just need a way to get menu items for all book elements of current node.
Book module provides a block with books navigation, but I don't want all books to be visible in this menu.
Comment: Indeed, interesting question. In D6, the flat book module (http://drupal.org/project/flat_book) makes the trick. Unfortunately, this module is not port to d7 and even the d6 version seems not maintain anymore. but maybe it's still a good starting point.
Answer: I was surprised that I couldn't find a contrib module that does this out of the box so I created a simple one.
Here it is on Github too:
https://github.com/balsama/bookoblock (bookoblock = "Book Outline Block")
Info file (bookoblock.info):
name = Book Outline Block
description = "Provides a block containing an outline of the current book."
package = Other
core = 7.x
dependencies[] = book

Module File (bookoblock.module):
<?php
/**
 * Define the Book Outline Block/
 * Implementation of hook_block_info()
 */
function bookoblock_block_info() {
  $block['book_outline'] = array (
    'info' => 'Book Outline',
    'cache' => DRUPAL_CACHE_PER_PAGE,
  );
  return $block;
}

/**
 * Define the contents and title of the block/
 * Implementation of hook_block_view()
 *
 * Returns a renderable block array containing the current book's outline if
 * the node is part of a book.
 *
 * Returns NULL if the current menu item is not part of a book heirarchy.
 */
function bookoblock_block_view() {
  if ($book = bookoblock_is_book_node()) {
    $tree = menu_tree_all_data(book_menu_name($book['bid']));

    // If we pass 1 as the second param, we just get the top level node in the
    // book. Since the key will be the top level mlid, we'll have to use
    // ['p1'] in the $book array (which contains the top mlid) when we use it
    // for the block title.
    $book_name = (book_toc($book['bid'], 1));

    // Build the block title and contents
    $block['subject'] = $book_name[$book['p1']];
    $block['content'] = menu_tree_output($tree);

    // Return the block
    return $block;
  }
  // If the current node isn't part of a book, just return nothing.
  return NULL;
}

/**
 * Checks to see if current node is part of a book.
 * Returns book array if it is part of a book, FALSE is not.
 */
function bookoblock_is_book_node() {
  if ((arg(0) == 'node') && (is_numeric(arg(1)))) {
    $node = menu_get_object();
    if (isset($node->book)) {
      return $node->book;
    }
  }
  return FALSE;
}

Just drop those two files into a folder named bookoblock inside your modules directory and enable the module. You'll now see a new block in the block admin page named Book Outline. That block will only appear on node pages where the node is part of a book. It contains a heierarchical menu of the entire book and it's title is the root node's title.
Comment: Just change that line to this: `$tree = menu_tree_all_data(book_menu_name($book['bid']), NULL, 2);` where "`2`" = the number of levels you want to show. It's also be pretty easy to put that setting in a module admin page.
Comment: The latest version of the module on github adds a configuration page where you can set the depth of the menu (and an uninstall file to remove that var if you ever uninstall the module). https://github.com/balsama/bookoblock
Comment: This looks promising, I will try it and let you know.
Comment: Just one question, do you know how to limit the number of levels? I suspect I will have to change this line: `$tree = menu_tree_all_data(book_menu_name($book['bid']));`.
Comment: @Bulat, this module is pending full project status here: http://drupal.org/node/1930370. Please consider reviewing it and setting it to RTBC if you would like support for it to be continued.
Comment: thank you for the update. can I suggest one major change. I think `book_outline` will be more straight forward as a machine name. Otherwise I will have a look. It worked for me so far.
Comment: Project application closed because you can use book.module's block: https://www.drupal.org/node/1930370#comment-7676439
Answer: Book Block is what I've used on a site. It's currently got a dev release but it's been stable for us so far on Drupal 7.
It provides you with a block per book. You can then use context to set the conditions of display.
Update - In terms of scaling it you will have to automate the block position, something like when a new book is created a new context is added with the correct conditions and reactions. Unfortunately this isn't as simple as a drop in contrib solution.
Comment: This solution does not sound to be scalable. Number of books will grow and users will create new books and content within them. So I want anything that belongs to a book to have this block by default. Unless I am missing something.
Comment: Ah ok, you should update the question with the extra information. I'll update my answer with more info.
Comment: Thanks for your answer and update. What I really need is a way to create just one block, that will have that menu. I am sure it is achievable with one block for all books. I am surprised this was not implemented by somebody yet.
Comment: I don't know of any contrib solution for this but I can think of a few custom solutions, you could extend the book block module and use the node object to filter the results.
Answer: See https://drupal.org/node/1930370#comment-7676439.
How does this request differ from what is available out-of-the-box from the core Book module?
From the original question:

Book module provides a block with books navigation, but I don't want all books to be visible in this menu.

On the core Book Block configuration page, you have the option to Show block only on book pages, which according to the description:

If Show block only on book pages is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The Page specific visibility settings or other visibility settings can be used in addition to selectively display this block.

Unless I'm missing something (again), I think both of us somehow missed this the first time.
Screenshot
Comment: Did you manage to make it show collapsed items? I guess that could be the original problem. I needed all items of the book with depth level of two to make accordion. When I do it with book's block it only shows one element as expanded.
Comment: Accordion needs all items of the tree rendered on the page. Accordion can be done with JavaScript and CSS, that is why it's not the main concern of the question.
Comment: No, but your original question clearly states, "I am not interested in how to implement accordion functionality, I just need a way to get menu items for all book elements of current node." And the accepted answer doesn't address that either. I think we both missed this simple solution.
Answer: In addition to Adam's answer I would like to add some information on how to theme the outline of a book.
There are few theming tricks. It took me quite a while to figure them out, so I decided share:

You can massage renderable array that you get with menu_tree_output() to change the output. You can either inject html directly to element of the leaf like this:
$leaf['#title'] = "Custom  html here.. ";
$leaf['#localized_options']['html'] = TRUE;
$leaf['#attributes']['class'][] = 'my-class-link';

Or you can change the theming function that will be applied to the element at the desired level of the tree:
$leaf['#theme'] = 'menu_link__my_book_outline';

in this case you will have to provide reference to this theme in hook_menu and provide implementation of the theme function declared in the hook:
/**
* Implements hook_theme()
*/
function my_book_outline_theme() {
  return array(
        'menu_link__my_book_outline' => array(
           'render element' => 'element'
        )
    );
}

/**
* Returns HTML for the section level elements of the my book outline
* see theme_menu_link for default implementation
*/
function theme_menu_link__my_book_outline(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';
  $output = '
' . $element['#title'] . '
';
  if ($element['#below']) {
    $sub_menu = drupal_render($element['#below']);
  }
  return $output . $sub_menu . "\n";
}

This way you will override the theme_menu_link call with your custom rendering.

If you theming depends on levels don't forget that you can always find out the depth of the item -  $element['#original_link']['depth']
Answer: Books structures are actually stored in the same way as menus, so if you are comfortable editing your database, you can very simply create a custom menu from to the book menu tree (that will therefore be available as a block).
This youtube video shows you how to do this:
http://www.youtube.com/watch?v=FuQIe5ik9as
Alternatively the menu_block module will offer your books as options in the list of menus when you create a new 'menu block' to suit your needs.

How to create a menu for current book


Inverse Propagation of Uncertainty

Question: Can any one help me find a reference under the title of  " Inverse Propagation of Uncertainty". I am starting a research on this topic after I had studied the forward propagation of uncertainty.
Thank you in advance
Comment: That's interesting. What is the meaning of inverse propagation of uncertainty? I thought that the underlying filtration imposes a forward flow of information.
Comment: @PantelisSopasakis  In general, a forward propagation of uncertainty  studies  the influence of the uncertainty in the inputs of a model  on the  output. For the inverse(or backward) one search for methods that help do the reverse, i.e. given the uncertainty  of the output, how one would predict the uncertainty of the input ( it is some kind of generalisation of parameter estimation error analysis).
Answer: Chapter 5 of Hartley & Zisserman, Multiple View Geometry in Computer Vision treats the topic.

Inverse Propagation of Uncertainty


KOTLIN orphanRemoval not working

Question: Is any one having the similar issue of removing child records but fail to use orphanRemoval annotation ?
Below is my code sample in Model Class constructor:
@OneToMany(orphanRemoval = true, cascade = arrayOf(CascadeType.ALL))
@JoinColumn(name = "categoryId", nullable = false)
var books: List<BOOK> = emptyList()

The CRUD with the cascade = arrayOf(CascadeType.ALL) is work perfectly, but when adding orphanRemoval = true into "ONE-TO-MANY" annotation, it could not work any more and throw the exception as below:

org.hibernate.HibernateException: A collection with
  cascade="all-delete-orphan" was no longer referenced by the owning
  entity instance:

the purpose of adding orphanRemoval to remove the child record permanently from database.
Answer: There's a blog post that explains what might be the cause of such exception: http://www.onkarjoshi.com/blog/188/hibernateexception-a-collection-with-cascade-all-delete-orphan-was-no-longer-referenced-by-the-owning-entity-instance/

You are setting a new collection via the setter thus leaving the original collection unreferenced by the Parent entity. This doesn't fly well with Hibernate and leaves it confused about what to do.

books is var, not val so I assume that in some place in your code, you are setting it to the desired value. Assuming the reasoning from the blog post is valid here, using final field of mutable type should be way to go,
val books: MutableList<BOOK> = mutableListOf()

Answer: @OneToMany(cascade = arrayOf(CascadeType.ALL), orphanRemoval = true, fetch = FetchType.EAGER)
@JoinColumn(name = "categoryId", nullable = false)
var books: MutableList<BOOK> = mutableListOf()
    set(value) {
        field.clear()
        field.addAll(value)
    }

KOTLIN orphanRemoval not working


How much overhead does x86/x64 virtualization have?

Question: How much overhead does x86/x64 virtualization (I'll probably be using VirtualBox, possbly VMWare, definitely not paravirtualization) have for each of the following operations a Win64 host and Linux64 guest using Intel hardware virtualization?

Purely CPU-bound, user mode 64-bit code
Purely CPU-bound, user mode 32-bit code
File I/O to the hard drive (I care mostly about throughput, not latency)
Network I/O
Thread synchronization primitives (mutexes, semaphores, condition variables)
Thread context switches
Atomic operations (using the lock prefix, things like compare-and-swap)

I'm primarily interested in the hardware assisted x64 case (both Intel and AMD) but wouldn't mind hearing about the unassisted binary translation and x86 (i.e. 32-bit host and guest) cases, too.  I'm not interested in paravirtualization.
Comment: (1) "x86" means 32-bit. You will not be able to run 64-bit code. AMD64 (also known as x64) virtualization has different limitations because it requires hardware extensions. (2) Do you mean x86 virtualization by binary translation (x86 only) or hardware assisted virtualization (VT)?
Comment: @Miles:  I've clarified the question.
Answer: I found that there isn't simple and absolute answer for questions like yours. Each virtualization solution behaves differently on specific performance tests. Also, tests like disk I/O throughput can be split in many different tests (read, write, rewrite, ...) and the results will vary from solution to solution, and from scenario to scenario. This is why it is not trivial to point one solution as being the fastest for disk I/O, and this is why there is no absolute answer for labels like overhead for disk I/O.
It gets more complex when trying to find relation between different benchmark tests. None of the solutions I've tested had good performance on micro-operations tests. For example: Inside VM one single call to "gettimeofday()" took, in average, 11.5 times more clock cycles  to complete than on hardware. The hypervisors are optimized for real world applications and do not perform  well on micro-operations. This may not be a problem for your application that may fit better as real world application. I mean by micro-operation any application that spends less than 1,000 clock cycles to finish(For a 2.6 GHz CPU, 1,000 clock cycles are spent in 385 nanoseconds, or 3.85e-7 seconds).
I did extensive benchmark testing on the four main solutions for data center consolidation for x86 archictecture. I did almost 3000 tests comparing performance inside VMs with the hardware performance. I've called 'overhead' the difference of maximum performance measured inside VM(s) with maximum performance measured on hardware.
The solutions:

VMWare ESXi 5
Microsoft Hyper-V Windows 2008 R2 SP1
Citrix XenServer 6
Red Hat Enterprise Virtualization 2.2

The guest OSs:

Microsoft Windows 2008 R2 64 bits
Red Hat Enterprise Linux 6.1 64 bits

Test Info:

Servers: 2X Sun Fire X4150 each with 8GB of RAM, 2X Intel Xeon E5440 CPU, and four gigabit Ethernet ports
Disks: 6X 136GB SAS disks over iSCSI over gigabit ethernet

Benchmark Software:

CPU and Memory: Linpack benchmark for both 32 and 64 bits. This is CPU and memory intensive.
Disk I/O and Latency: Bonnie++
Network I/O: Netperf: TCP_STREAM, TCP_RR, TCP_CRR, UDP_RR and UDP_STREAM
Micro-operations: rdtscbench: System calls, inter process pipe communication

The averages are calculated with the parameters:

CPU and Memory: AVERAGE(HPL32, HPL64)
Disk I/O: AVERAGE(put_block, rewrite, get_block)
Network I/O: AVERAGE(tcp_crr, tcp_rr, tcp_stream, udp_rr, udp_stream)
Micro-operations AVERAGE(getpid(), sysconf(), gettimeofday(), malloc[1M], malloc[1G], 2pipes[], simplemath[])

For my test scenario, using my metrics, the averages of the results of the four virtualization solutions are:
VM layer overhead, Linux guest:

CPU and Memory: 14.36%
Network I/O: 24.46%
Disk I/O: 8.84%
Disk latency for reading: 2.41 times slower
Micro-operations execution time: 10.84 times slower

VM layer overhead, Windows guest:

CPU and Memory average for both 32 and 64 bits: 13.06%
Network I/O: 35.27%
Disk I/O: 15.20%

Please note that those values are generic, and do not reflect the specific cases scenario.
Please take a look at the full article: http://petersenna.com/en/projects/81-performance-overhead-and-comparative-performance-of-4-virtualization-solutions
Comment: This article is out of date
Comment: @Mr. Happy, you are right. I got 385 nanoseconds by: 1000 / 2600000000 = 0.000000385 = 385 nanoseconds. Do you agree with this? Thanks for pointing this out.
Comment: `For a 2.6 GHz CPU, 1,000 clock cycles are spent in 23 milliseconds`, shouldn't that be a simple division of 1,000 by 2,600,000 to get the number of seconds 1,000 clock cycles take? (which is not 23 milliseconds)
Comment: @dyasny, I'm looking for hardware to repeat the tests with updated versions. Any idea where can I found it?
Comment: hardware can be easily found in a store
Comment: @PeterSenna Yes agreed! I was even wrong in my comment xD (2.6 GHz isn't the same as 2,600,000 Hz ofc)
Answer: There are too many variables in your question, however I could try to narrow it down. Let's assume that you go with VMware ESX, you do everything right - latest CPU with support for virtualaization, VMware tools with paravirtualized storage and network drivers, plenty of memory. Now let's assume that you run a single virtual machine on this setup. From my experience, you should have ~90% of CPU speed for CPU bound workload. I cannot tell you much about network speeds, since we are using 1Gbps links and I can saturate it without a problem, it may be different with 10Gbps link however we do not have any of those. Storage throughput depends on type of storage, with I can get around ~80% of storage throughput with local storage, but for 1Gbps NFS it is close to 100% since networking is bottleneck here. Cannot tell about other metrics, you will need to do experimentation with your own code.
These numbers are very approximate and it highly depends on your load type, your hardware, your networking. It is getting even fuzzier when you run multiple workloads on the server. But what I'm truing to say here is that under ideal conditions you should be able to get as close as 90% of native performance.
Also from my experience the much bigger problem for high performance applications is latency and it is especially true for client server applications. We have a computation engine that receives request from 30+ clients, performs short computations and returns results. On bare metal it usually pushes CPU to 100% but same server on VMware can only load CPU to 60-80% and this is primarily because of the latency in handling requests/replies.
Comment: I can speak from experience that saturating a 10GbE link with a single VM is very difficult. We've used VMWare FT, which can easilly saturate a 1Gbps link on its own, over 10Gbe and it didn't come close to saturating it.
Answer: I haven't dug down to the performance of the basic primitives like context switching and atomic operations, but here are my results of a brute force test I carried out recently with different hypervisors. It should be indicative of what you might expect if you are mostly CPU and RAM bandwidth limited.
https://altechnative.net/virtual-performance-or-lack-thereof/
Comment: That's great that you've got some info for Xen and KVM... But what about the most popular two Hypervisors?! They're completely missing. And you've including several Type 2 Hypervisors, no sane SysAdmin would use that for production.
Comment: Down voted. Link is dead.


Should I rank my data before doing post hoc Dunn test?

Question: I am trying to compare multiple algorithms using the Friedman test and post hoc Dunn test. I know that we should use the rank of our data before performing the Friedman test, but should we rank the data for Dunn test too? I am using python to perform the tests. Here is my code:
import pandas as pd
import scikit_posthocs as sp
print("Q synthetic statistical test:")

q_synthetic = {
"DATA_1": [0.61,0.54,0.59,0.52,0.59,0.54,0.62,0.52,0.49,0.50,0.47,0.62,0.60,0.56],
"DATA_2": [0.43,0.43,0.24,0.24,0.43,0.43,0.43,0.24,0.43,0.43,0.43,0.43,0.43,0.43],
"DATA_3": [0.82,0.82,0.64,0.61,0.82,0.74,0.82,0.74,0.82,0.82,0.82,0.81,0.82,0.82],
"DATA_4": [0.70,0.58,0.10,0.20,0.70,0.58,0.70,0.70,0.70,0.70,0.70,0.69,0.70,0.70],
"DATA_5": [0.53,0.11,0.10,0.10,0.53,0.10,0.53,0.53,0.53,0.53,0.53,0.53,0.53,0.53],
"DATA_6": [0.66,0.11,0.10,0.10,0.66,0.18,0.66,0.66,0.66,0.66,0.66,0.66,0.66,0.66]
}

dataframe_q_synthetic = pd.DataFrame.from_dict(q_synthetic, orient='index',
                                   columns=["ALGO_1", "ALGO_2", "ALGO_3", "ALGO_4", "ALGO_5", "ALGO_6", "ALGO_7","ALGO_8", "ALGO_9", "ALGO_10", "ALGO_11", "ALGO_12", "ALGO_13", "ALGO_14"])
general_dataframe_q_synthetic = dataframe_q_synthetic
l_data = pd.melt(general_dataframe_q_synthetic, var_name='criteria', value_name='score')
p_values_general_q_synthetic = sp.posthoc_dunn(l_data, val_col='score', group_col='criteria', p_adjust='bonferroni')
res = friedmanchisquare(general_dataframe_q_synthetic["ALGO_1"],general_dataframe_q_synthetic["ALGO_2"],general_dataframe_q_synthetic["ALGO_3"], general_dataframe_q_synthetic["ALGO_4"], general_dataframe_q_synthetic["ALGO_5"] ,general_dataframe_q_synthetic["ALGO_6"], general_dataframe_q_synthetic["ALGO_7"], general_dataframe_q_synthetic["ALGO_8"], general_dataframe_q_synthetic["ALGO_9"], general_dataframe_q_synthetic["ALGO_10"], general_dataframe_q_synthetic["ALGO_11"], general_dataframe_q_synthetic["ALGO_12"], general_dataframe_q_synthetic["ALGO_13"], general_dataframe_q_synthetic["ALGO_14"])
print("friedman test: ")
print(res)
print("algorithms p_values")
print(p_values_general_q_synthetic)

It returns all 1.0 matrix which means there is no significant difference between any of the groups while the Friedman test shows the existence of a significant difference. Therefore, I suppose there should be some mistake. Now, should I change my input to ranks? For example, should I write my data like this:
q_synthetic = {
    "DATA_1": [12, 6, 9, 4, 9, 6, 13, 4, 2, 3, 1, 13, 11, 8], ...

Which one is correct?
Answer: Yes.
Dunn's test is applied to ranks of the original data, and when implemented properly, corrects for ties. The specific ranks used are those applied to all data taken across all groups (i.e. Dunn's test uses the same ranks as the Kruskal-Wallis test), and not merely ranks within the two groups being compared in a single post hoc test.
Aside: Friedman's test is for repeated measures (blocked) data, while Dunn's test is for unblocked data.
Dunn, O. J. (1964). Multiple Comparisons Using Rank Sums. Technometrics, 6(3), 241–252.


Easy SQL Select Case When Statement throwing an incorrect syntax error and I can't find it!

Question: Thank you to everyone who tries to help!
I have 2 case select statements and I get the error on this statement:
Center,
Case Center
When 'Concord Call Center' Then 'CCC'
When 'Livermore Call Center' Then 'LCC'
When 'Morgan Hill Call Center' Then 'MHCC'
When 'Natomas Call Center' Then 'NCC'
When 'Virtual Call Center' Then 'VCC'
Else Center
End,

The error I'm getting is:
No column was specified for column 14 of 'ANDREWSTABLE'.
Please see below where it exists in the full statement:
Select segstart
    ,Interval
    ,dialed_num
    ,FiscalMonthYear
    ,SegStart_Date
    ,row_date
    ,Name
    ,Xferto
    ,TransferType
    ,Agent
    ,Sup
    ,Manager
    ,Center
    ,Xferfrom
    ,talktime
    ,ANDREWSTABLE.transferred
    ,ANDREWSTABLE.disposition
    ,split1
    ,hsplit.starttime
    ,CASE
    WHEN hsplit.callsoffered > 0
    THEN (CAST(hsplit.acceptable as DECIMAL)/hsplit.callsoffered)*100
    ELSE '0'
    END AS 'Service Level'
    ,hsplit.callsoffered
    ,hsplit.acceptable
FROM
(
Select segstart,
CASE
--when hour is = 0 and min < 30 then 0
WHEN DATEPART(HOUR, segstart) = 0 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('0' as int)
--when hour is = 0 and min > 30 then 30
WHEN DATEPART(HOUR, segstart) = 0 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('30' as int)
--when hour is 1 and min < 30 then 100
WHEN DATEPART(HOUR, segstart) = 1 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('100' as int)
--when hour is 1 and min > 30 then 130
WHEN DATEPART(HOUR, segstart) = 1 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('130' as int)
--when hour is 2 and min < 30 then 200
WHEN DATEPART(HOUR, segstart) = 2 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('200' as int)
--when hour is 2 and min > 30 then 230
WHEN DATEPART(HOUR, segstart) = 2 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('230' as int)
--when hour is 3 and min < 30 then 300
WHEN DATEPART(HOUR, segstart) = 3 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('300' as int)
--when hour is 3 and min > 30 then 330
WHEN DATEPART(HOUR, segstart) = 3 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('330' as int)
--when hour is 4 and min < 30 then 400
WHEN DATEPART(HOUR, segstart) = 4 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('400' as int)
--when hour is 4 and min > 30 then 430
WHEN DATEPART(HOUR, segstart) = 4 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('430' as int)
--when hour is 5 and min < 30 then 500
WHEN DATEPART(HOUR, segstart) = 5 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('500' as int)
--when hour is 5 and min > 30 then 530
WHEN DATEPART(HOUR, segstart) = 5 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('530' as int)
--when hour is 6 and min < 30 then 600
WHEN DATEPART(HOUR, segstart) = 6 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('600' as int)
--when hour is 6 and min > 30 then 630
WHEN DATEPART(HOUR, segstart) = 6 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('630' as int)
--when hour is 7 and min < 30 then 700
WHEN DATEPART(HOUR, segstart) = 7 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('700' as int)
--when hour is 7 and min > 30 then 730
WHEN DATEPART(HOUR, segstart) = 7 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('730' as int)
--when hour is 8 and min < 30 then 800
WHEN DATEPART(HOUR, segstart) = 8 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('800' as int)
--when hour is 8 and min > 30 then 830
WHEN DATEPART(HOUR, segstart) = 8 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('830' as int)
--when hour is 9 and min < 30 then 900
WHEN DATEPART(HOUR, segstart) = 9 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('900' as int)
--when hour is 9 and min > 30 then 930
WHEN DATEPART(HOUR, segstart) = 9 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('930' as int)
--when hour is 10 and min < 30 then 1000
WHEN DATEPART(HOUR, segstart) = 10 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1000' as int)
--when hour is 10 and min > 30 then 1030
WHEN DATEPART(HOUR, segstart) = 10 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1030' as int)
--when hour is 11 and min < 30 then 1100
WHEN DATEPART(HOUR, segstart) = 11 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1100' as int)
--when hour is 11 and min > 30 then 1130
WHEN DATEPART(HOUR, segstart) = 11 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1130' as int)
--when hour is 12 and min < 30 then 1200
WHEN DATEPART(HOUR, segstart) = 12 AND DATEPART(MINUTE, segstart) < 30  THEN CAST('1200' as int)
--when hour is 12 and min < 30 then 1230
WHEN DATEPART(HOUR, segstart) = 12 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1230' as int)
--when hour is 13 and min < 30 then 1300
WHEN DATEPART(HOUR, segstart) = 13 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1300' as int)
--when hour is 13 and min > 30 then 1330
WHEN DATEPART(HOUR, segstart) = 13 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1330' as int)
--when hour is 14 and min < 30 then 1400
WHEN DATEPART(HOUR, segstart) = 14 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1400' as int)
--when hour is 14 and min > 30 then 1430
WHEN DATEPART(HOUR, segstart) = 14 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1430' as int)
--when hour is 15 and min < 30 then 1500
WHEN DATEPART(HOUR, segstart) = 15 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1500' as int)
--when hour is 15 and min > 30 then 1530
WHEN DATEPART(HOUR, segstart) = 15 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1530' as int)
--when hour is 16 and min < 30 then 1600
WHEN DATEPART(HOUR, segstart) = 16 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1600' as int)
--when hour is 16 and min > 30 then 1630
WHEN DATEPART(HOUR, segstart) = 16 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1630' as int)
--when hour is 17 and min < 30 then 1700
WHEN DATEPART(HOUR, segstart) = 17 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1700' as int)
--when hour is 17 and min > 30 then 1730
WHEN DATEPART(HOUR, segstart) = 17 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1730' as int)
--when hour is 18 and min < 30 then 1800
WHEN DATEPART(HOUR, segstart) = 18 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1800' as int)
--when hour is 18 and min > 30 then 1830
WHEN DATEPART(HOUR, segstart) = 18 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1830' as int)
--when hour is 19 and min < 30 then 1900
WHEN DATEPART(HOUR, segstart) = 19 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('1900' as int)
--when hour is 19 and min > 30 then 1930
WHEN DATEPART(HOUR, segstart) = 19 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('1930' as int)
--when hour is 20 and min < 30 then 2000
WHEN DATEPART(HOUR, segstart) = 20 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('2000' as int)
--when hour is 20 and min > 30 then 2030
WHEN DATEPART(HOUR, segstart) = 20 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('2030' as int)
--when hour is 21 and min < 30 then 2100
WHEN DATEPART(HOUR, segstart) = 21 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('2100' as int)
--when hour is 21 and min > 30 then 2130
WHEN DATEPART(HOUR, segstart) = 21 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('2130' as int)
--when hour is 22 and min < 30 then 2200
WHEN DATEPART(HOUR, segstart) = 22 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('2200' as int)
--when hour is 22 and min > 30 then 2230
WHEN DATEPART(HOUR, segstart) = 22 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('2230' as int)
--when hour is 23 and min < 30 then 2300
WHEN DATEPART(HOUR, segstart) = 23 AND DATEPART(MINUTE, segstart) < 30 THEN CAST('2300' as int)
--when hour is 23 and min > 30 then 2330
WHEN DATEPART(HOUR, segstart) = 23 AND DATEPART(MINUTE, segstart) > 30 THEN CAST('2330' as int)
ELSE Null End as Interval,
    FiscalMonthYear,
    SegStart_Date,
    dialed_num,
    callid,
    Name,
    t.Queue AS 'Xferto',
    TransferType,
    RepLName+', '+RepFName AS Agent,
    SupLName+', '+SupFName AS Sup,
    MgrLName+', '+MgrFName AS Manager,
    Center
Case Center
When 'Concord Call Center' Then 'CCC'
When 'Livermore Call Center' Then 'LCC'
When 'Morgan Hill Call Center' Then 'MHCC'
When 'Natomas Call Center' Then 'NCC'
When 'Virtual Call Center' Then 'VCC'
Else Center
End,
    q.Queue AS 'Xferfrom',
    e.anslogin,
    e.origlogin,
    t.Extension,
    transferred,
    disposition,
    talktime,
    split1
From CMS_ECH.dbo.CaliforniaECH e

INNER JOIN Cal_RemReporting.dbo.TransferVDNs t on e.dialed_num = t.Extension
INNER JOIN InfoQuest.dbo.IQ_Employee_Profiles_v3_AvayaId q on e.origlogin = q.AvayaID
INNER JOIN Cal_RemReporting.dbo.udFiscalMonthTable f on e.SegStart_Date = f.Tdate

Where SegStart_Date between getdate()-90 and getdate()-1
    And SegStart_Date between RepToSup_StartDate and RepToSup_EndDate
    And SegStart_Date between SupToMgr_StartDate and SupToMgr_EndDate
    And SegStart_Date between Avaya_StartDate and Avaya_EndDate
    And SegStart_Date between RepQueue_StartDate and RepQueue_EndDate
    AND e.disposition like '4'
    OR e.transferred like '1'
) AS ANDREWSTABLE

Inner Join CMS_ECH.dbo.hsplit hsplit on hsplit.starttime = ANDREWSTABLE.Interval and hsplit.row_date = ANDREWSTABLE.SegStart_Date and ANDREWSTABLE.split1 = hsplit.split

Where did I go wrong? It says I have incorrect syntax and I have stared and stared and googled and googled and I can't come up with how to correct this! It must be something simple.
Thank you again for all your help!
Comment: Why are you trying to obfuscate nice, descriptive names?!  Shame!
And why stop there.  The codes all end with "CC"; it's redundant.  Make them: "C", "L", "MH", "N", and "V".  ;-)
Answer: You just need an alias for the column name. Since it's a derived table, it requires it.
 Case Center
    When 'Concord Call Center' Then 'CCC'
    When 'Livermore Call Center' Then 'LCC'
    When 'Morgan Hill Call Center' Then 'MHCC'
    When 'Natomas Call Center' Then 'NCC'
    When 'Virtual Call Center' Then 'VCC'
    Else Center
 End as Center

Comment: Duh derived tables! Thank you so much! You are truly an asset to this community!
Answer: This part is not well formed:
    ,Center
Case
When 'Concord Call Center' Then 'CCC'
When 'Livermore Call Center' Then 'LCC'
When 'Morgan Hill Call Center' Then 'MHCC'
When 'Natomas Call Center' Then 'NCC'
When 'Virtual Call Center' Then 'VCC'
Else Center
End
    ,q.Queue AS 'Xferfrom'

You either need a comma before Case, or something like Mycenter = instead of the first Center.
Comment: I have changed the commas per your recommendation and I am still getting Incorrect syntax near the keyword 'Case'. So I have changed it back. Since Center is an actual column in the table do I need to add a new column name?


Cut circle into percentage proportions

Question: I want to implement a function in javascript but I'm not sure how to construct the math logic behind it. What I need to have happen is position the circle in relation to a vertical line that cut it into two. The ratio of the slice is determined by percentages [(70,30), (40,60), (22,78)]. All I seem to know is that the area of a circle is:
$A = \pi r^2$
However I'm not seeing how to use this when dealing with portions of a circle. See diagram (note: I just eye-balled what (20,80) should look like)

Question
If I have an array of data tuples like [(70,30), (40,60), (22,78)], what formula will help me position my circle so that the area of the circle at either side of the line correctly reflects the percentage value (as in the diagram?).
In other words I'm taking percentages as inputs and what I need to output is the coordinates for the center of the circle such that the area as sliced by the line equals the percentage inputs.
Comment: What you want is the area of a circular segment: https://en.wikipedia.org/wiki/Circular_segment . That said,. are you sure this is a good graphic? A rectangle would make it much easier for the viewer to estimate the percentage.
Answer: You have the general form of the circle:
$$(x-h)^2+(y-k)^2=r^2$$
You can set your circle in $xy$-coordinate space as follows (where $h=r$ and $k=0$):

You can rewrite the circle now as a function:
$$f(x)=\sqrt{r^2-(x-h)^2}+k^2$$
Now, if $P$ is the smaller of the two percentages you are going to divide your circle with, then simply you have to solve for $a$ to satisfy your problem. Here, $a$ is the $x$-coordinate of your intersection point $(a,f(a))$:
$$P\pi r^2=\int_0^a f(x) dx- \frac12af(a)$$

Cut circle into percentage proportions


Why does the proportion of native language speakers have an arcsine like distribution?

Question: Based on actual data, given below is the distribution of the languages spoken in India by nearly $1.4$ billion people. There are more than $1600$ active languages in India which have been classified into $122$ broad languages. Out of these there are about $30$ major languages with more than a million speakers each. The country is divided into $35$ states, states are divided into $640$ districts and districts are divided into $5923$ sub-districts. Some sub-districts have only one community and have only $1$ language while the most heterogeneous sub-district has as many as $105$ languages.
For each language which is spoken in sub-district, divide the number of native speakers of a language in a sub-district by the total population of the sub-district to get the proportion of native speakers of that language in that sub-district. When we do this for all sub-districts and language combinations, this gives us $105961$ data points. The histogram of the distribution of these proportions is shown below which resembles an arcsine distribution.

The same shape appears even if we plot of the larger states instead of the whole country. Similarly, even if we plot at a district level, the same arcsine like shape appears.
Question 1: Why do we have this distribution that roughly resembles an arcsine like distribution. Note that I am not saying that it is necessarily a perfect arcsine in the theoretical sense but rather in an engineering application sense where it is good enough to assume the nearest matching distribution in order get the job done. I know that random Brownian motion results in an arcsine distribution but I am not sure if that is the underlying reason here.
Question 2: What would be the best way to interpret this observation? For example does such a shape indicates that a few major/dominant language will eventually cannibalize the numerous minor languages?
Comment: Do you have 35 data points per sub-district (a proportion speaking that "major language" within that sub-district)?
Comment: We have a data point whenever a language has $\ge 1$ speaker in a sub-district. Some sub-districts have only one community and have only 1 language hence i.e. data point while the most heterogeneous sub-district has as many as 105 languages i.e. 105 data points.
Comment: I can't follow what your graph is showing --- Frequency of what?  Proportion of what out of what?
Comment: @Ben-ReinstateMonica Divide the number of native speakers of a language in a sub-district by the total population of the sub-district to get the proportion of native speakers of that language in that sub-district. When we do this for all sub-districts and language combinations, this gives us 105961 data points. The histogram of the distribution of these proportions is shown
Comment: Okay, so "proportion" refers to the proportion of native speakers of a language in a subdistrict.
Answer:
Question 2: What would be the best way to interpret this observation? For example does such a shape indicates that a few major/dominant language will eventually cannibalize the numerous minor languages?

How many subdistricts are there? It looks like in most districts (about 2 or 3 thousand?) one language is dominant with 80% or more of the people that have this as their native language (and high dominance seems to be more likely than little dominance)
As a consequence, this leaves only 20% for the other languages in a district, and that creates this mirrored image. A language is spoken either by many (scoring >80%) or (as a consequence) on the other side only by a few (scoring <20%).
(Possibly there might be some bilingual speakers, but I assume that in most cases the native speakers of languages should add up to more or less 100% in a single subdistrict.)

In short:
You don't see many languages in the middle around 50% because there is often a dominant language in a district, which causes bumps at the high end (representing the percentage of native speakers of the dominant language) but also a bump at the low end (representing the percentage of native speakers of the non-dominant languages).

A nice way to add information to that graph would be to make a stacked graph where you sub divide the bars and give different colours to the 1st most spoken language, the 2nd most spoken language, and the other languages. In that way you can see how the mirror image is created from on the right the dominant (most spoken) language. And on the left the rest.

Question 1: Why do we have this distribution that roughly resembles an arcsine like distribution. Note that I am not saying that it is necessarily a perfect arcsine in the theoretical sense but rather in an engineering application sense where it is good enough to assume the nearest matching distribution in order get the job done. I know that random Brownian motion results in an arcsine distribution but I am not sure if that is the underlying reason here.

I don't believe that it is so simple as 1d Brownian motion. But maybe it could be insightful to make some maps and see how the languages distribute.
What I imagine is that the majority of the curve is dominated by the mayor languages which are concentrated in regions where they are the 1st language spoken:
From https://commons.m.wikimedia.org/wiki/File:Language_region_maps_of_India.svg#mw-jump-to-license

and on top of that you can imagine some mixing of those languages  at the borders which causes the distribution to deviate from a perfect 0/100% split.
You might see this spread as some sort of Brownian motion process (but possibly with some attractive forces). And the probability for languages to reach further from their origin reduces and in that way you get some distribution that might be simular to the arcsine distribution, but probably it will be more complex, maybe you could model(approximate) it more generally as a beta distribution, but possibly it is a mixture of something more complex, that happens to look like an arcsine.
Comment: My intuitive reasoning is exactly inline with your thoughts. There are distinct 5923 sub-districts. There is no bilingual because the census only counts 1 mother tongue per person.
Comment: I wonder if we can have theoretical derivation or justification to show that under such scenarios, we will get an arcsine distribution like in the case of Brownian motion.
Comment: @StatsIT How many points are there which have >50%. These can only be at most 5923 points right? Because there can never be two or more languages in a region that are simultaneously over 50%. Given that consideration, how can that curve represent 105k points if the right side, which looks almost half the points/weight represents at most 6k points (and might be less since the dominant language can be less than 50%). Could you provide more background about the data, or maybe the data itselve. Are most of those 105k points zero and not plotted?
Comment: Because there are 5923 sub-districts, but each one from this sub-districts can have between 1 and 105 languages to while one sub-district can have one data point for its only language, the next sub-district can give 105 data points, one for each of its language.
Comment: @StatsIT but at most 5923 points can be on the right side of the graph not? I would expect the left sight to have more weight, unless the graph represents only 10/20k points. It seems like the zero (with 80k or more points) is not plotted.
Comment: Yes theoretically, at max 5293 points can be on the right side.. All points were plotted.
Comment: @StatsIT So how many points are on the left side?
Comment: Left side of what?? Didn't get you? Or do you mean the leftmost?
Comment: The left and the right half (divided by the 50% mark). The right side can contain *at most* 5923 points, so the left side must contain the rest of the 105961 points, which is at least 100038 points. So those two sides should not look like having equal weights (as is the case now). What you probably have is that the points with low percentage are not plotted (the plot is missing two bars close to 0%, but that is where the majority of the points are). So it is far from an arcsine curve.
Comment: I found that in the data, they have treated outliers at the left side. If the populaiton of a language in sub-district is too small compared to others,  they have been categorized as All Others in that sub-district. E.g. If a sub-district has 1 million hindi speakers, 500000 marathi speakers and 23 chinese speakers, then Chinese are counted as others since did not make sense to have separate statistics for just 23 chinese. The cutoff of others at sub-district level is below 2% of the total population of the sub-district. So the plot is essentially for proportion of the non-others languages.
Comment: @StatsIT that makes that plot almost entirely about only 1st and 2nd languages. Probably a few regions will have 3 or 4 large languages which is pushing that left side a bit higher up than the right side but the difference between left and right is not much. So what you see mostly is on the right side the proportion by which the first language is spoken, and the left side is nearly a mirror image of this.
Comment: You should make a map where you plot the percentages of the first language. This will give you an insight how the process works.
Comment: No it doesnt make the plot almost entirely about the first and second sub district because  on a average we have about 20 non-other languages in a sub-district. Yes will try to plot on a map, issue is unlike state borders, I don't know if there are geo coordinates for districts or sub districts in an interactive map for India
Comment: @StatsIT the majority of those average 20 per district do not make it to the plot. I guess that there is only 15-20k in the plot, and 80k are not plotted because they are below the 2% mark.
Answer: The arcsine function describe a known distribution: the beta distribution $\mathcal{B}(\alpha = 1/2, \beta = 1/2)$. While a random walk would give a good mechanistic explanation, there is perhaps an answer in probability theory:

for any district the calculated proportion is a number between $0$ and $1$ - one can see it as the probability that the people from that district would speak its official native language,
when looking at the whole set of districts, this number can be considered as a random variable such that it is well described by the conjugate of the Bernouilli trial distribution, that is, the beta distribution,
this distribution has two parameters $\alpha$ and $\beta$.

Yet we need to understand why we should get $\alpha=1/2$ and $\beta =1/2$... Still half an answer: half full and half empty :-)


How to prove multinomial partitioning

Question: I was assigned an extra problem by my instructor (who's a grad student btw). So far, I know this question requires combinations and summation but I am so lost - I have no idea how to go about writing it or even starting it.
A picture of the question
Here's my current understanding of the problem: I have n items, and I want to put them into k subsets (where k^n) requires the use of the multinomial theorem.
Thanks and I appreciate any help or hints!!
Comment: Please [don't use images instead of typing your formulas](https://math.meta.stackexchange.com/questions/34121/why-image-cannot-be-used-for-explaining-my-maths-problem)

[Type the text](https://math.stackexchange.com/help/notation) of your exercise
Answer: I guess you refer to the multinomial theorem:
$$(x_1+\ldots+x_k)^n = \sum_{n_1+\ldots+n_k=n} {n\choose n_1,\ldots,n_k} x_1^{n_1}\cdots x_k^{n_k}.$$
Put $x_1=\ldots=x_k=1$.
Comment: Thanks, wasn't exactly what I was looking for but I appreciate the help :)

How to prove multinomial partitioning


Busca binária em lista encadeada

Question: Como posso realizar uma busca binária numa lista encadeada simples com cabeça? Também se é possível fazer isso, se existe algum método especial. No EP não posso contar de antemão a quantidade de elementos dessa lista, tenho que fazer a busca e inserir uma célula entre q->prox e p.
Comment: O que significam "EP", `p` e `q->prox`, dentro do problema?
Comment: Não entendi o motivo dessa pergunta ter sido classificada como ampla demais.
Comment: @MarcelodeSousa, acho que vale a pena você dar uma melhorada na sua pergunta. Você já teve 10 votos com ela assim. Se você fizer isso, sua pergunta tem a chance de ser reaberta e ganhar mais atenção da comunidade, com mais respostas e discussão em cima desse interessante tema.
Comment: A lista está ordenada, importo ele de um arquivo texto, porém a quantidade de elementos "não sei de antemão".
Comment: E você pode contar os elementos ?
Answer: É possível aplicar a Busca Binária numa lista encadeada, entretanto, você perderá a principal razão para usar a Busca Binária, o fator dela ter complexidade O(log n).
O fato é que buscas binárias aplicadas em arrays são eficientes, pois os elementos de um array são alocados na memória de maneira contígua, o que torna o acesso a qualquer elemento uma operação trivial, O(1).
Por outro lado, numa lista encadeada não há garantias de que os elementos serão alocados de maneira contígua e, mesmo que estivessem, não há como acessá-los em tempo O(1). Dessa maneira, para encontrar o meio da lista, é preciso desenlvolver um algoritmo que terá que visitar nós até encontrar o meio. Essa operação já torna o algoritmo mais lento. O pior caso de uma busca binária em uma lista encadeada é O(n log n).
Por causa disso, buscas sequenciais em listas encadeadas tendem a ser mais rápidas, já que O(n) < O(n log n). Ver atualização mais embaixo para casos onde pode valer a pena usar busca binária numa lista encadeada.
Sobre o algoritmo, esse parece interessante: http://www.algo-faq.com/Linked-List/Find-the-middle-node-of-a-linked-list.php
O desafio, se comparado com os arrays, é determinar o meio da lista. Para isso, o algoritmo acima utiliza uma abordagem diferente que não requer saber o tamanho da lista previamente.
Este mesmo algoritmo está descrito nesse artigo: http://www.ijcsit.com/docs/Volume%205/vol5issue02/ijcsit20140502215.pdf
Já que se trata de um exercício, eu iria pelo caminho citado acima.
Atualização
Existem casos onde pode fazer sentido o uso da busca binária em uma lista encadeada, explico.
Suponha que a tarefa de fazer a comparação seja custosa. Nesse caso, o algoritmo de busca binária pode fazer sentido, pois são feitas O(log n) comparações. Na parte O(n) não é feita comparação, mas apenas percorre-se a lista para encontrar o meio.
Por outro lado, se o algoritmo for busca sequencial então, pra cada item, a comparação é realizada. O que pode se tornar algo mais custoso se comparado com o cenário da busca binária.
Answer: Considerando que se possa obter a quantidade de elementos, você pode descobrir qual é a metade da lista. Com isso você teria que percorrer da cabeça da lista até o meio e comparar o valor desejado com o nó do meio, caso seja maior deverá considerar a posição do meio o inicio da lista e encontrar o meio do novo início até o fim, ou o contrário caso seja menor. O problema é que você não tem ganho nenhum de performance.
Abaixo um código que não testei, mas creio que deve ser próximo do que precisa.
celula *buscaR (int qnt, int x, celula *ini)
{
   if (ini->prox == NULL)
      return NULL;
   p = ini->prox;
   int count = 0;
   while (p != NULL && p->conteudo != x) {
        p = p->prox;
        if(count++ == qnt/2){
            if (p->prox->conteudo == x)
              return p->prox;
            else{
                if(p->prox->conteudo > x){
                    return buscaR (qnt/2, x, p->prox);
                }else{
                    return buscaR (qnt/2, x, ini->prox);
                }
            }
        }
   }
}

Atualização
Se considerarmos o trecho "No EP não posso contar de antemão a quantidade de elementos dessa lista" a resposta é:

Não será possível aplicar uma solução já que é primordial na busca
  binária termos quantidade de elementos da lista.
Comment: "No EP não posso contar de antemão a quantidade de elementos dessa lista"
Comment: No enunciado do problema não diz que sim nem que não, se poderíamos contar os elementos, não há uma restrição aparente, qdo perguntaram ao professor ele deu a entender que deveríamos pesquisar sobre esta questão, se era possível ou não.
Comment: Uma lista ligada não se beneficia de busca binária - no seu código mesmo você faz uma busca linear pela lista procurando o elemento - (e tem um outro erro: você não retorna a posição quando encontra o elemento - o caso em que `p->conteudo == x` no while)
Comment: Eu informei no fim do texto que não tem ganho performático. A razão de se aplicar uma varredura binária em uma lista encadeada ordenada é porque é um exercício, não tem sentido e ganho nenhum considerando a pesquisa sequencial para encontrar o meio da lista.
Comment: Eventualmente pode ter um ganho. Veja a atualização da minha resposta.
Comment: A declaração da função é: celula *BuscaBinaria (celula *p, celula *lista); essa funçao me retorna um ponteiro para em seguida inserir, outra função, na lista... estou buscando implementar usando a solução dos dois ponteiros, como no artigo apresentando, usando um ponteiro rápido e outro lento, usando um laço while interno, aparentemente vai funcionar, o que tá pegando são as condições de parada.
Comment: Na opção de algoritmo que apresentei, não é necessário contar de antemão a quantidade de elementos da lista. Pelo contrário, a lista pode ter tamanho arbitrário que o algoritmo funcionará. A única premissa, obviamente, é que a lista esteja ordenada antes da execução do algoritmo.

Busca binária em lista encadeada


How to SWIG my entire c++ code base to Java

Question: I have used swig to do generate java bindings for independent classes. However when I tried to do it for my existing codebase which is quite complex, having calls for STL , OSG and OpenGL. When I am trying to swig it am getting issues.
Here is my .i file
/* File : Line.i */
%module Line

%{
#include "Elements/LineFeatureObject.h"
%}

/* Let's just grab the original header file here */
%include "Elements/LineFeatureObject.h

This headers includes several other headers files. Some of them are read by the code while for some it gives the following warnings.
Warning 401: Nothing known about base class 'ELEMENTS::ILineAlgebra'. Ignored.
These classes are listed in the same location with rest of the classes. So am not sure what is going wrong with just couple of these classes.
Moving on I am able to get _wrap.cxx but when am compiling it am getting the following errors.
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2144: syntax error : 'void' should be preceded by ';'
error C2086: 'int WINGDIAPI' : redefinition
      C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\GL/gl.h(1152) : see declaration of 'WINGDIAPI'
.
.
.
Have not listed all of them here.
Can you help me find way around these errors.
Answer: SWIG by default only processes the top-level file named by %include.  It does not recurse into additional #include files.  You must explicitly %include the headers files you wish SWIG to process.
SWIG also does not know anything about STL, but there are some SWIG headers that can add support for STL types like std::string and std::vector.  STL templates must be explicitly instantiated and given a target language name:  Example:
%include <std_string.i>
%include <std_vector.i>
%template(IntVector) std::vector<int>;

SWIG also does not know about Windows types and compiler extensions and can be confused by __stdcall, __cdecl, __declspec(dllexport), DWORD, UINT, etc., but this include helps:
%include <windows.i>


Upload image to parse

Question: I am new to iOS, I want to upload image to parse. Here is the code
func imagePickerController(picker : UIImagePickerController, didFinishPickingMediaWithInfo info : [NSObject : AnyObject]) {
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        let scaledImage = self.scale(pickedImage, and : CGSizeMake(100, 100))
        let imagedata = UIImagePNGRepresentation(scaledImage)
        let imageFile = PFFile(data : imagedata)
        PFUser.currentUser()?.setObject(imageFile, forKey : "profileImage")
        PFUser.currentUser()!.saveInBackgroundWithBlock {
            (success : Bool, error : NSError?) -> Void in
            if (success) {
                println("success")//Neither success nor fail is printing
            } else {
                println("fail")
            }
        }

      //  print("")
        dismissViewControllerAnimated(true, completion : nil)
        let viewcontroller = self.storyboard?.instantiateViewControllerWithIdentifier("yoyo") as? UIViewController
        self.presentViewController(viewcontroller!, animated : true, completion : nil)

    }
}

The problem is databrowser is not populated with image. Thanks.
Answer: You may want to use the saveInBackgroundWithBlock like this
var parseImageFile = PFFile(name: "uploaded_image.png", data: imageData)
                    posts["imageFile"] = parseImageFile
                    posts.saveInBackgroundWithBlock({
                        (success: Bool, error: NSError?) -> Void in

                        if error == nil {
                            //back to home
                            println("Success")
                            self.performSegueWithIdentifier("something", sender: self)

Comment: Thanks its working but i want to know whats wrong in my code.

Upload image to parse


Imce file browser not showing select button

Question: The IMCE file browser seems to be missing it's select button. On this File field, I'd like content creators to choose an image that is already located on the server (not uploading a new image).
What setting might I have missed?

The field on this content type is configured as follows:

The profile is configured as followed:

File Permissions
The /tmp folder is owned by the apache user and is chmod'ed to 2775. The /sites/default/files/images folder is owned by apache and chmod'ed to 755.
Answer: Oh for heavens sake. I turned off Aggregate-JavaScript and cleared cache. The select button shows up. As to why this happened I can't say. I'm leaving this answer here in case any one comes up against this.
Temporarily turn off magnification and clear cache.
Comment: Thank you for this! I think this also solved another issue with batching (like rebuilding permission) for me as well.


Finding point of intersection of two parametric equations

Question: Given the following two parametric equations:
$$x = t^2$$
$$y = t^3 -t$$
how can I find the points of where those two lines intersect?
I am asked to find the area of the enclosed region and so far I got
the integral figured out now I just need the limits of integration
which should be where these two lines meet, right?
Comment: Are you sure that you want a *parametric* equation.  Note that $\{x, y\} = \{t^2, t^3 - t\}$ describes a **single** curve.
Comment: ...or are you interested in looking at the region in the plane between the graphs of the two functions $y_1 = t^2$ and $y_2 = t^3 - t$?  (It's a bit confusing to call one of these functions $x$ if they're both functions to be graphed on the same set of axes.)
Comment: yes, the parametric equation describes a single curve in the x,y plane
Comment: So, you're looking for the point(s) where the curve intersects *itself?*
Comment: yes, but when I plotted the graph I got the points 0 and 1 as my bounds but that did not work...
Answer: Suppose that $t_1$ and $t_2$ are the parameters of a self-intersection point.  In other words,
$$
t_1^2 = t_2^2 \qquad \text{and} \qquad t_1^3 - t_1 = t_2^3 - t_2.
$$
The first equation (equating the $x$-coordinates) shows that $t_2 = \pm t_1$, so if we want distinct parameters
$$
t_2 = -t_1.
$$
Then, the second equation (equating the $y$-coordinates) becomes
$$
\begin{align}
t_1^3 - t_1 &= \left( -t_1 \right)^3 - \left( -t_1 \right) \\
&= - t_1^3 + t_1 \\
&= - \left( t_1^3 - t_1 \right),
\end{align}
$$
which shows that
$$
0 = 2(t_1^3 - t_1) = 2t_1(t_1 - 1)(t_1 + 1).
$$
Hence, $t_1 = \pm 1$ with $t_2 = - t_1 = \mp 1$.  Therefore, the self-intersection point is
$$
(x, y) = (1, 0).
$$


Why the multiplication of a covariance matrix with the inverse of its sum with the identity matrix is symmetric?

Question: I have an empirical result (meaning it is always true by simple simulation e.g. in R) which I cannot prove to myself:
Let $A$ be a $n \times n$ covariance matrix (i.e. it is symmetric PSD), let $I_n$ be the identity matrix, $\theta_1$ and $\theta_2$ some scalars (in my case they are always positive but it does not matter). Let:
$V = (\theta_1 A + \theta_2I_n)^{-1}A$
It seems that $V$ is always symmetric! Can we prove it?
E.g. in R:
<code>A <- cov(rbind(c(1,2.1,3), c(3,4,5.3), c(3,4.2,0)))
isSymmetric(solve(2 * A + 3 * diag(3)) %*% A)
</code>

<code>[1] TRUE
</code>

To anyone interested: it is important to me mainly because this means I have two symmetric matrices $A, B$ which multiply to a symmetric matrix $AB$, in which case its eigenvalues are in fact multiplications of the eigenvalues of $A$ and $B$ according to this, which also simplifies its trace.
Comment: Note that $A$ can be decomposed into $P\Lambda P^{-1}$. Furthermore, $I = PIP^{-1}$. So, $(\theta_1 A + \theta_2I)^{-1}A = (\theta_1P\Lambda P^{-1} + \theta_2PIP^{-1})^{-1}P\Lambda P^{-1} = P(\theta_1\Lambda + \theta_2I)^{-1}P^{-1}P\Lambda P^{-1}$, which means that the resulting matrix is of the form $P\Xi P^{-1}$ with $\Xi$ being diagonal. The product is hence symmetric (as $P$ is orthonormal, ie $P'=P^{-1}$)
Comment: because $P(\theta_1\Lambda + \theta_2 I)^{-1}P^{-1}P\Lambda P^{-1} = P(\theta_1\Lambda + \theta_2I)^{-1}\Lambda P^{-1}$. Now put $\Xi = (\theta_1\Lambda + \theta_2I)^{-1}\Lambda$. It is obvious that $\theta_2I$ is diagonal, $\Lambda$ is diagonal by construction, and the product of diagonal matrices is also diagonal. So $\Xi$ is diagonal
Comment: Related: https://math.stackexchange.com/questions/126306/what-are-the-conditions-for-the-product-of-2-symmetric-matrices-being-symmetric
Comment: @lmaosome why is $\Xi$ diagonal?
Comment: Right, got confused with $A$. That's actually a great proof!
Answer: $$
V = (\theta_1 A + \theta_2I_n)^{-1}A
\implies
(\theta_1 A + \theta_2I_n)V = A\\
\implies
V^*(\theta_1 A + \theta_2I_n)^* = A^*
\implies
V^*(\theta_1 A + \theta_2I_n) = A\\
\implies
V^*(\theta_1 A + \theta_2I_n)A^{-1} = I_n
\implies
V^*(\theta_1 I_n + \theta_2A^{-1}) = I_n\\
\implies
V^*A^{-1}(\theta_1 A + \theta_2I_n) = I_n
\implies
V^*A^{-1} = (\theta_1 A + \theta_2I_n)^{-1}\\
\implies
V^* = (\theta_1 A + \theta_2I_n)^{-1}A
$$
Notice: it is way shorter if you know that $()^*$ and $()^{-1}$ commute

To state a general theorem, given $p(x,y),q(x,y)$ two polynomials (or even any function, if you know how to apply those to matrices), then $p(A,A^{-1})$ and $q(A,A^{-1})$ commute if $A$ is symmetric (Hermitian if complex).
Comment: Accepting this answer but see also lmaosome's comment on why those matrices commute.
Answer: If $$XY=YX$$ then by multiplying by left and right by $X^{-1}$:
$$YX^{-1}=X^{-1}Y$$ Thus since $[(\theta_1 I+\theta_2 A),A]=0$ also
$[(\theta_1 I+\theta_2 A)^{-1},A]=0$, thus the product is symmetric (all this assuming all inverses exist etc).
Comment: $[A,B]=AB-BA$ and is called the commutator
Comment: That is what I don't get, why they commute? Thanks.
Comment: my comment contains the proof; Note that $(\theta_1\Lambda + \theta_2I)^{-1}$ and $\Lambda$ commute. After interchanging their positions, factor back in $P$ and $P^{-1}$
Comment: @GioraSimchoni is this edit better?
Comment: @user619894 I got why they commute with lmaosome's comment. This $[A, B]$ notation is unclear to me but I'm guessing I'm the problem. Thanks.


How do I tell Jenkins to run a Python Flask application, then Selenium tests, then close the Flask application?

Question: I have a Flask application that spins up my web application to a specific port (e.g., 8080). It launches by running python run.py. I also have a Python file that runs Selenium end-to-end tests for the web app.
This is part of a GitHub repo, and I have it hooked up so that whenever I push/commit to the GitHub repo, it initiates a Jenkins test. Jenkins is using my machine as the agent to run the commands. I have a series of commands in a Pipeline on Jenkins to execute.
Ideally, the order of commands should be:

Spin up the Flask web application
Run the Selenium tests
Terminate the Flask web application

Now, my issue is this: I can use Jenkins to clone the repo to the local workspace on my computer (the agent), and then run the Python command to spin up my web app. I can use Jenkins to run a task in parallel to do the Selenium tests on the port used by the web app.
BUT I cannot figure out how to end the Flask web app once the Selenium tests are done. If I don't end the run.py execution, it is stuck on an infinite loop, endlessly serving up the application to the port. I can have Jenkins automatically abort after some specified amount of time, but that flags the entire process as a failure.
Does anyone have a good idea of how to end the Flask hosting process once the Selenium tests are done? Or, if I am doing this in a block-headed way (I admit I am speaking out of copious amounts of ignorance and inexperience), is there a better way I should be handling this?
Comment: Just to clarify, I don't need Selenium to do the shutting down; I only mentioned Selenium to illustrate why I am doing things this way. The critical part is the actual act of shutting down the port programatically. Thanks!
Comment: Does this answer your question? [How to run Flask Server in the background](https://stackoverflow.com/questions/36465899/how-to-run-flask-server-in-the-background)
Answer: I'm not aware of an official way to cause the development server to exit under Selenium control, but the following route works with Flask 1.1.2
from flask import request
...

if settings.DEBUG:
    @app.route('/quit')
    def quit():
        shutdown_hook = request.environ.get('werkzeug.server.shutdown')
        if shutdown_hook is not None:
            shutdown_hook()
            return "Bye"
        return "No shutdown hook"

Comment: Thanks, Dave.  Creating a shutdown route would definitely work, but I am hesitant about having a route that just anyone could use to shutdown my server. I wonder if there would be a way to protect just that one route, keep it open only for testing purposes?
Comment: You can selectively enable the route, e.g., only if DEBUG is enabled. I changed the example slightly to demonstrate that.
Comment: use token_required decorator to protect the route.


Como fazer divisão de itens a cada X itens

Question: Estou utilizando o PHP para receber os dados do servidor porém quero fazer com que a cada 7 itens a div seja fechada e abra uma nova porém não estou conseguindo.
Exemplo:

    <item>
    <item>
    <item>
    <item>
    <item>
    <item>
    <item>

.....

Answer: Basta utilizar um foreach, dividir o valor da contagem por 7 e capturar o resto da divisão. Caso seja 0, fecha a tag.
Você pode capturar esse valor utilizando $count % 7 === 0.
Exemplo:
<?php

$arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22];

/* Imprime a aberta da tag */
echo '
';

$count = 1;

foreach($arr as $value) {

    echo "
{$value}
";

    /**
     * Verifica se o número é divisível por 7 e se
     * a contagem é menor ou igual ao número
     * de elementos do array
     */
    if ( $count++ % 7 === 0 && count($arr) >= $count ) {
        echo '

';
    }
}

/* Fecha a tag */
echo '
';

Comment: Muito obrigado, ajudou muito eu estava no caminho certo porém de outra forma :D


Randomly choose 50% of n things, but only 1% of specific k things implies what?

Question: Summary: If you randomly choose 50% of n entities total, but it turns out you only chose 1% of k specific pre-chosen entities, does that imply n is likely to be much larger than k?
I'm trying to use Mathematica to solve https://www.reddit.com/r/answers/comments/9hcvk0/if_thanos_destroys_half_the_population_of_the/ using Bayesian analysis, but hit a brick wall
Statement of problem: If Thanos destroys half the population of the universe, with individuals chosen completely at random, but only 1% of the population of Earth is destroyed, what exactly would that mean? Would that imply that Earth only makes up for a tiny fraction of the population of the universe?
My claims:

The chance that only 1% Earth population destroyed is constant, since it only depends on the Earth population. It's Binomial[k,k/100]*(1/2)^(k/100)*(1/2)^(k-k/100) which simplifies to Binomial[k, k/100]/2^k.
If only 1% of the Earth population is destroyed, it is more likely the universe is larger.
The claims above do not conflict.

The first claim seems trivial, here's my work on the second claim:
(*

n = number of people in the universe including Earth
k = number of people on Earth

Note that k is a known number (about 8 billion), but I am generalizing to any k

We now select k/100 people from the k people on Earth and the
remaining n/2-k/100 people from the n-k people not on Earth.

Since the total ways of selecting people is 2^n, the probability our
selection matches the criteria is:

*)

f[n_, k_] = Binomial[k, k/100]*Binomial[n-k, n/2-k/100]/2^n

(*

Note that our problem requires that:

  - k is a multiple of 100
  - n is even
  - n > 99*k/50

I'm not completely happy with this formulation because it implies
exactly 1% of Earth's population is destroyed, instead of "less than
1%" or "approximately 1%". The fact that we're specifying an exact
number seems wrong (for one thing, as above, it requires the
population of Earth be an exact multiple of 100). However, I'll go
with it for the moment.

The smallest numbers that work are k=100 and n=198 for which we have:

f[198,100] == 25/100433627766186892221372630771322662657637687111424552206336

If we bump n to 200 (it has to be even), we get:

f[200,100] == 625/100433627766186892221372630771322662657637687111424552206336

Dividing the two:

f[200,100]/f[198,100] == 25

seems to show it's 25 times more likely that our condition will be met
if n=200 than if n=198.

Continuing along these lines:

N[Table[f[n,100]/f[198,100], {n, 198, 210, 2}]] ==
 {1., 25., 321.938, 2845.38, 19405., 108857., 522913.}

shows that the larger n is, the more likely our conditions are met for
k fixed at 100.

To find the absolute probability than n=198 (for example), we'd want:

f[198,100]/Sum[f[n,100], {n, 198, Infinity, 2}]

but this yields "Sum::div: Sum does not converge.", somewhat as
expected since f[n,k] grows with n

This makes me suspicious that my Bayesian analysis isn't accurate.

However, if we break our n population into groups of k, the percentage
of people killed in each group is distributed almost normally
(essentially normally for large n) with a mean of k/2 and a variance
of k/4 (thus a standard deviation of Sqrt[k]/2). The more groups there
are, the more likely it is there will be at least one group that has
only 1% of the population killed.

However however, I don't think this argument can be applied to a
*specific* group of k people.

Thoughts?

*)

Comment: It appears that your initial formulation has sampling with replacement which allows killing an inhabitant more than once.  You probably want the hypergeometric distribution where sampling is without replacement.
Comment: If 50% of the inhabitants of the universe are killed, and "only" 1% of earthlings, then we are very lucky indeed.
Answer: This is not an answer to your question but rather an answer to a somewhat related question.
If $n$ is the total population size with $k$ elements of a certain type ("the pre-chosen entities") and a random sample is taken of size $n_0$ without replacement, then the probability of obtaining $k_0$ or fewer of the $k$ elements is given by
$$\sum _{k_1=0}^{k_0} \frac{\binom{k}{k_1} \binom{n-k}{n_0-k_1}}{\binom{n}{n_0}}$$
This is the cumulative distribution function of the hypergeometric distribution.
A Mathematica implementation might be
f[n_, n0_, k_, k0_] := N[Sum[Binomial[n - k, n0 - k1] Binomial[k, k1]/Binomial[n, n0], {k1, 0, k0}]]
f[1000, 500, 10, 1]
(* 0.0104361 *)

Using numbers somewhat closer to what you have in the original question:
f[1000, 500, 100, 1]
(* 4.01903*10^-31 *)


SQL Server generate unique value

Question: SQL Server 10, .NET, C#
This is similar to this question but not exactly the same.
When a user creates new event on my event-scheduling web site, a unique sort-of easy to remember code is to be generated, so this code can be communicated (via the phone, let's say) to the participants. This generated code looks something like:
JohMonBlue5
<abbriviated name><abbriviated week day><some uniquefying suffix><index>
Yes, I could just e-mail a link, but I suppose that's not an option.
Note: I chose to use uniquefying suffix that is a color, since I think it's easier to remember color, than a number. Still I use number in case of a collision.
Question: What's a good way to implement this (I have unique constraint on the column already):

I can generate the index on the app side, and use try { insert } catch { retry }. In this case, how do I tell "Duplicate column  value" exception from any other exception? by message text?
Use something in the MSSQL Server to increment trailing number until uniqueness reached.
Answer: I suggest not using the catch to handle your duplicates exclusively. Catch should only be used for true errors and besides that, exception catching is slow and expensive. It would be better to generate a value and test to see whether it already exists via a query rather than allowing an exception to get thrown.
I would also implement the generator in one place. Don't split it by creating a color on the client and the number in the db. Pick one side or the other. On the Sql Server side a good way of implementing this type of feature is to use a UDF. Perhaps you can create a table of keywords (colors in your case) and randomly select from it and then perform your increment for uniqueness.
I also think that you don't have to o colors exclusively. Choose any words that are simple and easy to remember. Dragon, Friday, Blue, House...

SQL Server generate unique value


How would I calculate value with negative numbers?

Question: I have a dataset of basketball player salaries and an estimation of how many wins they contributed to their teams over the season. So a reasonable thing to do with those numbers would be to divide the wins with their salary to get a number showing the value of their contract.
The problem is that some players have negative production, as in over the season they provided their team with, say, -2 wins. That makes sense basketball-wise but when doing the division, obviously, you get a negative number, which doesn't seem to really describe the value of the contract. A really low number normally would mean the player is out-producing his contract, but that's clearly not the case with negative value players.
So my question is, how would you evaluate those players' contracts?
Here's an example of the data just to be clear:
<code>+-----------------+----------+-------+
|     Player      |  Salary  | Wins  |
+-----------------+----------+-------+
| Carmelo Anthony | 25534253 | 0.16  |
| Rajon Rondo     |  9000000 | -0.51 |
| Pascal Siakam   |  1544951 | 11.88 |
| Stephen Curry   | 37457154 | 14.43 |
| Kevin Knox      |  3739920 | -5.67 |
| Isaiah Thomas   |  2029463 | 0.00  |
| James Harden    | 30431854 | 18.57 |
| LeBron James    | 35654150 | 11.15 |
+-----------------+----------+-------+
</code>
Comment: You may want to describe how you're counting "wins", as I'd expect that to be a non-negative integer. How does someone provide the team with -2 wins, or with 0.16 wins? As you've described it, a low win/salary ratio indicates that the player is overpaid, the opposite of your interpretation - a player with 100 wins earning \$100k has a ratio of 1, but a player with 100 wins earning \$1M has a ratio of 0.1 (they are only as good as the \$100k player, but are paid 10x as much). A negative ratio indicates that you're paying someone to help you lose! I'm not seeing the problem here.
Comment: Wins are calculated from other data to (hopefully) sum up to team wins. So when team A wins 44 games in a season, we could estimate that Player X contributed, say, 7.44 of them. It's not as clear as counting pitcher wins in baseball but a rough estimate. -2 would mean indeed the player made the team perform worse than they would have without him.
Comment: Say two players make 10$m each. One contributes 0.01 wins, the other -0.01. I would expect their value to be almost exactly the same - how could I get there mathematically? Or players who are exactly at 0 wins - how could I compare their value to players who, say, are paid *a lot* more but contribute *a little* more?
Answer: Well, I figured out a way to do it that satisfies me.
I calculated the average cost of a win for the whole league, then calculated how many wins each player should produce based on their salary, then compared that to their actual production.
Peace!