MySQL and PHP for attendance

Question: I know that there are quite a few different topics out there but I don't believe they are exactly what I'm looking for.
I need to make a database using MySQL for a club I'm in. I want to create a webpage that the Secretary can log into and take attendance for the meeting. First off I need to know what exactly I would need for creating the table.
I was thinking something like
CREATE TABLE 'Members'('memberName' VARCHAR(40) NOT NULL,
    '11/1/11' INT NOT NULL DEFAULT 0,
    'totalAttendance' INT NOT NULL DEFAULT 0,
     PRIMARY KEY('memberName'), UNIQUE INDEX 'memberName'_UNIQUE('memberName' ASC));

and just keep adding columns for the date using ALTER TABLE 'Members' ADD '11/8/11' INT NOT NULL DEFAULT 0;
I was going to use PHP to create the column and change the default 0 to a 1 if they attended (not exactly sure how I was going to do that but I was thinking some check boxes and if they are checked edit that column for that name?) So something like mysql_query("UPDATE 'Member' SET 'meetingDate'='1' WHERE memberName='nameOfMember'")
or die(mysql_error()); I believe.
What I really need is a way to count across each column to count the total number of times each member has attended and then placing that into the totalAttendance column.
Can someone help me out with that? I'm thinking it is a really easy statement I'm just not asking Google the correct question. Thank you for any help you can give.
Comment: Gaaah... dates as field names? Gag me with a pitchfork!
Comment: [Fundamentals of Relational Database Design](http://www.deeptraining.com/litwin/dbdesign/FundamentalsOfRelationalDatabaseDesign.aspx)
Answer: Please Please Please do not do this.
Make every date a new row.
Make a date column.
So your table would look something like this:
  ID   |    Date      | Attendance |
   1   |  2011-11-01  |      5     |
   2   |  2011-11-02  |      12    |
   3   |  2011-11-03  |      3     |

Then you can have a table of members.
Then you can have a join table of member to meeting. --
    Then when you have this you do not even need an Attendance column in the Meeting table because you can just count how many members are at each meeting with a simple query:
SELECT m.id, m.date, count(*) as `attendance`
FROM meeting m
LEFT JOIN meeting_to_member mm ON mm.meetingid = m.id
LEFT JOIN member mem ON mem.id = mm.memberid
GROUP BY m.id;

Answer: I'd suggest you set up something like this:
id    |  MeetingDate |    MemberName    |    Present
 1    |  2011-11-01  |    John Doe      |       1
 2    |  2011-11-01  |    John Smith    |       0
 3    |  2011-11-01  |    John Jackson  |       1
 4    |  2011-11-02  |    John Doe      |       0
 5    |  2011-11-02  |    John Smith    |       1
 6    |  2011-11-02  |    John Jackson  |       1
 7    |  2011-11-03  |    John Doe      |       1
 8    |  2011-11-03  |    John Smith    |       1
 9    |  2011-11-03  |    John Jackson  |       1

So you'd have a row for each member for each meeting whether they attended or not. My guess is you're not going to have millions and millions of rows with this system, so don't worry about performance.
You can get the total attendance for a member with something like:
SELECT SUM(Present) as total FROM attendance WHERE MemberName = 'John Doe';
// returns -> 2

You can get the total attendance for a meeting with something like:
SELECT SUM(Present) as total FROM attendance WHERE MeetingDate = 2011-11-01;
// returns -> 2

You can get all of the members that missed a specific meeting with something like:
SELECT MemberName FROM attendance WHERE Present = 0 AND MeetingDate = 2011-11-01;
// returns -> John Smith

Comment: +1 Storing a record (with Present = 0) for people who *missed* a meeting might not be necessary, but it is nice. It avoids confusion between missing a meeting and not being in the group at the time of the meeting.
Comment: I think I'm going to try this method because it does what I wanted it to do pretty easily. Thank you very much. If it works I'll set it as the accepted answer but if it doesn't I'll just keep posting.
Comment: Okay so I hit one snag of the Primary Key. If I have it set as the ID then I can't have two occurrences of the same person if they have two different meetings.
Comment: not sure what you mean... the id is unique for **each** person at **each** meeting. You would basically duplicate each person for each meeting in the db. I'll update my answer with more rows in the table.  (although I would pull the members out to a separate table to normalize a bit -- that wouldn't be necessary in this small of an application)
Comment: Sorry didn't mean to post that. I figured that part out. What I need to know now is how to query all the unique names and output them into some html form. I want to make a list of names with a checkboxes next to them. And then a submit button at the end. Once the user presses submit it should create a row of every one of the names including whether they were present or not. Understand?
Comment: @eric, Ooh... This seems to have taken on a different flavor since I voted for it. It's completely [denormalized](http://en.wikipedia.org/wiki/First_normal_form) (bad). You should have a separate table for Members (MemberId, Name, ...) and another for Meetings (MeetingId, Date, ...). The table that records attendance would be MemberMeeting (MemberId, MeetingId, Present). The PK would be both MemberID and MeetingID. To get all members, you just query the Members table!
Comment: @grossvogel - from a purist standpoint, I agree with you. If this application would grow to anything more than an attendance record, then you should definitely normalize the database (I mentioned that above).
Comment: @grossvogel - We're not talking millions and millions of records here, so duplicating members in the db is not going to hurt performance of the system. I agree it's not the "correct" solution according to db admins out there, but the system will get the job done without causing any additional problems.
Comment: @swatkins: IMO, normalization is mostly about a single point of truth and conceptual correctness, not optimizing storage. If a member's name changes, you have to update all of their attendance records. Two meetings cannot happen on the same day, and two members cannot have the same name. If you want to add PhoneNumber for your members or a Topic for your meetings, you have to add it to every attendance record. If you want to create checkboxes to take attendance, the only members available are the ones who have ALREADY attended a meeting. So it's impossible to attend your first meeting!
Answer: You could try using INSERT into... SELECT COUNT...
Look at this guys code:
insert ...select ... count ... on duplicate key update error
You can cut out a lot of his query but it should help.

MySQL and PHP for attendance


Restrict a menu item only for a particular accessible after loggin and optionally for a role

Question: This is the menu I have.
  $items['start'] = array(
    'title' => 'Take A Test',
    'page callback' => 'qe_start',
    'access callback' => TRUE,
    'expanded' => TRUE,
  );

return $items;

What should be passed in this to make it accessible to a role/logged in user?
'access arguments'    => array('content'),

And how can I make this to a callback_menu?
Answer: If you actually just want to check if the user is logged in and don't want to set up permissions, you can set the access callback to 'user_is_logged_in':
$items['start'] = array(
  'title' => 'Take A Test',
  'page callback' => 'qe_start',
  'access callback' => 'user_is_logged_in',
  'expanded' => TRUE,
);

Answer: You would normally define a permission for your module/page using hook_permission() and then assign that permission to a particular role/set of roles in the admin section.
You can then use that permission in your menu callback safe in the knowledge that only the roles you've allowed can access the page:
function qe_permission() {
  return array(
    'take a test' => array(
      'title' => t('Take a test')
    ),
  );
}

function qe_menu() {
  $items['start'] = array(
    'title' => 'Take A Test',
    'page callback' => 'qe_start',
    'access arguments' => array('take a test'),
    'type' => MENU_CALLBACK,
    'expanded' => TRUE,
  );

  return $items;
}

The permissions admin page is at http://mysite.com/admin/people/permissions just in case you haven't already seen it.


Como acrescentar Role em Cadastro de usuário?

Question: Tenho um projeto Asp.Net MVC que usa Individual User Account e eu gostaria de acrescentar uma categoria (Role) pra ele na hora do cadastro.
O que eu tenho no momento é:

View:
@model Projeto.Models.RegisterViewModel
@{
    ViewBag.Title = "Register";
}

@ViewBag.Title.

@using (Html.BeginForm("Register", "Account", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
    @Html.AntiForgeryToken()

Create a new account.

    @Html.ValidationSummary("", new { @class = "text-danger" })

        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })

            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })

        Categorias

            <select name="role" class="form-control">
                <option value=""></option>
                @foreach (var role in ViewBag.allRoles)
                {
                    <option value="@role.Name">@role.Name</option>
                }
            </select>

        @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })

            @Html.PasswordFor(m => m.Password, new { @class = "form-control" })

        @Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })

            @Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })

            <input type="submit" class="btn btn-default" value="Register" />

}

Controller:
public async Task<ActionResult> Register(RegisterViewModel model)
        {

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here");

                    return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

Answer: Bem, a alternativa que vejo é usar o FormCollection.
Ficaria assim o seu controller:
public async Task<ActionResult> Register(RegisterViewModel model, FormCollection frm)
    {

        if (frm["role"] == "")
            ModelState.AddModelError("Role", "Erro na escolha da categoria.");

        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                var currentUser = UserManager.FindByName(user.UserName);

                var roleresult = UserManager.AddToRole(currentUser.Id, frm["role"]);

                await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                // Send an email with this link
                // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here");

                return RedirectToAction("Index", "Home");
            }
            AddErrors(result);
        }

        var context = new ApplicationDbContext();
        var allRoles = context.Roles.ToList();

        ViewBag.allRoles = allRoles;

        // If we got this far, something failed, redisplay form
        return View(model);
    }

Answer: Cara, um tempo atrás eu usei o exemplo a seguir, dá uma olhada se te serve.
http://typecastexception.com/post/2014/02/13/ASPNET-MVC-5-Identity-Extending-and-Modifying-Roles.aspx


How to overwrite a file upload so that the filename is always the same?

Question: I am trying to implement a file upload form in which files are saved. I had written the following code
<?php

/**
 * @file
 * Contains \Drupal\avenue_product_import\Form\avenue_product_importForm.
 */

namespace Drupal\avenue_product_import\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;

class AvenueForm extends FormBase {

