Scrape subreddit post titles and use them as filename using Praw

Question: My code currently downloads images from a given subreddit and will name them as the original file name. What I would like the code to do is to name them as what they are posted on Reddit. Would anyone be able to help me out please? I think it's something to do with Submission.title but I can't figure it out. cheers.
<code>import praw
import threading
from requests import get
from multiprocessing.pool import ThreadPool
import os

client_id = 'xxxxxxxxx'
client_secret = 'xxxxxxxxx'
user_agent = 'xxxxxxxxx'
image_directory = 'images'
thread_count = 16

target_subreddit = 'space'
image_count = '10'
order = 'hot'

order = order.lower()

reddit = praw.Reddit(client_id=client_id,
                     client_secret=client_secret, user_agent=user_agent)

def get_order():
    if order == 'hot':
        ready = reddit.subreddit(target_subreddit).hot(limit=None)
    elif order == 'top':
        ready = reddit.subreddit(target_subreddit).top(limit=None)
    elif order == 'new':
        ready = reddit.subreddit(target_subreddit).new(limit=None)
    return ready

def get_img(what):
    image = '{}/{}/{}'.format(image_directory,
                              target_subreddit, what.split('/')[-1])
    img = get(what).content
    with open(image, 'wb') as f:
        f.write(img)

def make_dir():
    directory = f'{image_directory}/{target_subreddit}'
    if not os.path.exists(directory):
        os.makedirs(directory)

def main():
    c = 1
    images = []
    make_dir()
    for submission in get_order():
        url = submission.url
        if url.endswith(('.jpg', '.png', '.gif', '.jpeg')):
            images.append(url)
            c += 1
            if int(image_count) < c:
                break

    results = ThreadPool(thread_count).imap_unordered(get_img, images)
    for path in results:
        pass

    print('Done')

if __name__ == '__main__':
    main()
</code>
Comment: Anyone has working version of this, that also really renames the files based on post title? I have tried for half a day without any success.
Answer: Yeah, so if your 'url' variable is giving you a correct url, then simply submission.title should give you the title. You may be getting tripped up with the encoding, so you may want to convert it with str(), or get a bit fancier with the encode function. Also, some characters are not allowed in many file names, so perhaps try stripping unallowable characters from the title.


REST login problem

Question: I'm trying to log in to drupal8 via api rest using postman.
<code>POST: https://example.com/user/login?_format=json
Content-type: application/json

{
"name": "admin",
"pass": "password"
}
</code>
The problem is that this error is returned to me.

An unexpected error has occurred. Please try again later, thanks.
  Symfony\Component\Serializer\Exception\NotEncodableValueException: Syntax error in Symfony\Component\Serializer\Encoder\JsonDecode->decode() (line 78 of C:\www\drupal-composer\vendor\symfony\serializer\Encoder\JsonDecode.php).
Symfony\Component\Serializer\Encoder\JsonEncoder->decode('', 'json', Array) (Line: 40)
  Symfony\Component\Serializer\Encoder\ChainDecoder->decode('', 'json', Array) (Line: 280)
  Symfony\Component\Serializer\Serializer->decode('', 'json') (Line: 167)
  Drupal\user\Controller\UserAuthenticationController->login(Object) call_user_func_array(Array, Array) (Line: 123)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 582)
  Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 124)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
  Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber{closure}() (Line: 151)
  Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
  Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
  Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
  Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
  Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
  Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
  Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
  Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
  Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
  Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

Does anyone have a solution?
I am using Drupal 8.7.1.
Comment: Solution found! Postman: method POST; endpoint: http://{your_url}/user/login?_format=json; section Header: Content-Type: application/json; section body: {
   "name": "admin","pass": "admin"}
Comment: Is the payload correct? The error thrown is `NotEncodableValueException`. See comment #4 in what you linked to.
Comment: Yes the payload is correct, obviously I have inserted some sample data.
Even trying with curl I get the same error.
Comment: The error stack is indicating that the $data is empty though
Comment: I'm trying to log-in following the official documentation here:

https://www.drupal.org/docs/8/core/modules/rest/javascript-and-drupal-8-restful-web-services#users

using postman with the correct payload and the correct endpoint, but nothing. Always the same error.

