ZMQ Multiple Publisher and Single Subscriber -- data loss observed

Question: I have created 2 Publishers connecting to the same static location .
Publisher1
dummyFrontEnd::dummyFrontEnd():context(1),socket(context,ZMQ_PUB) {

}

void dummyFrontEnd::Init()
{
    socket.connect("tcp://127.0.0.1:5555");
    cout << "Connecting .... " << endl;
}

void dummyFrontEnd::SendTwoTables()
{

   cout << "In SendTwoTables" <<endl;

   while(1) {
   canlogreq canLogObj = canlogreq::default_instance();
   canLogObj.set_fromhours(11);
   canLogObj.set_fromminutes(7);
   canLogObj.set_fromseconds(2);
   canLogObj.set_fromday(16);
   canLogObj.set_frommonth(5);
   canLogObj.set_fromyear(2020);
   canLogObj.set_tohours(12);
   canLogObj.set_tominutes(7);
   canLogObj.set_toseconds(4);
   canLogObj.set_today(17);
   canLogObj.set_tomonth(5);
   canLogObj.set_toyear(2020);

   zmq::message_t logsnippetmsg(canLogObj.ByteSizeLong() + sizeof(uint16_t));
    *((uint16_t*)logsnippetmsg.data()) = 20;

  canLogObj.SerializeToArray(logsnippetmsg.data()+sizeof(uint16_t), canLogObj.ByteSizeLong());

   socket.send(logsnippetmsg);
                  usleep(1);

   canLogObj.clear_fromhours();
   canLogObj.clear_fromminutes();
   canLogObj.clear_fromseconds();
   canLogObj.clear_fromday();
   canLogObj.clear_frommonth();
   canLogObj.clear_fromyear();
   canLogObj.clear_tohours();
   canLogObj.clear_tominutes();
   canLogObj.clear_toseconds();
   canLogObj.clear_today();
   canLogObj.clear_tomonth();
   canLogObj.clear_toyear();

}

}

Publisher2:
dummyFrontEnd::dummyFrontEnd():context(1),socket(context,ZMQ_PUB) {

}

void dummyFrontEnd::Init()
{
    socket.connect("tcp://127.0.0.1:5555");
    cout << "Connecting .... " << endl;
}

void dummyFrontEnd:: SendData() {

while (std::getline(file, line_str)) {
        std::stringstream ss(line_str);
        double tdiff;
        int i;
        char J;
        int _1939;
        int pgn;
        char p;
        int priority;
        char _0;
        int source;
        char dash;
        std::string direction;
        char d;
        int length;
        int data[8];

        ss >> tdiff >> i >> J >> _1939 >> pgn >> p >> priority >> _0 >> source
           >> dash >> direction >> d >> length >> data[0] >> data[1] >> data[2]
           >> data[3] >> data[4] >> data[5] >> data[6] >> data[7];

        timestamp += tdiff;

        while (gcl_get_time_ms() - start_time <
                uint64_t(timestamp * 1000.0) - first_time) { usleep(1); }

        if (arguments.verbose) {
            std::cout << timestamp << " " << i << " " << J << " " << _1939 << " "
                << pgn << " " << p << " " << priority << " " << _0 << " " << source
                << " " << dash << " " << direction << " " << d << " " << length
                << " " << data[0] << " " << data[1] << " " << data[2] << " "
                << data[3] << " " << data[4] << " " << data[5] << " " << data[6]
                << " " << data[7] << std::endl;
        }

        uint64_t timestamp_ms = (uint64_t)(timestamp * 1000.0);

        protoTable.add_columnvalues(uint64ToString(timestamp_ms) /* timestamp */);
        protoTable.add_columnvalues(intToString(pgn) /* PGN */);
        protoTable.add_columnvalues(intToString(priority) /* Priority */);
        protoTable.add_columnvalues(intToString(source) /* Source */);
        protoTable.add_columnvalues(direction /* Direction */);
        protoTable.add_columnvalues(intToString(length) /* Length */);
        protoTable.add_columnvalues(intToString(data[0]) /* data1 */);
        protoTable.add_columnvalues(intToString(data[1]) /* data2 */);
        protoTable.add_columnvalues(intToString(data[2]) /* data3 */);
        protoTable.add_columnvalues(intToString(data[3]) /* data4 */);
        protoTable.add_columnvalues(intToString(data[4]) /* data5 */);
        protoTable.add_columnvalues(intToString(data[5]) /* data6 */);
        protoTable.add_columnvalues(intToString(data[6]) /* data7 */);
        protoTable.add_columnvalues(intToString(data[7]) /* data8 */);

    zmq::message_t create_values(protoTable.ByteSizeLong()+sizeof(uint16_t));
        *((uint16_t*)create_values.data()) = TABLEMSG_ID;  // ID
        protoTable.SerializeToArray(create_values.data()+sizeof(uint16_t), protoTable.ByteSizeLong());

        socket.send(create_values);

        protoTable.clear_columnvalues();
        usleep(1);
    }

}

Subscriber
TransportLayer::TransportLayer():context(1),socket(context,ZMQ_SUB){ }

void TransportLayer::Init()
{
    socket.bind("tcp://*:5555");
    socket.setsockopt(ZMQ_SUBSCRIBE, "", 0);
}

void TransportLayer::Receive()
{
    cout <<"TransportLayer::Receive " << " I m in server " << endl;

    static int count = 1 ;
    // Producer thread.
    while ( true ){

    zmq::message_t request;
    string protoBuf;

    socket.recv(&request);

    uint16_t id = *((uint16_t*)request.data());

    cout <<"TransportLayer : "<<"request.data:  "<< request.data() << endl;
    cout << "TransportLayer: count " << count<< endl; count = count+1 ;
    cout <<"TransportLayer : request.data.size "<< request.size() << endl;

    protoBuf = std::string(static_cast<char*>(request.data()+sizeof(uint16_t)), request.size()-sizeof(uint16_t));
    cout << "ProtoBuf : " << protoBuf << endl;

     InterfaceLayer *interfaceLayObj = InterfaceLayer::getInstance();

     switch(id) {
        case TABLEMSG_ID: cout << "Canlyser" << endl;
      interfaceLayObj->ParseProtoBufTable(protoBuf);
            break;
        case LOGSNIPPET_ID:cout << "LogSnip" << endl;
             interfaceLayObj->ParseProtoBufLogSnippet(protoBuf);
             interfaceLayObj->logsnippetSignal(); // publish the signal
            break;
        default:
            break;
    }

    usleep(1);

    }

}

Observation :
I)
Execution Order .
1. Started Subscriber
2. Started Publisher1 ( it sent only one data value)
Subscriber missed to receive this data.
II)  modified Publisher1 to send the same data in a while loop
Execution Order
1. Started Subscriber
2. Started Publisher1
3. Started Publsiher2 .
Now I see that  Subscriber is receiving the data from both publishers .
This gives me an indication that there is a possibility for data loss.
How do I ensure there is absolutely no data loss.
Thanks
Answer: Your producer may be sending its one message before the subscription handshaking has completed. As a result, the publish socket discards the message because there is no subscription registered.
If you use an XPUB socket (ZMQ_XPUB -- see http://api.zeromq.org/4-2:zmq-socket) instead of a PUB socket, your program can wait for a subscribe message, so that it knows that someone is listening, before sending its message(s).


How do I get the format of my URLs to be username/controller/:id in Rails 3.1?

Question: I want it similar to the way Twitter handles the URLs for its tweets.
For instance, right now my URL looks like this: mydomain.com/feedbacks/1/, where feedbacks is the name of the controller.
I want it to look like: mydomain.com/username/feedbacks/1/ which is similar to Twitter's: twitter.com/username/status/:id/.
My routes.rb looks like this:
  resources :users do
      resources :feedbacks
  end

When I have it like this, it gives me the URLs as mydomain.com/users/1/feedbacks, but I want the actual username in the URL.
How do I get that?
Thanks.
Edit 1: If you are adding another answer to this question, please make sure it addresses my comments/questions to the answer already given. Otherwise it will be redundant.
Answer: scope ":username" do
  resources :feedbacks
end

From the docs:

This will provide you with URLs such as /bob/posts/1 and will allow
  you to reference the username part of the path as params[:username] in
  controllers, helpers and views.

UPDATE:
I have tested and confirmed the accuracy of paozac's answer. I'll clarify it a bit.
Suppose you had a @feedback object with an id of 12, and the associated user had a username of foouser. If you wanted to generate a URL to the edit page for that @feedback object, you could do the following:
edit_feedback_url(:username => @feedback.user.username, :id => @feedback)

The output would be "/foouser/feedbacks/12/edit".
# A URL to the show action could be generated like so:
feedback_url(:username => feedback.user.username, :id => feedback)
#=> "/foouser/feedbacks/12"
# New
new_feedback_url(:username => current_user.username)
#=> "/foouser/feedbacks/new"

Additionally, as noted by nathanvda in the comments, you can pass ordered arguments which will be matched with the corresponding dynamic segment. In this case, the username must be passed first, and the feedback id should be passed second, i.e.:
edit_feedback_url(@feedback.user.username, @feedback)

Also, if you need help handling the params from the controller, I suggest creating a new question specific to that.
Comment: It works, but last thing is, how do I then link to it? I tried `feedbacks_path(feedback)` and it produced the regular `domain.com/:id/feedbacks` URL. Also, on an index page where I have the path, if I have `edit_feedback_path(feedback)` it gives me this routing error: `No route matches {:action=>"edit", :controller=>"feedbacks",`, but if I pluralize the `edit_feedbackS_path`, I get this error `undefined method 'edit_feedbacks_path' for`. What am I doing wrong?
Comment: nathanvda thanks for that! Makes sense that ordered arguments are associated with corresponding dynamic segments. And the `_url` or `_path` are optional (defaults to `_url`). https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/routing/route_set.rb#L181
Comment: Sorry, should be `feedback_url(:username => feedback.user.username, :id => feedback)`
Comment: Run 'rake routes' from the command line to get all of the routes for your app.
Comment: Here is the `rake routes` output: https://gist.github.com/1193147. If the named routes (i.e. `edit_feedback_path`, etc.) don't have a way to handle route prefixes, you can always write them yourself: `"#{@user.username}/feedbacks/new` or extract this into helper methods that take the user or username as input.
Comment: I did run `rake routes` and tried the paths that I listed above. These are my routes - https://gist.github.com/1193719 but even then when I do `edit_feedback_path(feedback)` it still gives me that routing error. Why would that be? I would think that if it is in my routing table, I wouldn't need to write anything custom, no?
Comment: I even tried this: `<%= link_to 'Edit', :action => :edit, :controller => :feedbacks %>` for this route: `edit_feedback GET    /:username/feedbacks/:id/edit(.:format) {:action=>"edit", :controller=>"feedbacks"}` and I get this error: `Routing Error
No route matches {:action=>"edit", :controller=>"feedbacks"}`.
Comment: Funnily enough, when I go directly to `domain.com/username/feedbacks/:id` it works. So it's just named routes are not being created properly by Rails. Not too familiar with writing my own route prefixes - can you point me to an article/tutorial or some other helpful link that will help get me on my way? Thanks. Edit: I just retried with any string in the URL, not just valid `usernames` and it worked. So it seems as if it's not checking for valid usernames.
Comment: i.e. `domain.com/non-user/feedbacks/2` works, even though `non-user` is not a valid username.
Comment: Sorry, I'm really not that familiar with this feature of Rails. I can tell you that in a path like, `/someusername/posts`, your params hash will have a `:username` key, where `params[:username]=="someusername"`. So to get the posts for `someusername` you might try `@posts = User.find_by_username(params[:username]).posts`
Comment: I didn't find anything useful about named routes, i.e. `edit_feedback_path`, being used with a scope. It is simple to create these methods yourself however. `def scoped_edit_feedback_path(user, feedback) "/#{user.username}/#{edit_feedback_path(feedback)}"; end`. I'm totally winging that method but I would do something like that.
Comment: Ok Thanks. Hopefully someone can give me a more elegant solution - if not, then I will try what you have suggested.
Comment: Looks like paozac has the rest of the answer: http://stackoverflow.com/questions/7297231/how-do-i-get-the-format-of-my-urls-to-be-username-controller-id-in-rails-3-1/7350998#7350998
Comment: What would the delete action look like? Before I had this: `<%= link_to 'Delete', feedback, confirm: 'Are you sure?', method: :delete %>` But that is now giving me this error: `(No route matches {:action=>"show", :controller=>"feedbacks", :username=>#})`
Comment: Same as show but with different verb, see [docs](http://guides.rubyonrails.org/routing.html#crud-verbs-and-actions): `<%= link_to 'Delete', feedback_url(:username => feedback.user.username), confirm: 'Are you sure?', method: :delete %>`
Comment: Ok another issue has come up. When I do `/test/feedbacks/3/` for example, it shows me the right feedback, i.e. `feedback` with `:id => 3`, which happens to belong to `user = test`. However, when I do `test2/feedbacks/3/` it still shows me the feedback with `id=3`, but it shouldn't...because that feedback belongs to `username = test`. How do I restrict this from happening?
Comment: You can scope the find through the user, i.e. `User.find_by_username(params[:username]).feedbacks.find(3)`. Or in a `before_filter` you could find the user, i.e. https://gist.github.com/1216898.
Comment: Thanks. I am going to implement something like `Devise` and see if using `current_user` will fix this problem. Thanks for the tip and the gist though. I have forked it, so you can delete if you want.
Answer: Once you have defined the scope like dwhalen says you can generate the url like this:
feedbacks_url(:username => 'foo')

and get
http://www.example.com/foo/feedbacks

or
edit_feedback_url(:username => 'foo', :id => 1)

and get
http://www.example.com/foo/feedbacks/1/edit

Comment: you can use `feedback_url(:username => 'foo' :id => feedback.id), :method => :delete`
Comment: What would the delete action look like? Before I had this: `<%= link_to 'Delete', feedback, confirm: 'Are you sure?', method: :delete %>` But that is now giving me this error: `(No route matches {:action=>"show", :controller=>"feedbacks", :username=>#})`


How to find domain of complicated composite functions

Question: If I was asked to find the domain of arccos($e^x$), are there universal steps I can take to be able to find the domain? I know that you want the inner function in f(g(x)) to be defined as well as the function whole but I'm not sure how to think this one through...
Comment: Are you asking about the domain of the specific function $\arccos(\mathrm{e}^x)$?  or are you asking, more generally, about how to determine the domain of composite functions?
Answer: For $f(g(x))$ to make sense, one needs $g(x)\in\mathrm{dom}(f)$, so $\mathrm{dom}(f\circ g) = g^{-1}(\mathrm{dom}(f))$.
With $f=\arccos$ and $g=\exp$, you have $\mathrm{dom}(f)=[-1,1]$ and $\mathrm{dom}(f\circ g)=g^{-1}([-1,1])=(-\infty,0]$.
Answer: $e^x$ is defined everywhere, but for $\arccos(t)$ you want $-1 \le t \le 1$.  Now $e^x > 0 > -1$, but what do you need to get $e^x \le 1$?


How to override VBO redirect after performed action?

Question: My problem is that I can't override the URL where VBO redirects me to after I performed a default action. When I write my own actions I simply put $_GET['destination'] = 'my/target'; inside my custom form submit and that works. I tried to alter VBO's form submit to attach my own submit but that doesn't work:
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_bulk_operations_form') {
    array_push($form['#submit'], 'MYMODULE_vbo_redirect');
  };
}