  public function getFormId() {
    return 'AvenueForm';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['description'] = [
      '#markup' => '
Use this form to upload a CSV file of Data
',
    ];

    $form['import_csv'] = [
      '#type'              => 'managed_file',
      '#title'             => t('Upload file here'),
      '#upload_location'   => 'public://importcsv/',
      '#default_value'     => '',
      "#upload_validators" => ["file_validate_extensions" => ["csv"]],
      '#states'            => [
        'visible' => [
          ':input[name="File_type"]' => ['value' => t('Upload Your File')],
        ],
      ],
    ];

    $form['actions']['#type'] = 'actions';

    $form['actions']['submit'] = [
      '#type'        => 'submit',
      '#value'       => $this->t('Upload CSV'),
      '#button_type' => 'primary',
    ];

    return $form;
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {

    /* Fetch the array of the file stored temporarily in database */
    $csv_file = $form_state->getValue('import_csv');

    /* Load the object of the file by it's fid */
    $file = File::load($csv_file[0]);

    /* Set the status flag permanent of the file object */
    $file->setPermanent();

    /* Save the file in database */
    $file->save();

    drupal_set_message('File Uploaded Successfully');
  }

}

But here the problem is if the filename in which I am uploading is same. It is getting renamed. But I want to replace old with the new one. How can I achieve this? I had seen file_move() and file_save_upload() function. What changes should I make to replace existing filename with the newly uploaded CSV file?
Comment: There is a thread on Drupal.org regarding this. Somewhere in core, when a file is saved with the file field widget or managed file, `FILE_EXISTS_RENAME` is the default with no way to override it yet. https://www.drupal.org/project/drupal/issues/2648816
Comment: If you do this, be sure to also adjust htaccess/caching rules in regards to Varnish etc if in use... so the browser gets the latest version of the file. We did this for a client for a file field containing PDFs.
Answer: This would save always save the latest upload to public://importcsv/avenue.csv:
public function submitForm(array &$form, FormStateInterface $form_state) {

  /* Fetch the array of the file stored temporarily in database */
  $csv_file = $form_state->getValue('import_csv');

  /* Load the object of the file by it's fid */
  $file = File::load($csv_file[0]);

  $file_real_path = \Drupal::service('file_system')->realpath($csv_file->getFileUri());
  $file_contents = file_get_contents($file_real_path);
  file_unmanaged_save_data($file_contents, 'public://importcsv/avenue.csv', FILE_EXISTS_REPLACE);

  drupal_set_message('File Uploaded Successfully');
}

Comment: In drupal 9, use $file = file_save_data($data, $fileLocation, 'EXISTS_REPLACE');
Answer: In Drupal 9 I was able to save the uploaded file so that the filename is always the same, as follows:
public function submitForm(array &$form, FormStateInterface $form_state) {

  // Loads file from upload form.
  $csv_file = $form_state->getValue('import_csv');
  $file = File::load($csv_file[0]);

  // Gets content from file.
  $file_real_path = \Drupal::service('file_system')->realpath($file->getFileUri());
  $file_contents = file_get_contents($file_real_path);

  // Saves new file with fixed name and replaces any existing file.
  \Drupal::service('file.repository')->writeData($file_contents, 'public://import.csv', FileSystemInterface::EXISTS_REPLACE);

  // Deletes the uploaded file.
  $file->delete();
  $this->messenger()->addStatus($this->t('The file has been uploaded.'));
}

I used Cesar's example as a starting point.
Answer: You could check if a file exists with that name beforehand? If so, remove it and continue saving.
https://api.drupal.org/api/drupal/core%21includes%21file.inc/function/file_delete/8.5.x
or
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!File!FileSystem.php/function/FileSystem%3A%3Aunlink/8.2.x


specify string in title while using groovy template for play 1.2.5

Question: I am using the groovy template for play 1.2.5.  I need to append a string to the title tag for the html page.  However, the option I tried below does not result in the value to be displayed - I am wondering whether the issue is related to escaping or faulty logic on my part (which I'm quite capable of).
<code>#{set title:'ABCD | ${object.property}' /}  // object.property has a valid String value
</code>
Thanks in advance!
Comment: just figured it out - I did not need the ${}.
Answer: While setting the title, the title is already within the "#{}" tag.  If passing another variable, one does not need to use the ${} format - simply ensure that you pass in the variable needed  without the ${} and outside the '' characters.  For instance:
<code>#{set title:'ABCD | ' + object.property /}
</code>
This worked (as long as object.property is valid).  Unless I am mistaken, you can also use the
object?.property format to avoid cases where the object might be null.  Hope it helps!


Como desvincular o diretório local de repositório remoto?

Question: Preciso trocar de máquina e desvincular o diretório local do repositório para usá-lo somente em uma outra máquina.
Como posso fazer essa desvinculação?
Answer: Você pode:

Apagar o diretório da máquina. Assim o computador não terá mais o código fonte e, consequentemente, será impossível fazer alguma alteração.
Remover o remote do repositório local.
git remote rm origin

Como o GIT é um sistema de versionamento distribuído, todos que fizeram um clone do repositório remoto têm um repositório próprio. O que acontece é que as modificações feitas nestes repositórios próprios são refletidas para o repositório central sempre que é feito um push (de forma análoga, o pull faz o processo inverso).
Fazer com que o repositório local não tenha mais um remote impede que sejam feitos pushs e pulls.
Comment: Valew cara, me ajudou bastante ;)


Modern convention for writing raised sixth in figured bass?

Question: In typesetting figured bass for modern readers, what is the preferred way of indicating a raised sixth?
I'm under the impression that the backslash approach is considered archaic, but unsure about whether to prefer natural/sharp symbols vs. a generic + sign.  Basically, I'm trying to find out which convention would be most commonly seen, and thus understood, by people who read this style of notation (and I'm not concerned with preserving the stylistic features of any particular time/place).
Comment: Seeing as figured bass is in and of itself kind of archaic, it seems strange to worry about this too much. I would say that as long as the font size is big enough to see it clearly, I greatly prefer the slash. But, if the preference is for accidentals, I would use whatever is proper for the key (generally standard among composers of the day). However, if raised six is being referred to generically, outside of any particular key, then sharp would be appropriate. A plus wouldn't be confusing, but I haven't seen it in practice with 6. This is only my opinion though, so I won't post as an answer.
Comment: A plus sign is very specific. It means an augmented chord. Really that has nothing to do with a raised sixth.
Comment: @NeilMeyer so the content here https://en.wikipedia.org/wiki/Figured_bass#Accidentals is wrong?
Comment: @NeilMeyer I don't think there's any danger of the + looking like augmented in the context of figured bass, the meaning would be perfectly clear. However, the only interval I've seen plus used with on a regular basis is 4, but then it comes after the the number--like a vertical line through an extended crossbar from the 4. At any rate, I think the real answer here is that there wasn't a standard at the time, and there doesn't really seem to be one now.
Comment: I have disagreed with Wikipedia on several occasions but hey what can I tell a journal that is written by anyone with an internet connection. They clearly got me beat on the credentials.
Comment: What looks like a 4 with a plus sign is really just a four with a slash through the cross bar.
Answer: I've checked three sources.

C.P.E. Bach's Essay on the True Art of Playing Keyboard Instruments. Unfortunately, there does not seem to be a public domain English translation (you might be able to find one in a university library), so I checked the first edition German version on IMSLP (which I can't actually read). As far as I can tell from a quick skim of it, he appears to use the slashed-6 quite frequently. Now, you might argue that this is not modern typography -- which is true. But consider that C.P.E. Bach's work is (1) regarded as essentially the definitive treatise on continuo realization by modern scholars, as well as (2) the fact that he was writing at the very end of the Baroque (it was first published in 1753), so continuo styles had pretty much evolved as far as they were going to. In light of this, I think that following his conventions would be at least a safe choice.
My modern Dover edition of Handel's Messiah. This used a 6♮ to raise what would otherwise be a flatted note (e.g. B♭ in the key of F). However, in sharp movements (e.g. in D), the slashed-6 was used to raise what would otherwise be natural.
My modern Dover edition of Bach's Brandenburg Concertos & Orchestral Suites, which only has figures on the 5th concerto (in D, for the non-solo sections) and the 1st two Orchestral Suites (in C and D). These consistently use a slashed-6 to indicate sharped sixths. However, I did find one instance of a 6♮ in the 1st Orchestral Suite. This was an interesting case, because it was actually a cautionary accidental. The key signature was C, but the previous measure had a B♭ in the bass part, so the measure in question had a D with a cautionary natural next to the six. At any rate, this matches the use of 6♮ in Handel to raise a flatted note.

In no case did I ever see either a 6+ or a 6♯, so from this limited sample (two modern Dover scores), it appears slashed-6 is the preferred way to go... at least if you have a way to support the symbol in your software.
Answer: The raised sixth in figured bass is indicated today by a six with a slash through it.
If you're editing old music for modern readers, you should consult as many recent, authoritative editions as you can to make sure you are following modern conventions. The slashed sixth is the practice of the Neue Bach Ausgabe (new Bach edition) published by Baerenreiter. The same convention is followed in the widely used music-theory textbook, Aldwell and Schachter's Harmony and Voice Leading.
Older editions, such as the 19th-century editions of Bach reprinted by Dover, and the original manuscripts and prints, will generally not be as consistent.


python re.search not working on multiline string

Question: I have this file loaded in string:
// some preceding stuff
static char header_data[] = {
    1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,
    1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,
    1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,1,
    1,0,1,1,1,0,0,1,1,0,0,1,1,1,0,1,
    0,0,0,1,1,1,1,1,1,1,1,1,1,0,1,1,
    1,0,0,0,1,1,0,1,1,1,1,1,0,1,1,1,
    0,1,0,0,0,1,0,0,1,1,1,1,0,0,0,0,
    0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,
    0,1,1,1,0,0,0,0,0,0,1,1,0,1,1,0,
    0,0,0,0,1,0,0,0,1,0,0,1,0,1,0,0,
    1,1,1,0,1,1,0,0,1,1,0,0,0,1,1,1,
    1,1,0,1,1,1,1,1,1,1,1,0,0,0,1,1,
    1,0,1,1,1,0,0,1,1,0,0,0,0,0,1,1,
    1,1,0,1,0,1,0,1,1,1,1,0,0,0,0,1,
    1,1,1,0,1,1,0,1,1,0,1,1,1,1,0,1,
    1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1
    };

I want to get only the block with ones and zeros, and then somehow process it.
I imported re, and tried:
In [11]: re.search('static char header_data(.*);', src, flags=re.M)

In [12]: re.findall('static char header_data(.*);', src, flags=re.M)
Out[12]: []

Why doesn't it match anything? How to fix this? (It's python3)
Comment: Think about the non-greedy match in my answer. Otherwise you will match more than one parethis section. But it depends how your source file will look like.
Comment: @wenzul Actually it's the last brace in the file, but I'll add that, just to be on the safe side.
Comment: @wenzul I rejected your edit, because it's not improving it at all. When I encountered the problem, I googled almost exactly what I now have in the title - this way people will easily find it.
Answer: You need to use the re.S flag, not  re.M.

re.M (re.MULTILINE) controls the behavior of ^ and $ (whether they match at the start/end of the entire string or of each line).
re.S (re.DOTALL) controls the behavior of the . and is the option you need when you want to allow the dot to match newlines.

See also the documentation.
Comment: Thanks, works great now. The "MULTILINE" name is a bit confusing
Answer:
and then somehow process it.

Here we go to get a useable list out of the file:
import re
match = re.search(r"static char header_data\[\] = {(.*?)};", src, re.DOTALL)
if match:
    header_data = "".join(match.group(1).split()).split(',')
    print header_data

.*? is a non-greedy match so you really will get just the value between this set of braces.
A more expicit way without DOTALL or MULTILINE would be
match = re.search(r"static char header_data\[\] = {([01,\s\r\n]*?)};", src)

Answer: If the format of the file does not change, you might as well not resort to re but use slices. Something on these lines could be useful
>>> file_in_string
'\n// some preceding stuff\nstatic char header_data[] = {\n    1,1,1,1,1,1,0,0,0
,0,1,1,1,1,1,1,\n    1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,\n    1,1,0,1,0,1,0,1,1,0,1
,0,1,0,1,1,\n    1,0,1,1,1,0,0,1,1,0,0,1,1,1,0,1,\n    0,0,0,1,1,1,1,1,1,1,1,1,1
,0,1,1,\n    1,0,0,0,1,1,0,1,1,1,1,1,0,1,1,1,\n    0,1,0,0,0,1,0,0,1,1,1,1,0,0,0
,0,\n    0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,\n    0,1,1,1,0,0,0,0,0,0,1,1,0,1,1,0,\
n    0,0,0,0,1,0,0,0,1,0,0,1,0,1,0,0,\n    1,1,1,0,1,1,0,0,1,1,0,0,0,1,1,1,\n
 1,1,0,1,1,1,1,1,1,1,1,0,0,0,1,1,\n    1,0,1,1,1,0,0,1,1,0,0,0,0,0,1,1,\n    1,1
