Inverse Sine and cosine

Question: $\arcsin(\cos(x))=1/2$
Find $x$.
I got $-1/2$ or $2\pi-1/2$, but I don't know the correct answer. I tried graphing unit circle.
Comment: Notice that $\cos(x) = \cos(2\pi + x) = \cos(4\pi + x) = \dots = \cos(2k\pi+x)$ where $k$ is any integer.  Also note that $\cos(x)=\cos(-x)=\dots=\cos(2k\pi - x)$.  So, if you find one such value of $x$, there are countably infinitely many other answers, all of the form $2k\pi \pm x$.  I.e., assuming your math was correct, both $-1/2$ and $2\pi - 1/2$ are correct, but is not the full list.
Comment: Yeah I know that. Now I'm only solving for one answer first. I got x= (pi -1)/2 this time.
Answer: To begin, first remember that the sine and cosine functions are just phaseshifted copies of eachother, but otherwise behave the same.  That is, $\sin(x)=\cos(x-\frac{\pi}{2})$ and that $\cos(x)=\sin(x+\frac{\pi}{2})$
$$\arcsin(\cos(x))=\frac{1}{2}$$
$$\arcsin(\sin(x+\frac{\pi}{2}))=\frac{1}{2}$$
$$x+\frac{\pi}{2} = \frac{1}{2}$$
$$x=\frac{1}{2}-\frac{\pi}{2}$$
So, one such answer is $\frac{1}{2}-\frac{\pi}{2}$.  Remembering that $\cos(x)=\cos(2k\pi \pm x)$ for every integer $k$, the full list of possible answers then is $2k\pi\pm(\frac{1}{2}-\frac{\pi}{2})$.
Answer: on the unit circle, look at the points $1/2, \pi/2 - 1/2$ in the first quadrant. the terminal points have  coordinates $(\cos 1/2, \sin 1/2), (\sin 1/2, \cos 1/2)$ mirror images on the mirror $y = x$. the $$\sin^{-1}(cos(1/2)) = \pi/2 - 1/2$$ because the terminal point corresponding to $\pi/2 - 1/2$ has the terminal point with the $y$-coordinate $\cos(1/2).$  other solutions are $1/2-\pi/2$ and you can add a multiple of $2\pi$ to either on of them to find all solutions.

Inverse Sine and cosine


Apply Image styles for migrated images

Question: I've done a drupal migration from drupal 6 to drupal 8.
While all files are migrated well, image styles are not applying on migrated images.
Image styles are applied to the newly added images.
I think I should have force drupal to generate image styles for migrated images, but I don't know how.
any help one that is appreciated.
PS. I read some information about Flushing image styles in drupal 7 but I couldn't find a proper way to use it in drupal 8 as a custom module.
Comment: The drush command for this is https://drushcommands.com/drush-8x/core/image-flush/
Comment: @Kevin Thanks for help, actually my shared hosting only supports drush 8.1.4 which is not compatible with drupal 8.4.3 so I cant use drush commands to solve my problem
Comment: and I don't think that flushing the images would have helped. All that does is delete the already generated images for that style so that new ones can be made, but no new ones were being made in the first place
Answer: To solve the problem I used the code below to force Drupal to create the image styles for my nodes:
<code>if ($entity->hasField('field_image') && $entity->get('field_image')->getValue() != null) {
  $image = \Drupal::service('image.factory')->get($entity->get('field_image')->entity->getFileUri());
  if ($image->isValid()) {
    $styles = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple();
    $image_uri = $entity->get('field_image')->entity->getFileUri();
    foreach ($styles as $style) {
      $destination = $style->buildUri($image_uri);
      $style->createDerivative($image_uri, $destination);
    }
  }
}
</code>
Here is the description, for each node ($entity variable) , first it checks if the node type has a field with machine name "field_image" and if this field has a value, then it checks if fileUri is a valid image, then if all conditions met, the code iterates on all image styles available and build that style for given image manually.
This code can be used as a cron job function, so on every cron job image styles of given nodes are created.
Comment: Actually I added a new custom module, there I used a batch process to repair all image styles of migrated nodes
Comment: where did you put the code? In a custom module? In a stand alone php file that loads drupal first?

Apply Image styles for migrated images


Check activity of authors during period

Question: I would like to have some sort of management system in the control panel where the management people can view the activity of the authors during a certain period.
So: A person with a certain role (management) must have a management tab in his administration menu where he can see the names of all authors + their amount of articles they have created during a certain period, where the manager can choose the start and enddate of this period.
Example:
Manager chooses:
1 september 2015 - 30 september 2015
and then he gets an overview of all authors and their amount of articles made that month.
Is there a module by any chance that already has this?
Answer: Well, to those who might wonder the same as me: apparently there is no module yet that does this, which surprises me, as it seemed such a basic request to me.
So you have to do it yourself.
For those who need it, I hereby give you my own solution, but ofcourse other possibilities are available.
I made this with a combination of views & total control module.
Total Control gives you a nice dashboard which you can fill with views. For the view itself, you just have to use the aggregate function and a relationship.

Relationship: 'Content: Author'
Use AJAX: Yes (this is to make sure filters can be exposed in blocks)
Use Aggregation: Yes
Make a table with following fields and filters:

Field: '(author) User: Name'
Field: 'Content: Nid' (Aggregation: Count (Distinct))
Filter: 'Content: Post date' => Exposed

extra fields are possible, like I have also added the date of the last article that person made in this specific period, which could also be handy for activity monitoring. To do this, use the field 'Content: Post date' and add the aggregation setting MAX to it.
Hope this helps others in case they are looking for something similar that is easy to maintain, without having to invent things that already exist.

Check activity of authors during period


Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed

Question: I have a similar situation as described here but I use a $http call to get my data to build a infinite scrolling table,
and get:
Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys. Repeater: i in items, Duplicate key: string:b, Duplicate value: b

source code is here
this is the source code that i followed and it worked
i need help in this situation!
thanks
Answer: You have a few issues in your jsFiddle. As noted above by @Manjesh V, you need to add track by $index, giving you:

{{i.name}}

Also, as mentioned by @sMr, you need to add $http module to the directive.
Finally, your jsFiddle links to a really old version of AngularJS, v1.0.0. You can add 1.2.1 and it will work.
Answer: Duplicate Items in the array, If you want to ignore use track by $index

{{i.name}}

Answer: it seems to be you have not passed the $http module to your main controller.
function Main($scope,$http) {
    $scope.items = [];

        $scope.loadMore = function() {
        $http.get('/echo/json').
                success(function(data, status, headers, config) {
                    // this callback will be called asynchronously
                    // when the response is available
                    $scope.items += data;
                }).
                error(function(data, status, headers, config) {
                    // called asynchronously if an error occurs
                    // or server returns response with an error status.
                    console.log("call failed");
                });
    };
    $scope.loadMore();
}

working demo


Functional harmony: extending the minor triads in Minor Swing

Question: I was having a go at this tune Minor Swing which solo sections follow this utterly simple changes:

I've been soloing over that using a backing track and also I've been playing simple voicings of the chords on the keyboard.
My main question is: what are your thoughts with regards to using Am6, AmΔ or Am7 instead of Am; Dm6, DmΔ or Dm7 instead of Dm?
For context, below is an attempt to describe my own limited knowledge about the matter (likely innacurate and/or wrong).

