Alternative ways to programmatically add user fields?

Question: Currently i use this to programmatically add user fields:
function openid_connect_entity_base_field_info(EntityTypeInterface $entity_type) {
    if ($entity_type->id() === 'user') {
        $fields['field_family_name'] = BaseFieldDefinition::create('string')
          ->setLabel(t('Family Name'))
          ->setDescription(t('Add a description'))
          ->setRevisionable(TRUE)
          ->setTranslatable(TRUE)
          ->setDisplayOptions('form', array(
            'type' => 'string_text_field',
            'weight' => 30,
            'default_value' => 0,
          ))
          ->setDisplayConfigurable('form', TRUE);
      }
return $fields;
}

but it results in the admin of the site unable to uninstall my module as shown:

Are there any alternative ways to programmatically add user fields without causing the admin of the website to be unable to uninstall the module that programmatically added the user fields?
Answer: I found a solution using .yml files in your modules config/install directory.
1. Create the user field
go to Configuration -> People -> Account Settings -> Manage fields and press "+ Add field". I'd recommend giving the field a unique machine-readable name to prevent conflicting user fields.

Enable the user field in Manage Form Display and Manage Display if you want it to appear in the users edit profile and registration page.
2.Exporting the user field configuration
go to Configuration -> Development -> Configuration Synchronization -> Export -> Single Item
Once you get to a screen similar to the one below, set configuration type to "Field" then set the configuration name to the field you have created.
In your modules Config/install folder(create one if needed) create a new file named after filename highlighted in red below.
Finally Copy the yml text into the file you just created.

then set configuration type to "Field Storage" then set the configuration name to the user field you have created.
In your modules Config/install folder(create one if needed) create a new file named after filename highlighted in red below (don't copy the one in the example your user field will have a different name).
Finally Copy the yml text into the file you just created.

YOU NEED BOTH YML FILES TO PROGRAMMATICALLY CREATE A USER FIELD
I recommend doing this for each user field you want to programmatically add rather than copying the configuration and changing a few properties.
With this method you can uninstall the module that created these user fields.
Comment: Note: The user field will be disabled on manage form display and manage display even if you enabled them before the configuration.
Comment: UUID has to be deleted before enabling the module.
Comment: With only these 2 YML files, when I install the module the custom user field is added in the list "Manage fields" (/admin/config/people/accounts/fields). But the new field is not displayed in the user edit form, so I can't change its value.
Comment: If you need to update or override existing fields, take a look at https://www.drupal.org/project/config_rewrite
Answer: Updated answer:
You can include the field as default configuration upon installing the module. By adding the field configuration to exportable files (yaml-files) these can then be installed and added.
When uninstalling the module the field and data will remain.
In your module create the folders config/install and place the configuration files there:
For example, if I add a field field_test_email I would place the field storage and then the instance on the User entity.
field.storage.user.field_test_email.yml
field.field.user.user.field_test_email.yml
https://www.drupal.org/docs/8/creating-custom-modules/include-default-configuration-in-your-drupal-8-module
Old answer:
If you want to keep the fields and its data the short answer is No, you cannot and shouldn't.  Field definitions, configuration are coupled to its creator "the module", and when uninstalling everything it did goes away with it.
Drupal used to support disabling a module, but that was removed. See explanation here: https://www.drupal.org/node/2225029

A huge debate and amount of thought went into this, with strong talk on both sides.
  Reasons for this include :
  * Because upgrades of some contrib module don't actually work (and can't > be made to work) if upgrading when they are disabled.
  * Because data left behind by a disabled module that is later re-enabled > causes integrity issues and cruft.
  * Some upgrades left behind data loss due to things like unexpected > dependencies, plugins or hooks being unavailable at key times - when things were disabled.
  .. that issue is several hundred arguments long, so It's unfair to over-summarize.

During an uninstall there are different  validators called to ensure the system does not break or keep data that cannot be handled later. For your case it is the FieldModuleUninstallValidator validator that gives you those error messages.
So to uninstall the module all data has to be deleted; You can try using the Field API bulk deletion for that: https://api.drupal.org/api/drupal/core!modules!field!field.purge.inc/group/field_purge/8.2.x
Comment: I found the same solution as you have, i've made a step by step tutorial how to do it below.
Comment: > You cannot programmatically create the fields to the User entity.

Not strictly true. No reason you can't create the config entities from code, without using the config import folder. FieldStorageConfig::create() and FieldConfig::create() will do it.
Comment: @Sam152 Thanks for the correction. Removed that part of my answer.
Answer: You can use this module to programmatically create fields and attach them to the User entity:
https://drupal.org/project/field_create
As you can read in the documentation, this module is meant for development purpose only. Enable it, create your fields and then uninstall it. This way, there is no "custom module" to install in order to have your custom fields.
Your website admin does not have to worry about uninstalling anything :)


Numpy fast for loop

Question: I have a list named "y" with 8 numpy arrays of the shape (180000,)
Now I want to create a new numpy array named "Collision" with the same shape that counts how many values of y are not 0. See the following example:
import numpy as np
collisions = np.zeros(len(y[0]), dtype=np.uint8)
for yi in y:
    collisions[np.where(yi > 0)] += 1

The calculation of this function takes a relatively long time. Is there a faster implementation to do this?
Comment: are you trying to count how many elements are greater than zero for each element, 0 to 180000 ? Although the code is not optimal, it should not take so long
Answer: I am not sure why your calculation takes so long, hope this helps to clarify, for example your list of array is like this:
import numpy as np
y = [np.random.normal(0,1,180000) for i in range(8)]

Running your code, it works ok:
collisions = np.zeros(len(y[0]), dtype=np.uint8)
for yi in y:
    collisions[np.where(yi > 0)] += 1

collisions
array([4, 2, 4, ..., 4, 4, 5], dtype=uint8)

You can do it a bit faster like this, basically making your list of arrays a matrix and doing a row sum of >0, but I don't see the problem with that above:
(np.array(y)>0).sum(axis=0)

array([4, 2, 4, ..., 4, 4, 5])

Answer: I'm assuming you're looking for something like this:
import numpy as np

# simulating your data by randomly generating numbers in [-0.5, 0.5)
y = np.random.rand(8, 180_000) - 0.5
print(y.shape) # (8, 180000)

collisions = np.sum(y > 0, axis=0, dtype=np.uint8)
print(collisions.shape) # (180000,)
print(collisions) # [4 4 4 ... 1 6 7]

Numpy fast for loop


Qual a diferença entre Varchar e Varchar2?

Question: Tenho uma dúvida em relação aos tipos de dados no oracle:
No Oracle, Qual é a diferença entre varchar e varchar2?
Answer: VARCHAR
Atualmente (até a versão 11G do Oracle Database), VARCHAR nada mais é do que um sinônimo para VARCHAR2 (desde o Oracle8), por isso a Oracle recomenda não utilizar este tipo de dado.
Existe a possibilidade de VARCHAR ser utilizado em versões futuras para ter uma semântica ou característica diferente de VARCHAR2, mas mesmo, estando tudo igual atualmente, utilize VARCHAR2 ao invés de VARCHAR, para evitar possíveis problemas futuros.
VARCHAR2
VARCHAR2 armazena caracteres alfanuméricos de tamanho variável, entre 1 e 4000 bytes ou caracteres. O tamanho padrão desta coluna é especificado em bytes.
Agora vem a pergunta, qual a diferença entre armazenar bytes ou caracteres?
Quando utilizamos caracteres multibyte, como por exemplo UTF-8 (para representar caracteres específicos em múltiplas linguagens), 1 único caractere pode ser armazenado em até 3 bytes. Nestes casos, uma palavra contendo caracteres especiais, como por exemplo, FÁBIO, poderá ter mais bytes do que caracteres (a letra Á, internamente será armazenada em 2 ou 3 bytes), por isso, recomenda-se, nestes casos, especificar o armazenamento da coluna em caracteres, ao invés de bytes.
Fonte: Qual tipo de dado devo usar: CHAR, VARCHAR ou VARCHAR2?
Comment: O UTF-8 pode usar até 4 bytes, não 3. Não sei se o mesmo ocorre no Oracle, mas no MySQL existem até dois tipos, o UTF8 e o UTF8MB4, o segundo é o completo, até 4 bytes.
Comment: Tem mais alguma diferença?


Extract into interface

