Find item position in the pager

Question: I was wondering how I am able to find the position of an item in a views pager?
I have nodes listed using views (with pager, 20 every page), and I'd like to find out for example where in the pager one node is?
Comment: Take look here http://stackoverflow.com/q/5271121
Comment: Sorry but that didn't help either?
Comment: Do you want to have the position of the node in the current view (like 1 - 20), the overall position among all pages or the page number?
Comment: I don't have an answer at this point, but this is a very interesting issue, perhaps a candidate for a small module. I'm just wondering how you would like to refer to the node you want to find, by nid or some other parameter (text, title)...? And I'm BTW jealous you managed to get a pager to work.
Answer: EDIT (2015-04-05): Updated the snippet to work with newer version of Views.
Us too had to find the page number of a views result on which a certain node appears, so we can redirect visitors to this page.
By loading a view for a specific display and executing it's query manually, we retrieve an array with all node ids which we can search for a specific node id. When we have got the position of the node id, we divide it by the amount of items per page to get the page number the node is on.
I understand that loading all node ids at once in an array probably should not be done on large amounts of records, but for this exercise it's okay.
/**
 * Custom function to get the page number of a views result a node appears on.
 * Based on Drupal's views_get_view_result().
 */
function yourmodulename_views_get_item_page_nr($nid, $name, $display_id = NULL) {
  $args = func_get_args();
  array_shift($args); // remove $nid
  array_shift($args); // remove $name

  if (count($args)) {
    array_shift($args); // remove $display_id
  }

  $view = views_get_view($name);
  if (is_object($view)) {
    if (is_array($args)) {
      $view->set_arguments($args);
    }

    if (is_string($display_id)) {
      $view->set_display($display_id);
    }
    else {
      $view->init_display();
    }

    // Set the limit to 0 to retrieve all node ID's.
    $view->set_items_per_page(0);

    $view->pre_execute();
    $view->build();

    // Get the views query.
    $query = $view->build_info['query'];

    // Get the return assoc array with return objects, keyed by nids.
    $nids = $query->execute()->fetchAllAssoc('nid');

    // Get position of nid in nids array.
    $index = array_search($nid, array_keys($nids));

    // Get amount of items per page.
    $items_per_page = $view->display[$view->current_display]->handler->get_option('items_per_page'); // As seen at http://api.drupal.org/api/views/includes%21view.inc/function/view%3A%3Aexecute/7.

    // Calculate page number.
    $page_nr = $index === null ? 0 : intval($index / $items_per_page);

    return $page_nr;
  }
  else {
    return 0;
  }
}

Answer: If you want to look at the css, it will tell you that in that each "item" is a row, and each are surrounded with a div that has classes like:

and the views-row-## is the item number in that pager.
You can also use the Global: View result counter  as a field, but that gives you the position in ALL of the results, not the page in question, but a quick manipulation of that and the page number in question will give you the result, too.  You might be able to use ctools' Global: Math expression to do that for you if it isn't already there for you (you didn't say what versions/modules you are running.)
If you want to dig into the guts of the view in a template, you could also get this information from $view->result.
Answer: Your problem is that you don't need that. I mean knowing the precise position of a node in a views query, makes you search on the entire record set, which would result in a huge performance issue.
The good question is : why do you need that ? There is 100% another, more accurate solution to your functional problem.
Comment: I have a leaderboard with many scores on it. I want a "jump to my position" button, so that people who are 10-20 pages in can find themselves quickly. What else could you do for that scenario?
Comment: This should be a comment, not an answer.
Answer: How to get that depends on what you will be using for.
If you will be using for calculation under the hud. You could get it using simple php:
$position = $absolute_position % $pager_limit;

$absolute_position is position of node in not paged the whole list if Drupal does not give you that by default at worst case scenario you could run a simple foreatch loop which would check for the node`s title or id to match.
If you will use it for theming Javacript can do the trick. Would personally use use jQuery library to make easier for my self. A simple script could find desired element by it`s tags and then change it accordingly.

Find item position in the pager


Filtering a table using views

Question: I am  new to drupal and I already created a table using view. I also applied exposed content filtering and I got the following screen

Now I want to show all the category and author are listed in a drop down and i want to select one from that. Also how can I display all the content of table before filter?
Thanks in advance.
Comment: Can u please specify **Also how can I display all the content of table before filter**
Comment: i think when you have created a field "category" and "author"  in your content type .you have selected "novel" and "rachit sharma" as a default value
Comment: @vishnu !!!cheers
Answer: You can change filter view settings. goto admin/structure/views and edit your views and follow the screenshots
Comment: @Vamsi, you are assuming that the fields Category and Author are term reference fields. Since Vishnu cannot find the setting, I believe he is dealing with Text fields. Vishnu, the Dropdown option only appears for Term reference fields. You may want to create Vocabularies for Category and Author, and change the fields "Category" and "Author" to Term references referencing terms in those Vocabularies.
Comment: Thanks for the quick support :) But I have no such `settings` link there. Is it get by adding any additional module?
Comment: @ argiepiano, You are right! I just trying this with text fields. Is there any way to show the list of entered `author` as list as term reference
Answer: You can do it when you select add filter checked dropdown box

For more details dynamically-filter-content-drupal-view
Comment: Thnks for your quick reply :D
Comment: have you check mention link
Comment: yup.. but as per the instruction, I dont get any dialog asking `selection type`. Any extra module needed do so?
Answer: Any time you need a dynamic dropdown view filter, you need a Taxonomy vocabulary (Drupal core).
In your example, you need first to create a vocabulary named Authors (no need to enter terms).
Secondly, you add a field Author, type Term reference, related to your vocabulary Authors, in the content type displayed in your View.
Choose autocomplete widget to allow term creation when a content is created.
Now, if you create a new content and enter a value in the field Author, it will add each new term or suggest existing ones.
Finally, in your View, you add an exposed filter related to your field Author, and you can select dropdown list. All authors entered during content creation will appear in the list.
If you need to enter manually authors, you just have to get to your Vocabulary Authors and add terms. They will also appear in your dropdown filter.
Hope this helps.
Comment: Is it practical to create hundreds of authors as term? I dont know, I just start drupal, I commonly use the vocabulary for adding general categories like **Category of books , Gender**, etc for easy choosing
Comment: Concerning the author field values, it depends on the amount of data. Of course you can enter your content one by one and each time you fill the field Author with a new value, the vocabulary will grow. If you have hundreds of lines in your external database, you should try the Feeds module... but this is another subject, I guess. Anyway, if your website allow contributors to add content, your vocabulary (and consequently your exposed filter) will be created progressively by users.

Filtering a table using views


Excel-VBA: Do a calculation only if cells involve contain values

Question: I have the code that nicely calculates the average I want. But now I want it to only run the calculation if the referenced cells contain values. I am totally stumped on how to feed conditions into my code.
Dim i%
Dim rng As Range
Dim last&
last = Range("A65536").End(xlUp).Row
    For i = 2 To last
        Range("F" & i).FormulaR1C1 = _
            "=AVERAGE(R[-4]C[-2],R[-2]C[-2],RC[-2],R[2]C[-2],R[5]C[-2],)"
    Next i
End Sub

I tried working with AVERAGEIF, but I can't get it to work either. the range gives me a #Value in the range whenever I try to set it using the function wizard. So I have no idea how to make it work in VBA.
Any and all help would be appreciated.
*Edit - I only want the average line to appear in the cells, but I want to test the cells for values before doing the calculation. (Siddharth, thanks for your answer anyway!) To clarify:
Dim i%
Dim rng As Range
Dim last&
last = Range("A65536").End(xlUp).Row
    For i = 2 To last
     'test for all cells having values here
        Range("F" & i).FormulaR1C1 = _
            "=AVERAGE(R[-4]C[-2],R[-2]C[-2],RC[-2],R[2]C[-2],R[5]C[-2],)"
    Next i
End Sub

***Edit 2: To be more clear as to what I'm looking for, I want something like this:
Dim i%
Dim last&

last = Range("A65536").End(xlUp).Row
For i = 2 To last
    While Range("D" & i - 4).Value <> "" And Range("D" & i + 4).Value <> ""
        Range("F" & i).FormulaR1C1 = _
                 "AVERAGE(R[-4]C[-2],R[-2]C[-2],RC[-2],R[2]C[-2],R[5]C[-2]))"
    Wend
Next i

But my while statement is giving me trouble, as I keep getting an error when I reach that point in the code. I also have tried:
While Range("D" & i - 4 And "D" & i + 4).Value <> ""

Which gives me run time error 13: type mismatch.
Comment: You want the cell to stay blank (no value or formula) if the cells have no value?
Comment: Slightly confused with `R[-4]` when the value of i < 5
Comment: If you were to put the formula directly in say F2 in Excel, what would it be?
Comment: Tim-Yes, exactly. Siddharth - It loops around to the bottom of the column, which also contains no value.
Comment: I deleted  my post. Let me rework in it and post back
Comment: ok, if the cells are blank you don't want to calculate the average else you want to calculate? Is my understanding correct?
Comment: `While Range("C" & i - 4).Value <> "" And Range("C" & i + 4).Value <> ""` Did you by any chance mean Col D and for all 5 rows?
Comment: I think yo u need to take a moment and rethink because the average checks for i,i-2,i-4,i+2,i+5 cells.
Answer: If I understand you correctly then you need to check if the number of cells in a range equal the number of filled values. For example
Sub a()
    Dim i%
    Dim rng As Range
    Dim last&
    last = Range("A65536").End(xlUp).Row

    For i = 2 To last
        Range("F" & i).FormulaR1C1 = "=if(" & _
                     "Rows(R[-4]C[-2])+Rows(R[-2]C[-2])+Rows(RC[-2])+Rows(R[5]C[-2])<>" & _
                     "COUNTA(R[-4]C[-2],R[-2]C[-2],RC[-2],R[2]C[-2],R[5]C[-2],)-2,""Blank""," & _
                     "AVERAGE(R[-4]C[-2],R[-2]C[-2],RC[-2],R[2]C[-2],R[5]C[-2],))"
    Next i
End Sub

Comment: But how can I do it without everything before the average showing up in the cell?
Comment: I didn't get your meaaning
Comment: I only want my equation to start as "=AVERAGE(..."
Comment: Why don't you try this then `=AVERAGE(IF(Rows(rng)<>COUNTA(rng)-2,rng,"Blank"))` You can take `rng` values from my code above


Create Vector Image in PHP to Allow Upscaling

Question: This question is slightly long, so I'll try to be clear.
On a website I co-develop, I created a drawing application that sends data about the lines drawn on the <canvas> element to the server to create an image and save it at multiple sizes, which works. There is the standard save which is saved to 320x212, with the smaller version at 240x176.
However, some users on my site use a pretty antiquated device/browser for the application. It also has a smaller viewport, so the image is smaller, so server-side I'm currently multiplying the coordinates to compensate when saving the image to the standard size. But this has the side-effect of random unfilled parts of the image showing up, that were filled in on the canvas.