,0,1,0,1,0,1,1,1,1,0,0,0,0,1,\n    1,1,1,0,1,1,0,1,1,0,1,1,1,1,0,1,\n    1,1,1,1
,1,1,0,0,0,0,1,1,1,1,1,1\n    };\n'
>>> lines = file_in_string.split()
>>> lines[9:-1]
['1,1,1,1,1,1,0,0,0,0,1,1,1,1,1,1,', '1,1,1,0,1,1,0,1,1,0,1,1,0,1,1,1,', '1,1,0,
1,0,1,0,1,1,0,1,0,1,0,1,1,', '1,0,1,1,1,0,0,1,1,0,0,1,1,1,0,1,', '0,0,0,1,1,1,1,
1,1,1,1,1,1,0,1,1,', '1,0,0,0,1,1,0,1,1,1,1,1,0,1,1,1,', '0,1,0,0,0,1,0,0,1,1,1,
1,0,0,0,0,', '0,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,', '0,1,1,1,0,0,0,0,0,0,1,1,0,1,1,
0,', '0,0,0,0,1,0,0,0,1,0,0,1,0,1,0,0,', '1,1,1,0,1,1,0,0,1,1,0,0,0,1,1,1,', '1,
1,0,1,1,1,1,1,1,1,1,0,0,0,1,1,', '1,0,1,1,1,0,0,1,1,0,0,0,0,0,1,1,', '1,1,0,1,0,
1,0,1,1,1,1,0,0,0,0,1,', '1,1,1,0,1,1,0,1,1,0,1,1,1,1,0,1,', '1,1,1,1,1,1,0,0,0,
0,1,1,1,1,1,1']


Generating random variates from the following pdf

Question: I'm working through some example of probability distributions and I'm struggling to derive the formula for the following pdf
$f(x) = \frac{1}{0.02}e^{-\left\lvert x \right\rvert/0.01}$
My undersanding is that I would need to integrate the pdf between 0 and x to derive the cdf and then invert the cdf to get the formula I am after. I am having trouble doing the integration to get the cdf as I have never come across a pdf with an absolute value in it.
Answer: This is laplace distribution, you can either use the CDF given in the wikipedia page or find a proof. If you want to integrate it yourself, the integral will be from $-\infty$ (not $0$) to $x$, and for the negative portion, you'll just substitute $|x|=-x$ and do the integration.
Answer: For actually generating $n$ such values, here are three approaches in R turning uniform pseudo-random variables into your distribution.  The first is a simplified inversion of the CDF, putting its two parts together; the second essentially takes an exponential distribution and applies a random sign; and the third is essentially the difference between two exponential distributions
<code>n <- 1000
rate <- 0.01

U1 <- runif(n)
U2 <- runif(n)

Xa <- (1/rate) * log(abs(2*U1-1)) * (2*U1-1) / abs(2*U1-1)

Xb <- (1/rate) * log(U1) * (2*(U2 > 1/2) - 1)

Xc <- (1/rate) * log(U1) - (1/rate) * log(U2)
</code>
Comment: +1. There are plenty of other methods available in `R`, ranging from using normal variates (`x <- rnorm(n);
y <- rnorm(n);
X <- sign(x) * (x^2+y^2) * rate/2`) to Poisson processes (`X <- rate * n * diff(sort(U1)) * sign(diff(U1))`) to direct use of `rgamma` or `rchisq`.


Why can't I use projectname.file to import

Question: I add a custom_site.py in my project, But I can't import it in admin.py and urls.py. I try to use sys, and use Pycharm to mark them as Sources Root, but it still can't import! It always Report an error like:
from typeidea.custom_site import custom_site
ModuleNotFoundError: No module named 'typeidea.custom_site'

Why typeidea.custom_site can't import?
My project directory and my code are here:
admin.py
from ..typeidea.custom_site import custom_site
...
@admin.register(Category, site=custom_site)

urls.py
import typeidea.custom_site

urlpatterns = [
    url(r'^super_admin/', admin.site.urls),
    url(r'^admin/', typeidea.custom_site.custom_site.urls),
]

Project:
typeidea
-blog
——admin.py
...
--comment
...
--config
...
--typeidea
——init.py
——urls.py
...
custom_site.py
Comment: Don't post pictures of code.  Cut-and-paste the code.  Your inner files do not have any idea what the outer layers look like.  You might be able to do `from ..typeidea.custom_site import custom_site` (note the double dots).
Comment: What do you mean by "I try to use sys"?  You COULD do what you want by adding the root path to `sys.path`.  Did you try that?
Comment: @TimRoberts I have try to use sys.path
Comment: @TimRoberts My file custom_site.py is in the parent directory, I can't use so.
Comment: @TimRoberts Please told me that how to use sys.path.I don't know if I am using it correctly.
Comment: You can say `sys.path.append( os.path.abspath( os.path.dirname( __file__ ) + "/.." ) )` to add the current file's parent to the path.  Make it `"/../.."` to add the current file's grandparent.
Comment: @TimRoberts I find that I can use `sys.path.append("..") ` to import too! Thank you so much!
Comment: OK, but REMEMBER that `sys.path.append("..")` appends the parent of the CURRENT directory.  If you run the script from somewhere else (`python ../../admin.py`), that won't match the script location.
Answer: when you write:
from X import y
it imports the X module, and create reference to the y objects/methods. yours:
from typeidea.custom_site import custom_site
the typeidea.custom_site is not a module, so you cant import it.
the module that you want to use is custom_site, so just do:
import custom_site.
for more detailed explanation about it:
`from ... import` vs `import .`


Is completeness of the real line needed to show that if $\lim\limits_{x\to0} f'(x)$ exists and $f$ is continuous at $0$ then $f'(0)$ is the limit?

Question: A question about existence of derivative of function at Zero
The question linked above inspires another question.

$f:\mathbb R\to\mathbb R$ is assumed to be continuous everywhere.
It is assumed to be differentiable at all points besides $0.$
It is assumed that $\displaystyle\lim_{x\to0} f'(x)$ exists in $\mathbb R.$

The question was whether $f'(0)$ exists and is equal to that limit. A posted affirmative answer used L'Hopital's rule, and another used the mean value theorem directly. Either of those relies on the gaplessness of the real line.
Can an affirmative answer be proved without completeness?  Could it be proved, for example, in the field of rational numbers? If not, what would be a counterexample in $\mathbb Q$?
Comment: I don't think so, although giving a formal proof that it can't be proved without using completeness will be difficult. Any serious / non-trivial theorem of calculus/analysis is based on completeness and without completeness we are just dealing with rationals via algebraic manipulation. Also see https://math.stackexchange.com/a/1787254/72031
Comment: See related https://math.stackexchange.com/q/1908005/72031
Comment: Are you interested in cases where the domain is not complete, the range is not complete, or both?
Comment: @MichaelLee : Perhaps what I'm interested in is whether the proposition can be proved in $\mathbb R$ without using the existence of a least upper bound of every non-empty bounded sets.
Answer: Completeness in the image is not an issue, but something of this type is needed in the domain, or, alternatively a condition on absolute continuity.
As an example take for $f$ the Cantor function (defined first on $[0,1]$, then periodically continued to a continuous function on ${\Bbb R}$). It has derivative 0 a.e. but has infinite derivative at 0.
Put in another way, let $C$ be the periodically continued Cantor set in ${\Bbb R}$ and set $U={\Bbb R}\setminus C$ which is open, dense and of full Lebesgue measure. Then, $f:{\Bbb R} \rightarrow {\Bbb R}$ is continuous, $f$ restricted to $K=U\cup\{0\}$ has derivative zero everywhere except at zero where it is infinity. Restricting to  the set $K\cap {\Bbb Q}$ which is dense in ${\Bbb Q}$ you (almost) get a counterexample as asked for in the last question.
A more constructive result: If $f$ is absolutely continuous, then it is the integral of its derivative (see e.g. Royden, Real Analysis, Chap 5 sect 4) so the mean value theorem holds [whence the wanted result].
In particular if $f$ is locally Lipschitz continuous (which implies abs cont) and $|f'|\leq M$ a.e. then $f$ is in fact $M$-Lipschitz.
Comment: The Cantor function is not differentiable everywhere except for at $0$.
Comment: Right. So I should rather have placed it as a comment to the hypotheses you made in your answer.
Comment: Very nice counterexample. There was a mistake in assuming the Lipschitz condition that I did, but if absolute continuity is given, then the theorem I tried to prove is essentially trivial.
Comment: yes, I completed with some remarks regarding abs cont.


Calculating integral value of Fourier series

Question: Given fourier series:
$$\mathrm{S}\left(x\right) =
{3 \over \pi}\sum_{n = 0}^{\infty}
{\sin\left(\left[2n + 1\right]x\right) \over 2n + 1}\,,\qquad
\left\langle -\pi,\pi\right\rangle
$$
Evaluate: $\displaystyle{%
\int_{-\pi}^{\pi}\mathrm{f}\left(x\right)\,\mathrm{d}x}$.

I suppose that Parseval's identity should be used somehow to calculate this.
I only know how to calculate sum using Parseval, don't know how to apply it to find value of an integral. Any help would be welcomed.
Answer: Why Parseval? It is way easier than that: the integral of an odd integrable function over a symmetric interval with respect to the origin is just zero.
Comment: but what would happen if I had to evaluate, for example $\int_{-\pi}^{\pi}f(x)sin(3x)dx$? Would I have to use Parseval then? And how would I use it?
Comment: What do you know about the integrals $\int_{-\pi}^{\pi}\sin(nx)\sin(mx)\,dx$ ? Try to figure the answer by yourself, it is not difficult.
Comment: Is  f(x) supposed to equal S(x), otherwise the question makes no sense


Who keeps the record of each international cricket match?

Question: There are lot of websites (like ESPN or Yahoo) that mention the statistics and records of all the international / domestic cricket matches. But, is there any international reputed agency that keeps track of all the international cricket matches?
Answer: The local governing body will determine what status particular games are - First Class, List A, Test, ODI, T20 International and whatever.
As a consequence internationals of whatever form are ratified by the board - or boards - involved. The scorers for a game, normally appointed by the teams (law 4), will have to submit their (agreed) cards to that governing body.
The national boards will keep their own record, but there's no 'official' body for results and statistics.
The ICC lay down a lot of rules and guidelines for many aspects of the games, but I can't find anything mandating that particular feeds should be available to third parties. The ICC haven't yet tried to copyright the scores (as some American sports leagues have tried to).
For example, Wisden is explicitly (#20) unofficial!


Given $a+b+c=0$ find the value of $\big(\frac{b-c}{a}+\frac{c-a}{b}+\frac{a-b}{c}\big)\big(\frac{a}{b-c}+\frac{b}{c-a}+\frac{c}{a-b}\big)$

Question: I already have a solution, which is correct, it is $9$. I'm just wondering if there is a simpler method.
First we just expand and find
$3+\frac{b-c}{a}\big(\frac{b}{c-a}+\frac{c}{a-b}\big)+\frac{c-a}{b}\big(\frac{a}{b-c}+\frac{c}{a-b}\big)+\frac{a-b}{c}\big(\frac{a}{b-c}+\frac{b}{c-a}\big)$
Each of these "not yet a number" terms can be expanded to give
$\frac{b-c}{a}\big(\frac{b}{c-a}+\frac{c}{a-b}\big) = \frac{2(c-b)^2}{(c-a)(a-b)}$
$\frac{c-a}{b}\big(\frac{a}{b-c}\big)= \frac{2(a-c)^2}{(c-a)(a-b)}$
$\frac{a-b}{c}\big(\frac{a}{b-c}+\frac{b}{c-a}\big) = \frac{2(b-a)^2}{(b-c)(c-a)}$
Adding each of these terms together and factoring out the two we find that they equal
$2\frac{3(a-b)(b-c)(c-a)}{(a-b)(b-c)(c-a)}=6$
So our total is $9$.
This is problem 160 of The USSR olympiad problem book by Shklarsky, Chentzov and Yaglom, they give the answer and $9$ is correct. Obviously they don't give the method.
Answer: plugging $$c=-a-b$$ in the given Expression we get two factors
$$\frac{b+a+b}{a}+\frac{-2a-b}{b}+\frac{a-b}{-a-b}=-\frac{(a-b) (2 a+b) (a+2 b)}{a b (a+b)}$$
and the other factor $$\frac{a}{b+a+b}+\frac{b}{-2a-b}+\frac{-a-b}{a-b}=-\frac{9 a b (a+b)}{(a-b) (2 a+b) (a+2 b)}$$
putting Things together we obtain $$9$$ if the denominators are $\ne 0$
Answer: $$\sum_{cyc}\frac{b-c}{a}=\frac{\sum\limits_{cyc}(a^2b-a^2c)}{abc}=\frac{(a-b)(a-c)(b-c)}{abc}$$ and
$$\sum_{cyc}\frac{a}{b-c}=\frac{\sum\limits_{cyc}a(a-b)(c-a)}{(a-b)(b-c)(c-a)}=$$
$$=\frac{\sum\limits_{cyc}(a^2c-a^3-abc+a^2b)}{-(a-b)(a-c)(b-c)}=\frac{\sum\limits_{cyc}(a^2c+a^2b+abc-a^3+abc-3abc)}{-(a-b)(a-c)(b-c)}=$$
$$=\frac{9abc}{(a-b)(a-c)(b-c)},$$
which gives the answer: $9$.
Comment: Dear Michael i think it is $9$
Comment: Yes, I also got this number.
Comment: fine then it is all ok
Answer: Observe that $(a+b+c)((a-b)^2+(b-c)^2+(c-a)^2)=a^3+b^3+c^3-3abc$ so $a^3+b^3+c^3=3abc$. Now
\begin{eqnarray*}
&&\left( \frac{b-c}{a}+\frac{c-a}{b}+\frac{a-b}{c}  \right) \left( \frac{a}{b-c}+\frac{b}{c-a}+\frac{c}{a-b}  \right)\\
&=&3+\frac{a}{b-c}\left(\frac{c-a}{b}+\frac{a-b}{c}\right)+
\frac{b}{c-a}\left(\frac{a-b}{c}+\frac{b-c}{a}\right)+
\frac{c}{a-b}\left(\frac{b-c}{a}+\frac{c-a}{b}\right)\\
&=&3+\frac{a}{b-c}\frac{c^2-ac+bc-b^2}{cb}+.+.\\
&=&3+\frac{a}{b-c}\frac{(a-b-c)(b-c)}{cb}+.+.\\
&=&3+\frac{2(a^3+b^3+c^3)}{abc}=\color{red}{9}.
\end{eqnarray*}
Comment: Good point, I didn't want to use the result $a^3+b^3+c^3=3abc$ because that is problem 161, but nice spot.
Answer: It is an olympiad question. Otherwise picking suitable numbers will do:
$$a=1,b=2,c=-3$$
$$S=\left(5-2+\frac13 \right)\left(\frac15 -\frac12+3\right)=\frac{10}{3}\cdot \frac{27}{10}=9.$$
Comment: That's actually a good point, next time I'll plug in some numbers and see what they give, then try to prove that's always the case. Not sure how I didn't see that initially.


What is the difference between driver.findElement() and driver.findElements()?

Question: In driver.findElements() we get another function for finding size() which is not available in driver.findElement().
Is it the only difference?
Comment: The answer is in the documentation: http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/WebDriver.html#findElements(org.openqa.selenium.By) .... `Returns: A list of all WebElements, or an empty list if nothing matches` ...think about it logically: you can check the `size` of a collection but how can you check the `size` of a single item?
Answer: driver.findElement() returns first matching element with specified locator and driver.findElements() returns all(array of) matching elements with specified locator.
Answer: findElement throws NoSuchElementException if no matching elements are found while findElemetns don't throw any such exception.
findElement will return first matched element and wont look further match while findElemets() will look for all possible matching elements. This do have impact on performance
Answer:
findElement is used to find the first matching element with the specified locator in the HTML page.
findElement's return type is WebElement.
findElement will throw an error if the specified locator is not find in the loaded web page.


Puzzled over terminology "Indefinite Integral" in Royden

Question: In Royden (pg 125), a function of the form $$f(x)=f(a)+\int_a^x f'$$ is called an "indefinite integral" of $f'$ over $[a,b]$.
However, I don't see how it is "indefinite", I thought indefinite means it has no limits, something like $\int g$?
Thanks for any explanation.
Comment: $x$ varies within $[a,b]$; hence, the term indefinite.
Answer: Indefinite doesn't really mean an integral without limits. It is an integral without "definite" limits, that is, without real numbers as the limits. Instead, one or both of the limits are variables.
Answer: Isn't $f(a)$ an arbitrary constant because you can pick any $f(a)$? For each $f(a)$ you get a different antiderivative of $f'$.
You might write the indefinite integral as $\int f'(x)dx=f(x)+C$. But this is just the same as $f(x)+f(a)$ since we can take $f(a)$ to be any constant.
Comment: Thanks. But in the book, the interval $[a,b]$ is a given fixed interval.
Comment: I think the question is about the limits on the integral, not on the arbitrary constant attached to it.


Range with variable in row and columns

Question: I want to check text in a range of cells, Columns A to L and Rows 2 to 10, I want to use a for to go through that range, but I get an error
For x = 1 To 12 Step 1
 For i = 2 To 10 Step 1
    If Range(x, i).Value = "#REF!" Then
     Range(x, i) = "0"
    End If
   Next
 Next

Comment: To replace any error value (not just `#REF!`), you could do `Range("A2:L10").SpecialCells(xlCellTypeFormulas, xlErrors).Value = "0"`. Also, you could use this to loop through fewer cells to find the `#REF!` values: `Set srg = Range("A2:L10").SpecialCells(xlCellTypeFormulas, xlErrors)` and continue with a loop `For Each sCell in srg.Cells`... and apply BigBen's solution.
Answer: Multiple issues:

Use Cells instead of Range.
Test if the cell contains a #REF! error

For x = 1 To 12 Step 1
    For i = 2 To 10 Step 1
        If IsError(Cells(x, i).Value) Then
            If Cells(x, i).Value = CVErr(xlErrRef) Then
                Cells(x, i).Value = 0 '<--- no quotes, write a number not a string
            End If
        End If
    Next
Next

Answer: No need to use numerical loop, you can use structure like this, if you want to detect all #REF errors and replace them with zero.
Sub test()
    Dim itm As Variant
    For Each itm In Range("A2:L10")
        If IsError(itm.Value) Then
            If itm.Value = CVErr(xlErrName) Then itm.Value = 0
        End If
    Next itm
End Sub

Comment: Although this is working, you should remove possible confusion by rather using something like `Dim Cell As Range`˙, `For Each Cell In Range("A2:L10").Cells` and replace the remaining occurrences of `itm` with `Cell`. A rule of thumb would be: *use `Variant` if no other way*. Re-read MS Docs' [For Each...Next statement](https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/for-eachnext-statement) (the *element* part).

Range with variable in row and columns


Modeling within-person design (items-within-person), need to account for item?

Question: Apologies if this is an elementary question - within-person designs are not my forte, and I am more used to designs in the quasiexperimental econometric tradition.
I have two designs that are the same in terms of the question I am asking here, where (to use a proxy case) I'm basically asking people if they like 10 different types of fruit, where I'm randomizing things (in two different data collections) across the fruits to see if there's an effect of treatment on how they feel about the fruit:

Design #1: Half of the fruits, within-person, are randomly assigned to include the modified "in a pie," to see whether specifying we mean that it's a fruit pie matters for people's opinions.
Design #2: After the fifth fruit, they get an article talking about the harms of the fruit industry, to see whether they feel differently about fruit as a result of that knowledge.

And yes, the order of the fruits as presented on the survey is randomized across participants.
I understand (I think) that my basic modeling approach would be a mixed effect design, predicting the outcome (the person's response to the item) from the treatment. Random intercept and slope model shown below just for sake of argument.
$$
FruitFeeling = \beta_{0j}+\beta_{1j}InPie_{ij}+\varepsilon_{ij}
$$
$$
\beta_{0j} = \gamma_{00}+u_{0j}
$$
$$
\beta_{1j} = \gamma_{10}+u_{1j}
$$
However, the distribution of the outcome varies across the fruits substantially, in such a way that my econometric impulse is to include a fixed effect for the type of fruit, as below.
$$
FruitFeeling = \beta_{0j}+\beta_{1j}InPie_{ij}+\boldsymbol{\beta_2FruitType_{ij}}+\varepsilon_{ij}
$$
where $\beta_2$ is a vector of dummy variables for the different fruit types.
However, I don't think that makes sense, and there's a bug in my head telling me that this would be somehow collapsing the variance the model is working with to a point of somewhat negating the point of the within-person model (though I could be wrong). I do expect the effect of treatment varies across fruits, but I was planning on estimating this via separate one-level models for each fruit. Unfortunately, even if I am right, I don't have the expertise or language to be able to fully articulate this thought.
TL;DR: When you have a within-person design where the repeated measures do exhibit different distributions in the outcome variable, should you be accounting for the differences in the repeated measures with some kind of measure-specific fixed effect?
Answer: Short answer: Yes.
Longer answer: Maybe I am missing something but it seems to me you have several variables:

Fruit
In a pie vs. not
The interaction (you didn't mention this, but I'm guessing it's important).

(and then, of course, person effects)
The interaction effect is almost certainly present. E.g. apples are good plain or in a pie; rhubarb is probably preferred in pies; bananas are probably preferred not in pies (yeah, I know there are pies with bananas).
You mention doing a separate analysis for each fruit. That's certainly possible, and would eliminate the possibility (much less the benefit) of having fruit and the interaction as variables. But it means you have a bunch of models instead of one, and it means you can't measure the effect size or p value for fruit type and the interaction.
I don't understand what this has to do with the person effect or what you mean by "collapsing the variance".
Comment: Thank you for your response, and I appreciate your patience with my imprecision. And yes, the interaction is important, but I was somewhat worried about the cleanliness of the interpretation and the model having now 18 terms in it (9 for the mean effects of the fruits, 9 for each interaction). (explaining what I meant by "collapsing the variance" in next comment)
Frankly, if there was a high quality published analysis with the same kind of design, I'd be open to reading it - the problem is that my Googling mostly comes up with repeated measures stuff, which isn't exactly like this.
Comment: By "collapsing the variance" - I should say that I also have been thinking about this all day and it's occurred to me that this isn't any different than what I'd be doing in a panel analysis. But still, there's something that feels odd to me about it, and at least in the brief reads of some repeated measures stuff I'm finding, it doesn't seem to me that they're accounting for the observation #, which would feel like it fits the same logic if you think the distribution of the outcome just naturally changes over time.
Comment: Plus, my causal logic makes me unsure of the point given the fruit effect isn't associated with the treatment assignment, beyond perhaps increasing the precision of estimates (which I'm not discounting as a reason, but I am searching for a more substantive reason). This all said, I'm very open to someone telling me "Your instinct to include the fixed effects is correct and you are just having anxiety."


How to clear CSS and Javascript with a bookmark?

Question: I would like to clear the CSS and javascript in D7 with the help of a bookmark.
Currently this is:
<domein>/admin_menu/flush-cache/assets?token=<tokenid>

This is working however the tokenid is changing all the time. It there a easy solution for this so I don't have to change the tokenid in the bookmark every time?
Comment: The token isn't valid for long enough to make it worthwhile, plus it requires a valid cookie for an authenticated user to either use or generate a new one. Just use what admin_menu gives you, a cache clear button in a couple of hovers. You can always preprocess the menu item and add an accesskey attribute if you want a shortcut to it via the keyboard too
Answer: In a custom module add a menu item (Using hook_menu) to function with this code:
drupal_clear_css_cache();
drupal_clear_js_cache();

Then you can just bookmark your custom page url. Or set a cron job to call that page at regular intervals if thats what you're after


From square coordinates to CSS matrix3d()

Question: I'm stuck on a problem :
Let's consider a square on a surface that is moving (in a video). So remember it's not always a plane surface, it can be skewed, rotated, etc.
Right now, I'm detecting it with Aruco JS and getting the coordinates (x,y) of its 4 corners. I'm pretty sure that, starting with this coordinates, I can render the transformation using transform: matrix3d();.
The thing is : I have about 0 knowledge in maths stuff and especially trigonometry. And I really would learn / understand that.
To sum up : With the coordinates of the corners of a square in real time, how can I apply the transformation to another element using CSS transform property ?
Here's some things I've done so far (this code is executed inside a window.requestAnimationFrame) :
  console.log('rotation : ', rotation[0], rotation[1], rotation[2]);
  console.log('translation : ', translation);
  var dimensions = {
    width: lineDistance(corners[0], corners[1]),
    height: lineDistance(corners[0], corners[3])
  }, center = {
    x: corners[0].x + (corners[1].x - corners[0].x)/2,
    y: corners[0].y + (corners[3].y - corners[0].y)/2
  }, rotateAngle = angle(corners[0].x, corners[0].y, corners[1].x, corners[1].y),
    rotateXangle = parseInt(rotation[1][2]);

    img.style.top = corners[0].y;
    img.style.left = corners[0].x;

  img.style.width = dimensions.width;
  img.style.height = dimensions.height;
  img.style.transform = 'rotate('+rotateAngle+'deg) rotateX('+Math.asin(-rotateXangle)+'deg) rotateY('+-Math.atan2(rotation[0][2], rotation[2][2])+'deg) rotateZ('+Math.atan2(rotation[1][0], rotation[1][1])+'deg)';

Answer: The core of this question has been asked on Math Stack Exchange, titled Finding the Transform matrix from 4 projected points (with Javascript). My answer there should serve your needs as well. Plus the better math type setting there will make things easier to read.
Comment: This definitly answer the question. Thank you really much :)


What post-hoc analysis should I use following a two-way ANCOVA with factor, integer and numeric variables?

Question: I ran a two-way ANCOVA on the data below, in r-studio, to determine whether pupal weights Pupal.wt were affected by caterpillars' diet Trt, initial weights Initial.wt, and the amount of time it took each caterpillar to reach pupation Larval.period: aov(Pupal.wt ~ Trt*Initial.wt*Larval.period, data = GM).
Is a Tukey HSD test sufficient to follow this analysis?
If so, how can I change either the code or my data to make TukeyHSD() work (I get this error message when I try to run it:
ANCOVA <-aov(Pupal.wt ~ Trt*Initial.wt*Larval.period, data=GM)
TukeyHSD(ANCOVA)

Error in rep.int(n, length(means)) : unimplemented type 'NULL' in 'rep3'
In addition: Warning messages: 1: In replications(paste("~", xx), data = mf) :
non-factors ignored: Initial.wt 2: In replications(paste("~", xx), data = mf) :
non-factors ignored: Larval.period 3: In replications(paste("~", xx), data = mf) :
non-factors ignored: Trt, Initial.wt 4: In replications(paste("~", xx), data = mf) :
non-factors ignored: Trt, Larval.period 5: In replications(paste("~", xx), data = mf) :
non-factors ignored: Initial.wt, Larval.period 6: In replications(paste("~",xx), data=mf):
non-factors ignored: Trt, Initial.wt, Larval.period

Here are my data:
> str(GM)
'data.frame':   44 obs. of  5 variables:
 $ Trt           : Factor w/ 2 levels "Infested","Uninfested": 2 2 2 2 2 2 2 2 2 2 ...
 $ Larval.period : int  14 20 18 18 26 14 22 26 20 18 ...
 $ Initial.wt    : num  0.181 0.318 0.218 0.195 0.25 0.181 0.213 0.255 0.236 0.298 ...
 $ Pupal.wt      : num  0.294 0.519 0.527 0.326 0.511 0.299 0.584 0.442 0.481 0.452 ...
 $ X..weight.gain: num  62.4 63.2 141.7 67.2 104.4 ...

Comment: Are you primarily interested in the effect of Treatment on Pupal Weight? Do you believe this effect might be different depending on Initial Weight and Larval Period? You need to clarify your research question first.
Comment: It seems your terminology is off - a two-way Ancova would imply that your model includes two factors (one of whom is Treatment), presumed to be of primary interest, and one or more numerical variables you need to control for and which are of secondary interest in addressing your research question(s). However, as @gung pointed out, you only have one factor in your model, so at best you would have a one-way Ancova. In any event, for an Ancova analysis, you would want to use the lm() function.
Answer: This seems to be due to a confusion on your part about the necessity (and even applicability) of post-hoc tests.  When you have a categorical variable with multiple levels (e.g., several subspecies) and you get a test of that variable, you are testing that variable as a whole.  Assuming the test is significant, it implies that there is some difference somewhere amongst the various levels, but you don't know where.  The post-hoc test follows the initial test of the variable to help you identify what levels might differ from each other.
In your case, you have only one categorical variable (factor), so the others are appropriately ignored.  In addition, your only factor (treatment) has only two levels ("infested", and "uninfested").  So if there is reason to believe that there are differences amongst the levels somewhere, and there are only two, logically, the difference has to be between those two.  In other words, there is nothing here to test with Tukey's test.
You have a three dimensional space comprised of pupal weight, initial weight and larval period.  Your model is fitting a plane, that is allowed to twist as necessary, that picks out the mean pupal weight as a function of the initial weight and the larval period.  With two levels of treatment, the model fits two such planes, one each for infested and uninfested.  If the three way interaction is significant, it means that these planes are tilted and/or twisted differently from each other, as well as possibly shifted up or down.  To understand your results more fully, you should try to make some plots, or at least solve the regression equation for different points in the covariate space for each treatment level.  However, there is no need to try to use Tukey's test to determine which set of two levels are the two that differ out of the (only two) levels that you have.


Question about Angle-Preserving Operators

Question: This an exercise out of Spivak's "Calculus on Manifolds".
Edit:  There was a typo in the exercise as is noted below in the answers.  The statement has been edited to reflect this.
Given $x,y\in\mathbb{R}^{n}$, the angle between $x$ and $y$ is defined by
$$\angle(x,y) = \arccos\left(\frac{\langle x,y \rangle}{|x|\cdot |y|}\right),$$ where $\langle x,y \rangle$ denotes the standard Euclidean inner product.
A linear operator $T:\mathbb{R}^{n}\to\mathbb{R}^{n}$ is said to be angle-preserving if
$\angle(T(x),T(y)) = \angle(x,y)$ for every $x,y\in\mathbb{R}^{n}$.
The exercise as stated:
Let $\{x_{1},\dots, x_{n}\}$ be a basis for $\mathbb{R}^{n}$.  Then suppose that $\lambda_{1}, \dots, \lambda_{n}\in \mathbb{R}$ are such that $Tx_{j} = \lambda_{j}x_{j}$ for each $j = 1,\dots, n$.
Then $T$ is angle-preserving only if (not if and only if!)$|\lambda_{i}| = |\lambda_{j}|$ for every $1\leq i\leq j\leq n$.
I'm having problems with the $(\Rightarrow)$ direction.
My best attempt (which seems to lead nowhere) is to suppose that $|\lambda_{j}|\neq |\lambda_{k}|$.  Then by assumption,
\begin{align*}
\angle(Tx_{j},Tx_{k}) & = \arccos\left(\frac{\langle Tx_{j},Tx_{k} \rangle}{|Tx_{j}|\cdot |Tx_{k}|}\right)\\
& = \arccos\left(\frac{\langle \lambda_{j}{x_{j}},\lambda_{k}{x_{k}} \rangle}{|\lambda_{j}{x_{j}}|\cdot |\lambda_{k}{x_{k}}|}\right)\\
& = \arccos\left(\frac{\lambda_{j}\lambda_{k}\langle {x_{j}},{x_{k}} \rangle}{|\lambda_{j}|\cdot|\lambda_{k}|\cdot|{x_{j}}|\cdot |{x_{k}}|}\right)\\
& = \arccos\left(\text{sign}(\lambda_{j})\text{sign}(\lambda_{k})\frac{\langle {x_{j}},{x_{k}} \rangle }{|{x_{j}}|\cdot |{x_{k}}|}\right)\\
\end{align*}
may also be calculated as
\begin{align*}
\angle(Tx_{j},Tx_{k}) & =  \angle(x_{j},x_{k})\\
& = \arccos\left(\frac{\langle x_{j},x_{k} \rangle}{|x_{j}|\cdot |x_{k}|}\right).
\end{align*}
Then since $\arccos$ is injective, I believe I can make the jump that $\text{sign}(\lambda_{j})\text{sign}(\lambda_{k}) = 1$, which does not resemble the conclusion that I should arrive at.
Note:  I wasn't sure what tag to put this under, so anyone who knows better please feel free to adjust.
Thanks for any help you can give.
Comment: @Henry T. Horton:  Thanks for cleaning it up,  I was wondering how to do $\langle x,y \rangle$.  :)
Comment: I'm wondering whether the basis is supposed to be required to be orthogonal? That would make the statement true.
Comment: I will check first thing in the morning!  :)
I forgot to bring the text home with me.
Answer: If necessary, rescale each $x_i$ so they all have unit length and relabel the indices so that $|\lambda_1|\neq |\lambda_2|$.
Now, consider the vectors $v_1 = x_1 + x_2$ and $v_2 = x_1-x_2$.
First, I claim that $v_1$ and $v_2$ are orthogonal, for \begin{align*} \langle v_1, v_2\rangle &= \langle x_1+x_2,\; x_1-x_2\rangle \\\ &= \langle x_1, x_1\rangle + \langle x_1, x_2\rangle - \langle x_2,x_1\rangle - \langle x_2, x_2\rangle \\\ &= |x_1|^2-|x_2|^2 \\\ &=0\end{align*} where the last equality follows since both $x_1$ and $x_2$ are unit length.
Next, I claim that $Tv_1$ and $Tv_2$ are not orthogonal.  For, \begin{align*} \langle Tv_1,  Tv_2\rangle &= \langle \lambda_1 x_1 + \lambda_2 x_2,\; \lambda_1 x_1 - \lambda_2 x_2\rangle \\\ &= \lambda_1^2 \langle x_1, x_1\rangle +\lambda_1 \lambda_2 \langle x_1, x_2\rangle - \lambda_2\lambda_1 \langle x_2,x_1\rangle - \lambda_2^2 \langle x_2, x_2\rangle \\\ &= \lambda_1^2 |x_1|^2 - \lambda_2^2 |x_2|^2 \\\ &= \lambda_1^2 - \lambda_2^2\end{align*}
and this last line is $0$ iff $|\lambda_1| = |\lambda_2|$.
Comment: Summary:  My argument shows the forward implication is valid while copper.hat's argument shows the backwards implication is not.
Comment: Thanks very much.  I seem to have an awful habit of misreading implications as double-implications.  Or even worse, (and I've done this at least a half-dozen times) mistake a list of independently true claims for a "the following are equivalent" type claim.  I'll double check the text tomorrow morning, but most likely: my apologies for the oversight!
Comment: Fortunately for me the error was not mine for a change.

Very nice solution by the way.
Answer: This is not true.
Let $T=\begin{bmatrix} 1 & -2 \\ 0 & -1 \end{bmatrix}$. Then $x_1=\begin{bmatrix} 1 \\ 0 \end{bmatrix}$, $x_2=\begin{bmatrix} 1 \\ 1 \end{bmatrix}$ form a basis, and $T x_1 = x_1$, $T x_2 = - x_2$, hence the eigenvector requirement is satisfied.
However, take $x=\begin{bmatrix} 1 \\ 0 \end{bmatrix}$, $y=\begin{bmatrix} 0 \\ 1 \end{bmatrix}$, then $\langle x , y \rangle = 0$, but $\langle T x , T y \rangle = -2$. Since $\arccos$ is bijective on the domain $[-1,1]$, it follows that $\arccos \frac{\langle x , y \rangle}{\|x\| \|y\|} \neq \arccos \frac{\langle T x , T y \rangle}{\|T x\| \|T y\|}$.
Here is a slightly more satisfactory answer:
$T$ is angle preserving iff $T^T T = \sigma^2 I$, for some $\sigma >0$.
($\Rightarrow$) Suppose $T^T T = \sigma^2 I$. Then $T' = \frac{1}{\sigma} T $ is orthogonal. It follows that $\langle T'x, T'y \rangle = \langle x, y \rangle$,
and $\|T'v \| = \|v\|$, hence $\frac{\langle T'x, T'y \rangle}{\|T'x \| \|T'y \|} = \frac{\langle Tx, Ty \rangle}{\|Tx \| \|Ty \|} = \frac{\langle x, y \rangle}{\|x \| \|y \|}$.
($\Leftarrow$) Let $e_k$ be the standard basis on $\mathbb{R}^n$, and suppose $T$ is angle preserving. It follows immediately that $\frac{\langle T e_i, T e_j \rangle}{\|Te_i \| \|Te_j \|} = \delta_{i,j}$, hence $\frac{T e_i}{\|Te_i \|}$ form an orthonormal basis. Let $ Q = \begin{bmatrix} \frac{T e_1}{\|Te_1 \|} & \cdots & \frac{T e_n}{\|Te_n \|} \end{bmatrix}$, and let $\Lambda = \mathbb{diag}(\|Te_1 \|, \cdots, \|Te_n \|)$. It should be clear that $Q$ is orthogonal, and since $Tx = \sum x_i \|Te_i \| \frac{T e_i}{\|Te_i \|} = Q \Lambda x$, we have $T = Q \Lambda$. Now, adapting @Jason DeVito's trick, we notice that $\langle e_i+e_j, e_i-e_j \rangle = 0$, and so since $T$ is angle preserving, we have $\langle T(e_i+e_j), T(e_i-e_j) \rangle = \langle \Lambda(e_i+e_j), \Lambda(e_i-e_j) \rangle = \langle \Lambda e_i, \Lambda e_i \rangle - \langle \Lambda e_j, \Lambda e_j \rangle= 0$. It follows that $ \|T e_i \|^2 = \|T e_j\|^2$, and so we can write $\Lambda = \sigma M$, with $M$ a diagonal matrix filled with $\pm1$ (note $M^2=I$) and $\sigma = \|T e_1 \|$. Hence $T = \sigma QM$, from which is follows that $T^T T = \sigma^2 I$.
Comment: I added a slight elaboration purely for my own satisfaction...
Comment: @mSSM: It basically means that $T$ is a scaled rotation.
Comment: @mSSM: Note that if $T^T T = \sigma^2 I$, then (since ${1 \over \sigma}$ is orthogonal) we have $|\lambda_k|^2 = \sigma^2$ for all eigenvalues of $T$. Does this answer your question?
Comment: Ah, the good ol' confusion of eigenvalues with singular values... the book author must have thought of the latter (for which the statement is true).
Comment: Or I mistransposed the statement.  I'll check tomorrow morning.

Either way thanks very much for this.  If I could accept both answers I would! :)
Comment: I am currently working through the same problems in Spivak's, and found this answer. What is the idea behind the assumption $T^T T = \sigma^2 I$?
Comment: @copper.hat: I have a hard time seeing why you start out with that argument. How do you connect the assumption $T^T T = \sigma^2 I$ with the assumptions laid out in the problem?
Comment: @copper.hat: I have worked through the proof given in a PDF document I have found in this answer: http://math.stackexchange.com/q/354848/152538 (link to PDF: cs.bsu.edu/homepages/fischer/math445/angles.pdf ), which also clarified the trouble I had with your answer. It's a lot clearer now. Thanks.
Answer: New bug in above arguments! If $AA^T=\sigma ^2I$ we cannot say $A=\sigma I$. Counterexample:  take a diagonal matrix with $+\sigma$ or $-\sigma$ on diagonal, and $0$ elsewhere.
The very final answer to these circle of ideas is
Which I have proved with ALL details, and optimal care to such issues as orientation.
For 1 => 2 see ALL Orthogonality preserving linear maps from $\mathbb R^n$ to $\mathbb R^n$?
Please challenge.
Comment: But, why am I getting - votes?! Look, the description given before me does not include the case of reflection against the x-axis in $R^2$. Why the hell did they vote against me?
Comment: I didn't downvote, but the answers above do not conclude that if $A A^T = \sigma^2 I$ then $A = \sigma I$. Any rotation suffices as a counterexample. Where is the bug in the argument?


Twitter Cards with Metatag module not working

Question: I've got the metatag module installed and enabled. I've also enabled the MetaTag: Twitter Cards sub-module so that it will render meta tags for twitter cards, but I'm not seeing any twitter: metatags in the head at all.
Here is a page on my site:
http://www.inadaydevelopment.com/app/ios/10bii-financial-calculator
Here is the export of the Node configuration, I believe the only thing I changed is that I set a creator value for my twitter account:
$config = new stdClass();
$config->disabled = FALSE; /* Edit this to true to make a default config disabled initially */
$config->api_version = 1;
$config->cid = '1';
$config->instance = 'node';
$config->config = array(
  'title' => array(
    'value' => '[node:title] | [site:name]',
  ),
  'description' => array(
    'value' => '[node:summary]',
  ),
  'og:title' => array(
    'value' => '[node:title]',
  ),
  'og:description' => array(
    'value' => '[node:summary]',
  ),
  'twitter:creator' => array(
    'value' => '@inadaydev',
  ),
  'twitter:title' => array(
    'value' => '[node:title]',
  ),
  'twitter:description' => array(
    'value' => '[node:summary]',
  ),
);

I created a new default metatag for content type App, here is the export:
$config = new stdClass();
$config->disabled = FALSE; /* Edit this to true to make a default config disabled initially */
$config->api_version = 1;
$config->cid = '2';
$config->instance = 'node:app';
$config->config = array(
  'twitter:image' => array(
    'value' => '[node:field_phone_screenshots]',
  ),
);

However, none of the twitter:card meta tags show up at all... what am I doing wrong?
Answer: If you´re using the 7.x-1.0-Beta4 version of metatag module (which is the most recent one at the time of writing this answer), you may want to apply the following patch:
http://drupal.org/node/1846978#comment-6764558
(for Unix-like systems only) For applying the patch, just go to a terminal, change the working directory to the directory of the metatag module and try this:
curl http://drupal.org/files/metatag-fix_theme_hook_mismatch-1846978-0.patch | patch -p1
Comment: For a complete how-to applying patches in other systems, just refer to the official documentation: http://drupal.org/patch/apply


What does "mInflator" stand for?

Question: I see "mInflator" used as a property name around the place in many examples, but I can't seem to figure out what it stands for. Specifically, the "m". "myInflator"? "mobileInflator"? Please enlighten me.
Eg:
this.mInflator = LayoutInflater.from(context)

Comment: In addition to denoting **member var**, it improves readabliity of the code.  It gives you a visual cue/mnemonic for what is a member var, vs. a local var
Answer: Just a convention to denote instance variables used in the class.
In android m is used.
Similarly in Flutter code you'll see k.
Answer: m stands for "member". it is a convention for naming member variables. They are variables defined outside of any methods but inside the class and are used in different methods. member variables often said as fields in java / android studio

What does "mInflator" stand for?


My CloudWatch Event rule doesn't trigger my CodePipeline pipeline

Question: I'm having some issues with AWS CloudWatch Events.
I'm creating a CodePipeline CI pipeline which have a CodeCommit repository as the Source, a CodeBuild project as the Build/Test phase (then, it deploys to Lambda, but the problem isn't there).
We have multiple projects and we are going to push multiple other projects. So, I created a script that manages the AWS CI stuff (i.e. creating a pipeline, a CodeBuild project, ... AND a CloudWatch Events rule, linked to the pipeline).
The first time I push my code, it works. But then, the process stop getting triggered by the push on CodeCommit.
I found a solution (but NOT the one I want) : I just have to modify the pipeline, modify the stage (Source), not touching anything, and saving the null modification : and it works (before saving, it ask the authorization to create a CloudWatch Events rule associated with this pipeline).
Does somebody encountered this issue ? What did you do to bypass it ?
I really want to make a 100% automated CI, I don't want to go to the AWS Console each time my team create a new repository or push a new branch on an existing repository.
EDIT :
Here is the JSON of my CloudWatch Events rule :
{
    "Name": "company-ci_codepipeline_project-stage",
    "EventPattern": "cf. second JSON",
    "State": "ENABLED",
    "Arn": "arn:aws:events:region:xxx:rule/company-ci_codepipeline_project-stage",
    "Description": "CloudWatch Events rule to automatically trigger the needed pipeline from every push to project repository, on the stage branch on CodeCommit."
}