function MYMODULE_vbo_redirect() {
  $_GET['destination'] = 'my/target';
}

When I testhack the VBO module and inside views_bulk_operations_form_submit($form, &$form_state) replace the final redirect with my custom one, it works. How can I get inside there and get my custom redirect executed without hacking?
Comment: This shouldn't be different for VBO forms, I think the accepted answer to [this question](http://drupal.stackexchange.com/questions/5440/form-redirect-not-working-if-destination-is-in-url-drupal-7) is the sort of thing you're looking for
Answer: I tried to alter the wrong form. VBO somehow merges its form stuff with the views form itself. So I had to look there to attach my custom submit. Then I had to pay attention to attach it in the same moment the views_bulk_operations_form_submit is attached. Then I set the cancel button to also bring me back where I want. And finally set $form_state['redirect'] in my attached submit.
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'views_form_MYVIEW_page') {
    $form['#action'] = '/my/target'; // the url that is displayed when in VBO multistep

    if (!empty($form['actions']['submit']['#submit'])) {

      if (in_array('views_bulk_operations_form_submit', $form['actions']['submit']['#submit'])) {
        $form['actions']['submit']['#submit'][] = 'MYMODULE_vbo_redirect';
        $form['actions']['cancel']['#href'] = 'my/target';
        $form['actions']['cancel']['#options']['path'] = 'my/target';
        dpm($form);
      }
    }
  }
}

function MYMODULE_vbo_redirect($form, &$form_state) {
  $form_state['redirect'] = array('my/target');
}

Only problem left is that this redirect always gets triggered when performing any VBO action from this view. But I can live with that for now.
Comment: this is just excellent :)
Answer: Has for VBO, there is a hook dedicated for form alter: hook_views_bulk_operations_form_alter(&$form, &$form_state, $vbo)
Example (modifying the email action):
function YOURMODULE_views_bulk_operations_form_alter(&$form, &$form_state, $vbo) {
  $form_id = $form['#form_id'];
  if ($form_state['step'] == 'views_form_views_form') {
    // Alter the first step of the VBO form (the selection page).
  }
  elseif ($form_state['step'] == 'views_bulk_operations_config_form') {
    // Alter the configuration step of the VBO form.
    if( $form_id == 'views_form_YOURVIEWSMACHINENAME_YOURVIEWSDISPLAY' ) {
      if(isset($form['recipient'])) {
        $form['recipient']['#default_value'] = '[user:mail]';
      }
      $form['message']['#type'] = 'text_format'; //> sends an array...
      $form['message']['#default_value'] = variable_get('NAMEOFAVAR');
      //add token list
      $form['token'] = array(
          '#type' => 'fieldset',
          '#title' => t('Replacement tokens'),
          '#collapsible' => TRUE,
          '#collapsed' => TRUE,
      );
      $token_types = array('user');
      $form['token']['tokens'] = array(
          '#markup' => theme('token_tree', array('token_types' => $token_types))
      );
    }
  }
  elseif ($form_state['step'] == 'views_bulk_operations_confirm_form') {
    // Alter the confirmation step of the VBO form.
    array_unshift($form['actions']['submit']['#submit'], 'YOURMODULE_sendmail_vbo_submit');
    $form_state['operation']->formOptions['message'] = $form_state['operation']->formOptions['message']['value'];
  }
}

The only thing to notice is that in your custom submit function, you don't get the form_state values has usual. I must dig more to find how to get them.
EDIT:
To alter/save/do stuff with $form_state['values'], better to put your custom submit or validate function in the second "if":
elseif ($form_state['step'] == 'views_bulk_operations_config_form') {
      $form['actions']['submit']['#validate'][] = 'YOURMODULE_sendmail_vbo_validate';
}

Comment: Be aware my code is work in progress, don't just rely on it... Must need some more "if" to be perfect... ;-)
Answer: In general you can use hook_drupal_goto_alter, for example:
/**
 * Implements hook_drupal_goto_alter().
 */
function MYMODULE_drupal_goto_alter(&$path, &$options, &$http_response_code) {
  if (CONDITION) {
    $path = 'my/target';
  }
}

Where in CONDITION you can check for the specific desired path which you want to override by checking the value of current_path() or arg().
Not ideal, but it's simpler than implementing custom submit handler (e.g. in $form['select']['action::views_bulk_operations_argument_selector_action']['#submit']) using hook_views_bulk_operations_form_alter and finding the right place to override the path.
Answer: function MYMODULE_form_alter(&$form, &$form_state, $form_id)
{
 if ($form_id == 'views_bulk_operations_form') {
$form['#submit']='MYMODULE_vbo_redirect';
}
}
function MYMODULE_vbo_redirect() {
// Use die('here') and submit the form.if it dies then redirect it
drupal_goto('my/target');
}

Hope this will helps you
Comment: I think `drupal_goto()` is no good idea here. It stops the whole VBO process before performing any action.


how to correct a mistake from two dimensional matrix using matlab programming

Question: Based on data (e.g. gender,age and handedness) of 8 participants of an experiment I am required to store the information in a 2-dimensional matrix using MATLAB Programming and correct a "mistake" by changing the handedness of two participants. Say:
% id, gender, age, handedness
participants = [1 1 21 1;
                2 2 25 1;
                3 2 19 1;
                4 2 23 2;
                5 1 23 1].

Can someone help me please?
Comment: your example contains 5 participants right? also what do you mean by correcting a mistake?
Answer: You're showing a 5x4 matrix:
% id, gender, age, handedness
participants = [1 1 21 1;
                2 2 25 1;
                3 2 19 1;
                4 2 23 2;
                5 1 23 1];

In Matlab you can access cells in a matrix with the () operator. In your case your 2D matrix  you have (row,column). E.g. the age of the second participant would be
display(participants(2,3)); % 25

You can show all rows or columns with : e.g.
display(participants(2,:)); % 2 2 25 1

will display all information for the second participant whereas
display(participants(:,3)); % 21; 25; 19; 23; 23

will display the third column "age".
If you want to modify a cell you can do it like this:
participants(2,3) = 99

This would change the age of the second participant to 99.
hth
Answer: If I understood you correctly, probably what you want to do is to estimate (e.g., with OLS) the handedness as a function of the other two variables and project on the fitted hyperplane, to find the handedness values that gives the largest errors. We can do the same with the following:
X = participants(:,2:3); % explanatory variables or regressors
y = participants(:,4); % the dependent variable handedness
w = inv(X'*X)*(X'*y); % weights learnt for OLS
y_hat = X*w % predicted handness
% 0.97038
% 1.41533
% 1.22988
% 1.35351
% 1.03220
e = abs(y - X*w) % errors in prediction
% 0.029620
% 0.415325
% 0.229878
% 0.646491
% 0.032196

As we can see, the two handedness values with maximum errors (mistakes) are the 2nd and the 4th value, which are respectively 1 and 2 in the sample data. So they are the ones with maximum mistakes and can be corrected.


How often has an unseeded woman player gotten to/won the Finals in a Grand Slam tournament?

Question: If unseeded Shuai Peng manages to beat Caroline Wozniacki in the semi-finals of the U.S. Open (unlikely in my opinion), she will face either Serena Williams or Ekaterina Makarova in the finals. And if Peng wins the final, she will be an unseeded woman Grand Slam winner.
How often has that happened in the past? And to whom?
Comment: The list of GS finals at Wikipedia contains also seedings of the finalists: http://en.wikipedia.org/wiki/List_of_Open_Era_Grand_Slam_men's_singles_finals The seedings are not shown in analogous list of women's finalists: http://en.wikipedia.org/wiki/List_of_Open_Era_Grand_Slam_women's_singles_finals
Comment: From TennisForum: [Most surprising runs by unseeded players at Grand Slams?](http://www.tennisforum.com/showthread.php?t=372281) and [Best Unseeded Players at the Grand Slams since 1989](http://www.tennisforum.com/showthread.php?t=368148). From MensTennisForums: [Greatest Unseeded/WC/Q run at a Grand Slam?](http://www.menstennisforums.com/showthread.php?t=152131)
Answer: Wikipedia has a list of the men's singles Grand Slam finals in the Open Era, and it shows the player seedings.  There have been 30 unseeded men's Grand Slam finalists since 1968.  Eight of those players won the final.  There has never been a men's Grand Slam final in the Open Era in which both players were unseeded.
The Tennis Channel has an article that lists the unseeded women's Grand Slam finalists of the Open Era up to January 2010.  There were 11 unseeded women's Grand Slam finalists between 1968 and the 2010 Australian Open, and 3 of them won the final.


Multivariate linear regression : (more theoretical and) geometrical aspect)