I also try other drupal installations, same mistake :-(
Comment: @FabioMarsigliano you should answer your question and accept it in case somebody in the future has the same question.
Answer: Solution found!
In Postman:

method: POST
endpoint: http://{your_url}/user/login?_format=json
in section Header: Content-Type: application/json
in section body: {"name":"admin","pass": "admin"}

Note: name and password must be entered in the body section (raw)
Comment: You just saved my life... i spent 4 hours already :D

REST login problem


Expected number of elements for the first of $n$ many hash tables to be filled?

Question: This is a generalization of the questions: question 1 and question 2.
There are $n$ many hash tables each of size $m$. Each turn a random element in one of the hash tables is filled. If the element selected for that turn has already been filled then we've wasted that turn, and we go to the next turn as usual. What is the expected number of turns needed for one of the hash tables (whichever is first) to be filled?
Another way of asking this problem is: If we have an $n*m$ - sided die and we partition the possible values into $n$ equal sized sets of $m$ elements, what is the expected number of rolls before we complete a set?
For example, if we have a 15-sided die and we split the numbers into 3 sets of 5: $\{1,2,3,4,5\}, \{6,7,8,9,10\}$, and $\{11,12,13,14,15\}$, then $m = 5$ and $n=3$. On average, how many times must we roll the die before we've seen all $5$ rolled values for one of the sets (whichever is first)?
In question 1 WhatsUp gave a recursive equation for a two hash table scenario: $$E(a, b) = 1 + \frac a {2m}E(a - 1, b) + \frac b {2m} E(a, b - 1) + \left(1 - \frac{a + b}{2m}\right)E(a, b)$$ with $E(a,b)$ defined as the expected number of elements to fill one table, if there are still $a$ and $b$ empty entries in the two tables. Can this be modified for a 3 hash table scenario as: $$E(a, b, c) = 1 + \frac a {3m}E(a - 1, b, c) + \frac b {3m} E(a, b - 1, c) + \frac c {3m} E(a, b, c - 1) + \left(1 - \frac{a + b + c}{3m}\right)E(a, b, c)$$ and if so how do we go about solving for $E(m,m,m)$? Could we generalize this equation to deal with $n$ many hash tables, and if so how?
In question 2 there are some great answers involving Markov chains, but if $n$ and $m$ get very large, the transition matrix gets very large and impractical to fill out or work with. (At least that's how it seems to me. Are there are tricks to deal with this?) So another approach is necessary. Thomas Andrews gave a very detailed answer which included a generalized solution which should work here $$\sum_{i_1}^{a_1}\cdots\sum_{i_n=1}^{a_n}(-1)^{\sum (i_j-1)}\binom{a_1}{i_1}\cdots\binom{a_n}{i_n}\frac{a}{\sum i_j}$$ but isn't intuitive and I'm struggling to see how it came about. He mentioned, and I can see, that it stems from an inclusion-exclusion argument, but I'm failing to see how to start there and end up with his equation.
Comment: Noote, in your case, all the $a_i=m$ and that slightly simplifies my expression.
Answer: Answer: The expected time until the first filling of any of the hash tables is
$$
nm\sum_{k=1}^n (-1)^{k-1} \binom{n}{k}H_{km},\tag1
$$
where $H_r=\frac11+\frac12+\dots+\frac1{r}$ is the $r^\text{th}$ Harmonic number. We can also give a generalization for when the hash tables have unequal sizes. If there are $n$ tables, with sizes $m_1,\dots,m_n$, then the expected time is
$$
(m_1+\dots+m_n)\times \!\!\!\!\!\!\!\!\!\sum_{\varnothing \neq S\subseteq \{1,\dots,n\}}(-1)^{|S|-1}H_{m_S},\tag2
$$
where $m_S$ is shorthand for $\sum_{i\in S}m_i$. The sum ranges over all nonempty subsets of $\{1,\dots,n\}$.
Proof: Let $T_1,T_2,\dots,T_n$ be random variables, where $T_i$ is the time it takes to fill the $i^\text{th}$ hash table. We want to find the expected value of $\min(T_1,\dots,T_n)$. We start with the following algebraic relation between $\min$ and $\max$. For any real numbers $x_1,\dots,x_n$,
$$
\min(x_1,\dots,x_n)=\sum_{\varnothing \neq S\subseteq \{1,\dots,n\}} (-1)^{|S|-1}\max_{i\in S}x_{i}.\tag3
$$
This can be proven by induction on $n$, using the relation $\min(x_1,x_2)=x_1+x_2-\max(x_1,x_2)$.
We apply $(3)$ with $x_i\gets T_i$, and then take the expected value of both sides:
$$
E[\min(T_1,\dots,T_n)]=\sum_{\varnothing \neq S\subseteq \{1,\dots,n\}} (-1)^{|S|-1}E[\,\max_{i\in S} T_i\,].\tag4
$$
Note that $\max_{i\in S} T_i$ is the time it takes to fill all of the hash tables whose indices are in $S$. Letting $m_S=\sum_{i\in S}m_i$, I claim that the expected time it takes to do this is
$$
E[\,\max_{i\in S} T_i\,]=(m_1+\dots+m_n)H_{m_S}\tag5
$$
To see this, define a sample to be useful if it is drawn from one of the hash tables in $S$, and wasted otherwise. If we ignore all of the wasted samples, then this is exactly like the coupon collector's problem where we try to collect all of the coupons in the union of the hash tables in $S$. This means that
$$
E[\text{# useful samples}]=m_S\cdot H_{m_S}\tag6
$$
To account for wasted samples, note that the expected time it takes to get the next useful sample is $\frac{m_1+\dots+m_n}{m_S}$, because the time is geometrically distributed with probability of success equal to $\frac{m_S}{m_1+\dots+m_n}$. It follows that
$$
E[\,\max_{i\in S} T_i\,]=\frac{m_1+\dots+m_n}{m_S}\cdot E[\text{# useful samples}]\tag7
$$
Combining $(6)$ and $(7)$ proves $(5)$, then combining $(4)$ and $(5)$ proves $(2)$. Finally, $(1)$ is a special case of $(2)$ when $m_1=\dots=m_n=m$.
Answer: I find that a useful way to think about this kind of problem is in terms of the relation
$$
E[T]=\sum_{t=0}^\infty P(T\gt t)
$$
for random variables that take non-negative integer values. This allows inclusion–exclusion arguments for probabilities to be directly translated into corresponding equations for the expectation values.
Let $T$ be the number of turns required to fill one of the hash tables. By inclusion–exclusion, the probability that at least one of the $n$ hash tables is filled after $t$ turns is
$$
P(T\le t)=\sum_{k=0}^n(-1)^k\binom nkP_k(T_k\le t)\;,
$$
where $P_k(T_k\le t)$ is the probability that $k$ particular hash tables are filled after $t$ turns. If we replace each probability by $1$ minus its complement, all the $1$s on the right cancel, and the $1$ on the left cancels the $k=0$ term, so
$$
P(T\gt t)=\sum_{k=1}^n(-1)^{k-1}\binom nkP_k(T_k\gt t)\;.
$$
Now summing over $t$ and using the relation mentioned at the top yields
$$
E[T]=\sum_{k=1}^n(-1)^{k-1}\binom nkE[T_k]\;.
$$
But $E[T_k]$ is just a scaled expectation in a standard coupon collector's problem: The $k$ particular tables are hit a fraction $\frac kn$ of the turns, and the expected number of hits required to fill them is $kmH_{km}$, so the expected number of turns required is $\frac nk\cdot kmH_{km}=nmH_{km}$. Thus, the expected number of turns to fill at least one of the tables is
$$
E[T]=nm\sum_{k=1}^n(-1)^{k-1}\binom nkH_{km}\;.
$$
For the case $n=2$ treated in the other questions, we recover the result
\begin{eqnarray}
E[T]
&=&
2m\sum_{k=1}^2(-1)^{k-1}\binom 2kH_{km}
\\[8pt]
&=&
4mH_m-2mH_{2m}\;.
\end{eqnarray}
Answer: For an $mn$-sided die with faces in $n$ groups of $m$ faces each, the exponential generating function for the probability of having $j$ complete groups on the $k$th roll is
$$s_j(x) = \binom{n}{j} (e^{x/mn}-1)^{jm} (e^{x/mn})^{(n-j)}$$
for $0 \le j \le n$.  By inclusion/exclusion, the EGF for the probability of having no complete group on the $k$th roll is
$$f(x) = \sum_{j=0}^n (-1)^j s_j(x) = [e^{x/n} - (e^{x/mn}-1)^m]^n$$
This is also the EGF for $P(T>k)$, where $T$ is the first roll on which we have a complete group. So
$$\begin{align}
E(T) &= \sum_{k=0}^{\infty} P(T>k) \\
&= \int_0^{\infty} e^{-x} f(x) \; dx \tag{1}\\
&= \int_0^{\infty} e^{-x} [e^{x/n} - (e^{x/mn}-1)^m]^n \; dx \\
&= \int_0^{\infty} [1-(1-e^{-x/mn})^m]^n \;dx  \\
\end{align}$$
It will simplify notation a bit if we make the substitution $t = e^{-x/mn}$, with result
$$\begin{align}
E(T) &= mn \int_0^1 \frac{1}{t} \cdot [1-(1-t)^m]^n \; dt \\
&= mn \int_0^1 \frac{1}{t} \cdot \sum_{i=0}^n (-1)^i \binom{n}{i} \sum_{j=0}^{im} (-1)^j \binom{im}{j} t^j \;dt \tag{2} \\
&= mn \int_0^1 \frac{1}{t} \cdot \sum_{i=0}^n (-1)^i \binom{n}{i} \sum_{j=1}^{im} (-1)^j \binom{im}{j} t^j \;dt \tag{3} \\
&= mn \int_0^1  \sum_{i=0}^n (-1)^i \binom{n}{i} \sum_{j=1}^{im} (-1)^j \binom{im}{j} t^{j-1} \;dt \\
&= mn \sum_{i=0}^n (-1)^i \binom{n}{i} \sum_{j=1}^{im} (-1)^j \binom{im}{j} \frac{1}{j}
\end{align}$$
Given the wealth of identities on binomial coefficients, it's likely this result can be put in a more elegant form, but I'm not seeing how to do so at present.
Notes:
$(1)$ Making use of the identity $\int_0^{\infty} x^k e^{-x} \;dx = k!$ to sum the coefficients of the EGF
$(2)$ Applying the Binomial Theorem twice
$(3)$ The terms of the summation with $j=0$ sum to zero since $\sum_{i=0}^n (-1)^i \binom{n}{i} = 0$.  This is an important simplification because otherwise we would encounter a divergent integral in the next step.
Edit added 3 Mar 2023:
Making use of the identity
$$- \sum_{k=1}^n (-1)^k \binom{n}{k} \frac{1}{k} = H_n$$
where $H_n$ is the $n$th harmonic number, $H_n  = \sum_{j=1}^n 1/j$, shows that the result above can be put in the form
$$E(T) = mn \sum_{i=0}^n (-1)^{i-1} \binom{n}{i} H_{im}$$
A proof of the identity can be found in the Wikipedia article on harmonic numbers: Harmonic number: Calculation.


Reaper GUI dissapearing

Question: I managed to remove the bar with all the play, loop and stop buttons as well as the time signature and playback rate slider on. How can I get it back? I've tried all of the options under view.
The bar appears just above the mixer which is at the bottom of the window.
Comment: I have already asked it on the reaper forums and got an answer, sorry for posting it unnecessarily here. I didn't know where to ask it.
Comment: I am certain that Reaper has a user discussion forum. You should post your question there because it is full of Reaper experts. There are not many people who use Reaper on this site.
Comment: @WheatWilliams I agree the Reaper forum is better than here for this. At the same time, the question of whether software support is on topic seems to have been asked and answered on meta: http://meta.music.stackexchange.com/questions/2365/music-software-should-we-engage-in-general-troubleshooting
Comment: @DanielCann If you copy and paste the answer here, you can answer your own question and add to the knowledge here, plus maybe get some extra rep for it.
Answer: You can get the view back by clicking:

View > Transport

Reaper GUI dissapearing


NFC enableForegroundDispatch to handle Beam within activity

Question: the app i'm working on ought to receive a beam and then call the processIntent(intent) function from onResume. the problem i'm having is that when it receives the beam, it opens a completely new instance of the app instead of just staying within the activity (i've already called enableForegroundDispatch).
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
    if (mNfcAdapter == null){
        Toast.makeText(this, "No NFC on this device", Toast.LENGTH_LONG).show();
    }
    // Create a PendingIntent object so the Android system can populate it with the details of the tag when it is scanned.
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    // Declare intent filters to handle the intents that the developer wants to intercept.
    IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
    try {
        ndef.addDataType("application/com.sample.mime/string");
    } catch (MalformedMimeTypeException e) {
        Log.wtf("mimeexception", e);
        e.printStackTrace();
    }
    intentFiltersArray = new IntentFilter[] {ndef};

... ...
}

public void onResume() {
  super.onResume();
  // Enable the foreground dispatch when the Activity regains focus.
  NfcAdapter.getDefaultAdapter(this).enableForegroundDispatch(this, pendingIntent, intentFiltersArray, null);

  if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
        processIntent(getIntent());
  }
}

public void onPause() {
  super.onPause();
  // Disable the foreground dispatch when the Activity loses focus.
  NfcAdapter.getDefaultAdapter(this).disableForegroundDispatch(this);
}

void processIntent(Intent intent){
    // do stuff
}

and here's the activity from the manifest
<activity
        android:name="com.example.app.opensthisonebutshouldnt"
        android:label="@string/app_name"  >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
<activity
        android:name="com.example.app.shouldopenthisone"
        android:label="@string/title_activity_create_invoice"
        android:launchMode="singleTop" >
        <intent-filter>
            <action android:name="android.nfc.action.NDEF_DISCOVERED" />\
            <category android:name="android.intent.category.DEFAULT" />\
            <data android:mimeType="application/com.example.nfctest/invoice" />
        </intent-filter>
</activity>

thanks for the input
Comment: Better to Override onNewIntent() If your activity is opened at that situtation it will be called..!
Answer:
application/com.sample.mime/string (used in ndef.addDataType(...);) does not look like a valid MIME type. Are you sure that you are Beaming this type? Even if you do, I'm not sure that Android will be able to process it. A valid MIME type that comes close to yours would be application/com.sample.mime (without the /string part). The same applies to the /invoice part of application/com.example.nfctest/invoice.
If your foreground dispatch intent filter does not match the first record of the Beamed NDEF message, your app may receive NFC events according to intent filters in your manifest. Note that NDEF_DISCOVERED intent filters only match the first record of an NDEF message.
If the Beamed message contains an AAR (Android Application Record) and none of your NFC-related intent filters matches the first record of the Beamed NDEF message, the first activity declared in your manifest that has an intent filter for actuion MAINwith category LAUNCHER will be started. This seems to happen in your case. So double check that your foreground dispatch actually matches the incoming NDEF message. Otherwise your activity may be re-launched according to the definitions in your app manifest.
Checking getIntent() in onResume() will only check the intent currently associated with your activity (i.e. the intent that initially started your activity or that has been registered using setIntent(...)) when your activity is resumed. However, events from the foreground dispatch that you registered will arrive in your activity's onNewIntent() method. So you need to override that method and process the NFC event there:
@Override
public void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
        processIntent(intent);
    }
}

Comment: that was it, the MIME type was wrong so it wasn't being caught correctly.


Would the melodic leap of the opening phrase of Mozart's K545 be considered dissonant?

Question: The Complete Musician by Steven Laitz states that melodic intervals larger than a fifth are considered dissonant and should be avoided. It also states that two leaps of a third are fine, provided you change direction afterwards, which Mozart does.
In Mozart's Sonata K545, the opening melody of the Allegro (one of the simplest and most popular melodies of the period) runs 1-3-5-7-8-9-8.
By the definition given in The Complete Musician, would the jump from the high 5 (G) down to the 7 (B) be considered a melodic dissonance, at it forms a minor 6th? This seems crazy, as the melody sounds extremely natural to me (and, I imagine, everybody else).
Comment: I suspect he just means that we should treat them as dissonant in the sense of requiring some kind of resolution. Thus large leaps are usually best followed by a step backwards, as we see Mozart do in this example, Imagine if the note after the B had been lower. It would sound uncomfortable and clumsy. The step up to C is the 'resolution' of the leap. .
Comment: I'm with PJ here -  "dissonance" can have two meanings.   Do-ti sounds very nice, except that then I wait itchily for the final Do' .
Answer: There are at least two explanations for why this leap is acceptable:

First is the idea of "gap fill," also sometimes called "registral return" or the "post-skip reversal." In short, when there is a large leap, we can soften it by subsequently moving by step in the opposite direction. This is a Gestalt principle of good melodic design that can often explain instances of "rule breaking."
Another concept is that the E and G in the first measure really just embellish the original C. As such, this isn't really a leap from G down to B, but rather just a simple step from the initial C down to B. This is a question of musical hierarchy: C is more important than the E or the G, so we therefore connect the larger-scale motion from C down to B, which is just a half step. Depending on what edition of the textbook you have, Laitz may address this concept in a section devoted to "compound melody."
Comment: A good example of this gap fill is from Mozart's _Te Deum._  http://www3.cpdl.org/wiki/images/5/57/Te_Deum_-_Choral_Score.pdf  On page 15 the tenor sings "In te domine speravi", jumping down a half step, up a tritone, down a half step, and then down a minor seventh.  Kind of bizarre but it works.  Of course, Mozart was twelve when he wrote it...
Answer: It is traditional when teaching vocal writing to advise a preference for smaller intervals, which are easier to sing. A natural accent accrues to large intervals. The term melodic dissonance is sometimes used in connection to large intervals, the larger being more melodically dissonant.
By your book's definition, yes this is a melodically dissonant leap.
The advice to avoid such intervals is typical of advice to beginners not to break the rules until they're understood. Masters such as Mozart may do as they please, but if newcomers write melodies full of large leaps, they are likely to make an unsingable mess.
This usage should not be confused with consonant or dissonant harmony, nor with any idea of what sounds pleasant or unpleasant. It is a perhaps unfortunate piggy-backing of existing terms.
Answer: Could be that this rule in your textbook refers to the melody building of the Gregorian chant.
In the early church music till the time Palestrina there were some rules about intervals in a melody like a major sixth or bigger were considered as not good for singing.
Answer: Laitz's statement doesn't make much sense without some context, but in any case, "so what?" There has never been a "rule" that music must always be consonant.
But what the melody in question sounds like to the OP (and/or "everybody else"  at the present time) is irrelevant. What matters is what it sounded like to Mozart and his contemporaries - who of course had never heard any 19th, 20th, or 21st century music at all.


uniform continuity and equivalent sequences

Question:
Let $X$ be a subset of $\mathbb{R}$, and let $f : X\to \mathbb{R}$ be a function. Then the following two statements are logically equivalent:
(a) $f$ is uniformly continuous on $X$.
(b) Whenever $(x_n)$ and $(y_n)$ are two equivalent sequences consisting of elements of $X$, the sequences $(f(x_n))$ and $(f(y_n))$ are also equivalent.

Proof
First I will state the definitions of uniform continuity and equivalent sequences.

(Uniform continuity). Let $X$ be a subset of $\mathbb{R}$, and let $f : X\to\mathbb{R}$ be a function. We say that $f$ is uniformly continuous if, for every $\epsilon > 0$, there exists a $\delta > 0$ such that $f(x)$ and $f(x_0)$ are $\epsilon$-close whenever $x, x_0 \in X$ are to points in $X$ which are $\delta$-close.

.

(Equivalent sequences). Let $m$ be an integer, let $( a_n)_{n=m}^\infty$ and $( b_n)_{n=m}^\infty$ be two sequences of real numbers, and let $\epsilon> 0$ be given. We say that $( a_n)_{n=m}^\infty$ is $\epsilon$-close to $( b_n)_{n=m}^\infty$ iff $a_n$ is $\epsilon$-close to $b_n$ for each $n\geq m$. We say that $( a_n)$ is eventually $\epsilon$-close to $( b_n)$ iff there exists an $N\geq m$ such that the sequences $(a_n)$ and $(b_n)$ are $\epsilon$-close. Two sequences $(a_n)$ and $(b_n)$ are equivalent iff for each $\epsilon> 0$, the sequences $(a_n)$ and $(b_n)$ are eventually $\epsilon$-close.

since $x\in X$ is an adherent point to $X$, then there exists a sequence $(a_n)$ such that $a_n \in X$ and converges to x. since f is continuous, then the sequence $(f(a_n))$ converges to $f(x)$.
Let $(b_n)$  be a sequence equivalent to $(a_n)$. Therefore, $\forall \epsilon>0, \exists N\text{ such that } |a_n-b_n|\leq\epsilon$. choose $\epsilon=\delta$, then we have $|a_n-b_n|\leq\delta \forall n\geq N$
Hence, $|f(a_n)-f(b_n)|\leq\epsilon\text{   }, \forall n\geq N$
hence, $(f(a_n))$ and  $(f(b_n))$ are equivalent.
Is my proof correct?
Comment: It seems that your proof is nearly correct that $a \Rightarrow b$, though I don't know why you construct $(a_n)$ to be a convergent sequence - you want to show this for all equivalent sequences, and it could be the case that both are nonconvergent. You just need them to live in $X$.

You haven't proved $b \Rightarrow a$.
Comment: Your definition of equivalent sequences $(a_n)_{n\geq1}$ and $(b_n)_{n\geq1}$ simply says that the difference $a_n-b_n$ tends to $0$ (no matter whether or not each single sequence converges).
Comment: Try proving the contrapositive: Assume the negation of uniform continuity: that there exists an $\epsilon$ such that for all $\delta$, there exists points $x_\delta$ and $y_\delta$ that are $\delta$-close but $f(x_\delta)$ and $f(y_\delta)$ are $\epsilon$-apart. Then find sequences that violate (b): equivalent sequences whose images under $f$ aren't equivalent.
Comment: thanks for the correction. To prove $b\implies a$ How can I switch from sequences to functions. Should I use continuity?
Answer: Some remarks concerning your proof:

You write:

Since $x \in X$ is an adherent point, then there exists a sequence $(a_n)$ such that $a_n \in X$ and converges to $x$.

Note that, by definition, we have to consider any two sequences $(a_n)_n$, $(b_n)_n$ in $X$ which are equivalent. This means in particular that $(a_n)_n$ need not be convergent and it does not suffices to prove the statement for some sequence $(a_n)_n$ (convergent to $x$), but we have to prove it for all sequences $(a_n)_n$ (equiavalent to some sequence $(b_n)_n$.)
You write:

Choose $\epsilon = \delta$.

In my oppinion, that's not a good formulation - both $\delta$ and $\epsilon$ are fixed numbers and we cannot simply set $\epsilon = \delta$. In this situation it is better two introduce a new variable in the following way:

Since $(a_n)_n$ and $(b_n)_n$ are equivalent, $$\forall \varrho>0 \, \exists N \, \forall n \geq N: |a_n-b_n| \leq \varrho.$$ Choose $\varrho = \delta$.

(a) $\Rightarrow$ (b) (Corrected version):
Let $(a_n)_{n \in \mathbb{N}}$, $(b_n)_{n \in \mathbb{N}} \subseteq X$ be two equivalent sequences and $\epsilon>0$. Since $f$ is uniformly continuous, we can choose $\delta>0$ such that $$|x-y| < \delta \Rightarrow |f(x)-f(y)|< \epsilon. \tag{1} $$ It follows from the very definition of equivalent sequences that $$\forall \varrho>0 \, \exists N \in \mathbb{N} \, \forall n \geq N: |a_n-b_n| < \varrho.$$ Choose $\varrho = \delta$. Then $|a_n-b_n| < \delta$ implies $$|f(a_n)-f(b_n)| < \epsilon$$ by $(1)$ for all $n \geq N$. Since $\epsilon>0$ is arbitrary, this shows that $(f(a_n))_{n \in \mathbb{N}}$ and $(f(b_n))_{n \in \mathbb{N}}$ are equivalent.
(b) $\Rightarrow$ (a):
Suppose that $f$ is not uniformly continuous. Then there exists $\epsilon>0$ such that for any $\delta>0$, we can find $x,y \in X$ such that $$|x-y|< \delta \qquad \text{and} \qquad |f(x)-f(y)| \geq \epsilon.$$ Choosing $\delta = \frac{1}{n}$ for $n \in \mathbb{N}$, we find sequences $(x_n)_{n \in \mathbb{N}}$ and $(y_n)_{n \in \mathbb{N}}$ such that $$|x_n-y_n| \leq \frac{1}{n} \qquad \text{and} \qquad |f(x_n)-f(y_n)|> \epsilon.$$ Show that $(x_n)_{n \in \mathbb{N}}$ and $(y_n)_{n \in \mathbb{N}}$ are equivalent and that $(f(x_n))_{n \in \mathbb{N}}$, $(f(y_n))_{n \in \mathbb{N}}$ are not equivalent.


Auto remove white background and add alpha channel to images?

Question: I use Drupal 7.41 and created a "customers" view. This view get's nodes from the content-type (also called) "customers".
When I add images, some of them contain white background that conflicts with the sites gray-brick background; Is there a way to automatically make all uploaded images' white background removed and added an alpha channel so there won't be such a conflict?
Note: I can do that process manually with GIMP\Photoshop but looking for a module that does that automatically.
Thanks.
Comment: Is the white background on the image itself? if yes, then you need to edit the image with Photoshop or some other image editing program and crop the image so there is no white.
Comment: actually you could use an image crop module, but you still have to select the area you want to upload. So it's not really automatic.
Comment: Yea I always do this things manually but I though there is something automatic... :|
Answer: I think you will need something like https://www.drupal.org/project/imagecache_actions if this is in fact not a CSS border added to the image. I would definitely check that first but the above module does do some alpha blending and you can also run your own PHP/GD code during an image cache action so if they don't include it you can find code that does and run that code to perform the change.
You could also put a border around the image with CSS that would separate the image from the site background to solve something like that.
Comment: Looks great, I hope they'll port it to Drupal 8 soon :|


Chord progression (Em7,Bm7 what next)?

Question: I'm working on my song and need help with chord progression.
The verse chord progression is : G, Cadd9, A7sus4 and Dsus4
I made a bridge with Em7 and Bm7 but I don't know what need to come next (I sing the next chord but can find him LOL)
So I have 3 questions:
1. How can I find the key of song? (In most popular song I find the key by melody or simple chord progression)
2. How can I improve my knowledge about chord progression and make interesting chord progressions and not simple like this example at F major scale (F,A,Dm,Bb)
3. What need to be the next chord ? :)
Here's the bridge:
https://drive.google.com/file/d/1o2Ie38Qfvi-YU5A7hiRov401oAMqih5n/view?usp=drivesdk
Sorry about audio quality I'm not at my home
Comment: "what chord would be good next" is highly opinion-based, and for the other things there are existing questions and answers if you search this site. If you try major and minor chords for all 12 notes, it will only be 24 things to try, and so it will take at most 30 minutes to cover all possibilities via brute force. :)
Comment: Sadly this site isn't really here to answer questions like this - particularly when we don't have much idea of the rest of the song. As piiperi says, there's 24 simple ones to try, and that's without using 7ths, sus, etc. You could always re-use a previous chord - that's what most songs actually do!
Comment: To be fair, I think that the BEST way to learn is by example ... but this site is a bit challenged in that regard. :/ Even uploading actual music examples is not supported. StackExchange is not really for learning, it's for building some kind of a "database of truths", but the problem is that people who have music-related issues are beginners and they cannot even explicate their questions as text. And even if they could, the terminology and everything would have to be somehow normalized, so the database could be _searched_. So the whole site is kind of designed to fail for many practical uses.
Answer: The real answer is: "any chord you want comes next." That may sound glib, but it isn't. Sure, there are convention chord changes, and you can use those conventions to say "what should come next," but you can intentionally play against conventions for effect, to surprise, or venture off into distance areas in a large composition.
From a conventional point of view...

The verse chord progression is : G, Cadd9, A7sus4 and Dsus4

You can analyze that as (simplified) I IV V7/V V. That is pretty conventional. You have a tonic chord, pre-dominant harmony, and then the dominant. You could paraphrase that as just I... V.

I made a bridge with Em7 and Bm7

Going from G to Em is I to vi or going to the relative minor. That's very conventional for a bridge or middle eight.
In terms of harmonic contrast going to the relative major or minor works, because you change the mode/quality and get a different mood.
But a bridge should also go somewhere. It's a bridge to something. The bridge first provides harmonic contrast, then it goes to some other tonal space. A conventional thing to do is play the dominant chord of wherever you are going, or some progression to the dominant in the key you are going to. If you are going back to G try playing D to conclude the bridge.
Assuming a return to G and an eight bar bridge you could try some things like:
|Em7|Bm7|Em7|Bm7|Em7|Em7| + |D|D7| ...to G
|Em7|Bm7|Em7|Bm7|Em7|Em7| + |Am|D7| ...to G
|Em7|Em7|Bm7|Bm7| + |Em7|Em7|Am|C D7| ...to G
Notice the variation in endings, but they all end on the dominant of G. Using a dominant chord is a very conventional approach to change or return to a tonal area. It's not the only way to do it, but it's common.
On the other hand you could do something more unconventional, like...
|Em7|Bm7|Em7|Em7|Em7|Em7| + |Bm7|B7| ...to |Emaj7|
...a digression into a totally new place: E major.
You can play around with the specific chord extensions and voicings, as well as how many bars to hold a chord. You have latitude to try various options in that regard. But what you really want to ask about the bridge is where do you want it to take you harmonically?

...How can I improve my knowledge about chord progression and make interesting chord progressions...

There isn't really a short cut to understanding. The thing to do is analyse the chord in lots and lots of songs. Ideally you should find a library with a good music score collection and look for songbooks. Combine song analysis with studying a good harmony textbook (like Kostka, Tonal Harmony. or some other college level book.) The important point being it takes time to develop deep understanding rather that just cobbling together common progressions through trial and error.
Answer: Before the question is closed for being off-topic, I want to answer question number 2.

How can I improve my knowledge about chord progression and make interesting chord progressions and not simple like this example at F
  major scale (F,A,Dm,Bb)

I don't know how you can do it, but for me, nothing has been as beneficial as playing songs by ear in different keys. Trying to find the chords without cheating and looking at pre-made transcriptions is of course good, but if you can't find the chords even after trying, it's OK to read them. But then, memorize the chords, and play the song in different keys. And then make changes - substitute something with something, and see how it changes the mood, and the melodic possibilities.
Why this works for me - I think it's learning by example. If I play something without reading, I automatically learn the harmonic patterns that happen, and after playing it long enough, I start to apply the same patterns and suddenly I notice that I've learned it. Compared to this, theoretical thinking has not been very useful for me at all, except for getting names and terms for things. But it's always been like, (1) first learn the actual thing by example, and then (2) find names for the structural patterns, if needed. But it's never like "theory first". If theory is presented first, it's mostly meaningless mumbo jumbo. Ok, I it is possible to proceed even that way, but it's so much more work, it's inefficient waste of time.
So: play songs! You get to experience lots of chord, melody and rhythm patterns first-hand. To learn more interesting chord progressions, play songs that have interesting chord progressions. :) It's unrealistic to expect that you could actually invent or deduce things from abstract formulas.
Answer: Chords are created from scales. Scales are merely combinations of notes.