And here is the EventPattern JSON :
{
  "source": [
    "aws.codecommit"
  ],
  "detail-type": [
    "CodeCommit repository state change"
  ],
  "resources": [
    "arn:aws:codecommit:region:xxx:project"
  ],
  "detail": {
    "event": [
      "referenceCreated",
      "referenceUpdated"
    ],
    "referenceType": [
      "branch"
    ],
    "referenceName": [
      "stage"
    ]
  }
}

Comment: What is your CloudWatch event rule? Do you see the CloudWatch metrics for the rule show the rule being triggered when you push? I'm trying to figure out if the problem is with the rule or the pipeline.
Comment: @TimB, my CloudWatch Events rule has a template with a CodeCommit source, and details matching a **branch** named **dev** each time it is **created** or **updated** (with the good JSON format).
I can't see any metrics for my rule !
Comment: Can you post the actual JSON of the rule?
Comment: @TimB, I updated my post with the needed JSON
Answer: I've found this issue is typically related to the event rule/target/role configuration. If you don't have a target associated with your rule, you will NOT see the event invoked when reviewing metrics. Since your EventPattern looks correct, I'm thinking the target might be your issue.
You should have a configured target that looks something like:
{
    "Rule": "company-ci_codepipeline_project-stage",
    "Targets": [
        {
            "RoleArn": "arn:aws:iam::xxx:role/cwe-codepipeline",
            "Id": "ProjectPipelineTarget",
            "Arn": "arn:aws:codepipeline:region:xxx:your-pipeline"
        }
    ]
}