Question: What is the geometrical shape of the resulting function when you do a multivariate linear regression (aka General linear model)?  (roughly speaking: Y = f(X) = A.X where f is the resulting function, A is a matrix and X, Y vectors)
Firstly, I thought that it was always a line in a r dimensional space (r = input + output dimension). But if you work in 3D, with real inputs of size 2 (X=(x1, x2)) and real outputs to predict with size 1 (y), it seems that you obtain a plane (f(X) = f(x1, x2) = a.x1 + b.x2 = y).
With inputs of $n$ dimensions and outputs of $m$ dimensions, I find that you obtain the equation of $m$ hyperplanes in a $r=m+n$ space (or m linear equations). So you have a line only if you have n=1 (I didn't take into account the special case where there are collinear hyperplanes). So in most cases, you find the resultant function is a linear subspace of size n (since the intersection of m hyperplanes has a dimension of r-m = n+m-m = n).
Am I right about that?
I'm curious but, is it possible to train a multivariate regression model (i.e. a training model where independent and dependant variables are vectors, not scalars) with always a line shape? (for me, it seems theoretically impossible)
I use sklearn.linear_model.LinearRegression to do my multivariate linear regression (which seems to be ok).
Comment: Multivariate linear regression GLM (or MAN(C)OVA, whenever categorical predictors are present) is almost a synonym to canonical correlation analysis (CCA). Wrt fitting algorithm they are same, difference are in accents: in any regression, we pay different attention and compute different statistics for dependent and independent variables, in correlation, we usually are not interested "asymmetric" testing accompanying all that. CCA is the latent framework of GLM (general linear model).
Comment: (cont.) There is decent number of threads on this site about it (so search). Since you want geometry, I recommend [my answer explaining CCA](https://stats.stackexchange.com/a/65817/3277) for initial acquaintance. One more [answer](https://stats.stackexchange.com/a/169483/3277) of mine stresses the close relationship of MANOVA and linear discriminant analysis (which is equaivalent to CCA with dummy variables).
Comment: One this site you should use $\LaTeX$-notation for mathematics, please edit (I added some)
Answer: I'm not sure to understand (deeply) correctly the statistical perspective of my problem, but I think that I have an answer (and maybe I can explain it a little more clearly):
1) Yes, I think I'm right: you find always the equation of linear subspace of size n which tries globally to get as close as possible of all the vectors (training set) in the whole space (size n+m). These vectors being constructed in concatenating your training input and output vectors.
2) Indeed, it is not really possible to have the equation of a line. If you have a set of training vectors (size n+m), naturally, you can look for the line which approches globally the best all yours vectors. But you need to predict output vectors in using input vectors of size n. So, it exist a lot of points which are even not in the domain of your function. Thus, it is a nonsense to want to approximate your resulting model function by a line!


How to share Entity Framework Fluent API mapping with Dapper?

Question: There are two ORM in my .NET Core 6.0 project, Entity Framework and Dapper. I would like to share the Fluent API with Dapper. Is there any way to do this? The main goal is to centralize the mapping of the entities.
It would be interesting if the dapper took advantage of the hasColumnName("String_Column_Name") of the fluent Api, to map the object according to Entity Framework.
For example: I have a view VW_PERSONAL_DATA that returns something like this:
ID, NAME, NAME_AND_SURNAME

and an entity:
public class PersonalData
{
    public string? id { get; set; }
    public string? MainName { get; set; }
    public string? FullName { get; set; }
}

I have a Fluent API configuration file that looks like this:
public class PersonalDataConfig : IEntityTypeConfiguration<PersonalData>
{
    public void Configure(EntityTypeBuilder<PersonalData> builder)
    {
        builder.ToView("VW_PERSONAL_DATA")
               .HasNoKey();

        builder.Property<string>(o => o.Id)
               .HasColumnName("ID");

        builder.Property<string>(o => o.MainName )
               .HasColumnName("NAME");

        builder.Property<string>(o => o.FullName)
               .HasColumnName("NAME_AND_LASTNAME");
    }
}

In my DbContext, I have this code:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.ApplyConfiguration(new PersonalDataConfig ());
}

When I use Entity Framework, it works.
private readonly IReadRepository<PersonalData> _repository;

var pd = (await _repository.ListAsync()).FirstOrDefault();

But when I use Dapper, it fails to map, and it's logical, because Dapper doesn't have its mapping configured:
private readonly IDapperRepository _repository;

var pd = await _repository.QueryFirstOrDefaultAsync<PersonalData>($"SELECT * FROM VW_PERSONAL_DATA", cancellationToken: cancellationToken);

I would like to take advantage of the mapper that Entity Framework uses also for Dapper, without having to rewrite everything again.
Centralizing all mappings in one place. Remembering that I use the Fluent API for migration. Therefore, it would be interesting to preserve it.
Please, I don't want us alias in query.
I hope that was clear. Thanks.
Answer: After the model is built, examine the mappings to drive a custom dapper mapping.
foreach (var e in db.Model.GetEntityTypes())
{
    Console.WriteLine($"{e.Name} -> {e.GetTableName()}");
    foreach (var p in e.GetProperties())
    {
        Console.WriteLine($"{p.Name} -> {p.GetColumnName()}");

    }
}

Comment: Thanks for the idea, but I would like something more performant. As a supported solution in any nuget package. I will evaluate further responses for now.


Slow migration process when migrating nested field collections

Question: I'm migrating a site with something like 2000 nodes each having some field collections with other field collections nested in them. Basically, example node looks like this:
              level 1                 level 2
node -> field collection[0] ->  field collection[0] -> fields
     |                      |-> field collection[1] -> fields
     |                      |-> field collection[2] -> fields
     |
     -> field collection[1] -> field collection[0] -> fields
                            |-> field collection[1] -> fields
                            |-> field collection[2] -> fields
                            |-> field collection[3] -> fields
                            |-> field collection[4] -> fields

So, each node can have one or more level 1 field collections and each level 1 collection can have one or more level 2 collections.
My migration procedure was to first migrate nodes, then level 1 collections and now I'm migrating level 2 collections. Everything seems to work fine but when I got to level 2 import, the migration process slowed down to a crawl: close to one field collection entity per second!
This is insane! I've checked the queries that are being run during migration and it turned out that each migrated collection triggers a query that looks like this:
INSERT INTO field_data_field_level_2 (
    entity_type, entity_id, revision_id, bundle, delta, language,
    field_level_2_value, field_level_2_revision_id)
VALUES
    ('field_collection_item', '1486', '1486', 'field_level_1', '0', 'und', '3626', '3626'),
    ('field_collection_item', '1486', '1486', 'field_level_1', '1', 'und', '3627', '3627'),
    ('field_collection_item', '1486', '1486', 'field_level_1', '2', 'und', '3628', '3628'),
    ('field_collection_item', '1486', '1486', 'field_level_1', '3', 'und', '3629', '3629'),
    ...
    ...

The list goes on and on up to hundreds of rows! And it is increasing with each imported field collection. This is insane. How to optimize that?
The data is imported from other database, so map_joinable is set to false - could this be the reason? But why? It looks like for each import some rows are deleted and then saved again.
I've ran the import process with --instrument switch and got these results:
Name                Cum (bytes)  Count  Avg (bytes)
 destination import  51215952     100    512160
 Timer                                              Cum (sec)  Count  Avg (msec)
 page                                               424.547    1      424547.19
 destination import                                 421.709    100    4217.089
 field_collection_save                              418.984    100    4189.84
 saveIDMapping                                      0.948      100    9.484
 mapRowBySource                                     0.716      2204   0.325
 MigrateFieldsEntityHandler->prepare                0.27       100    2.704
 lookupDestinationID                                0.1        200    0.5
 MigrateDestinationEntity->prepareFields            0.061      100    0.613
 MigrateFieldsEntityHandler->complete               0.043      100    0.428
 MigrateDestinationEntity->completeFields           0.038      100    0.375
 MigrateValueFieldHandler->prepare                  0.008      200    0.042
 MigrateSourceSQL performRewind                     0.007      1      7.2
 MigrateSourceSQL execute                           0.007      1      7.11
 MigratePathEntityHandler->prepare                  0.006      100    0.059
 MigrateTaxonomyTermReferenceFieldHandler->prepare  0.004      100    0.044
 MigrateSourceSQL getNextRow                        0.004      100    0.041
 VildmedmadIngredientMigration prepareRow           0.003      100    0.035

I don't know what 'page' timer is but field_collection_save timer looks very suspicious.
[edit]
Similar issues happens when I do rollback. Same insane queries inserting lots of rows into field_data_field_level_2 table for each removed filed collection. Here's weird thing though: I would expect that the rollback is slow at the beginning and speeds up when there is less rows to work with. But no! It started very fast and slowed down over time.
Comment: Oh, great, now it's only 1 row per 2 seconds! :(
Answer: For those facing similar problem: the issue was caused by one node that had 3000+ level 2 field collections. This was caused by a malformed source data. After fixing this, the largest level 2 field collection set was roughly 50 entities and that worked fine. No slowdowns.
The problem with 3000+ field collections working slow was caused by entity_save that each time sent that humongous query to the database. It basically meant that each newly added field collection resulted in a query that contained all of the field collections for that particular entity.
This is insane, but that's how it works inside entity_save. The only workaround for that seems to be manually creating those entities or maybe even writing custom entity controller. Fortunately I didn't have to do that - fixing source data relationships solved the problem.


Getting QueryResultIterable not found error with Gradle appengine project using Objectify

Question: I've set up an Gradle App Engine project and ported over my code that was working perfectly well. But on building the project through Gradle I get the following error:
20:28:32.036 [INFO] [org.gradle.api.internal.tasks.compile.jdk6.Jdk6JavaCompiler] Compiling with JDK Java compiler API.
20:28:32.088 [ERROR] [system.err] D:\Projects\WordBuzzIntegrated\wordbuzzserver\src\com\wrc\wordbuzzweb\service\Login.java:21: error: cannot access QueryResultIterable
20:28:32.088 [ERROR] [system.err]         User user = ofy().load().type(User.class).id(facebookUser.id).now();
20:28:32.089 [ERROR] [system.err]                                 ^
20:28:32.089 [ERROR] [system.err]   class file for com.google.appengine.api.datastore.QueryResultIterable not found

I don't really have a clue what's causing the problem and Googling the error returns zero results. My dependencies list is as follows:
  dependencies {
        appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.15'
        providedCompile 'javax.servlet:servlet-api:2.5'
        testCompile group: 'junit', name: 'junit', version: '4.11'
        compile 'com.fasterxml.jackson.core:jackson-databind:2.4.3'
        compile 'com.fasterxml.jackson.core:jackson-annotations:2.4.0'
        compile 'com.fasterxml.jackson.core:jackson-core:2.4.3'
        compile 'com.googlecode.objectify:objectify:5.1.1'
    }

If anyone has any idea what might be going on here then your help would be greatly appreciated.
Answer: There are other App Engine dependencies that need to be included in the Gradle build. I ended up including all of the following for good measure - although I'm not sure all are required:
dependencies {
    appengineSdk "com.google.appengine:appengine-java-sdk:$appEngineVersion"
    compile "com.google.appengine:appengine-api-1.0-sdk:$appEngineVersion"
    compile "com.google.appengine:appengine-endpoints:$appEngineVersion"
    compile "com.google.appengine:appengine-endpoints-deps:$appEngineVersion"

    compile "com.googlecode.objectify:objectify:5.1.1"
    compile group: "com.google.guava", name: "guava", version: "18.0"
}

If using IntelliJ be sure to add the jars to your artifact for deployment.
If you want to run unit tests (junit included below) then you'll also need to add the following
    testCompile group: "junit", name: "junit", version: "4.11"
    testCompile "com.google.appengine:appengine-testing:$appEngineVersion"
    testCompile "com.google.appengine:appengine-api-labs:$appEngineVersion"
    testCompile "com.google.appengine:appengine-api-stubs:$appEngineVersion"

You'll also need to include the following in your Gradle script to set the app engine version. (Check what the latest version is)
ext.appEngineVersion = '1.9.21'

Answer: The Problem can be solved by adding the following dependendy in Gradle:
compile 'com.google.appengine:appengine-api-1.0-sdk:1.9.21'