Go to  https://www.scalechords.com
Select notes you are playing in your melody or scale if you are sure
You will get chords within the selected scale, experiment with different combinations
Make sure to select "Show Chord Tones" in settings
I found your mentioned chords in C Major/Ionian scale
For progressions, stick to the chords of scales (not a strict rule,just a guideline)
always trust your ears
Comment: With the mentioned chords, G major would be a closer bet. Just because something's on the 'net...
Comment: Chords are probably better described as created from intervals above a root, and you guessed the wrong key.
Comment: I didn't go to your web page, sorry, but you shouldn't need an "app" for something as basic as this. :D Why contend with assisted helplessness instead of actually learning even a bit of something. I think you should learn basics and practice actual playing a bit more if you suggest C major for the OP's stuff. :)
Comment: I am a programmer not a musician. I am learning things from people around me like you all. I am here to see what people asks about theory. I dont know much theory/music..music is produced not calculated.. but for new things, we use learning aids. It helps to improve our understanding..I am using too. Saying it is not required is upto you. But if you you check 1 b2 b7 on guitar neck visually, you can find many chord shapes by your own, without finding chord charts.. You just please check what that link is about and can guide me on what things will be helpful. Not all people learn in one way.
Comment: piiperi - I am learning music..I have not mentioned I am a composer. Mentioned chords I saw are  in c major so I said it. I dont recognise key by ear and all.
@Michael, my key may be wrong, but those chords do exist in C Major .. Obviously intervals are the basic building blocks. Scales include Intervals as well..what you said doesnt mean what I said is wrong..I just used other words its like a=b b=c so a=c.
Comment: F# is the only difference apart from the starting note in C  and G major scales. I checked these chords with G scale, same result because F/F# chord is not used in above progression
Comment: I know where you're coming from, so to speak, and I can sympathize. But really, it is essential to be able to feel the tonal center by listening and playing. "These notes exist in this scale" does not cut it, tonal center is not about pitch sets and subsets.
Comment: About tonal center, you are right.. i will keep it in my mind. but my program works well.. will try to add a good algorithm to come up with various chord progressions!
Comment: Downvoted for "always trust your ears", then using software to guess the incorrect key. @abhijeet888 FYI, what key a piece of music is in is always subjective, and I think it'll be very difficult for you to design an algorithm to identify that. It's hard enough teaching that skill to some humans!
Comment: @user45266, Its toooooo difficult knw that very well..I will have to use AI for that ;) . and I am not telling you to produce songs with that program..its my love to music made me create that piece.. I would love to hear about that when you actually see it.. i see humans just give negative comments for things which they havent even seen. I m not a musician but I have learned things which help me understand it.. About key.. I told already that all chords are there in C major too..let it be G scale or Z scale.. I just request people who understand these things to help me improve my program..
Comment: I want to help learners see how these things work on guitar fretboard (piano will be included later)..Its like using DYNAMIC charts..no need to search for different scales and chord shapes.. you get it all there.....We dont need to be guru in something to share what we actually want to show to others..and main thing..I havent said that program guessed any KEY, its you who can see which chords actually EXISTS  in the selected scale..its simple..plzz check it and let me know