Theory seems to say that, from a functional harmony standpoint, the 4 (I'm including the m triad) are pretty much equivalent and interchangeable (with the exception of the "major II-V device", where the II is a m7).
Fake book music sheets seem to agree. However, implications related to aesthetics (rather than functionality) seem to exist: you are likely to use m7 in Summertime, but m6 in Autumn Leaves.
As I play the chords, I can tell each of them has a distinct sound.
I realise their use affects voice leading (for example, in Minor Swing, the tone B in Dm6 in measure #10 moves to the tone C in Am) but I don't know whether this has any relevance.

Side questions:

To what extent are these chords really "equivalent" in practical use?
You choose between them based on what?
I've seen them coexist in a given tune, but to what extent do you actually mix them? For example, in a given tune in the key of Am, you can find Am6, then Gm7 as part of a II-V to F. However, would it sound odd if, with voice leading in mind, in Minor Swing, I did:
Answer: The Django recording of this has pretty much all m6 and m6/9 chords being played by both piano and guitar. Is this your own chart/transcription or is it from a book?
That aside, if you hand this chart in its original form to experienced jazz musicians they will automatically and instinctively play some or all of the chord extensions and alterations you mentioned both in their comping and their soloing. The minor chords will become m9 or m6, the 7th chords will become b9, #9 or altered. Dorian modes or ascending melodic minor scales may be substituted for natural minor scales, etc. This is done creatively, spontaneously, and most importantly collectively, with players listening to each other to see what is being done and reacting to it. If a comping pianist hears a soloist playing B naturals on a Dm he might react with Dm6. If he hears lots of G's and B's on an Am chord he will probably play Am9. When it's his turn to solo he will choose what suits his ear and taste.
Bottom line, don't feel like you have to compose a set of more advanced changes but by all means try them out. Experiment, listen and react to what others do. Discover what sounds good to you. Don't worry much about analysis and function, this is simply a minor 1-4-5 tune.
Answer: Since this is blues/jazz, ordinary theory doesn't necessarily sit happily here.
Am maj7 is only really valuable as a stepping stone chord from Am to Am7.
Using Am6 can work, but it's somewhat dependant on what's in the melody.
Am7 works most of the time - particularly in the bar preceding iv (Dm).
Dm6 is a re-voiced Bm7♭5, so leads well to E or E7.
In a minor blues the V chord works well as 7♯9. A sort of melange of major and minor - aka Hendrix chord.
Choose between them based on what? Based on how I feel at that moment, listening to what's going on at that moment and prior.
Am9 and E9 both work well too. As would A7 (or A9) before moving to Dm.
Just a few random ideas! Use of ears is always recommended, and often works better than consideration of theory - especially when recommended by someone else...
Comment: Am maj7 is valuable everywhere! :) I think the only right way for the OP is to try things and develop a taste.
Comment: @piiperiReinstateMonica - Personally, I haven't quite reached that point where I would be inclined to put m maj7 in 'everywhere', or even 'anywhere'. Do put it in 'somewhere' but am still quite choosy! Probably to do with what the rest are doing - it won't fit well in some cicumstances - imo... And voicing, as ever, is pretty important, with a chord like that.
Comment: I mean, m maj7 doesn't have to be out of the question in any genre. The maj7 can be used for its color in itself, and the voice-leading pressure used as a separate independent "feature". A bit like in blues tradition, dominant seventh chords are used as color, not letting the tension "properly" resolve. But yes, it is very tempting to move the maj7 voice to either direction.
Comment: It's not just a passing chord, some of the best spy movie themes end with a m maj7 chord, actually usually a m 6, maj7, 9. James Bond, The Pink Panther.
Answer:
what are your thoughts with regards to using Am6, AmΔ or Am7 instead
  of Am

My thoughts are: it's completely a matter of taste. When I was younger, so much younger than today, I used a lot of m7, but now it's more often mmaj7 or m6.
Answer: For the rhythmic guitar of Minor Swing for instance, it is cool to play Am6 and Dm6 (and E7#9), see this video.
Why? Because their dissonance really fit the restlessness of the tune.
Consonance, dissonance, dynamics, timbre, tempo and rhythm changes, modulation...they are just colours of your palette. Learn from the artists you like, and use such tools to express your feelings. Do not feel constrained.


Form API #states with #default_value?

Question: I have a form which has a select with a #default_value which get's set when the user has errors/has filled out the form. Then I have two fields with a #states that show them when the select has a value.
So basically
$checkout_form['wrap'] = array(
  '#type' => 'select',
  '#title' => t('How many item(s) do you wish to have wrapped?'),
  '#default_value' => $default_giftwrap_amount,
);

Then I have a fieldset and a textarea with
  '#states' => array(
    'visible' => array(
      ':input[name="wrapping[wrap]"]' => array('!value' => ''),
    ),
  ),

which make those two fields show when ever something is selected.
But my problem is when the #default_value is say 400 (ie something is selected) it doesn't show the fieldset and textarea, only when you select something.
Is there anyway for it to check on page load as well as onchange?
Answer: Your default value in the select element must be the array key of your values not the value it self.So i set an empty "value" and check for that.
That said i got this working like:
$form['wrap'] = array(
  '#type' => 'select',
  '#title' => t('How many item(s) do you wish to have wrapped?'),
  '#options' => array('--', '1', '2', '300', '400'),
  '#default_value' => 2,
);
$form['textarea'] = array(
  '#type' => 'textarea',
  '#title' => t('How many item(s) do you wish to have wrapped?'),
  '#states' => array(
    'visible' => array(
      ':input[name="wrap"]' => array('!value' => 0 ),
      ),
    ),
  );

In the example above my default value is 300.
The state visibility will work onload and onchange and will show the textarea only when the value is the empty value (0 => '--')

Form API #states with #default_value?


WPF DataGrid: Resource per row?

Question: Is there a way to xamly set a StaticResource per row in a DataGrid accessing it from all the columns?

UPDATE
My aim is to have 3 ComboBox columns, while only the last one is actually bound to the rows item.
For instance, the DataGrid represents a list of Items. I have Category->Vendor->Style->Finish ComboBoxes, but those are only for navigation assistance, in fact, the Item class has only a 'Finish' relationship.
So if there coulda been a StaticResource per row, I could set the ItemsSource & IsSynchronizedWithCurrentItem props of the ComboBox and this would work automatically.
Thanks a lot.
Answer: Technically, I guess you could because the row is in the visual tree. But what are you trying to achieve? There's probably a better way.
Comment: I wanted to do 2 cascading ComboBoxes.
I put them both on 1 DataGridTemplateColumn setting the CollectionViewSource in the DataTemplate.Resources.
Comment: But it would still be a great idea to have a resource for each row, because now I have all the cascading comboboxes in one column, if i would be able to have a resource per row, i could separate them between columns.
Waiting to hear forward.
Answer: Can you elaborate a little bit more on what you are trying to achieve ?
I'd think it is simpler to have one staticResource which represents a Collection (See ObjectDataProvider type) and then bind the DataGrid's ItemSource property to it in XAML.
Comment: I want to treat each row with its custom DataSource, take a look at what I did, it's on Josh's response.
Comment: But it would still be a great idea to have a resource for each row, because now I have all the cascading comboboxes in one column, if i would be able to have a resource per row, i could separate them between columns.
Waiting to hear forward.
Comment: @Shimmy : it looks like you're using the wrong control here for the job. Are you looking for a series of comboboxes where the choice in one filters the options for the next combobox in sequence ?
Comment: I have a DataGrid that shows 'Product' entities.
I included these cols: Vendor, Style, Finish; while the chain works this way: Product.Finish.Style.Vendor.
So I want to make in each of these columns a combobox that cascades according to the previous selected value in the prev column.
Comment: @Shimmy - So if I design the Product class to expose Properties called Finish, Style and Vendor. Bind each column of the datagrid to the appropriate property. The properties themselves lookup the deciding sibling property and return the right list for the dropdown in the combobox e.g. Style would see lookup the value of this.Finish in an internal hashtable to get the list of allowed choices for the Style value. Would that work for you ?
Comment: Please take a look at: http://stackoverflow.com/questions/3203416/accessing-control-between-datagridcells-dynamic-cascading-comboboxes
Answer: You can certainly set it at the DataGrid level like this:
xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

<data:DataGrid x:Name="..."  ItemsSource="{Binding ...}" >
    <data:DataGrid.Resources>
    </data:DataGrid.Resources>
</data:DataGrid>

I would presume you can set it at row level if you define a row template?
Comment: No, I want it to be set per row, not per the Control container. My aim is to set the CollectionViewSource within the row according to a value the user selected for this row.
Answer: What I did and solved my issue (no guarantees for whether this is the proper way), I am using a UserControl as the DataTemplate content, having the resource declared in the UserControl, then it gets initialized each time.
Another thing I havn't tried is setting x:Shared attribute to false, which I believe is supposed to help with the issue.

WPF DataGrid: Resource per row?


Low-fret string buzz

Question: So Ive had an EVH Striped Series guitar for about an year now and I'm now starting to learn about truss rods and intonation and I've noticed some things recently.
First, the low frets seem to buzz more and I think the neck might be slightly bowed and thats what's causing it, but I'm not sure. To test this I used the d-tuna on the bride which drops the low E down to Drop D (pretty cool btw) and playing that string open buzzes quite a lot. Is that normal, or does the truss rod, action, or intonation need fixing? I will attach some photos and please tell me what you think.
Comment: Also BTW, the photo of the neck is a bad one, I had to balance it between my legs to take it, so the guitar really isn't standing very straight.
Comment: When I got married, I wouldn't dare use the d-tuna on my bride...
Answer: Looks to me like the neck could do with less relief, as the action is rather high, but, the nut seems a bit low, making the first couple of frets appear too high, which must be where the buzz is coming from. Dropping the tension on the low E will exacerbate the problematic buzzing, as the string isn't as tight as normal. Even tightening the truss rod - which will help the action - won't solve the lack of space between lowest frets and strings, and raising the saddles won't help the action - unless you want to play slide!
Comment: Honestly, it's a million times easier when I have the instrument in my hands, as I can see and feel what's happening. Right now, if you value the instrument and are not happy potentially wrecking it, take it to a luthier. Otherwise, try a sliver of metal or hard plastic as close to the nut as possible, to raise the strings enough so the buzz disappears. The thickness will give a good indication as to how much the nut needs raising - if that's the problem. Bear in mind that the top of that material will be equivalent to the bottom of the slots in the nut.
Comment: It's a locking nut, wouldn't that affect it? Also, are you sure that adjusting the truss rod, action, or intonation wouldn't help? I'm pretty sure none of those things were ever done.
Comment: I'm not sure about anything, as I only have a few pics to go on.Intonation certainly won't help fretbuzz, obviously action and truss rod may. Looking at what I can see, my suggestions stand.
Comment: Ok. I'll give adjusting the action a go.
Comment: The neck is too straight. You may follow what have been explained in this video to setup your guitar correctly. https://youtu.be/aAyDdbNldig
Answer: I had exactly the same problem on my Ibanez, the solution is to bow the neck in the right way (probably up-ways) to adjust it.
Anyway you are always gonna hear a light buzz when playing the strings clean, it's caused by the low action and the jumbo frets.
If you are thinking of adjusting the neck, I would go to an expert, because you really risk to damage the guitar if you don't know what you do
Comment: upvoted you as there is no reason you were down-voted. There is too much relief, the truss rod is pulling harder than the strings.

Low-fret string buzz


Is table tennis based on tennis?

Question: According to Wikipedia, tennis was first played sometime between 1859 and 1865, and table tennis was first played in the 1880s.
I tried to do some research online, but couldn't find an answer. Was the invention of table tennis based on tennis?
Answer: Most sources, including the history presented on the website of the International Table Tennis Federation (ITTF), agree that the game was developed in the 1880's as a variant to the already-popular lawn tennis, which in turn derives from so-called "real tennis", an earlier game, hailing back to medieval times, whose name is derived from French "Tenez!",
meaning "Hold!", "Receive!" or "Take!".
The popularity of real tennis had declined during the 18th and early 19th centuries, but lawn tennis, the game we play today, was on the rise. The Wimbledon tournament was started in 1877, several years before table tennis' purported origins, showing that the sport was enjoying high popularity at the time. However, it was not the only racket-and-ball game played; also popular like stické, rackets and squash, which could certainly have inspired table tennis as much as lawn tennis did.
However, a good indicator that lawn tennis was the real source of inspiration lies in the name itself. Table tennis was known under many names, again according to the ITTF's website. These included "ping pong", "gossima", "whiff-whaff", "pim pam" and "pom-pom", most of which are onomatopoeic, but also "table tennis", "indoor tennis", "parlour tennis" and "tennis de salon", the large variety of names involving "tennis", combined with the lack of names like "table squash" or "parlour rackets", are a strong indicator that tennis, the then-popular lawn tennis rather than the antiquated real tennis, was the strong driving force behind the invention of the game.
Answer:
The game originated in England during the 1880s, where it was played among the upper-class as an after-dinner parlour game. It has been suggested that the game was first developed by British military officers in India or South Africa who brought it back with them. A row of books was stood up along the center of the table as a net, two more books served as rackets and were used to continuously hit a golf-ball from one end of the table to the other.

So imo, yes it is based on tennis since those military guys probably knew what the tennis was.
Hodges 1993, p. 2
Letts, Greg. "A Brief History of Table Tennis/Ping-Pong". About.com. The New York Times Company.
WashingtonPost.com.
Comment: Your answer doesn't actually bring any proof, just an unsourced quote about the origin of the game, and an unsubstantiated claim that the military officers "probably" knew about tennis.
Comment: At least i tried to answer it instead of complaining like you...
Comment: Sorry gusy, I am new to Stack Exchange and this was my first answer I hope it's better now
Comment: He's not complaining, he's explaining how SE works. This is a Q&A site, not a forum or discussion site. We have different criteria for both questions and answers here and it's part of what makes this such a nice corner of the internet. He might have been nicer about it, I agree. So I'm trying to make up for that by providing more information. Try adding the source for your quote at least? And welcome to StackExchange!
Comment: I was a bit snipey, I agree, and I apologize for that. I also fully agree with @gomad's explanation.
Comment: @AvnerShahar-Kashtan it's no problem. I hope Semiru comes back and updates the answer. He's been back once already to comment, so I have confidence he will.

Is table tennis based on tennis?


sql server 2008 express changing version (not edition!)

Question: I think what I have right now is SQL Server Express 2008 with SP1. What I am trying to install is SQL Server Express 2008 with Advanced Services (and then maybe get SP1 separately on my own).
How should I do this so that I don't lose my data already in database? I don't have much going on in Master. Only my own databases.
edit: should I just run SQL Server Express 2008 with Advanced Services installation and things will get sorted out on their own?
Answer: When upgrading MS products you should use always use a version that is equal or higher than the current version. Most of the time the installation program would check the installed version and refuse to install if "downgrading" is not supported, so you could try.
In all situations, the best option for not loosing data is making a full backup before the installation. It sounds obvious, but it's the best option.
In your case since it seems that there isn't a bundled package of Advanced Services SP1, I would uninstall the current version, install advanced services and then upgrade to SP1 (if possible always use the latest SP).
Other option is upgrading to Express 2008 R2, but note that R2 is a different version, so after upgrading you can't restore backups from 2008 R2 into another server that runs "just" 2008 (for some reason SQL Server never provided a method from restoring/importing a backup from a higher version).


Trying to call callback after S3.putobject

Question: kinda new to aws and javascript and i am trying to achieve the following, uploading a photo using binary support and on my return value adding status paramater to be later used in API Gateway
var params = {
  Key: key,
  Body: image,
};
this.s3.putObject(params, function(err, data) {
  if (!err) {
    callback(null,{newValue: "This is my test value"});
  }
});

the code works fine if I comment out the callback line, but crashes otherwise.
 Any tips on how to fix it will be greatly appreciated
Answer: The call structure should be as mentioned over here
putObject(params = {}, callback)

Your code can be:
var params = {
  Key: key,
  Body: image,
};
var newValue = ""
this.s3.putObject(params, function(err, data) {
    if (!err) {
        this.newValue = "This is my test value";
    }
});

Here 'function(err, data) {}' represents 'callback'
Answer: Rajana is correct. To add on this: this is a core functionality of JS (or functional programming). The putObject function accepts two parameters: params, an object containing parameters and callback which is a function that is executed inside the putObject function when completed. So you don't call callback, putObject calls callback.
So you create a function, and it as a function argument of the putObject function. So this would also work:
const params = { some: 'params'};
const fn = function(err) { console.log(err); }
this.s3.putObject(params, fn);

But you can also define the function in the argument (making it an anonymous function), what you did in your example.
Comment: yea but my issue is that i want to return a value back to an end point, how can i pass a value on this callback so it will be part of the response.
Comment: you can't do that, but you can use how JS using scoping. This works:

`const x = 'hi';
s3.putObject(params, function(){
   console.log(x);
}`

See https://scotch.io/tutorials/understanding-scope-in-javascript for more info. Basically if you define a variable outside the function, you can use it inside the function.


Localization of Coordinate Rings: $\mathbb C[V_f] = \mathbb C[V]_f$.

Question: Let $V\subseteq\mathbb C^n$ be an irreducible affine variety, then the coordinate ring
$$\mathbb C[V] = \mathbb C[x_1,\dots,x_n]\big/\mathbf I(V)$$
is an integral domain. Let $f\in\mathbb C[V]\setminus\{0\}$, then we can define the localization
$$
\mathbb C[V]_f = \left\{\,g\big/f^\ell \in \mathbb C(V)\,\big|\, g\in\mathbb C[V], \ell\ge 0\,\right\},
$$
where $\mathbb C(V)$ denotes the field of fractions of $\mathbb C[V]$.
I want to proof that $\mathbb C[V]_f$ is the coordinate ring of the principal open subset
$$V_f = \left\{\,p\in V\,\big|\, f(p)\neq 0\,\right\}.$$
We can see $V_f$ as an affine variety by identifying it with
$$\widetilde{V_f} = \mathbf V(\mathbf I(V)+\langle gy-1\rangle) \subseteq \mathbb C^n\times \mathbb C,$$
where $y$ is $(n+1)$th coordinate, $g\in\mathbb C[x_1,\dots,x_n]$ represents $f\in\mathbb C[V]$ and the projection $\mathbb C^n\times \mathbb C\to\mathbb C^n$ maps $\widetilde{V_f}$ bijectively onto $V_f$. Then
\begin{align}
\mathbb C[V_f] &\cong \mathbb C[\widetilde{V_f}] = \mathbb C[x_1,\dots,x_n,y]\big/(\mathbf I(V)+\langle gy-1\rangle)\\
&\cong \mathbb C\left[x_1,\dots,x_n,1\big/g\right]\big/\mathbf I(V)\\
&\cong \left(\mathbb C[V]\right)\left[1\big/f\right] \cong \mathbb C[V]_f.
\end{align}
Is this reasoning correct? I'm not sure everything done in the last chain of isomorphisms is rigorous.
Answer: Yes it's correct.
For details see, for example, these notes definition 1.13 and the example right before definition 1.16.
Comment: Why $A(X)_{f} \cong K[x_{1}, \dots, x_{n},y]/J$ in the notes? I know that $R_{f} \cong R[X]/(xf-1)$ in general, but I don't see how this finishes.


Can a suspension resolve by leap downwards?

Question: Here is an example of what I mean. In the below excerpt from Coldplay's "Don't Panic", the note E is accented and comes after the note F in the previous bar. I have not included this as it is on another page and it is irrelevant. If the E resolved by step downward to D it would be a textbook 7-6 suspension. In this case however, the non-chord tone E resolves down by leap to the A chord tone. Is there another type of name for this kind of non-chord tone or would this still considered a suspension?
Comment: Aside from @laurencepayne's correct answer, I think it's important to note that in these "Can I do X" questions there's always a context. Is it "allowed" in first-species counterpoint? In Wagnerian post-tonality? In jazz? In Mongolian throat singing? In this case, while Coldplay is certainly beholden to the legacy of tonality and even voice leading, it's important for the music theorist to analyze a work according to its conventions.
Comment: Its's no suspension, and certainly not a 'non-chord-tone'. The chord is F maj7, so contains both the E and the A sung. False premise?
Comment: Tim, that is certainly one way to look at it
Answer: In this case, as the note E persists in the accompaniment, I don't think it 'resolves' at all.  The whole bar is a Fmaj7 chord, the E isn't treated as a dissonance.
Comment: if that note just stayed on E wouldnt sound right so I am still hearing a resolution from that dissonant E to the A
Comment: In context - see the post by @leftaroundabout - it would feel strange not to continue the musical phrase to a lower note.   But this isn't Mozart.  Resolving a melody on the tonic maj7 is mainstream in today's pop/jazz music.
Comment: In the pop music I listen to I have not really seen many melodies resolving on a major 7th... maybe more of a jazz thing... I am sure it does exist but it is rare.
Comment: Yes, probably more jazz.  And today's Musical Theatre ballads have a cliche of ending on a tonic maj9 or (addd9) chord.
Answer: We should look at it in context.
[Reload the page if the notation doesn't make any sense; there's a bug in StackExchange's ABC integration]
X:1
L:1/8
M:C
K:C
%%score T1
V:T1           clef=treble
% 1
[V:T1] "am"E3 (G G)F E2 | "C"E3 (G G)F E2 | "F"D3 (C C4) | z8
[V:T1] "am"E3 (G G)F E2 | "C"E3 (G G)F E2 | "F"E3 (A, A,4) | z8

A couple of things to notice:
It's two times almost the same thing, except for the last bar (and the second time is the spot you were asking about)
This is a pretty common schema, and indeed quite "classical": a repitition with two different endings. Classically, it would in particular also be the harmonies that do something different (like, go to the dominant one time), but in pop it's much more common to have chord loops that keep on going, regardless of melodic considerations.
The melody keeps returning to E on all the 1 beats, except the first iteration of the last bar
So E has almost a bit of a pedal note quality. That alone is enough to justify using it basically over any chord: it doesn't come out dissonance, because it's already accepted as ambient in a sense.
By contrast, the D in the first iteration does stick out, and indeed I would call D-C a 6-5 suspension over the first F chord. (Disagree with Aaron that suspensions need to be prepared.)
The 1-beat E is generally a consonance, except in each last bar
So mostly, we don't even need to invoke pedal point. Only in that final bar is the E a maj7 and as such could be considered sticking out more dissonant than the previous stuff. In combination with the fact that the first time around does have the 6-5 suspension, this could be taken to mean that we are also this time dealing with a suspension, but I would disagree.
Instead, what the melody is doing is very resolved indeed, it just happens to be kind of detached from the harmonies. Namely, the melody is returning to both its E home-point, as well as then to the actual A tonic. I.e., it's outlining the entire tonic chord, a complete return home. No suspension quality at all.
Only, as I said, the accompaniment repeats the am-C-F, as chords repeat in pop music unless there's a very special moment that wants to be highlighted. Otherwise, the harmony changes aren't supposed to be highlighted or have an anyhow disrupting effect, as harmony changes often have in classical music.
That is IMO the biggest reason why it's not a suspension: because what it actually is is a downward arpeggio, just not of the chord that occurs in the accompaniment.
– But also, yeah, as already said, a maj7 can actually be pretty consonant anyway, so there's not much issue there in the first place.
Answer:
If the E resolved by step downward to D it would be a textbook 7-6 suspension.

No it would not.
A textbook 7-6 suspension might be exemplified by a composer like Corelli... not Cold Play.
Harmonic styles treating sevenths and ninths as consonances that don't need resolution have been around for about 100 years!
Don't apply theory out of context.

Is there another type of name for this kind of non-chord tone or would this still considered a suspension?

Well, in jazz harmony extensions are the notes of the tertian stack above a seventh, the ninth, eleventh, and thirteenth. Or, if you like you can include the sus and add type chords. So you could speak of it as "extended" harmony. Although the E in question is a seventh so even by this standard it's a chord tone.
There are other terms like pan-diatonicism, poly-tonality, or quartal harmony that would toss out the conventional sense of non-chord tones, but they don't fit the style here.
Comment: ok so what isnt a chord tone then in jazz harmony?
Comment: Exactly! I asked a question recently that got into that issue of NCTs in jazz harmony  https://music.stackexchange.com/questions/108230/block-chord-style-and-appoggiaturas
Comment: IMO, unless the style is getting into `7sus4`, quartal-ish, harmony, the fourth is the only obvious non-chord tone. But melodic & rhythmic context will have an influence. But the main point is all the criteria for NCTs that comes from _common practice_ definitions are based on definitions of dissonance that don't apply to jazz. So, you need to use good judgement to decide whether in jazz some tone is NCT or just an extension.
Comment: hmmmm now you got me scratching my head :)
Comment: Try looking at the `A` part of the song _Cherokee_. First notice that whole section of the song's melody is based on a tonic `add6` chord with each 8 bar phrase ending on the supertonic `^2`. Without chords the sense of the melody is perfectly clear: tonic outlined, half cadences on `^2`, final cadence on `^1`.
Comment: The harmonization details can be tricky because of differing arrangements. But bars 3-7 have a number of extension tones, elevenths, thirteenths, ninths, and sevenths. Bar 3 is specificially an `11`. None of them resolve as if NCTs, except bars 7-8 where there is a `#11` that steps down to the chord's third.
Comment: It's interesting to compare the elevenths in bars 3 and 7, because the first doesn't "resolve" like a NCT, but the second one does. Again, _all_ melodic tones can be explained as tonic `add6` arpeggiation with half cadencing on `^2`. You could say the `11` in bar 3 is just part of the extended harmonic vocabulary of jazz (the tone is the tonic, a stable tone, does it really need resolution?), and you can say the `#11` is a NCT, because it steps down to "resolve" to a chord tone. But must you? The dominant seventh, sharp eleventh in bar 7 could also be called part of the extended harmony.
Answer:
This is not a suspension. To be a suspension, the note must begin as a chord tone in a different harmony, carry over into a new harmony (the suspension) in which it is a dissonance, and then resolve. (For this reason, the omitted portion is not only relevant, but essential.)

Thie E is not dissonant. In the context of popular music and jazz, major seventh chords — the harmony accompanying the E — are not generally considered dissonant, so nothing is resolving here: it's just a chord tone moving to another chord tone.

The D is not consonant. Even allowing the E were dissonant, the underlying harmony is F major, not D minor, so the D would still be dissonant.

Popular music and jazz cannot be consistently analyzed according to the classical theory of tonality. Different aesthetic standards apply. So while both follow many of the patterns and principles of tonality, they depart from it liberally and cannot be considered to exemplify it.

Suspensions always resolve down by step. However, what can sometimes happen is that the suspended voice will initially leap to another chord tone before then moving to the expected pitch of resolution. The "leap note" is decorative and not considered the resolution.
Comment: "To be a suspension, the note must begin as a chord tone in a different harmony": unless it is an unprepared suspension.
Comment: @phoog: "Unprepared suspensions" exist in German theory, where the word Vorhalt incorporates various figures that in English-language theory would be called accented passing tones and appoggiaturas. The German word for appoggiatura (Vorschlag) is more limited in scope than what is assumed in English-language theory.  So the "suspension" (Vorhalt) concept is broader.  But I've never heard the term "unprepared suspension" used in English-language theory except when trying to get across a translation of Vorhalt. Otherwise, we call these notes appoggiaturas and maybe accented passing tones, no?
Comment: @phoog "unprepared suspension"?
Comment: Thank you. What happens before is irrelevant because I explained that the note is an F from the previous bar. that is the only info that is important accompanying my question... so F chord tone to E non chord tone  to A chord tone... and my question was more related to classical music not jazz... heck I think m9 must be a chord tone by now in jazz. Good point about the D :)
Comment: @armani Because it's an F in the previous bar, it's not a suspension. The previous bar is the essential element in determining if it's a suspension in the first place. The E is a chord tone. And my point is that pop and jazz don't follow classical rules, so you can't use a pop example to ask a classical question. Point 5 answers the classical question.
Comment: I have spent so much time analyzing music and I can assure you that classical music theory covers the overwhelming majority of dissonant tones im Coldplays music. You dont get phrases just ending on 7ths. I admit I am not a jazz follower when it comes to theory but pop music, at least the kind I listen to, follows classical theory more closely than jazz. In fact there are very few occasions where it doesn't.  To my ears that melody would not sound right ending on E and to my ears that 7th is dissonant and sounds like  suspension does.. only the way it resolves is different which is why I asked
Comment: Just so you know, I did a checklist last year and.. out of 78 vocal phrases in Coldplays music, 62 of them end on the 3rd or the 5th of a harmony. and 8 end on the root of the harmony. That is 70 out of 78 phrases that end on one of the "chord tones". It looks to me more in line with classical music in the way vocal phrases end.
Comment: @armani: In classical music, there are many types of dissonances that can occur on an accented beat.  If the note in question does indeed move from an F in the previous bar down to an accented E here, and if it were to continue down to a (consonant) D, that would be an "accented passing tone" in classical style, not a suspension.  There are also appoggiaturas; etc.; context is important in classifying hon-harmonic tones.  That I think was Aaron's point here.
Comment: @armani: Also, having just listened to the song, the Maj7 sound is very much a part of this song.  It's a standard part of the harmonic vocabulary.  This has little to do with "classical vs. jazz" or whatever -- regardless of style, if a composer repeatedly uses a figure or in this case a chord in a particular way, that becomes part of the "vocabulary" of the piece.  Also, just because a note wouldn't sound right at the end of a phrase doesn't mean it can't be a chord tone.  In lots of jazz/pop styles, ending on a seventh in the melody is rare, but it's still part of the harmonic vocabulary.
Comment: the 7th in the chord is for colour, that is how I hear it at least and not as a chord tone where the vocal melody wants to end
Comment: @Aaron [unprepared suspension](https://duckduckgo.com/?q=unprepared+suspension).
Comment: @phoog Yes, I've seen all of those. They give the obvious literal definition, but I'd like to see how it works in practice and how it's distinct from all the other ornaments that would seem to already handle the available cases.
Answer: Music theory/harmony books never cover the most interesting and exceptional situations in voice-leading and harmonic analysis. Some Bach chorales are full of such examples. Here is an example of a Bach chorale where a suspension doesn't resolve by step. Consider this was analyzed as being in A minor (no key signature, upper staff = treble clef and lower staff = bass clef). There is no sensible analysis other than considering the E in the bass voice carried over from the first measure as a suspension. It doesn't resolve stepwise because that would create too much parallel motion and parallel fourths between bass and tenor which wouldn't sound good in that style.
Comment: Analyzing this in A minor means that the bass E is not a suspension. Since it's consonant with the E minor chord on the first beat, there's nothing to resolve. That's also why it can leap down to the B, which is also a chord tone. A suspension isn't just a carry over note; it's a carry-over that results in a dissonance against the primary harmony.
Comment: Dear Aaron, the piece was analyzed in A minor, but the passage above is clearly a tonicization of G major (hence VII). The chord where that E appears in the first bar can only be analyzed as either IV65 (of G), then immediately vii⦰2 (of G), or just vii⦰2 (of G) considering the B and the G above it suspensions that resolve on the A and F#.
Comment: The first chord next bar cannot be E minor, since there is no G in it and when the G comes, it's a G major triad. Moreover, the previous chord is a dominant of G which is partly resolved with the B in the upper voices and immediately the G major triad. I've played it many times and can't hear E minor. So the E on the first beat of the second bar is a dissonance expected to resolve on D, just as A resolves on G.
Comment: The downbeat is not VII6. It's vi/VII with a 4-3 suspension. That the harmony changes at the resolution doesn't change the nature of the E minor harmony on the downbeat. E minor is first achieved on the preceding beat 4, with the C as an accented passing tone. But on the half-beat it becomes [iio/vi]/VII over an E pedal tone. (Alternatively, [iio[4-2]/vi]VII resolving to vi/VII by common tone.)


Riemannian distance function on a product manifold

Question: Let $(M_1,g_1)$ and $(M_2,g_2)$ be two Riemannian manifolds. Let $d_1$ and $d_2$ be the respective induced Riemannian distance functions, i.e. metrics in the sense of metric spaces.
Let $(M_1\times M_2,g_1\oplus g_2)$ be the product Riemannian manifold, and let $d$ be the induced distance function on $M_1\times M_2$.
Let $d_{\text{prod}}$ be the product metric on $M_1\times M_2$ in the metric sense, i.e.
$$d_{\text{prod}}((x_1,x_2),(y_1,y_2))=\sqrt{d_1(x_1,y_1)^2+d_2(x_2,y_2)^2}.$$
Question 1: Is it always true that
$$d=d_{\text{prod}}?$$
Comment: This seems to be true if either $M_1$ or $M_2$ is a manifold where any two points are connected by a unique geodesic (e.g. $\mathbb{R}$). I suspect that in general we only have $d\leq d_{\text{prod}}$. But what is an illustrative example?
Question 2: If not, is there anything that we can say about the relationship between $d$ and $d_{\text{prod}}$? (E.g. does there exist $C>0$ such that $\frac{1}{C}d\leq d_{\text{prod}}\leq Cd$; do they induce the same topology on $M_1\times M_2$?)
Answer: Let:

$p_1,q_1$ be two points in $M_1$.
$p_2,q_2$ be two points in $M_2$.
$c_1: [0,l_1] \rightarrow M_1$ be any parametrized by arc-length piecewise regular smooth curve in $M_1$ connecting $p_1$ and $q_1$.
$c_2: [0,l_2] \rightarrow M_2$ be any parametrized by arc-length  piecewise regular  smooth curve in $M_2$ with unit-speed connecting $p_2$ and $q_2$.
$\hat{c}=(\hat{c}_1,\hat{c}_2): [0,1] \rightarrow M_1 \times M_2$ be any piecewise regular smooth curve in $M_1\times M_2$  connecting $(p_1,p_2)$ and $(q_1,q_2)$.

Clearly $c: [0, 1] \ni t \mapsto \left( c_1(l_1t),  c_2( l_2t) \right) $ is a piece-wise smooth connecting $(p_1,p_2)$ with $(q_1,q_2)$ and $\text{length}(c)=\sqrt{l_1^2+l_2^2}$. Hence, $d_{g_1 \oplus g_2}\left( (p_1,p_2),(q_1,q_2) \right) \le \sqrt{l_1^2+l_2^2}$. In consequences,$$d_{g_1 \oplus g_2}\left( (p_1,p_2),(q_1,q_2) \right) \le \sqrt{ d_{g_1}\left( p_1,q_1 \right)^2+d_{g_2}\left( p_2,q_2 \right)^2}.$$
Now, consider $\hat{c}$. We see that $\hat{c}_1,\hat{c}_2$ must also be piecewise smooth curves. Let $\hat{l_1},\hat{l_2}$ be the length of $\hat{c}_1$ and $\hat{c}_2$, respectively. We have:
\begin{align}
\mathrm{length}(\hat{c}) &= \int_0^1 \sqrt{ |\hat{c}'_1(t)|^2+|\hat{c}'_2(t)|^2 } \mathrm{d}t \\
&\ge \frac{1}{ \sqrt{ \hat{l}_1^2+\hat{l}_2^2}} \left( \hat{l}_1\int_0^1 |\hat{c}'_1(t)| \mathrm{d}t+\hat{l}_2\int_0^1 |\hat{c}'_2(t)| \mathrm{d}t \right)
=\sqrt{ \hat{l}^2_1+\hat{l}_2^2}
\end{align}
Thus, $\text{length}(\hat{c}) \ge \sqrt{ d_{g_1}\left( p_1,q_1 \right)^2+d_{g_2}\left( p_2,q_2 \right)^2}$. Therefore,
$$d_{g_1 \oplus g_2}\left( (p_1,p_2),(q_1,q_2) \right) \ge \sqrt{ d_{g_1}\left( p_1,q_1 \right)^2+d_{g_2}\left( p_2,q_2 \right)^2}.$$
Hence, the desired conclusion.
Comment: How do we know that there are length minimizing curves to begin with?
Comment: Because I work with Riemannian distance.
Comment: Well, consider $\mathbb{R}^2 \setminus \left\lbrace 0 \right\rbrace$ equipped with the usual Riemannian metric. Then we cannot find any minimising curve that joins $(1, 0)$ and $(-1, 0)$.
Comment: Right, forgot that.
Answer: The answer to your second question is yes! To see this, first consider an admissible (piecewise smooth) curve $\gamma: \left[ a, b \right] \rightarrow M_1 \times M_2$. Then, it is of the form $\gamma \left( t \right) = \left( \gamma_1 \left( t \right), \gamma_2 \left( t \right) \right)$, where $\gamma_1: \left[ a, b \right] \rightarrow M_1$ and $\gamma_2: \left[ a, b \right] \rightarrow M_2$ are admissible curves. Now, consider the lengths of these curves. By definition, we have
$$L \left( \gamma \right) = \int\limits_{a}^{b} \| \gamma' \left( t \right) \| \ \mathrm{d}t,$$
$$L \left( \gamma_1 \right) = \int\limits_{a}^{b} \| \gamma_1' \left( t \right) \|_1 \ \mathrm{d}t,$$
$$L \left( \gamma_2 \right) = \int\limits_{a}^{b} \| \gamma_2' \left( t \right) \|_2 \ \mathrm{d}t.$$
Here, $\| \cdot \|, \| \cdot \|_1,$ and $\| \cdot \|_2$ are the norms induced by $g_1 \oplus g_2, g_1,$ and $g_2$ on the tangent spaces of $M_1 \times M_2, M_1,$ and $M_2$ respectively.
We know that the product Riemannian metric $g_1 \oplus g_2$ is given by
$$\left( g_1 \oplus g_2 \right)_{\left( p, q \right)} \left( \left( X_1, Y_1 \right), \left( X_2, Y_2 \right) \right) = \left( g_1 \right)_p \left( X_1, X_2 \right) + \left( g_2 \right)_q \left( Y_1, Y_2 \right).$$
This clearly gives
$$\| \gamma' \left( t \right) \| = \left( \| \gamma_1' \left( t \right) \|_1^2 + \| \gamma_2' \left( t \right) \|_2^2 \right)^{\frac{1}{2}}.$$
That is,
\begin{align}
L \left( \gamma \right) = \int\limits_{a}^{b} \| \gamma' \left( t \right) \| \ \mathrm{d}t &= \int\limits_{a}^{b} \left( \| \gamma_1' \left( t \right) \|_1^2 + \| \gamma_2' \left( t \right) \|_2^2 \right)^{\frac{1}{2}} \mathrm{d}t \\
&\geq \max \left\lbrace L \left( \gamma_1 \right), L \left( \gamma_2 \right) \right\rbrace. \\
&\geq \max \left\lbrace d_1 \left( \gamma_1 \left( a \right), \gamma_1 \left( b \right) \right), d_2 \left( \gamma_2 \left( a \right), \gamma_2 \left( b \right) \right) \right\rbrace.
\end{align}
Let us now denote $\gamma_1 \left( a \right) = p_1, \gamma_1 \left( b \right) = q_1, \gamma_2 \left( a \right) = q_1,$ and $\gamma_2 \left( b \right) = q_2$. That is, $\gamma_1$ is an admissible curve in $M_1$ joining $p_1$ and $q_1$, $\gamma_2$ is an admissible curve in $M_2$ joining $p_2$ and $q_2$, and $\gamma$ is an admissible curve in $M_1 \times M_2$ joining $\left( p_1, p_2 \right)$ and $\left( q_1, q_2 \right)$.
This gives that $d \left( \left( p_1, p_2 \right), \left( q_1, q_2 \right) \right) \geq d_{\infty} \left( \left( p_1, p_2 \right), \left( q_1, q_2 \right) \right)$, where I use the notation:
$$d_{\infty} \left( \left( p_1, p_2 \right), \left( q_1, q_2 \right) \right) = \max \left\lbrace d_1 \left( p_1, q_1 \right), d_2 \left( p_2, q_2 \right) \right\rbrace.$$
On the other hand, we have
\begin{align}
L \left( \gamma \right) &= \int\limits_{a}^{b} \left( \| \gamma_1' \left( t \right) \|_1^2 + \| \gamma_2' \left( t \right) \|_2^2 \right)^{\frac{1}{2}} \ \mathrm{d}t \\ &\leq C \left( \int\limits_{a}^{b} \left( \| \gamma_1' \left( t \right) \|_1^2 + \| \gamma_2' \left( t \right) \|_2^2 \right) \ \mathrm{d}t \right)^{\frac{1}{2}} \ \ \ \ (\text{by the Holder's inequality}) \\
&\leq C \left( L \left( \gamma_1 \right)^2 + L \left( \gamma_2 \right)^2 \right)^{\frac{1}{2}}.
\end{align}
In the last inequality, we have used the "reverse Holder inequality".
This gives us
$$d \left( \left( p_1, p_2 \right), \left( q_1, q_2 \right) \right)^2 \leq C^2 \left( L \left( \gamma_1 \right)^2 + L \left( \gamma_2 \right)^2 \right).$$
Notice that this is true for every admissible curve $\gamma_1$ joining $p_1$ and $q_1$, and $\gamma_2$ joining $p_2$ and $q_2$. So we can get
$$d \left( \left( p_1, q_1 \right), \left( p_2, q_2 \right) \right) \leq C \left( d_1 \left( p_1, q_1 \right)^2 + d_2 \left( p_2, q_2 \right)^2 \right)^{\frac{1}{2}} = C d_{prod} \left( \left( p_1, q_1 \right), \left( p_2, q_2 \right) \right),$$
in the notation of your question. That is, we have
$$d_{\infty} \leq d \leq C d_{prod}.$$
But one can easily prove that if $\left( X_1, d_1 \right)$ and $\left( X_2, d_2 \right)$ are metric spaces, then $d_{\prod}$ and $d_{\infty}$ are equivalent metrics (for instance, see here). Thus, there is some $A > 0$ such that
$$A d_{prod} \leq d \leq C d_{prod}.$$
That is $d$ and $d_{prod}$ are equivalent metrics and hence they give the same topology.
Also, this proof gives an idea (which need not be correct at the moment) that in general the two metrics need not be equal unless $A = C = 1$. However, at this moment I do not have a counterexample.


Would a cached page be considered, solely by definition, RESTful?

Question: I'm thinking about how to explain the definition of a RESTful interface and am wondering if a cached page in Drupal would, purely by definition be considered RESTful: http://en.wikipedia.org/wiki/Representational_state_transfer#Constraints
Obviously if I wanted to setup a truly RESTful interface I would use Services or Fago's RestWS modules.
But by definition if the page is cached in D7 (thus there is no state information) is there a difference in definition between returning an xml or json endpoint and cached html page? Would the fact that there is javascript on the page create a state that would make it by definition not RESTful?
Again this is purely intellectual so thanks for any of your thoughts.
Answer: If a page is cached or not is in my opinion completely irrelevant. Page caching is transparent to the user and the content, he doesn't know if it is and once the cache expires, he might get something different on the same page. A page could for example be a view that randomly displays a node. Enabling page caching (and the node only switches e.g. every 5 minutes) doesn't change anything.
RESTful is about having a 1-1 mapping between an URL and a resource (or maybe better: a representation of that resource, e.g. a different format). IMHO, it doesn't matter if..

authorization is necessary to have access to that resource (anon vs. authorized users)
Only a part of that resource is visible (e.g. someone with additional permissions might see more information)
If the displayed information changes, as long as it's the same resource on the same URL (e.g., a node could have an access counter that increases every time you view it.)
If there is some sort of caching involved, either page or partial caching.
Comment: Yes, but that state depends on the Cookie that is sent by the client. The very next sentence there reads "Each request from any client contains all of the information necessary to service the request, and any session state is held in the client. The server can be stateful; this constraint merely requires that server-side state be addressable by URL as a resource.". What I mean is that it *possible* to have RESTful pages with authentication, it's not ruled out because of that. Of course, most websites where you are authenticated aren't.
Comment: Thanks for the response. You say it doesn't matter between auth and anon users, but wouldn't that break the stateless requirement if Drupal is storing session data. From wikipedia "The client–server communication is further constrained by no client context being stored on the server between requests."
Answer: as i see it for point Client–server and Stateless
a logged in user will be using a cookie and break these rules
also Manipulation of resources through these representations (point 2. of Uniform Interface)
again, as fe. only logged in users can manipulate nodes, this rule cannot be applyed
so, in my opinion, only an anonymous cached page can be considered RESTful.

except; there would be a URI allowing user-access, like /u234/gwd8932p/node/32(/edit)

each endpoint is is considered valid, so also X(HTML).
javascript is the Code on demand as long as all content is visible in the X(HTML), and not pulled via AJAX.
but - i may be wrong ;)


Alternative ways to clear the Drupal cache

Question: We have a busy database server and when we try to clear the cache on Drupal, Drupal tries to TRUNCATE cache tables and that causes a backlog until the MySQL hits to maximum number of user connections (which is 800).  This is causing problems for us.
As far as we can tell, the problem is being caused by the locks during the table truncation.
Are there any alternative methods to clear cache that can help avoid this situation?
Comment: The problem probably isn't the clearing of the caches, but the rebuilding all of them on the next page load.
Comment: Sorry, but truncate is probably the fastest way to purge large amounts of data from MySQL tables.
Comment: Is this for Drupal 6 or 7?
Answer: While your cache bins are in MySQL, truncating tables is by far and away the quickest method to clear the cache. It's not a particularly expensive operation, so if it's backing up your db connections I would get your sysadmin to investigate what config issues might be causing that (or whether it's simply a lack of sufficient resources).
As MPD notes in the comments above, your real problem might actually be the cache rebuild after the flush has happened, though. You should be able to confirm that after a bit of testing.
You can take the pressure off the database by using redis/memcache/etc as a cache backend instead of the database. Doing that will also make considerable speed improvements to your site in general.
Your mileage will vary, of course, but it's very likely that flushing cache from memory will be a quicker operation than flushing it from the disk. As Mołot notes below, writing the new cache items to memory rather than disk will also give you a performance boost (there will still be reads and writes to MySQL, but far fewer writes in theory).
As with anything, it needs testing on the bare metal to see how any of this would affect your specific configuration.
Comment: And rebuilding it to memory will be lighter than rebuilding it to database.
Comment: While truncating continue, lots of query appears for fetching and updating the cache tables. Since the table is locked, those queries are waiting. That's the main problem actually. Truncating is also slower than usual because the data sits on a SAN storage, so I need a solution that doesn't lock the tables while emptying them.

Can I basically delete the content of the cache tables? It doesn't matter if it'll take longer as long as the tables are not locked.
Comment: Sure, the end goal is to get rid of all expired data in the cache tables - Drupal isn't particularly fussed how that happens. But a core cache flush will always try to be as efficient as possible, for obvious reasons, and in the MySQL world that's usually a `TRUNCATE`. BTW a `TRUNACTE` in 5.0.3 or greater actually drops the table before re-creating it, which might help to explain where the lock is coming from.
Comment: To make it happen you'll probably need to write a class that inherits from [`DrupalDatabaseCache`](https://api.drupal.org/api/drupal/includes!cache.inc/class/DrupalDatabaseCache/7) and overriders the `clear()` method to use your preferred method. Then you can use settings.php to replace the default cache class with your new one
Answer: If you are using drush cc all or clearing cache from the Performance page (admin/config/development/performance), then you end up calling drupal_flush_all_caches().  This will

Rebuilt the registry
Clear CSS and JS caches
Rebuild theme info
Rebuild entity and field info
Rebuilt the menu router table
Rebuild the action list
Clear all of the cache tables

The actual clearing of the cache tables is rather quick, but the other operations are pretty expensive.  In addition, on the next page load (which can be when you get redirected back to the Performance page), you will rebuild all of the cache tables.
When I have seen cache clear problems, the root cause has normally been a misconfigured LAMP stack.  Calculating out the max number of Apache processes to prevent system overload can help, as can having MySQL provisioned properly for buffers, max connections, etc.
In addition, in a live environment, you don't really need to rebuild everything from a drupal_flush_all_caches(); you usually just want to clear the cache tables themselves.  In this case, I use a variant of this:
/**
 * Implements hook_form_FORM_ID_alter().
 */
function MYMODULE_form_system_performance_settings_alter(&$form, $form_state) {
  $form['clear_cache']['clear_forward'] = array(
    '#type' => 'submit',
    '#value' => t('Clear all forward facing caches'),
    '#submit' => array('MYMODULE_clear_forward_submit'),
  );
}

function MYMODULE_clear_forward_submit($form, &$form_state) {
  MYMODULE_clear_forward_caches();
}

function MYMODULE_clear_forward_caches() {
  $cache_tables = array(
    'cache',
    'cache_block',
    'cache_filter',
    'cache_page',
    'cache_views',
  );

  foreach ($cache_tables as $table) {
    cache_clear_all('*', $table, TRUE);
  }

  drupal_set_message(t('Cleared forward facing caches.'));
}

This will add a new submit button to the Performance page which will just clear the caches that affect the forward facing portion of your website (ie, main pages).  I wouldn't use this as is; you need to adjust the table list to match your environment.  Once of these days I will make this more generic and release it as a public module.
Comment: From the OP's comments it looks like the problem is actually table-level locking, so anything that uses `DrupalDatabaseCache::clear()` (i.e. `cache_clear_all('*')`) is going to have the same issue. Agree that reducing the number of affected tables will help, but it might not help enough
Comment: @Clive Yeah, I wrote that up while those comments were rolling in.  Interesting problem.
Answer: You can try to use drush like drush -y cc all command, or TRUNCATE/DELETE the tables manually by the following one-liner:
drush sqlq --extra=--skip-column-names "SHOW TABLES LIKE 'cache%'" | xargs -L1 -I% echo "DELETE FROM %;" | drush sqlc --extra=-v


Hypothesis testing: X impact on Y

Question: I am testing the Hypothesis that: (where Y = life_expectancy and X= GDP)

Ho= GDP does not affect life expectancy.
HA= GDP affects life expectancy.

*I already did the model building and got my regression results. To test this hypothesis do I reject/fail to reject based on the coefficient results? If it wouldn't have an impact the coefficient would be 0 and another number otherwise, right?

Or do I get the t-stat from my regression table and do the normal hypothesis testing.
Comment: Hi: You have to hypothesize some relationship. Suppose that you assume it's linear. Then you would run a regression where life expectancy is the response and GDP is the predictor. This would assume that no other variables besides GDP effect life expectancy which is probably a stretch. Include others if you think that they could effect life expectancy also. The t-stat on GDP tells you whether GDP effects life expectancy, linearly.
Comment: Hi! Yes in effect I do have other predictors towards life expectancy, such as schooling, HIV, etc. In fact i did the linear regression already, and got the results. My t-stat is equal to -2.601 should I follow the normal procedure for the hypothesis test of (beta 1) ?
Comment: yes, but once you have other variables in the model, things get more tricky because the other predictors could be spuriously "consuming" some of the predictive power of GDP ( for example, if there's multi-colinearity ). or you could be leaving out some other important variable ? So, you have the right idea ( using a t-test on beta ) but note that what you are testing is  the effect of GDP GIVEN that the other variables are in the model.
Answer: Yes, you just look at the GDP coefficient. If 0 is in the confidence interval for this parameter estimate, fail to reject. Otherwise, reject. Similarly, if your p-value for that is smaller than your pre-specified alpha value, reject. Otherwise, fail to reject.

Hypothesis testing: X impact on Y


Regex para procurar palavra dentro de tag com CDATA

Question: Tenho um arquivo que contém as seguintes possibilidades de sequência de caracteres:
1º Caso: <text><![CDATA[Casa]]></text>
2º Caso: <text><![CDATA[Qualquer texto que tenha Casa no meio]]></text>
Estou tentando montar uma expressão regular para fazer um replace da palavra Casa por Edifício, mas estou com problema para montar tal expressão, tentei da seguinte forma:
String text = "<text><![CDATA[Casa]]></text>";
String regex = "(\\<text><![CDATA[\\(\\w+)(/Casa)(\\w+)(\\]]></text>))";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);

Mas isso me retorna erro na compilação do pattern, será que é possível fazer um regex que me retorne esses dois casos?
Comment: Se vc quer só substituir "Casa" por "Edificio", um simples `.replace("casa", "edificio")` não resolve?
Comment: @Gustavo queria que fosse simples assim, tenho locais no mesmo arquivo que contem a palavra casa que não pode ser alterado.
Comment: Recomendo você dar uma olhada nos métodos `string.matches(Pattern)` e `string.regionMatches(int, String, int, int)`.
Answer: Sugiro a seguinte expressão RegEx: (?<=CDATA\[)(.*?)(?=\])
Exemplo de implementação
    String casoUm = "<text><![CDATA[Casa]]></text>";
    String casoDois = "<text><![CDATA[Qualquer texto que tenha Casa no meio]]></text>";
    String regex = "(?<=CDATA\\[)(.*?)(?=\\])";

    Matcher matcher = Pattern.compile(regex).matcher(casoUm);
    if (matcher.find()) {
        System.out.println(casoUm.replaceAll("Casa", "Edifício"));
    }

    matcher = Pattern.compile(regex).matcher(casoUm);
    if (matcher.find()) {
        System.out.println(casoDois.replaceAll("Casa", "Edifício"));
    }

Exemplo: RegEx Exemplo
Comment: Francisco, estou com uma duvida, vc consegue me dizer se é possível considerar o `` no pattern?
Comment: Sim, possivelmente algo assim: **(?<=<\/text>)**  `String regex = "(?<=<\\/text>)";`
Answer: Se você está lendo ou manipulando XML/HTML, é melhor usar parsers específicos, em vez de regex (pois regex não é a melhor ferramenta para estes casos). Em Java, uma boa alternativa é usar a biblioteca jsoup.
Além disso, estou me baseando nos comentários, em especial neste, no qual você diz que "tenho locais no mesmo arquivo que contém a palavra casa que não pode ser alterado". Então estou assumindo que a substituição deve ser feita somente quando a palavra "Casa" está dentro de um CDATA, e este por sua vez está dentro da tag text. Em qualquer outro caso, não deve ser feita nenhuma substituição.
Sendo assim, com jsoup ficaria assim:
import org.jsoup.Jsoup;
import org.jsoup.nodes.CDataNode;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.parser.Parser;

String texto = "<text><![CDATA[Casa]]></text><text><![CDATA[Qualquer texto que tenha Casa no meio]]></text>"
        + "<text>Texto com Casa mas não dentro de CDATA</text>"
        + "<text><![CDATA[Casamento]]></text>";
Document doc = Jsoup.parse(texto, "", Parser.xmlParser());

for (Element e : doc.select("text")) { // pegar todas as tags "text"
    for (Node node : e.childNodes()) { // verificar se tem CDATA
        if (node instanceof CDataNode) {
            // trocar por outro CDATA, contendo "Edifício" no lugar de "Casa"
            CDataNode cdata = (CDataNode) node;
            String novoTexto = cdata.getWholeText().replaceAll("\\bCasa\\b", "Edifício");
            cdata.replaceWith(new CDataNode(novoTexto));
        }
    }
}
System.out.println(doc);

Como eu só verifico as tags text que possuem CDATA, e só troco o texto nestes casos, o resultado é:
<text><![CDATA[Edifício]]>
</text>
<text><![CDATA[Qualquer texto que tenha Edifício no meio]]>
</text>
<text>
 Texto com Casa mas não dentro de CDATA
</text>
<text><![CDATA[Casamento]]>
</text>

O terceiro caso não foi substituído pois, apesar de ter a palavra "Casa", não está dentro de um CDATA (que é o critério que usei, mas de qualquer forma, se o critério for outro, não é difícil usar as funcionalidades do jsoup para verificar as condições que você precisar - algo bem mais difícil de verificar com regex, dependendo do que você precisa).
Repare que o quarto caso (um CDATA que contém a palavra "Casamento") não é substituído. Esse é um corner case que a outra resposta deixou passar (no caso, o código que tem lá trocaria a palavra "Casamento" por "Edifíciomento" - veja).
No meu código acima isso não acontece porque eu uso o marcador \b (conhecido como "word boundary", algo como "fronteira entre palavras" - veja mais sobre ele aqui). Basicamente, o \b marca uma posição da string que possui um caractere alfanumérico antes e um não-alfanumérico depois (ou vice-versa), garantindo assim que só farei a substituição de "Casa" quando esta for uma palavra completa, e não parte de uma palavra.
Outro detalhe da outra resposta é que ela verifica se existe um CDATA, mas depois faz o replace em toda a string (inclusive nos demais trechos que você deu a entender que não deveriam ser substituídos - veja). Então se tiver um CDATA com "Casa", mas também tiver "Casa" em outra parte da string que não deve ser substituída, ambas serão.
De qualquer forma, usar uma biblioteca específica evita o uso de regex, que não é a ferramenta mais adequada para este caso. Regex lida com texto sem levar em conta o formato ou semântica do mesmo, e XML/HTML possuem variações demais (mais do que uma expressão regular é capaz de detectar). Qualquer variação mínima no XML poderá exigir uma mudança - nem sempre trivial - na regex (como podemos ver aqui - o exemplo é em HTML, mas as mesmas considerações valem para XML).
Um caso típico é se a tag estiver dentro de um comentário (ou seja, entre ). O jsoup consegue detectar esta situação e ignora os comentários, ou seja, não faz a substituição do trecho comentado. Já a solução da outra resposta não consegue detectar este caso (pois a regex só olha o CDATA, sem levar em conta o contexto no qual ela está) e acaba substituindo tudo. Claro que por ser comentário, talvez não faça diferença, mas este é somente um dos muitos casos possíveis no qual o parser é muito melhor do que a regex para detectar determinadas situações e não dar falsos positivos.


How to calculate $P(X<Y)$ of two random variables

Question: I want to calculate the following probability of two random variables (we don't know if they are independant) $P(X<Y)$.
In the case they are discrete it is easy: $P(X<Y)=\sum_{-k=\infty}^{+\infty}P(X < Y \cap Y=k)=\sum_{-k=\infty}^{+\infty}\sum_{j=-\infty }^kP(X=j\cap Y=k)$
The question is the following: What is $P(X<Y)$ in the case of $X,Y$ being continuous ? The intuitive answer would be to replace $P(X=j\cap Y=k)$ with the joint density function $f_{X,Y}$ and so to have $\int_{-\infty}^{\infty}\int_{-\infty}^{y}f_{X,Y}(x,y)dxdy$
Is this correct or is there a mistake somewhere?
Comment: "Is this correct" Quite.
Answer: If $X$ and $Y$ have joint density $f(x,y)$, then for any Borel subset $D\subset{\bf R}^2$, one has
$$
P((X,Y)\in D) = \iint_{D}f(x,y)\,dxdy.
$$
In your example, you have $D = \{(x,y)\in{\bf R}^2\mid x<y\}$. By the Fubini theorem,
$$
P(X<Y) = \iint_{\{x<y\}}f(x,y)\,dxdy = \int_{-\infty}^{\infty}\int_{-\infty}^yf(x,y)\,dxdy.
$$
So the integral you have is correct.
Answer: The idea is to apply the law of total probability :
\begin{align*}
\mathbb{P}(X<Y) &= \int_\mathbb{R} \mathbb{P}(X<Y \mid Y = y) f_Y(y) dy\\
&= \int_{-\infty}^\infty\int_{-\infty}^y f_{X\mid Y}(x\mid y)f_Y(y) dx dy\\
&= \int_{-\infty}^\infty\int_{-\infty}^y f_{X, Y}(x, y) dx dy\\
\end{align*}
Another way of doing that is using expectation :
\begin{align*}
\mathbb{P}(X<Y) &= \mathbb{E} [ \mathbb{1} (X<Y) ]\\
&= \int_{-\infty}^\infty\int_{-\infty}^{\infty} \mathbb{1} (x<y) f_{X, Y}(x, y) dx dy\\
&= \int_{-\infty}^\infty\int_{-\infty}^{y} f_{X, Y}(x, y) dx dy\\
\end{align*}
I prefer the second one since it matches better with measure theory.
Comment: To pass by conditional densities to reach absolute densities is unnecessary (and rather illogical, I must say). // And in the second part, aren't you simply repeating a part of the question?
Comment: @Did Could you expend on why it is illogical ? I don't really see your point. For second part it depends on how you define your random variable, but if you do it using the measure $f_{X,Y}$ then your done in one step since the expectation is defined as the Lebesgue integral of the argument. In the end is the Riemann integral I gave since the random variables are continuous.
Comment: Yes the second approach is the one -- unfortunately it is already explained in the question. The first one is illogical because $f_{X\mid Y}(x,y)$ is defined as the ratio $f_{X,Y}(x,y)/f_Y(y)$ hence to rediscover that the product $f_{X\mid Y}(x,y)f_Y(y)$ is $f_{X,Y}(x,y)$, is... well, going backwards.
Comment: Actually, it seems that the first reflex of many users on this site, when asked any question involving a joint distribution, is to rush to a conditional distribution, as if these were the primary notion in this context instead of the joint distributions themselves (from which one can in a second phase, yes, deduce conditional distributions). One day, I should find the courage (and the time) to study in depth the curricula based on this ill-conceived notion, to understand where it is coming from...
Comment: This is true, I understand what you mean.


Bayesian prior choice for multivariate Gaussian distribution

Question: Consider spatially distributed data $\boldsymbol x = \{x(t_1),x(t_2),\dots,x(t_n)\}$ which is described by a multivariate Gaussian distribution with mean $\mu$, standard deviation $\sigma$ and stationary correlation function which depends on the absolute distance $\tau = |t_1-t_2|$ between points:
$$\rho(\tau) = \exp\left(-\frac{2\tau}{\theta}\right)$$
Which produces a correlation matrix $\boldsymbol \rho(\theta)$.
$$\boldsymbol x \sim \mathcal{N}(\mu,\sigma^2 \boldsymbol \rho)$$
Given samples of $\boldsymbol x$ the aim is to predict $\mu$, $\sigma$ and $\theta$. Under the Bayesian approach I believe that a joint prior distribution must be specified. Since I want to use Markov Chain Monte Carlo to sample from the posterior I believe the prior should be proper. My approach is to specify marginal distributions $P(\mu)$, $P(\sigma)$, $P(\theta)$ and take the joint prior to be given by:
$$P(\mu,\sigma,\theta) = P(\mu)P(\sigma)P(\theta)$$
Is it reasonable to take the priors to be independent?
Can I take the priors to be uniform if am confident the parameter values fall in certain intervals i.e.
$$\mu \sim U[-100,100]$$
$$\sigma \sim U[0,1000]$$
$$\theta \sim U[0,1000]$$
Or should I use a Cauchy for $\mu$ and the folded-Cauchy for $\sigma$, $\theta$?
Comment: What's this for, kriging?
Comment: Often a inverse Gamma prior is assumed for $\sigma$. Do you have any specific reasons (read: prior knowledge/belief) to prefer a uniform prior?
Comment: @DeltaIV Jarad's comment has made me realise that a uniform prior is not non-informative. Do I believe that $Pr(0 \leq \sigma \leq 100) = Pr(900 \leq \sigma \leq 1000)?$. No. However I don't like inverse Gamma due to its behaviour around zero (see [here](https://arxiv.org/abs/1104.4937)). I think I will go with the folded Cauchy.
Comment: Any prior can be used, there is no way one can recommend one prior as been better than another on a general basis.
Answer: I believe you are using uniform priors in an attempt to be non-informative, but I believe your priors are more informative than you would like them to be. Do you really believe $\sigma$ or ($\theta$) is equally likely to be between (0,1) as (999,1000)? And that there is absolutely no probability whatsoever that these parameters are above 1000?
Also, there is no requirement to have proper priors for MCMC.
My suggestion is $\sigma \sim Ca^+(0,c_\sigma)$ (i.e. a folded-Cauchy or half-Cauchy), $\theta \sim Ca^+(0,c_\theta)$, an improper uniform for $\mu$ and all independent. You will need to choose $c_\sigma$ and $c_\theta$ for your problem, but since you are already "confident" the parameter values fall into the regions given, you can just choose these values so the probability outside those regions is small. I prefer heavy-tailed distributions like the half-Cauchy because the data can more easily overwhelm the prior in case you were wrong in your prior assumptions. If you really want the prior to be proper, you could use a Cauchy for $\mu$ and specify a location and scale parameter.
Comment: Thanks Jarad. I know that proper posteriors can come from proper priors thanks to your [youtube video](https://www.youtube.com/watch?v=SPRnFY8J_GI). I am not against improper priors per se however I want to ensure my posterior is proper. How do we know that the improper uniform prior will yield a proper posterior?
Comment: This is a question that need be solved on a case-by-case basis since you have to integrate out the likelihood against the prior to demonstrate properness. A computational approximate resolution is to check for stability of the posterior as a sequence of proper priors converging to the improper prior grows.


Python search for an exact string in a file

Question: import re

def find_string(file_name, word):
   with open(file_name, 'r') as a:
       for line in a:
           line = line.rstrip()
           if re.search("^{}$".format(word),line):
             return True
   return False

if find_string('/tmp/myfile', 'hello'):
    print("found")
else:
    print("not found")

myfile:
hello world #does not match
hello #match

If I remove ^ and $ then it will match but it will also match "he","hel" etc. How can I match the exact string if there are multiples words on a single line?
Comment: Use word boundaries `\bhello\b`. Please see [**demo**](https://regex101.com/r/DF45OY/2)
Comment: That's a good point. Are you trying to match a single word in a line? if you are just trying to match string "hel" in "hello world", well, its there. That is a proper match.
Comment: Try: `if re.search(r"\b{}\b".format(word),line):` and note the `r` before the regex string which makes sure the backslashes are interpreted correctly.
Comment: We can't solve this problem if you don't respond to questions.
Comment: Can you expand on what you are trying to match? If you want to match a line that is exactly `word`, then `line.rstrip() == word` will do.
Comment: It sounds like you want _whole words_, not _exact strings_.
Comment: using word boundary worked! thanks
Answer: You may try using word-boundaries around your text. Something like:
\bhello\b

You can find the demo of the above regex in here.
Sample implementation in Python
import re
def find_string(file_name, word):
   with open(file_name, 'r') as a:
       for line in a:
           line = line.rstrip()
           if re.search(r"\b{}\b".format(word),line):
             return True
   return False

if find_string('myfile.txt', 'hello'):
    print("found")
else:
    print("not found")

You can find the sample run of the above implementation in here.
Answer: Do you want something like this? Sorry if not
import re

with open('regex.txt', 'r') as a:
    word = "hello"
    for line in a:
        line = line.rstrip()
        if re.search(r"({})".format(word), line):
            print(f'{line} ->>>> match!')
        else:
            print(f'{line} ->>>> not match!')

text file:
hello world #does not match
hello #match
test here
teste hello here

[output]
hello world #does not match ->>>> match!
hello #match ->>>> match!
test here ->>>> not match!
teste hello here ->>>> match!


Same results for both ACF and PACF with Strange plot when I forecast

Question: I have two questions. The first one is related with the ACF and PACF and the second with the Resulted Plot after using sarima.for
I have downloaded the bitcoin price for the last 3 years as an example to learn how to do time-series analysis.
Then as you can see I took the first difference and the lag in order to detrend and remove variance.

How can I interpret the following case?
This is my model:
fit2 <- sarima(Ret.Bit,2,1,2)
with
> $ttable
         Estimate     SE  t.value p.value
ar1       -0.7751 0.0221 -35.1251  0.0000
ar2       -0.9381 0.0175 -53.7412  0.0000
ma1        0.7410 0.0158  46.9190  0.0000
ma2        0.9672 0.0134  72.1050  0.0000
constant   2.4933 1.3152   1.8957  0.0582

Using sarima.for(Ret.Bit,n.ahead = 4,2,1,2 I ve got this strange plot.

For me, it is strange both the dates, 1560 etc, and the fact that going out of the plot the line.
Please, any help suggestion?
Comment: The "strange plot" bit is a question about software and off-topic here, and it is not answerable because you did not provide a reproducible example (including describing which package `sarima.for` is from, and the type of the variable `Ret.Bit`, which I'm guessing is wrong).
Answer: if the acf of the original series = acf of the residual series your model has self-cancelling structure thus equivalent to a (0,0,0)(0,0,0,) . This is a great example of using VERY BAD software to "identify" a model. Sometimes software is worth what you pay for it. Notice how the ar coefficients are eerily similar to the ma coefficients.
Comment: The orders aren't identified by the software, they were supplied in the function call.
Comment: @Chris Haug  ..Thanks for the correction. My faith in free software has been (partially !) restored. It would be interesting to me if the OP explained how they assumed the model or just saw it in a book/post somewhere and decided to go with it without understanding the consequences.


Proving a Subset Identity: If $A\subseteq B$ and $C\subseteq D$, then $A\cap C\subseteq C\cap D$ and $A\cup C\subseteq C\cup D$

Question: Working on part A of this problem:

Prove each of the following results without using Venn diagrams of membership tables. (Assume a universe $\mathscr U$.)
a) If $A\subseteq B$ and $C\subseteq D$, then $A\cap C\subseteq C\cap D$ and $A\cup C\subseteq C\cup D$.
b) $A\subseteq B$ if and only of $A\cap\overline B=\emptyset$.
b) $A\subseteq B$ if and only of $\overline A\cup B=\emptyset$.

I worked out  the first part like this:

If $A$ is a subset of $B$ then $\forall~x~[x\in A \implies x\in B]$

Same goes for $C$ being a subset of $D$ (If $x$ is in $C$ it is in $D$)

If $A\cap C$, then $x\in A \wedge x\in C$

$x\in B ~\wedge x\in D$ (From steps $1$ and $2$)

Since $A\cap B \implies B\cap D$ we can say $A\cap C \subseteq B\cap D $

I'm wondering if I've made any errors in proving the first part of the consequent?
Comment: Things like $A\cap C$ are sets, and so don't have true/false values. Statement 3, for example, needs to be of the form 'if $A\cap C$ has some property'.
Comment: @Darcy $A\cap B \to B\cap D$ is saying $A\cap B$ implies $B\cap D$. Sets aren't statements, sets can't imply anything. In 5. I'd just write, from 3. and 4. it follows that $\forall x(x\in A\cap C\to x\in B\cap D)$, that is, $A\cap C\subseteq B\cap D$. In 3. you should add the universal quantifier over $x$. And even though I personally accept the steps until 4., I would prefer to  make everything explicit (e.g. If $A\cap C$, then $\forall xA\cap C(x\in A\land x\in C)$, thus $x\in A$ for all $x\in A\cap C$ and from 2. it follows that $x\in B$. Similarly for the other sets).
Comment: It seems right, but the beginning of 5. is gibberish.
Comment: @GitGud What I'm trying to say is that Step 3 implies Step 4. Since x being a member of A and C implies it is a member of B and D, I can say A intersect C is a subset of B intersect D. How would you suggest I improve my clarity?
Comment: @Darcy  $\TeX$ tip: use /in for "is in the set", rather than \epsilon, although they do look very similar handwritten!
Comment: Another related question: [If $A\subset B$ and $C\subset D$, then $A\cap C \subset B\cap D$ and $A'\cap C'\subset B'\cap D'$](https://math.stackexchange.com/q/511797)
Answer:
Starting with $(3)$ I would instead assume $x \in A\cap C$.

Then argue as you did (...so $x \in A \land x\in C$; using this together with the implications in $(1), (2)$ from modus ponens we get $x\in B$ and $x\in D$.
Conclude $x \in B\cap D$.

Therefore $x \in A \cap C \rightarrow x \in B\cap D$. (Having assumed $x \in A\cap C$, we derived $x \in B\cap D.$, Hence this implication is justified.)
Finally we conclude $A\cap C \subseteq B\cap D$.


$f\in L^{1}[0,1]$ Show $\lim_{n\to\infty}\int_{0}^{1}|f(x)|^{\frac{1}{n}}dx = m(\left\{ {x:f(x)\neq 0}\right\} )$

Question: The following is from a Sample Exam question I am studying from, and the question  has stumped me.
$$f\in L^{1}[0,1]$$
$$\lim_{n\to\infty}\int_{0}^{1}|f(x)|^{\frac{1}{n}}dx = m(\left\{ {x:f(x)\neq 0}\right\} )$$
What I think:from below:
I set $A=\{x:f(x)\neq 0\} )  $
Then
$\lim_{n\to\infty}\int_{A}|f(x)|^{\frac{1}{n}}dx \leq \lim_{n\to\infty}\int_{0}^{1}|f(x)|^{\frac{1}{n}}dx$
When I take the limit both sides I get that required lower bound.
from above I get
$$\lim_{n\to\infty}\int_{0}^{1}|f(x)|^{\frac{1}{n}}dx \leq 1$$
any insight would be appreciated.
My Texing skills are not great, but the above sums up the best of my attempts so far.
Thank you
Comment: Yes, the key observation is that $$\lim_{n \to \infty}|f(x)|^{1/n} = \begin{cases}1 & \text{ if }f(x) \neq 0 \\ 0 & \text{ if }f(x) = 0 \\ \end{cases}$$So interchanging the limit and the integral gives you what you want. The dominated convergence theorem justifies the interchange, as the answer by Dr. MV nicely demonstrates.
Comment: How did you conclude that $\lim_{n \to \infty}\int_0^1 |f(x)|^{1/n}dx \leq 1$?
Comment: Have you evaluated $\lim_{n \to \infty} |f(x)|^{1/n}$?
Comment: I evaluated the integral to be  |f(x)|^1/n *m([0,1]), then took the limit, which gave me 1*1=1 for the upper bound.
Comment: You evaluated the integral and the result was a function of $x$? How can that be?
Comment: I suggest that you evaluate $\lim_{n \to \infty}|f(x)|^{1/n}$. It should be clear what to do once you have that answer.
Comment: Sorry, I took |f(x)|<t for some t between [0,||f||infinity],
Comment: Thank you for your suggestion regarding switching limits, I think I have it now.
Answer: Note that for $n>1$ we have for $f(x)\ne 0$
$$
|f(x)|^{1/n}\le
\begin{cases}
|f(x)|&,|f(x)|\ge 1\\\\
1&,|f(x)|<1
\end{cases}$$
Since $|f(x)|^{1/n}\le \max(1,|f(x)|)$, and since $f\in L^1[0,1]$ the Dominated Convergence Theorem guarantees that
$$\lim_{n\to \infty}\int_0^1 |f(x)|^{1/n}\,dx=\int_0^1 \lim_{n\to \infty}|f(x)|^{1/n}\,dx=m\{x: f(x)\ne 0\}$$


PyPDF merge two PDFs

Question: I'm working with Python and Django and want to merge several PDF's into a single one.
I've seen several answers to this question, like this and this, which I've tried to apply but I'm getting errors.
First, this is the PDF data I'm working with:
In my example, I have these two PDFs:
type(pdf1) = <class 'bytes'>
type(pdf2) = <class 'bytes'>

And if I print() them, they look like this:
b'%PDF-1.5\n%\xb5\xed\xae\xfb\n4 0 obj\n<< /Length 5 0 R\n   /Filter /FlateDecode\n>>\nstream\nx\x9c\x85Vmo\xdb6\x10\xfe\xae_q(VT\x06f\x99\x94\xac7 \x0b\x90\xa6.\x96\x01u\xdbD\xdf\x82\xc1\x90%\xca\xe6&\x93.E95\x86\xfd\xf7\x92\xa2$S\xa9\x87\xc1\xb0$\x92\x0f\x1f\xde=w<\x12\x03R\xbf9V\x8fd\x89\xa188\xdf\x1c\xd4\xf5\x89\x1d,r\x04\xbb\xc6y\x9f9\xd8\xefq>\x84\x91\xe7\x87\x10\xc5^\x90.\xc3\x00Cvp\x16\xd5\x1c\xcd\x11\xa8\xef\xcaq\xffy\x0b\r\x91p\xccwd#\xa9\xac\t\xfc\x06o\xd6\\\xd2\x82\x00\xaf\xe0\x91P\xd6\xc8\\\x92\x03a\xf2\r\xbc\xfd\x17\xcc\x84Y\xf6\x97\xa3\x17\xf0p\x14\xa2 \x85\xact\xdc:o\xe4F\x90\x82\xd0\x13)7\xf9\x81\xb7Lj\xb6_B\xe4!dM\x86R\x11\x8eH\r\xc1x\x8e\xc2\xb9\x8fp\xa2a3\xe7\n{?\xb5\xd9\xf3\x97M^\x14\x9a\\\xbd%=QyV\x14R\xb4\xc4Z\xa1\x835t\xc7r\xd9\n\xb2\xd9\x93\xbc\xecAW\xc9\xcdD\xca\x8a\xba-\t\xbc\xfb@\xaa\xbc\xad%\xfc\xae\xa6\x11\xf1N\xf3j\x87S/L\xd3$\\\x1aq\xc7\x96\x8f#\xc5\xb5D\x91\x0f8\x89\xbd\x08\xc78Iz\xa1\xf1 \xf4\xb3\xbb\xfe\x9c=\xdc\xaf\xe0\xf3Gx\\=\xac\x9f\xb2Y\x18\xbaw\xfa\x91\xad>\xad\xd6\x19\xcc\xfe\xcc\xfe\xf89r>\x8a=\x1d8\xf5\xf9*t\xcf\xee5\x93\xbf\xcc\x96\xd8\xe55-\xce\xf0\xc0*nl\xd7\xcc\xb1\x97 \xe4\xa7\x81\xe1\x1f[\xfd*a\xe8a\x1c\xf8I\x97 \xcfn\xb6\xa7\xcdl\x1e\xc5.t\xcf\x17Z\xd7c3q\x1b"N\xc4\x1a\xcem,\xebrG\x7f:=\\\xeesi\x01\xe4~2w\xcb-\xae\xc4\xadi#Ii\x01\x8e\x9d33\xe7\xd2C\x9b\xa6\xb5 \x89\xbb=[x\xde\n\xabU\xf0\xc31gg\x0b\xbcW\xd6Zd[B\x98\x85\x17C\xbe\x97\xaf\xfdK\x06\xfe\xaa\x1f\xd1\xa2N#\xdc\xa10^\xf8h\xa1Sy@X\x11\xeb\x1d`\xdd\x90I\xc2x\x99\x04\x91N\xc2gW\xa7\xb5(s\xa6\xe5\x0b;\xacR^\xee\xfbFh\x94\xeb\x07$\x11\x87f\x1c\xc9Y9|;\xcagVRI9\xd3f\x9bqm\xb4M\xd2w\xf7\xda\xe2 p\xbd\x919\x1bVq\xd4S\xd8\xfb\x7f\x84\xd0\xcb\xca\xc4x\xf9\xa4T\x86\x9b\x1bX\xdc\x15\xb2\xcd\xeb\x8c|\x97pS\x91\xaaB(\x8a\xf4\xff\x16no\xe1\xfd\x87{#\x086\x82\xdc \x84\xf0\xad\xdaY\xabO\xf7\xaf\x94"zk_\xdc\xe5\xcc|^\x13=\xfc_\xcd\'\x02\xb9/\xf9\xc5|+\xdeWCR\xaa\x821\xc7A\x17|\x1c\xa4\xae\xe4C\xf3\x9a%z\xe4n\xd7)\xa5\xc1`z\x1e\xc9\xb7\x96\x8a~\x85\xa9m\xde\x84\xdc\xec\xba\xb1#\x19\xf7\x92E\xa6#i5\xa7\xf1ql\xb6\xfcx\xac)\x99\x12\x1a\xf3\xc7\xd9].XS\x04\xa9\x88 *\xff\xca)U\x9f\'\x13\xd3\xda\xc3\x96\x88\xa9i\xac>O\xe8:\xd1U\xdb\x19\x10%7\xf6\xa4\x96\x83\x96\xee\xd12\x

When I try merging them with like this:
from PyPDF2 import PdfFileMerger

pdfs = [pdf1, pdf2]

merger = PdfFileMerger()

for pdf in pdfs:
   merger.append(pdf)

merger.write("result.pdf")
merger.close()

I get this error: embedded null byte
And when I try merging them like this:
from functools import partial

with open(fpath, 'rb') as f, open(target_fpath, 'wb') as target_f:
    for _bytes in iter(partial(f.read, 1024), ''):
        target_f.write(_bytes)

I get this other error: 'bytes' object has no attribute 'seek'
I think these errors are caused by the format of the data I'm handling. Should I decode the binary data before being able to apply the solutions I linked?
Comment: Possible duplicate of [Mysterious "embedded null byte" error](https://stackoverflow.com/questions/38731132/mysterious-embedded-null-byte-error)
Answer: It might be a problem with how you imported it. The way I did it was
pdf1 = PdfFileReader("path\\to\\file.pdf")
type(pdf1)

and I got
PyPDF2.pdf.PdfFileReader

Comment: Due to how the rest of the codebase is working, I only have access to the binary data, not to the path where they are saved. So I would like to merge them, operating with the binary data if possible.
Comment: I see. I found this thread (https://stackoverflow.com/questions/43019315/how-do-i-create-a-pdf-file-from-a-binary-code-using-python), so maybe you could convert to PDF from binary, then open the file as binary like ```open(file.pdf, "rb")``` then merge?

PyPDF merge two PDFs


Prove : For $k\in \mathbb{Z}: \lim\limits_{n\to\infty}\sqrt[n]{n^k}=1$

Question: Proof: For $k\in \mathbb{Z}: \lim\limits_{n\to\infty}\sqrt[n]{n^k}=1$
Proof:
Observe $k=1$. Then $\lim\limits_{n\to\infty}\sqrt[n]{n}=1$. Let $c_n:=\sqrt[n]{n}-1$, then: $$\begin{gather}
n=(1+c_n)^n>1+\begin{pmatrix}n \\ 2 \end{pmatrix}c_n^2 \Longrightarrow n-1>\frac{n(n-1)}{2}c_n^2 \Longrightarrow c_n<\sqrt{\frac{2}{n}} \Longrightarrow \lim\limits_{n\to\infty}c_n=0 \quad  \Box
\end{gather}$$
I don't understand the first step with Bernoulli's inequality - why $\begin{pmatrix}n \\ 2 \end{pmatrix}$?
Secondly, I don't understand why $n-1>\frac{n(n-2)}{2}c_n^2$ implies $c_n<\sqrt{\frac{2}{n}}$...
Any ideas?
Answer: If you expand $(1+c_n)^n$, you'll obtain $1 + {n\choose 1} c_n + {n \choose 2} c_n^2 + \ldots$, and the inequality should now be obvious.
For the second point, their proof is actually incorrect here. Note that $\frac{n(n-2)}{2}c_n^2 < n-1$ implies that $c_n^2 < \frac{2}{n} \cdot\frac{(n-1)}{(n-2)}$. You can't get what they stated here because $\frac{n-1}{n-2}$ is actually greater than $1$, but you can get something just as good; note that $\frac{n-1}{n-2} = 1 + \frac{1}{n-2} < 2$ for all $n>3$, so we get $c_n^2 < \frac{4}{n}$ for $n>3$. So we have $c_n < \sqrt{\frac{4}{n}}$, and the proof ends in the same way.
Answer: It is well known that
$$(x+1)^n = x^n + \binom{n}{n-1} x^{n-1}+\binom{n}{n-2}x^{n-2}+...+\binom{n}{2}x^2+\binom{n}{1}x+1$$
Since every term is positive we have $(c_n+1)^n>1 + \binom{n}{2} c_n^{2}$.
For your second question
$$n-1>\frac{n(n-2)}{2}c_n^2\Rightarrow c_n^2<\frac{2(n-1)}{n(n-2)}$$
Now there's a mistake since $n-1>n-2$ you can't conclude that $$c_n^2<\frac{2(n-1)}{n(n-2)}<\frac{2}{n}$$ See @auscrypt answer for a just as good result.
Edit: It seems like there's a much easier way to do that. Using the arithmetic of limits we have that
$$\lim_{n\rightarrow\infty} \sqrt [n]{n^k} = \lim_{n\rightarrow\infty} (\sqrt[n]{n})^k =(\lim_{n\rightarrow\infty} \sqrt[n]{n})^k= 1^k =1$$
Comment: For your alternative proof, one has to prove that $\lim_{n\rightarrow\infty} \sqrt[n]{n}=1$
Comment: @ParabolicAlcoholic That's right. I figured from your question that you already know that.
Answer: Let $L=\lim_{n\to\infty}n^{\frac{k}{n}}$
Take log both sides, we get  $$logL=\lim_{n\to\infty}\frac{klogn}{n}=\lim_{n\to\infty}\frac{\frac{k}{n}}{1}=0$$
(using L'Hospital Rule)
So $logL=0$ which gives $L=1$
Hope this will be helpful!
Comment: The tags literally say "limits without L'Hopital"
Comment: I didn't see that. Anyway, I did use L'Hospital rule for most obvious part $\lim_{n\to\infty}\frac{logn}{n}=0$ as $n$ increases faster then $logn$ as $n$ approaches higher value. It's better to use a simple solution like this than to expand un-necessarily using binomial expansions.


Unexpected behavior when increasing network speed and connecting to a node.js server

Question: I have a simple node.js server like:
var app = require('express')();
var compression = require('compression');
app.use(compression());

app.get('/', function(request, response) {
  response.send('<!DOCTYPE html>.......');
}
app.listen(2345);

The html I send is 2.4kB (1.2kB when compressed).
When testing on different network speed (using dev tools) I get this unexpected behavior:
50kbps:  Latency 600ms, download   1ms
250kbps: Latency 300ms, download 0.6ms
750kbps: Latency 100ms, download 100ms
2Mbps:   Latency  10ms, download 200ms
32Mbps:  Latency   5ms, download 210ms

I don't think that the download time is supposed to increase when network speed increases after 250kbps. What is going on?
Again look at what happens if I remove compression:
var app = require('express')();

app.get('/', function(request, response) {
  response.send('<!DOCTYPE html>.......');
}
app.listen(2345);

Now the file is just 2.4kB and look at the latency/download times:
50kbps:  Latency 550ms, download 230ms
250kbps: Latency 350ms, download  50ms
750kbps: Latency 120ms, download  15ms
2Mbps:   Latency  35ms, download   6ms
32Mbps:  Latency   4ms, download 0.5ms

The response with the non-gzipped content (and contet-length header) seems to be ok, but the response with the gzipped content (with transfer-encoding chunked header) doesn't seem to be ok.What is this all about?I strongly encourage you to simulate a similar test yourself with whatever tools you like and see the results yourself before saying that my benchmark is wrong and that this cannot be possible. And if you get different results please share them.
Comment: As I keep saying, this is not a good test.  Obviously your browser's development tools are affecting this.  Why it does that has nothing to do with your Node.js application.  Besides, your sample size and data sizes are extremely small and don't really tell you much.  If you want a real test, use a basic HTTP client and a packet sniffer.  If you want to simulate internet speeds, there are tools for doing that at the network level.
Comment: I guess it takes ~200ms to process the uncompression, but this is done async but when there's not enough time to do it during the latency session it's added onto the download timing, likely because it's waiting for an uncompression callback before saving the timing for downloading
Comment: Why then if I send the same content with Apache I don't get these crazy times but completely normal behavior? As far as I can see the only difference between sending [gzipped] content with Apache and node is that one gets sent chunked and the other not chunked. And if it's the browsers fault then why when I send the same data using Apache I don't get those download times?
Comment: Maybe sending the content in chunks is messing with the underlying `TCP` implementation of [Nagle's algorithm](https://en.wikipedia.org/wiki/Nagle%27s_algorithm)? Just a thought. Might be worth digging further about this.
Comment: Maybe the problem is that the compression is made on the fly and it simply takes some time -- try to compress your file beforehand and repeat your tests.
Comment: 50kps took a total of 601ms to download, 250kps took 300.6, 750 took 200ms., and so on. in fact, each time the network speed went up, your network time went down, just like anyone would expect. just because something buffers small gzip results before starting the download doesn't mean it defies physics...
Comment: 1.2kB is approx 10kb of pure payload, On a 50kpbs line how can the download time be any less than 200ms assuming? Comment from @dandavis might offer a clue.
Comment: How are you controlling the network speeds for the tests?
Comment: Do repeated tests have the same results?
Comment: Yep, I repeated these test so many times already and the times are all about in the same range. You can check it out yourself by doing what I did.
Comment: As I made the html 3.5kB the problem seems to be gone. But with 2.4kB response we get this weird behavior . Seems like too small responses behave strangely.
Comment: How many simultaneous connections are you running for your tests?  What does your test harness look like?
Comment: Hmm... What about to repeat your tests with 250Mb file and regular chronograph? I don't think you faced with a miracle.
Answer: Express.js compression options
I would also not hesitate to change the different compression quality settings, strategies and especially the treshold setting of the express compression module, as described here: https://github.com/expressjs/compression, especiall the:
Compression Treshold Level
Since you are sending only a few bytes of textual data as body, try to set the treshold lower then the default of 1Kb.

The byte threshold for the response body size before compression is considered for the response, defaults to 1kb. This is a number of bytes, any string accepted by the bytes module, or false.

(cited from the express compression module github page)
HTTP Compression is not always faster
Make sure to play around with other HTTP features like HTTP Pipelining or accepted encodings (also on the client side) as switching those on or off may vastly alter the outcome of download time.
IBM conducted a series of excellent HTTP tests which I recommend you read about here: http://www.ibm.com/developerworks/library/wa-httpcomp/