If that seems all good, I'd next check that the role associated with the target is granting the correct permissions. My role looks something like:
{
    "Role": {
        "Description": "Allows CloudWatch Events to invoke targets and perform actions in built-in targets on your behalf.",
        "AssumeRolePolicyDocument": {
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Action": "sts:AssumeRole",
                    "Principal": {
                        "Service": "events.amazonaws.com"
                    },
                    "Effect": "Allow",
                    "Sid": ""
                }
            ]
        },
        "MaxSessionDuration": 3600,
        "RoleId": "xxxx",
        "CreateDate": "2018-08-06T20:56:19Z",
        "RoleName": "cwe-codepipeline",
        "Path": "/",
        "Arn": "arn:aws:iam::xxx:role/cwe-codepipeline"
    }
}

And it has an inline policy of:
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codepipeline:StartPipelineExecution"
            ],
            "Resource": [
                "arn:aws:codepipeline:*:xxx:*"
            ]
        }
    ]
}

For reference, check out this documentation
Answer: I had the same problem, and finally realized that, in my case, the error was that the line
"detail-type": ["CodeCommit Repository State Change"]

was not written correctly. Eventbridge expects the wording to be exactly that, so if it is even slightly different, the event is not intercepted.


Operação de datadiff no SQL 2014

Question: Tenho uma base acumulada de datas no sql, eu queria selecionar todas as datas que tiverem da data atual (hoje) até 60 dias atrás, mas não estou conseguindo acertar o código.
<code>***select
    data
    CPF,
    sum(risco) as risco
    from BASE_ACUMULADA
    where  Data in (datediff(dd, 0, getdate())),(datediff(dd, 0, getdate())-60))
    group by data, CPF***