Chord progression (Em7,Bm7 what next)?


How to Loop over Numeric Column in Pandas Dataframe and filter Values?

Question: df:
Org_Name   Emp_Name   Age  Salary
0  Axempl    Rick       29    1000
1  Lastik    John       34    2000
2  Xenon     sidd       47    9000
3  Foxtrix   Ammy      thirty 2000
4  Hensaui   giny       33    ten
5  menuia    rony      fifty  7000
6  lopex     nick       23    Ninety

I want loop over Numeric Column (Age, Salary) to check each value whether it is numeric or not, if string value present in Numeric column filter out the record and create a new data frame without that error.
Output :
  Org_Name   Emp_Name   Age  Salary
0  Axempl    Rick       29    1000
1  Lastik    John       34    2000
2  Xenon     sidd       47    9000

Answer: You could extend this answer to filter on multiple columns for numerical data types:
import pandas as pd
from io import StringIO

data = """
Org_Name,Emp_Name,Age,Salary
Axempl,Rick,29,1000
Lastik,John,34,2000
Xenon,sidd,47,9000
Foxtrix,Ammy,thirty,2000
Hensaui,giny,33,ten
menuia,rony,fifty,7000
lopex,nick,23,Ninety
"""

df = pd.read_csv(StringIO(data))
print('Original dataframe\n', df)

df = df[(df.Age.apply(lambda x: x.isnumeric())) &
        (df.Salary.apply(lambda x: x.isnumeric()))]
print('Filtered dataframe\n', df)

gives
Original dataframe
   Org_Name Emp_Name     Age  Salary
0   Axempl     Rick      29    1000
1   Lastik     John      34    2000
2    Xenon     sidd      47    9000
3  Foxtrix     Ammy  thirty    2000
4  Hensaui     giny      33   ten
5   menuia     rony   fifty    7000
6    lopex     nick      23  Ninety
Filtered dataframe
   Org_Name Emp_Name Age Salary
0   Axempl     Rick  29   1000
1   Lastik     John  34   2000
2    Xenon     sidd  47   9000

Answer: I believe this can be solved using Pandas' "to_numeric" function.
import pandas as pd

df['Column to Check'] = pd.to_numeric(df['Column to Check'], downcast='integer', errors='coerce')

df.dropna(axis=0, inplace=True)

Where 'Column to Check' is the column name that your are checking for values that cannot be cast as an integer (or any numeric type); in your question I believe you will want to apply this code to 'Age' and 'Salary'. "to_numeric" will convert any values in those columns to NaN if they could not be cast as your selected type. The "dropna" method will remove all rows that have a NaN in any of your columns.
To loop over the columns like you ask, you could do the following:
for col in ['Age', 'Salary']:
        df[col] = pd.to_numeric(df[col], downcast='integer', errors='coerce')
df.dropna(axis=0, inplace=True)

EDIT:
In response to harry's comment. If there are preexisting NaNs in the data, something like the following should keep any valid row that had a preexisting NaN in one of the other columns.
for col in ['Age', 'Salary']:
    df[col] = pd.to_numeric(df[col], downcast='integer', errors='coerce')
    df = df[df[col].notnull()]

Comment: dropna will also drop actual null values.
Comment: That's a good point. There are other ways to drop the appropriate rows from the DataFrame if there are already existing NaNs in the data.
Answer: You can use a mask to indicate wheter or not there is a string type among the Age and Salary columns:
mask_str = (df[['Age', 'Salary']]
            .applymap(lambda x: str(type(x)))
            .sum(axis=1)
            .str.contains("str"))
df[~mask_str]

This is assuming that the dataframe already contains the proper types. If not, you can convert them using the following:
def convert(val):
    try:
        return int(val)
    except ValueError:
        return val

df = (df.assign(Age=lambda f: f.Age.apply(convert),
                Salary=lambda f: f.Salary.apply(convert)))


Habilitar Downloads em Webview

Question: Olá estou desenvolvendo um app com WebView e percebi que não é possível baixar arquivos da web.
Tentei baixar um arquivo do dropbox,
É possivel que antes de baixar o arquivo apareça um alerta mostrando o seguinte:

Deseja baixar: Nome-Do-Arquivo e os botões Sim e Não

Código já feito:
package br.and.browser;