(source: socialcu.be)
(On the canvas for the smaller device's viewport, the entire bottom portion was green, while my method of multiplying the coordinates to compensate caused many unfilled portions to appear on the scaled-up image)
So my first question is, would making the image as a vector image, scaling it up (while still a vector), and then saving it as a raster image fix that issue?
And secondly (if the answer to the first is a yes), then what would be the best way of doing this in PHP? I've heard of Cairo, but the information on it (namely tutorials for the PHP package) is quite lacking. Optimally, is there a way to do this in Imagick, or a tutorial on how to use Cairo?
Answer: Cairo is definitely lacking for documentation on the PHP site, so it will be a much more difficult set to learn. I found a couple of links through Google in regard to Cairo. There is a blog post about someone's experience with it here: Getting started with Cairo
Referenced in that post are a couple of walk-thrus by Michael Maclean.
There are quite a few examples of drawing with imagick on the imagick website, and from the looks of the available research online with examples imagick has a better support community (for now). imagick will output a raster image though.
Comment: Thanks for the response. :) I'm not tracing a preexisting image, I'm sending data on how the lines were drawn on the canvas (the coordinates of the drawn lines, their color, the width, etc.). What I mean is, is there anyway I could create a vector image from this data instead of a raster image, scale it up, and then save it as a raster to the filesystem?
Comment: Hello? When you get the chance, could respond to my previous comment? :)
Comment: Hello? Not everybody lives on StackOverflow. There are several APIs for drawing with vectors and saving out raster formats.


Webform Thumbnail Image Not Created

Question: I've created a form using D7 Webform 7.x-3.20. This form works in the following manner:

Fill out form and attach image.
Email is sent to admin to approve where you click through to admin approval pages.
Approve information and upload image (no issue).
Page generation page where you select the image to thumbnail.
Save and publish page which is added automatically to a gallery.

One issue; on step 4 the thumbnail will occasionally break and not generate in the specified folder I've defined after so large a size. I've tested to confirm it's not a file type since all JPG, GIF and PNG work just fine. It is only after about 10MB of size the issue presents itself. I've also checked my form to confirm it allows for larger file uploads than that. Additionally my php.ini file is currently allowing for up to 150 MB file size during this time of testing.
Hopefully we can brainstorm this issue together and come up with a solution.
Thanks in advance!
Comment: A 10MB image might need a lot of CPU and RAM to resize.
What are your settings for `memory_limit` or maybe `max_execution_time`? Maybe try to increase them for testing.
Comment: Welcome to Drupal Answers! Please ask an explicit question. So far, you simply stated facts.
Comment: Hi Kiamlaluno, sorry if I didn't come across clearly as to what I was trying to troubleshoot. My express interest is in solving why the thumbnail made in number 4 of the list above doesn't always generate.
Comment: @Chris   php_value max_execution_time              60
  php_value memory_limit                    128M
Comment: @Chris I've tested this to the point where I've pushed those settings far beyond the time and limit listed in the comment above. Additionally, I've also been able to have the thumbnail not generate at a much lower file size – 4mb. Any additional thoughts?

Additionally the file they are located in has a permissions level of 775
Comment: I just tried it as well but that seems to be fine. Did you try another server? Maybe it's not so much  a Drupal issue but rather a combination of Drupal and Server or similar? I can't really reproduce the issue.
Comment: I have not tried this myself, but I think that may be where my issue is taking me next from the research that I've done to this point. I'll try and reproduce similar results on a separate server instance, and if I continue to have the same results issues I'll be sure to double back here with my findings or my success.
Comment: Sadly, after some time with this the new server update didn't resolve the issue.
Comment: Update: So it seems the images are failing apparently due to a max-width. The images that are failing are a particularly large size in one way or another. 4000px wide. Once the image size is reduced and resaved the file works correctly without issue.

Thoughts in regards to how to remove this restriction?
Answer: Issue resolved. This was a combination of issues with file compression, max width dimensions and ram allocated to it.

Webform Thumbnail Image Not Created


Section of a coherent algebraic sheaf being zero over a principal open