</code>
Answer: Experimente essa sugestão utilizando o operador Between:
<code>where Data between dateadd(dd, -60, cast(getdate() as date)) and getdate()
</code>
Espero que ajude

Operação de datadiff no SQL 2014


Trying to replace 2011 with getFullYear in copyright div

Question: I am trying to replace the year 2011 in a footer that contains the copyright with the current year.
Currently I have successfully replaced '2003-2011' with '2003-2012' like this:
  $(document).ready (function (function(){

 $('div.footer').html($('div.footer').html() .replace('2003-2011','2003-2012'));

});

But I wanted to replace '2011' with the current year so you don't have to change the script every year.
Doing this doesn't work:
 var year=getFullYear();

 $(document).ready (function(){

$('div.footer').html($('div.footer').html() .replace('2011','year'));

 });

As always, thank you very much for your help.
Comment: `('div.footer').html($('div.footer').html().replace('2011', new Date().getFullYear()));`
Comment: If you have access to server-side code, I would do this there as the user could have their date set to 01/01/1970 and would see `2003-1970`.
Comment: You might want to do some research into copyright law to see whether that's necessary at all. (Personally I think it is not.)  Look up, in particular, the Berne Convention.
Answer: Don't put year in quotes, it's a variable.  Putting it quotes makes the string 'year'.
var year = new Date().getFullYear();  // This should be `new Date`
                                      // as `getFullYear` isn't a global function
$(document).ready (function(){
   $('div.footer').html($('div.footer').html() .replace('2011', year));
});

Answer: This:
getFullYear();

Should be:
new Date().getFullYear();

And, as pointed out by the other answers, the string "year" should be a variable year.
Answer: You need to remopve the quotes from 'year' so it's a variable. Otherwise, it's a string...
Answer: Try this
$('div.footer').html($('div.footer').html() .replace('2011',year));

Since year is a  variable you should not put quotes around it.
Answer: You're replacing 2011 with the string 'year' not with the value of your variable.
Answer:

One Liners are always effective in real life as well as Coding. To keep it simple and sweet just this one line of code.
As script takes javascript as default again declaring it not needed.
In this context You can add that one line of code after 2003- "add line of code here"
P.S. Works with all the major browsers and all browsers enabled with javascript(which mostly are).


Topological modules and relative homological algebra.

Question: This question might be a bit dumb but I'm tired right now and this is just going over my head at the moment, in "The homology of Banach and topological algebras" Helemskii said that relative homological algebra can be used to define certain homological concepts in categories of locally convex modules, he refers to this paper by Taylor (http://bit.ly/1BeS6q5), I was reading this paper but there are a couple of things I was confused about:

There's a map $\epsilon: C \rightarrow A$ defined on page 141, but what is $C$?
I went to page 146 where Taylor says:

Note that our projective modules are not projective objects in the
  category of all $A$-modules. They are, however, relative projectives
  in the sense of chapter 13 of "Homology" by Sanders Mac Lane, if the
  class of allowable short exact sequences is taken to be the class of
  $C$-split short exact sequences.

In the context of relative homological algebra a relative projective is a $\square$-allowable projective objet $P$ in some abelian category $\mathcal{A}$, where $\square$ is a covariant functor between two abelian categories $\mathcal{A}$ and $\mathcal{B}$ which is additive, exact and faitful, if in this case $\mathcal{A}$ is the category of all $A$-modules, What is the abelian category $\mathcal{B}$?
Answer: Regarding your question on notation in Taylor's paper $C$ means the field of complex numbers and $\epsilon$ stands for the natural embedding of $C$ into unital algebra $A$.
As for the deinition of relative homology you must understand that you can't transfer Mac Lane's definition word by word to the realm of topological modules for one simple reason - their categories are not abelian. But you don't need to do so!
In fact you don't need to put any restrictions on $\mathcal{A}$ and $\mathcal{B}$ in order for this definition to work. For example if you take $\mathcal{A}$ as category of compact Hausdorff topological spaces, $\mathcal{B}$ the category of all sets and $\square:\mathcal{A}\to\mathcal{B}$ as obvious forgetful functor, then $\square$-allowable projective Husdorff topological spaces are extremelly disconnected compact spaces. This result is due to Rainwater, but, of course, he hasn't used such fancy language.
Comment: Sorry for taking so long to respond, thanks for the answer, so here $\mathcal{A}$ = the category of locally convex $A$-modules = **$A$-mod**, and $\mathcal{B}$ = the category of $C$-modules (relative to $\widetilde{\otimes}$) = topological vector spaces over $C$ (which are not abelian categories, but they are additive). Honestly, what I wanted to know was if the category of chain complexes in **$A$-mod** has the structure of a model category.
Comment: Unfortunately, I now nothing on model categories.
Comment: That's fine thanks, but back to my original question, the one thing I don't understand is why would Taylor say that his theory is an example of relative homological algebra if it isn't? There's a class of allowable short exact sequences but the categories involved $\mathcal{A}$ and $\mathcal{B}$ are additive, but Mac Lane defines relative homological algebra, relative abelian categories, and allowable short exact sequences when the categories involved are abelian, or I guess it doesn't matter since the techniques are still useful if we want to do homological algebra in this context?
Comment: In times of MacLane no one bothered too much about non-abelian categories, this is reason why MacLane gave his definition for abelian categories, though it works in much wider context.
Comment: It's a lot more clear now, thanks again.


UMVUE of $\frac{\theta}{1+\theta}$ while sampling from $\text{Beta}(\theta,1)$ population

Question:
Let $(X_1,X_2,\ldots,X_n)$ be a random sample from  the density $$f_{\theta}(x)=\theta x^{\theta-1}\mathbf1_{0<x<1}\quad,\,\theta>0$$
I am trying to find the UMVUE of $\frac{\theta}{1+\theta}$.

The joint density of $(X_1,\ldots,X_n)$ is
\begin{align}
f_{\theta}(x_1,\cdots,x_n)&=\theta^n\left(\prod_{i=1}^n x_i\right)^{\theta-1}\mathbf1_{0<x_1,\ldots,x_n<1}
\\&=\exp\left[(\theta-1)\sum_{i=1}^n\ln x_i+n\ln\theta+\ln (\mathbf1_{0<x_1,\ldots,x_n<1})\right],\,\theta>0
\end{align}
As the population pdf $f_{\theta}$ belongs to the one-parameter exponential family, this shows that  a complete sufficient statistic for $\theta$ is $$T(X_1,\ldots,X_n)=\sum_{i=1}^n\ln X_i$$
Since $E(X_1)=\frac{\theta}{1+\theta}$, at first thought, $E(X_1\mid T)$ would give me the UMVUE of $\frac{\theta}{1+\theta}$ by the Lehmann-Scheffe theorem. Not sure if this conditional expectation can be found directly or one has to find the conditional distribution  $X_1\mid \sum_{i=1}^n\ln X_i$.
On the other hand, I considered the following approach:
We have $X_i\stackrel{\text{i.i.d}}{\sim}\text{Beta}(\theta,1)\implies -2\theta\ln X_i\stackrel{\text{i.i.d}}{\sim}\chi^2_2$, so that $-2\theta\, T\sim\chi^2_{2n}$.
So $r$th order raw moment of $-2\theta\,T$ about zero, as calculated using the chi-square pdf is $$E(-2\theta\,T)^r=2^r\frac{\Gamma\left(n+r\right)}{\Gamma\left(n\right)}\qquad ,\,n+r>0$$
So it seems that for different integer choices of $r$, I would get unbiased estimators (and UMVUEs) of different integer powers of $\theta$. For example, $E\left(-\frac{T}{n}\right)=\frac{1}{\theta}$ and $E\left(\frac{1-n}{T}\right)=\theta$ directly give me the UMVUE's of $\frac{1}{\theta}$ and $\theta$ respectively.
Now, when $\theta>1$ we have $\frac{\theta}{1+\theta}=\left(1+\frac{1}{\theta}\right)^{-1}=1-\frac{1}{\theta}+\frac{1}{\theta^2}-\frac{1}{\theta^3}+\cdots$.
I can definitely get the UMVUE's of $\frac{1}{\theta},\frac{1}{\theta^2},\frac{1}{\theta^3}$ and so on. So combining these UMVUE's I can get the required UMVUE of $\frac{\theta}{1+\theta}$. Is this method valid or should I proceed with the first method? As UMVUE is unique when it exists, both should give me the same answer.
To be explicit, I am getting $$E\left(1+\frac{T}{n}+\frac{T^2}{n(n+1)}+\frac{T^3}{n(n+1)(n+2)}+\cdots\right)=1-\frac{1}{\theta}+\frac{1}{\theta^2}-\frac{1}{\theta^3}+\cdots$$
That is, $$E\left(\sum_{r=0}^\infty \frac{T^r}{n(n+1)...(n+r-1)}\right)=\frac{\theta}{1+\theta}$$
Is it possible that my required UMVUE is $\displaystyle\sum_{r=0}^\infty \frac{T^r}{n(n+1)...(n+r-1)}$ when $\theta>1$?
For $0<\theta<1$, I would get $g(\theta)=\theta(1+\theta+\theta^2+\cdots)$, and so the UMVUE would differ.

Having been convinced that the conditional expectation in the first approach could not be found directly, and since $E(X_1\mid \sum\ln X_i=t)=E(X_1\mid \prod X_i=e^t)$, I had proceeded to find the conditional distribution $X_1\mid \prod X_i$. For that, I needed the joint density of $(X_1,\prod X_i)$.
I used the change of variables $(X_1,\cdots,X_n)\to (Y_1,\cdots,Y_n)$ such that $Y_i=\prod_{j=1}^i X_j$ for all $i=1,2,\cdots,n$. This lead to the joint support of $(Y_1,\cdots,Y_n)$ being $S=\{(y_1,\cdots,y_n): 0<y_1<1, 0<y_j<y_{j-1} \text{ for } j=2,3,\cdots,n\}$.
The jacobian determinant turned out to be $J=\left(\prod_{i=1}^{n-1}y_i\right)^{-1}$.
So I got the joint density of $(Y_1,\cdots,Y_n)$ as $$f_Y(y_1,y_2,\cdots,y_n)=\frac{\theta^n\, y_n^{\theta-1}}{\prod_{i=1}^{n-1}y_i}\mathbf1_S$$
Joint density of $(Y_1,Y_n)$ is hence $$f_{Y_1,Y_n}(y_1,y_n)=\frac{\theta^n\,y_n^{\theta-1}}{y_1}\int_0^{y_{n-2}}\int_0^{y_{n-3}}\cdots\int_0^{y_1}\frac{1}{y_3y_4...y_{n-1}}\frac{\mathrm{d}y_2}{y_2}\cdots\mathrm{d}y_{n-2}\,\mathrm{d}y_{n-1}$$
Is there a different transformation I can use here that would make the derivation of the joint density less cumbersome? I am not sure if I have taken the correct transformation here.

Based on some excellent suggestions in the comment section, I found the joint density of $(U,U+V)$ instead of the joint density $(X_1,\prod X_i)$ where $U=-\ln X_1$ and $V=-\sum_{i=2}^n\ln X_i$.
It is immediately seen that $U\sim\text{Exp}(\theta)$ and $V\sim\text{Gamma}(n-1,\theta)$ are independent.
And indeed, $U+V\sim\text{Gamma}(n,\theta)$.
For $n>1$, joint density of $(U,V)$ is $$f_{U,V}(u,v)=\theta e^{-\theta u}\mathbf1_{u>0}\frac{\theta^{n-1}}{\Gamma(n-1)}e^{-\theta v}v^{n-2}\mathbf1_{v>0}$$
Changing variables, I got the joint density of $(U,U+V)$ as
$$f_{U,U+V}(u,z)=\frac{\theta^n}{\Gamma(n-1)}e^{-\theta z}(z-u)^{n-2}\mathbf1_{0<u<z}$$
So, conditional density of $U\mid U+V=z$ is $$f_{U\mid U+V}(u\mid z)=\frac{(n-1)(z-u)^{n-2}}{z^{n-1}}\mathbf1_{0<u<z}$$
Now, my UMVUE is exactly $E(e^{-U}\mid U+V=z)=E(X_1\mid \sum_{i=1}^n\ln X_i=-z)$, as I had mentioned right at the beginning of this post.
So all left to do is to find $$E(e^{-U}\mid U+V=z)=\frac{n-1}{z^{n-1}}\int_0^z e^{-u}(z-u)^{n-2}\,\mathrm{d}u$$
But that last integral has a closed form in terms of incomplete gamma function according to Mathematica, and I wonder what to do now.
Comment: At the point (early on) where you introduce $T$ you should be minded to work in terms of the variables $Y_i=-\log X_i.$ It's almost immediate that they are proportional to $\Gamma(1)$ distributions, which quickly reduces your problem to considering the joint distribution of $(U,U+V)$ where $U\sim\Gamma(1)$ and $V\sim\Gamma(n-1).$  That will shortcut the remaining two pages of mathematics and give you a quick route to a solution.
Comment: @whuber But how would I get $E(X_1\mid ...)$ from $E(\ln X_1\mid ...)$ directly ?
Comment: @whuber Please have a look at my edit. I have almost done it but not sure what to do with that integral. I am fairly confident my calculations are correct.
Comment: You should proceed with the first method, finding the conditional distribution of $X_{[1]} | \prod X_i$, which form of the sufficient statistic may be easier to work with in this application.
Comment: @whuber To be clear, are you suggesting that I find the density of $(-\ln X_1,-\ln X_1-\sum_{i=2}^n\ln X_i)$ first and from that find the density of $(X_1,\prod X_i)$? I had noticed that the $-\ln X_i$'s are exponential variables with rate $\theta$ (which is also a Gamma variable as you say), but hadn't thought of working with that.
Comment: You don't need to find the joint density of $X_1$ and $\prod X_i$: you can stop at finding the joint density of their logarithms.  That will suffice for computing the conditional expectation of $X_1.$
Comment: Writing $Y_i=X_i$ and conditioning on $\prod X_i=t,$ you would compute $E(\exp(Y_1)\mid \sum Y_i=\log(t)).$
Comment: @whuber I think the problem is solved now.
Answer: It turns out that both approaches (my initial attempt and another based on suggestions in the comment section) in my original post give the same answer. I will outline both methods here for a complete answer to the question.
Here, $\text{Gamma}(n,\theta)$ means the gamma density $f(y)=\frac{\theta^n}{\Gamma(n)}e^{-\theta y}y^{n-1}\mathbf1_{y>0}$ where $\theta,n>0$, and $\text{Exp}(\theta)$ denotes an exponential distribution with mean $1/\theta$, ($\theta>0$). Clearly,  $\text{Exp}(\theta)\equiv\text{Gamma}(1,\theta)$.
Since $T=\sum_{i=1}^n\ln X_i$ is complete sufficient for $\theta$ and $\mathbb E(X_1)=\frac{\theta}{1+\theta}$, by the Lehmann-Scheffe theorem  $\mathbb E(X_1\mid T)$ is the UMVUE of $\frac{\theta}{1+\theta}$.
So we have to find this conditional expectation.
We note that $X_i\stackrel{\text{i.i.d}}{\sim}\text{Beta}(\theta,1)\implies-\ln X_i\stackrel{\text{i.i.d}}{\sim}\text{Exp}(\theta)\implies-T\sim\text{Gamma}(n,\theta)$.
Method I:
Let $U=-\ln X_1$ and $V=-\sum_{i=2}^n\ln X_i$, so that $U$ and $V$ are independent. Indeed, $U\sim\text{Exp}(\theta)$ and $V\sim\text{Gamma}(n-1,\theta)$, implying $U+V\sim\text{Gamma}(n,\theta)$.
So, $\mathbb E(X_1\mid \sum_{i=1}^n\ln X_i=t)=\mathbb E(e^{-U}\mid U+V=-t)$.
Now we find the conditional distribution of $U\mid U+V$.
For $n>1$ and $\theta>0$, joint density of $(U,V)$ is
\begin{align}f_{U,V}(u,v)&=\theta e^{-\theta u}\mathbf1_{u>0}\frac{\theta^{n-1}}{\Gamma(n-1)}e^{-\theta v}v^{n-2}\mathbf1_{v>0}\\&=\frac{\theta^n}{\Gamma(n-1)}e^{-\theta(u+v)}v^{n-2}\mathbf1_{u,v>0}\end{align}
Changing variables, it is immediate that the joint density of $(U,U+V)$ is $$f_{U,U+V}(u,z)=\frac{\theta^n}{\Gamma(n-1)}e^{-\theta z}(z-u)^{n-2}\mathbf1_{0<u<z}$$
Let $f_{U+V}(\cdot)$ be the density of $U+V$. Thus conditional density of $U\mid U+V=z$ is \begin{align}f_{U\mid U+V}(u\mid z)&=\frac{f_{U,U+V}(u,z)}{f_{U+V}(z)}\\&=\frac{(n-1)(z-u)^{n-2}}{z^{n-1}}\mathbf1_{0<u<z}\end{align}
Therefore, $\displaystyle\mathbb E(e^{-U}\mid U+V=z)=\frac{n-1}{z^{n-1}}\int_0^z e^{-u}(z-u)^{n-2}\,\mathrm{d}u$.
That is, the UMVUE of $\frac{\theta}{1+\theta}$ is $\displaystyle\mathbb E(X_1\mid T)=\frac{n-1}{(-T)^{n-1}}\int_0^{-T} e^{-u}(-T-u)^{n-2}\,\mathrm{d}u\tag{1}$
Method II:
As $T$ is a complete sufficient statistic for $\theta$, any unbiased estimator of $\frac{\theta}{1+\theta}$ which is a function of $T$ will be the UMVUE of $\frac{\theta}{1+\theta}$ by the Lehmann-Scheffe theorem. So we proceed to find the moments of $-T$, whose distribution is known to us. We have,
$$\mathbb E(-T)^r=\int_0^\infty y^r\theta^n\frac{e^{-\theta y}y^{n-1}}{\Gamma(n)}\,\mathrm{d}y=\frac{\Gamma(n+r)}{\theta^r\,\Gamma(n)},\qquad n+r>0$$
Using this equation we obtain unbiased estimators (and UMVUE's) of $1/\theta^r$ for every integer $r\ge1$.
Now for $\theta>1$, we have $\displaystyle\frac{\theta}{1+\theta}=\left(1+\frac{1}{\theta}\right)^{-1}=1-\frac{1}{\theta}+\frac{1}{\theta^2}-\frac{1}{\theta^3}+\cdots$
Combining the unbiased estimators of $1/\theta^r$ we obtain $$\mathbb E\left(1+\frac{T}{n}+\frac{T^2}{n(n+1)}+\frac{T^3}{n(n+1)(n+2)}+\cdots\right)=1-\frac{1}{\theta}+\frac{1}{\theta^2}-\frac{1}{\theta^3}+\cdots$$
That is, $$\mathbb E\left(\sum_{r=0}^\infty \frac{T^r}{n(n+1)...(n+r-1)}\right)=\frac{\theta}{1+\theta}$$
So assuming $\theta>1$, the UMVUE of $\frac{\theta}{1+\theta}$ is $g(T)=\displaystyle\sum_{r=0}^\infty \frac{T^r}{n(n+1)...(n+r-1)}\tag{2}$

I am not certain about the case $0<\theta<1$ in the second method.
According to Mathematica, equation $(1)$ has a closed form in terms of the incomplete gamma function. And in equation $(2)$, we can express the product $n(n+1)(n+2)...(n+r-1)$ in terms of the usual gamma function as $n(n+1)(n+2)...(n+r-1)=\frac{\Gamma(n+r)}{\Gamma(n)}$. This perhaps provides the apparent connection between $(1)$ and $(2)$.
Using Mathematica I could verify that $(1)$ and $(2)$ are indeed the same thing.
Comment: In fact, the equality between $(1)$ and $(2)$ follows by writing the power series expansion of $e^{-u}$ in $(2)$ and then interchanging the integral and sum.
Answer: I think we can get to the more compact answer that you alluded to in regards to the upper incomplete gamma function. Using the first Method, I found the expression
$$ E \left[ X_1 | X_1X_2 \cdots X_n=e^T \right]=\left( n - 1 \right) \int_0^1 z^r \left( 1 - r \right)^{n-z}dr,$$ where $z=e^T.$
Wolfram Alpha integrates this to get
$$E \left[ X_1 | X_1X_2 \cdots X_n=e^T \right] = \frac{e^T \left( n-1 \right)}{T^{n-1} } \left[ \left( n-2 \right) !-\Gamma \left(n-1,T \right) \right]$$
Now the incomplete gamma function term does have a closed form when $n$ is an integer. It is
$$\Gamma \left(n-1,T \right)=\frac{\Gamma \left(n-1 \right)}{e^T} \sum_{j=0}^{n-2} \frac{T^j}{j!} $$
Rewriting the expectation and simplifying, we find
$$E \left[ X_1 | X_1X_2 \cdots X_n=e^T \right] = \frac{\Gamma \left( n \right)}{T^{n-1}} \left[ e^T-\sum_{j=0}^{n-2} \frac{T^j}{j!} \right]$$
I don't have access to the software that will verify equivalence with your results $(1)$ and $(2)$, but hand calculations for $n=2$ and $n=3$ do match with your $(1)$.
Comment: Where you wrote $\operatorname E\left[ X_1\mid x_1x_2\cdots x_n=e^T \right],$ you ought to have $\operatorname E\left[ X_1\mid X_1X_2\cdots X_n=e^T \right]. \qquad$


PCA: How can the first principal component both maximize variance AND define the line that most closely fits the data?

Question: I'm reviewing Chapter 6 from An Introduction to Statistical Learning. I'm having trouble understanding PCA and the provided example.
Can someone explain how the first principal component direction can yield observation projections with the largest possible variance, but also minimize the sum of the squared perpendicular distances between each point and the line?
In other words: how does the green line both maximize variance and fit the data as closely as possible? Seems to me that projections onto the blue line would have higher variance.
For reference, here's the excerpt from the book and the example figures:

The first principal component direction of the data is that along which the observations vary the most. For instance, consider Figure 6.14, which shows population size (pop) in thens of thousands of people, and ad spending for a particular company (ad) in thousands of dollars, for 100 cities.

  The green solid line represents the first principal component direction of the data.

  If we projected the 100 observations onto this line (as shown in the left hand panel of Figure 6.15), then the resulting projected observations would have the largest possible variance; projecting the observations onto any other line would yield projected observations with lower variance.

  There is another interpretation for PCA: The first principal component vector defines the line that is as close as possible to the data. In Figure 6.14, the first principal component line minimizes the sum of the squared perpendicular distances between each point and the line. These distances are plotted as dashed line segments in the left-hand panel of 6.15, in which the crosses represent the projection of each point onto the first principal component line.

6.14
6.15
Answer: Your reference already gives the answer.

The first principal component direction of the data is that along which the observations vary the most

This is referring to the projections of the data onto that line, sort of the variance explained by that line.
I think you might be interpreting this something like:

The first principal component direction is the dimension on which the residuals vary the most

which is really the opposite statement: the residuals are the deviations from that line, which is the variance of everything that is not the first principal component.
The first principal component is the one that captures as much of the variance as possible along that dimension.
Answer: If you are looking for a visual and more intuitive explanation of what the PCA does and represents I recommend looking at this video: https://youtu.be/TJdH6rPA-TI. It walks through an example using PCA on a music dataset.
Comment: Hi SirHauler and Welcome to Cross Validated. Do you mind to provide some explanation to the material provided in the link? In the future links may be dead or not available and in this site  we are looking for stable&solid answers.


Python for loop to modify DataFrame

Question: How to use for loop to execute the following operation?
df_1=pd.concat([df_ab1,df_xy1], axis=1)
df_2=pd.concat([df_ab2,df_xy2], axis=1)
df_3=pd.concat([df_ab3,df_xy3], axis=1)
df_4=pd.concat([df_ab4,df_xy4], axis=1)
df_5=pd.concat([df_ab5,df_xy5], axis=1)
df_6=pd.concat([df_ab6,df_xy6], axis=1)
df_7=pd.concat([df_ab7,df_xy7], axis=1)
df_8=pd.concat([df_ab8,df_xy8], axis=1)
df_9=pd.concat([df_ab9,df_xy9], axis=1)

I was thinking something like this
for i in np.arange(9):
    df_str(i+1)=pd.concat([df_ab+str(i+1),df_xy+str(i+1)], axis=1)

Apparently, something is missing.
Many thanks in advance!
Comment: Do not create variable names dynamically (`df_str(i+1)`). There are workarounds to do that, but don't... Instead go for the list structure you already have in mind. Declare the list `df`; initialize it by: `df = []`. then use `df.append(pd...)` to append the `pd` objects. Then you can access list elements by their index (`pd[0]`).
Answer: I am not recommended below solution but this will solve your problem
variables = locals()
for i in np.arange(9):
    variables["df_{0}".format(i+1)] = pd.concat([variables["df_ab{0}".format(i+1)],variables["df_xy{0}".format(i+1)]], axis=1)

Comment: Agreed, a better code structure would be to have a master df with multiindex
Comment: Many thanks! It seems that there is a right square bracket missing.
Comment: @ER1CZZ yes :-) you got it ~
Comment: @RichieV Thanks for the comment. I will try to use a master df.

Python for loop to modify DataFrame