Validation on custom entity edit form

Question: I have a custom entity where, instead of using the usual way of specifying edit form options with setDisplayOptions('form' and relying on ContentEntityForm to create my edit form for me, I have to make my own form in buildForm() (I have Ajax interactions between the fields that the stock from can't provide). It works all right, just that I can't trigger validation as with the stock form. The constraints are there and if I override:
public function validateForm(array &$form, FormStateInterface $form_state) {
  $entity = parent::validateForm($form, $form_state);
  $violations = $entity->validate();
  foreach ($violations as $v) {
    dpm($v->getMessage());
  }
  return $entity;
}

the validation errors are in fact found and listed, just the form won't display the usual red warnings, keeping the user from going on. Can I reconcile the custom form building with the automatic validation?
Answer: The User entity is one of the Drupal core entities that programmatically adds form elements in their entity form class and still uses validation constraints.
AccountForm::form() adds the form elements and User::baseFieldDefinitions() adds the validation constraints to the entity fields.
(AccountForm is the class extended by the ProfileForm and the RegistrationForm classes, which are two of the three entity form classes used for the User entity.)
  // Account information.
  $form['account'] = [
    '#type' => 'container',
    '#weight' => -10,
  ];

  // The mail field is NOT required if account originally had no mail set
  // and the user performing the edit has 'administer users' permission.
  // This allows users without email address to be edited and deleted.
  // Also see \Drupal\user\Plugin\Validation\Constraint\UserMailRequired.
  $form['account']['mail'] = [
    '#type' => 'email',
    '#title' => $this->t('Email address'),
    '#description' => $this->t('A valid email address. All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email.'),
    '#required' => !(!$account->getEmail() && $user->hasPermission('administer users')),
    '#default_value' => !$register ? $account->getEmail() : '',
  ];

  // Only show name field on registration form or user can change own username.
  $form['account']['name'] = [
    '#type' => 'textfield',
    '#title' => $this->t('Username'),
    '#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
    '#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
    '#required' => TRUE,
    '#attributes' => [
      'class' => [
        'username',
      ],
      'autocorrect' => 'off',
      'autocapitalize' => 'off',
      'spellcheck' => 'false',
    ],
    '#default_value' => !$register ? $account->getAccountName() : '',
    '#access' => $account->name->access('edit'),
  ];

  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Name'))
    ->setDescription(t('The name of this user.'))
    ->setRequired(TRUE)
    ->setConstraints([
    // No Length constraint here because the UserName constraint also covers
    // that.
    'UserName' => [],
    'UserNameUnique' => [],
  ]);
  $fields['name']
    ->getItemDefinition()
    ->setClass('\\Drupal\\user\\UserNameItem');
  $fields['pass'] = BaseFieldDefinition::create('password')
    ->setLabel(t('Password'))
    ->setDescription(t('The password of this user (hashed).'))
    ->addConstraint('ProtectedUserField');

The AccountForm, ProfileForm, and RegisterForm classes, which extend the [ContentEntityForm][6] class, doen't extend [ContentEntityForm::validateForm()][7], though. They implement methods that is necessary to Drupal to understand which entity fields are edited and which violations should be shown:  [AccountForm::getEditedFieldNames()][8] and [AccountForm::flagViolations()`]6.
protected function getEditedFieldNames(FormStateInterface $form_state) {
  return array_merge([
    'name',
    'pass',
    'mail',
    'timezone',
    'langcode',
    'preferred_langcode',
    'preferred_admin_langcode',
  ], parent::getEditedFieldNames($form_state));
}

protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
  // Manually flag violations of fields not handled by the form display. This
  // is necessary as entity form displays only flag violations for fields
  // contained in the display.
  $field_names = [
    'name',
    'pass',
    'mail',
    'timezone',
    'langcode',
    'preferred_langcode',
    'preferred_admin_langcode',
  ];
  foreach ($violations->getByFields($field_names) as $violation) {
    list($field_name) = explode('.', $violation->getPropertyPath(), 2);
    $form_state->setErrorByName($field_name, $violation->getMessage());
  }
  parent::flagViolations($violations, $form, $form_state);
}

ContentEntityForm::validateForm() shows only the validation errors for those form elements that are reported to be edited. This means that if a form element name isn't returned from AccountForm::getEditedFieldNames() or ContentEntityForm::getEditedFieldNames(), that form element and the corresponding entity field aren't considered edited.
To answer the question: Yes, it's possible to add form elements in the form() method of the entity form class and use validation constraints (and their "automatic" validation), as long as the entity form class implements the  getEditedFieldNames() and flagViolations() methods.
Comment: Very nice, thank you, I'll go this route.

Validation on custom entity edit form


Run a python unittest only if another one passes

Question: I'm new to unit testing and I am writing a class to model the behavior of a pinhole camera.
I have several unit tests in Python using the unittest module, and my Camera class has a lookAt method which makes it look at a certain point, i.e it rotates the camera frame such that it points at the target.
I also have a method which projects a 3D point into the camera's picture.
My scenario here is that i first test the lookAt method, then if the test passes, i use the lookAt method to make my camera look at a cube and then project that cube onto my camera's picture (the projection is the thing to test here).
Is there a possibility in the unittest module for me to run a test in another test? Or should i just make a lookAt matrix by hand and then test the projection separately(I think this would be the best practice here)?
Comment: You can use 2 asserts in the same test function. If the first one fails, then that test will stop and the second one won't get evaluated.
Answer: Take a look at the skipIf decorator and applying it to your dependent tests based on a condition set in your first test.
<code>@unittest.skipIf(condition, reason)
</code>
For example (from the above link):
<code>    @unittest.skipIf(mylib.__version__ < (1, 3),
                 "not supported in this library version")
</code>
There is also subTest(), but the docs describe the purpose of subTest as being for "When some of your tests differ only by a some [sic] very small differences."
Comment: Which pattern you use depends on how you want the results represented. Multiple asserts in a single test still roll up as 1 test that passed or failed, and anything after the first assertion failure will not be executed. If you want more granular results indicating the result of each, then you'll want to model them as separate tests.
Comment: I might go with Seaman's answer though, seems simpler and straight-forward, as there doesn't seem to be a possibility for me to control what order the tests are ran other than the order in which they are written. On a side note, i believe it is better to manually write the expected output of a lookAt function and use it in my projection test function, that way tests do not depend on one another.
Comment: I agree with you as i believe that is the best approach.


Views: Apply a wrapper to every 3 and 6 group of rows

Question: My Views Output is as follows

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

I would like to put a wrapper around every 3 and 6 group of Views rows.
So my mark up would look like this:

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

Content

How can I do this? I know that I have to use views-view-unformatted.tpl.php but I am not sure how to alter the $row variable to target every 3rd and 6th group.
Thanks in advance for any help that can be offered.
Answer: A few tips about the code:

You also can make the logic in the preproccess or proccess hooks if you prefer
To complete the information given by Jack: Modular arithmetic . This theory help us to build our strategy.

Aplication in a preprocces hook to separate the logic from the template:
function YOURTHEME_preprocess_views_view_unformatted(&$vars){

   $vars['prefix'] = array();
   $vars['suffix'] = array();
   $group = 1;
   $last_row = count($vars['rows']) -1;

   foreach($vars['rows'] as $id => &$row){

     $vars['prefix'][$id] = '';
     $vars['suffix'][$id] = '';

     // Apply modular arithmetic
     $remainder = $id % 9;

     // First div => 3 items
     if($remainder == 0){
       $vars['prefix'][$id] = "\n
";
     }

     if($remainder == 2){
       $vars['suffix'][$id] = '\n
';
       $group++;
     }

     // Second item => 6 items
     if($remainder == 3){
       $vars['prefix'][$id] = "\n
";
     }

     if($remainder == 8){
       $vars['suffix'][$id] = '\n
';
       $group++;
     }

     // Close the div in case there are not enough items
     if($last_row == id && $remainder != 2 && $remainder != 8){
        $vars['suffix'][$id] = '\n
';
     }

   }

}

Your template in you theme that will overwrite the default template provided by views:
<?php if (!empty($title)): ?>

<?php print $title; ?>

<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
  <?php if (!empty($prefix[$id])){ print $prefix[$id]; } ?>
    <div <?php if ($classes_array[$id]) { print 'class="' . $classes_array[$id] .'"';  } ?>>
    <?php print $row; ?>
  <?php if (!empty($suffix[$id])){ print $suffix[$id]; } ?>

<?php endforeach; ?>

Comment: Is there any way to avoid having to create a template file for this?
Answer: If you are using views-view-unformatted.tpl.php
you can achieve a goal with the foreach loop:
<?php
$group_nr = 1;                  // first group number
$last_row = count($rows) -1;    // last row
$wrapper  = 3;                  // put a wrapper around every 3 rows
?>
<?php foreach ($rows as $id => $row): ?>
<?php if ($id % $wrapper == 0) {print '
'; $i = 0; $group_nr++; } ?>
    <div class="views-row-<?php print $id+1; ?>">
        <?php print $row; ?>

<?php $i++; if ($i == $wrapper || $id == $last_row) print '
'; ?>
<?php endforeach; ?>

* In this example, every 3 rows will be wrapped in div with class "group-[group nr]"
If these rows should be wrapped every sixth row as well, then you will need add some logic to tell whether do not wrapp in third row, but wait for the sixth.
Hope this will help.
Comment: Thanks to Rufein I have noticed a lack of condition in case last row. It has been correted.
Comment: It's interesting to note that you can't have a consistent print function to output the views-row wrapper. It doesn't render if you try to print that.
Answer: Views Rows Wrapper module wraps every N rows with custom HTML element.
Answer: views-view-list.tpl.php might be a better option to wrap groups of results.
This template is used to output the results list, and might be a bit more concise scope wise.
Answer: Example in Twig for views-view-unformatted.tpl.php wrapping every 3 rows:
{#
/**
 * @file
 * Theme override to display a view of unformatted rows.
 *
 * Available variables:
 * - title: The title of this group of rows. May be empty.
 * - rows: A list of the view's row items.
 *   - attributes: The row's HTML attributes.
 *   - content: The row's content.
 * - view: The view object.
 * - default_row_class: A flag indicating whether default classes should be
 *   used on rows.
 *
 * @see template_preprocess_views_view_unformatted()
 */
#}
{% set wrapper_count = 3 %}
{% set wrapper_attributes = create_attribute({'class': ['group']}) %}
{% set row_classes = [default_row_class ? 'views-row'] %}

{% if title %}

{{ title }}

{% endif %}

{% for row in rows %}
  {% if loop.index % wrapper_count == 1 %}

  {% endif %}

    {{- row.content -}}

  {% if loop.index % wrapper_count == 0 or loop.last %}

  {% endif %}
{% endfor %}


How to make multiple where queries while foreach

Question: I want to make more than one where on a query with Eloquent.
Basically you have this :
<code><?php

    User::where('gender', '=', 'male')->where('age', '=', '18')->get();

?>
</code>
So imagine you have a foreach which turns twice to concatenate that two where clause.
How could you do ?
<code>$user = User::where('gender', '=', 'male')
foreach ($array as $key => $value) {
    $user .= $user->where($key, '=', $value);
}
</code>
Do you see what I mean ? I know that my example is not possible at all.. but it's to explain.
Thank you !
Comment: I don't understand your question.
Comment: I edit my post maybe its gonna be clearer
Comment: Still don't understand. Give some real example, because it's hard to guess what you mean by concatenating the object.
Answer: You can do the following.
<code>$user = User::where('gender', 'male');

// Now add on a chain with loops or whatever expression you need.
$user = $user->where('field', 'value');

// Now get the results
$user = $user->get();
</code>


Getting error INSTALL_FAILED_CONFLICTING_PROVIDER

Question: I was trying to run the Leaf Explorer and at the end while installing the app, I am getting this error.

Installation failed due to: 'INSTALL_FAILED_CONFLICTING_PROVIDER:
Scanning Failed.: Can't install because provider name
org.monora.uprotocol.client.android.debug.fileprovider (in package
com.leaf.explorer.pro.debug) is already used by
com.genonbeta.TrebleShot.debug'

What can be the error here? I have tried many times and still the same error coming up? Please help on this.
Thank you.
Answer: If you're using app provider then change your authorities to dynamic
<code>        **android:authorities="${applicationId}.provider"**

</code>


Como usar variavel $wpdb no wordpress

Question: tenho esse código num arquivo php dentro da instalação do wordpress, nao funciona nao sei o que esta errado.
global $wpdb;

$tableName = "wp_posts";

$result = $wpdb->get_results("SELECT * FROM $tableName");

foreach ($result as $row) {

echo '
' .$row->coluna_teste. '
';

Comment: Está com exatamente esses parametros? porque realmente tudo está errado desse jeito
Answer: $wpdb::get_results() retorna uma lista de objetos por padrão, em que cada propriedade é uma coluna. A tabela wp_posts não tem uma coluna coluna_teste, por isso o erro.
O codigo abaixo está correto e vai trazer os títulos:
global $wpdb;

// Para tabelas padrãdo do WP Use $wpdb->nomedatabela para buscar o nome
// já com o prefixo, que pode não ser o mesmo em todos os ambientes
$result = $wpdb->get_results( "SELECT * FROM {$wpdb->posts} LIMIT 10" );

foreach ( $result as $row ) {
    echo '
' . $row->post_title . '
';
}

Documentação completa da classe $wpdb
PS: O resultado dessa consulta é o mesmo de get_posts(). Usar a função é preferível.

Como usar variavel $wpdb no wordpress


Neural networks output probability estimates?

Question: Suppose my training data contains ~100 variables, and each example is tagged as "success" or "failure".
I understand how a neural network can be used to try and predict success vs failure based on the variables.
However I am interested in the neural network outputting the posterior probability rather than success or failure. In fact, I evaluate the efficacy of the NN based on how accurate the probabilities are (eg. AUROC over the entire dataset) rather than % of cases with correct prediction.
Are NN's the right tool for the job here and if so how do you structure the NN to output this?
(NOTE: I'm a ML newbie!)
Comment: Some answers were given to this question in an [earlier post](http://stats.stackexchange.com/questions/44223/methods-cran-packages-to-predict-probability-using-neural-networks-or-others-m)
Answer: If your activation function is for example logistic, then it will output continuous value between 0-1, or you can use softmax in the case of multiple outcome variables.
Answer: Just for people who are still interested in this question:
Softmax of state-of-art deep learning models is more of a score than probability estimates.
Most deep networks nowadays are overconfident, check the follow paper if you are interested:
https://arxiv.org/pdf/1706.04599.pdf
Answer: In your NN, if you use a softmax output layer, you'll actually end up with an output vector of probabilities.  This is actually the most common output layer to use for multi-class classification problems.  To fetch the class label, you can perform an argmax() on the output vector to retrieve the index of the max probability across all labels.


How take two database lists in the same page?

Question: I create one controle to take menu itens in my database, but now i need list itens in the same page.

use Illuminate\Support\Facades\DB;

class ControllerPageViews extends Controller {

    public function home(){
        $menus = DB::select('select * from menus');
        return view('index')->with('menus', $menus);
    }

    public function comofunciona(){
        $menus = DB::select('select * from menus');
        return view('como-funciona')->with('menus', $menus);
    }

    public function especialidades(){
        $menus = DB::select('select * from menus');
        return view('especialidades')->with('menus', $menus);
    }

    public function exames(){
        $menus = DB::select('select * from menus');
        return view('exames')->with('menus', $menus);
    }

    public function medicamentos(){
        $menus = DB::select('select * from menus');
        return view('medicamentos')->with('menus', $menus);
    }

    public function solicitarconsulta(){
        $menus = DB::select('select * from menus');
        return view('solicitar-consulta')->with('menus', $menus);
    }

    public function contatos(){
        $menus = DB::select('select * from menus');
        return view('contatos')->with('menus', $menus);
    }
}

I go use elloquent after, but now, is possible import 2 lists in the same page ?
Answer: Use compact instead
public function contatos(){
$menus = DB::select('select * from menus');
$menus2 = DB::select('select * from menus2');
return view('contatos',compact('menus','menus2'));
}

and then on the view:
@foreach($menus as $menu)
{{ $menu->field_name }}
@endforeach

@foreach($menus2 as $menu2)
{{ $menu2->field_name }}
@endforeach


Show that $\mu((a,b)):=b-a$ finitely additive.

Question: This question is related to Show that $\mu$ is finitely additive and Measure on the set of rationals. However, the first post did not give convincing answer and the second post mostly focused on the $\sigma$-additivity.
The question arises from A. N. Shiryaev's "Probability" Page $138$ Problem 1. It states as follows:

Let $\Omega:=[0,1]\cap\mathbb{Q}$ be the set of rational points of $[0,1]$, and $\mathcal{A}$ the algebra of sets each of which is a finite sum of disjoint sets $A$ of one of the forms $(a,b)\cap\mathbb{Q}$, $[a,b)\cap\mathbb{Q}$, $(a,b]\cap\mathbb{Q}$, and $[a,b]\cap\mathbb{Q}$. And define $\mu(A):=b-a$. Show that $\mu(A)$ is finitely aditive but not countably additive.

Note that he does not require $a,b\in\mathbb{Q}$. For unknown reason, this question is quite confusing to me. I have two questions.

Firstly, I know that $\mu$ is not $\sigma$-additive in this case. Enumerate $[0,1]\cap\mathbb{Q}=\{x_{1},x_{2},\dots\}$. Then, $$E:=\bigcup_{n=1}^{\infty}[x_{n},x_{n}]\cap\mathbb{Q}=\bigcup_{n=1}^{\infty}\{x_{n}\}=[0,1]\cap\mathbb{Q},$$ and the union is disjoint. Therefore, $\mu(E)=1$. But for each $[x_{n},x_{n}]\cap\mathbb{Q}$, $\mu([x_{n},x_{n}]\cap\mathbb{Q})=x_{n}-x_{n}=0$, so $\sum_{n=1}^{\infty}\mu([x_{n},x_{n}]\cap\mathbb{Q})=0$. Hence, $\mu$ is not $\sigma$-additive.
But, how to show that $\mu$ is finitely additive? I mean... if you have $([a,b]\cup [c,d])\cap \mathbb{Q}$ where $[a,b]$ and $[c,d]$ are disjoint, how do you compute $\mu(([a,b]\cup [c,d])\cap \mathbb{Q})$? This disjoint union cannot form a new interval, right? Is $\mu$ even defined in this case?

Second, I know how to show that $\mathcal{A}$ is an algera. In fact, checking the closure under complement and under finite unions for those intervals is sufficient for us to extend the proof for any sets from $\mathcal{A}$. If $\Omega=[0,1]$, and those intervals are the usual real intervals (resp. rational intervals), then $\mathcal{A}$ can only be an algebra, since $[0,1]\cap\mathbb{Q}\notin\mathcal{A}$ because it has empty interior with respect to $[0,1]$.
But in this case, since every $[x_{n},x_{n}]\cap\mathbb{Q}=\{x_{n}\}\in\mathcal{A}$, it turned out that $\mathcal{A}$ contains every rational of $[0,1]\cap\mathbb{Q}$. This makes me wonder if $\mathcal{A}$ is actually a $\sigma$-algebra, since it is really close to $2^{[0,1]\cap\mathbb{Q}}$. But I cannot finish the proof since $\mathcal{A}$ only has finite union of those intervals, so it does not contain every subset of $\mathbb{Q}$. On the other hand, I cannot find a counterexample to show that $\mathcal{A}$ is not a $\sigma$-algebra.
I may be just confusing myself but I am trapped. I apologize in advance if the question is stupidly obvious. Thank you!
Answer: It seems to me they have worded the problem badly. They only define things like $\mu((a,b)\cap\Bbb Q)$ and although $\mathcal{A}$ contains things like $((a,b)\cup(c,d))\cap\Bbb Q$ they did not define $\mu$ on such sets. So, really they are asking: prove that $\mu$ can be extended to a premeasure on $\mathcal{A}$, ie that evaluations such as $\mu(((a,b)\cup(c,d))\cap\Bbb Q):=d-c+b-a$ are well defined. A priori it might not be since the same union can be written as a finite union of different subintervals.
They want to know if $\mu$ has a finitely additive extension to a map over all of $\mathcal{A}$. You have correctly shown that there is no countably additive extension of $\mu$. So! $\mu$ and $\mathcal{A}$ must somehow violate the Carathéodory extension theorem's hypotheses.
The algebra is not a $\sigma$-algebra since it does not contain things like: $$\bigcup_{n\in\Bbb N}[0,a_n)\cap\Bbb Q=[0,e^{-1})\cap\Bbb Q$$
Where $a_\bullet$ is an increasing sequence of positive rationals tending to $e^{-1}$ from below. That the union is not in $\mathcal{A}$ (why?) is a problem since each unionand is drawn from $\mathcal{A}$.
^^That example rested on my initial interpretation that $a,b$ must be rational. If not, we can still have examples like: $$\bigcup_{n\ge1}(2^{-(n+1)},2^{-n})\cap\Bbb Q\notin\mathcal{A}$$
Comment: Great answer. I am sorry for the late reply. For the counterexample of $\mathcal{A}$ not being a $\sigma$-algebra, recall that the sequence $(1+\frac{1}{n})^{1+n}\searrow e$ as $n\rightarrow\infty$, and thus $\bigcup_{n=1}^{\infty}(0,(1+\frac{1}{n})^{-1-n})\cap\mathbb{Q}=(0,e^{-1})\cap\mathbb{Q}$.

Therefore, I guess the problem in the book misses the requirement that for all those intervals $(a,b)\cap\mathbb{Q}$ and so on, $a,b\in\mathbb{Q}$ must be required. Otherwise there is just too much freedom.
Comment: For example, the counterexample you suggested, if without the requirement that $a,b\in\mathbb{Q}$, then $(0,e^{-1})\cap\mathbb{Q}$ still belongs to $\mathcal{A}$.

Therefore, as you mentioned, the problem itself is quite poorly written.
Comment: I don't quite understand your answer about the measure though. Caratheodory says that if $\mu_{0}:\mathcal{A}\longrightarrow [0,\infty]$ is a countably additive set function on an algebra $\mathcal{A}$, then there exsists a measure $\mu$ on $\sigma(\mathcal{A})$ such that $\mu=\mu_{0}$ on $\mathcal{A}$. Yes, the $\mu_{0}$ in our example is not countably additive.. so what happens next?
Comment: @JacobsonRadical I just mentioned the Carathéodory extension theorem as a thing worth noting: here is an example of a finitely additive premeasure that can't be extended. But it's not relevant to the problem per se. I had just assumed they wanted $a,b\in\Bbb Q$, but if not then there are plenty of other examples. For instance: $$\bigcup_{n\in\Bbb N}(2^{-(n+1)},2^{-n})\cap\Bbb Q$$Is not in $\mathcal{A}$ despite every unionand being in $\mathcal{A}$.
Comment: Ok. I get it, and thanks for another counterexamples. For some reason this problem just made my brain stuck. Do you know how to show that it can be extended to a finitely additive measure? Any hint will be enough.
Comment: @JacobsonRadical You're welcome. You need to check out whether $\mu(A):=\sum_i(b_i-a_i)$ is well defined, given $A=\bigcup_i(a_i,b_i)\cap\Bbb Q$ etc. and to show finite additivity remember it just suffices to show binary additivity, i.e. $\mu(A+B)=\mu(A)+\mu(B)$.


Bass alternative tuning picture

Question: I am a bass player and I use alternative tuning - DGCF to be more specific. I was searching for some nice picture of a fretboard with this tuning but found none (I could, of course, make myself one, but if there already is something like that, it would be great).
I am looking for something like this but in DGCF. Thanks for any links - if there is nothing like that, I will, perhaps, post my version of that picture when I make it :-)
Comment: Having taught bass(and gtr) for many years, I've never found a use for this sort of diagram. It's useful, maybe, to have a reference to the 'white notes' i.e. the naturals; then the sharps and flats will be in between.But I can't see how it helps, once you know the open notes, everything else should be referenced to them on the instrument itself.
Comment: Agreed with Tim here - this tuning is simple: all strings are down a M2 from original tuning, therefore, if you're familiar with the layout of a bass in standard tuning, the transposition is simple.
Answer: I haven't made the picture, but looking at your picture, you have DGCF at the 10th fret, and E♭A♭D♭G♭ at the 11th fret, and all the notes repeat at the 12th fret.  So just take the letters from the 10th and 11th frets, copy them down at the bottom, move the fret markers around to match your instrument, and you've got your DGCF fretboard.

Bass alternative tuning picture


What are the correct dovecot file permissions for /etc/dovecot on Ubuntu 18.04?

Question: I can't log in to my newly self-hosted webmail and I'm getting the following entries in /var/log/mail.log
Feb 10 02:00:31 ip-172-26-15-53 dovecot: lmtp(email@example.com): Error: open(
/etc/dovecot/sieve/before.d/no-spam.svbin.ip-172-26-15-53.31363.94959b0f9050164f) failed: R
ead-only file system
Feb 10 02:00:31 ip-172-26-15-53 dovecot: lmtp(email@example.com): Error: wEGGI
j+5QF6DegAAAPOhIQ: sieve: binary save: failed to create temporary file: open(/etc/dovecot/s
ieve/before.d/no-spam.svbin.) failed: Read-only file system

It looks to be a permissions problem. I've tried the suggestions here
My install is at /etc/dovecot and permissions are
-rwxrwx---   1 vmail dovecot

Dovecot was installed on Ubuntu 18.04 (Amazon lightsail) using the following playbook:
https://github.com/codecowboy/sovereign/blob/master/roles/mailserver/tasks/dovecot.yml
Dovecot version is 184.108.40.206 (d6601f4ec)
Mailbox is stored on an encfs volume
Comment: Have you checked if your filesystem is actually mounted read only?
Comment: /dev/xvda1 on / type ext4 (rw,relatime,discard,data=ordered) but that box also has encfs on it and I'm out of my depth there.
Answer: The systemd unit, which controls dovecot has ProtectSystem set to full or strict, which prevents the processes from writing to /etc.
Add to the unit file /etc/systemd/system/multi-user.target.wants/dovecot.service an exception for this directory:
ReadWritePaths=/etc/dovecot/sieve/before.d/
Reload the units with:
systemctl daemon-reload
And restart dovecot with:
systemctl restart dovecot
Comment: Thank you. There's a typo in daemon-reload fyi. Is there anything additional required to make that path an exception or literally add that line?
Comment: Fixed the missing dash, thank you!
Please test with just adding this line, that's all the docs mention.
Comment: Hm. Now I get `Failed to set up mount namespacing: /run/systemd/unit-root/etc/dovecot/sieve/before.d:` — that's not exactly what I was expecting when typing `ReadWritePaths=/etc/dovecot/sieve/before.d/`. Also, another error is `Failed at step NAMESPACE spawning /usr/bin/doveadm: No such file or directory`
Comment: Good find. This is a signal to migrate `/etc/dovecot/sieve.d` elsewhere, I chose `/home/mail/.sieve.d`.
Answer: In some kernels, instead of ReadWritePaths, use ReadWriteDirectories.
Comment: This does not provide an answer to the question. Once you have sufficient [reputation](https://serverfault.com/help/whats-reputation) you will be able to [comment on any post](https://serverfault.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/508610)


What do the __'s mean in this context?

Question: throw new Exception(__('exception'));

What do the __'s do? What are they called? I've seen this in several implementations and is common throughout the Magento codebase.
Thanks
Comment: As for what this is called, I like *"mmm mmm"*. *"So you just write 'echo mmm mmm open parens foobar close parens' to translate 'foobar'..."* ;)
Answer: __ is a common name for a localization function. __ is a valid function name like any other.
function __($text) {
    // return localized text
}

How exactly it works depends on the framework in question.
Comment: Look at `Mage_Core_Helper_Abstract#__()` to see how the functionality is implemented in Magento.
Answer: Usually if you see __() or _() its to pull the value of the string passed into the function from an i18n translation catalog. So the string passed to the function is looked up in the catalog and the appropriate translation is returned.

What do the __'s mean in this context?


Java socket connection

Question: I've tried to test socket connection in Java, but failed. Here is my code (two simple applications, server and client):
public class TestServer {

public static void main(String args[]) throws IOException {
    ServerSocket serverSocket = new ServerSocket(1111);
    System.out.println("Server socket created");
    Socket socket = serverSocket.accept();
    System.out.println("Socket accepted");
    PrintWriter output =  new PrintWriter(socket.getOutputStream(), true);
    System.out.println("Output created");
    output.write("Output string");
    socket.close();
    serverSocket.close();
}
}

public class TestClient {

public static void main(String args[]) throws IOException {

    Socket socket = new Socket("127.0.0.1", 1111);
    BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    System.out.println("Input: " + input.readLine());
    socket.close();

}

}

Output is (after running a server and, after it, client):
Server socket created
Socket accepted
Output created
Input: null

I've no idea what's the problem and why client didn't receive string sent to it. I'd appreciate any help.
Answer: Usually when I use classes like PrintWriter or OutputStream I need to flush its contents to send the data through a socket or write it to a file.
Comment: That's exactly what my problem was. Thanks.
Answer: input.readLine expects a newline in the input string. Try to replace the output.write with output.println.
I just tested it now and it should work correctly like this.
Comment: @Peter Lawrey: yeah, sorry, I was in a rush. Should work now.
Answer: In addition to all the other comments, you shouldn't close the socket itself, you should close the outermost output stream you have created around the socket's output stream. That will:
(a) flush the output stream
(b) close it
(c) close the input stream, and
(d) close the socket.
Answer: //Put the line

output.flush();

//in the end of the TestServer.java

//and in the TestClient.java I have used the code as given below

Socket socket = new Socket("127.0.0.1", 1111);
    BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    String str;
    while((str=input.readLine())!=null){
        System.out.println("Input: " + str);
    }
    socket.close();

}

Java socket connection


Continuity of function $p$

Question: Let $p$ :$\mathbb {R}^2$ to $\mathbb {R}$ with $p(x,y)$ = $x$ + $y$. Prove that $p$ is continuous.
My attempt was that since $p$ is the sum of two continuous projections then it is continuous it self, but I cant help but feel that something is missing from the proof. Can anyone confirm or maybe chime in with some insight?
Any help is much appreciated as always
Comment: What definition of continuity are you using? What theorems to prove continuity do you have?
Comment: The definition that given an open set from the domain the pre-image of that set is also an open set
Comment: If you're using the topological definition then you'll need to consider the preimage of open intervals of the form $(a,b)$. Start by graphing the boundary, which is $x+y=a$ and $x+y=b$ in $\mathbb{R}^2$ and then figuring out where the values that satisfy $x+y \in (a,b)$ are in relation to their boundary. From there, prove the set you've isolated is open. Finally consider what happens when you have two disjoint intervals such as $(a,b)$ and $(c,d)$ with $b<c$.
Comment: Your proof isn't sufficient since you need that addition is continuous to sum the two continuous projections, which is basically what the question is asking in the first place.
Comment: Yes, how do I prove that addition is continuous?
Comment: @GeorgeRevingston addition is like, really continuous in this situation. I have given you a proof of that fact below, since Lipshcitz continuity implies continuity.
Comment: I am not allowed to use this condition, I should do it only with open sets. Thank you for taking the time however
Answer: Since you know that $p$ is the sum of two projection maps which are continuous, it is enough to show that $+: \mathbb{R}^2 \rightarrow \mathbb{R}$ is continuous. For that we will show that pre-image of any basic open set in $\mathbb{R}$ is open in $\mathbb{R}^2$.
Let $(a,b)$ be any basic open set in $\mathbb{R}$. Then
\begin{align}
(+)^{-1}((a,b))&=\{(x,y): x+y \in (a,b)\} \\
&=\{(x,y): a<x+y<b\} \\
&=\{(x,y): x+y>a\} \cap \{(x,y):x+y <b\}
\end{align}
Take any point $(\alpha, \beta) \in \{(x,y): x+y>a\}$. Then $\alpha + \beta >a$, there exists $\epsilon >0$  such that $\alpha + \beta -\epsilon >a$. We have $(\alpha, \beta) \in \left(\alpha-\frac{\epsilon}{2}, \alpha+\frac{\epsilon}{2} \right) \times \left(\beta-\frac{\epsilon}{2}, \beta+\frac{\epsilon}{2} \right) \subseteq \{(x,y): x+y>a\}$. Since $\left(\alpha-\frac{\epsilon}{2}, \alpha+\frac{\epsilon}{2} \right) \times \left(\beta-\frac{\epsilon}{2}, \beta+\frac{\epsilon}{2} \right)$ is a basic open set in $\mathbb{R}^2$, we can say that $\{(x,y): x+y>a\}$ is a neighborhood of each of its points and so it's open in $\mathbb{R}^2$.
Similarly, $(\alpha, \beta) \in \{(x,y): x+y<b\}$. Then $\alpha + \beta <b$, there exists $\epsilon >0$  such that $\alpha + \beta + \epsilon <b$. We have $(\alpha, \beta) \in \left(\alpha-\frac{\epsilon}{2}, \alpha+\frac{\epsilon}{2} \right) \times \left(\beta-\frac{\epsilon}{2}, \beta+\frac{\epsilon}{2} \right) \subseteq \{(x,y): x+y<b\}$. Since $\left(\alpha-\frac{\epsilon}{2}, \alpha+\frac{\epsilon}{2} \right) \times \left(\beta-\frac{\epsilon}{2}, \beta+\frac{\epsilon}{2} \right)$ is a basic open set in $\mathbb{R}^2$, we can say that $\{(x,y): x+y<b\}$ is a neighborhood of each of its points and so it's open in $\mathbb{R}^2$.
Intersection of finitely many open sets is open. Thus  '$+$'  is continuous.
Comment: Thank you, I actually figured out by myself following this method. Appreciate the help
Comment: You're welcome.
Answer: Set $K=2$. Let $(x_1,y_1),(x_2,y_2)\in\mathbb{R}^2$.
Notice that $|x_1-x_2|,|y_1-y_2|\leq\sqrt{|x_1-x_2|^2+|y_1-y_2|^2}$ which implies  $$|x_1-x_2|+|y_1-y_2|\leq K\sqrt{|x_1-x_2|^2+|y_1-y_2|^2} $$
Then we have
$$\begin{align*}
|p(x_1,y_1)-p(x_2,y_2)|
&=|x_1+y_1-(x_2+y_2)|\\
&=|(x_1-x_2)+(y_1-y_2)|\\
&\leq|x_1-x_2|+|y_1-y_2|\\
&\leq K\sqrt{|x_1-x_2|^2+|y_1-y_2|^2}
\end{align*}$$
thus $p$ is Lipschitz.
Answer: Running with @CyclotomicField's hint, it's easy to see that for a basic open set $(a,b)\subset\mathbb R $, we get that $p^{-1}((a,b)) $ is an open rectangular strip between the two lines $x+y=a $ and $x+y=b $ in $\mathbb R^2$.  Hence open.   Hence  $p $ is continuous.

Continuity of function $p$


return specific item from a laravel collection and remove it

Question: im trying to map over a collection of query results. Specifically i want to check if an item of that collection suits my closure, returns that item and removes it from that collection. Further more i need to pass parameters with the closure function.
$data = Model::where('something', 'stuff')->get();
$res = array();
foreach ($x as $y) {
  $res = $data->each("closure function described above with $y");
  // do stuff with $res
}

i hope this was clear
thank You for Your help
cheers.
Comment: Looks like this one: https://laravel.com/docs/5.3/collections#method-pull
Answer: You may try something like following, assumed you have some data in $variables and $y=general as a type, so check if the type is general then do something with the model that has type general and then put that model in the $res array and then remove it from the collection:
$variables = '...';
$res = array();
$y = 'general'; // for example, $y contains general
$data = Model::where('something', 'stuff')->get();
foreach ($variables as $variable) {
    foreach ($data as $key => $model) {
        if($model->type == $y) {
            // Do something with $model
            $res[] = $model;
            unset($data[$key]);
        }
    }
}

Also you may check this article, it maybe helpful to interact with a Collection object.
Comment: @TheAlpha I was just agreeing with your answer.
Comment: I had my code, just like you proposed here, but i wanted to make it simpler/shorter and somehow use a method from the collection object itself. Btw, instead of unset() you can use forget($key) on the collection object. I will read the article you posted, thank you!
Comment: I used `unset` because it's faster than `forget` and this is better to use native `PHP` wrapper function.
Comment: This answer, `unset($data[$key]);`, is how I usually do it.
Comment: Sorry @Salkz, didn't get what you asked for.
Answer: To use a variable inside your closure you have to use the keyword 'use' ;).
function($item) use ($y) {
    // remove item from collection by key
    //using collection.forget($key)
}

Comment: Yes, but if I keep the closure function in a different class, how will i pass the '$y' on to it?
Comment: You didn't mention that in your question. I don't know if that is even possible. You could try to define the closure in your class with use and test whether it takes the current available y in the context or not
Comment: sorry for not being specific, would it be easier to define my function in the Collection class and use it directly on the $data object? or is that bad practice? thank you
Comment: I always write my closures where I need them (in your case inside the each) . That's why it's a closure. In another case it's just a normal passed function.
Answer: With laravel 5 you can use where method on collection.
$data = Model::where('something', 'stuff')->get();

$removedItem = $data->where('key', 'value');

Or if you use Larave 4 you can call filter method on collection
$removedItem = $data->filter(function($item) use ($removedValue) {
    return $item->key == $removedValue;
});

$data = $data->diff($removedItem);


Como fazer quebra de linha no print de uma lista em python?

Question: Minha dúvida é como fazer a quebra de linha em um print de uma lista no Python.
preciso fazer um print do código funcionando. Só que os nomes da lista estão ficando muito extensos.
A cada 10, o ganhador ganha 1 cupom, então se ele doa 100 o nome dele entra 10 vezes na lista, fazendo uma linha enorme tem como fazer uma quebra de linha no print?
listasorteio = []

while True:
    print('digite sair para encerrar')
    nome = input('digite o nome: ')
    if nome == 'sair':
        break
    valordoado = int(input('Digite o valor doado: '))
    valord = valordoado // 2
    listasorteio.extend([nome]*valord)
print(listasorteio)

Answer: Você pode utilizar Compreensão de listas para chamar a função print em cada elemento da lista:
[print(nome) for nome in listasorteio]

Como a função print já contém a quebra de linha, deve retornar o resultado desejado.
Para maiores informações sobre compreensão de lista, aqui no site existe bastante material sobre o assunto.
Comment: @OldSchoolGamer Se vc só queria imprimir os nomes, um em cada linha, é muito mais simples fazer `for nome in listasorteio: print(nome)`. Usar *list comprehension* nesse caso é um exagero, sem contar que com isso é criada uma outra lista desnecessariamente - os colchetes em volta da expressão criam uma lista. Se quer apenas imprimir e nada mais, pra que criar essa lista, sendo que dá pra imprimir tudo sem criá-la? Ver também: https://pt.stackoverflow.com/a/459289/112052
Comment: isso aí amigo, deu certo. Obrigado.
Comment: @OldSchoolGamer De nada. Se a resposta lhe serviu, não esqueça de aceitá-la para ajudar futuras buscas no site.
Comment: certo, entendi e funcionou bem também, Obrigado mesmo.


Accessing external database using the wpdb class in WordPress

Question: I have a custom page template in WordPress that is relying on an external database, and which is using the wpdb class for this purpose.
This is my code:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

        <?php the_title( '
', '
' ); ?>

<?php

class StudentsDatabase
{
    private $db;
    public function __construct() {
        try {
            $this->db = new wpdb(DB_USER, DB_PASSWORD, 'students_db', DB_HOST);
            $this->db->show_errors();
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
    public function getStudentById($student_id)
    {
        return $this->db->get_results("SELECT * FROM `students` WHERE id=$student_id");
    }
    public function getSchoolByAreaCode($area_code)
    {
        return $this->db->get_results("SELECT * FROM `schools` WHERE area_code=$area_code;--");
    }

}
$Students_DB = new StudentsDatabase();
$student_one = $Students_DB->getStudentById(1);
$school_one = $Students_DB->getSchoolByAreaCode(1);

?>

    <?php

    //do something with $student_one and $school_one ...

    the_content();

    ?>

Well, I was wondering if this is the right way to do it. Security-wise or any 'other'-wise actually.
It feels kinda sketchy to make external db calls from within the page's template itself. Should I register these functions on some external file and then just use them inside the template?
Comment: can i input this as a value for area_code or student_id? "`null or 1=1; drop table users; --`" Make sure that inputs are 'sanitised' before [bobby tables](https://xkcd.com/327/) gets his hands on your page.
Comment: @thenaglecode they're all sanitized well, I just posted part of the code (: but thanks for the concern though!
Answer: I think the most "clean" way is to implement a plugin that would be an API for your theme. Of course, it depends if it's a theme only for your own purposes beause Wordpress (so far) lacks from an dependency manager.
To sum up - in the theme use then this API.
Comment: Your plugin provides - for example - a method called _my_vendor_get_poll_data_ returning an array. You use this function within your theme and *only wrap* data there. Leave any processing to your plugin.
Comment: Didn't really get your sum up part. Anyway, this method is gonna be used around hundreds of different websites - Each of them has a different theme and specific own customizations. So a plugin isn't quite a solution in this case.. it'd require a lot of time and work to cover all these cases.
Comment: This isn't the most elegant answer, but it's 100 correct. If the question is how do I access an external DB, the answer is via an API. The OP's code sample is  gibberish, so this is a great answer!
Comment: @GonrasKarols - _has a different theme and specific own customizations_ - this is why it *should* be a plugin; plugin shares a common logic in abstract way - when you fix a bug, you just upgrade a plugin, not replace an each instance of code.
Comment: @eRIZ What do you mean by "a plugin that would be an API for your theme" - Does my custom-page.php template need to make API calls to the plugin or the opposite way? The plugin will deliver database information into the right sections in custom-page.php? I feel like I'm missing something..
Comment: Yep, that's what I thought when I wrote "Should I register these functions on some external file and then just use them inside the template?" in the question. **Thanks for clearing it out @eRIZ!**.
Answer: Put the class declarations, etc. in the functions.php file of the theme. Or, even better, require_once them there, and put them in an assets or includes folder of the theme.
-/theme/
   -/includes/classes/class-studentsDatabase.php
   -functions.php

In functions.php
define('TEMPLATE_PATH', get_template_directory());
require_once(TEMPLATE_PATH . '/includes/classes/class-studentsDatabase.php');

You can instantiate the class(es) for the theme as a whole, or as needed on the template page(s) as you're doing now.

As far as security goes, I would avoid putting DB connections that need to be secure within a theme that is going to be sent out into the wild.
I'm not sure I follow what you're doing to that end, but as presented, I would handle that bit outside of the theme environment.
Again, not knowing your use case, the theme could leverage an external api, and that api could be a wordpress wp-json api managing that DB connection at a central site.
That would allow the theme to GET / POST to an endpoint(s) that handle(s) authentication and any CRUD, and mitigate a lot of potential security issues. The theme on the external site would then just be parsing the returned json, and wouldn't have any DB access beyond that.
Comment: This is a common misconception amoungst beginner WP devs. If you are adding functionality, it should go in a plugin, not a theme. If you are modifying the "look" of a site, it should go in your theme's functions.php . This type of thing absolutely should not be put in a theme.
Comment: @JimMaguire If you build functionality that related to a specific theme, why not put it inside the theme functions.php? A theme in wordpress is a lot more then just a "theme" that defines the look of the site. (At least this is what it turned to be in the last 2-3 years)
Comment: It's hard to think of an example where what you're suggesting makes sense. Yes.. of course theme sellers put functionality in a theme, they want to lock you in and sell you something. I'd ask you why put it in a theme? It limits your options and gains nothing.
Comment: Re-reading it, I have to side with Jim on this. I was commenting on separating class declarations and their methods away from the template files, and wasn't direct enough in my comments on OP's database connection bit. The approach is an error, and should be redesigned. My "the theme could leverage an external api" should have "via a plugin" appended to it. Whatever functionality the theme may require should be relegated to the delivering of the template files. If OP is trying to create & manage a Server->Client relationship via a theme, he is asking for nothing but headaches.
Comment: I wasn't talking about managing server-client relationships via a theme - I just need a few simple get_results() statements that would link to an external db and the results would affect the page's content. Nothing too complicated and only a one-way communication is required (from db into the template).
So what you are saying is that I should build a plugin which handles the db connection and the queries, and sends out data to the template itself?
Comment: In short, yes. I see this as a GET from place A to be shown in place B, in which case I'd have a plugin do that bit to make the data available for the theme. Seriously consider the WP-json api if you haven't though. Place A with some custom routes/endpoints could remove the DB connection altogether, and make the data you need in the theme available in a clean way.
Answer: There is also more radical way to work with multiple DBs in Wordpress. As follows from wpdb Codex page:

The $wpdb object can talk to any number of tables, but only to one database at a time; by default the WordPress database. In the rare case you need to connect to another database, you will need to instantiate your own object from the wpdb class with your own database connection information. For extremely complicated setups with many databases, consider using hyperdb instead.

Hyperdb is a plugin that helps with multiple DBs and replaces your overthinking about code organization.


Dropout in Linear Regression

Question: I've been reading the original paper on dropout, (https://www.cs.toronto.edu/~hinton/absps/JMLRdropout.pdf) and in the linear regression section, it is stated that:
$\mathbb{E}_{R\sim Bernoulli(p)}\left[\| y\ - (R*X)w\|^2\right]$
reduces to:
$\|y - pXw\|^2 + p(1-p) \|\Gamma w\|^2$
I am having trouble understanding how they arrived at this result. Can anyone help?
Comment: I wrote a longform essay on this topic: http://madrury.github.io/jekyll/update/statistics/2017/08/12/noisy-regression.html
Comment: What's $\Gamma$ here?
Answer: $\newcommand{E}{\text{E}}$First let $R * X = M$ for convenience. Expanding the loss we have
$$
\|y - Mw\|^2 = y^Ty - 2w^TM^Ty + w^TM^TMw.
$$
Taking the expectation w.r.t. $R$ we have
$$
\E_R\left(\|y - Mw\|^2\right) = y^Ty - 2w^T(\E M)^Ty + w^T\E(M^TM)w.
$$
The expected value of a matrix is the matrix of cell-wise expected values, so
$$
(\E_R M)_{ij} = \E_R((R * X)_{ij}) = X_{ij}\E_R(R_{ij}) = p X_{ij}
$$
so
$$
2w^T(\E M)^Ty = 2pw^TX^Ty.
$$
For the last term,
$$
(M^TM)_{ij} = \sum_{k=1}^N M_{ki}M_{kj} = \sum_{k=1}^N R_{ki}R_{kj}X_{ki}X_{kj}
$$
therefore
$$
(\E_R M^TM)_{ij} = \sum_{k=1}^N \E_R(R_{ki}R_{kj})X_{ki}X_{kj}.
$$
If $i \neq j$ then they are independent so the off-diagonal elements result in $p^2 (X^TX)_{ij}$. For the diagonal elements we have
$$
\sum_{k=1}^N \E_R(R_{ki}^2)X_{ki}^2 = p(X^TX)_{ii}.
$$
Finishing this off, we can note that
$$
\|y - pXw\|^2 = y^Ty - 2pw^TX^Ty + p^2w^TX^TXw
$$
and we've found
$$
\E_R\|y - Mw\|^2 = y^Ty - 2pw^TX^Ty + w^T\E_R(M^TM)w \\
= \|y - pXw\|^2 - p^2w^TX^TXw + w^T\E_R(M^TM)w \\
= \|y - pXw\|^2  + w^T\left(\E_R(M^TM) - p^2 X^TX\right)w.
$$
In $\E_R(M^TM) - p^2 X^TX$, I showed that every off-diagonal element is zero so the result is
$$
\E_R(M^TM) - p^2 X^TX = p(1-p)\text{diag}(X^TX).
$$
The paper defines $\Gamma = \text{diag}(X^TX)^{1/2}$ so $\|\Gamma w\|^2 = w^T\text{diag}(X^TX)w$ which means we are done.

Dropout in Linear Regression


Permutation Matrix Measure

Question: I'm trying to make a neutal network with dynamic architecture, meaning that the weight matrices will be added/removed dynamically when they are simple permutation matrices and thus don't actually do any computation. However, I've run into an issue, I need a differentiable way to determine "how close" the weight matrices are to being a permutation matrix. This measure will then be added to the loss function as a regularization term.
I know how to find the closest permutation matrix by making a cost matrix as one of the answers from here:
Nearest signed permutation matrix to a given matrix $A$
However, if I use the norm of the cost matrix as a measure of "permutivity" the simple 0 matrix will always give a lower measure (loss) than an actuall permutation matrix. Whenever I try to find or come up with a measure I always run into this same problem, there's always some other matrix which gives a lower score than a permutation matrix.
Do any of you guys know of a "permutation matrix measure" pr do you have any brilliant ideas on how to make one? Keep in mind that it has to be differentiable to be usefull,so no max functions or anything like that.
Update: I have found one measure that does what I need, but it's too computationally heavy to use. I'll write it here anyway, since it gives insight to what I'm looking for and might spawn new ideas:
$ \text{"permutivity"} = \prod_{i = 1}^{n} \sum_{j = 1}^{m}\sum_{k = 1}^{m} (a_{j,k}-{p_{i}}_{j,k})^2 $
Where $p_i$ is the "i:th" permutation matrix of size MxM. So basically the product of the L2 distance of all the different permutation matrices. As you can imagine, this is not practical for several reasons, first of all, the number of permutation matrices are m! and for neural networks m is usually fairly large. Secondly, the product would explode causing numerical overflows when trying to calculate it. The second issue can be remedied using some sort of "cap" function around the sums, but I can't see any simple way to fix the first problem :(
Comment: Text in equations should be written using `\text{...}` so it's not as a sequence of variables. For italicized text in equations, you can use `\mathit{...}`. As you can see, just writing the text as variables gives it the wrong spacing.
Comment: Perhaps you could replace the minimum with which you would find the nearest matrix with a [smooth minimum](https://en.wikipedia.org/wiki/Smooth_maximum)
Comment: @BenGrossmann Very interesting idea, though I don't think it would work since the hungarian algorithm used to find the closest pertubation matrix using the cost matrix relies on the subtraction of the minimum value being zero and it only finds one of all possible matrices, so using the smooth minimum would still only go towards the closest matrix, but "smoothly". But it could possibly be used in other ways :) I've come up with one meassure that actually works, but it's too computationally heavy to use, I'll post it in the question though since it's technically a solution.
Answer: How about
$$
\lambda\sum_{i,j}a_{ij}^2\left(1-a_{ij}\right)^2+\mu\left(\sum_i\left(\sum_ja_{ij}-1\right)^2+\sum_j\left(\sum_ia_{ij}-1\right)^2\right)
$$
This is $0$ only if all entries are $0$ or $1$ and all row and column sums are $1$, i.e. if $A$ is a permutation matrix.
Comment: This one looks like it would work great :D I've been trying a lot of things like the second part (getting the row and column sums = 1) but I never thought about simply adding the 0/1 condition as another sum, it's genious :D Thank you, and super easy to compute :) Now I just need to find good values for the coefficients so there are no local minimas inbetween permutation matrices and matrices with only 1/0 or row/column sums to 1 :)

Permutation Matrix Measure


hook_user_presave at user registration, how to get field_data_field (set to auto increment) to take a new and unique value

Question: I'm trying to force a parallel identifier (to match up legacy system requirements) to correlate to uid by means of a field_data_field_foo table.
The field_data_field_foo was created in the Field UI and because I couldn't see any UNIQUE and AUTO_INCREMENT options I set them both for the column field_data_field_foo.field_foo_value in phpMyAdmin.
I assumed this would be enough to force the issue, but in development I setup a hook_user_presave() function in my custom module and with a break point inside it, for $account->field_foo I just get undefined....
field_foo   Array [1]
    und Array [0]

Because this is something that must be assigned uniquely and I'd like to assign at the point of registration should I be using a different hook? Or is there something else I'm missing?
Btw, it might be something more basic in Drupal that I'm missing as I'm not that experienced with it atm - i.e. due to the nature of this project, I've yet to spend time learning nodes and the like.
Comment: well, if you set the value in the database, you cant expect to see it in presave ?
Comment: so the aim is to have another number independent of uid, do i get it right ?
Comment: Yes, I want another unique number, independent of uid, which auto increments and can be assigned to every new user when they register.
Comment: try adding a [serial field](http://drupal.org/project/serial) to the user, but only ater they [support entities other than nodes](http://drupal.org/node/1333032) :(
Answer: Here's what I did in the end...
First I added the field to the user registration form, as one thing Drupal is great at is processing information - I did notice errors being caught so I might have to check that out.
Then I used hook_user_presave() and (managing the unique id creation seperately) passed that, overriding any existing value or adding a value if none existed.
(full details are here: Editing user submission of user registration form with hook_user_insert() and $edit)
I will still have to manage away the users ability to insert an identifier by hiding it through CSS/JavaScript etc. but because hook_form_alter() wouldn't edit the user registration form after repeat attempts this seems the most expedient way of solving the problem.


How much difference does the guitar make when recording with a modeler?

Question: I am learning and experimenting with Maschine Mk2, a MIDI Keyboard, and Komplete 9 for my pursuit of making music, a very EDM/DAW approach. When I got Komplete, although it came with Guitar Rig 5 and all the add-ons, it doesn't see any use despite it being a very, very powerful guitar processing system with tons of amp emulations and Rammfire.
I played guitar for a little over a year back in high school, and used a $99 Squier Mexi-Strat, and a little 40W Marshall. For learning video game tabs and practicing chords, it was sufficient, but I always coveted the sound of the Warlock Widow. I felt the guitar sounded deeper, chunkier and more metal - many told me this was because of the pickups. Then, I didn't have the budget to experiment and didn't spend much time at a Guitar Center, so I never learned much more around the gear end of things.
This was right around the time the Line6 POD came out and amp emulation type stuff was still coming into being. Being a dumb high school kid, I turned up my nose at my friend's POD and championed the virtues of my "real amp". However, I do recall that in an attempt to convince me otherwise, he had me hook into the POD and got my piddly little Squier to sound quite a bit more "thumpingly Warlock-y", however it was still a bit lifelessly digital.
Now, things have come a long way, and stuff like Guitar Rig and Rammfire exist. I'm beginning to think I want to own a guitar again so I can enjoy the Rammfire I already own.
With so much advancement in the power of guitar DSP in the 10+ years since I last paid any attention, how much does the quality of my electric guitar actually matter now? If I just pick up another $99 Squier or something of that sort, can the DSP of Guitar Rig do the work to make it really slam, or will the mundanely plain pickups of it hamstring my efforts? How much of a difference would I actually see if I stepped up to the $250 for a Warlock?
Comment: I wouldn't say you were necessarily wrong to turn up your nose at a Line6 POD, and the quality of a guitar is probably more important than ever going into something like that because the more you digitally manipulate a signal, the more you degrade the original - thus you want the highest possible quality original to begin with.  Playability, though, is probably just as big of a gain with a nice guitar as the pickups/wood/etc.  Also, even pros who use digital modeling typically run their signal through all kinds of standard/analog gear as well.
Answer: Honestly, if you want to play high gain stuff, it's all in the pickup.
Get yourself a cheap guitar and then upgrade it with the top of the line bridge pickup of your choice (the pickup will set you back $60-150 if you can handle a soldering Iron yourself). You are looking for a high gain pickup of brands like Seymour Duncan (very popular is the SH-6), Bareknuckle or Dimarzio. Search Youtube for comparisons and get what you like. As long it's not a vintage voiced Humbucker or Single coil, you should be fine (and some people even like those).
Guitar wise I personally would not go for a Les Paul style model, because they have a shorter scale length (the length of the neck from the nut to the saddle) (24.75") than Fender types (25.5"). When you tune down, a shorter scale needs thicker strings and gets mushy faster.
For a good tight metal rhythm sound I'd also recommend thick bottom strings (e.g. on a Fender scale length 09-42 is the default for standard tuning. I'd go for something like 10-52). Go as thick as you are comfortable with.
Also, playing aggressively and with a hard pick makes a world of difference
TL;DR: Get a cheap guitar with good humbucking pickup. Go with thick strings, a hard pick and play aggressively
Answer: I used IK Multimedia amp modelling for a few years. The amp sims are top notch, but even so, I found differences in guitars that I used. I started with a Mexican Squier Telecaster with single-coil pickups. Although I was able to conjure up some decent lead tones, the overall tone was thin, no matter how I adjusted the eq. etc.
Then I tried an Ibanez Prestige 1570 with stock pickups (humbuckers) and the difference for high-gain models was HUGE. The cheap single-coils in the Mexi-Squier Tele (mid-90s production) could not produce the base tones I was hunting. I did not try a Mahogany-bodied guitar with my modelling software, but, I think that would give you a very dark, lower-mid base to build giant metal tones upon, if that is what you are after.
So, single-coils (Strat, Tele) for clean, or bluesy stuff, and humbuckers for thick, meaty metal meals, sonically speaking. Mahongany+humbuckers (Les Paul styled guitars) thicker, meatier, metal, and truly belly rumbling blues.
However, I do recommend that you have a decent quality all-tube amplifier for reference. Marshall Class 5 combo would work, for darker stuff, for bright stuff that's a bit trickier, you must pay for the pleasure of a nice little Fender, unfortunately. Find a used tube Princeton, and you are set. To my ears, there are still differnces between and all-analog sound, and digitized, no matter how "good" the D/A conversion is.
Comment: The importance of the quality of the guitar/pickups cannot be overstated. The modeler only emulates how an amp affects the source signal. It won't magically create a full, rich sound from a weak thin sounding guitar.
Comment: "I started with a Mexican Squier Telecaster with single-coil pickups. Although I was able to conjure up some decent lead tones, the overall tone was thin, no matter how I adjusted the eq. etc. Then I tried an Ibanez Prestige 1570 with stock pickups (humbuckers) and the difference for high-gain models was HUGE. The cheap single-coils in the Mexi-Squier Tele (mid-90s production) could not produce the base tones I was hunting." - Thanks, this is pretty much _exactly_ the answer I was looking for, it sounds like I'll have to go for the Warlock after all... can't say I mind an excuse. [cont-]
Comment: You specify that the difference showed itself more in the higher gain patches - what would you say the difference was like as one moved towards cleaner, lower gain models? I'm mostly expecting to work with a lot of gain and processing, but it sounds like I'd still retain the freedom to use cleaner tones well with the 'heavier' guitar, should I so desire. The Warlock has some pretty distinctively heavy humbuckers, but it does have a switch (and it's easier to take meatiness out of a tone than add meatiness that was never there)
Answer:
How much does the quality of my electric guitar actually matter now?

Define your expectations. Find out exactly what you are trying to accomplish.
Many people obsess about the quality of the build and focus on the brand name. What makes a great guitar is not always the sum of the parts, sometimes it is the feel.

If I pick up another $99 Squier or something similar, can
  the DSP of Guitar Rig make it really slam, or will the
  mundanely plain pickups of it hamstring my efforts?

With tools like Guitar Rig, the power is in the hands of the operator. With a working knowledge of how to properly utilize this tool, perhaps you could make your guitar sound like it is worth more than $99.
But again, where is the line? What are you trying to achieve? These questions you must decide yourself.

How much of a difference would I actually see if I stepped up to the
  $250 for a Warlock?

You might hear 151 dollars worth of difference. You might not - it could sound worse. If a $99 guitar feels great to you, why not just stop there and be content you got a great deal? IF you want something new, perhaps your search for a great guitar is not yet over.
Comment: Well, to boil it down a bit, let's assume I have both a very high quality audio interface to capture the signal, and extensive working knowledge of the modeling software. My goal is mostly "screw around in Rammfire for fun", but I also want to be able to work with guitar tracks in my projects. What I want to know is how sensitive modeling is to the guitar's build quality. I can really tell the difference with traditional amps, but modeling has so much more power and depth that perhaps deficiencies in the input can be negated. With modeling, maybe a learner-grade guitar can melt faces?
Comment: @user1002617 - Of course! Knowledge is power my friend. With a good tool set and a deep knowhow, I would say it is definitely possible to melt some faces with a learner-grade guitar.


Como inserir vários endereços de e-mails dentro de uma variável apenas, no SQL?

Question: Por exemplo:
Se ao invés de eu digitar apenas um endereço de e-mail na variável abaixo, eu digitasse 30 endereços de e-mails, o que eu teria que alterar na minha query abaixo?
DECLARE @email as NVARCHAR
SET @email = 'email@example.com'

Select * FROM Padarias WHERE email = @email

Comment: Não creio que seja uma boa ideia trabalhar com atributos multi-valorados. Repense sua estratégia.
Comment: Pelo que entendi https://stackoverflow.com/questions/21160456/sql-server-procedure-declare-a-list
Comment: Muito obrigado Motta, a solução apresentada neste link serve para o meu caso! Um forte agraço.
Answer: Caso você esteja usando a partir da versão 2016 em diante do SQL Server, basta utilizar a função nativa string_split. Veja a seguir:
DECLARE @email as NVARCHAR(max)
SET @email = 'email@example.com,email@example.com,firstname.lastname@example.com,email@example.com@padaria.com'--e-mails de exemplo

Select *
FROM Padarias
WHERE email in(select value
                from string_split(@email, ',')--> O delimitador deve ser o mesmo usado para separar os e-mails inseridos na variável
                )


Install Xdebug through Installation Wizard

Question: Can someone help me with a next step? Which software should I use and how do I go about? What is RedHat?
I have XAMPP installed. I have searched some solutions but my case is a bit different.
Below is the output from Installation Wizard (https://xdebug.org/wizard).
I do not know how to proceed from point "2.Install the pre-requisites for compiling PHP extensions." and onwards.

Summary
Xdebug installed: no
Server API: LiteSpeed V7.8 CloudLinux 1.2
Windows: no
Zend Server: no
PHP Version: 7.4.13
Zend API nr: 320190902
PHP API nr: 20190902
Debug Build: no
Thread Safe Build: no
OPcache Loaded: yes
Configuration File Path: /opt/alt/php74/etc
Configuration File: /opt/alt/php74/etc/php.ini
Extensions directory: /opt/alt/php74/usr/lib64/php/modules
Instructions
1.Download xdebug-3.0.1.tgz
2.Install the pre-requisites for compiling PHP extensions.
On your RedHat system, install them with:
yum groupinstall "Development tools" && yum install php-devel autoconf automake

3.Unpack the downloaded file with tar -xvzf xdebug-3.0.1.tgz
4.Run: cd xdebug-3.0.1
5.Run: phpize (See the FAQ if you don't have phpize).
As part of its output it should show:
Configuring for:
...
Zend Module Api No:      20190902
Zend Extension Api No:   320190902

If it does not, you are using the wrong phpize. Please follow this FAQ entry and skip the next step.
6.Run: ./configure
7.Run: make
8.Run: cp modules/xdebug.so /opt/alt/php74/usr/lib64/php/modules
9.Edit  /opt/alt/php74/etc/php.ini and add the line
zend_extension = /opt/alt/php74/usr/lib64/php/modules/xdebug.so
Make sure that
zend_extension = /opt/alt/php74/usr/lib64/php/modules/xdebug.so
is below the line for OPcache.
10.Restart the webserver
Comment: So, as a new user, take the [tour] and read [ask] to get familiar with this. That said, you ask "What is RedHat?", which shows that you need to do a bit research online. Sorry, but  if you throw that question at a random search engine, you will find an answer to it. I think it isn't too much to ask that preliminary work first.
Comment: Fell free to ask me counter questions if you like. I am really new to this, and unsure how everything works. My goal is to be able to debug php code in my Wordpress's plugin.I am stuck on point 2 and onwards.
Comment: Yes, but RedHat is something for Linux...my computer runs on Windows, so that in itself would be an issue. But to what forum should I go then? You can give me something to start with. My computer knowledge is limited.
Answer: You're reading the *nix (Linux) installation instructions.

Install link is at the top of the website.
That brings you to https://xdebug.org/docs/install.
The eighth line on the page: Windows, with help from a wizard.
Brings you to https://xdebug.org/wizard
With instructions to download files from https://xdebug.org/download.
And to copy the contents of your php.ini file in the textarea at https://xdebug.org/wizard.

After following those clear instructions you will need to restart your HTTP server or if you have no idea and don't mind a temporary mindless brute-force (for your current XP) just sacrilegiously reboot your computer.
Here is what is in my WAMP (Windows, Apache, MariaDB, PHP) server's php.ini file for XDebug:
[XDebug]
;When updating server dump phpinfo in to here to determine correct download: https://xdebug.org/wizard.php
zend_extension = "C:\MEDIA\INTERNET\WAMP\PHP\ext\php_xdebug-2.9.7-7.4-vc15-x86_64.dll"
xdebug.profiler_output_dir = "C:\MEDIA\INTERNET\WAMP\Apache\logs"
xdebug.profiler_append = On
xdebug.profiler_enable_trigger = On
xdebug.profiler_output_name = "callgrind.%R-%u.trace"
xdebug.trace_options = 1
xdebug.collect_params = 4
xdebug.collect_return = 1
xdebug.collect_vars = 0
xdebug.profiler_enable=0
xdebug.trace_output_dir = "C:\MEDIA\INTERNET\WAMP\Apache\logs"
html_errors = 0

If installed correctly you should be able to simply append ?XDEBUG_PROFILE to the end of a URL (from your local server). The files will be output to whatever you set for xdebug.profiler_output_dir (use quotes if there are spaces in your local file system's path).
To open the generated logs I recommend using QCacheGrind.
If you get stuck leave a comment. It appears that you're likely having trouble concentrating because the vast majority of the instructions are very easy to access. Additionally you should make an attempt to find search the web before asking questions that are easy to find by...searching the web. Good luck.
Comment: The above phpinfo from Xdebug Installation Wizard is from my live website's PHP configuration. So that is my host's configurations. I do not think I can install Xdebug at their server. But now I have downloaded XAMPP and have Xdebug installed on my PC, that is, local hosted. I guess I have to migrate my Wordpress site to my PC, which would be my second choise after being able to debug my live site instead. I will also download Eclipse.
Comment: You should talk with your host to see if you can use it. Frankly you should be testing local first and then push changes to the live server.


How many circles (radius = 0.5) can fit in circle (radius = 35)?

Question: How many circles A (radius = 0.5) can fit in circle B (radius = 35)? I've been struggling a lot and couldn't find a solution so I decided to post it right here.
Comment: @Zubi The densest possible packing, which is $7$ circles arranged in a hexagon, has a density of $\frac{1}{6} \sqrt{3}$. Using this, we can get an upper bound of $(\pi * 35^2) / (\pi * 0.5^2) * \frac{1}{6} \sqrt{3} \approx 5658$.
Comment: @TobyMak It's a (very) open problem and difficult, too. Not just combinatorics and geometry. Have a look here http://hydra.nat.uni-magdeburg.de/packing/cci/cci.html#overview
Comment: @TobyMak $5658\times 0.5^2\times \pi\approx 4443.78$ while $35^2\times \pi \approx 3848.45$ something is wrong :)
Comment: @Raffaele Hence, the answer is **4315 circles**, with $r=1/0.5/35=0.0142857<0.0143235$ ... impressive... http://hydra.nat.uni-magdeburg.de/packing/cci/cci4315.html
Comment: @hyprfrcb never imagined it was so complex a problem! Actually the OP didn't specify that the packaging must be *optimal*, thus even $1$ could be an answer :)
Answer: I say $4315$ because I looked on  this page and considering that $0.5/35\approx 0.0142857$ I looked in the second column and the radius of $4315$ packed circle was the closest to the value I found, namely about $0.0143235$, so there should be at least $4315$ circles non overlapping having radius $0.5$ in a circle having radius $35$. The picture below is with larger radius and has $1394$ circles.


Surface normal to point on displaced sphere

Question: I want to calculate the surface normal to a point on a deformed sphere. The surface of the sphere is displaced along its (original) normals by a function $f(\vec x)$.
In mathematical terms:
Let $\vec x$ be a unit length vector in 3D, i.e. any point on the unit sphere centered on the origin. And let $f:\vec x \to \mathbb{R}$ be a function with a well-defined and continuous gradient $\nabla f(\vec x)$ for every $\vec x$.
Now a point on the surface of the resulting sphere is defined as:
$\vec P(\vec x) = (R + s \cdot f(\vec x)) \cdot \vec x$
where $R$ and $s$ are constants for the radius and the modulation depth, respectively.
The question now: Is this information sufficient to calculate the surface normal to the point $\vec P$? I feel like this should be possible, but it is entirely possible that it is not.
More information:
The function in my case is 3D Simplex Noise which gives values on the range $(-1,1)$ for any $\vec x$. The values for $R$ are about 10 times larger than those of $s$, probably more. Maybe this helps if only an approximation is possible.
UPDATE:
Yes, it is possible, though there is still some error in here:

Calculate the gradient: $\vec g = \nabla f(\vec x)$
Then project it into the tangent plane to the sphere passing through the point:
$\vec h = \vec g - (\vec g \cdot \vec x)\vec x$
The normal can then be calculated as
$\vec n = \vec x - s \cdot \vec h$

This works as long as the radius is $R=1$ and something like $s \ll R$.
I think for $R \ne 1$ the gradient has to be rescaled: $\vec g = \dfrac{1}{R} \nabla f(\vec x)$
But I have no idea why the error gets larger with increasing $s$.
UPDATE 2:
Alright, the rescaling factor was only half complete $\vec g = \dfrac{\nabla f(\vec x)}{R + s \cdot f(\vec x)}$
Now it works for all $R$ and $s$.

Here is an image to give some intuition what this thing looks like (with approximated normals).
Comment: What do you mean by "normal to a point". Normal means orthogonal, which requires more than a point. Do you mean the vector connecting the origin to the point?
Comment: I mean normal to the surface in point P.
Comment: Well, that's a completely different thing ;)
Answer: Let's say that your surface is defined by $\vec{r}(\Theta) = [r_0 + s f(\Theta) ] \hat{r}$, where $\Theta$ represents the spherical angles. I'm going to conjecture that the normal vector in radial coordinates is proportional to $\vec{n} = (-s\nabla f,1)$, where the gradient is a 2-vector in the angle space, and the last component is the radial direction.
We can verify this by making sure the normal vector is orthogonal to the surface. Note that a tangent vector to the surface is $(1,s\nabla f)$. This tangent points along the gradient direction. A normal vector orthogonal to this one points along an isocontour, which by construction is $(\hat{\Theta}^\perp,0)$, where $\hat{\Theta}^\perp$ is 90 degree rotation of the unit angular vectors in the tangent plane to the sphere passing through the point. The dot product with both of these is zero; the first is obvious, while for the second, $\nabla f \cdot \hat{\Theta}^\perp = 0$.
TL;DR: In radial coordinates: $(-s\nabla f,1)$, then just normalize and convert to cartesian if needed.
Elaboration: Based on your comment, it seems that your $f(\vec{x})$ is a scalar function of a 3-vector. Then $\nabla f$ is a 3-vector. The notation I use lumps the spherical coordinates $(\phi,\theta)$ into a single abstract 2-vector-like quantity $\Theta$, so that when you say $f(\Theta)$, that means $f$ is a function of only the spherical coordinate angles (and not a function of radius). Then $\nabla f(\Theta)=\nabla_\Theta f(\Theta)$ is a gradient in this 2-dimensional angle space. Your $\nabla f$ needs to be projected onto the surface of a sphere first, since you only ever sample $f(\vec{x})$ for $\lVert \vec{x} \rVert = 1$.
So, to calculate your normals, given a point $\vec{P}(\vec{x}) = [R + s f(\vec{x})]\vec{x}$ with $\vec{x}$ such that $\lVert \vec{x} \rVert = 1$,

Let $\vec{g}(\vec{x}) = \nabla f(\vec{x})$. Here, $\vec{g}$ is the true gradient of $f$ in 3-space, and you can calculate that in cartesian coordinates, so you get $\vec{g} = (g_x,g_y,g_z)$.
Project out the radial component of $\vec{g}$ to get $\vec{h}(\vec{x})$. To do this, $\vec{h} = \vec{g} - \frac{\vec{g}\cdot\vec{x}}{\vec{x}\cdot\vec{x}}\vec{x}$. Note that the denominator should be 1. Here, $\vec{h}$ represents the component of $\vec{g}$ that should be tangential to a sphere centered at the origin and passing through $\vec{P}$.
An outward normal vector to the surface is $\vec{n} = \vec{x} - s\cdot \vec{h}$. Normalize it to get a unit normal vector.

All these computations can be done in Cartesian coordinates, but notice that I never had to resort to referring to the Cartesian components of any vector; the basic operations are vector arithmetic and dot products.
Comment: Thanks for the answer, but can you elaborate more, especially on the coordinates you use? I'm not quite able to follow your solution.

Also $f$ and $\nabla f$ are given in cartesian coordinates and if possible I would like to skip calculating a bunch of angles because trigonometric functions are expensive on the hardware I use for this (GPUs).
Comment: Alright, now I get it, but the radial component of $\vec g$ should be $(\vec g \cdot \vec x)\vec x$ not $(\vec g \cdot \vec x)\vec g$ right?
Comment: @Gigo: Ah yes, you are absolutely right. Fixed.
Comment: Strange, there seems to be an error with the sign. If I use $\vec n = \vec x - s \cdot \vec h$ the resulting normal seems correct.
Comment: Well, I guess that makes sense, now that I think about it. The gradient points in the direction of increasing height, so the normal should tilt towards the opposite direction.
Comment: Ack! You are right again! I should not be in such a rush when posting answers.
Comment: Your help is very much appreciated, even with sign errors! ;)
Comment: If you're interested: There was a scaling problem left, have a look at the updated question.
Answer: Your modulated sphere is the 0-set of the function
$$
F(x,y,z)=x^2+y^2+z^2-r^2(x,y,z) \\
F(\rho,\theta,\varphi)=\rho-r(\theta,\varphi)
$$
($r=R+sf$ in your notation). We shall need its differential,
$$
dF=2\big((x-rr_x)dx+(y-rr_y)dy+(z-rr_z)dz\big) \\
dF=d\rho-r_\theta d\theta-r_\varphi d\varphi
$$
By the definition of the gradient, $\nabla F=(dF)^{\sharp}:\quad \forall v,\;dF(v)=\langle \nabla F, v\rangle=g(\nabla F,v)$. Using the usual Eucledian metric,
$$
g=dx^2+dy^2+dz^2 \\
g=d\rho^2+\rho^2d\theta^2+\rho^2\sin^2\theta \;d\varphi^2
$$
we get
$$
\nabla F \propto (x-rr_x)\partial_x+(y-rr_y)\partial_y+(z-rr_z)\partial_z \\
\nabla F=\partial_\rho-\tfrac{r_\theta}{\rho^2} \partial_\theta- \tfrac{r_\varphi}{\rho^2\sin^2\theta}\partial_\varphi = \vec e_\rho-\tfrac{r_\theta}{\rho}\vec e_\theta- \tfrac{r_\varphi}{\rho\sin\theta}\vec e_\varphi,
$$
where for the spherical coordinates we've written it first in the natural tangent space basis and then in the orthonormal basis used in vector calculus. These expressions evaluated at $F=0$ give a normal vector to the modulated sphere.


How to calculate conditional expectation?

Question: How can I calculate a conditional expectation like below?
$E(X^3 - Y^3 | X-Y)$
Answer: $$E(X^3 - Y^3 | X-Y)= E(X^3 | X - Y) - E(Y^3 | X - Y)$$
You never said if the variable was discrete or continuous. I'm going to do the discrete case below.
$$=\sum(X^3 \times \textrm{Pr}(X^3|X-Y)) - \sum(Y^3 \times \textrm{Pr}(Y^3|X-Y)) $$
For the continuous case, you integrate where there's a summation, and use the PDF for the conditional probability.
Comment: Thank you.I meant the continuous but it also will be useful for discrete mod.


Overriding an entry in paths-common.conf with paths-common.local in fail2ban?

Question: OS is CentOS 6.9, with fail2ban-0.9.6-1.el6.1.noarch
According to the fail2ban manual:

Every .conf file can be overridden with a file named .local. The .conf file is read first, then .local, with later settings overriding earlier ones. Thus, a .local file doesn't have to include everything in the corresponding .conf file, only those settings that you wish to override.
Modifications should take place in the .local and not in the .conf. This avoids merging problem when upgrading. These files are well documented and detailed information should be available there.

However, this does not seem to apply to the paths-*.conf files.  I tried creating paths-common.local to override:
dovecot_log = /var/log/dovecot.log

but this was ignored.  I found I had to modify paths-common.conf to get fail2ban to look at the right log file.
Did I misunderstand, or is the documentation just a bit too eager with "Every .conf file..." (or maybe it's a bug I should file in the tracker?)
Comment: What operating system and fail2ban version are you using?
Comment: Have you read this: https://github.com/fail2ban/fail2ban/issues/820#issuecomment-57445680 to check which files are read and if the file is probably called `files-overrides.local`?
Comment: @sebix OS and version added to the post
Comment: As to the link, that seems to be it, I'm not where I can test it right now but will try it tomorrow.  Based on the lack of hits in an explicit search for `file2ban files-overrides` (except for the link you provided) it appears that `files-overrides.conf` isn't documented.
Answer: In the fail2ban current version of paths-common.conf there is a section at the top for includes:
[INCLUDES]

after  = paths-overrides.local

To override paths-common.conf create paths-overrides.local in the fail2ban directory & add a default section header to the top of it:
[DEFAULT]

dovecot_log = /path/to/mail.log


Is it possible to create a table in current database of wordpress using a static page only (not creating as plugin)?

Question: I wrote my code as bellow,
<?php
    /*
    Template Name:Register Page
    Template URI:
    Author:yuva
    Author URI:
    Description:This Is Page for Gallery.
    */

    get_header();
    ?>

<?php  the_title(); ?>

    <?php
    global $wpdb;
    $my_table=$wpdb->prefix.'your_users';

    $sql="CREATE TABLE IF NOT EXISTS $my_table (id int(20) not null auto_increment,name varchar(40) not null,password varchar(20) notnull,PRIMARY KEY(id))";

    include_once(ABSPATH.'wp-admin/includes/upgrade.php');

    dbDelta($sql);

    ?>

    <?php
    get_footer();
    ?>

Can any one help me solve this?
Thank you!
Comment: Yes,it is possible :)
Answer: Since this code has nothing to interact with the user how about writing the above code in your function.php instead of the template page??
And yes you can surely create tables if you want.
Answer: Yes it is possible, but don't forget to use CREATE TABLE IF NOT EXISTS .Otherwise your query create new table every time when page is executed and your previous data will be lost.


Ninject: Singleton binding syntax?

Question: I'm using Ninject 2.0 for the .Net 3.5 framework. I'm having difficulty with singleton binding.
I have a class UserInputReader which implements IInputReader. I only want one instance of this class to ever be created.
 public class MasterEngineModule : NinjectModule
    {
        public override void Load()
        {
            // using this line and not the other two makes it work
            //Bind<IInputReader>().ToMethod(context => new UserInputReader(Constants.DEFAULT_KEY_MAPPING));

            Bind<IInputReader>().To<UserInputReader>();
            Bind<UserInputReader>().ToSelf().InSingletonScope();
        }
    }

        static void Main(string[] args)
        {
            IKernel ninject = new StandardKernel(new MasterEngineModule());
            MasterEngine game = ninject.Get<MasterEngine>();
            game.Run();
        }

 public sealed class UserInputReader : IInputReader
    {
        public static readonly IInputReader Instance = new UserInputReader(Constants.DEFAULT_KEY_MAPPING);

        // ...

        public UserInputReader(IDictionary<ActionInputType, Keys> keyMapping)
        {
            this.keyMapping = keyMapping;
        }
}

If I make that constructor private, it breaks. What am I doing wrong here?
Comment: `Bind().To().InSingletonScope()`
Comment: Some interesting variations on singletons: http://www.yoda.arachsys.com/csharp/singleton.html
Comment: You can make the constructor internal instead of private if they are in the same assembly. Perhaps that ads a little bit of safety if you are concerned about code from other assemblies accessing that constructor.
Answer: Of course it breaks if you make the constructor private. You can't call private constructors from outside your class!
What you're doing is exactly what you're supposed to do. Test it:
var reader1 = ninject.Get<IInputReader>();
var reader2 = ninject.Get<IInputReader>();
Assert.AreSame(reader1, reader2);

You don't need the static field to get the instance singleton. If you're using an IoC container, you should get all your instances through the container.
If you do want the public static field (not sure if there is a good reason for that), you can bind the type to its value, like this:
Bind<UserInputReader>().ToConstant(UserInputReader.Instance);

EDIT: To specify the IDictionary<ActionInputType, Keys> to use in the constructor of UserInputReader, you can use the WithConstructorArgument method:
Bind<UserInputReader>().ToSelf()
     .WithConstructorArgument("keyMapping", Constants.DEFAULT_KEY_MAPPING)
     .InSingletonScope();

Comment: Got it: `.WithConstructorArgument("keyMapping", /* whatever */);`
Comment: Ok, cool. But UserInputReader's constructor requires a `IDictionary`. How can I specify this in Ninject?
Comment: @Rosarch: either you create a marker wrapper around the dictionary, call it `IActionKeyMap` or something, and use that, or you use the method approach in your comment, or you use the facilities to specify ctor parameters (can't remember exactly how :(.
Comment: Is there a one-line syntax to do `ninject.Get().DoSomething()` just like the OP wanted to do with `UserInputReader.Instance.DoSomething()`
Answer: IInputReader does not declare the Instance field nor can it, since interfaces can not have field or static field or even static properties (or static methods).
The Bind class can not know that it is to find the Instance field (unless it uses reflection).

Ninject: Singleton binding syntax?


Slowly updating side input using windowing does not work

Question: I followed the code snippet of Slowly updating side input using windowing example but it printed nothing.
It only runs side_input instead of the whole.
I use DirectRunner for this.
import apache_beam as beam
from apache_beam.transforms.periodicsequence import PeriodicImpulse
from apache_beam.transforms.window import TimestampedValue
from apache_beam.transforms import window

def cross_join(left, rights):
    for x in rights:
        yield left, x

if __name__ == '__main__':
    data = list(range(1, 100))
    pattern = 'pat'
    main_interval = 10
    side_interval = 5
    pipeline = beam.Pipeline()
    side_input = (
            pipeline
            | 'PeriodicImpulse' >> PeriodicImpulse(fire_interval=side_interval, apply_windowing=True)
            | 'MapToFileName' >> beam.Map(lambda x: pattern + str(x)))
    main_input = (
            pipeline
            | 'MpImpulse' >> beam.Create(data)
            | 'MapMpToTimestamped' >> beam.Map(lambda src: TimestampedValue(src, src))
            | 'WindowMpInto' >> beam.WindowInto(window.FixedWindows(main_interval)))
    result = (
            main_input
            | 'ApplyCrossJoin' >> beam.FlatMap(cross_join, rights=beam.pvalue.AsIter(side_input))
            | 'log' >> beam.Map(print))
    res = pipeline.run()
    res.wait_until_finish()

thanks and regards.
Comment: Where are you running this code? You are not getting any error, just nothing is printed? Aren't you missing `()` in `yield`?
Comment: @PjoterS, I use DirectRunner,  no difference for the `()`, I also put a print() inside cross_join() but nothing prints out.
Answer: This is because the Python FnApiRunner does not yet support streaming mode. It is trying to run PeriodicImpulse to completion before executing the next stage of the pipeline. This is curently being worked on at BEAM-7514.
Unfortunately, it looks like the BundleBasedDirectRunner which can do streaming in Python has issues with PeriodicImpulse as well.
You could try using a "real" runner, e.g. Flink in local mode, by passing runner='Flink' to your pipeline creation.
Note also that your side input needs to be windowed as well, and the side and main inputs will be lined up by window.


How to reassign an Application Pool (not Identity) for SharePoint 2013

Question: Background: Our server beyond the recommended limit for SP2013 (Software Boundaries and Limits) on the number of Application Pools per server (10). I'm wondering if that is what is causing a noticeable lag, especially in Central Administration.  I already fixed a time-out issue by extending the "Shutdown Time Limit" in IIS, but would like also to optimize the performance.
Question: I'd like several of the SP Web Applications grouped to share an AppPool, so I have 3 AppPools instead more than 10.  By order of preference (1) How do I do this in Central Administration, (2) in PowerShell, (3) or in IIS (most likely not the recommended practice)?  What is the best practice?
Update (6/29/17): I actually ended up deleted 5 unused Web Apps (7 left, including Central Admin).  That didn't really fix the lag issue on the WFE side, but this these two Powershell commands seem to:
<code>    PS> Set-SPEnterpriseSearchService -PerformanceLevel PartlyReduced
    PS> Restart-Service SPSearchHostController
</code>
Credit: https://sharepoint.stackexchange.com/questions/125174/reduce-noderunner-exe-impact

I still have a lag with making changes in Central Admin.  Still seeking a solution for that.
Answer: Each time you create a web application in sharepoint, you are invited to create a new application pool, via central admin.
Each time you create a service application in sharepoint (search, user profile, managed metadata), you are invited to create a new application pool, via central admin.
If you want to re assign the app pool, you can navigate to central admin > Manage service application > Select the service > Click properties in the ribbon > Change the applicaiton pool.
As for powershell code to create app pool or re assign services, i do not have them.
From IIS side you shall only create application pool in case you want to re assign existing services applicaitons to new app pool.
As for your central admninistration slowness, may you share your existing machine specs ? For a developer VM, I have run my VM fast with 4vcpu / 24 gb ram / 8 gb for sql (limit the sql instance to 8gb) / set the search to minimal performance (there are powershell for this)
Comment: If Central Admin had the same modify functionality as "Manage service applications" steps you gave me, I'd be set.  Instead, so far, I've settled on deleting and recreating our unused SPWebApps, and created an AppPool shared by several WebApps.  Googling showed that even the Powershell method had many steps and one site recommended not doing it on a production environment.
Comment: ..even doing that, didn't really fix the lag issue on the WFE side, but this these two Powershell commands did:
1) Set-SPEnterpriseSearchService -PerformanceLevel PartlyReduced

2) Restart-Service SPSearchHostController

https://sharepoint.stackexchange.com/questions/125174/reduce-noderunner-exe-impact
Comment: Thanks for your sharing. There is such options in central admin (Sharepoint 2013 on premise), maybe you talk about an other product ?
Comment: Regarding setting the search to minimum is it not recommended for production use, but for development environment yes. Maybe your IOPS or RAM allocated is unsufficient ? You mind sharing your VM Specs (CPU / RAM) ?
Comment: Not the issue, though -- it's a VM: Intel Xeon CPU E5-4620 0 @2.20GHz with 24GB RAM
Comment: I was getting this error from SP Health Analyzer. I resolved it by creating/assigning separate Service Accounts. Seemed to help: "The server farm account should not be used for other services.
[DOMAIN\FarmAccount], the account used for the SharePoint timer service and the central administration site, is highly privileged and should not be used for any other services on any machines in the server farm.  The following services were found to use this account: ...
Browse to http://.../_admin/FarmCredentialManagement.aspx and change the account used for the services listed in the explanation."


if $h$ is one-to-one function and $f(h(x))= g(h(x))$, then $f=g$. True or False?

Question: True or False? Given any set $X$ and given any function $f:X\rightarrow X$ , $g:X\rightarrow X$ and $h:X\rightarrow X$, if $h$ is one-to-one function and $f(h(x))= g(h(x))$, then $f=g$. Justify your answer.
I know that this is false as $f$ and $g$ may not be injective and hence $f$ not equals to $g$ But how do I prove it more rigorously ?  Like what counterexamples can I give ?
Answer: HINT: If $h=e^x$, then $h$ is one-to-one.  But the range of $h$ is the positive real numbers.  Can you construct $f$ and $g$ so that they agree on the positive real numbers, but not the negatives?
Answer: Actually I think this is true? (correct me if I'm mistaken) because you said h was a one to one function on itself. That means its also a bijection on itself because the cardinality of the sets is same (corollary of Cantor-Bernstein theorem) and by definition also a permutation.
Thus, we can conclude that the range of h must also be the codomain which here is defined to be the domain
Since f and g share this domain, namely X, we know for certain that they must agree on every point in their agreeing domains, thus satisfying the definition for function equality