import ...

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getActionBar().setDisplayShowHomeEnabled(false);
        getActionBar().setDisplayShowTitleEnabled(false);
        getActionBar().hide();

        final EditText url = (EditText) findViewById(R.id.edittext_url);
        final WebView myWebView = (WebView) findViewById(R.id.webview);

        ImageButton Back = (ImageButton) findViewById(R.id.button_back);
        ImageButton Next = (ImageButton) findViewById(R.id.button_next);
        ImageButton Reload = (ImageButton) findViewById(R.id.button_reload);

        myWebView.setWebViewClient(new WebViewClient());
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setSupportZoom(true);
        myWebView.getSettings().getBuiltInZoomControls();
        myWebView.invokeZoomPicker();
        myWebView.loadUrl("https://www.google.com.br/");

        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.goBack();
            }
        });

        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.goForward();
            }
        });

        Reload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myWebView.reload();
            }
        });

        url.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                boolean handled = true;
                if (actionId == EditorInfo.IME_ACTION_GO) {
                    myWebView.loadUrl(url.getText().toString());
                    return true;
                }
                return true;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        WebView myWebView = (WebView) findViewById(R.id.webview);
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Comment: Chegou a implementar o `setDownloadListener`? Acho que essa pergunta pode te ajudar: http://stackoverflow.com/questions/10069050/download-file-inside-webview.
Comment: Já fiz isso mas ao tentar baixar o arquivo abre o navegador padrão
Answer: Consegui com o setDownloadListener, porém na versão do android 6+ o app para de funcionar!
Método para download PDF
wv.setDownloadListener(new DownloadListener() {

    public void onDownloadStart(String url, String userAgent,
                                String contentDisposition, String mimetype,
                                long contentLength) {
        DownloadManager.Request request = new DownloadManager.Request(
                Uri.parse(url));

        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "boleto.pdf");
        DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        dm.enqueue(request);
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
        intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
        intent.setType("*/*");//any application,any extension
        Toast.makeText(getApplicationContext(), "Baixando Arquivo", //To notify the Client that the file is being downloaded
                Toast.LENGTH_LONG).show();

    }
});