Question: I have multiple classes for doing database access, each class contains a set of methods for extracting information from the database and passing it into another object. The problems is that most of the methods extract into a different object. What would be the best way to extract this into an interface to help tidy up my code and reduce the number of database connections
example class
public class RequestDB
  {

    public RequestDB(string connectionStringName)
    {
        connectionString = connectionStringName;
    }

    public void UpdateRequest(AssignmentRequest request)
    {
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("UpdateAssignmentRequest", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@assignmentRequestId", SqlDbType.Int));
        cmd.Parameters["@assignmentRequestId"].Value = request.RequestID;
        cmd.Parameters.Add(new SqlParameter("@jobTitle", SqlDbType.VarChar));
        ...
        try
        {
            // Move to a cache class library
            con.Open();
            cmd.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            Log.Write("Exception", "Unknown", "RequestDB", "UpdateRequest",    request.ToString(), ex.Message);

            throw new ApplicationException("Data source error.");
        }
        finally
        {
            con.Close();
        }
    }

    public int RequestAssignment(AssignmentRequest request)
    {
        int result = 0;
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("RequestAssignment", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@clientUserId", SqlDbType.Int));
        cmd.Parameters["@clientUserId"].Value = request.RequestingUserID;
        ...
        try
        {
            // Move to a cache class library
            con.Open();
            var reader = cmd.ExecuteReader();
            reader.Read();
            result = reader.GetInt32(reader.GetOrdinal("id"));
        }
        catch (SqlException ex)
        {
            Log.Write("Exception", "Unknown", "RequestDB", "RequestAssignment", request.ToString(), ex.Message);

            throw new ApplicationException("Data source error.");
        }
        finally
        {
            con.Close();
        }
        return result;
    }

    public List<AssignmentRequest> GetAllAssignmentRequests()
    {
        var result = new List<AssignmentRequest>();
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("GetAssignmentRequests", con);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            // Move to a cache class library
            con.Open();
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                result.Add(new AssignmentRequest()
                               {
                                   RequestID = reader.GetInt32(reader.GetOrdinal("requestId")),
                                   RequestingUserID = reader.GetInt32(reader.GetOrdinal("clientUserId")),
                                   ...
                               });
            }
        }
        catch (SqlException ex)
        {
            Log.Write("Exception", "Unknown", "RequestDB", "GetAllAssignmentRequests", "", ex.Message);

            throw new ApplicationException("Data source error.");
        }
        finally
        {
            con.Close();
        }
        return result;
    }

    public AssignmentRequest GetAssignmentRequestById(int requestId)
    {
        var result = new AssignmentRequest();
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("GetAssignmentRequestByID", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@requestId", SqlDbType.Int));
        cmd.Parameters["@requestId"].Value = requestId;
        try
        {
            // Move to a cache class library
            con.Open();
            var reader = cmd.ExecuteReader();
            reader.Read();
                result = new AssignmentRequest()
                {
                    RequestID = reader.GetInt32(reader.GetOrdinal("requestId")),
                    RequestingUserID = reader.GetInt32(reader.GetOrdinal("clientUserId")),
                    JobTitle = reader.GetString(reader.GetOrdinal("jobTitle")),
                    Department = reader.GetString(reader.GetOrdinal("department")),
                    ...
                };

        }
        catch (SqlException ex)
        {
            Log.Write("Exception", "Unknown", "RequestDB", "GetAssignmentRequestById", requestId.ToString(), ex.Message);

            throw new ApplicationException("Data source error.");
        }
        finally
        {
            con.Close();
        }
        return result;
    }

    public List<AssignmentRequest> GetRequestHistoryByClientUserId(int clientUserId)
    {
        var result = new List<AssignmentRequest>();
        SqlConnection con = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("GetRequestHistoryByClientUserId", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@clientUserId", clientUserId);
        try
        {
            // Move to a cache class library
            con.Open();
            var reader = cmd.ExecuteReader();
            while (reader.Read())
            {

                var request = new AssignmentRequest()
                                  {
                                      RequestID = reader.GetInt32(reader.GetOrdinal("requestId")),
                                      RequestingUserID = reader.GetInt32(reader.GetOrdinal("clientUserId")),
                                      JobTitle = reader.GetString(reader.GetOrdinal("jobTitle")),
                                      ...
                result.Add(request);

            }
        }
        catch (SqlException ex)
        {
            Log.Write("Exception", "Unknown", "RequestDB", "GetRequestHistoryByClientUserId", "", ex.Message);

            throw new ApplicationException("Data source error.");
        }
        finally
        {
            con.Close();
        }
        return result;
    }

}

Answer: Use generics, something like this:
public interface Repository<K, T> {
    List<T> find();
    T find(K key);
    K save(T value);
    void update(T value);
    void delete(T value);
}

Answer: I have seen several similar questions like these. My suggestion is DON'T rush to interface design.
Are those classes from your own code ? Can be modified ?
I have found that a good alternative techique to your problem, instead of rushing to interfaces, is to make a superclass or group of superclasses from the classes / objects that share common behavior (methods, properties). Later, they can be  migrated to an interface.
Step 1 (How classes are now) :
public class MSSQLConnection
{
  public void DoSomething() { ... }
}

public class XMLConnection
{
  public void TrySomething() { ... }
}

public class OracleConnection
{
    public void MakeSomething() { ... }
}

Step 2 (Rename / Refactor same ids.):
public class MSSQLConnection
{
  public void DoSomething() { ... }
}

public class XMLConnection
{
  public void DoSomething() { ... }
}

public class OracleConnection
{
    public void DoSomething() { ... }
}

Step 3 (Make common superclasss) :
public class BaseConnection
{
    public virtual void DoSomething() { ... }
}

public class MSSQLConnection: BaseConnection
{
  public override void DoSomething() { ... }
}

public class XMLConnection: BaseConnection
{
  public override void DoSomething() { ... }
}

public class OracleConnection: BaseConnection
{
    public override void DoSomething() { ... }
}

Cheers.
Answer: Use generics to create a common interface as pointed out by duffymo.
To share the database connection across your different database access classes
use constructor injection:
public class ExampleRepository1
{
  private SqlConnection _conn;
  public ExampleRepository(SqlConnection conn)
  {
    _conn = oConnection;
  }
}

public class ExampleRepository2
{
  private SqlConnection _conn;
  public ExampleRepository2(SqlConnection conn)
  {
    _conn = conn;
  }
}

// Share the database connection:

var conn = new SqlConnection(...);

try
{
  conn.Open();

  // You could also begin a transaction here
  // Use conn.BeginTransation()

  var repo1 = new ExampleRepository1(conn);
  var repo2 = new ExampleRepository2(conn);

  repo1.Find(...);
  repo2.Delete(...)
}
finally
{
  conn.Close();
}

Extract into interface


Why does a right hand overhead thumber roll right instead of left?

Question: Preface- When I say roll, I'm referring to the flight angles of pitch, yaw and roll.  Disc golfers also refer to this as hyzer angle.
Suppose an overstable PDGA style disc is thrown with a right hand backhand (RHBH).  Looking down on the disc from above, the disc spins clockwise.  The roll angle turns left (or hyzer).  Compare this to a right hand overhead "thumber" throw.  This throw is also spinning clockwise.  However, the thumber throw rolls right (or anhyzer).  Even more so, the thumber thrown flight often completes an entire 360 degree "barrel roll" before landing.  What flight mechanics are in play that cause the thumber to roll the opposite direction from a backhand?
Answer: If I understand your question correctly, you're describing what I would call a hammer throw where the disc is near veritcal over the head at release and whether a thumber or backhand delivery is clockwise spin for a right-handed throw.  The side of the disc moving forward provides lift and counters the angle of hyzer in a regular frisbee disc, but not so much with golf discs because of the additional weight of the discs, hence overstable discs tend to fall to the angle of hyzer.  This particular throw at first appears to defy that logic except that because of the angle of release, the lift generated by the faster forward-moving part of the disc is working -with- gravity instead of against it and therefore turns even harder, in this case turning harder to the left, or as you describe it, rolling to the right.  If you release the disc at the same angle, but flipped 180 degrees it will not not drop off to the left much at all, if any, and will tend to hold that angle until it makes contact with the ground and rolls.  It all comes down to the flight dynamics of the naturally-designed lift working with gravity instead of against it.
I hope I've explained this clearly - it's not easy to describe.
I personally use this shot as an up-shot if there are obstacles in front of me that I can clear by throwing over, but want the disc to come down flat and -not- roll or skip.
Bill Hileman
PDGA #1577


Printing fieldable panel pane custom field in template properly

Question: I've created a fieldable panel pane that has two fields, a title and a body. My template file looks like this:

        <?php print render($content['field_pane_title']); ?>

        <?php print render($content['field_body']); ?>

This seems fairly basic. Now, when I actually add one of these panes to a page, this is the DOM that is spit out:

    Test Pane

This is a test body.

Ignoring the fact that it's printing out empty field-label elements, why is the pane-title field being printed outside the pane-title h2 element? Is there a better way to be doing this? Thanks!
Answer: Ignoring the empty field-labels was a problem! The issue was with those field labels, which were generated by the Fences module. They were somehow causing the h2 to close too early.
I ended up modifying the module myself, so that the template checks if a field label exists before adding it to the DOM.
I also ended up finding an issue on the module's issue page which has a patch to fix it directly, in case anyone is interested: https://www.drupal.org/node/2605808


Existence of Non-Trivial, Convex, Open Set in $C_{\mathbb{C}}[0,1]$ Under $L^{0}$ Metric

Question: I've been struggling with this problem for the last four hours. The problem is to show that the space of $\mathbb{C}$-valued continuous functions on $[0,1]$ under the metric
$d(f,g)=\int_{0}^{1}\frac{\vert f-g\vert}{1+\vert f-g\vert}dx$ has no non-trivial convex open sets. I refer to d as the $L^{0}$ metric since such a metric is often used on the space of random variables on a probability space.
I've taken two thus far fruitless approaches. In the first approach, I assumed there existed such a non-trivial convex set U then  tried to show that any $f\in C_{\mathbb{C}}[0,1]$ can be expressed as convex combination of elements of U.
Second thought was to use the fact that $C_{\mathbb{C}}[0,1]$ is a dense subspace of $L^{p}$, with $0<p<1$, under the $L^{0}$ metric. I wanted to show that since such a set does not exist in $L^{p}$ under this metric then it cannot exist in $C_{\mathbb{C}}[0,1]$. I was able to get as far as showing that these $L^{p}$-spaces have no non-trivial, convex, open sets under this metric.
Answer: Suppose $U$ is a nonempty convex open set. By translation, we can assume that $0\in U$. Thus there is a $\delta > 0$ such that $B_\delta(0) = \{ f : d(f,0) < \delta\} \subset U$. Fix an integer $n > 2/\delta$, and for $0 \leqslant k \leqslant n$, let
$$\varphi_k(x) = \begin{cases} n\left(x - \frac{k-1}{n}\right) &, \frac{k-1}{n}\leqslant x \leqslant \frac{k}{n}\\ n\left(\frac{k+1}{n}-x\right) &, \frac{k}{n} \leqslant x \leqslant \frac{k+1}{n}\\ \qquad 0 & x < \frac{k-1}{n} \lor x > \frac{k+1}{n}\end{cases}$$
where, for $k = 0$ resp $k = n$ the part outside $[0,1]$ is of course ignored. Then we have
$$\sum_{k=0}^n \varphi_k(x) = 1$$
for all $x\in [0,1]$, and for any continuous $f\colon [0,1]\to\mathbb{C}$,
$$f = \sum_{k=0}^n \frac{1}{n+1}\cdot \underbrace{\left((n+1)\cdot \varphi_k\cdot f\right)}_{\psi_k}.\tag{1}$$
Now,
$$d(\psi_k,0) = \int_0^1 \frac{\lvert \psi_k(x)\rvert}{1+\lvert\psi_k(x)\rvert}\,dx = \int_{(k-1)/n}^{(k+1)/n} \frac{\lvert \psi_k(x)\rvert}{1+\lvert\psi_k(x)\rvert}\,dx < \int_{(k-1)/n}^{(k+1)/n}\,dx = \frac{2}{n} < \delta$$
for $1 \leqslant k < n$, and adjusting the integral limits to $0$ resp. $1$ for $k = 0$ resp. $k = n$ shows that also $d(\psi_0,0) < \delta$ and $d(\psi_n,0) < \delta$.
Thus $(1)$ shows that $f$ is a convex combination of functions in $B_\delta(0) \subset U$, and since $U$ is convex, it follows that $f\in U$. Since $f$ was completely arbitrary, this shows that $U = C([0,1],\mathbb{C})$. Thus there are only two convex open sets - $\varnothing$ and $C([0,1],\mathbb{C})$ - in the topology defined by $d$.
Comment: So you're basically building a sort of explicit partition of unity. Shit! Why didn't I think of that? Thanks for your help!
Comment: @SingularDegenerate Actually, what Fischer did was more than building a sort of explicit partition of unity, because this partition additionally satisfies the condition that the sum of functions in the partition is exactly $1$. And this condition is quite important in his proof.
Comment: That's actually what "unity" in "partition of unity" means, Sam, most people require it


Send php variable to js in drupal 8 block

Question: I am trying to attach js files attach_library() inside a custom block.
However, I can neither add the libraries via block nor add a library. attach_library() does not accept variables like drupal_add_js.
Any ideas on A) how to send a php variable in the block::build() function and how to attach the library through the build() instead of the twig templates?
Answer: This is from the documentation how to attach libraries to a render array of a block plugin and send php variables to js:

Attaching to a render array of a Block Plugin
To give another example of attaching a library to a render array, If
  you are building a block plugin in your module, you can attach the
  libraries to the render array in the build() function of your class
  extending the BlockBase class (as of Drupal 8 beta 6).

return array(
      '#theme' => 'your_module_theme_id',
      '#someVariable' => $some_variable,
      '#attached' => array(
        'library' =>  array(
          'your_module/library_name'
        ),
      ),
    );

Attaching configurable JavaScript
In some cases, you may want to add JavaScript to a page that depends
  on some computed PHP information.
In this case, create a JavaScript file, define and attach a library
  just like before, but also attach JavaScript settings and have that
  JavaScript file read those settings, via drupalSettings (the successor
  to Drupal 7's Drupal.settings). However, to make drupalSettings
  available to our JavaScript file, we have to do the same work as we
  had to do to make jQuery available: we have to declare a dependency on
  it.
So that then becomes:

cuddly-slider:
  version: 1.x
  js:
    js/cuddly-slider.js: {}
  dependencies:
    - core/jquery
    - core/drupalSettings

and
$build['#attached']['library'][] = 'fluffiness/cuddly-slider';
$build['#attached']['drupalSettings']['fluffiness']['cuddlySlider']['foo'] = 'bar';

https://www.drupal.org/developing/api/8/assets


Recognise anacrusis in MuseScore, from Lilypond's midi files

Question: I generate midi files in LilyPond and import them into MuseScore for playback.
When there is an anacrusis in the piece, I use LilyPond's \partial command:
\version "2.21.0"
\score {
    {
        \time 4/4
        \partial 4
%  0
            a'8 b' |
%  1
        c''4 4 4 c''8 b' |
%  2
        a'4 4 4
        \bar "||"
    }
    \layout {}
    \midi {}
}

Compiling the code generates a midi file. Opening this file with MuseScore will render the score, and a "MIDI import panel" will also pop up.  Several settings (such as "MuseScore instrument", "Max. Quantisation", etc.) can be adjusted, then clicking "Apply" will re-import the file.
The setting called:
"Recognise anacrusis" [English (GB)] / "Recognize pickup measure" [English (US)]
seems like it should be just what I need to get the anacrusis to render correctly, but it doesn't work. Whether selected or not, MuseScore continues to render the score as if there isn't an anacrusis.
LilyPonds \partial command apparently isn't effecting the midi file in a way that MuseScore understands.

I do know how to fix the anacrusis manually:

Selecting everything, and Cutting,
Right clicking the first measure and selecting "Bar Properties" / "Measure Properties",
Changing the setting in "Bar Duration" / "Measure Duration" - "Actual" (e.g. from 4/4 to 1/4),
Then Pasting everything back in.

This works, but is so tedious.

To get MuseScore's "Recognise anacrusis" setting to work, I need to change the LilyPond code:
I have to explicitly write a "bar" with a time signature that corresponds to the length of the anacrusis, and then set the real time signature for the first complete bar. (This makes the \partial command unnecessary.)
To get the pdf to look right, I also have to adjust their appearance of the time signature, and to set the first bar number to zero.
\version "2.21.0"
\score {
    {
        \time 1/4
        \set Staff.timeSignatureFraction = 4/4
        \set Score.currentBarNumber = #0
%  0
            a'8 b' |
%  1
        \time 4/4
        c''4 4 4 c''8 b' |
%  2
        a'4 4 4
        \bar "||"
    }
    \layout {}
    \midi {}
}

This does work; the code compiles, the pdf is accurate, and MuseScore can recognise the anacrusis.  But my problems with this method are, that it isn't really right from a music theory perspective (it's not a bar of 1/4), and it's compromising my LilyPond code.
Why won't LilyPond's \partial command effect the midi file in a way that MuseScore's "Recognise anacrusis" setting will understand?
Is there a simpler way to get MuseScore to understand an anacrusis from LilyPond?
Comment: My understanding from working with the Musescore program is that Musescore indeed *does* treat the anacrusis as a measure in 1/4 time, and you *will* need to work around this. https://musescore.org/en/handbook/3/measure-operations#properties displays how an anacrusis can get treated as a measure with a nominal duration matching the overall time signature but an actual duration of 1/4.
Comment: @Dekkadeci — That link shows documentation for the method suggested in the dot points of my question. But I think you're right: this is the only way that MuseScore can encode an anacrusis.
Answer: Unfortunately LilyPond's \partial command doesn't actually encode the anacrusis into the midi file.  This is because midi files don't even have a way to encode a "real" anacrusis.
This leaves either of the workaround solutions suggested in the question.
The second solution posed in the question (using a bar with a reduced time signature at the start of the piece) is probably the best method, because it works in LilyPond, looks fine in the pdf, and is exactly what MuseScore's "Recognise Anacrusis" is looking for.
I'm not sure why LilyPonds's partial function doesn't implement an anacrusis in the way that MuseScore will recognise, but it doesn't.


Partial check in

Question: We are having problem with 'check in'. Is there any way to check in some changes in a file and leaving behind other changes.Because we want test some changes only. I know we can exclude some changed files for check in. But is there any way to exclude some changes in single file and check in other changes in same file??
Comment: A better way is to break up the work into smaller tasks that you can check in independently - with unit tests. You could Shelve your files and check in partially. I think partial checkins will lead to broken builds.
Comment: Consider that you can't do a "partial build". You don't want to check in anything that you can't build and test.
Answer: No, TFS forces all changes in a file to be checked in at the same time. There is no way to check in "only some" changes for a given file. Git does have some support for this but it is complicated. If you decide to use Git, TFS has a tool called git-tf that allows you to integrate between the two systems.

Partial check in


PHP not getting any response from the form on the previous page

Question: I have the file "1.php" and the file "2.php" ...
In "1.php", there's a HTML form:
<form action="2.php" method="post">

    Field 1:
    <input type="text" class="form-control" id="db_username">

    Field 2:
    <input type="password" class="form-control" id="db_password">

    Field 3:
    <input type="text" class="form-control" id="db_passphrase">

  <button type="submit" class="btn btn-default">Next</button>
</form>

While in "2.php", the action must be applied in PHP .. like that:
<?php
// Process of Step "1"
$db_username = $_POST['db_username'];
$db_password = $_POST['db_password'];
$db_passphrase = $_POST['db_passphrase'];

if( !isset($db_username) || !isset($db_password) || !isset($db_passphrase) ) {
    header("Location: 1.php?error=1");
    die();
}
?>

However .. the values of $_POST['db_username'], $_POST['db_password'] and $_POST['db_passphrase'] is empty...
Comment: Because you must use `name=""` instead of `id=""`
Answer: You need to use the name attribute to later access the $_POST data, as in:
<input type="text" class="form-control" id="db_username" name="db_username">

Answer: You need to give the inputs a name and acces that value through $_POST['name'].
id is not relevant for php form handling
Answer: You have to give name for the textfields like this
<input type="text" class="form-control" id="db_username" name="db_username">

Answer: Put name instead of id. it should work


How to correctly join tables and return rest API object. [MYSQL + PHP]

Question: I've got 3 table and need to make a nice smooth SELECT which could return me data from all of them.
This is how tables looks like:
cases
    ------------------------------------------------------
    |   id   |  case_id  |  country |         vin        |
    ------------------------------------------------------
    |   22   |    9856   |    US    |  VF7S6NFZF56215577 |
    ------------------------------------------------------
    |   23   |    9857   |    GB    |  GF7S6NFZF56215579 |
    ------------------------------------------------------
    |   23   |    9858   |    US    |  VF7S6NFZF56215577 |
    ------------------------------------------------------

calculations
    ---------------------------------------------------------------------
    |   calculation_id   |  case_id  |  totalcosts |       currency     |
    ---------------------------------------------------------------------
    |         95         |    9856   |    78945    |         USD        |
    ---------------------------------------------------------------------
    |         96         |    9856   |    12345    |         USD        |
    ---------------------------------------------------------------------
    |         97         |    9857   |    23986    |         XTB        |
    ---------------------------------------------------------------------
    |         98         |    9858   |    77896    |         USD        |
    ---------------------------------------------------------------------

positins
    --------------------------------------------------------------------------
    |   calculation_id   |  case_id  |  text  | method |   guide | position_id
    ---------------------------------------------------------------------------
    |         95         |    9856   | textAB |   E    |    N/A  |     11     |
    ---------------------------------------------------------------------------
    |         96         |    9856   | textAB |   E    |    N/A  |     22     |
    ---------------------------------------------------------------------------
    |         96         |    9856   | textAC |   L    |    N/A  |     3      |
    ---------------------------------------------------------------------------
    |         96         |    9856   | textAD |   E    |    Y    |     8      |
    ---------------------------------------------------------------------------
    |         97         |    9856   | textAE |   E    |    N/A  |     11     |
    ---------------------------------------------------------------------------

This is how result should looks like (this is part of rest API so i will provide JSON)
[
   {
      caseId:9856,
      country:'US',
      totalcosts:12345,
      currency:'USD',
      positions:[
         {
            method:'E',
            guide:'N/A',
            positionId:22,
            text:'TextAB'
         },
         {
            method:'L',
            guide:'N/A',
            positionId:3,
            text:'TextAC'
         }         {
            method:'E',
            guide:'Y',
            positionId:8,
            text:'TextAD'
         }
      ]
   },
   {
      caseId:9858,
      country:'US',
      totalcosts:77896,
      currency:'USD',
      positions:[

      ]
   }
]

Eplenation of response:

cases: User always provide VIN number and query need to collect rest of data. Important thing is that there can be multiple cases for each vin. There should be provided always all of them.
calculations: There can be multiple calculations for each case. Always just last one is important (the one with highest calculation_id for that case_id).
positions: just extract data here

What did I try:
SELECT c.case_id, c.country, cl.totalcosts, cl.currency,
  GROUP_CONCAT(DISTINCT po.text ORDER BY 1) AS texts,
  GROUP_CONCAT(DISTINCT po.part_position_id ORDER BY 1) AS partIds,
  GROUP_CONCAT(DISTINCT po.guidenumber ORDER BY 1) AS guideNumbers
FROM cases
  LEFT JOIN calculations as cl on c.case_id = cl.case_id AND c.country = cl.country
  LEFT JOIN axnmrs_positions as po on c.case_id = po.case_id AND cl.calculation_id = po.calculation_id

Idea behind this select is that I group all that data from positions table and then in my php file I will explode all of them by comma (,).
I think this is possible solution but really ugly one and I bet it is also not best for performance issues.
Can anyone think about better solution for this situation?
Thanks a lot!
p.s. All tables and query was simplify to keep this question clear. I'm sorry for possible typos
Comment: If you return JSON maybe you can use JSON_TYPE => https://dev.mysql.com/doc/refman/5.7/en/json.html and directly output JSON from you DB
Comment: That query do the job you want? Because if does then I dont see the ugly part. If doesnt I suggest you return your query without group_concat and build the json in a for .. loop in your php side
Comment: Hi @JuanCarlosOropeza if you return it without group_concat then i will not get all results from positions table, just one. Also I'm not sure if this select last calculation or first one
Answer: This is made somewhat more complicated by the fact that you've not told us anything about the cardinality/foreign key constraints here e.g. how to handle number calculations
So I can't tell you what your query should look like.
I do agree that using GROUP_CONCAT() in this way is ugly.
Hence, using a simpler example of a 1:m relationship between a relation named 'header' and one named 'detail' ....
SELECT h.id, h.date, h.other,
d.type, d.unit_cost, d.quantity
FROM header h
LEFT JOIN detail d
ON h.id=d.header_id
WHERE ds.date
;

$output=array();
while ($r=mysqli_fetch_assoc($result)) {
    extract($r);
    if (!is_array($output[$id])) {
       $output[$id]=array(
              'date'=>$date, 'other'=>$other,
              'detail'=>array());
    }
    if ($type) {
       $output[$id]['detail'][]=array(
          'type'=>$type, 'unit_cost'=>$unit_cost,
          'quantity'=>$quantity);
    }
}
print json_encode($output);

The LEFT JOIN and if ($type) { are needed if want to include header records which have no detail.


Why is JSHint not showing me the filenames?

Question: I'm getting the following from the console...
"C:\Program Files (x86)\JetBrains\WebStorm 11.0.3\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" C:\myProj\node_modules\grunt-cli\bin\grunt --gruntfile C:\Projects\dcloud-v2-ui\Gruntfile.js test
Running "jshint:all" (jshint) task

     16 |          })
                     ^ Missing semicolon.
     76 |          })
                     ^ Missing semicolon.
    113 |      })
                 ^ Missing semicolon.