You do not need to have the dependency for Cloud Endpoints if you are not using it, although it will also solve the problem as it has itself a dependeny on the missing one.
Ran into the same issue when generating a module with Android Studio which did not include this API. I could not even find any information on it in the documentation.


To solve $ \dfrac1m+\dfrac1n-\dfrac1{mn^2}=\dfrac34$

Question: How do we find all positive integers $(m,n)$ such that $ \dfrac1m+\dfrac1n-\dfrac1{mn^2}=\dfrac34$ ?
Comment: Note that you need $\frac 1m+\frac 1n\gt \frac 34$ which reduces the number of cases to a manageable number.
Comment: It reduces to the polynomial form $-3mn^2+4n^2+4mn-4=0$ if that's useful.
Answer: Since this is equivalent to the integer equation $$4n^2+4mn-4=3mn^2$$ then $n$ must divide $4$. Similar to @MarkBennet's answer, there are only a small number of cases to check.
If $n=1$, then $4m=3m$, so this doesn't give any solutions.
If $n=2$, then $12+8m=12m$, and $m=3$.
If $n=4$, then $60+16m=48m$, which gives no solutions.

You can check for negative solutions too this way, but it turns out there are none:
If $n=-1$, then $-4m=3m$, so this doesn't give any solutions.
If $n=-2$, then $12-8m=12m$, which gives no solutions.
If $n=-4$, then $60-16m=48m$, which gives no solutions.
Comment: @PeterHorvath Note that we can look at the equation as linear in $m$ so that $$m=\frac {4(n^2-1)}{3n^2-4n}$$

Now note that $\left(n, (n+1)(n-1)\right)=1$ (they have no non-trivial common factor), so if the expression is to be an integer we must have $n|4$.
Comment: @PeterHorvath Also you can look at that equation as $n(4n+4m-3mn)=4$ after rearranging terms and factoring. $n$ divides the left side, so it must also divide the right side.
Comment: Yes, the left side is divisable with 4, so the right side need as well. But why means that n must divide 4? What is if only 4|m ?
Comment: @MarkBennet Thanks!
Answer: Since you need $\frac 1m+\frac 1n\gt \frac 34$ you need either $\frac 1m\ge \frac 38\gt \frac 13$ or alternatively $\frac 1n \gt \frac 13$
The number of cases is small and they can be checked quickly.

To be more concrete about the cases involved, we must have either $m\leq 2$ or $n\leq 2$.
$m=1$ gives $\frac 1n-\frac 1{n^2}=-\frac 14$, or $4(n-1)=-n^2$which is impossible for positive integer $n$. Note that $n^2+4n-4=0$ has roots $n=-2\pm 2\sqrt 2$.
$m=2$ gives $\frac 1n-\frac 1{2n^2}=\frac 14$, which becomes $4n-2=n^2$ and this is impossible in integers since an even square is divisible by $4$. Also $n^2-4n+2=0$ has roots $n=2\pm \sqrt 2$
$n=1$ gives $\frac 1m+1-\frac 1m=\frac 34$ which is false.
$n=2$ gives $\frac 1m-\frac 1{4m}=\frac 14$ or $m=3$


Apply simple operation on array with sublists of unequal length

Question: I have a list made out of two sub-lists of unequal length:
a = [[0.2, 0.3], [0.6, 0.5, 0.7, 0.8]]

I nee to divide all elements by 10, so I though I could just convert a to a numpy array and then apply the operation like so:
np.asarray(a) / 10.

but this results in:
TypeError: unsupported operand type(s) for /: 'list' and 'float'

I could do this:
np.asarray([np.asarray(a[0]), np.asarray(a[1])]) / 10.

but I'm pretty sure this isn't the pythonic way to do this.
What is the correct way to apply an operation on an array made out of sub-lists with different lengths?
Answer: Do you need numpy? Otherwise you can just use a simple list comprehension:
b = [map(lambda x:x/10, i) for i in a]

Comment: Not necessarily, I just default to `numpy` for all operations on arrays.
Answer: I like the looks of:
In [273]: [(np.array(i)/10).tolist() for i in a]
Out[273]: [[0.02, 0.03], [0.06, 0.05, 0.06999999999999999, 0.08]]

Simply converting a to array is not good because it leaves the elements (2 of them) as lists.
In [275]: np.array(a)
Out[275]: array([[0.2, 0.3], [0.6, 0.5, 0.7, 0.8]], dtype=object)

An array of dtype=object is little more than a list with an array wrapper.  So you need to convert the sublists to arrays if you want to use the array division.
But the pure list comprehension version looks even cleaner - and more pythonic:
In [279]: [[j/10. for j in i] for i in a]
Out[279]: [[0.02, 0.03], [0.06, 0.05, 0.06999999999999999, 0.08]]

In my book, list comprehensions are very 'pythonic'; nested ones even more so. :)   Unnecessary use of numpy is un-pythonic, especially if it requires tolist afterwards.

An array of dtype=object tries to apply the operation to each element, e.g.
np.array(a)/10 => np.array([i/10 for i in a])

It doesn't work in the sublist case because [2,3]/10 is not defined.
But if we take the extra step of converting the sublists to array:
In [282]: aa=np.array([np.array(i) for i in a])

In [283]: aa/10.
Out[283]: array([array([ 0.02,  0.03]), array([ 0.06,  0.05,  0.07,  0.08])], dtype=object)

then the array division works.  But don't count on this working for all operations.  The dtype=object array code is not fully developed.
Comment: I'm selecting this answer because of it's simplicity, ie: it uses neither `numpy` nor the `lambda` function.
Answer: You could do this if you need to directly modify the existing list.
for i1, l in enumerate(a):
    for i2, _ in enumerate(l):
        a[i1][i2] /= 10


Error: XML declaration must be the first node in the document, and no white space characters are allowed to appear before it

Question: The functionality is like, we could write the plane SQL queries in XML and then we could import that xml in Product to see the changes in database.
In Update statement, I need to use the below update query. But getting 'XML declaration must be the first node in the document' error when trying to set column value.
<?xml version="1.0" encoding="utf-8"?>
<Metadata ClientSchemaVersion="1.1" Name="DummyName">
    <Differences>
      <Updates>
      ---
      ---
Begin
Update TABLE_NAME
Set //In next line, error is comming
ColumnName='<?xml version=''1.0'' encoding=''utf-16le'' ?><scenario xmlns='Text'><id>12345</id><title>
--
--
--
WHERE Condition

    END
    </Differences>
 </Metadata>

How I could achieve that use case. Could someone help me out here.
Note- The same Update Query is working fine if ran in SQL Server directly
Comment: Remove XML declaration, SQL Server doesn't use it. You can read [docs.ms](https://learn.microsoft.com/en-us/sql/relational-databases/xml/xml-data-type-and-columns-sql-server?view=sql-server-ver15#xml-storage-options)
Comment: That XML declaration would be, at best, a lie. You're using a non-nvarchar string literal (`''` without `N` prefix) which means it's not in any unicode encoding, let alone `utf-16le`.
Answer: Two XML declarations (<?xml ... ?>) are not allowed in an XML document.  Only one is permitted, and it must be at the very top of the document.
Here are two ways you can repair your XML:

Remove the second XML declaration.  The default for that SQL-embedded XML will then be XML version 1.0 with UTF-8 encoding.

If the processing application supports it, wrap the SQL in a CDATA section such that the SQL-embedded XML document is no longer parsed as XML.
<![CDATA[

   ... SQL with embedded XML that can contain its own XML declaration ...

]]>

See also

Error: The processing instruction target matching "[xX][mM][lL]" is not allowed


question about martingale inequality (similar to the condition of submartingale convergence)

Question: if $X_n$ is a submartingale which can be decomposed as $X_n=M_n-N_n$, where $M_n$ is a martingale, and $N_n$ is a non-negative supermartingale.
Can we get the conclusion that $\sup_n\mathbb E X_n^+<\infty$?
I have tried to show that $\sup_n\mathbb E|X_n|<\infty$ since we can get $\sup_n\mathbb E X_n<\infty$ easily. But it seems no reason to be true. So is there a counterexample? thanks.
Answer: Taking $X_n$ to be simple random walk is a counterexample.  Since $X_n$ is a martingale we have the decomposition with $N_n = 0$.  It's known that $E|X_n| \approx \sqrt{\frac{2n}{\pi}}$ (see MathWorld, eq. (39)), and by symmetry $EX_n^+ = \frac{1}{2} E|X_n|$ so $\sup_n E X_n^+ = \infty$.


Relationship between the phi, Matthews and Pearson correlation coefficients

Question: Are the phi and Matthews correlation coefficients the same concept? How are they related or equivalent to Pearson correlation coefficient for two binary variables? I assume the binary values are 0 and 1.

The Pearson's correlation between two Bernoulli random variables $x$ and $y$ is:
$$ \rho = \frac{\mathbb{E} [(x - \mathbb{E}[x])(y - \mathbb{E}[y])]} {\sqrt{\text{Var}[x] \, \text{Var}[y]}}
= \frac{\mathbb{E} [xy] - \mathbb{E}[x] \, \mathbb{E}[y]}{\sqrt{\text{Var}[x] \, \text{Var}[y]}}
= \frac{n_{1 1} n - n_{1\bullet} n_{\bullet 1}}{\sqrt{n_{0\bullet}n_{1\bullet}  n_{\bullet 0}n_{\bullet 1}}} $$
where
$$ \mathbb{E}[x] = \frac{n_{1\bullet}}{n} \quad
\text{Var}[x] = \frac{n_{0\bullet}n_{1\bullet}}{n^2} \quad
\mathbb{E}[y] = \frac{n_{\bullet 1}}{n} \quad
\text{Var}[y] = \frac{n_{\bullet 0}n_{\bullet 1}}{n^2} \quad
\mathbb{E}[xy] = \frac{n_{11}}{n} $$

Phi coefficient from Wikipedia:

In statistics, the phi coefficient (also referred to as the "mean square contingency coefficient" and denoted by $\phi$ or $r_\phi$) is a measure of association for two binary variables introduced by Karl Pearson. This measure is similar to the Pearson correlation coefficient in its interpretation. In fact, a Pearson correlation coefficient estimated for two binary variables will return the phi coefficient...
If we have a 2 × 2 table for two random variables $x$ and $y$

The phi coefficient that describes the association of $x$ and $y$ is
  $$ \phi = \frac{n_{11}n_{00} - n_{10}n_{01}}{\sqrt{n_{1\bullet}n_{0\bullet}n_{\bullet0}n_{\bullet1}}} $$

Matthews correlation coefficient from Wikipedia:

The Matthews correlation coefficient (MCC) can be calculated directly from the confusion matrix using the formula:
  $$ \text{MCC} = \frac{ TP \times TN - FP \times FN } {\sqrt{ (TP + FP) (TP + FN) (TN + FP) (TN + FN) } } $$
In this equation, TP is the number of true positives, TN the number of true negatives, FP the number of false positives and FN the number of false negatives. If any of the four sums in the denominator is zero, the denominator can be arbitrarily set to one; this results in a Matthews correlation coefficient of zero, which can be shown to be the correct limiting value.
Answer: Yes, they are the same.  The Matthews correlation coefficient is just a particular application of the Pearson correlation coefficient to a confusion table.
A contingency table is just a summary of underlying data.  You can convert it back from the counts shown in the contingency table to one row per observations.
Consider the example confusion matrix used in the Wikipedia article with 5 true positives, 17 true negatives, 2 false positives and 3 false negatives
<code>> matrix(c(5,3,2,17), nrow=2, byrow=TRUE)
     [,1] [,2]
[1,]    5    3
[2,]    2   17
>
> # Matthews correlation coefficient directly from the Wikipedia formula
> (5*17-3*2) / sqrt((5+3)*(5+2)*(17+3)*(17+2))
[1] 0.5415534
>
>
> # Convert this into a long form binary variable and find the correlation coefficient
> conf.m <- data.frame(
+ X1=rep(c(0,1,0,1), c(5,3,2,17)),
+ X2=rep(c(0,0,1,1), c(5,3,2,17)))
> conf.m # what does that look like?
   X1 X2