Question: I have a question on Proposition 6 of §43 of Serre's Coherent Algebraic Sheaves (page 51-52 in the link).
The proposition is stating that, given $X$ any irreducible algebraic variety, $Q$ a regular function on $X$, $\mathcal{F}$ a coherent algebraic sheaf on $X$ and $s$ a global section of $\mathcal{F}$ which restriction to $X_Q$ is zero, then for $n$ sufficiently large the section $Q^n s $ is zero on all $X$. (Recall that $X_Q$ is defined to be the open of $X$ given by thos $x$ such that $Q(x) \neq 0$).
This is my reasoning: the section $Q \cdot s$ is zero on $X$ since, at any point $x \in X$, either $x \in X_Q$, so $s(x)=0$, or $Q(x)=0$, so  $Qs(x)= Q(x)\cdot s(x)=0$
Following this reasoning, the whole proposition would be useless (for example the coherence condition wouldn't play any role). Where am I wrong?
Answer: Given an element $f \in \mathcal O_X(X)$ and a point $x \in X$, there are two notions of $f$ being zero/vanishing at $x$.

The germ $f_x$ is zero as an element of $\mathcal O_{X,x}$. This is meant, if you read $f_x=0$.
The germ $f_x$ is contained in the maximal ideal of $\mathcal O_{X,x}$, i.e. it is zero in the residue field $k(x)$. This is meant, if you read $f(x)=0$.

In particular $X_f$ is defined via the second notion, i.e.
$$X_f = \{x \in X ~|~ f_x \in \mathcal O_{X,x}^* ~\}.$$
In particular, if $x \notin X_f$, the germ $f_x$ is contained in the maximal ideal of the local ring $\mathcal O_{X,x}$, but it might be a non-zero element. In particular $(fs)_x$ might be a non-zero element of $\mathcal F_x$, i.e. $fs \in \mathcal F(X)$ might be a non-zero global section.
As an example, take $X = \operatorname{Spec} k[x], \mathcal F = (k[x]/(x^2))^\sim, f=x \in k[x], s=1 \in k[x]/(x^2)$.
The localization of $s$ at all primes in $X_f = \{(x)\}^c$ is zero, since we have $\operatorname{Supp} k[x]/(x^2) = \{(x)\}$. Hence, by the proposition, $f^ns=0$ for some $n$. And here, we have to take $n=2$, since $f^1s=x$ is a non-zero element of $k[x]/(x^2)$.

Some notes to understand the meaning of the second notion: Let $X = \operatorname{Spec}k[x_1, \dotsc, x_n]$ the affine $n$-space. and $f \in \mathcal O_X(X)=k[x_1, \dotsc, x_n]$ a regular function.
Let $p=(p_1, \dotsc, p_n)$ be a root of $f$, i.e. $f(p)=0$ and let $x = \mathfrak m_p = (x_1-p_1, \dotsc, x_n-p_n)$ be the corresponding maximal ideal. Of course the localization $f_x \in k[x_1, \dotsc, x_n]_x$ is non-zero, since any localization map from an integral domain is injective.
Hence the first notion of $f$ being zero at $x$ is not the correct one if we want to interpret $x$ as a root of $f$.
The second notion however is: $f(p)=0$ is equivalent to $f \in \mathfrak m_p$ by the Nullstellensatz, hence $f(p)=0$ is equivalent to the statement that $f_x$ is contained in the maximal ideal of $\mathcal O_{X,x}=k[x_1, \dotsc, x_n]_{\mathfrak m_p}$.


Copiar texto oculto igual whatsapp

Question: Ola, uma situação que preciso fazer e não consegui utilizamos métodos normais como "display: none, user-select: all" etc, é copiar textos ocultos ou em tags ocultas.
Se abrir o whatsapp web, verá que ao copiar uma mensagem, o NOME + DATA HORA aparece na frente de cada mensagem copiada. Esta seria a intenção de ser feita.
Temos um chat hoje, porem exibimos nome + data e hora, e isto acaba ficando visualmente estranho em nosso sistema. Gostaria-mos de esconder isto e exibir apenas ao copiar.
Alguem tem ideia de qual a possibilidade ou como fazer algo deste genero?
Comment: Oi talvez esse link [aqui](https://stackoverflow.com/questions/42089713/modify-clipboard-content-after-copy-event-javascript-jquery) te ajude!
Answer: Tem uma hakezinho que vc pode fazer... só tem um "problema" a pessoa tem que copiar o texto até o final, se ela não pegar o texto todo não vai pegar a data e hora que estão ocultos no fim do texto... Tipo, se a pessoa pegar só um pedaço do meio da mensagem não vai pegar a data e a hora. Veja o gif abaixo para entender melhor.

O que eu fiz foi colocar a cor transparente e diminuir o tamanho do texto com o letter-spacing. Então se a pessoa selecionar tudo arrastando, ou clicando 3x vai pegar a ata/hora, se seleciona do final pro início pega tb. Só não pega data/hora se a pessoa só seleciona um pedaço do início ou do meio do texto.
Teste vc mesmo...

.box {
    position: relative;
    width: 200px;
    border: 1px solid #000;
}

.box .txt {
    position: relative;
    display: inline-block;
}

.box .data {
    color: rgba(0, 0, 0, 0);
    letter-spacing: -7px;
    display: inline-block;
}

        Lorem ipsum dolor sit. Lorem ipsum, dolor sit amet consectetur adipisicing elit. Mollitia, dignissimos.
         24/08/19 8:20

OBS: Ocultar texto é considerado uma prática de black hat pelo Google, não use isso para ocultar key words e outras coisas, o Google pode te penalizar por isso... Utilize apenas no seu sistema de chat se desejar.
Comment: Perfeito! O do Whatsapp tambem não copia caso pegar metade / parte do texto, apenas selecionando tudo. Não temos nenhum site publico ou que precisamos de importância do Google, todos nossos sistemas são internos e acessados localmente pela equipe.
Comment: @OtavioFausto que bom que ajudou ai, se ficou igual o do whatsapp da bom de mais então haha
Comment: Não ficou igual kkk porque pelo o que eu entendi eles usam javascript que substitui, porem ficou parecido a questão da funcionalidade, ja servindo para o objetivo. Como é interno, não precisa de nada muito profissional.

Copiar texto oculto igual whatsapp


quadrilateral inside a parallelogram

Question:
the diagonals of the parallelogram creates four equal trianges, so △ ABD and △ BOC together becomes 3/4 s.  But stuck here, how do I find area of △DEM and △MFC, so as to solve to find out the area of MEOF??
Answer: Let $DM=xDC$.
Thus, $$S_{\Delta ADM}=xS_{\Delta ADC}=\frac{1}{2}xS$$ and
$$\frac{S_{\Delta ADE}}{\frac{1}{2}xS}=\frac{AE}{AM}=\frac{AE}{AE+EM}=\frac{1}{1+\frac{EM}{AE}}=\frac{1}{1+\frac{DM}{AB}}=\frac{1}{1+x}.$$
Hence, $$S_{\Delta ADE}=\frac{\frac{1}{2}xS}{1+x}.$$
In another hand,
$$S_{\Delta BCM}=(1-x)S_{\Delta BDC}=\frac{1}{2}(1-x)S$$ and
$$\frac{S_{\Delta BCF}}{\frac{1}{2}(1-x)S}=\frac{BF}{BM}=\frac{BF}{BF+FM}=\frac{1}{1+\frac{FM}{BF}}=\frac{1}{1+\frac{CM}{AB}}=\frac{1}{2-x}.$$
Hence, $$S_{\Delta ADE}=\frac{\frac{1}{2}(1-x)S}{2-x}.$$
Id est,
$$\frac{\frac{1}{2}xS}{1+x}+\frac{\frac{1}{2}(1-x)S}{2-x}=\frac{1}{3}S,$$
which gives $$x=\frac{1}{2},$$
$$S_{\Delta DEM}=S_{\Delta MFC}=\frac{1}{12}S$$ and
$$S_{EMFO}=\frac{1}{4}S-2\cdot\frac{1}{12}S=\frac{1}{12}S.$$
Answer: Notice that the area of △AMB is equal to the sum of the areas of △DMA and △BMC, or $\frac{1}{2}S$. And since we're given △AED and △BFC together have an area of $\frac{1}{3}S$, we can deduce that the combined area of △DME and △MFC is $\frac{1}{6}S$. But the area of △DOC is $\frac{1}{4}S$, hence we're left with $\frac{1}{12}S$ for the quadrilateral.

quadrilateral inside a parallelogram


Why is a test statistic called a test statistic?

Question: I'm sure this is really basic -- but I want to understand the origins (etymology) of the term "test statistic".
Answer: I think this is pretty straightforward, at least at one level. "Test" because it's the result of a test, "statistic" because it comes from a sample, not a population. (A mnemonic: Populations have Parameters; Samples have Statistics).
Or did you want something on the origin of the term "statistic"?
Comment: Actually I think you've answered it for me.
Answer: The term statistic has a particular meaning in stats.
For present purposes we could take it to be a real valued function of a set of observations. (For a given sample, that would give a number calculated from the sample.)
A test statistic is then a statistic that is used as the basis for a hypothesis test.


BruteForceMedian algorithm appears to have a quadratic efficiency. (Why?)

Question: This algorithm appears to have a quadratic efficiency. (Why?)
Answer: To analyze complexity, you just have to count the number of operations.
Here, there are two nested loops:
for i in 0 to n – 1 do
    for j in 0 to n – 1 do
        operation() // Do something
    done
done

With i = 0, operation will be ran for all j in [0,n-1] that is n times. Then increment i, and repeat until i > n-1. That is the operation is ran n*n times in the worst case.
So in the end, this codes does n^2 operations, that's why it has quadratic efficiency.
Answer: This is a trick question, it appears to be both n^2 and 2^n at the same time depending on the whether the if statement on line j is executed.
Comment: There is no re-indexing, so where do you see the exponential ?


Show PDF in Xamarin App from byte array

Question: I am creating a mobile application with Xamarin Forms where the user can view the result of exams done. The result of the exams comes from a database in a byte array format and I need to somehow convert the byte array into a PDF and display it to the user. I saw that I can use a WebView to display a PDF, but would need to save it to the device and then display it. Would anyone know how I can convert the byte array to PDF and display it in a WebView without having to save it or do it otherwise?
Thanks in advance.
Comment: Hey, thank you very much for the answer but I'm still trying to implement this. His answer was the most promising one for now.
Answer: Using pdf.js makes it really easy: https://mozilla.github.io/pdf.js/

Add a WebView to your Forms' Page and setup the dependency code to obtain the baseurl path per platform

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=vswin

Add the pdf.js and pdf.worker.js to your application projects (BundleResource for iOS and AndroidAsset for Android)
Convert your PDF stream to a Base64-string (via a MemoryStream if needed) and embed that string into a HTML-based string.
Use JavaScript (atob) to decode that base64 string and setup pdf.js /pdf.worker.jsto render to acanvas` defined in your html.

Example:
XAML:
<StackLayout x:Name="webViewContainer" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
    <WebView x:Name="webView" BindingContext="{x:Reference webViewContainer}" WidthRequest="{Binding Width}" HeightRequest="{Binding Height}"/>
</StackLayout>

Code-behind:
Note: Using a PDF via bundled resource, substitute your stream there....
            var baseUrl = DependencyService.Get<IBaseUrl>().Get();
            string base64Pdf;
            using (var stream = await FileSystem.OpenAppPackageFileAsync("Dank Learning 1806.04510.pdf"))
            using (var memoryStream = new MemoryStream())
            {
                await stream.CopyToAsync(memoryStream);
                base64Pdf = Convert.ToBase64String(memoryStream.ToArray());
            }
            var html = @"

StackOverflow / Base64 PDF

<canvas id=""the-canvas"" style=""border: 1px solid""></canvas>

by SushiHangover

";
            html = html.Replace("SUSHIHANGOVER", base64Pdf);
            webView.Source = new HtmlWebViewSource
            {
                BaseUrl = baseUrl,
                Html = html
            };

Show PDF in Xamarin App from byte array


why nodejs file system read console first then read the file

Question: I can read the files using nodejs file system:
const fs = require('fs');

fs.readFile('./assets/test1.txt', (err, data) => {
    if(err){
        console.log(err)
    }
    console.log(data.toString())
})

console.log('hello shawn!')

Why console.log('hello shawn!') read first times then read the console.log(data.toString())?
Is there any other things in file system read data first then read below console?
Answer: It is because .readFile is a asynchronous operation. Its last parameter is callback function, which is started after operation is done. I recommend read something about callbacks and event loop.
You can use a synchronous version of function readFileSync or use utils.promisify to convert callback function to promise and use async/await then example.


Check for field values to be unique when storing into the DB

Question: I have a content type "Research assignment" which has several fields including the field "field_user" and "field_project".
I want to remove any old research assignments having the same user and project fields as newly stored research assignment, before storing a new assignment in the DB.
This could be happily implemented by an UNIQUE SQL constraint, but it seems that the Drupal way is to use fields, not direct access to DB. What could you advise: to do this with fields or to revert to plain SQL?
Also, could you show how to use transactions in this case? I don't want anyone to see a research assignment deleted just before it is inserted again with probably other fields.
Answer: When you use fields GUI to add fields to a content type, it is correct that the GUI do not let you set the UNIQUE constraint.
However, the fields created in this way is stored in the SQL database. They're in a table named field_data_ followed by the machine name of the field.  So it is possible to add the UNIQUE constraint to any field after it has been created by means of an SQL ALTER TABLE statement, e.g.:
ALTER TABLE `field_data_field_user` ADD UNIQUE (
  `field_user_value`
)

As long as you only do this to fields that you've created yourself, I do not see any problems.
However, it is also possible to create the content type yourself as an entity, using the Drupal Schema API.  If you've business logic that isn't part of the fields repertoire (such as deletion of an old research assignment before inserting a new one), it is probably better to use the Schema API istead of fields to create the content type.  This is still not plain SQL (the Schema API expects you to declare the database tables in a structured array, and Drupal DB API is using PDO-like objects to interact with the database), but it has the same power and flexibility as plain SQL.
Note that declaring the user and project fields unique will not get the old assignment automatically deleted before the new is inserted.  All this constraint does is to reject operations that does not satisfy the contraint.
To use transactions in Drupal, you just call $txn = db_transaction(); in your own module. The transaction will remain open for as long as the variable $txn remains in scope. When $txn is destroyed, the transaction will be committed.


Can you explain with examples what exactly is mathematical modeling to a person that is not very good with math?

Question: Please explain with examples what exactly is mathematical modeling to a person that is not very good with math.
Comment: https://www.youtube.com/watch?v=xHtsuOB-TPw
Answer: The simplest kind of model can be a single equation. For example, a linear equation that roughly describes the relationship between the age of the population (say from 0 to 18) and average height. You input an age into the model and the average height for that age is the output.
There are many more models which are extremely complex but the principle is the same. Example: An aircraft structural model. Input flights loads into the model and the output is the individual loads in the structural members (frames, floor beams, wing spars etc). Engineers can then design these members to withstand the loads they carry.
Comment: Yes, mathematical in both a geometric representation of the structure and also in carrying the physical properties of each structural member. That is, the modulus of the material they are to be made from, their stiffness, and end fixity, You may have heard of the term "finite element analysis", well this is the kind of model they use to analyze the stresses and deflections of the structure.
Comment: So computer simulation of aircraft structural model is a mathematical model of that aircraft?


Does there exist a $1$-dimensional Noetherian domain whose set of all prime ideals has cardinality $\aleph_1$ ?

Question: Does there exist a $1$-dimensional Noetherian domain whose set of all prime ideals has cardinality $\aleph_1$ ?
What I know is that for $1$-dimensional Noetherian domain $R$, $\operatorname{max} \operatorname {Spec} R$, under Zariski subspace topology of $\operatorname{Spec} R$, is actually the co-finite topology. But I don't think that would help implying existence of any such domain with specified cardinality of prime spectrum.
Answer: Let $K$ be an algebraically closed field. Then the polynomial
ring $K[X]$ has $|K|$ prime ideals, namely $\{0\}$ and the
$\left<X-a\right>$ for $a\in K$.
There is an algebraically closed field of each infinite cardinality.


Divergence of a tensor in cylindrical coordinates

Question: I'm having a hard time finding a source with an actual derivation on this. I understand the divergence of a vector field in cylindrical coordinates. However, for a tensor, how do I go from this
$$\nabla \cdot \mathbf{S} = \left( \hat{r} \frac{\partial}{\partial r} + \hat{\theta} \frac{1}{r} \frac{\partial}{\partial \theta} + \hat{z} \frac{\partial}{\partial z}\right) \cdot \begin{pmatrix} S_{rr}  \:  \hat{r} \otimes \hat{r} + S_{r \theta}  \: \hat{r} \otimes \hat{\theta} + S_{rz}  \: \hat{r} \otimes \hat{z}\\
+ S_{\theta r}  \: \hat{\theta} \otimes \hat{r} + S_{\theta \theta} \: \hat{\theta} \otimes \hat{\theta} +  S_{\theta z} \: \hat{\theta} \otimes \hat{z}\\
+ S_{z r} \: \hat{z} \otimes \hat{r} +  S_{z \theta} \: \hat{z} \otimes \hat{\theta} +  S_{z z} \: \hat{z} \otimes \hat{z}\end{pmatrix}$$
to this
$$\nabla \cdot \mathbf{S} = \begin{bmatrix} \frac{\partial S_{rr}}{\partial r} + \frac{S_{rr}}{r} + \frac{1}{r} \frac{\partial S_{\theta r}}{\partial \theta} + \frac{\partial S_{zr}}{\partial z} - \frac{S_{\theta \theta}}{r} \\
\frac{1}{r} \frac{\partial S_{\theta \theta}}{\partial \theta} + \frac{\partial S_{r \theta}}{\partial r} + \frac{S_{r \theta}}{r} + \frac{S_{\theta r}}{r} + \frac{\partial S_{z \theta}}{\partial z}\\
\frac{\partial S_{z z}}{\partial z} + \frac{\partial S_{r z}}{\partial r} + \frac{S_{rz}}{r} + \frac{1}{r}\frac{\partial S_{\theta z}}{\partial \theta}\end{bmatrix}$$
?
I understand why the term $- \frac{S_{\theta \theta}}{r}$ appears (because the unit vectors are differentiated, which does not give zero), but I can not find the way to execute this calculation. I probably am missing some tensor algebra rules. How exactly is it done?
I care especially for the top row and the $- \frac{S_{\theta \theta}}{r}$ term. $\hat{r}$, $\hat{\theta}$ and $\hat{z}$ are supposed to be unit vectors, obviously.
Comment: Please use mathJax.
Comment: @KurtG. Now nicely formatted.
Comment: https://math.stackexchange.com/questions/833179/covariant-derivative-in-cylindrical-coordinates/4643576#4643576
Comment: @K.defaoite are there other ways to explain it? I am an engineer ...
Comment: Is what you're looking for the definition of dot product here? It ought to be $u\cdot(v\otimes w) = (u\cdot v)w$. The derivatives can be applied before hand with the product rule because the product rule applies to any bilinear product like $\otimes$. So for instance:
Comment: $$(\hat r\partial_r)\cdot(S_{r\theta}\hat r\otimes\hat\theta) = \hat r\cdot((\partial_r S_{r\theta})\hat r\otimes\hat\theta + S_{r\theta}(\partial_r\hat r)\otimes\hat\theta + S_{r\theta}\hat r\otimes(\partial_r\hat\theta)) = (\partial_r S_{r\theta})(\hat r\cdot\hat r)\hat\theta + S_{r\theta}(\hat r\cdot\partial_r\hat r)\hat\theta + S_{r\theta}(\hat r\cdot\hat r)\partial_r\hat\theta.$$
Comment: And in this case the second and third term drop out (derivative is zero) and in the first the unit vectors become $\left(\hat{r} \cdot \hat{r} \right) \hat{\theta} = 1\, \hat{\theta}$ therefore it ends up in the second row?
Comment: @Felix Yes, correct. This is precisely the second term you see in the second row.
Comment: @NicholasTodoroff You are a saint. I might seem like a simpleton to you now, but I find this amazing and it works! Can you please provide your comment as a normal answer so I can accept it? I am sure there will be more students who will find this question useful.
Comment: @NicholasTodoroff I'll wait a few more days and then answer the question myself, referring to your solution in the comments.
Answer: The dot product, as best as I can guess, is meant to be a left tensor contraction so that
$$
  u\cdot(v\otimes w) = (u\cdot v)w.
$$
Because the tensor product is bilinear the product rule for the derivatives $\partial_r, \partial_\theta, \partial_z$ is valid (where I've used the abbreviated notation $\partial_a = \partial/\partial a$). So for instance we can compute
$$\begin{aligned}{}
  [(\hat\theta/r)\partial_\theta]\cdot[S_{\theta\theta}\hat\theta\otimes\hat\theta]
    &= \frac1r\hat\theta\cdot\Bigl[(\partial_\theta S_{\theta\theta})\hat\theta\otimes\hat\theta + S_{\theta\theta}(\partial_\theta\hat\theta)\otimes\hat\theta + S_{\theta\theta}\hat\theta\otimes(\partial_\theta\hat\theta)\Bigr]
\\
    &= \frac1r\Bigl[(\partial_\theta S_{\theta\theta})(\hat\theta\cdot\hat\theta)\hat\theta + 0 - S_{\theta\theta}(\hat\theta\cdot\hat\theta)\hat r\Bigr]
\\
    &= \frac{\partial_\theta S_{\theta\theta}}r\hat\theta - \frac{S_{\theta\theta}}r\hat r.
\end{aligned}$$
This is the origin of the particular term $-S_{\theta\theta}/r$.


Need help calculating this limit for $\varepsilon \to 0$

Question: I used Gauss' identity to derive
$$(\ast) {1\over \varepsilon^{n-1}}\int_{\partial B(a,\varepsilon)} f dS = {1\over r^{n-1}}\int_{\partial B(a,r)} f dS $$
where $0<\varepsilon<r$ and $f$ is harmonic and $B(a,r)$ is the ball of radius $r$ around $a$.
Now I want to use this to prove that
$$ f(a) = {1\over |\partial B(a,r)|}\int_{\partial B(a,r)} f dS$$
Hence my question:

How to calculate  $\lim_{\varepsilon \to 0}{1\over \varepsilon^{n-1}}\int_{\partial B(a,\varepsilon)} f dS$?

Does $ \lim_{\varepsilon \to 0}{1\over \varepsilon^{n-1}}\int_{\partial B(a,\varepsilon)} f dS = f(a)$? (I'm not sure what the value of $|\partial B(a,r)|$ is in higher dimensions, I believe it is the surface are of the sphere so I am also missing a factor on the RHS)
Answer: When scaled by the surface area, you do indeed get $f(a)$. Roughly speaking this is because you have an average of values all of which are being made arbitrarily close to $f(a)$. Precisely speaking you can say essentially the same thing: given $\varepsilon$, find $\delta$ so that if $|x-a|<\delta$ then $|f(x)-f(a)|<\varepsilon$. Let $g(x)=f(x)-f(a)$. Then for $r<\delta$, the average of $f$ over the sphere is $f(a)$ plus the average of $g(x)$, which is at most $\varepsilon$ in magnitude. So the average of $f$ is within $\varepsilon$ of $f(a)$.
Scaling only by $r^{n-1}$ as you are, let $\sigma_n$ be the surface area of the unit sphere. Then I'll show the limit is $\sigma_n f(a)$ as follows:
$$\left |-\sigma_n f(a)+\frac{1}{r^{n-1}} \int_{\partial B(a,r)} f(x) dS(x) \right | = \left | -\sigma_n f(a) + \frac{\sigma_n}{|\partial B(a,r)|} \int_{\partial B(a,r)} (f(a) + g(x)) dS(x) \right | \\
= \left | -\sigma_n f(a) + \frac{\sigma_n |\partial B(a,r)| f(a)}{|\partial B(a,r)|}  + \frac{\sigma_n}{|\partial B(a,r)|} \int_{\partial B(a,r)} g(x) dS(x) \right | \\
\leq \frac{\sigma_n}{|\partial B(a,r)|} \int_{\partial B(a,r)} |g(x)| dS(x) \\
\leq \frac{\sigma_n \varepsilon |\partial B(a,r)|}{|\partial B(a,r)|}$$
provided $r<\delta$ as defined in the first paragraph.
Comment: What is the definition of "the average of $f$ over the sphere"?
Comment: @user174981 The integral of $f$ against the surface measure on the sphere, divided by the surface area of the sphere.
Comment: But the scaling in my derived equation isn't the surface area: or is $|\partial B(a,r)| = r^{1-n}$? It doesn't look right.
Comment: The surface area can be written as $\sigma_n r^{n-1}$ where $\sigma_n$ is the area of the unit sphere in dimension $n$ (for example, $\sigma_2=4 \pi$). So your expression converges to $\sigma_n f(a)$.
Comment: I don't understand: you seem to be talking about two different things which are both surface areas of the sphere. One surface area of the sphere is $\sigma_n r^{n-1}$ and another surface area of the sphere is $\sigma_n$?
Comment: @user174981 The surface area of a general sphere is $\sigma_n r^{n-1}$. The surface area of the *unit* sphere is $\sigma_n$, which is a dimension-dependent constant. One way to calculate $\sigma_n$ is given here: http://en.wikipedia.org/wiki/N-sphere#Volume_and_surface_area
Comment: Ok. That cleared up one confusion, thank you. But I still don't understand how to calculate the limit of $\varepsilon \to 0$....
Comment: @user174981 See my edit.
Comment: Thank you very much for the edit, I understand now everything! Thank you!
Comment: How do you avoid the problem that $\delta$ may force $r$ to be smaller than $\varepsilon$? We fix $0<\varepsilon0$. Then we compute the limit and extend $M$ continuously. Now $M$ is still constant, so $M(r) \equiv M(0)=f(a)$, which is what we originally wanted to prove.
Comment: @user174981 It may help to note that $M(0)=f(a)$ does not depend on the fact that $f$ is harmonic. We would still have that if $f$ were merely continuous. The fact that $f$ is harmonic just lets us say that $M$ is constant.


Apply and function a gsub in a lots of columns

Question: I am kind of new to R and I want to apply the gsub function in columns 6 to 12 in my data frame called x.  However, I am kind of stuck. I started using:
gsub("\\.", "",x[,c(6:12)])

But then it returned only a few rows.
Then, I tried to use this:
x1<-apply(x,c(6:12),function(x) gsub("\\.", "",x))

But I got the following error:
Error in if (d2 == 0L) { : missing value where TRUE/FALSE needed

Also tried this:
for (i in x[,c(6:12)])
{a<-data.frame(gsub("\\.", "",i))}

Does anybody have a tip or a solution?
It would also be great if someone showed me how to use an apply function and for.
Comment: check `?apply`. Perhaps , you need `x1<-apply(x[,6:12], 2, function(x) gsub("\.", "",x))`
Comment: Thanks a lot! I did not use the Margin argument.
Answer: Here is a solution in a data.table way. It worth considering when a table is big and time is a critical factor.
library(data.table)     # load library
x = as.data.table(x)    # convert data.frame into data.table
cols = names(x)[6:12]   # define which columns to work with
x[ , (cols) := lapply(.SD, function(x) {gsub("\\.", "", x)}), .SDcols = cols] # replace
x # enjoy results

Answer: Here is another solution. It returns all the columns of the original dataframe
library(dplyr)
mutate_at(x, 6:12, gsub("\\.", "", .))

Answer: Adding the solution using apply from the comments, as this one really helped me:
x1 <- apply(x[,6:12], 2, function(x) gsub("\\.", "",x))


What map is induced from evaluation by the tensor product?

Question: Note: I am not sure if this relates to representation theory, of which I am mostly ignorant -- the only result on Google was phrased in terms of representation theory, which I did not understand. These two questions on Math.SE (1)(2) might also be relevant?
In other words, please feel free to change the tags as appropriate.
Background:
Let $V$ be a vector space, I don't think it matters but just in case let's say finite-dimensional and over $\mathbb{F}\in \{\mathbb{R}, \mathbb{C}\}$. Then we naturally have a bilinear map $V^* \times V \to \mathbb{F}$ defined by $$e:(f,v) \mapsto f(v) \,,$$ which for obvious reasons I choose to call the evaluation map. Note in particular that any function formed by fixing the first argument is an element of $V^*$, and any function formed by fixing the second argument is an element of $V^{**}$. I am not sure if this is relevant.
Anyway, taking the tensor product $V^* \otimes V$, it follows from the universal property of tensor products that a linear map $V^* \otimes V \to \mathbb{F}$ is induced, call it $\tilde{e}$.
Because $V^* \otimes V$ is supposed to be canonically isomorphic to $\operatorname{End}(V)=\operatorname{Hom}(V,V)$, $\tilde{e}$ can be considered a linear function from the space of $n \times n$ matrices over $\mathbb{F}$ to $\mathbb{F}$ itself.
Question: What is $\tilde{e}$? Is it important or as important as $e$? What is significant about the fact that its kernel is $(n^2-1)$-dimensional? If anything? Can you point me to resources with which I could learn how to compute coordinate representations of this and similar maps?
It can't be determinant or trace, since those aren't linear functions. However, I am not sure how to compute it, since I am not sure how to compute a coordinate representation for $e$, and I don't think that $e$ can be written as the Cartesian product of two different maps, so as a result $\tilde{e}$ can't be written as the tensor product of two maps either, making it impossible to apply this similar question.
EDIT: What I said above was dumb -- the trace is linear, it's only the determinant which isn't linear. Is $\tilde{e}$ just the trace?
EDIT 2: Based on the fact that $\tilde{e}$ is the trace, its kernel is just $\mathfrak{sl}(n, \mathbb{F})$, the space of matrices with zero trace. The above (using the Rank-Nullity theorem on trace) is one way to derive its dimension; a perhaps more common way is to note that it is canonically isomorphic to the tangent space of $\operatorname{SL}(n, \mathbb{F})$ (space of matrices with determinant $1$), thus has the same dimension as $\operatorname{SL}(n, \mathbb{F})$, namely $n^2-1$ (the dimension of $\operatorname{SL}(n,\mathbb{F})$ following from the implicit function theorem and the fact that it is equal to $\det^{-1}(1)$).
Comment: Yes, it is the trace.
Answer: Fix dual basis $v_1,\dots,v_n$ and $\alpha^1,\dots,\alpha^n$ in $V$ and $V^*$ respectively.
If $T:V\to V$ then trace of $T$ by definition is
$$tr(T)=\sum_i T_i^i,$$
where $T^i_i$ are such that $T(v_j)=\sum_k T^k_j v_k.$
T can be equivalently written as
$$T=\sum_k\sum_j T_j^k\alpha^j \cdot v_k$$
But if you consider canonical isomorphism (as you mentioned) $\phi:V^*\otimes V\to End(V)$ then $T$ can be read as
$$T=\sum_k\sum_j T_j^k\alpha^j \otimes v_k.$$
Since by definition $\bar{e}(\alpha\otimes v)=\alpha(v)$ for any $\alpha,v$ then
$$\bar{e}(T)=\bar{e}(\sum_k\sum_j T_j^k\alpha^j \otimes v_k)=\sum_k\sum_j T_j^k\alpha^j(v_k)=\sum_k T^k_k.$$
And it is exactly the trace of $T$.
Comment: This is the type of detailed, rigorous calculation I was looking for -- I really appreciate it! I'll have to try this with some other maps now -- thank you for your help!
Answer: As I said in a comment it is indeed the trace map. I'd like to elaborate a bit more though.
This map $V^* \otimes V \to \mathbb{F}$ is in fact a map of $GL(V)$ representations, where the right hand side is the trivial representation.  Under the identification of $V^* \otimes V$ with $n \times n$ matrices with $GL(n)$ acting by conjugation, the fact that this is a map of representations is just the fact that conjugating a matrix does not change its trace. This is the unique such map up to a scalar.
There is also an adjoint map the other direction $\mathbb{F}^* \cong \mathbb{F} \to (V^* \otimes V)^* \cong V^* \otimes V$, called the coevaluation map. You may wonder what this map is in terms of matrices. It turns out this map is just the inclusion of the space of scalar matrices inside the space of $n \times n$ matrices. It's a map of $GL(n)$ representations as scalar matrices are invariant under conjugation. Again this is the unique such map up to a scalar.


Conditional Expectation of Square of a Gaussian

Question: Consider two Gaussian random variables $X\sim N(0,\sigma_X^2)$ and $Y\sim N(0,\sigma_Y^2)$ with known $E[XY] = \sigma_{XY}$. The question is how to find $E[X^2\mid Y]$ assuming $X$ and $Y$ are jointly Gaussian.
My approach: I thought of the following steps:

Find the pdf $f_Y(y)$;

Find the joint pdf $f_{X,Y}(x,y)$;

Now, we have the conditional pdf $f_{X\mid Y}(x\mid y) = f_{X,Y}(x,y)/f_Y(y)$;

$E[X^2\mid Y=y] = \int_{-\infty}^{\infty}x^2f_{X\mid Y}(x\mid y)\ dx$.

Is my approach correct? If yes, are there any other more elegant solutions?
Answer: Your approach is right if the distribution of $X$ and $Y$ is jointly Gaussian, rather than only each by itself being Gaussian. For example, suppose one had $Y = \begin{cases} +X & \text{if } |X|>c, \\ -X & \text{if } |X|<c. \end{cases}\quad$ Then it can be shown that $X$ and $Y$ are both Gaussian if $X$ is Gaussian, but the pair $(X,Y)$ is not Gaussian, since the probability that it is $0$ is positive.
Also, I would write $f_{X,Y}$ rather than $f_{XY}$ so as not to confuse it with the density of the product $XY.$
You don't need step 1.
The density is
\begin{align}
& c\cdot\exp \left( \frac{-1}{2(1-\rho^2)} \left( \left( \frac x {\sigma_X}\right)^2 + \left( \frac y {\sigma_Y} \right)^2 - 2\rho \left( \frac x {\sigma_X} \right) \left( \frac y {\sigma_Y} \right) \right) \right) \\[6pt]
& \qquad \text{where } \rho = \frac{\sigma_{X,Y}}{\sigma_X \sigma_Y} = \operatorname{cor} (X,Y). \\[4pt]
& \qquad \text{and } c = \tfrac 1 {2\pi\sqrt{\sigma_X^2\sigma_Y^2- \sigma_{X,Y}^2}}
\end{align}
To find the conditional density of $X$ given $Y$ we view the quadratic function of $x$ and $y$ just as a function of $x$ and complete the square:
\begin{align}
& \left( \frac x {\sigma_X}\right)^2 + \left( \frac y {\sigma_Y} \right)^2 - 2\rho \left( \frac x {\sigma_X} \right) \left( \frac y {\sigma_Y} \right) \\[8pt]
= {} & \left[ \left( \frac x {\sigma_X}\right)^2 - 2\rho \left( \frac x {\sigma_X} \right) \left( \frac y {\sigma_Y} \right) \right] + \left( \frac y {\sigma_Y} \right)^2 \\[8pt]
= {} & \left[ \left( \frac x {\sigma_X}\right)^2 - 2\rho \left( \frac x {\sigma_X} \right) \left( \frac y {\sigma_Y} \right) + \rho^2 \left( \frac y {\sigma_Y} \right)^2 \right] + \left( \frac y {\sigma_Y} \right)^2 - \rho^2 \left( \frac y {\sigma_Y} \right)^2 \\[8pt]
= {} & \left[ \frac x {\sigma_X} - \rho\cdot\frac y {\sigma_Y} \right]^2 + {} \underbrace{ (1-\rho^2) \left( \frac y {\sigma_Y} \right)^2}_\text{No "$x$" appears here.}
\end{align}
Things not depending on $x$ are in this context constants, so we have
\begin{align}
f_{X\,\mid\,Y\,=\,y} (x) & = \text{constant} \times\exp\left( -\frac 1 {2(1-\rho^2)} \left[ \frac x {\sigma_X} - \rho\cdot\frac y {\sigma_Y} \right]^2 \right) \\[8pt]
& = \text{constant} \times \exp\left( -\frac 1 {2\sigma_X^2(1-\rho^2)} \left[ x - \frac{\sigma_{X,Y}}{\sigma_Y^2}\cdot y \right]^2 \right)
\end{align}
This is a Gaussian density with variance $\sigma_X^2 (1-\rho)^2$ and expectation $\dfrac{\sigma_{X,Y}}{\sigma_Y^2} \cdot y.$
The expected value of the square of a random variable is the sum of its variance and the square of its expected value, thus it is
$$
\sigma_X^2(1-\rho^2) + \left( \frac{\sigma_{X,Y}}{\sigma_Y^2} \cdot y\right)^2.
$$
This can be viewed as a weighted average:
$$
(1-\rho^2) \sigma_X^2 + \rho^2\left( \frac{\sigma_X}{\sigma_Y}\cdot y \right)^2.
$$


Is there a reason why Clementi didn't suggest a change of finger for repeating notes? (Sonatina Op. 36 No. 2)

Question: I just began to learn Clementi Sonatina Op. 36 No. 2. The edition I have (ed. by W. Palmer) shows Clementi's original fingering suggestions. For many repeating notes there was no change of finger (see green circles). Is there a good reason for this? I did see elsewhere some teachers/sheetmusic suggest changing fingers, but surely Clementi knew better?

Similarly, in Sonatina No. 1, Second Movement:
Comment: On top of the Sonatina 1, there are these words: "The following note appears at the top of the first page of the original edition: 'N.B. Out of the VARIOUS MODES of fingering the SAME PASSAGE, the Author has preferred THAT, which appeared to him the best calculated to form the hand of a beginner.'"
Comment: Just to clarify: the edition you have is explicit in stating that these are Clementi's fingerings?
Comment: To add: occasionally, when the editor printed a fingering that is different from the original suggestion by the author, it is explicitly explained in a footnote which gives the original fingering.
Comment: Depending on how early in his career these were, some of these may have been written for harpsichord, in which case the fingering doesn't matter as much since they lack volume control based on how hard you press.  Clementi was among the earliest composers to write explicitly for piano, so the common techniques were still being developed, many by Clementi himself.
Comment: @Darrel Hoffman That didn't occur to me, indeed!
Answer: In every case where Clementi specifies no finger change, it is a weak beat (i.e., a half-beat) following be a strong beat. The one time he's explicit about changing fingers, it's on a strong beat followed by a weak (i.e., half) beat.
The convention of switching fingers on repeated notes is largely based on the idea that each finger gives a different nuance to the note. That idea supposes that using the same finger twice risks a robotic sound. However, Clementi may have felt that the rhythmic positioning of the repeated notes did not present a significant risk, except when the first note was the stronger of the two. I find this quite an ingenious observation on Clementi's part.
To back up this observation, I would point to measure 3, in which the first two Ds change fingers, but the same finger is specified on the second and third Ds.
This does beg the question of why Clementi leaves m. 7's second D unspecified. Here I suggest it's because since the first D is finger 1 and the third D is finger 3, he may have felt it sufficiently obvious that the second D would be finger 2. Clementi may also have felt that there was sufficient parallel to m. 3 that he did not need to re-specify the finger change.
Answer: My understanding is that generally changing finger on a repeated (or held) note is for two reasons:

to shift the hand position in preparation for what comes next
to play repeated notes rapidly

My thought in looking at this example was that none of the repeated notes are too fast.
Clementi's fingering examples in Art of Playing on the Piano Forte...

...show eighth notes as not too quick to require a finger change.
Of course there is a question of tempo. Your example is marked allegretto, but the Art of Playing example doesn't have a tempo mark.
I'm pretty sure Clementi, in the Art of Playing, was following the practice where note values were closely linked to general tempos. (The particular form was a general tempo indicator too.) So, eighth notes would be played in a fairly lively tempo. That's pretty vague by modern standards, but my point is don't worry about an extreme hypothetical, like eighth notes at presto 180 bpm. I suppose eighths up to around 120 bmp was the assumption for "not too fast."
Answer: A lazy student's interpretation would be that you don't change fingers when you don't need to do so.
To me it looks like all the places where Clementi suggests changing finger follow the pattern where a previous legato phrase ends, and there is a short pause where you switch to different finger. This fingering lets you position your hand for the next phrase and avoid thumb under fingering during the next legato part. Of course you will need smooth thumb under technique eventually, but this is a student piece and I'd interpret Clementi's note to mean that the suggested fingering is easiest for beginners.
I would interpret the fingering for bar 7 that it is not important which finger is used for second D as long as the third is on middle finger to prepare the next phrase.
Comment: @GrandAdagio Some beginners find finger changes difficult and counter-intuitive; others don't have a problem with it. There's no rule in that regard. More advanced players make the decision based on what allows them the greatest technical and musical command.
Comment: @GrandAdagio I would play it like Clementi suggests because it's easier that way, but I can't hear the nuances or robotic sound Aaron claims different fingerings have.
Comment: @GrandAdagio Those seem like good, well-considered decisions to me.
Comment: So (as far as finger change is concerned), does it make difference whether it's a beginner or more advanced playing? In other words, if I don't find changing finger difficult, should changing finger be recommended? (I'm trying to decide on which way to settle on :))
Comment: @ojs I claimed that's the idea behind it, not whether or not the effect actually happens.
Comment: @Aaron So I think I'll change finger when it makes it easier for me--e.g. in Measure 2, I'll change to fnger 4 instead of keeping 5, because 5-4-3 is tricker for the hand shape than 4-3-2 for that measure due to the black key F. On the other hand, in Measure 6, I'll keep the same finger 5, as it follows the upcoming direction of the hand movement. Hope my reasoning makes good sense?
Comment: @Aaron point taken. I'm not sure why the idea needs to be propagated if it's not necessarily true, but whatever gets the votes.
Comment: @ojs I guess I don't consider discussing Clementi's reasoning from a few centuries ago the same as propogating.
Comment: @Aaron I didn't realize that you were citing Clementi's (or anyone else's) reasoning. The other answer that actually references Clementi's writing sounds at least as plausible to me.
Comment: @ojs Agreed. That's a very good answer.
Comment: I up-voted to all of you--I got something from each of you. By the way, the same happened with Clementi's Sonatina No. 1: in its 2nd movement Andante, Measure 4, the last 3 notes FFF he had fingering 5-5-5, with a "(4)" printed above the middle "5", as an alternative. See the image I just added.
Comment: @GrandAdagio Thank you :) I can think of two explanations for that fingerings: The lazy student way to not play three successive notes on the weakest finger if you don't have to, and the rapid successive notes idea that even if that part is not fast, it is one legato phrase and switching fingers could help with playing the notes without noticeable pause between them. Or maybe that you could play the previous part of the phrase by more rolling your hand than pushing down and switching fingers allows continuing the rolling motion back and forth instead of pushing down.
Comment: @ojs I have trouble with that part (second image) either way as I continue to Measure 5 with finger 4 :(


Workflow and Organic Groups. How can you email the next state changer when content is posted?

Question: I would like to notify someone with editor permissions that a user has posted content in the workflow.  I found a token for the current state updating user ([workflow-current-state-updating-user-mail]]), but nothing for the next user in line.
I've seen reference to using rules to send email to all users of a particular role, but I need something more like 'send email to users of a role who belong to the current group'.
Is this possible?
Answer: Solved this by writing a module to create a custom token from a field added to the group_node_form.  I followed this tutorial, making adaptations for organic groups:  http://rbayliss.net/story/creating-tokens-drupal-6


javascript increment index in function

Question: I'm just trying to figure out how to create an index inside a function so I can keep track of the items its generating and I'm not sure how to do this seems like I should know this..
addLocation(options,location)

   funciton addLocation(options,location){
       $('#list').append('

'+location.label+'
'+
        '<input type="hidden" name="location['+x+'][lat]" value="'+location.lat+'" />'+
        '<input type="hidden" name="location['+x+'][lon]" value="'+location.lon+'" />'+
        '<input type="hidden" name="location['+x+'][loc]" value="'+location.loc+'" />'+
        '<input type="hidden" name="location['+x+'][label]" value="'+location.label+'" />'+
    '
');
   }

now normally you have a forloop to help you keep track of things but int this instance I'm not appending items in a loop and I keep getting an error saying x in undefined
appreciate any help you can give
thanks
Mike
Comment: What value(s) should x be in this example?
Comment: @Zack values should be an integer

@vicatcu i haven't defined x because I'm not sure how to  with out making x 0 all the time
Comment: @mcgrailm, do you mean that each time the function is called, `x` should be incremented?
Comment: @mcgrailm, clear enough now, I posted an answer...
Comment: @CMS not really I'm not sure how to call the function now
Answer: Since what you want is that each time the function is called, x should be incremented, you can store x in a closure, for example:
var addLocation = (function (){
  var x = 0; // store x
  return function (options,location) {
    x++; // increment x
    $('#list').append('

'+location.label+'
'+
    '<input type="hidden" name="location['+x+'][lat]" value="'+location.lat+'" />'+
    '<input type="hidden" name="location['+x+'][lon]" value="'+location.lon+'" />'+
    '<input type="hidden" name="location['+x+'][loc]" value="'+location.loc+'" />'+
    '<input type="hidden" name="location['+x+'][label]" value="'+location.label+'" />'+
    '
');
  };
})();

Comment: so then I call the the function by variable ? how are the parameters gonna make it into the function sorry never seen this done before
Comment: just call `addLocation(options, location)`.  The anonymous function with no parameters is there for the closure, the inner function that takes two parameters is what gets assigned to `addLocation`.
Answer: Declare x since in your above code it's not defined.
var x = 0;

Comment: Well you will have to define it as something I just put 0 because I don't know what value you are trying to derive. What are you basing the index off of, looks like `zincorp` has the right idea of what to base the index off of.
Answer: var x = $("#list>li").length + 1;

Comment: ok so lets say i have 4 li's x should be up to 3  then I add a fifth x goes to 4 i remove li3  and my x remains at 4
Comment: If the items get removed, this should still account for that as long as you are removing ``'s

javascript increment index in function


Unable to get /refresh to work with spring-cloud-client when server.port and management.port are different

Question: Below is the application.properties for a spring-boot application
spring.application.name=test-service
server.port=8080

management.port=8081
management.context-path=/admin

spring.cloud.config.uri=http://localhost:8888
endpoints.refresh.enabled=true
endpoints.restart.enabled=true

When I start the application, it contacts the config-server and loads the properties as expected.
I modify the properties on the config server and trigger a refresh on the application using
curl -X POST http://localhost:8081/admin/refresh

the API prints the name of the property that was changed.
When I do a get on the property, I still see the old value
curl -X GET http://localhost:8081/admin/env/{property_name}

I trigger a restart and it picks up the the new property value
curl -X POST http://localhost:8081/admin/restart

See the same behavior when I try to change the property value using
curl -X POST  http://localhost:8081/admin/env -d property1=123

When I try to get the property value I still see the old value
curl -X GET http://localhost:8081/admin/env/property1

When I change the management.port to 8080 (the same as the server.port) everything works as expected.
Is this behavior expected? Looks to me that it is modifying 2 different environments one for the server running on 8080 and the other running on 8081.
Answer: Could you please share your pom.xml and application main(Entry) file.No matter we can use different port. If you are able to get the changed properties in "/refresh" call then it should work in your application also. Are you sure you are using @RefreshScope in your bean?
Note : @RefreshScope will not work on @Configuration annotation.for further details please follow the documentation https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html.
Answer: Looks to be an issue with spring-boot.
EnvironmentEndpoint and EnvironmentMVCEndpoint are injected with 2 different Environments when the server.port and management.port are different.
The application environment has the updated value but when I get the value of a specific property EnviromentMVCEndpoint it does not reflect the right value in its Environment
@GetMapping(value = "/{name:.*}", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@HypermediaDisabled
public Object value(@PathVariable String name) {
    if (!getDelegate().isEnabled()) {
        // Shouldn't happen - MVC endpoint shouldn't be registered when delegate's
        // disabled
        return getDisabledResponse();
    }
    return new NamePatternEnvironmentFilter(this.environment).getResults(name);
}

@Override
public void setEnvironment(Environment environment) {
    this.environment = environment;
}


No recent Windows updates showing as applicable in WSUS

Question: I have setup a WSUS server on my domain, and have noticed that the most recent 'needed' updates release date was from August 2016.
The clients on my domain are all running Windows 2012 R2, and I have the following Products/Classifications selected:
*Products -All windows 2012 / 2012R2 products, except language packs
*Classifications - Critical, Security and Definition.
If I look at the list of all the updates, the WSUS sever seems to downloaded a lot of 2012 updates, they're just showing as Installed/not-applicable.
I'm concerned about this as the Windows Update that patched the WannaCry ransomware exploit was released in March, I would have thought it would show as needed.
Answer: Seems to have been due to the updates all being superseded by others. After installing the ~20 updates that were shown as applicable, it has now recognized all the new ones.


Changing a variable during a loop, Python

Question: So what I have is I am using Python's arcpy for ArcGIS.
I have several variables outside of my loop that point to text field in my ArcMap MXD document.
I then perform a loop that goes through each row of a buffer layer and I then select by location any point layers that are within said buffer.
I want to then take values from these point layers and put populate the text fields that were declared earlier.
My trouble is that I want to iterate the variables so that they point to a different text field each time the loop runs through.
My Code:
Pointing the text elements in my MXD document
HarbName1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName1")[0]
HarbName2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName2")[0]
HarbName3 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName3")[0]

The first loop
for row in rows_dfA_Asset:
   whereClause = '"HARB_CODE" = ' + str(row.getValue("Harb_Code"))
   arcpy.MakeFeatureLayer_management(dfA_Buffer, output, whereClause, "", "")
   arcpy.SelectLayerByLocation_management(dfA_Layer, "WITHIN", output, "", "NEW_SELECTION")
   test = arcpy.SearchCursor(dfA_Layer, fields="Harb_Code; NAME; CAPACACITY; HIGH_BERTH; HarbHiPerc; AvgBerth; HarbAvgPer; CLSD_DET; CLSD_NUM_M; Tot_Num_As; Num_SCH_As; PROV; GOV_SCORE; MAN_SCORE")

The second loop THIS IS THE ISSUE
for field in test:
    count = count + 1
    print count

    HarbName(count) = str(field.getValue("NAME"))

So it boils down to that I need to iterate the variable name which will match up with the global variable that points to be text element.
If anybody could think of way to do this it would be greatly appreciated. I know my code is not the greatest, I am not a programmer by trade.
Comment: Little confused as to what you are trying to do but from what I'm guessing you want a python dictionary.
Comment: @Andrew Is your 2nd loop nested inside the 1st one? If not, in the 1st loop you iterate over some data structure, compute a result (`test`) and then you throw it away, except for the last iteration...  Is this what you're really trying to get?
Comment: @gbofii Yes my 2nd loop is nested within my 1st one
Answer: I think the comment is on the right track.
What you want to do is restructure the following:
HarbName1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName1")[0]
HarbName2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName2")[0]
HarbName3 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName3")[0]

such as:
HarbName = { 1: arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName1")[0],
             2: arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName2")[0]
             3: arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName3")[0]
           }

Then the last bit would be along the lines of:
for field in test:
    count = count + 1
    print count

    HarbName[count] = str(field.getValue("NAME"))

Assuming that count is a zero based and matches the indexes defined above
Comment: You have one `+ 1` too much: after `count = count + 1` (or better: `count += 1`), `count` is `1`. With `HarbName[count+1]`, you start `Harbname`'s indexes with 2...
Answer: What you need for this is a list. You can then access each text element by your count. You need to change:
HarbName1 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName1")[0]
HarbName2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName2")[0]
HarbName3 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName3")[0]

To:
HarbNames = []
HarbNames.append(arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName1")[0])
HarbNames.append(arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName2")[0])
HarbNames.append(arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName3")[0])

Or, more compact:
HarbNames = [arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName1")[0],
             arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName2")[0],
             arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "HarbourName3")[0]]

Then, later, in your second loop, do HarbNames[count] (assuming count is zero-based):
for field in test:
    print count

    HarbNames[count] = str(field.getValue("NAME"))
    count = count + 1


I am unable to get data from one page to the next in listbox C#

Question: On my first page I have two listboxes for Movies and Snacks
They are called lbDisplay for Movies and lbSelected for Snacks.
I am currently using a "Go to cart" function that has the following code:
Session["lbSelectedMovies"] = lbDisplay;
Session["lbSelectedSnacks"] = lbSelected;
Response.Redirect("RingU6POSReview.aspx");

on the redirected page the two listboxes that I want the given values to transfer to are called lbRvMovies and lbRvSnacks
The page is called RingU6POSReview.aspx
Can anyone help me understand how to transfer the values when I redirect the customer?
I am taking a data from "lbDisplay" that is similar to.... "

"12 movies at $12.00 each."

and data from lbSelected such as "Snickers" "Twix" etc...
Comment: You have already asked this question just 15 mins back....http://stackoverflow.com/questions/22474880/trying-to-understand-transfering-listbox-values-items-with-sessions?noredirect=1#comment34188655_22474880
Comment: It wasn't answered correctly and was no long generating any response. I need help!
Answer: You can take the Session values as follows...
lbDisplay.Text=Session["lbSelectedMovies"].Tostring();
lbSelected .Text=Session["lbSelectedSnacks"].Tostring();

Please Mark as answer if it satisfies you..
Comment: Would this be on the initial page or in the page load of the redirected page?
Comment: Sorry,Use this in the redirected page...
Comment: Again, this is wrong. I have no idea how to do this.
Answer: In first page , store text of listbox in respective Session variables.
Session["lbSelectedMovies"] = lbDisplay.SelectedValue;
Session["lbSelectedSnacks"] = lbSelected.SelectedValue;

In second page ,
string selectedMovie = Session["lbSelectedMovies"].ToString();
string selectedSnacks = Session["lbSelectedSnacks"].ToString();

lbRvMovies.Items.Add(selectedMovie);
lbRvSnacks.Items.Add(selectedSnacks);


Passenger Apache - Forbidden You don't have permission to access this resource

Question: I'm stuck with Apache with Passenger configuration. I spent a few days looking for a solution. I follow this instructions. I'm getting this message in the browser:
Forbidden

You don't have permission to access this resource.

httpd.conf
# Use /usr/bin/node by default.
PassengerNodejs /usr/bin/node

<VirtualHost *:80>

    ServerName example.com

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteCond %{SERVER_NAME} =example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

<VirtualHost *:443>

    ServerName example.com

    # Tell Apache and Passenger where your app's code directory is
        DocumentRoot /var/www/example.com
        PassengerAppRoot /var/www/example.com/

    # Tell Passenger that your app is a Node.js app
        PassengerAppType node
        PassengerStartupFile app.js

    # Relax Apache security settings
    <Directory /var/www/example.com>
        Options FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
        Options -MultiViews
        # Uncomment this if you're on Apache >= 2.4:
        Require all granted
    </Directory>

    CustomLog /var/log/httpd/example.com_access.log combined
    ErrorLog /var/log/httpd/example.com_error.log

    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/fullchain.pem
    Include /etc/letsencrypt/options-ssl-apache.conf

</VirtualHost>

In /var/log/httpd/example.com_error.log I get:
[Sat Sep 04 07:24:00.473120 2021] [autoindex:error] [pid 907862:tid 139932467173120] [client 184.108.40.206:50337] AH01276: Cannot serve directory /var/www/example.com/: No matching DirectoryIndex (index.html,index.php) found, and server-generated directory index forbidden by Options directive

File rights
drwxr-xr-x. 5 root   root  102 Sep  3 23:53 ..
drwxr-xr-x. 8 root   root  163 Sep  4 07:11 .git
-rw-r--r--. 1 root   root   35 Sep  4 07:11 .gitignore
-rw-r--r--. 1 root   root  215 Sep  4 07:11 README.md
-rw-r--r--. 1 root   root  390 Sep  4 07:11 app.js
drwxr-xr-x. 3 root   root   21 Sep  4 07:12 node_modules
-rw-r--r--. 1 root   root 2655 Sep  4 07:12 npm-shrinkwrap.json
-rw-r--r--. 1 root   root  318 Sep  4 07:11 package.json
drwxr-xr-x. 2 root   root   22 Sep  4 07:11 public

curl http://127.0.0.1:3000/ returns Hello from Node.js/io.js + Connect.js!
Is there anything i should check?
Answer: This error means there is no default index file in the directory and that is clearly your case considering the files list you mentioned.
Solution:

Create a blank index.html page inside your public directory.
touch /var/www/example.com/index.html

Modify Options Directive inside httpd.conf and add Indexes:
Options -MultiViews -Indexes

Restart/reload httpd
service httpd reload

Edit:
Looking again at your httpd.conf, it seems you didn't point DocumentRoot to where your public directory is. Therefore:

Replace the current DocumentRoot to /var/www/example.com/public:

DocumentRoot /var/www/example.com/public

Change Directory path to the same path as well:

<Directory /var/www/example.com/public>

For more information, refer to Passenger website
Comment: Thank you for your answer. `index.html` files works fine, but Passenger runs the node app from `app.js` file.
Comment: Thanks for next suggestions. Unfortunately that's not it. I think issue can by realated with user rights. When I `curl http://127.0.0.1:3000/` from server I get page content but when I curl using domain `http://example.com/` I get **403 Forbidden**.
Comment: @neuroine well, you need a reverse proxy in that case not a root folder


What does R do when it plots the residuals of an AR fit?

Question: This is a question that's been bugging me for some time. The problem is this: I'm modelling the residuals of a model $f(t,\vec{\theta})$ with (what I think is) an AR process plus a white noise process via MCMC using a multivariate gaussian likelihood, where I model the covariance matrix elements equal to the elements of the autocovariance of an $AR(1)$ process plus i.i.d. white noise $\varepsilon(t)$, i.e., I model
$$r(t)=AR(1)+\varepsilon(t),$$
where $r(t)$ are the residuals of my model, $r(t)=d(t)-f(t,\vec{\theta})$, where $d(t)$ is the data. The thing is that I wanted a visual check to see if my fit was ok (because I'm pretty sure via some previous analysis that my residuals can be modelled efficiently by an $AR(1)$ plus white noise: I just want to show this result to other people "quickly").
What I did then was to analize the residuals via the arima and arma functions in the tseries library in R, and I saw that they actually fitted the residuals with the same coefficient as the MCMC result (within the error bounds and not taking in consideration the additive white noise, which has small variance anyways). However, I also saw that the arma function has some nice plots: it plots the "residuals" of the AR fit.
This is my question: how do you plot the residuals of an AR fit if an AR model is stochastic by nature? I think my question goes down to: how do you generate a realization of your model that matches your particular realization of the AR process (i.e. your data) in order to substract this realization from your data? The only way I could think of is to simulate various realizations of the $AR(1)$ model until one fits your data, but this seems like stacking the deck.
Answer: First, an AR(1) plus white noise is equivalent to an ARMA(1,1) process. So unless there is a good reason to formulate your model with a latent AR(1) process observed with error, I would suggest you use the simpler and equivalent model with ARMA(1,1) errors.
Then you can write
$$  d_t = f(t,\theta) + r_t$$
where
$$ r_t = \phi r_{t-1} + \gamma e_{t-1} + e_t$$
and $e_t$ is white noise. The residuals are
$$e_t = r_t - \phi r_{t-1} - \gamma e_{t-1}$$
which can easily be found recursively beginning with $r_0=e_0=0$ so that $e_1=r_1$.
If you wish to persist with the AR(1) + WN formulation, you can compute the errors via a Kalman filter.
Comment: Yeah, I actually have some physical reasons to model an $AR(1)$ model observed with errors. I believe that the $AR(1)$ process is the underlying signal, which is measured by pixel counts in a CCD. This measurement includes the arrival of photons, which is essentially a poisson process (which can be very well approximated in my case, where we have a high number of counts and exposure times, by a gaussian process).
Comment: What I'm actually doing is to model directly the covariance matrix via the autocovariance function of an $AR(1)$ process, which for the diagonal elements I sum the variance of the white noise (I derived this result some time ago and I think it makes sense).

The interesting thing about this approach is that we actually know the variance of the poisson process (we have a well understanding of the CCD response), so this also is a kind of double-check for the $AR(1)+WN$ model.
Comment: All this makes sense to me: does it makes sense to you? I'm really open to second opinions :-)
Answer: As Rob Hyndman wrote, you can use Kalman filter. Your
interest seems on estimating the unobservable AR(1) component $\alpha_t$  in
your model written as
$$
\left\{\begin{array}{r l}
   \alpha_{t} &= \phi \,\alpha_{t-1} +\eta_{t}\\
   y_t &= \alpha_t + \varepsilon_t
\end{array}\right.
$$
where $\eta_{t}$ and $\varepsilon_t$ are independent gaussian
noises. This is a standard linear State Space (SS) model.  I used
$y_t$ in place of your $r(t)$ to stick to more conventional notations.
Assuming $\alpha_t$ to be centered as here is not essential.
Kalman filtering techniques are dedicated to the recursive computations
of the conditional expectations
$$
a_{t|u} := \mathbb{E}\left[\alpha_t \,\vert\, y_1,\,y_2,\,\dots,\,y_u\right]
$$
for times $t$ and $u$.  The estimation task is called
prediction if $t>u$, filtering if $t=u$ and smoothing for $t < u$. The best estimation $a_{t|u}$ of
$\alpha_t$ uses all observations, hence is for $u=n$, the last
observation. This kind of estimation is often called signal
  extraction, which makes sense for a ``signal $+$ noise'' model as
yours.
In R, you can use e.g. the KalmanSmooth function (from the stats package),
 which will require
the SS model, and will give you the estimated or "de-noised" signal
$a_{t|n}$. To assess the effect of the measurement noise, then
simply display a time plot of the estimation, or of the estimate of
the observation noise $\varepsilon_t$. The best estimate of
$\varepsilon_t$ is its expectation conditional on the whole series
$e_{t|n}:=y_t - a_{t|n}$. The parameters to be estimated are $\phi$, $\sigma^2_\eta$ and also $\sigma^2_\varepsilon$, at least if you do not consider it as
perfectly known.  You can use the KalmanLike and optim to get Maximum
Likelihood estimates which will require a little program.
The problem of simulating the series $\alpha_t$ (or the noises)
conditional on the observations is known as ``simulation smoothing''.
Efficient algorithms have been designed by (among others) Durbin and
Koopman and seem to be available in the R package KFAS.
## parameters
phi <- 0.8; sig.eta <- 1; sig.epsilon <- 2
n <- 200
## simulate AR(1) + noise
y <- arima.sim(n = n, list(ar = phi, ma = numeric(0), sd = 1))
y <- y + rnorm(n, sd = sig.epsilon)
plot(y, type = "o", pch = 16, cex = 0.6)
## write the State Space model
mod <- list(T = phi,  Z = 1, h = sig.epsilon^2, V = sig.eta^2)
## initialisation part
P <- sig.eta^2 / (1-phi^2)
mod <- c(mod, list(a = 0, P = P, Pn = P))
## smooth
res <- KalmanSmooth(y = y, mod = mod, nit = -1)
lines(res$smooth, col = "orangered", lwd = 2)
legend("topleft", lty = rep(1, 1), lwd = c(1, 2),
       pch = c(16, NA), col = c("black", "orangered"),
       legend = c("y (raw)", "a (smoothed)"))


Check all checkbox in respective columns

Question: Without class or id, how to check all checkboxes in the same column? I have the screenshot of my table, HTML and jquery code below:
Screenshot
Notice that I use rowspan and colspan will it affect anything?

HTML code
When  I click on the Read checkbox, same column will be check. But when I click on the Create column, still it trigger the Read column's checkboxes.

            Modules (Page)
            Permissions

            Read
<input type="checkbox" class="big-checkbox">

            Create
<input type="checkbox" class="big-checkbox">

            Edit
<input type="checkbox" class="big-checkbox">

            Delete
<input type="checkbox" class="big-checkbox">

        Page 1
        <input type="checkbox" class="big-checkbox" value="r">
        <input type="checkbox" class="big-checkbox" value="c">
        <input type="checkbox" class="big-checkbox" value="e">
        <input type="checkbox" class="big-checkbox" value="d">

        Page 2
        <input type="checkbox" class="big-checkbox" value="r">
        <input type="checkbox" class="big-checkbox" value="c">
        <input type="checkbox" class="big-checkbox" value="e">
        <input type="checkbox" class="big-checkbox" value="d">

jQuery code
$("th p input[type='checkbox']").on("change", function() {
    var cb = $(this),          //checkbox that was changed
       th = cb.parent(),      //get parent th
       col = th.index() + 2;  //get column index. note nth-child starts at 1, not zero

    $("tbody td:nth-child(" + col + ") input").prop("checked", this.checked);  //select the inputs and [un]check it
});

Comment: The parent of your checkbox is a paragraph tag, not `th`
Answer: .parent() points to p, not th so...
Use .closest("th") to get the closest th

$("th input[type='checkbox']").on("change", function() {
    var $cb = $(this),
        $th = $cb.closest("th"), // get parent th
        col = $th.index() + 2;  // get column index. note nth-child starts at 1, not zero
    $("tbody td:nth-child(" + col + ") input").prop("checked", this.checked);  //select the inputs and [un]check it
});

            Modules (Page)
            Permissions

            Read
<input type="checkbox" class="big-checkbox">

            Create
<input type="checkbox" class="big-checkbox">

            Edit
<input type="checkbox" class="big-checkbox">

            Delete
<input type="checkbox" class="big-checkbox">

        Page 1
        <input type="checkbox" class="big-checkbox" value="r">
        <input type="checkbox" class="big-checkbox" value="c">
        <input type="checkbox" class="big-checkbox" value="e">
        <input type="checkbox" class="big-checkbox" value="d">

        Page 2
        <input type="checkbox" class="big-checkbox" value="r">
        <input type="checkbox" class="big-checkbox" value="c">
        <input type="checkbox" class="big-checkbox" value="e">
        <input type="checkbox" class="big-checkbox" value="d">

What you're still missing is:

if super checkbox is selected - but than one of the column checkboxes is deselected → the super checkbox should deselect as well.

but thumbs up!
Comment: Are you able to assist in the missing part too? I guess the logic is to check in the `tbody` if any checkbox was deselected, then it will make sure that the respective `th` checkbox deselect too. Am I right?
Comment: @LeaveCover yes. The logic is pretty similar, copy paste what you have and use the right selectors


Find the number of bit strings of length 9 that do not contain three consecutive zeros

Question: So, i recently have a math quiz test, i think my calculation is wrong or something, so here my calculation: so i calculate the posibility of bit strings length of 9 which contain 3 zeros which is 2^6, and i take 2^9 - 2^6 to have bit strings length of 9 which doesn't contain 3 zeros, so my answer for the above question is: 448, but there are four different answers for the question above:
a.
None of the other choices is correct
b.
274
c.
149
d.
230
and instead of choosing b which is the correct answer, i choose a, and when looking at the final answer i don't know why b is the correct answer.
Comment: Note that a string of three consecutive zeros could begin in any of the first seven positions.  You could use an inclusion-exclusion argument based on this idea, but it is easier to approach this problem by writing a [recurrence relation](https://math.stackexchange.com/questions/2129260/find-a-recurrence-relation-for-the-number-of-bit-strings-of-length-n-that-do-not?rq=1).
Comment: Please edit to explain how you did your calculation.
Comment: If you are unfamiliar with both Inclusion-Exclusion and recurrence relations, then I suspect that @N.F.Taussig is right.  However, if (for whatever reason), you wish to try Inclusion-Exclusion: see [this article](https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle) for an
introduction to Inclusion-Exclusion.
Then, see [this answer](https://math.stackexchange.com/questions/4427605/inclusion-exclusion-principle-what-is-1n1/4427645#4427645) for an explanation of and justification for the Inclusion-Exclusion *formula*.
Answer: Your calculation is wrong: the number of strings containing 3 consecutive zeros is not $2^6$.
It would be $2^6$ if we demand that zeros are present in some particular 3 positions. For example, there are $2^6$ strings that starts from 3 zeros. Then each of remaining 6 positions can be filled with either 0 or 1, giving us $2^6$ possible options.
If you want to use this approach to solve your problem, you need to consider many options: there are $2^6$ strings starting with 3 zeros; there are also $2^6$ strings with zeros on positions 2-4; there are also $2^6$ strings with zeros on positions 3-5... etc.
But you cannot simply sum this up to get the answer. Because these sets are intersecting. For example, the string consisting of 9 zeros belongs to all of these sets (it starts from 3 zeros, it has zeros on positions 2-4 etc.) and so the string '000000000' would be counted many times. There are also lots of strings which are counted several times in this enumeration, for example, the string '000100010' was counted twice.
The correct approach is to use recurrence relations. Here is the idea: let $A_n$ denote the number of strings of length $n$ which does not contain 3 consecutive zeros. Then $A_n = A_{n-1}+A_{n-2}+A_{n-3}$.
Why is that so? Well, there are 3 options for a string of length $n$ that satisfies our condition. It may ends with 1, or ends with 10, or ends with 100. These options are mutually exclusive, and also they cover all possible strings. So number of strings of length $n$ is the sum of numbers of these three kinds of strings (ending with 1, ending with 10, and ending with 100).
It remains to note that number of strings of length $n$ ending with 1 is exactly number of strings of length $n-1$ satisfying the original condition, so there are $A_{n-1}$ of them. Similarly, number of strings of length $n$ ending with 10 is exactly number of strings of length $n-2$ satisfying the original condition, so there are $A_{n-2}$ of them. And similarly for strings ending with 100, so there are $A_{n-3}$ of them.
But before using the formula $A_n = A_{n-1}+A_{n-2}+A_{n-3}$, we need to calculate several steps manually first.
So, let's count now:
$A_1=2$, there are strings '1' and '0';
$A_2=4$, there are strings '10', '01', '00' and '11';
$A_3=7$, there are all strings of length 3 except '000'.
Then $A_4=7+4+2=13$; $A_5=13+7+4=24$; $A_6=24+13+7=44$; $A_7=44+24+13=81$; $A_8=81+44+24=149$; $A_9=149+81+44=274$.
Comment: Sorry that i don't have enough reputation to upvote your answer, but thank you for clearing my confusion :)


Can I apply PCA to combine correlated variables into one variable?

Question: I have a dataset with 11 observations and 11 features. I want to use linear regression for estimating the coefficients by using OLS method.
I know it is not advisable to use linear regression with such a big number of features, comparing with the number of records. I have about 6 features that are highly correlated (correlation > 0.88). But I don't want to delete these columns, as I can loose information.
So can I use PCA on these 6 highly correlated features to transform them into one column? Then, I want to combine this column with other features and use linear regression.
Comment: What is the goal here?
Comment: The goal of research.  With a dataset so tiny, I don't see scope for any more than single-predictor regressions.
Comment: You mean the goal of research or the goal of applying PCA? The latter I already described in the question. The goal of research is to estimate the effect of governmental programs on poverty rates.
Comment: Why do you think so?
Comment: Can you find serious published research based on such tiny datasets and multiple regression? Sorry, but there is no good news on that point.
Comment: There is much scope for a question easier to answer here. Post all your data, say more about your goal and ask for advice. (In turn whether you'd be better off in an economics or econometrics or political science or sociology forum is hard to tell.)
Comment: I think you're right about the fact that I should ask for advice in political science forums. But, still, my question is still valid. I'm still curious, whether it works for high dimensional dataset with observations >100 and features = observations-1.
Comment: In order to properly identify a regression line/hyperplane from data general advice is to have some 15-20 observations per dimension. Something can be done with say 10 even though normally very imprecise, so you may just about get away with one predictor, as @NickCox wrote.
Comment: Your last comment raises a new question, and the short answer is the same. You need many observations for each parameter estimated. (It's the community that decides whether a question is valid....)
Answer: Technically you can use the first principal component of your predictors as the sole predictor (I don't think multiple regression with more than one predictor variable makes sense with 11 observations). However, the interpretation of this is then determined by the data, which determine the principal component. One thing I recommend in such cases is to think hard about how to create a meaningful index for what is relevant to you from your variables rather than using PCA to do it automatically, as the meaningful index will normally give you better interpretation. If however you think you need more than one predictor for what you want to achieve (like interpreting relative importance of the different variables), you are pretty much lost with 11 observations.
Generally, in regression problems where the number of dimensions is critically high compared with the number of observations, there are various techniques for dimension reduction and variable selection. PCA is one of them, but this is typically done in situations in which interpretation of the impact of single predictor variables is not a central issue. Also you are much better off in terms of information in the data with, say, 500 observations and 600 variables than with 11 and 11. Statistics is not magic. If you plot your data you should already get a feeling for how difficult it is to precisely locate the line with one predictor and 11 observations (unless residual variance is very small). Making statements about how more predictors work together to explain the observations, taking into account their correlation, is near impossible. Also any diagnosis of the model assumptions will be extremely unreliable if possible at all.
Comment: Well, I have only ONE variable, where interpretation is crucial, but the other ones are covariates. The relationship between the covariates and the target variable is already proven, so I don't need to interpret ones.


Problema com offset e limit MySQL

Question: Quando realizo uma consulta usando limit e offset o banco de dados retorna uma linha a menos do que deveria. Qual é o problema? Cache? Lock? Índice? Dados?
Por exemplo:
select distinct(id_table1) from table_1
join table_2 on table_2.id_table2 = table1.id_table2
where table2.campo = numero_qualquer
limit 100
offset 523800;

A query acima retorna 99 resultados invés de 100.
Se eu fizer a mesma query com limit 1 e offset 523842 retorna 0 resultados. Note que se eu aumentar o limite para 2 retorna 1. Não sei porque essa linha não é exibida ela aparentemente é contada pelo offset para pular mas não é exibida no resultado do limit.
As tabelas são em MyISAM, já reparei índice e dados e nada de funcionar.
Comment: vc está usando alguma ide? workbench? se executar em linha de comando o que acontece?
Comment: acontece a mesma coisa
Answer: O problema é que quando você faz um LIMIT com GROUP_BY ou DISTINCT o OFFSET passa a mudar de acordo com o agrupamento criado.
então o id 523800 não faz referencia a posição dele na TABELA e sim a posição dele no agrupamento, sendo assim quando o agrupamento foi criado, talvez  você não possua mais 100 registros à partir desse índice e sim apenas 99.
Tente aumentar o número do LIMIT para 200, ele continuará a ser 99.
Comment: conforme informei se o offset for de 523842 e o limit de 1 o banco retorna 0 registros se o limite for 2 retorna 1, portanto é nesse ponto que está o problema, se eu fizer um limit de 200 ele retorna 199 registros, a partir desse ponto em diante sempre é retornado um a menos, o select em questão tem mais de 3 milhões de resultados
Comment: Bom o que consegui analisar ontem foi isso, que coloquei como resposta, até fiz uma simulação com bastante dados aqui, porém realmente fica difícil sem os dados reais. =/

Problema com offset e limit MySQL