Answer: Você pode tentar:
wv.setDownloadListener(new DownloadListener() {
    public void onDownloadStart(String url, String userAgent,
                String contentDisposition, String mimetype,
                long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
});

Você pode checar esse artigo para referência completa (em inglês).
Link da resposta original (em inglês).

Habilitar Downloads em Webview


Transformation linearity and eigenvalues

Question: Consider $E$ as a linear subspace in $\mathbb{R^n}$ with dimension $k$ and F as a linear subspace in $\mathbb{R^n}$ with dimension $n-k$, such that $E\cap F=\{0\}$
The transformation $p:\mathbb{R^n}\longrightarrow\mathbb{R^n}$ defined as followes:
for every $x\in\mathbb{R^n}$ consider the subspace $F^\prime$ that goes throgh $x$ and parallel to $F$ with the same dimension as $F$, since $E\cap F$ is a point, so $E\cap F^\prime$ is a point too, name this point $p(x)$
First I want to prove that this transformation is linear, so I should prove that for every $x\in\mathbb{R^n}$ we have:

$p(x+x^\prime)=p(x)+p(x^\prime)$
$p(cx)=cp(x)$

I know that $F^\prime$ is $F$ transferred by $x$, so these 2 conditions  seems true but I don't know how to express my prove mathematically.
Then I want to find a base for $\mathbb{R^n}$ like $\mathfrak{B}$ such that:
$M^{\mathfrak{B}}_{\mathfrak{B}^\prime}(p)=\begin{bmatrix}
I_k & O\\O&O
\end{bmatrix}$
I found this for $\mathfrak{B}$:
$\mathfrak{B}={\{e_1,e_2,\dots,e_k,f_{k+1},\dots,f_{n}\}}$
where $e_j$ are elements of $E$ and $f_j$ are elements of $F$
Is this correct?
And how do I can find eigenvalues and characteristic polynomial of $p$?
Comment: $p$ is indeed linear and is called the projection onto $E$ parallel to $F$. The eigenvalues of $p$ are zero and $1$ because $p(e) = e$ for $e\in E$ and $p(f) = 0$ for $f\in F$.
Answer: Let's work with the basis you suggested: Let $e_1,\ldots, e_k$ and $f_{k+1},\ldots, f_n$ be bases for $E$ and $F$ respectively. Then $\mathfrak B = \{e_1,\ldots, e_k, f_{k+1},\ldots, f_n\}$ is a basis for $\mathbb R^n$ (since $E$ and $F$ have trivial intersection). Let $x=(x_1,\ldots, x_n)$ in this basis, and let's compute $p(x)$.
Edit: Remember that $p(x) \in E\cap F^\prime$. Because of the way we chose the basis, a vector in $E$ is of the form $(a_1,\ldots,a_k,0,\ldots,0)$ for some $a_i\in\mathbb R$. Similarly, a vector in $F$ is of the form $(0,\ldots,0,b_{k+1},\ldots,b_n)$ for some $b_i\in\mathbb R$, which means that a vector in $F' = x+F$ is of the form $x+(0,\ldots,0,b_{k+1},\ldots,b_n)$. Since $p(x)$ is both a vector in $E$ and in $F'$, we can now write $p(x)$ in two ways:
$$
p(x) = \begin{pmatrix} a_1 \\ \vdots \\ a_k \\ 0 \\ \vdots \\ 0 \end{pmatrix}
\qquad {\rm and} \qquad
p(x) = x + \begin{pmatrix} 0 \\ \vdots \\ 0 \\ b_{k+1} \\ \vdots \\ b_n
\end{pmatrix}
= \begin{pmatrix} x_1 \\ \vdots \\ x_k \\ x_{k+1}+b_{k+1} \\ \vdots \\ x_n+b_n
\end{pmatrix}
$$
(Edit end)
We see that this forces $a_i=x_i$ for all $1 \le i \le k$, so that we have the unique solution:
$$
p(x) = \begin{pmatrix} x_1 \\ \vdots \\ x_k \\ 0 \\ \vdots \\ 0
 \end{pmatrix}
$$
We see that $p$ is the linear transformation given by the matrix you wrote. The characteristic polynomial is:
$$
{\rm det}(xI-M) = (x-1)^kx^{n-k}
$$
with eigenvalues 0 and 1 (assuming $0<k<n$).
Comment: Thank you very much for your answer, But I didn't understand the part which you wrote $p(x)$ in two ways. Would you mind explaining it a little more?
Comment: Of course, I can see I went a bit fast there. See if my edit clarifies things for you.


CSS for Display Suite Fields not applied

Question: I'm attempting to place text next to the top of images inline for a content type using CSS on fields through Display Suite with a built-in layout in Panels (Two column bricks)
I am using a subtheme for the professional_theme.

I have tested and ensured the style sheet is over-ridden to my subtheme
I have inserted the css shown below (and tried several other css codes)
I have inserted the css names into the CSS Classes of Display Suite
I tried applying the styles to several different fields as well as
the region independently and together.

That all being said I think there is extra CSS code being inserted that is throwing off the style being pulled in but I have no idea where it is coming from.
The code below displays Source Code with the CSS style I applied to the image and a text field:
The names of the only styles I applied are: .floatleft .floatright

Are you the Kind to be Kind

Tuesday, October 27, 2015 - 09:30

You are in charge of your own destiny, don't try to control theirs.

CSS CODE: Keep in mind that I have tried other CSS codes
.floatleft {
clear: both;
vertical-align: top;
float: left;
padding: 4px 10px 4px 0;
}

.floatright {
clear: both;
vertical-align: top;
float: right;
padding: 4px 0 4px 10px;
}

Any help is greatly appreciated. I tried finding CSS documentation for Display Suite with little luck.
WO
Answer: First of all, remove the . from your classes in your HTML code, e.g. you should have
 and not
.
If you want to have one element next to another by using float, your combined element's width should be smaller than their container's. And since both your image and the text are inside divs and a div has 100% width by default, you have to specify a smaller width to each.
.floatleft {
    float: left;
    width: 40%;
}

.floatright {
    float: right;
    width: 30%;
}

Just change the widths accordingly.
Comment: Wow, I figured the problem was going to be something simple but not as simple as removing a period. Thank you so very much Typologist!
Comment: Cool! I'm glad it worked :)


How do I achieve addition of each rows in T-SQL?

Question: I have my data which may be around 50 records for each address:
Id  AddressId   Income  Expense Revenue
----------------------------------------
1   1           100     200     300
2   1           150      20     200
3   1           160      80     800
4   1            50      90     200
5   1           600     700     500

Now  I need my data in the following format:
Ids       Count Income  Expense Revenue
---------------------------------------
1           1    100     200     300
1,2         2    250     220     500
1,2,3       3    410     300    1300
1,2,3,4     4    460     390    1500
1,2,3,4,5   5   1060    1090    2000

Every row is being added one after another.
For example:

The Ids 1,2 is a sum of Id 1 and 2
The Ids 1,2,3 is a sum of Id 1 and 2 and 3 and so on

I don't need the Ids column, the only thing I need is the sum
Comment: This sounds like something that would be trivial to do in the code that consumes the data, and awkward to do in TSQL
Comment: @MarcGravell actually quite easy to do with TSql, as long as you are not working an a really old version, using the `over` clause with `sum` and the propert `partition by` and `order by` makes it a walk in the park...
Answer: You could use STUFF, ROW_NUMBER() OVER(), and SUM() OVER() like this
DECLARE @SampleData AS TABLE
(
    Id int,
    AddressId int,
    Income int,
    Expense int,
    Revenue int
)

INSERT INTO @SampleData
VALUES
( 1, 1, 100, 200, 300),
( 2, 1, 150, 20 , 200),
( 3, 1, 160, 80 , 800),
( 4, 1, 50 , 90 , 200),
( 5, 1, 600, 700, 500)

SELECT
      STUFF(
             (
             SELECT ',' + CAST(sd1.Id AS varchar(10))
             FROM @SampleData sd1
             WHERE sd1.AddressId = sd.AddressId AND sd1.Id <= sd.Id
             FOR XML PATH('')
             ),
          1,1,'') AS Ids,
      Row_number() OVER(PARTITION BY sd.AddressId ORDER BY sd.Id) AS Count,
      sum(sd.Income)  OVER(PARTITION BY sd.AddressId ORDER BY sd.Id) AS Income,
      sum(sd.Expense)  OVER(PARTITION BY sd.AddressId ORDER BY sd.Id) AS Expense,
      sum(sd.Revenue)  OVER(PARTITION BY sd.AddressId ORDER BY sd.Id) AS Revenue
FROM @SampleData sd
ORDER BY sd.AddressId, sd.Id

Demo link: http://rextester.com/HRIWH92029
Note: The last revenue should be 2000 instead of 1600
Answer: If you're using SQL server 2012 and above,
please use below query for the sum up the previous rows
Select ID,
count(*) OVER (PARTITION by AddressID
ORDER BY ID
ROWS BETWEEN unbounded PRECEDING AND current row) as[Count],
sum(Income) OVER (PARTITION by AddressID
ORDER BY ID
ROWS BETWEEN unbounded PRECEDING AND current row) Income,
sum(Expense) OVER (PARTITION by AddressID
ORDER BY ID
ROWS BETWEEN unbounded PRECEDING AND current row)Expense,
sum(Revenue) OVER (PARTITION by AddressID
ORDER BY ID
ROWS BETWEEN unbounded PRECEDING AND current row) Revenue from TableName

Answer: If you're using SQL server 2008 and below, please use below query for the sum up the previous rows.
Select ID, (select count(*) from Tablename A where A.Id<=Tablename.ID)[Count],
(select sum(Income) from Tablename A where A.Id<=Tablename.ID) Income, (select
sum(Expense) from Tablename A where A.Id<=Tablename.ID) Expense, (select
sum(Revenue) from Tablename A where A.Id<=Tablename.ID) Revenue from Tablename


UITextview edit mode in swift

Question: I would like to edit some text in UITextview but whenever keyboard comes, it clear the previous text . I want to previous text on keyboard open as well.
Any one has idea how to do it in swift 3.0?
I am setting the string of UITextView in viewdidload
treatment.text = patient?.patientTreatment;
   func textViewDidBeginEditing(_ textView: UITextView)
{
    if textView.text != ""
    {
        textView.text = patient?.patientTreatment
    }
}

I observed 1 thing here, My text is disappear once keyboard opens and comes back once keyboard closes.
Comment: you want same as placeholder text ?
Comment: pls more details, share your code
Comment: You are talking about `UITextView` right? Or it's `UITextField`?
Comment: delete everything inside your `func textViewDidBeginEditing(_ textView: UITextView)` and test once.
Comment: Yes I did that and I have deleted the treatment.becomeFirstResponder() also, It is not taking the focus automatically and not clearing the text.
Answer: for that you need to check the condition whether the string present inside the UITextView your PlaceHolder or not
you can use the following
  func textViewDidBeginEditing(_ textView: UITextView)
  {
        if textView.text == "YOUR_PLACEHOLDER"  {
          textView.text = ""
        } else if textView.text == ""  {
          textView.text = "YOUR_PLACEHOLDER"
        }
  }

Comment: He doesn't want to clear the `textView`. Read the question carefully.
Comment: if we use the text as placeholder means we can clear that otherwise that will  not.Check the above code once you will got the result
Comment: I am using UITextView. I want to 2 thigns here. 1. Adding the placeholder if string is empty. 2. Placing the text if string is not empty. Right now string is clear automatically once keyboard opens.
Comment: @GaneshManickam : I know this will solve the problem of placeholder. My problem is , I have a UITextview in which I set a string in starting. once I click on the uitexview for editing anything, it is clearing my data , I want my previous data to be there while editing it.
Comment: check the value present in `patient?.patientTreatment` or not. I think that will be the problem
Answer: Here it's an example.
Inside your viewDidLoad add this:
yourTextView.text = ""

And use this delegate method as it is:
func textViewDidBeginEditing(_ textView: UITextView) {
    if textView.text.isEmpty {
    // your textView is Empty, set your Placeholder
        textView.text = "Placeholder"    // Use your desired String here
    } else {
    // your textView contains some texts, change it according to your needs
        textView.text = "My Text"        // Use your desired String here
    }
}

Answer: Try this
func textViewDidEndEditing(_ textView: UITextView) {

        if textView.text.isEmpty{
            textView.text = "Enter description here..."
            textView.textColor = UIColor.lightGray
        }else{

              textView.text = "My description"
          }
    }

Answer: set placeholder in UITextView
extension EditProfileVC:UITextViewDelegate{

    func textViewDidBeginEditing(_ textView: UITextView) {
        if textView.textColor == UIColor.lightGray {
            textView.text = nil
            textView.textColor = UIColor.black
        }
    }
    func textViewDidEndEditing(_ textView: UITextView) {
        if textView.text.isEmpty {
            textView.text = "Placeholder"
            textView.textColor = UIColor.lightGray
        }
    }
}

UITextview edit mode in swift


Is there a module which allows users to edit text together?

Question: I am building a site which allows users to upload handwritten images and transcribe them. Ideally, I would like if there were a text box type of module which updated the default value of the box each time it was written to.
Example:

Text box is created: "Default text"
Alice updates text box: "Alice was here!"
Text box now reads "Alice was here!" to all users.
Bob updates text box to "Alice was here! And bob!"

Is it possible to do something like this using modules? If so - which?
The page layout will look something like this:
<code>[ Slide show ]
[ Transcription box ]
[ Comments ]
</code>
Ideally it would have the following functionality:

The image must appear on the page alongside the text editor (I had a WYSIWYG in mind but haven't been able to crack it yet)
Version control for text
Load 'default text'
Saving would overwrite the 'default text'

Is it possible to embed the transcription box in to the page? It is important that the user is able to view the image while editing the text box.
Thanks
Comment: Hi @Pierre.Vriens , thanks for your response. I edited my post to explain why using a 'body field' is not an appropriate solution. If you have any additional insight, please let me know :)
Answer: Maybe you want to consider using the Paragraphs module for this. Here are some quotes about it (from its project page):

Paragraphs is the new way of content creation!
  It allows you — Site Builders — to make things cleaner so that you can give more editing power to your end-users.
Instead of putting all their content in one WYSIWYG body field including images and videos, end-users can now choose on-the-fly between pre-defined Paragraph Types independent from one another. Paragraph Types can be anything you want from a simple text block or image to a complex and configurable slideshow.
Paragraphs module comes with a new "paragraphs" field type that works like Entity Reference's. Simply add a new paragraphs field on any Content Type you want and choose which Paragraph Types should be available to end-users. They can then add as many Paragraph items as you allowed them to and reorder them at will.

Some other questions on Drupal.SE where the Paragraphs module has also been suggested as a possible answer (which may help for more inspiration about what kind of things you can do with this podule):

Modular Drupal content-type (variable sections).
Simulating TYPO3-Template-Engine.
Alternative to Field Collection.
What is the best method to allow a user to attach dynamic, custom content sections to a node?


What does this crossed-note notation mean?

Question: What does this notation mean?

It is in 5/8 time. Is it some sort of a performance direction?
Comment: @Tim We don't have a rule against homework.  It's one thing if it's low quality in which case we treat it like any other post: http://meta.music.stackexchange.com/a/2148/28 and http://meta.music.stackexchange.com/q/304/28
Comment: I'm voting to close this question as off-topic because this site isn't about completing someone's homework.
Comment: How about a dupe: http://music.stackexchange.com/questions/7527/four-dots-over-tremolo-minim-half-note-with-slash
Answer: The beam fragments drawn through stems mean that each of the 8th notes should be divided into two 16th notes.
This has nothing to do with the 5/8 time, it's simply a shorthand notation to save writing a lot of note heads, particularly for dense tremolos.


What is the purpose of using MatchIt in R with nearest neighbors for estimation of ATE if it does not report which pairs are matched?

Question: I am currently using the matchit function in MatchIt in R. For example, in the call:
m.out1 <- matchit(treat ~ age + educ + black + hispan + nodegree + married + re74 + re75, method = "nearest", data = lalonde)
> head(match.data(m.out1))
    treat age educ black hispan married nodegree re74 re75       re78
NSW1     1  37   11     1      0       1        1    0    0  9930.0460
NSW2     1  22    9     0      1       0        1    0    0  3595.8940
NSW3     1  30   12     1      0       0        0    0    0 24909.4500
NSW4     1  27   11     1      0       0        1    0    0  7506.1460
NSW5     1  33    8     1      0       0        1    0    0   289.7899
NSW6     1  22    9     1      0       0        1    0    0  4056.4940
      distance weights
NSW1 0.6387699       1
NSW2 0.2246342       1
NSW3 0.6782439       1
NSW4 0.7763241       1
NSW5 0.7016387       1
NSW6 0.6990699       1

no where does it display which treated units are paired with which control units. It seems to just be a jumble of the original data, but now with some removal of control units that could not be paired.
I fail to see the purpose of matchit in this case. If I want to find the ATE after matching, how would I go about this?
Answer: There are two components to matching: creating matched samples and pairing units.
Creating matched samples involves pruning away individuals in the control group that are dissimilar from units in the treated group. We assign weights of 0 to these pruned units and weights of 1 or more to the remaining units. match.data() creates a new data set that removes everyone with a weight of 0. Effect estimation on the matched sample involves applying these weights in a weighted independent samples t-test of the outcome on the treatment. This is the technique recommended by Stuart (2010) and others, mostly from Rubin's camp.
Pairing units involves creating subsets of the matched sample, where each subset contains at least one treated and at least one control. Effect estimation in a paired sample involves performing a related measures t-test or a regression that accounts for the clustering of units (i.e., either by including pair membership as a fixed or random effect or using them in cluster-robust standard errors). This technique is recommended by Austin (2011) and others, mostly from Rosenbaum's camp.
Propensity score matching yields some controversy here. You perform the act of pairing in order create matched samples, but as I mentioned above, there is debate about whether you should retain the pair membership of the units in your analysis or just use the matched samples and their weights to estimate the treatment effect.
The authors of MatchIt, which include Stuart, recommend using the matched samples approach (not the paired units approach) to estimating treatment effects after matching. Therefore, if you did 1:1 matching without replacement, you can just do a t-test on the remaining units resulting after match.data(). Otherwise, you need to include the weights generated by matchit() in a weighted analysis (which will also work with 1:1 matching without replacement). Regardless, it would be advisable to use robust standard errors, though this hasn't been recommended explicitly in the literature.
However, if you want to move forward with a paired analysis, you will have to do some work. The pair membership is in the m.out1 object, in the match.matrix component. This is a matrix where the row names are the indices of the treated units, and the values in the columns are the indices of the control units that are paired with each treated unit. You can extract the pairs and create the pair subsets with a bit of R finagling, and then run a paired analysis on these pairs. (Note that if you're doing k:1 matching and not all treated units have k matches, there will be NAs in the match.matrix, which can be ignored.)
One final thing to note is that the ATE cannot be estimated from matched data resulting from a call to matchit(). Only the ATT can be estimated. This is because the covariate distribution of the matched samples will be similar to that of the treated group, and therefore the estimated effect is not generalizable to the population at large.
Edit 2022/01/05:
MatchIt now includes pair membership in the matched dataset for all matching methods that involve pairing. The MatchIt vignettes explain how to incorporate pair membership in effect estimation after matching, which is now considered best practice. Also, the ATE is available for some matching methods (full matching, subclassification, etc.)
Comment: Thanks for the post. Can you elaborate on your last sentence about how the ATE can't be estimated? Is this a general result regarding propensity score matching or just how `matchit()` does everything?
Comment: In general the ATE cannot be estimated with matching, although I think you may be able to with the `Matching` package and possible `optmatch`. The reason you typically cannot estimate the ATE is that you pick control units that look similar to the treated units, so the matched sample looks similar to the treated units. To estimate the ATE, you would need the matched sample to look similar to the overall population.
Comment: Thanks for your comment, that makes a lot of sense. I'm just curious, would you be able to direct me to any resources or if you know off the top of your head which assumptions mathematically do not hold that imply we cannot normally obtain the estimate of the ATE?


Is there any good reason not to use IPv4 only within a local network?

Question: We have a small office with maybe 10-20 devices connected to our local network (LAN). We are running an internal DNS for resolving local network domains. The DNS has been configured with only IPv4 addresses, because it was easier to set up like this, and there seems to be no reason to use IPv6 addresses for our local network devices. None of the devices needs to be reached from outside (WAN).
Am I overlooking any good reason to use IPv6 within the local network?
Note that this question seems to be a duplicate of Why would you use IPv6 internally? only at the first glance. My question is about using IPv4 internally, while the other question discusses NAT etc. which doesn't apply here.
Comment: I think you've got the question backwards. Do you even have a reason to use IPv4 at all? Why isn't everything IPv6? It's much easier to work with and solves a wide variety of problems with IPv4.
Comment: @MichaelHampton Well, as stated in my question: "because it was easier to set up like this". This may be due to my limited knowledge about IPv6 configuration, yet that's a valid reason in my eyes.
Answer:
None of the devices needs to be reached from outside (WAN).

Yet.
Some day you merge with a different company, who also uses the 10.0.1.0/24 block, and they want to integrate your network with theirs. Things turn ugly, and everyone asks why RFC1918-addresses were used.
The IPv6 ULA is big enough that you can pick a range, and be reasonably sure that noone else will use the same range, if you pick a random /64 or four.
Comment: Why could this a problem beyond just exchanging the IP addresses in the local DNS?
Comment: Because that day you suddenly have multiple networks which is statically configured in various routers and devices, and you spend a week readdressing stuff. Of course, *if* you do everything by the book *always* it won't be a problem. But that's unlikely to happen; people will take some short cuts, documentation won't be perfect and so on.
Answer: In your case, there is no practical benefit to use IPV6. IPV4 will serve all your requirements.
However, if you want to be ready for future ahead of time, you can play with IPV6. But it may be another decade or two when IPV6 becomes more prevalent.


Exportar html para PDF com html2pdf background-size nao funciona

Question: Boas,
Estou tentando converter uma pagina html para pdf utilizando html2pdf, mas ao que parece o background-size:cover nao esta funcionando.
Na minha pagina web a minha imagem fica assim:

Ao converter para PDF a imagem perde as formaçoes css e fica assim:

Meu código e o seguinte

Alguem sabe alguma forma de eu conseguir que a imagem no pdf preencha a div na totalidade sem repetição?
Comment: Poste o link da lib que vc está usando, qual é esse html2pdf que vc usa, tem o link do projeto dele?
Comment: No exemplo que postou o `background` está escrito de forma incorreta - `backgroud-repeat: no-repeat;`. O seu código está assim também?
Comment: dua div utiliza uma classe imagem, mostra pra mim o que tem nessa classe
Comment: .imagem{border: 7px solid #e0e0e0; border-top-left-radius: 50px; border-bottom-right-radius: 50px; height:340px; width:265px; overflow:hidden;}
Answer: Edição em 4/7/19. A implementação https://github.com/spipu/html2pdf, de fato, praticamente ignora a propriedade background-size, e esse não é o primeiro problema deles com CSS. Você pode contribuir no projeto, melhorando-o ou apenas informando o problema, ou pode apenas ajustar as dimensões da imagem manualmente de modo que fique satisfatória no PDF. Ou, além disso, pode seguir a sugestão abaixo.
Existem várias implementações de html2pdf. Eu sugiro a você o wkhtmltopdf. Ele é bastante poderoso para reproduzir HTML's não triviais em PDF, por usar o motor do Qt WebKit.
Por exemplo, você não expos o que tem na sua classe CSS imagem (class="imagem"), mas para preencher uma
 com uma imagem, com as dimensões, seria algo mais ou menos assim (testado no Chrome, Firefox e Safari):

#com_imagem_de_fundo {
  height: 256px;
  width: 512px;

  background-image: url(https://ichef.bbci.co.uk/news/660/cpsprodpb/0288/production/_105284600_44935b21-9f20-4727-a2c0-84bef85a4548.jpg);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

Assumindo que os trechos acima estão dispostos adequadamente num arquivo pagina.html, gere o pagina.pdf pela linha de comando:
$ wkhtmltopdf pagina.html pagina.pdf

Ou, no PHP, usando o phpwkhtmltopdf:
<?php
require __DIR__.'/vendor/autoload.php';
use mikehaertl\wkhtmlto\Pdf;

$pdf = new Pdf('pagina.html');
if (!$pdf->saveAs('pagina.pdf')) {
  $erro = $pdf->getError();
  // ... lide com o erro
}
?>

Rode composer require mikehaertl/phpwkhtmltopdf no diretório de pagina.html, para testar. Observo que o comando wkhtmltopdf deve estar já funcionando no sistema (phpwkhtmltopdf é um pequeno wrapper).
Comment: @TiedtTech, veja https://github.com/dompdf/dompdf , como outra alternativa. Mas já observo que este está em conformidade com [CSS 2.1](https://www.w3.org/TR/2011/REC-CSS2-20110607/colors.html#q14.0), no qual ainda não está definida a propriedade *background-size*; além disso, no *dompdf* a propriedade *float* está em desenvolvimento
Comment: Mas o `wkhtmltopdf` não tenho que instalar algo no servidor? As vezes não temos acesso ao servidor por isto ele é um pouco mais complicado utilizar.
Comment: Sim, ele deve ser instalado no sistema. Apesar disso, é uma boa alternativa. ;)


What hardware for powerfull server setup

Question: Sorry if it's a bit too broad question...
I'd like to have my own personal server, mainly for php websites (although I might later add some features when I'll need them... like Jabber, mail, etc). I might later decide to also offer some space for clients, but for now it will be for my own use and tests. Its main role will be LAMP.
The thing is that I never worked with 'real' server hardware... I know how to run server software, etc, but now I only have my desktop home pcs, laptops, etc... And I would like to buy some good quality hardware for this setup (so after I'll learn enough I might try to also offer some space for clients).
By hardware I mean:

powerful server like for professional hosting setups (where i can change faulty components live without rebooting it, etc)
emergency power source? how do i get something like this?
i guess reliable server should have backup? good cooling? good UPS?
anything else that is important in powerful setup that you could recommend?

Also please note that I'm not interested in VPS reseller, I would like to learn with physical hardware.
Comments, links to example hardware stores (I mean... where do I get this stuf?? Hah) and links to any external resources will be helpful.
Edit:
About hotswapping software support - I would probably use Debian as my OS.
Noise... what about it? I would probably like to have my server where I live.
Answer: What kmarsh said, plus

64bit, with more than 4gb of memory
A Remote Console solution (ILO/RSA - NOT something like VNC - you need hardware level remote access) if you're going to locate the server somewhere you can't physically get to it 24/7
Multiple NICs (at least 4), that allow NIC Teaming
The most important bit: Maintenance Contract (4/8/12hour response time etc)
Comment: +1 oh, yes, maintenance contract!
Comment: You could say "Remote Console" or something more generic than ILO/RSA.     I also don't think "every" server needs 4 NICs, especially just starting out.
Comment: Depends on how serious you want to be. We team the NICs into pairs, and each pair is a Smart Load Balancing & Failover team plugged into separate switches. Peace of mind.  Also, most pizza-box servers come with 2 NICs onboard.  It isn't going to break the bank to shove in an extra card with 2 NICs
Comment: thanks Izzy, kmarsh, Kevin... great answers, I'll vote you up when I'll have enough rep to vote
Comment: Can you accept an answer for this thread with low rep?
Comment: If you ask a question, you can accept an answer regardless of your rep.
Answer: Any server:

Should have ECC memory
Should have 2 power supplies
Should have at least a mirrored pair
of disks or 4-disk RAID 5 with hot
spare plugged in.
Should have room for growth of RAM and storage (external OK), for example an slot free for an FC card or free SAS ports and room for more disks.
Should be on a UPS with more than sufficient runtime to a.) endure a typical power outage for your area (say, 10-15 minutes) plus time to shut down (another 10-15 minutes).
Should have a backup solution integrated from the start (can be external)

The fastest servers are rarely as fast as the fastest gaming systems, nor do they need to be. Servers are more about capacity and throughput, while gaming systems are more about reducing GUI latency. Indeed, most servers have fairly lame graphics hardware.
Answer: Form Factor
I think that you'll have better luck with what you're talking about if you go for a rackmount server. However, those are noisy(they typically live in a datacenter where people aren't, so it's not usually a problem).
UPS
You'll want a UPS & possibly a generator outside for those times when the power goes out for a while, although depending on reliability of power, you might be able to get by with just a UPS. APC has some good ones, and they even have a sizing guide.
Hard Disks
kmarsh mentioned RAID 5(3 disks) although RAID 1(2 disks) would also work. Going from 1 to 5 is a speed boost.
Power Supplies
kmarsh also mentioned redundant power supplies. You should definitely get those.
Network
Dual NICs are also a requirement. Linux can bond two NICs together so that it looks like one address. You'd then connect these to redundant switches/routers/firewalls so that either of them could go down without affecting uptime.
Hot Swapping
You mention that you'd like to change faulty components without rebooting; that's known as hot-swapping. That's commonly found in a rackmount.
For all the peripheral components(power supplies, HDs, DVD/CD drives, any floppy drives, & memory), you should check with the manufacturer that they're hot-swappable. Hot swapping has to be supported by the host OS, the motherboard, & the component you're swapping. You mentioned Linux; that does support hot swapping. We use Dells for all of our hardware; they have pretty good support. Also, the support we have will make sure that everything is hot-swappable.
Remote Access
We also have Dell RACs in our servers; the Remote Access Cards allow you to remotely power on/off/reboot the server, connect to the console, or put in a CD without being there. It's like being at the console without driving/flying out there. About the only thing you can't do(that you would be able to onsite) is to replace faulty hardware.
Comment: "You mention that you'd like to change faulty components without rebooting; that's known as hot-swapping and I think that it's more commonly found in a rackmount."  False.  These features are more commonly found in good servers regardless of the case style.  I have two Dell PowerEdge systems that have hot-swappable power supplies and hard disks, but are not in rack-mount cases.
Comment: @phuzion: Whoops. Now that I think about it, we have a customer that does that a lot. Thanks.
Comment: Cool, I'm using APC on my PC xD.

That's something I didn't thought about... noise. How noisy usually servers are? Is it possible to limit noise? Any suggestions? Btw I'll probably want to use Debian for it (I think... that's what I have most experience with)
Comment: Noise! This is a factor. For example, Sun "T" servers are VERY noisy. I wouldn't want to work beside one all day.
Answer: I recommend a DL100 or DL300 series from HP.  Get dual power supplies, hardware RAID controller, lots of RAM, and some nice disks.  You'll be able to swap disks and power supplies without downtime, and having dual power supplies allows you to move to different power circuits / UPS feeds without downtime too.  It will also come with an iLo interface for out-of-band management magic.  Those are the main features of "server-class" hardware, IMO.
Next, buy a nice quarter-height rack with some integrated fans, and put a small UPS in the bottom of it.  Throw away all of your DLink gear and buy rack-mountable SOHO routers and switches.  Buy a variety of lengths of Ethernet patch cables, and a bunch of velcro cable ties to make it all purty.  APC has very nice stuff for this.
Comment: Actually if you can get them before they are gone, you can get great deals on the HP ML 150.
Answer: Your best bet is to find used hardware on Ebay, actually. In fact, that's what we use where I work - we don't have an enormous client base, so there's no great need for super-powerful, cutting edge stuff. What we do need however, are servers that are super-reliable. Most of our servers are various models of Dell Poweredge servers. 2450's are what we use for our DNS servers and our own website and even that is overkill. But they're also very cheap and fairly easy to find. We have a 6650 for our hosted websites and that does its job well, even with several popular sites that use CMS's like Mambo.
Yes, you'll need a good UPS and cooling and backups. The UPS might also be something you can snag on Ebay, but you'll have to make sure the batteries are new. I have no idea where you could get a bargain on air conditioning though. We use a consumer-grade computer in a server case for our backup machine (hard drives are cheap, tape drives are expensive, and you can't automate them worth a damn anyhow). The backup machine could go up and down like a yo-yo and your customers won't notice. And if the drive fails (or if you really like, the software RAID array) then you have until your next data catastrophe to replace it. 6 hours of downtime on this box is also something your customers won't notice.
Now, the bill for all this... will likely be more than a beer budget. For this reason, I don't really know why you don't go in for formal training, because the tuition would be about as much, and you'd probably get to work with newer hardware. Moreover, there's nothing to it. This kind of hardware is so reliable that you hardly even need to interact with it, and you just end up going by what the manual says. The software on the server will be much more complicated, in fact.
Comment: I don't have anything against paying more and starting out with completely new and fancy hardware. I'd like to have as much fun with it as possible, investing in hardware isn't an issue for me. I'm also not that much into training at all... rather wanted to explore something new and interesting by myself. Thanks for your suggestions, though. *looks at specs*
Comment: If you buy on eBay, you have no service warranty, which means you will have to purchase your spares ahead instead of rely on 4 hour service contracts. That can eat up a lot of your savings.
Answer: For play at home (and your learning process), buy used as Ernie and some others have said and by all means use it at home.  It won't be quiet - most proper servers sound like a jet engine when they are starting up.
For anything where you are hosting for your clients on the Internet however, renting is always better than buying.  You aren't going to have a diesel generators, multiple power grid connections, multiple internet connections, N+1 UPS, N+1 cooling, everything.
Comment: ...well, yes, actually what I'm asking about is how do I get diesel generators, multiple power grid connections, multiple internet connections (hmmmm? maybe that's really something to think about. didn't thought about it...), cooling, etc. I mean - some people are actually starting these datacenters, eh? All those resellers have to buy from someone ;). I'm trying to learn how to be that 'someone'. Btw if you're saying they're terribly noisy then I would need separate building?
Comment: Well, like a jet engine but not as loud. There may have been an exageration there.

Know what I reccommend?  Check out a site like telco hotel and find a nearby facility. Find a provider in that facility, and ask for a tour (assuming you haven't done that).
Comment: www.carrierhotels.com for instance.


Why is constrained RDA symmetrical?

Question: I am having a very weird behaviour when performing constrained RDA. The hypothesis I am testing is: is there a difference between treated and control samples? The experimental design comprise 10 sites, where a control and a treatment were sampled.
As far as I understand, this is the classical example of random effects caused by the sampling site so my model would be:
data_agglomerated_at_taxa_level ~ Treatment + Condition(Site)
However, something very interesting happens: as you can see from the picture:

what I am observing are samples that are symmetrical (I and III quadrants, II and IV quadrants). Each couple of symmetrical data point is the couple $(control_{s}, treatment_{s})$, $s = site$.
If I remove the constraint on the Site, then the data points are not symmetrical anymore so, as far as I can tell, there is something happening with the Site. So said, I can't really understand what that might be. Any ideas?
PS: i am performing the analysis with Phyloseq ordinate() function and the issue seems to be related to the type of data in the sample_data(phylo_object).
UPDATE:
i am adding a reproducible example, and another issue that i found:
library("vegan")

# create fake otu
otu_fake <- matrix(0, nrow=10, ncol=100)

# zero inflated random fake otu data
for (i in 1:100) {
    zero_infl <- ifelse(rbinom(n = 10, size = 1, prob = 0.90)==1, 0, 1)
    ones <- which(zero_infl==1)
    otu_fake[ones, i] <- sample.int(10, length(ones))
}
colnames(otu_fake) <- paste0("otu_", 1:100)
rownames(otu_fake) <- paste0("sample_", 1:10)

# create fake meta
fake_meta <- data.frame(sample=paste0("sample_", 1:10), treatment=c(rep("CTR", 5), rep("TRT", 5)), site=c(1:5, 1:5))

# perform RDA
rda_veg <- rda(formula = otu_fake ~ treatment+Condition(site), data = fake_meta)

# plot it
dev.new()
plot(rda_veg, main="Site as integer")

# cast site to factors
fake_meta$site <- as.factor(fake_meta$site)

# perform RDA
rda_veg <- rda(formula = otu_fake ~ treatment+Condition(site), data = fake_meta)

# plot it
dev.new()
plot(rda_veg, main="Site as factors")

This is an example of the experiment i am working on. The funny thing is that the symmetry appears only after casting the site variable to factor (or character).
So, updated question is: is this due to the design/mathematical structure of the data, is there something else, or am I missing something?
UPDATE on the update
I provide here more code where the symmetry issue can be tested, while the factor/integer problem remains.
To see the symmetry, samples needs to equal to 10. For higher values, the symmetry is lost.
library("vegan")

# choose a number of taxa and samples
taxas <- 100
samples <- 30

# with samples = 10 the setting is:
# 1 site comprise 1 control and 1 treated sample

# with samples = 20, 30, 40... the setting becomes:
# 1 site comprise 2 controls and 2 treated samples

# create fake otu
otu_fake <- matrix(0, nrow=samples, ncol=taxas)

# zero inflated random fake otu data
for (i in 1:taxas) {
    zero_infl <- ifelse(rbinom(n = samples, size = 1, prob = 0.90)==1, 0, 1)
    ones <- which(zero_infl==1)
    otu_fake[ones, i] <- sample.int(samples, length(ones))
}
colnames(otu_fake) <- paste0("otu_", 1:taxas)
rownames(otu_fake) <- paste0("sample_", 1:samples)

# create fake meta
fake_meta <- data.frame(sample=paste0("sample_", 1:samples), treatment=c(rep("CTR", samples/2), rep("TRT", samples/2)), site=1:5)

# perform RDA
rda_veg <- rda(formula = otu_fake ~ treatment+Condition(site), data = fake_meta)

# plot it
dev.new()
plot(rda_veg, main=paste("Site as integers with ", samples, " samples"))

# cast site to factors
fake_meta$site <- as.factor(fake_meta$site)

# perform RDA
rda_veg <- rda(formula = otu_fake ~ treatment+Condition(site), data = fake_meta)

# plot it
dev.new()
plot(rda_veg, main=paste("Site as factors with ", samples, " samples"))

Comment: What is on the z-axis the RDA component? Why is symmetry weird?
Comment: this I did not verified...also I found it more unexpected than weird. I have never seen anything like that (but I am not an expert).
Comment: The effect also occurs when you plot pc1 versus pc2. The rda function seems to do some sort of centering and scaling that makes all the treatments opposite of each other for each site.
Comment: @SextusEmpiricus may I ask you how do you plot pc1 versus pc2?
Comment: `plot(rda_veg, choices = c(2,3), main="Site as factors")` use the choices parameter
Answer: This really will happen always with this specific design – or if I understood your design correctly: you did not have a reproducible example. I understood that for each "Site" you have only two units, one Control, another Treatment. You have only one constrained axis and the second axis is unconstrained PC. So the first axis will separate Control and Treatment. The effect of Condition(Site) is to make the mean of sites equal which in this multivariate setting means that their mean will be at the origin of the constrained axis (RDA1). Second axis, PC1, is unconstrained, and it shows how the observed values of your SUs differ from that mean, and Control and Treatment are equally far away from their mean or the origin. If you draw a line from Control and Treatment point of a Site, the line must go through their mean – or the origin - and distance from the origin is equal for Control and Treatment because this is how mean (average) is defined for two observations.
Was so long that I try to summarize: by the definition of partial RDA, the mean of each Site is at the RDA1 axis value 0, and by the definition of mean, the distance to origin is equal to the mean for both points of a Site.
I think that it may make sense to show only the RDA1 axis.
EDIT: To clarify, the reason for symmetry is that you Condition on mean of pairs of points. That mean will be at the origin (0,0), and the two points will be at equal distances and opposite directions from the origin. This concerns in particular unconstrained axes, or residual PC's.
You can produce this with any random seed using:
x <- matrix(rnorm(10*15), nrow=10) # random data
cl <- gl(5,2) # class for pairs of rows
library(vegan)
pcpair <- rda(x ~ Condition(cl)) # center for pairs of points
plot(pcpair, dis="si")
ordisegments(pcpair, cl) # connect two points of the class

Comment: I see the point: the problem is due to the experimental design which comprise only one control and one treatment for one site.
in contrast, what I am observing would not happen for a design where I have, for example, 3 controls and 3 treated for each site. is this correct?

(i will also try to provide a working example, but not with the original data which are too big).
Comment: When you parallel out "Site", points of each site will be centred to the origin. You see that easily for two-point Sites, but the situation is similar when you have more points in a Site (class). This centring will appear on all unconstrained PC-axes that are all centred to class-means (Site means). CCA has the same feature, but not as visible because points are weighted, and the weights influence their distances from the origin.
Comment: i am working on my understanding of what you said. however I found another issue which is related to the data type which encodes the variable "site" and I am quite puzzled, now.
Comment: I think I got the point. Also, as far as i can see from the help of `cca`, the variable should be a factor. however if I cast "Site" from integer to factor, the ordination goes sideways. Is there a reason for that? Same applies to the example you provided. If I define `cl <- rep(1:5, each=2)` then run the RDA, the plot will be very different. A priori, how do I know which one is correct? Maybe this should be an entirely different question, though.
Answer: I don't know much about RDA and CCA, but this might help you into the right direction.
The function seems to apply some transformation before performing PCA. This transformation seems to center the data in a way such that the average PC score per site is zero.
### rda uses Ybar, some centered and re-scaled values per column
### the object contains 'rda_veg[6]$pCCA$Fit' this is a vector
### that resembles the center of the pca scores per site
### when you subtract it then you get the symmetry

### perform pca with this
otu_transform = rda_veg[3]$Ybar-rda_veg[6]$pCCA$Fit

pc = prcomp(otu_transform, center = FALSE)$rotation

### plot pc1 and pc2
### still not exactly the same but similar
sites1 = otu_transform[,] %*% pc[,1]
sites2 = otu_transform[,] %*% pc[,2]
plot(sites1,sites2, col = fake_meta$site)

The upper graph pc1 vs pc2 is made with your code but adding a choices parameter plot(rda_veg, choices = c(2,3), main="Site as factors")
Comment: thank you for the example, i am working on the mathematics!

Why is constrained RDA symmetrical?