1   0  0
2   0  0
3   0  0
4   0  0
5   0  0
6   1  0
7   1  0
8   1  0
9   0  1
10  0  1
11  1  1
12  1  1
13  1  1
14  1  1
15  1  1
16  1  1
17  1  1
18  1  1
19  1  1
20  1  1
21  1  1
22  1  1
23  1  1
24  1  1
25  1  1
26  1  1
27  1  1
> cor(conf.m)
          X1        X2
X1 1.0000000 0.5415534
X2 0.5415534 1.0000000
</code>
Comment: Thanks, Peter! Mathematically, why are phi and Mathew equivalent to Pearson for two binary random variables?
Comment: If you take the definition of the Pearson correlation and manipulate it so it refers to counts rather than to sums of the differences between individual observations and the means, you get the Matthews formula.  I haven't actually done this, but it must be reasonably straightforward.
Answer: Firstly, there was a typo error in the question: $\mathbb{E}[xy]$ is not $\displaystyle \frac{n_{\bullet 1}n_{1\bullet}}{n^2}$ but rather
$$ \frac{n_{11}}{n} \times 1 \times 1 + \frac{n_{10}}{n}\times 1 \times 0 + \frac{n_{01}}{n} \times 0 \times 1 + \frac{n_{00}}{n} \times 0 \times 0 = \frac{n_{11}}{n} $$
Secondly, the key to showing that $\rho = \phi$ is
$$ n_{11} n - n_{1\bullet} n_{\bullet 1}
= n_{11} (n_{01} + n_{10} + n_{11} + n_{00}) - (n_{11} + n_{10}) (n_{11} + n_{01}) \\
= n_{11} n_{00} - n_{10} n_{01} $$
Answer: I want to note that the phi coefficient $\phi$ (MCC is just a synonym for it) will be equal to the Pearson correlation coefficient $r$ not only when random variables $X$ and $Y$ take values from the set $R = \{0,1\}$ but also in the following cases:

When $X$ and $Y$ take values from any other numeric binary set, for example, from $R = \{-1,1\}$ or $R = \{5.5,3.7\}$, etc.

When random variable $X$ takes values from the set $R_X = \{x_1, x_2\}$ and random variable $Y$ takes values from the set $R_Y = \{y_1, y_2\}$ (this case may correspond to a situation when $X$ and $Y$ are two nominal variables with two categories each, and we mapped the categories into some different numeric values; such mapping is, of course, not unique) if the contingency table is built in such a way that numbers $\mathrm{max}(x_1,x_2)$ and $\mathrm{max}(y_1,y_2)$ are the names of the 1st row and 1st column of the table respectively (or the names of the 2nd row and the 2nd column of the table respectively).

The proof is given below.
A key mathematical property of the Pearson correlation coefficient is that it is invariant under separate changes in location and scale in the two variables. That is, we may transform $X$ to $a + b X$ and transform $Y$ to $c + d Y$, where $a, b, c, d$ are constants with $b, d \gt 0$, without changing the correlation coefficient (this holds for both the population and sample Pearson correlation coefficients). So if we have $R_X = \{x_1,x_2\}$ and $R_Y = \{y_1,y_2\}$ there always exist linear mappings $$f: X \to U = \frac{-x_1 + X}{x_2 - x_1}, \quad g:Y \to V = \frac{-y_1+Y}{y_2-y_1},$$
they map $R_X \overset{f}{\to} \{0,1\}, \, R_Y \overset{g}{\to} \{0,1\}$.
Using them, we get that Pearson's $r(\mathbf{x},\mathbf{y}) = r(f(\mathbf{x}), \, g(\mathbf{y})) = r(\mathbf{u}, \mathbf{v})$, where $\mathbf{u}, \mathbf{v} \in \{0,1\}^n$. Next, user ryan_tt proved in his post that $r(\mathbf{u}, \mathbf{v}) = \phi(\mathbf{u}, \mathbf{v})$. Hence,
$$r(\mathbf{x},\mathbf{y}) = \phi\left(\frac{-x_1 + \mathbf{x}}{x_2 - x_1}, \frac{-y_1+\mathbf{y}}{y_2-y_1} \right), \qquad \forall \mathbf{x},\mathbf{y} \in \mathbb{R}^n.$$


Eclipse Formatter: Enums and Constructor

Question: i try to apply some guidelines for my code formatter in eclipse, but i have a problem with two things:
at first, enum Classes:
What I expect after formatting:
enum class Type : uint8_t
{
    first = 1,
    second = 2,
    third = 3
}

What i get after formatting:
enum class Type
    : uint8_t {
        first = 1,
    second = 2,
    third = 3
}

The second thing are constructors:
Again, what i expect:
Example::Example(int x) :
    _x(x)
{
}

And what i get:
Example::Example(int x) :
        _x(x)
{
}

So I have to change the line break of Enums and the indention of the assigment list of constructors. But sadly I dont find this options..
Can someone help?
Answer: You can control the formatting options using the Project Properties->C/C++ General->Formatter tab:

Using Edit the following window with all the options pops up:

Regarding enum declarations it seems only line wrapping options can be controlled:
Comment: Thank you, now i found out how to change the constructor thing. But i do not find any option, to get the enum class formatted correctly. Do you know where i can find this option?


Why does $\frac{d}{d\theta} \left(\theta \ln\prod\limits_{i=1}^nx_i\right) = \sum\limits_{i=1}^n\ln x_i$

Question: Is this just the product rule? I have this in my notes but I didn't think anything of it and now I'm wondering how this happens?
Edit:
Im working with maximum likelihood estimation and in my notes I have that the likelihood funciton $=L(x;\theta)=\prod_{i=1}^nf(x;\theta)$ where $x$ is the variable and $\theta$ is the parameter of a probability distribution. To estimate I was told that we take the log of the likelihood function, i.e. $\ln(L)$, then take its derivative to estimate the parameter. The function I'm working with is $f(x;\theta)=(\theta +1)x^{\theta}$. So
$$
  L(x;\theta)=\prod_{i=1}^n(\theta +1)x_i^{\theta}=(\theta+1)^n\prod_{i=1}^nx_i^{\theta}.
$$
Now
$$
  \ln(L(x;\theta))=n*\ln(\theta+1)+\theta \ln\left(\prod_{i=1}^nx_i\right).
$$
Here's where I'm confused, I have in my notes that
$$
  \frac{d(\ln L)}{d\theta}=\frac{n}{\theta+1}+\sum_{i=1}^n\ln(x_i).
$$
Why does the product of $x_i$ become the summation of $x_i$?
Comment: Are you aware of the product rule $\log(abc\cdots)=\log(a)+\log(b)+\log(c)+\cdots$? (I think students should learn how logarithms work before they cover derivatives, personally.)
Comment: Can you please clarify what you mean? Edit your post to provide more context.
Comment: @anon AH! Yes I did know that and for quite some time, but for some reason that did not register in my mind immediately. I get logarithms, but my mind skipped a basic rule. Thank you for the reminder.
Answer: $$
\ln(a\cdot b) = \ln(a) + \ln(b)
$$
This is a fundamental property of logarithms.
so lets use $$\prod_{i=0}^n x_i = P_n$$
then we have
$$
\ln\left(\prod_{i=0}^n x_i\right) = \ln\left(x_n\prod_{i=0}^{n-1} x_i\right) = \ln(x_n) + \ln\left(\prod_{i=0}^{n-1} x_i\right) = \ln(x_n) + \ln(P_{n-1})
$$
using induction or just seeing a pattern you find that we have
$$
\ln\left(\prod_{i=0}^n x_i\right)  = \sum_{i=0}^n\ln x_i
$$
Answer: $$ \ln(L(x;\theta))=n\ln(\theta+1)+\theta \ln\left(\prod_{i=1}^nx_i\right)$$
$$=n\ln(\theta+1)+\theta \ln\left(x_1x_2...x_n\right)$$
$$\stackrel{(*)}{=}n\ln(\theta+1)+\theta [\ln(x_1) + \ln(x_2) + ... + \ln(x_n)]$$
$$=n\ln(\theta+1)+\theta \sum_{i=1}^n\ln(x_i)$$
$$ \implies \frac{d(\ln L)}{d\theta}=\frac{n}{\theta+1}+\sum_{i=1}^n\ln(x_i) $$
$(*)$ For real numbers $x_1, x_2, ..., x_n > 0$, we have
$$\ln\left(x_1x_2...x_n\right)=[\ln(x_1) + \ln(x_2) + ... + \ln(x_n)]$$


If $f(x) = \frac{x}{\sqrt{x^2 + 1}}$, what is $\underbrace{f(f(f( \dots f}_{2013}(x) \dots )))$?

Question: Given the function
$$f(x) = \frac{x}{\sqrt{x^2 + 1}},$$
I need to evaluate the iterated (nested) function
$$\underbrace{f(f(f( \dots f}_{2013}(x) \dots ))).$$
I believe the alternative notation for this is $f^{2013}(x)$. I've tried looking at the problem as a geometric progression, but I haven't gotten very far. Any help would be appreciated.
Comment: Have you tried computing, say $f^2(x)=f(f(x))$ and $f^3(x)=f(f(f(x)))$? You should be able to see a pattern.
Comment: As soon as you notice the pattern, just use mathematical induction to prove that it is the case for any given $n$.
Answer: Hint: $$f(f(x)) = \frac{x}{\sqrt{2x^2 + 1}}, \quad f(f(f(x))) = \frac{x}{\sqrt{3x^2 + 1}}, \quad \ldots$$
Answer: Let $g_k(x) = \frac{x}{\sqrt{kx^2 + 1}}$, such that $f = g_1$
$$\begin{align*}
g_1(g_k(x)) & = \frac{\frac{x}{\sqrt{kx^2 + 1}}}{\sqrt{\left(\frac{x}{\sqrt{kx^2 + 1}}\right)^2+1}} = \frac{\frac{x}{\sqrt{kx^2 + 1}}}{\sqrt{\frac{x^2}{\sqrt{kx^2 + 1}^2} + 1}} \\
& = \frac{\frac{x}{\sqrt{x^2 + 1}}}{\sqrt{\frac{x^2 + kx^2 + 1}{x^2 + 1}}} = \frac{x}{\sqrt{(k+1)x^2 + 1}} \\
& = g_{k+1}(x)
\end{align*}$$
We have now proven, that $f^k(x) = g_k(x)$, so
$$f^{2013}(x) = g_{2013}(x) = \frac{x}{\sqrt{2013x^2 + 1}}$$


What do the numbers mean in awstats report?

Question: Under the "Links from an Internet Search Engine" in "Connect to site from" section, there are lines like
<code>Google .com 0   / 24
</code>
What does the number 0 and 24 mean?
Answer: Those numbers are just a "quick view" of the PAGES and HITS you get for each entry.

If you click "Full List" you can see the headers which give the actual breakdown of numbers and the column headers showing what they represent.


Textbooks on Complex Analytic Spaces

Question: I'm looking for textbooks or monographs on complex analytic spaces. I'm aware of Coherent Analytic Sheaves by Grauert/Remmert and Several Complex Variables by Gunning/Rossi, but more references would be appreciated. I would prefer resources that allow nilpotents.
Context:
Complex-Analytic techniques play an important role in algebraic geometry. I'm interested in algebraic geometry, so naturally I want to learn it. A particular interest is Berkovich spaces over $\Bbb Z$ à la Poineau, and these contain complex analytic spaces as fibers over archimedean points, the reason being that Berkovich geometry over $\Bbb C$ is complex-analytic geometry. In terms of potentially relevant background, I'm familiar with some algebraic geometry, sheaf cohomology, complex analysis, algebraic topology and some theory of Riemann Surfaces.
Comment: Did you check the book "Complex analytic sets" by Chirka?
Answer: What comes to mind is:

Algebraic curves and Riemann surfaces by Rick Miranda

Complex Geometry by Daniel Huybrechst

Principles of Algebraic Geometry by Griffith and Harris.