>> 3 errors in 407 files
Warning: Task "jshint:all" failed. Use --force to continue.

Aborted due to warnings.

Process finished with exit code 3

Why is is not showing me the file with the missing semi colons? I notice is is finishing on a 3. Is that related to an error in Grunt or just telling me the 'hinting' failed?
Answer: It looks like this is a bug in grunt-contrib-jshint. It previously worked as you would expect, but this is a regression in recent versions. Unfortunately the root of the issue doesn't seem to have been identified, and there's surprisingly little activity on the issue.
Comment: this was confirmed by a colleague actually too so thanks for the answer it's not the answer but its right!
Answer: The bug in the chosen answer hasn't been fixed yet. The workaround suggested in the issue thread is to use jshint-stylish - an external reporter for jshint
The setup for my project is as easy as running npm install --save-dev jshint-stylish and adding the following to options section of jshint:
jshint: {
  options: {
    reporter: require('jshint-stylish')
    ...
  },
...


Changing order of integration using chart technique error?

Question: Consider: $$ \int_0^3 \int_4^{\sqrt{25-z^2}} \int_{-\sqrt{25-y^2-z^2}}^{\sqrt{25-y^2-z^2}} dxdydz$$
i. Clearly sketch the graph of the solid whose volume this triple integral determines.
ii. Present all other triple integrals in rectangular coordinates equivalent to the given triple integral but each of a different order than the others.<br>
Here is my graph and chart for changing order of integration any help at all in confirming my answers would be great! I also added my work if that helps as well.
Answer: No, that is not correct.
Note that only $x$ can take negative values, the other two coordinates, whichever order you are doing it, must have $y\geq 4$ and $z\geq 0$ built in to the limit.  The region is only in 2 of the 8 octants.
The $\mathrm{d}x\,\mathrm{d}z\,\mathrm{d}y$ integral should have limits
$$
\int_{4}^{5}\int_{0}^{\sqrt{25-y^2}}\int_{-\sqrt{25-y^2-z^2}}^{\sqrt{25-y^2-z^2}}\mathrm{d}x\,\mathrm{d}z\,\mathrm{d}y
$$
similarly the other four.
Comment: ty! if i can ask a question, when given that first triple integral, can i assume that 4 is going to be the lowest for all y values? and same with z? and we get 5 for dy's highest value cause we are solving y from dz with respect to dy? Thanks again for answering.


Multi task learning

Question: I have a dataset where all observations are measured several times and reported outcomes correspond to those measurements. In other words, my set of data points looks like
$\{x_i, y_{i_1}, y_{i_2}, ... y_{i_n}\}$
I would like to make a predictive model to predict $y$ from $x$. What should I try to predict? The average of $y_i$?
I've heard something about multi task learning, where each observation is considered as $n$ different regression problems and all problems share the same kernel. Can anybody elaborate on this?
Comment: It would be good if you could supply us with more information on your dataset, or just a snapshot of your dataset with the question of interest.
Comment: Do you have a deterministic, but noisy process that is yielding your measurements y_i?  Or do you have a process with some hidden variable that leads to different measurements y_i?  The structure of your problem will determine what you should do to model the measurements.
Comment: wouldn't this be multiple instance learning rather than multi-task learning (which involves learning different, but related problems with shared attributes, whereas in this case the problems are all the same)?
Comment: I hope the rephrased question help you to understand the problem. The problem is I have a several Y for a given X and I wonder, what should I target when I am building a predictive model ?! the average of Y? or ...
Comment: there are some hidden variable; since the measuring is not exactly unique. but I am not really interested in them.
Answer: If observations (for the same subject) are uncorrelated, then one thing you can do is to average observations and use some prediction technique (e.g., regression). However, if these observations are correlated, then the data set is longitudinal.. then you need to use, say generalized linear mixed models...
Answer: If the observations are independent, and the ordering of the observations is unimportant, then you could simply estimate the mean as Actuary suggests (+1); however the fact that multiple observations are made suggests that the spread of the observations may be of intrinsic interest, in which case it would be a good idea to model the distribution of plausible values for the observation (for a Gaussian, this would be modelling the conditional variance as well as the conditional mean).
I did some work on predictive uncertainty a few years ago, and the method described in
G. C. Cawley, N. L. C. Talbot, R. J. Foxall, S. R. Dorling and D. P. Mandic, Heteroscedastic kernel ridge regression, Neurocomputing, vol. 57, pp 105-124, March 2004.
might be of some interest (MATLAB demo here).  It ought to be fairly straightforward to make a basic multiple instance version assuming the observations are conditionally i.i.d.
BTW I think this kind of problem is known as "multiple instance learning" in machine learning, there is a fair amount of research on this indexed by Google Scholar, so hopefully there may be something relevant to your problem there somewhere (too many papers, too little time to read them... ;o).
Answer: All of the answers above suggest techniques that assume the data are uncorrelated (with the exception of the later part of user9292's response).  I highly doubt that is the case here since you have multiple observations from the same subjects.  As a results, you'll likely to take the correlation into account using methods such a mixed models, generalized estimating equations (GEE), or transitional models.  This site has plenty of information on all of these methods and how you might be able to apply them.
Answer: Basically you can use Pareto Front  when choosing between different solutions. One particular approach which extensively uses Pareto front is genetic programming. There are several good frameworks for this stuff where you can setup you problem and see the result without months of coding, check answer to this question with list of frameworks.

Multi task learning


Consider the Sequence and prove.

Question: Question: Consider the sequence defined as
$a_1 = 2$
 and
$a_k = a_{k-1}+2k-1$
 for all positive integer
$ k \geq 2$
.  Show that
$a_n = 1+\sum(2i-1, i = 1 .. n)$
.
  Hint:  Start with
$\sum(2i-1, i = 1 .. n)$
and use the recursive definition of the sequence.
Answer: I am unsure where to start on to show the proof. I can't find an example. I know the equation is $n^2+1$, I need to use the recursive definition. Any help would work. Thanks!
Comment: Prove your guess by induction.
Answer: The proof is by induction on $n$, the base case being obvious. For the inductive step, for any $n\ge 1$, proceed as follows:
\begin{align}
a_{n+1}
  &=a_n+2(n+1)-1
\\&=1+\left(\sum_{k=1}^n2k-1\right)+2(n+1)-1
\\&=1+\left(\sum_{k=1}^{n+1}2k-1\right)\end{align}
The last step follows from the inductive definition of $\sum_{k=1}^n$; by definition, $\sum_{k=1}^{n+1}b_k=\left(\sum_{k=1}^n b_k\right)+b_{n+1}$ holds for all $n\ge 1$. Let $b_k=2k-1$.
Answer: you may proceed as follows:
\begin{array}
 aa_n & = a_{n-1}&+ 2n&- 1\\
a_{n-1} & = a_{n-2}&+2(n-1)&-1 \\
\ldots&=\ldots&+\ldots&-1\\
a_2 &=a_1&+2(2)&-1
\end{array}
Add all rows and see what you get. Further simplification will will you
$$a_n=2\Big(\frac{n(n+1)}{2}\Big)-n+1=n^2+1$$

Consider the Sequence and prove.


How do I show that $(\hat{\beta_1}-\beta_1)$ and $ \bar{u}$ are uncorrelated, i.e. that $E[(\hat{\beta_1}-\beta_1) \bar{u}] = 0$?

Question: I asked this question and thought I found a solution to it by working through the steps in my textbook. Unfortunately the comments told me that I was completely wrong and only got the proof to work by chance, so now I'm really lost and frustrated.
To fix notation, the model is
$$
y_i = \beta_0 + \beta_1 x_i + u_i,
$$
where $u_i$ is the error term.
Earlier I showed that
$\hat{\beta}_1 = \beta_1 + \sum_{i=1}^n w_i u_i$, where
$w_i = \frac{x_i - \bar{x}}{SST_x}$, and $SST_x = \sum_{i=1}^n (x_i - \bar{x})^2$.
Here is what I did so far:
\begin{align}
E[(\hat{\beta_1}-\beta_1) \bar{u}] &= E[\bar{u}\displaystyle\sum\limits_{i=1}^n w_i u_i] \\
&=\displaystyle\sum\limits_{i=1}^n  E[w_i \bar{u} u_i] \\
&=\displaystyle\sum\limits_{i=1}^n w_i E[\bar{u} u_i] \\
&= \displaystyle\sum\limits_{i=1}^n w_i \left(Cov(\bar{u}, u_i) + E(\bar{u})E(u_i)\right) \\
&= \displaystyle\sum\limits_{i=1}^n w_i Cov(\bar{u}, u_i) \\
&= \displaystyle\sum\limits_{i=1}^n w_i Cov\left(\frac{1}{n} \displaystyle\sum\limits_{i=1}^n u_i, u_i\right) \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i Cov\left(\displaystyle\sum\limits_{i=1}^n u_i, u_i\right) \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i E\left(\left[u_i - E(u_i)\right]\left[ \displaystyle\sum\limits_{i=1}^n u_i - E\left(\displaystyle\sum\limits_{i=1}^n u_i\right)\right]\right) \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i E\left(\left[u_i - E(u_i)\right]\left[ \displaystyle\sum\limits_{i=1}^n u_i - \displaystyle\sum\limits_{i=1}^n E\left(u_i\right)\right]\right) \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i E\left(\left[u_i - 0\right]\left[ \displaystyle\sum\limits_{i=1}^n u_i - 0\right]\right) \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i E\left(u_i\displaystyle\sum\limits_{i=1}^n u_i\right) \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i \left[E\left(u_i u_1 +\cdots + u_i u_n \right)\right] \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i \left[E\left(u_i u_1\right) +\cdots + E\left(u_i u_n \right)\right] \\
\end{align}
At this point, I want to say that because we assume the errors for each observation are i.i.d:
\begin{align}
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i \left[E(u_i) E(u_1) +\cdots + E(u_i) E(u_n)\right] \\
\end{align}
But if this is the case then since $E(u_i) = 0, \forall i$, everything just cancels and I don't need to use the definition of $w_i$, which one of the comments mentioned I needed to use last time.
This is in chapter 2 of my book and the book says that "it's obvious that..." so maybe it is obvious. I keep asking for hints but I feel like I don't understand them, but I want to keep trying.
Is this correct? If not, does anyone have a hint on this part of the problem, assuming I'm on the right path? I can probably make the steps less verbose, but I want to make sure it's right first. The last time I worked this out, I reached the right conclusion but the proof was all wrong, so I'm assuming I only have it right by chance again.
Comment: Two things on the proposed proof: (a) on the second line, the factor $1/n$ shouldn't appear as ${\rm E}(\sum R_i) = \sum{\rm E}( R_i)$; (b) on line 8, there is an expectation missing since ${\rm cov}(R, S) = {\rm E} \left[\left\{ R - {\rm E}(R) \right\}\left\{ S - {\rm E}(S) \right\}\right]$.
Comment: Hint: the relation ${\rm E}(u_i u_j) = {\rm E}(u_i){\rm E}(u_j)$ holds for all $j$ except one. Which one is it?
Comment: Why can the expectations operator move past w?
Comment: @QuantIbex Dumb typos, thank you. I'm still not using the definition of $w_i$ since I just get everything to cancel, so this can't be right.
Comment: @QuantIbex Right! So we have

\begin{align}
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i \left[E\left(u_i u_1\right) +\cdots + E(u_i u_i) + \cdots+ E\left(u_i u_n \right)\right] \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i \left[E(u_i) E(u_1) +\cdots + E(u_i^2) + \cdots + E(u_i) E(u_n)\right] \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i E(u_i^2) \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i \left[Var(u_i) + E(u_i) E(u_i)\right] \\
&= \frac{1}{n}\displaystyle\sum\limits_{i=1}^n w_i \sigma^2 \\
\end{align}
Comment: (continued)

\begin{align}
&= \frac{\sigma^2 }{n}\displaystyle\sum\limits_{i=1}^n w_i \\
&= \frac{\sigma^2 }{n}\cdot 0\\
\end{align}

by definition of $w_i$.
Comment: This seems fine to me, well done. If you were to do this proof again, you could jump directly from line 3 to line 11 (and maybe index the elements of the second sum by $j$ to avoid potential confusion).
Comment: $$\sum\limits_{i=1}^n  E[w_i \bar{u} u_i] = \sum\limits_{i=1}^n w_i E[\bar{u} u_i] \\$$

Is this supposed to be conditional on $x_1, x_2, ..., x_n$?
Comment: @beets Great question. You may want to see my [comment](http://stats.stackexchange.com/questions/64654/how-do-i-show-that-hat-beta-1-beta-1-and-baru-are-uncorrelated-i#comment371146_64654) and my [question](http://stats.stackexchange.com/questions/195291).
Answer: Strategy
It can be illuminating to move back and forth among three points of view: the statistical one (viewing $x_i$ and $y_i$ as data), a geometrical one (where least squares solutions are just projections in suitable Euclidean spaces), and an algebraic one (manipulating symbols representing matrices or linear transformations).  Doing this not only streamlines the ideas, but also exposes the assumptions needed to make the result true, which otherwise might be buried in all the summations.
Notation and assumptions
So: let $y = X\beta + u$ where $y$ is an $n$-vector, $X$ is an $n$ by $p+1$ "design matrix" whose first column is all ones, $\beta$ is a $p+1$-vector of true coefficients, and $u$ are iid random variables with zero expectation and common variance $\sigma^2$.  (Let's continue, as in the question, to begin the coefficient vector's indexes with $0$, writing ${\beta} = ({\beta_0}, {\beta_1}, \ldots, {\beta_p})$.)  This generalizes the question, for which $X$ has just two columns: its second column is the vector $(x_1, x_2, \ldots, x_n)'$.
Basic properties of OLS regression
The regression estimate $\hat{\beta}$ is a $p+1$-vector obtained by applying a linear transformation $\mathbb{P}$ to $y$. As the solution to the regression, it projects the exact values $X\beta$ onto the true values $\beta$: $$\mathbb{P}\left(X\beta\right) = \beta.$$  Finally--and this is the crux of the matter before us--it is obvious statistically (thinking of these values as data)--that the projection of $y = (1, 1, \ldots, 1)'$ has the unique solution $\hat{\beta} = (1, 0, 0, \ldots, 0)$: $$\mathbb{P}1_n' = (1, 0, 0, \ldots, 0),$$ because when all the responses $y_i$ are equal to $1$, the intercept $\beta_0 = 1$ and all the other coefficients must vanish.  That's all we need to know.  (Having a formula for $\mathbb{P}$ in terms of $X$ is unimportant (and distracting).)
Easy preliminaries
Begin with some straightforward algebraic manipulation of the original expression:
$$\eqalign{
(\hat{\beta}-\beta)\bar{u} &= (\mathbb{P}y-\beta)\bar{u} \\
&= (\mathbb{P}(X\beta+u)-\beta)\bar{u} \\
&= \mathbb{P}(X\beta+u)\bar{u} - \beta\bar{u} \\
&= (\mathbb{P}X\beta)\bar{u} + \mathbb{P}u\bar{u} - \beta\bar{u} \\
&= \beta\bar{u} + \mathbb{P}u\bar{u} - \beta\bar{u}\\
&= \mathbb{P}(u\bar{u}).
}
$$
This almost mindless sequence of steps--each leads naturally to the next by simple algebraic rules--is motivated by the desire to (a) express the random variation purely in terms of $u$, whence it all derives, and (b) introduce $\mathbb{P}$ so that we can exploit its properties.
Computing the expectation
Taking the expectation can no longer be put off, but because $\mathbb{P}$ is a linear operator, it will be applied to the expectation of $u\bar{u}$.  We could employ some formal matrix operations to work out this expectation, but there's an easier way. Recalling that the $u_i$ are iid, it is immediate that all the coefficients in $\mathbb{E}[u\bar{u}]$ must be the same.  Since they are the same, each one equals their average.  This can be obtained by averaging the coefficients of $u$, multiplying by $\bar{u}$, and taking the expectation.  But that's just a recipe for finding $$\mathbb{E}[\bar{u}\bar{u}] = \text{Var}[\bar{u}] = \sigma^2/n.$$  It follows that $\mathbb{E}[u\bar{u}]$ is a vector of $n$ values, all of which equal $\sigma^2/n$.  Using our previous vector shorthand we may write $$\mathbb{E}[(\hat{\beta}-\beta)\bar{u} ] = \mathbb{E}[\mathbb{P}(u\bar{u})] = \mathbb{PE}[u\bar{u}]=\mathbb{P}1_n'\sigma^2/n=(\sigma^2/n, 0, 0, \ldots, 0).$$
Conclusion
This says that the estimated coefficients $\hat{\beta_i}$ and the mean error $\hat{u}$ are uncorrelated for $i=1, 2, \ldots, p$, but not so for $\hat{\beta_0}$ (the intercept).

It is instructive to review the steps and consider which assumptions were essential and which elements of the regression apparatus simply did not appear in the demonstration.  We should expect any proof, no matter how elementary or sophisticated, will need to use the same (or stronger) assumptions and will need, in some guise or another, to include calculations of $\mathbb{P}1_n'$ and $\mathbb{E}[u\bar{u}]$.
Comment: Right, the $u_i$ are exchangeable, so once you know one $e[u_i \bar{u}]$, you know all of them and you've found the entire vector. Thank you for the great answer.
Comment: This is really helpful. One clarification: "Recalling that the $u_i$ are iid, it is immediate that all the coefficients in $E[u\bar{u}]$ must be the same. Since they are the same, each one equals their average. This can be obtained by averaging the coefficients of $u$, multiplying by $\bar{u}$, and taking the expectation." What coefficients are you referring to? That part still confuses me a little.
Comment: $u$ is a vector with $n$ coefficients, $u_1, u_2, \ldots, u_n$.  Thus, $\mathbb{E}[u\bar{u}]$, being the expectation of a vector $u$ times a scalar $\bar{u}$, itself is a vector with $n$ coefficients $\mathbb{E}[u_i\bar{u}]$. The parallels with the working-out in your comments to the question should be clear. What is illuminating about this symmetry-based argument is that it shows how the iid assumption implies the conclusion.
Comment: Just so I'm clear. All the coeffs in $E[u\bar{u}]$ are the same because

\begin{align}
E[u \bar{u}] &= E
\begin{bmatrix}
u_1 \bar{u} \\
\vdots \\
u_n \bar{u}
\end{bmatrix} \\
&=
\begin{bmatrix}
E[u_1 \bar{u}] \\
\vdots \\
E[u_n \bar{u}]
\end{bmatrix} \\
&= \frac{1}{n}
\begin{bmatrix}
E[u_1 \sum\limits_{j=1}^n u_i] \\
\vdots \\
E[u_n \sum\limits_{j=1}^n u_i]
\end{bmatrix} \\
&=\frac{1}{n}
\begin{bmatrix}
E[u_1 u_1] + \cdots + E[u_1 u_n] \\
\vdots \\
E[u_n u_1] + \cdots + E[u_n u_n] \\
\end{bmatrix} \\
&=\frac{1}{n}
\begin{bmatrix}
\sigma^2 \\
\vdots \\
\sigma^2 \\
\end{bmatrix} \\
\end{align}
Comment: You are correct, but that is not the reasoning I presented here: I am intentionally avoiding that calculation. After the second equality you only have to observe that $\mathbb{E}[u_i\bar{u}] = \mathbb{E}[u_j\bar{u}]$ for all $i$ and $j$ because the $u_i$ are iid (actually, exchangeability would suffice) and you can jump directly to the conclusion.  There's no need for all the extra algebra.
Comment: $$\sum\limits_{i=1}^n  E[w_i \bar{u} u_i] = \sum\limits_{i=1}^n w_i E[\bar{u} u_i] \\$$

Is this supposed to be conditional on $x_1, x_2, ..., x_n$?
Comment: @BCLC The $x_i$ are rows of the design matrix, which is just a bunch of numbers.  But if you want to view them as realizations of a (vector) random variable *that is independent of the* $u_i$ , then nothing changes if you interpret all expectations as conditional on those values.
Comment: @whuber I suppose the proof is okay for a beginner in linear regression who is not expected to know advanced probability theory, but to be strict about it, I think all these expectations have to be first conditional, shown to be a constant and then deduced to be unconditional. Am I wrong?


Opencv videocapture cannot capture image

Question: I'm using cmake to build my project with opencv. There are two sub-projects, A and B, under the top directory. A has no opencv function while B uses VideoCapture to get image from webcam. There is no problem at first.
However, after I add the code from B to A, B can still capture image from webcam, but A cannot do the same thing, the error is below:
HIGHGUI ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
VIDIOC_STREAMON: Inappropriate ioctl for device

It is strange, and I find that VideoCapture cannot get image in A, the code is below
VideoCapture cam;
cam.open(0);
if(!cam.isOpened()){
  cout << "Failed to open webcam" << endl;
  return false;
}
Mat Image;
cam >> Image;
if(Image.empty())
        cout<<"Image empty"<<endl;

"Image empty" is always in console, which means it just cannot capture the image at all!!
I followed some suggestions such as install "v4l2ucp", but there is no folder under "/usr/lib/" named "libv4l", so LD_PRELOAD=/usr/lib/libv4l/v4l1compat.so dose not work.
I will very appreciate it if someone could give me some help to solve the problem in project A.
Answer: The file might not necessarily be there. Try find / -name "*v4l1compat.so*" 2>/dev/null or find / -name "*libv4l*" 2>/dev/null. It should succeed since your project B captures frames just fine. Then try to export found file to LD_PRELOAD.
If it won't succeed - check out your libv4l installation.
And make sure you trying to open() the correct camera which is not already in use.
Comment: Thanks for your answer. I found `/usr/lib/i386-linux-gnu/libv4l/v4l1compat.so` and I set `LD_PRELOAD=/usr/lib/i386-linux-gnu/libv4l/v4l1compat.so` when I ran my project, but the same problem still exsited.


drupalLogin in functional JavaScript tests passes locally but fails on Drupal.org?

Question: During functional JavaScript testing one can create and login a user by using the following code:
$this->testUser = $this->drupalCreateUser(['permission']);
$this->drupalLogin($this->testUser);

This works fine when testing locally with PhantomJS. However after uploading this code to drupal.org it keeps failing randomly (sometimes 1 test, sometimes 2) with the following error:

User dOGIV5Iu successfully logged in.
Failed asserting that false is true.

This appears to be coming from the assertion in BrowserTestBase::drupalLogin, which should normally check that the user is logged in correctly.
$this->drupalGet('user/login');
$this->submitForm([
  'name' => $account->getUsername(),
  'pass' => $account->passRaw,
], t('Log in'));

// @see BrowserTestBase::drupalUserIsLoggedIn()
$account->sessionId = $this->getSession()->getCookie($this->getSessionName());
$this->assertTrue($this->drupalUserIsLoggedIn($account), new FormattableMarkup('User %name successfully logged in.', ['%name' => $account->getAccountName()]));

How come that a test function does work locally, but not on Drupal.org? Even stranger is that this function is used throughout multiple core tests without issues. And why is this assertion failing randomly?
Comment: the only idea I have is that there is a fatal error/exception on that page.  why is hard to debug, you could try to print the page content which should be visible in the output. Could be the php version, an extension or something like that. One common difference to local is that the testbot runs tests in a subdirectory
Comment: Thanks @Berdir, I started thinking it might be because of the web driver engine replacing PhantomJS, but from what I could find I was not really sure if that was implemented already and if it could indeed result in different results.
Answer: After reading through the Jenkins results for the nth time, it finally occurred to me that during Drupal core (and other contrib) tests all JavaScript tests always run separately from the other tests. All my tests however were in the same list.
This happened because all test classes started originally as functional tests, but as I went ahead some of them were changed into functional javascript tests. Class extending was changed into JavascriptTestBase and the tests were working perfectly fine. At least, when running individually. Running multiple tests at the same time made them somehow interfere with one another.
The problem was that I had never changed the namespace from Functional into FunctionalJavascript. The Drupal testrunner makes sure that all FunctionalJavascript tests are ran one by one, but performs other tests in parallel.
After changing the namespace, the tests were listed separate from the rest, ran after each other and finally showed up green on Drupal.org


Invoke without Func in Property's getter for .Net 2.0

Question: I would like to use Invoke in getter, how to do it when using .Net 2.0 not e.g. 4.0? For .Net > 2.0 we can use Func and what is replacement for .Net 2.0?
Here is example for .Net 4.0 (from link)
public ApplicationViewModel SelectedApplication
{
    get {
            if (this.InvokeRequired)
            {
                return (ApplicationViewModel)this.Invoke(new Func<ApplicationViewModel>(() => this.SelectedApplication));
            }
            else
            {
                return _applicationsCombobox.SelectedItem as ApplicationViewModel;
            }
        }
}

Comment: You could create your own delegate.
Answer: Since you're using .NET 2.0, you won't have the Func delegate available to you, but you can use the MethodInvoker delegate.
You won't be able to use the lambda expression syntax with .NET 2.0, but you can use the "anonymous delegate" syntax (which is pretty much the same thing), as shown in the code example below.
Querying data in UI controls from a non-UI thread is generally an uncommon thing to do; usually your UI controls trigger events that execute on the UI thread, so you gather the data you need from your UI controls at that time and then pass that data on to some other function, so you don't need to worry about doing an Invoke.
In your case, though, you should be able to do something like this:
public ApplicationViewModel SelectedApplication
{
    get
    {
        if (this.InvokeRequired)
        {
            ApplicationViewModel value = null; // compiler requires that we initialize this variable
            // the call to Invoke will block until the anonymous delegate has finished executing.
            this.Invoke((MethodInvoker)delegate
            {
                // anonymous delegate executing on UI thread due calling the Invoke method
                // assign the result to the value variable so that we can return it.
                value = _applicationsCombobox.SelectedItem as ApplicationViewModel;
            });
            return value;
        }
        else
        {
            return _applicationsCombobox.SelectedItem as ApplicationViewModel;
        }
    }
}

EDIT: Now that I look at your .NET 4.0 code sample and also look at the Invoke function, I see how it can return a value (not something that I've had a reason to use before).
Well, the MethodInvoker delegate does not expect a return value, but as @haiyyu pointed out, you could define your own delegate.  For instance, you would just need to define your own Func<TResult> delegate, and the original code would probably work fine:
// this is all that is required to declare your own Func<TResult> delegate.
delegate TResult Func<TResult>();

Sample code from the MSDN page:
public partial class Form1 : Form
{
    public Form1()
    {
        // Create a timer that will call the ShowTime method every second.
        var timer = new System.Threading.Timer(ShowTime, null, 0, 1000);
    }

    private void ShowTime(object x)
    {
        // Don't do anything if the form's handle hasn't been created
        // or the form has been disposed.
        if (!this.IsHandleCreated && !this.IsDisposed) return;

        // Invoke an anonymous method on the thread of the form.
        this.Invoke((MethodInvoker) delegate
        {
            // Show the current time in the form's title bar.
            this.Text = DateTime.Now.ToLongTimeString();
        });
    }
}

Answer: Use delegates, they are a sort of typed function pointers.
Here's some more reading: http://msdn.microsoft.com/en-us/library/ms173171%28v=vs.80%29.aspx
Comment: Can I create it inside property's getter?
Comment: Yes, a delegate is a type of a variable, and it can be passed the same way any other variable gets passed.


Ordem em classe para Serializar Objeto C#

Question: Estou trabalhando em um projeto de NFe, e optei por usar serialização de uma classe com base a estrutura do xml que quero. Implementei a classe, tudo funcionou como esperado, root, nodes etc, porém estou com um problema, referente a ordem os meus elementos no xml de saída
Está é minha classe que serializo:
public class nfeProc
    {
        public infNFe infnfe;

    }

    public class infNFe
    {
        public ide ide;
        public emit emit;

    }

    public class ide
    {
        public string  cUF { get; set; }
        public string  cNF { get; set; }
        public string natOp { get; set; }
        public string indPag { get; set; }
        public string mod { get; set; }
        public string serie { get; set; }
        public string nNF { get; set; }
        public string dEmi { get; set; }
        public string tpNF { get; set; }
        public string cMunFG { get; set; }
        public string tpImp { get; set; }
        public string tpEmis { get; set; }
        public string cDV { get; set; }
        public string tpAmb { get; set; }
        public string finNFe { get; set; }
        public string procEmi { get; set; }
        public string verProc { get; set; }

    }

    public class emit
    {
               public string CNPJ { get; set; }
               public string xNome { get; set; }
               public string xFant { get; set; }
               public enderEmit enderEmit;
               public string IE { get; set; }
               public string CRT { get; set; }

    }

    public class enderEmit{

               public string xLgr { get; set; }
               public string nro { get; set; }
               public string xCpl { get; set; }
               public string xBairro { get; set; }
               public string cMun { get; set; }
               public string xMun { get; set; }
               public string UF { get; set; }
               public string CEP { get; set; }
               public string cPais { get; set; }
               public string xPais { get; set; }
    }

Esta é minha saída exemplo, sem alguns elementos só para exemplificar:
    <nfeProc>
      <infnfe>
        <ide>
          <natOp>HUEHUEBR</natOp>
          <mod>HUEHUEBR</mod>
          <nNF>HUEHUEBR</nNF>
        </ide>
        <emit>
          <enderEmit>
            <CEP>04858480</CEP>
            <cPais>55</cPais>
          </enderEmit>
          <xNome>USHAUSHAUSHUA</xNome>
          <xFant>USHAUSHAUSHUA</xFant>
          <IE>USHAUSHAUSHUA</IE>
          <CRT>USHAUSHAUSHUA</CRT>
        </emit>
      </infnfe>
    </nfeProc>

E o meu problema é a ordem dos elementos, não consigo manipular eles A saída que espero é esta, onde o nó enderEmit vai abaixo de alguns elementos determinados por mim. Exemplo dois (xNome e xFant):
<nfeProc>
  <infnfe>
    <ide>
      <natOp>HUEHUEBR</natOp>
      <mod>HUEHUEBR</mod>
      <nNF>HUEHUEBR</nNF>
    </ide>
    <emit>
    <xNome>USHAUSHAUSHUA</xNome>
      <xFant>USHAUSHAUSHUA</xFant>
     <enderEmit>
        <CEP>04858480</CEP>
        <cPais>55</cPais>
      </enderEmit>
      <IE>USHAUSHAUSHUA</IE>
      <CRT>USHAUSHAUSHUA</CRT>
    </emit>
  </infnfe>
</nfeProc>

Answer: Decora todas as propriedades da classe com o atributo [XmlElement(Order = 1)]
Comment: É isso ai valeu a ajuda !


Access ESXi via the exclusive network management interface for an IPMI board

Question: I would like to know if it's possible to access the ESXi configuration through vSphere using an dedicated network interface for IPMI connection with the motherboard.
We have some Supermicro boards, specifically X9SCM, with IPMI in a private 10.0.0.0/8 network and we would like to give the ESXi management console some IP address in this network too. But the guests should be able to bridge to a real IP address.
I can only think in some obscure VLAN configuration, but I don't know if I'm right.
Thanks in advance,
Additional Interface:
VLAN 1 - Managament (10.0.0.0/8)
VLAN 10 - Servers (x.y.z.0/26)
Gigabit Ethernet eth0 = VLAN10
Gigabit Ethernet eth1 = Not in use (and not supported by ESXi; it needs an additional e1000e network driver)
Fast Ethernet for IPMI = VLAN1
Answer: IPMI will give you access to the physical console or serial port as if you were sitting in front of the server.
It's not some magical out-of-band network interface available to the operating system¹.
1: Unless you have IBM servers with IMM2 management interfaces. Then you can actually configure the IMM to forward ports to the internal management interface on the server. Super-handy!
Comment: Yeah… that's what I said :) You get the KVM console and a serial port, but there's no direct access as far as the OS is concerned on the SuperMicro hardware.
Comment: Hello MikeyB. I don't know if its the case. Throught this port we can access a full featured web manager console; with KVM, SOL and etc. So there's much more than a single serial port.
Comment: Hmm... Now I understand. So it's "almost" an useless port for my situation. Thanks Mikey. Really thanks.


Find limit of integral basic integral

Question: I'm trying to solve this question:

Find $$\lim_{n\to\infty}\int_0^1(1-x)^n\cos{x}\ dx$$

I would really appreciate if you gave me some direction. The hint was that predecessor doesn't need to be found. thanks
Answer: Since $|\cos x|\leq 1$
$$-\frac{1}{n+1}=-\int_0^1(1-x)^ndx\leq\int_0^1(1-x)^n\cos{x}\;dx\leq\int_0^1(1-x)^ndx=\frac{1}{n+1}$$
By the squeeze theorem we get that
$$\lim_{n\to \infty}\int_0^1(1-x)^n\cos{x}\;dx=0$$
Comment: +1, nice. Also took the liberty of LaTeX cleanup edits
Comment: Sure! Thank you. Im pretty new here and i'm still learning to write clean LaTeX

Find limit of integral basic integral


How do I add the tax inside the if statement when I chose one variable already?

Question: I am trying to create a tax calculator in Java, but I just can't seem to figure out how to tax the income when I have already used the if statement to verify the province so I can do the calculations.
How do I do the calculations without using a else if statement within the if statement?
(I just started Java and suck. So sorry for all the mistakes)
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {

    Scanner income = new Scanner(System.in);
    System.out.println("What is your taxable income: ");
    double taxable_income = income.nextDouble();
    System.out.println("You're income is: " + taxable_income);

    Scanner province = new Scanner(System.in);
    System.out.println("In which province do you live? (1 for BC, 2 for Alberta, and 3 for Ontario):  ");
    int province_inhabiting = province.nextInt();
    System.out.println("You chose " + province_inhabiting);

    if (province_inhabiting == 1) {
      taxable_income <= 40707 {
        tax = income & 0.0506;
      }

      (taxable_income >= 40707 && taxable_income <= 81416) {
        tax = income & 0.077;
      }

       (taxable_income >= 81416 && taxable_income <= 93476) {
        tax = income & 0.105;
      }

      (taxable_income >= 93476 && taxable_income <= 113503) {
        tax = income & 0.105;
      }

      }
    }
  }
}

The problem I'm having here is that I want to do the calculation within the if statement, the problem is I just don't know how to do it within the if statement. Basically let's say province is a variable which has 3 parts to it (BC, Ontario, and Alberta) now within this if statement I want to take the inputted income (the user inputs earlier) and tax it by let's say 5.6% or * 0.056. How would I do that under an if statement in Java.
Comment: "How do I do the calculations without using a else if statement within the if statement?", nope, that's how you do it. You put `if`s and `else if`s inside the other `if`.
Comment: Aside: `tax = income & 0.0506` what do you expect this to do?
Comment: With the if statement i want to verify which province the user has inputted then within that if statement I want to apply the tax calculations, but that is the problem which I can't figure for the life of me. I can't tax it within that one province. I want to use the else-if statement for the second province.
Comment: I'm not sure I understand the problem you're having: nesting `if`s? Having more than one province? Do you think you can't have more than one `if` per method? Are you familiar with any other programming language (or even just pseudo-code) that you could use as a bridge to explain what you're trying to do? If so please [edit] your question to include that as well.
Comment: Yes I can explain in python. Basically let's say province is a variable which has 3 parts to it (BC, Ontario, and Alberta) now within this if statement I want to take the inputted income and tax it by let's say 5.6% or * 0.056. How would I do that under an if statement?
Comment: Ok, please [edit] your question to include the python version so we can go from there
Comment: Ok I will do that right now
Comment: You can put an if statement within an if statement, as Federico klez Culloca already said: `if (provinceInhabiting == 1) { if (someOtherCondition) { ... } }`.
Comment: Thank you guys for the help, just figured it out!
Answer: For almost all the times, you can replace if else if else statement to using a  Map object, and this is a better practice in a lot ways. You should checkou principle in Software Design Pattern, like this one open-close principle. You can add a lot of key:values to the map, and your code is not modified, but added.
if (a == 1){
   print(1)
} else if (a >1 && a < 100){
   print(10)
} else {
   print(100)
}

//change the code to
HashMap<@Nullable Predicate<Integer>, @Nullable Consumer<Integer>> map = Maps.newHashMap();
map.put(x -> x == 1, (x) -> {
    System.out.println(1);
});
map.put(x -> x > 1 && x < 10, (x) -> {
    System.out.println(10);
});
map.put(x -> x != 1 && !(x > 1 && x < 10), (x) -> {
    System.out.println(100);
});

int a = 1;
for (Map.Entry<Predicate<Integer>, Consumer<Integer>> entry : map.entrySet()) {
    if (entry.getKey().test(a)) {
        entry.getValue().accept(a);
    }
}


Why are most of my Timefields blank when editing a node?

Question: I have a node with four timefields. When I want to edit the node, all the timefields, except the first one (that has a default value), are blank.
I found this issue still doesn't solved:
https://www.drupal.org/node/1970252
How can I solve this? I could populate the missing fields via javascript?
Altenatives of timefield module?
Comment: I'm afraid I'm going to have to vote for closure here. There's a number of different things that could cause this and we're not given any indication of what might be the cause (e.g. did this start to happen after a certain module install? is there things causing DB corruption? did this happen after cloning your environment?)
Comment: Added small patch to fix: https://www.drupal.org/node/1970252
Answer: Some of the starting pointers I can think of.

Ensure there is no custom code altering the form field. Search for hook_form_alter/hook_form_FORM_ID_alter in your custom modules.
Check your drupal error logs, if there is any error log related to timefield.
Check your browser console, if there is a JS error.
Check the timefields module issue queue if there is already such error reported, that would be the best place to start from.
Try installing timefields module on vanilla drupal and check if the same issue persist, in that case add this in the issue queue of module.
Make sure all timefields get data when saved for first time, debug using devel module,go to 'devel/php' page and run dpm(node_load('your_node_id'));, see inside node object if those fields have value or not.
"Like s there anyway the possibility to keep the values of the fields somewhere, so I could populate the missing fields via javascript?" Yes, you can only if field values are getting saved to database, in fact simple would be to do at php level something like
function my_module_form_alter($form, &$form_state, $form_id) {
  if($form_id == 'your_form_id') {
    if(isset($form[#node]->nid)) {
      $form['yourtime_field']['#default_value'] = $form['#node']->yourtime_field['und'][0]['value'];
    }
  }
}

But this is not the solution, this will be re-attempt of what drupal already trying to do. Best would be to identify why values are not showing by default.
Comment: glad it helped you!
Comment: Found same issue here: https://www.drupal.org/node/1970252
Comment: Added small patch to fix


Find the number of items in $10000$ sets of 10 throws each in which you would expect no even numbers.

Question: Given to us is that we have an irregular six-faced die and the expectation that in $10$ throws, $5$ even numbers show up is twice the expectation that $4$ even numbers show up.
The question( as in the title) is:
"How many items in $10000$ sets of $10$ throws each, would one expect no even numbers?"
What I did:
I assumed a binomial model and then I equated the probabilities of $5$ and $4$ even numbers respectively. It gave me the probability of success as $\frac{5}{8}$.
I don't really understand how to proceed further.
Please help.
Comment: I agree p(even) gives p(5 even)=2*p(4 even). Now what is the chance in one set of 10 throws you get no evens?  Then multiply by 10000 to get the expected value.
Comment: @ross millikan: okay i got it.
Comment: You could write up the answer and (after a wait) accept it so this doesn't stay unanswered
Comment: Will do that soon
Answer: Given n = 10, calculate p = probability of getting an even number.
As you calculated it to be 5/8,
So p = 5/8, q = (1-p) = 3/8.
Now according to the question, we need to find how many sets out of 10000 sets would produce odd outputs only like (1,3,5)?
This means no evens i.e. P[X=0].
So the answer for the problem would be 10000*P[X=0] = 1(approx)


Horse Race: number of different trifecta bets

Question: In a race with 17 horses, how many different trifecta bets can be made.  a trifecta is when you pick the first three finishers in the exact order.
Comment: why bother betting at all? just give me your $4000
Comment: There are 17 x 16 x 15 ways
Answer: $17\times 16\times 15$. There are $17$ choices for first. For each of these there are $16$ choices for second. For each of these combinations there are $15$ choices for third.
Answer: Here is one way to look at the problem:

Choose $3$ out of $17$ horses: $\binom{17}{3}=\frac{17!}{14!\times3!}=680$
Multiply by the number of different ways to order these $3$ horses: $3!=6$

And the result is $680\times6=4080$.
Answer: Also, since order matters this is a permutation:
$$
P_{3}^{17} = \frac{17!}{14!} = 17 * 16 * 15 = 4080
$$


Como aproveitar a consulta em dois relatórios de layouts distintos?

Question: Tenho dois relatórios em Jasper para visões diferentes sobre a mesma massa de dados. É tanto que os parâmetros e a consulta são exatamente iguais.
Existe algum método, inerente ao Jasper Reports, que permita eu escrever essa consulta/esses parâmetros apenas uma única vez e aproveitar em ambos .jrxmls? Ou devo fazer eu mesmo algum método de substituição textual para permitir isso?
Comment: Como você faz os seus relatórios hoje com relação a fonte de dados ? Você coloca as consultas dentro de cada relatório ?
Comment: @hebertrfreitas sim, desse jeito mesmo. Consulta dentro do `jrxml`
Answer: O jasperreports possui o conceito de DataSources, onde você pode montar a fonte de dados do seu relatório a partir de vários formatos e enviá-la para os seus relatórios.
Você precisa executar um código que retorne alguma implementação da interface JRDataSource
Existem algumas implementações desta classe dependendo de como você deseja passar seus dados, seguem exemplos:

JRResultSetDataSource Caso você queira trabalhar com um ResultSet
JRBeanCollectionDataSource Caso você queira passar uma Collection qualquer como List, Set, etc
JRXlsDataSource Caso você queira usar um arquivo do Excel como fonte de dados.

Basicamente, ao invés de montar a consultar no relatorio e mandar ele executar a consulta você passar a fonte de dados para o método fillReport, segue exemplo:
InputStream isDesignRelatorio = this.getClass().getResourceAsStream("path/to/report.jasper");
JRDataSource dataSource = new JRResultSetDataSource(this.getResultSet());
JasperPrint print = JasperFillManager.fillReport(isDesignRelatorio,this.getParametros(), dataSource);
ByteArrayOutputStream pdfStream = new ByteArrayOutputStream();

JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(Arrays.asList(print)));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(pdfStream));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration() ;
configuration.setCreatingBatchModeBookmarks(true);
exporter.setConfiguration(configuration);
exporter.exportReport();

Algumas observações sobre este trecho de código:
Este exemplo assume que você tem um arquivo jrxml já compilado em formato .jasper, eu compilo o relatório pela ferramenta de design para que o código não "perca tempo" fazendo isso, no entanto você também pode optar por compilar em tempo de execução.
Neste caso acrescente uma linha compilando o relatório, por exemplo:
 JasperReport report = JasperCompileManager.compileReport(isDesignRelatorio);

O método this.getResultSet() é para representar a sua fonte de dados que neste caso seria um ResultSet, mas como expliquei anteriormente pode ser até uma Collection de um DTO montado por você ou até mesmo uma entidade do banco.
Neste caso troque a implmentação do data source de new JRResultSetDataSource(this.getResultSet()); para algo como new JRBeanCollectionDataSource(this.getCollection());
Este exemplo escreve a saída da exportação em um ByteArrayOutputStream, caso você esteja trabalhando com um sistema web pode escrever diretamente no outputstream.
Também existe a possibilidade de gerar a saída em um arquivo físico, basta explorar as formas de exportação.


Semisimple quotient modules

Question: I am trying to show quotients of semisimplens modules need be semi simple but without use of correspondence. I am stuck on a minor (but major) detail:
(note, I showed submodules of semi simple need be semi simple)
Let $M$ be a semi simple module, let $N \subseteq M$ be a submodule.
Then we can write $M= N \oplus N'$ where $N'$ is some other submodule of $M$
Then we wish to show $M/N$ is semi simple, but this boils to showing
$(N \oplus N') / N$ is semisimple, if I can show this is isomorphic to $N'$,
Then Im done as $N'$ is a submodule and it is semi simple. How can I show
$(N \oplus N') / N \simeq N'$ ? I tried writing the short exact seq:
$\{0\} \rightarrow N \rightarrow N \oplus N' \rightarrow (N \oplus N') / N \rightarrow \{0\}$ But couldn't gather any info from here. help? hints?
Answer: Try naming a map $N \oplus N' \to N'$. What's the image? What's the kernel?
Comment: yeah via projection right? so the image need be everything? so the kernel is trivial? is that correct
Comment: Where is $(n, 0)$ sent to?

Semisimple quotient modules


How do I play this correctly in key or without?

Question: im trying to learn this song by a Chinese singer.
If you look at the score here (it's a keyboard score) you will see on top left corner it says key in Bb however when I transpose that to my keyboard the left hand, it doesn't sound right. Does it mean in this score I'm suppose to transpose my right hand and not my left?
If yes I don't think my Roland entry level model is capable of splitting it
Comment: I doubt you need to have hands in different keys. Is there any chance you could post a link to the song on youtube, or at least give us the title of the song (like me, quite a few of our users won't be able to read Chinese).
Comment: I'm voting to close this question because the links are dead, and the question no longer contains enough information to be answerable.
Comment: It's only a guess - never seen this stuff before- but the chords are written in Bb, and the numbers relate to notes from that key - 1=Bb, 2=C, 3=D etc. If it was played in a different key, the numbers would still relate to the new key, but the chords won't fit. More often than not (!) both hands play in the same key in one song.
Comment: Er, where is the score?
Comment: Why not just play it in Bb. It's only two flats. What you're asking doesn't make any sense. You're asking to transpose left hand only or right hand. Just learn both hands in the same key. If you have an entry level piano I doubt you can split transpose
Answer: I'm not sure why you would need to transpose anything.  If I were trying to play this on a keyboard from the sheet music you linked to, I would just play the chords in the key of B flat as written (B flat, Dm, E flat, Bflat etc.) and play the melody with the right hand.  The numbers refer to the notes of the scale so in B flat, 1 = B flat, 2 = C, 3 = D, 4 = E flat, 5 = F, 6 = G and 7 = A.  Hope that helps!


Isometry, completion of a normed $\mathbb{K}$-vector-space, banach-space

Question:
Let $(E,\|\cdot\|)$ be a normed $\mathbb{K}$-vector-space and $(\overline{E},\iota)$ a completion of $E$. Show, that
1) For every normed space $F$ is $\iota^\ast\colon L(\overline{E}, F)\to L(E, F), T\mapsto T\circ \iota$ an isometry.
2) Is $F$ a banach space, then is $\iota^\ast\colon L(\overline{E}, F)\to L(E, F)$ and isometric isomorphism.
3) In 2) it is important, that $F$ is complete.

I want to show these statements.
I have to show, that $\iota^\ast\colon L(\overline{E}, F)\to L(E, F)$ is an isometry. Therefore:
$\|\iota^\ast(f)\|_E=\|f\|_{\overline{E}}$. Since $(\overline{E},\iota)$ is a completion of $E$ we know, that $\iota: E\to \overline{E}$ is an isometry.
It is $\|\iota^\ast(f)\|_E=\|f\circ \iota\|_E$.
How can I proceed from here? I could stipulate like this, but do not think, that it helps to show equality:
$\|f\circ \iota\|_E\leq \|f\|_{op}\|\iota(f')\|_E$, for $f'\in L(E,F)$
Since $\iota$ is an isometry, then we get: $\|f\|_{op}\|\iota(f')\|_E=\|f\|_{op}\|f'\|_{E'}$
at 2):
Here I need to show, that $\iota^\ast$ is surjective, since from 1) we get that it is injective.
at 3):
I do not now how to give a counterexample.
Can you give me some hints on how to tackle this problem?
Thanks in advance.
I gave a bounty worth 100 reputations, for a full answer.
Comment: Ad 1): recall the definition of the operator norm, $\lVert f\rVert = \sup \dotsc$. Ad 2) all that remains is the surjectivity of $\iota^{\ast}$, that amounts to showing that every $f \in L(E,F)$ can be continuously extended to $\overline{E}$. Ad 3): consider $F = E$.
Comment: I know that for the operatornorm it is $\|x\|\leq 1$, hence I can stipulate further and get $\|f'\|_E$?
Comment: Write the definitions out, $\lVert f\rVert_{L(\overline{E},F)} = \sup \dotsc$, and $\lVert f\circ\iota\rVert_{L(E,F)} = \sup \dotsc$. Compare. What guarantees the two are the same?
Comment: Do you mean it like this: $\|f\|_{L(\overline{E},F)}=\sup_{\|x\|_\overline{E}\leq 1}\| f(x)\|_F$ and $\|f\circ\iota\|_{L(E,F)}=\sup_{\|\iota(x)\|_E}\|f(\iota(x)\|_F$. Since $\iota$ is an isometry, we know that $\|\iota(x)\|_{\overline{E}}=\|x\|_E$.
Comment: Yes. So what do you know about the sets $\{ x \in \overline{E} : \lVert x\rVert \leqslant 1\}$ and $\{ \iota(y) : \lVert y\rVert \leqslant 1\}$?
Comment: That they attain their supremum at the same value.
Comment: I gave a bounty worth 100 reputations, for a full answer. Thanks in advance.
Comment: @DanielFischer Do you mind giving further help? Thank you.
Answer: For the clarity I'll explicitily assume that you mean the following. Both $F$ and $E$ are normed spaces over the field $\Bbb K$, where $\Bbb K=\Bbb R$ or  $\Bbb K=\Bbb C$, $\iota:E\to\overline{E}$ is an isometry, and for given normed spaces $E',E''$ over the field $\Bbb K$, $L(E',E'')$ denotes the space of continuous linear operators from $E'$ to $E''$ endowed with the norm $\|T\|_{L(E',E'')}=\sup\{\|T(x)\|_{E''}: x\in E', \|x\|_{E'}=1\}$, for each $T\in L(E',E'')$. Now it remains to use Daniel Fischer's comment.
Put $B=\{x\in E, \|x\|_{E}=1\}$ and $\overline{B}=\{x\in \overline{E}, \|x\|_{\overline{E}}=1\}$. Since $\iota$ is an isometry, $\iota(B)\subset \overline{B}$. Moreover, since $\iota$ is an isometric embedding of the metric $E$ into its completion, $\iota(B)$ is dense in $\overline{B}$. Indeed, let $x\in \overline{B}$ be an arbitrary point. Since $\iota(E)$ is dense in $\overline{E}$, there exists a sequence $\{x_n\}$ of points of $E\setminus\{0\}$ such that a sequence $\{\iota(x_n)\}$ converges to the point $x$. Then a sequence $\{y_n\}$, where $y_n=x_n/\|x_n\|_E$ for each $n$, belongs to $B$ and a sequence $\{\iota(y_n)\}$ converges to the point $x$, because of continuity of the map $\Bbb K\times E\to E$, $(\lambda, y)\mapsto \lambda y$ at a point $(1,x)$ and the convergence of the sequence $\{\|x_n\|_E\}$ to $1$.
1) Let $T\in L(\overline{E},F)$ be an arbitrary operator. Then $$\|T\|_{L(\overline{E},F)}=\sup\{\|T(y)\|_{F}: y\in \overline{B}\}=\sup\{\|T(y)\|_{F}: y\in \iota(B)\}=
\sup\{\|T(\iota(x))\|_{F}: x\in B\}=
\sup\{\|(T\iota)(x)\|_{F}: x\in B\}=$$
$$\sup\{\|\iota^*(T)(x)\|_{F}: x\in B\}=
\|\iota^*(T)\|_{L(E,F)}.$$
The second equality holds because the map $T$ is continuous  and $\iota(B)$ is dense in $\overline{B}$.
2) 3) To be continued.


Generate random variable with given pdf

Question: I want to generate a random variable $X$ with the pdf $f(x) = \Bigg\{ f_1(x)$ if $x < 1$ and $f_2(x)$ if $x > 1$.
As an example, $f_1(x)$ is ($x^{\alpha-1}$) where $0 < \alpha < 1$ and $f_2(x)$ is exp($\lambda$).
How should I do it, assuming I know how to generate Uniform $(0,1)$ ?
Comment: Ok.  So what help would you like from us?
Comment: Does your $f(x)$ integrate to 1?
Comment: @kimchilover yes I can add suitable constants, and then $f(x)$ will integrate to 1.
Comment: @Mathemagical I don't know how to do it. I guess I could use  inverse method. But I don't know exactly how to go about it.
Answer: Hint :
Find the inverse of the CDFs in each range and feed rand() into the inverse function of the CDF.  For example
Let $X$ be $exp(a)$
$F(x) = 1-exp(\frac{x}{a}), x>0\tag{1}$
$ = 0 , otherwise$
Solving $u = F(x)$, you get $x = F_{-1}(u) = -aln(1-u)\tag{2}$
Generate $u$ rvs from $U(0,1)$, then apply the above equation (2) obtain $X$ having the CDF in (1)
In your example:
$F(x) = \int_{0}^{x}f_{1}(x), x<1$  where $f_{1}(x) = x^{\alpha-1}$
$F(x) = \int_{0}^{1}f_{1}(x)+ \int_{1}^{x}f_{2}(x), x>1$ where $f_{2}(x) = \lambda e^{-\lambda x}$
For $x\le 1$
$F(x) = \frac{x^{\alpha}} {\alpha - 1}$
And thus $x = e^{\left[\frac{ln(\alpha-1)}{\alpha} + \frac{ln(u)}{\alpha}\right]}$
For $x\gt 1$
$F(x) = \frac{1}{\alpha-1}+e^{-\lambda}-e^{-\lambda x}$
$u =  \frac{1}{\alpha-1}+e^{-\lambda}-e^{-\lambda x}$
$x = -\frac{1}{\lambda}ln\left(\frac{1}{\alpha-1}+e^{-\lambda} - u\right)$
Generate $u$ rvs from $U(0,1)$, then apply the above equation  obtain $X$ having the above CDF
Answer: A different approach.  Assuming the integral of $f$ is $1$. Let $p=\int_{-\infty}^1 f(x)dx$, flip a biased coin for which the probability of heads is $p$.  If the coin shows heads, repeatedly draw samples $x\sim f_1$ until $x<1$.  If the coin shows tails repeatedly draw samples $x\sim f_2$ until $x>1$.  The final value of $x$ will have the correct distribution.
Why this works: the final value of $x$ will obey $x\le 1$ with probability $p$, and $x>1$ with probability $1-p$, which is certainly correct.  Now suppose $x\sim f, x_1\sim f_1, \text{ and } x_2\sim f_2$.  The distribution of $x$ conditional on $x< 1$ is the same as that of $x_1$ conditional on $x_1< 1$, similarly the distribution of $x$ conditional on $x>1$ is the same as that of $x_2$ conditional on $x_2>1$.
The business of repeated draws from $x\sim f_1$ until $x< 1$ is a way of generating a sample from $f_1$ conditional on $x<1$, and so on.
There might be situations where this is practical, when say the cdfs are too complicated to evaluate or invert numerically.
Comment: Could you also explain how x generated this way has pdf $f(x)$? Thanks in advance.
Comment: I have edited my answer to try to explain why.

Generate random variable with given pdf


How do I set a String to null in java?

Question: The code String name = null; is simply setting my variable name to the string "null" so I can't figure out how to make my program wait for input. The compiler won't allow me to declare the variable without initializing it.
Comment: You should probably show the code that won't compile.
Comment: You could set it as String name = ""; or move your string to the place you are getting the input. If you share your code here it will be easier to help.
Comment: *The code `String name = null;` is simply setting my variable name to the string "null"* it doesn't. It sets it to `null`. Unless you mean it *prints* as the string "null", which is a different thing altogether.
Comment: That's what I mean, when I call the variable it simply prints the string 'null'
Comment: @22niel you set the variable to null, then you print it and it prints null. What were you expecting instead?
Comment: that's why I'm asking how to set it to a null value
Comment: yeah that's why I'm asking how to set it to a null value that doesn't return a string value
Answer: When we write
String s = "Hello World!";
s = null;

The String still exists in memory because this does not delete it. However the garbage collector will clear the object from memory as there is no variable referencing it.
For all practical purposes s = null; deletes the String.
Answer: Before posting here on basic Java Questions, study the Java Tutorials provided by Oracle free of cost.
See the tutorial page on string literals. To quote:

There's also a special null literal that can be used as a value for any reference type. null may be assigned to any variable, except variables of primitive types. There's little you can do with a null value beyond testing for its presence. Therefore, null is often used in programs as a marker to indicate that some object is unavailable.

 So null is not a piece of text with four characters. The keyword null means "no object at all", no String, nothing at all, an empty reference.
To test if an object reference variable is null (contains no reference), you have a few choices:

Objects.isNull( myVar ) and Objects.nonNull( myVar )
null == myVar and null != myVar
myVar == null and myVar != null

I prefer the first.
I recommend you avoid the last. It is much too easy to make the mistake of using a single EQUALS SIGN. The code myVar = null executes, clearing any existing reference from that variable while also returning true. So the compiler will not catch your typo for you. In contrast, the compiler will catch the mistake of null = myVar because null cannot receive an assignment.
Comment: the code: package demo;
import java.util.Scanner;

public class ask {
    public static void main(String[] args) {
        Scanner answer = new Scanner(System.in);
        String name = null;
        System.out.print("What is your name? ");
        while(answer.hasNextLine())
            name = answer.nextLine();
        System.out.println("Hello " + name);
    }
}
How can I make it wait for my input??

How do I set a String to null in java?