The first two are an easier read the last one requires knowledge of basic differential geometry namely differential forms. But all three should focus on what you're looking for and 3. Being more algebraic and less analytic as well as the first one but 2. Is purely analytic.
Comment: Thanks you for the answer! These look very interesting, but I want to stress that I'm looking for resources on complex analytic spaces (which can have singularities and non-reduced "rings of functions") and not just on complex manifolds. From looking at the tables of content of these references, it seems that they only treat complex manifolds.
Comment: You might find [this](https://link.springer.com/book/10.1007/BFb0077071) helpful @LukasHeger or [this](https://www.amazon.com/Complex-Analytic-Sets-Mathematics-Applications/dp/0792302346)
Answer: You should study Gerd Fischer's Complex Analytic Spaces, Lecture Notes in Mathematics 538.
Fischer got his PhD under Remmert, one of the greatest 20th century complex analysts.
Fischer has written many excellent research articles in complex analytic geometry but is also a great pedagogue who has written many books at various levels.
His books on linear algebra, algebraic curves, etc. have been tremendously successful  in Germany in the last decades.
The book I recommend is about general, i.e. non reduced, complex spaces.
Reducedness is never assumed except in a few places where that hypothesis is explicitly mentioned.
Answer: Despite perhaps the fact that the following references are too "elementary" (in a sense to be precise below) respect to the needs of the Asker, I cannot refrain to advise at least to have a look at the following two masterpieces
[1] Stanisław  Łojasiewicz, Introduction to complex analytic geometry,  translated from the Polish by Maciej Klimek, (English), Basel-Boston-Berlin: Birkhäuser Veralg, pp. xiv+523 (1991), ISBN 3-7643-1935-6, MR1131081, Zbl 0747.32001.
[2] Hassler Whitney, Complex analytic varieties, (English)
Addison-Wesley Series in Mathematics. Reading, Massachussets-Menlo Park, California-London-Don Mills, Ontario: Addison-Wesley Publishing Company. XII, 399 p. (1972), MR0387634, Zbl 0265.32008.
A brief (scant...) description of the approach of these texts.
From the preface of Łojasiewicz [1], p. v:

The subject of this book is analytic geometry understood as the geometry of analytic sets (or, more generally, analytic spaces), i.e., sets described locally by systems of analytic equations.
... Its aim is to familiarize the reader with the basic range of problems, using means as elementary as possible.

Whitney [2] p. vii, is even more straightforward:

The purpose of the present book is to provide a foundation, with reasonably elementary methods, for the theory of analytic varieties and spaces.

These works are textbooks, meaning that their aim is to teach the topic from the scratch, by using only basic prerequisites. Nevertheless they are advanced ones in that their aim is to provide a sound introduction to the field to future researchers. Regarding their contents,  I've arranged below a brief description of what you'll find (and of what you'll not find) there:

Both books start from a very elementary level. Whitney has an introductory chapter on holomorphic functions and then goes to the very heart of the matter i.e. to analytic varieties. On the other hand, Łojasiewicz starts with tre chapters on preliminaries (A on algebra, B on topology and C on complex analysis) and in chapter 1 describes the rings of germs of holomorphic functions: analytic varieties are the topic of the second chapter.

Both the books do not use the language of sheaves nor the one of currents (as instead Chirka does in his wonderful monograph cited by Moishe Kohan in his comment above)

Finally a personal observation: the main reason I like these works is that they make you feel you are understanding what the topic is about, this does not means that they are easy, just that they are masterful expositions.

Well, my two cents.

Textbooks on Complex Analytic Spaces


DELPHI, TADOQuery, TClientDataSet

Question: Quero checar o retorno da consulta, mas sempre vem como null.
// Inicio consulta
  dm.qryConsMovCx.Close;
  dm.qryConsMovCx.SQL.Clear;
  dm.qryConsMovCx.SQL.Add('select SEQ');
  dm.qryConsMovCx.SQL.Add('from tabela');
  dm.qryConsMovCx.SQL.Add('where data =:pData');
  dm.qryConsMovCx.Parameters[0].Value := DateToStr(date);
  dm.qryConsMovCx.Open;

  dm.cdsConsMovCx.Close;
  dm.cdsConsMovCx.Params.Clear;
  dm.cdsConsMovCx.Open;
  // Fim consulta, o select está ok.

  if not dm.cdsConsMovCx.IsEmpty then
   begin
     // Não entra nesse if.
   end
   else
   begin
     // trecho
     // Mensagem
   end;

Comment: qual banco que esta fazendo essa consulta, pergunto porque você tem que ver se a data esta indo no padrão aceito por ele. Acretido que o dm.cdsConsMovCx.Params.Clear; esta linpando a consulta antes do seu if
Comment: Utilizo oracle, essa estrutura faz parte, mesmo assim retirei e continua sem retornar.
Answer: Qual banco que esta fazendo essa consulta?
pergunto porque você tem que ver se a data esta indo no padrão aceito por ele.

dm.cdsConsMovCx.Params.Clear;

Acredito que o esta limpando a query antes do seu if;
Comment: Uso o banco Oracle. Essa linha faz parte da estrutura, mas retirei para testar e ainda continua.
Comment: Certo, faz o if antes do Close da linha a cima, de repente esta limpando sua consulta ao fechar; Depois você abre e fecha a query. Mas olha tambem o padrao de data aceita.
Comment: Coloquei, fiz o teste mas ainda continua.
Comment: Se executar essa mesma Query direto no BD ela retorna resultado? O parametro esta recebendo valor?
Do que da para ver no código tudo indica que seja o o fechamento, limpeza de paremetros e reabertura da query que está fazendo ela se perder.
Como está usando um parametro só, sugiro que faça o teste usando ParamByName
Answer: Se o campo no banco de dados for do tipo Date ou DateTime, você não deve converter o parâmetro para string:
dm.qryConsMovCx.Parameters[0].Value := DateToStr(date);
Apenas passe:
dm.qryConsMovCx.Parameters[0].Value := date;
E como já comentado, essa parte do código não faz sentido nenhum. Deve ser removida:
 dm.cdsConsMovCx.Close;
 dm.cdsConsMovCx.Params.Clear;
 dm.cdsConsMovCx.Open;

Answer: Esse seu trecho aqui ate que tudo bem, só deve ficar atento ao formato da data usado, o formato pode variar de banco para banco e com isso você pode não estar trazendo informação alguma, experimente fazer uma busca FULL e validar e se retornar os dados o problema é a condição.
dm.qryConsMovCx.Close;
dm.qryConsMovCx.SQL.Clear;
dm.qryConsMovCx.SQL.Add('select SEQ');
dm.qryConsMovCx.SQL.Add('from tabela');
dm.qryConsMovCx.SQL.Add('where data =:pData');
dm.qryConsMovCx.Parameters[0].Value := DateToStr(date);
dm.qryConsMovCx.Open;
Olhe esse código a seguir que você fez em momento algum o seu client data set recebe a coleção de dados que vieram através da query, e voce usa ele no seu código a partir de então.
dm.cdsConsMovCx.Close;
dm.cdsConsMovCx.Params.Clear;
dm.cdsConsMovCx.Open;
// Fim consulta, o select está ok.
Um outro ponto, ele não vai entrar nesse if pq seu clientdataset não tem registros, esta vazio. voce não atribuiu nada a ele.
if not dm.cdsConsMovCx.IsEmpty then
begin
// Não entra nesse if.
end
else
begin
// trecho
// Mensagem
end;
Comment: Sua resposta pode ser melhorada adicionando informações fundamentais. Basta [edit] para adicionar mais detalhes, como citações ou documentação para que outras pessoas possam confirmar que sua resposta está correta. Você pode encontrar mais informações sobre como escrever boas respostas [na Central de Ajuda](/help/how-to-answer).

DELPHI, TADOQuery, TClientDataSet


In what sense is the length of a parameterized curve an area?

Question: A little confusion on my part. Study of multi variable calculus and we are using the formula for length of a parameterized curve. The equation makes intuitive sense and I can work it OK. But I also recall using the same integral with out the parameterizing to find the length of a curve where the first term of the square root in just one. The former formula is the general case.
Now for the question:  I had just previously used the integral for completing the quadrature i.e.  Find the area under a curve.   Is the single integral used for finding both area and length ?  I guess I am trying to unify the concepts in my mind to understand the context of how they are used and know the difference. Thank you.
Comment: You should think of the integral as a kind of (generalized) sum. How to interpret the results depends on *what* you are "summing".
Comment: From a numerical point of view, if $\partial B$ is the boundary of a bounded convex set in $\mathbb{R}^2$ then the length of $\partial B$ is the surface area of the strip  $\partial B \times [0,1]\subset\mathbb{R}^3$, for instance. Of course from a *dimensional* point of view a length (measure unit $m$) is always different from an area (measure unit $m^2$).
Comment: In the previous case, we also have $L(\partial B) = \lim_{\varepsilon\to 0^+}\frac{A(B+\varepsilon D)-A(B)}{\varepsilon}$ where $L$ stands for length, $A$ for area and $D$ for the unit disk in $\mathbb{R}^2$.
Answer: You are given a function $\phi:\>[a,b]\to{\mathbb R}$ representing some "intensity" depending on $x$. The integral
$$\int_a^b \phi(x)\>dx$$
then captures the "total impact" that this variable intensity has on the domain $[a,b]$.
If we are talking about the "area under a curve $y=f(x)\geq0$" then the intensity $\phi_{\rm area}(x)$ is just $f(x)$, because the area per "infinitesimal increment" $dx$ is $f(x)\,dx$. It follows that the total area under the curve between $x=a$ and $x=b$ is
$$\int_a^b\phi_{\rm area}(x)\>dx=\int_a^b f(x)\>dx\ .$$
If we are talking about the "length of a curve $\gamma: \ y=f(x)$" (or some other geometrical or physical quantity associated to a curve $\gamma$) then the intensity $\phi_{\rm length}$ in question is $\sqrt{1+f'^2(x)}$ because a certain geometrical and limit argument has shown that the length increment of $\gamma$ per "infinitesimal increment" $dx$ is $\sqrt{1+f'^2(x)}\,dx$. It follows that the total length of the curve $\gamma$ is given by
$$L(\gamma)=\int_a^b\phi_{\rm length}(x)\>dx=\int_a^b\sqrt{1+f'^2(x)}\>dx\ .$$
Answer: Yes. A single integral is the limit of a sequence of sums (having more and more terms) that approaches the quantity you want to find. You can find a curve's length by using sums of the lengths of smaller and smaller straight-line segments between nearby points on the curve. You can find an area by using sums of the areas of narrower and narrower rectangles underneath the curve. Just what goes inside the integral depends on a little geometry, and there are assumptions that can be shown are sufficient to guarantee that the limit (the integral) does approach the thing you are calculating, but basically in both cases you are finding a quantity by using approximations that are sums and then taking a limit as the approximations get more and more accurate.


Error while importing images to Google storage in Cloud AutoML Vision

Question: I am using the google AutoML vision api object detection option in google console.When I try to upload the images to google storage to train the model, I am getting "Failed to import" error.
I have tried using the csv file option as well but the same error persists.
Since I am using the object detection model in google console, so it does not require to write any code.
The expected result is that the images are imported successfully so that I can move to next step to train my model.
Answer: I was facing same problem when upload a dataset from cloud storage (Select a CSV file on Cloud Storage) it shows this error message.
Failed to import data
However, I checked the IMAGES section and the dataset was there, so it was successfully uploaded regardless the error message (you can ignore it).


Comparing distribution of a biomarker among patients with overlapping clinical features

Question: I have a question how to compare the distribution of one tested biomarker among patients with different overlapping clinical syndromes.
In my case, I am investigating patients with multiple sclerosis and their clinical symptoms can be attributed to several different syndromes, for example pyramidal, sensory, etc. Moreover, one patient may have two or even 3 syndromes. In my datasheet, I created columns with different syndromes (5 in total), and I encoded all my patients as positive (1) or negative (0). At this moment, I want to compare if the distribution of the tested biomarkers differ among these syndromes. Until now, I was able only to evaluate if it differs within one group, for example among patients with pyramidal syndrome and without it (using Mann-Whitney U test as data is non-parametric). However, this kind of comparison does not allow me to evaluate the overlap of these syndromes as the patient with pyramidal syndrome may also have sensory and brainstem symptoms.
How should I organise my data and what statistical test should I perform to evaluate the distribution of biomarker between different syndrome while keeping in mind their overlap?
Comment: Welcome to CV.  One note: Data cannot be parameteric or non-parametric, that's a quality of tests and models.  Also, can you show a sample of what your data looks like and tell us e.g what your sample size is and about how many people have more than one syndrome, and which pairs of syndromes are common?
Comment: Thank you for your answer, @PeterFlom. My sample size is small, only 19 patients because it is a pilot study, preliminary data. The amount of patients positive for one syndrome varies between 1 and 9. You syndromes (pyramidal and sensory) is the most common pair, however, a few patients also have an additional one (3 syndromes at once), while other are positive only for one of them.
Answer: In your comment you say that you have 19 patients. With this few, there are very few statistical models you can run without risking overfitting, and statistical tests are unlikely to work very well unless the result is obvious. You could run single t-tests or U tests, but you have already done that.
Since you note that this is a pilot study, I would just give means and SDs (or medians and IQRs) and make a few plots, to show what is going on and demonstrate the need for more data.
The very fact that you have patients with multiple syndromes is a sign that you will need more data if you want to make good models and tests.


Keyboard fingering: jumps vs spread fingers

Question: In short and in general, my question is:
When choosing the best fingering, is it better to add more jumps to avoid fingers spreading, or spread fingers to reduce amount of jumps?
There are particular examples where it happens to be hard to choose for me.
Example 1 (red marked on picture)
Initially I preferred to play it with 4-2-1 because my 4th finger kept its position and it was easier to hit the key.
But then I found that if I played it with 5-2-1 it made my arm more relaxed (and it seems like a more "usual" position), but it required some extra effort to avoid mis-hitting.
Example 2 (green marked)
Again, 4-1 helped me to play easily without looking at the keyboard, because my 4th finger kept its position.
But the 5-1 option made my arm less tense, and then again, playing an octave with 5-1 seemed more usual for me. But it added an extra position change (a little one, but nonetheless).
Example 3 (blue marked)
Here I wanted to play it with 4-2 to keep my 2nd finger here for the next note, and to keep my 1st finger on the low A-note from the previous bar (left blue bracket) and move it out slightly only on the end of this bar (right blue bracket). In this case I didn't have to look at the keyboard at all.
The fingering I finally chose made my arm more relaxed, but added 3 position changes instead of keeping my arm in one position.
So, in all these cases I chose to keep fingers together in more relaxed way, but the cost was that I added some extra position changes and now it requires extra accuracy to not miss the keys. Did I get it right or wrong (given the reasoning above)?
Comment: I actually ended up picking some fingerings for trickier pieces for reasons other than the ones you mentioned - for example, better ability to play the passage of dyads legato or staccato, or better accuracy of triads that share some notes.
Comment: What is the piece and composer?
Comment: @Aaron it's the "Cadets, advance!" movie OST, "she song about love" by Viktor Lebedev
Comment: ...and the alternative name of song is "the earth where so many farewells" (i hope i translate it right))
Answer: Unfortunately, there is no general rule for fingering. It's entirely dependent on the musical context and one's hand size/shape. The only general advice is that it's best to stay as relaxed as possible.
Example 1
I prefer, and recommend, the 1-2-5 fingering for example the reason you've stated: it allows my hand/arm to be more relaxed. There's nothing wrong with 1-2-4, and a person with very long fingers, for example, might prefer it, but for me, 1-2-5 is more relaxed.
The key to making this work is to make sure finger 5 relaxes after playing C in the preceding triplet. If the finger remains stiff, it will be harder to get it in place for the marked chord. The same is true for the other fingers that play the triplet: once their job is done, they must fully release/relax, to ensure ease of movement to the chord.
Example 2
Again I would prefer 1-5 for the reasons already given.
Example 3
Here I would use 2-5. It both keeps my hand most relaxed but also keeps my thumb ready for the upcoming lower notes.
Additional suggestions
The small position changes involved in the various examples are such that with time and experience they will become easier to feel, to the point where they will become natural.
However, situations like these are also fairly common — where one finger is easier because it's nearby, but another finger allows for more relaxation and/or a better transition to the upcoming notes. In these cases, the finger switch is invaluable. In example 1, this would mean playing initially with 1-2-4 but then "sliding" finger 5 to take finger 4's place. In this way, the initial execution is more convenient, but the substitution of finger 5 allows the hand to relax.
The technique takes some time to get used to and to execute smoothly, but once it feels natural, it can be used in many, many places.


Problema ao mostrar dados XML em HTML

Question: <?xml version="1.0" encoding="ISO-8859-1"?>
<CATALOG>
    <CD>
        <TITLE>Empire Burlesque</TITLE>
        <ARTIST>Bob Dylan</ARTIST>
        <COUNTRY>USA</COUNTRY>
        <COMPANY>Columbia</COMPANY>
        <PRICE>10.90</PRICE>
        <YEAR>1985</YEAR>
    </CD>
    <CD>
        <TITLE>Hide your heart</TITLE>
        <ARTIST>Bonnie Tyler</ARTIST>
        <COUNTRY>UK</COUNTRY>
        <COMPANY>CBS Records</COMPANY>
        <PRICE>9.90</PRICE>
        <YEAR>1988</YEAR>
    </CD>
</CATALOG>

Este é código XML e de seguida coloco o meu HTML.

O problema é que ao abrir a página e ela vem em branco, como resolver isto?
Comment: Tenta fazer debug com `alert("MensagemDeAlerta")` no JavaScript para ver onde e se rebenta em algum lado.
Comment: Ambos os códigos não funcionam no Chrome. Qual seria o problema?
Answer: Amigo, substitui o seu código JavaScript puro por outro utilizando jQuery.
Dessa forma fica um pouco mais atual.
Note que é precisa chamar o jQuery.js para o código funcionar :)

Comment: Thomas, bem vindo ao SOpt. Nossa comunidade é um pouco diferente de fóruns como você viu no [tour](http://pt.stackoverflow.com/tour). Se possível, acrescente mais detalhes referente a sua resposta, e.g o que foi alterado e por que. Talvez [esse link da nossa central](http://pt.stackoverflow.com/help/how-to-answer) seja útil.
Comment: eu fiz esse código em HTML mas o resultado foi uma pagina em branco. Nao sei se me consegue ajudar nisso
Comment: Estranho, aqui funcionou... verifique se o arquivo contactos.xml está na mesma pasta desse arquivo html e verifique qual é o retorno que aparece no console javascript

Problema ao mostrar dados XML em HTML


Angular Compomente Personalizado emitEvent: false

Question: Eu preciso que no meu componente personalizado quando efetue um atribuição de valor ele não emita eventos, quando eu setar isso no patchValue ou setValue.
patchValue  { onlySelf: true, emitEvent: false }
StackBlitz Demo
ao setar o valor inicial ele dispara o this.onChangeCallback(v)
set value(v: any) {
   this.onChangeCallback(v);
}

onChangeCallback: (_: any) => void = () => {};

ignorando o emitEvent: false
Answer: Após o carregamento do seu formulário é emitido um único evento para o lastName que escreve uma mensagem no console:
event.last.name Reis

Isto ocorre porque seu segundo input é um componente customizado (app-input-text) que implementa o ControlValueAccessor. O problema é que a função que é passada como parâmetro pelo framework no método writeValue não recebe o segundo parâmetro do método this.form.patchValue. Inclusive há uma questão aberta sobre este comportamento neste link.
Você poderia tentar um workaround utilizando uma variável booleana por exemplo, ou alterar seu código de modo a não precisar deste recurso.
Answer: Resolvido o problema era no writeValue
troquei
writeValue(v: any): void {
  this.value = v;
}

por
writeValue(v: any): void {
 if (v !== this.innerValue) {
   this.innerValue = v;
 }
}


Jira Rest API JQL query

Question: I'm making use of the Jira API with the following call:
https://site.url/rest/api/2/search?jql=project=PROJECT&updated>=startOfWeek(-1w)
When I run this, I'm getting over 6000 results. But when I run the jql query of project = PROJECT AND updated >= startOfWeek(-1w) inside of my Jira sites search bar, I only get around 60 results.
Is there something I'm missing in my api call that would limit the returned to the data to the above query?
Edit
Looking further it appears my call is only bringing back results from my project space and not using the updated query. What should I do so it picks up both?
Answer: There is a typo in your query. You have used an ampersand instead of the word 'and'. The ampersand is the character used to add query parameters, so you effectively did this query
https://site.url/rest/api/2/search?jql=project=PROJECT

then Jira just ignored what came after the ampersand, as it didn't know what the parameter 'updated>' was, or how to make it equal 'startOfWeek(-1)'
Within the JQL, you must use the word 'and' with spaces before and after, like this:
https://site.url/rest/api/2/search?jql=project=PROJECT and updated>=startOfWeek(-1w)

Only use ampersands to add other query parameters afterwards, like this:
https://site.url/rest/api/2/search?jql=project=PROJECT and updated>=startOfWeek(-1w)&startAt=0&maxResults=500

Answer: Please send the API as POST method:
API: https://url/rest/api/2/search

Body:
{
"jql": "project='project name'&updated>=startOfWeek(-1)"
}

Jira Rest API JQL query


Why Java locales are not constants?

Question: Java supports the Locales shown in this link.
But I can't figure out why only some countries such as France, Canada, China and United States and some languages such as Italian, Japanese and German have constants for their locales.
Everytime a need a locale, I consult the table of supported locales and do the following, using Strings:
Locale locale = new Locale("pt", "BR");

Why only some locales as constants?
Is there a Java library which provide constants for locales?
Comment: You can always make your own constant ;)
Comment: When you say constant, what do you mean? The Locale attributes in java.util.Locale are static, but not constant. What is the constant bit that you are referring to?
Comment: Constant means `(public/private) static final TYPE NAME = VALUE;`. `Locale.ITALY`, for example, is documented as "Useful constant for country".
Answer:
Why only some locales as constants?

The javadoc for Locale says this about the constants:

"The Locale class provides a number of convenient constants that you can use to create Locale objects for commonly used locales."

The obvious implication is that Locale constants have not been defined for other countries and languages because the locales were thought at the time to be "less commonly used" in Java programs.  Or at least, not common enough to warrant adding the corresponding constants.  (I have no idea if this decision was made based on objective evidence or not.)
Be aware that there would be performance overheads in adding lots more Locale constants.  Not to mention all sorts of compatibility issues due to countries changing names, etcetera.  So, basically, they had to stop somewhere ... and the current set is where they stopped.
The other thing to note is that these constants are merely a convenience; i.e. a simple way to get Locale objects that you can always get by other means; e.g. by using a Locale constructor, or by calling Locale.lookup(...).

Is there a Java library which provide constants for locales?

AFAIK, no.  And it seems to me like a bad idea, for the reasons above.
And, of course, you can always define your own class containing Locale constants that are convenient for your applications.

For the record, I found an RFE in the Java Bug Database (in the Google cache) that relates to this:

JDK-4399080 : RFE: java.util.Locale.SPANISH and java.util.Locale.SPAIN does not exist

This RFE was reported against Java 1.3.0, and was closed as "Won't Fix" with the following Evaluation:

We decided a while ago not to add more predefined locale constants. It's easy enough to write new Locale("es"); or new Locale("es", "ES"); so there's no real benefit in adding more constants - especially since it's not feasible to cover all languages and countries.
norbert.lindenberg@Eng 2001-01-05
Answer: Although "why" questions are open-ended, I think it's safe to say that the constants probably exist only for backward compatibility. Here is Oracle's article about Internationalization, specifically, the pre-constructed locales.
As you can see, the author freely admits that the provided constants do not cover all the "Important" countries (from whatever perspective that may be), and that they shouldn't be used at all.
The constants today are the same as they were in JDK 1.1, so I believe they are kept only for backward compatibility.
Answer: Why only some locales as constants?
It was really a mistake to make any locales constants (as RealSkeptic's answer shows). Countries are not constant. They come and go, and what was once a country may end up consigned to the history books and no longer commonly used (e.g. recently USSR, Yugoslavia). It would be a mistake to have a convenient Locale constant for a country that no longer exists.
You may say, "well so what, let's just put all countries in as constants, and if they cease to exist, where's the harm"? The problem there is that new countries also come into existence all the time (e.g. East Timor, Kosovo). Either new constants have to be continually added to the list, which would bring its own compatibility issues, or the constant list would remain (as it is now) frozen in time, representing only countries that existed when JDK 1.1 came out.
Answer: Because Locales are not constant. Remember the episode of family guy where Peter founded the Country "Petoria"?
Comment: @troig Yes, check out another creative question: https://stackoverflow.com/questions/55433978/

Why Java locales are not constants?


Como enviar dados para o constructor?

Question: Preciso que me ajudem a entender a parte da programação em objectos.
Precisava de passar um valor que não estivesse dentro do objecto teste para saber qual era a escolha do utilzador.
Mas mais uma vez sou iniciante em objectos.
Como tenho que chamar na minha actividade principal para receber a escolha ?
public class organizadist {

    public organizadist()
    {

    }

    public teste[] inicializa(teste[] objecto) {
// o que eu queria receber
if (escolha==1){
// faz alguma coisa
}
if (escolha==2){

//faz outra coisa

    }

    public static class teste {
        public int ola;

    }

}

Answer: Simples aprendi isso ontem curiosamente.
Na sua actividade :
 organizadist enviadados = new organizadist();
                organizadist.Teste array [] = enviadados.inicializa(listaClothes ,  escolha);

No seu constructor:`
public teste[] inicializa(teste[] objecto , int escolha);

Comment: Acho que não está a funcionar pelos testes que fiz aqui mais tarde volto com mais info
Answer: Você pode fazer da seguinte forma para passar valor usando construtor.
public FrmDetalheRet(string pstrAba, int pnuNuped, int pnuCdRetPedVda)
{
   InitializeComponent();

  if (pstrAba == strConstReceb)
     //this.dgvReceb.Columns["colCdPRod"].Visible = false;
  if (pstrAba == strConstPed)
     // this.dgvReceb.Columns["col2"].Visible = false;

  string strMsg = string.Empty;
  bool booRetorno = false;

  BLLDetItPedv bLLDetItPedv = new BLLDetItPedv();
  DataTable  dt = bLLDetItPedv.SelectDadosDetItPedvDAL(out strMsg, out    booRetorno, pnuNuped, pnuCdRetPedVda);

  if (!booRetorno)
  {
     MessageBoxHelper.WarningMessage(strMsg);
     return;
  }
  else
  {
     PopulaTabela(dt);
  }
}

// Na chamada da classe você passa os parâmetros necessários para o construtor:
FrmDetalheRet frmDetalheRet = new FrmDetalheRet(strConstPed,(int)dgvPedido["colNuPed", dgvPedido.CurrentRow.Index].Value,                    (int)dgvPedido["colCdRetPedVda", dgvPedido.CurrentRow.Index].Value);
frmDetalheRet.Show();

No exemplo acima na classe eu faço algumas operções com os valores que foram passados como parâmetros.
Comment: Obrigado Lucas Queiroz Ribeiro, estou pegando o jeito de como postar código formato.

Como enviar dados para o constructor?


Convergence of $\frac{a_{n+1}}{a_n}$, where $|a_{n+1}a_{n-1} - a_n^2| = 1 $

Question: Let $(a_n)$ a non-decreasing sequence of positive real numbers such that $\lim a_n = \infty$ and $|a_{n+1}a_{n-1} - a_n^2| = 1 $. Prove that the sequence $(\frac{a_{n+1}}{a_n})$ converges.
My little ´´progress´´:
$|a_{n+1}a_{n-1} - a_n^2| = 1 $.
$\left|\dfrac{a_{n+1}a_{n-1}}{a_n^2} - 1\right| = \dfrac{1}{a_n^2} \to 0$
$\dfrac{a_{n+1}a_{n-1}}{a_n^2} \to 1$
$\dfrac{a_{n+1}}{a_n}\dfrac{a_{n-1}}{a_n} \to 1$
Answer: We separate two cases.

Case 1: for every $n \geq 0$, we have $a_{n + 1} - a_n \leq 1$.
In this case, since the sequence $(a_n)_n$ tends to infinity by assumption, we have $\lim\limits_{n\rightarrow\infty}\frac{a_{n + 1} - a_n}{a_n} = 0$, and it follows that $\lim\limits_{n\rightarrow\infty}\frac{a_{n + 1}}{a_n} = 1$.

Case 2: there exists $m\geq 0$ such that $a_{m + 1} - a_m > 1$.
We then have $a_{m + 2} \geq \frac{a_{m + 1}^2 - 1}{a_m} > \frac{a_{m + 1}^2 - 1}{a_{m + 1} - 1} = a_{m + 1} + 1$. Hence by induction, we have $a_{n + 1} - a_n > 1$ for all $n \geq m$. This implies $a_n > a_m + (n - m)$ for all $n > m$.
Without loss of generality, we may assume that $m = 0$ (otherwise just consider the sequence $(b_k)_k$ with $b_k = a_{m + k}$). We then have $a_n > n$ for all $n \geq 1$.
Now rewrite the equation in the question as $\frac{a_{n + 1}}{a_n} - \frac{a_n}{a_{n - 1}} = \pm \frac 1{a_{n - 1}a_n}$. Thus for any $0 < n < N$, we have: $$\frac{a_{N + 1}}{a_N} - \frac{a_{n + 1}}{a_n} = \pm \frac 1{a_na_{n + 1}}\pm\frac1{a_{n + 1}a_{n + 2}}\pm \dotsc\pm \frac1{a_{N - 1}a_N}.$$ Taking absolute value, we get: $$\left|\frac{a_{N + 1}}{a_N} - \frac{a_{n + 1}}{a_n} \right| \leq \sum_{n < k \leq N}\frac1{a_{k - 1}a_k} <\sum_{n < k \leq N}\frac1{(k - 1)k} < \frac 1 n.$$
This shows that the sequence $\left(\frac{a_{n + 1}}{a_n}\right)_n$ is a Cauchy sequence and hence converges.


$\frac{x^2y}{z}+\frac{y^2z}{x}+\frac{xz^2}{y}\geq x^2+y^2+z^2$

Question: Let $x,y,z$ be lengths of three sides of triangle. Show that $\frac{x^2y}{z}+\frac{y^2z}{x}+\frac{xz^2}{y}\geq x^2+y^2+z^2$. This seems simple but not easy. I try to use RAVI replacement but it doesn't work.
I think it has a relation to the inequality $x^2y(x-y)+y^2z(y-z)+z^2x(z-x)$ ( which appears  in $IMO$ $1983$.). Give me some idea to solve this, thank you so much!
Comment: The inequality is not rotational. Are you sure the question is correct?
Comment: @HwChu oh i am sorry so much. I have already change my mistake.
Comment: I can write your inequality in SOS form but very ugly.
Answer: The hint.
Use the BW:
Let $x=a+u$, $y=a+v$ and $z=a+u+v$, where $a>0$, $u\geq0$ and $v\geq0.$
After this substitution you'll get something obvious.


How to hit a high draw?

Question: My natural shot is a fade. I don't have a problem hitting (or feeling) a high, normal, or low fade.
However, I have no feel, but I can hit a draw. With a 5-iron, I can hit a low draw and chase it about 120-150 yards. My typical 5-iron goes about 200 yards.
How do I hit a high draw? I think it's a cool shot, and I want to be more comfortable when I attempt to hit a draw in general.
Comment: That is more than a cool shot.  It's beautiful!
Comment: @Ste Gorgeous even!
Comment: Update: Although I cannot hit the high draw as depicted here, I have developed a go-to, low-sweeping draw with a 5-iron (when and if the situation arises). I will usually hit it 120-150 yards with 50 yards of hook and 50 yards of roll. I know draws/hooks are hard to control, but I have gotten a hold of this one, and have used it in the past to get out of trouble.
Answer: A draw (or hook for those who can't control it) is accomplished by squaring the clubface perpendicular to the line of your body, then swinging from inside to out. As the clubhead moves from the left to the right (for a right-handed golfer) across the ball, it will induce the sidespin that makes the ball curve left.
To do this, raise your backswing, and think about swinging down, then out at the ball as you swing through. Be careful to still strike the ball with the sweet spot; if you hit off the toe, it'll push or worse. Another method is to move the ball towards your trailing foot, and close the clubface slightly; the clubhead will move in a circle, and so its tangent direction further back in the swing will be more outward. However, the further back you move it, the higher the clubface will be in its arc at impact, so the ball will launch lower; if you want a "high" draw, you're going to want the ball to be more forward in your stance, so you hit it as you come up out of the bottom of the swing.
If this is a tee shot, you may also tee it a bit higher to add some loft; the more of the club that's under the ball when it hits, the higher the ball will launch. Don't go crazy high, though, or the club can pass right under the ball, obliterate the tee, and send the ball pretty much straight up.
Understand the clubface must remain square to the ball. If you open the clubface, you'll bring it in line with the swing path and the result will be a straight push. If you close it, you'll launch the ball left and it will curve "lefter"; a "snap hook".
Comment: So, if you want the highest, straightest-launching draw you can get, you want a square clubface. You can go higher by opening the clubface, but that then requires you to swing even further outside, first to simply get enough sidespin to curve the ball inside, and second to overcome the initial outside launch. If you go this route, I would also open your stance, which would aim the ball more toward your side of it at address.
Comment: Found time to go to the range today. It doesn't go as high or draw as much as I want to. However, I'll be working on this shot and get some reps in.
Comment: I think the high-draw 8-iron would launch about the same as a straight 7-iron, because at impact the 8-iron would be slightly closed relative to its swing path thus imparting less backspin, more sidespin. I deleted my comment to re-format it so I'll simply restate: the amount of sidespin is actually a function of the difference between the swing path and clubface angles. The shot will draw (curve inside) when the swing path is outside the clubface angle. You get much more radical angles (more curve) by closing the clubface; the problem is that a closed clubface launches lower and to the left.
Comment: One thing I used to do is set my feet for a fade, *then* open my clubface. I used to be appaled that it would push rather than fade... No wonder why my attempt at draws were knee-high at best with this thinking. Would you prefer to focus on where your feet or your club is pointing at address? (we know that we cannot do both or that would lead to a pull or push)
Comment: You actually do have to focus on both. Unfortunately, "open" and "closed" are subjective terms, and it's the combination of the relative openness or closure of various parts of your stance and swing that determine how the clubface is pointing and moving when it hits. Basically, the higher you want it, the more open the clubface has to be (or the deeper under the ball you will have to get), but the more open the clubface is, the more outside it will launch and the less spin your inside-out path will induce. So, you have to keep the clubface angle pointing left of the swing path no matter what.
Comment: Interesting. Good stuff.
Comment: Opening your stance by aiming the line of your toes in the direction of the intended draw will correct the outside launch caused by the open clubface. However, it will also make the required swing path feel ridiculous; you'll feel like you're swinging from behind your back to out in front the way you're facing.
Answer: From Golf.com:

"...requires a wide, shallow swing path. Place the ball forward in your
  stance, off your left armpit, which will allow you to take greater
  advantage of the upward path of your driver at impact, and close your
  stance about an inch to invite a right-to-left ball flight.  During the takeaway, focus on keeping your hands in front of your body all the way up to the top — this will prevent you from taking the club up and down on a steep path, which is a killer when you're trying to hit a high shot... And there's no need to turn your right arm over your left at impact in an effort to impart draw spin to the ball — your slightly closed stance will take care of that."

There are a few more tips on that page, but I think this is the gist of it.
Comment: Do you have any experience of your own you would like to add?
Comment: I'm not a great player by any stretch of the imagination, and honestly haven't really tried to work on this shot.  But conceptually it sounds good and makes complete sense to me.
Comment: Matt, this would 100x better if you could add some more commentary, maybe even summarize another tip or two from that page.
Comment: Likewise. My issue is not being able to **feel** the shot. It is so unnatural to me. I've watched instructional videos and practiced...and I've come a long way. This makes sense to me also, but the fact that my swing is mental and mechanical on draw shots is what gets me.
Comment: @Matt What if I want to hit a draw with an iron (see "cool shot" in question)? How would you "close your stance?"

How to hit a high draw?