Optimizing MongoDB indexing multiple field with multiple query

Question: I am new to database indexing. My application has the following "find" and "update" queries, searched by single and multiple fields
                reference | timestamp | phone | username | key | Address
update              x     |           |       |          |     |
findOne                   |     x     |   x   |          |     |
find/limit:16             |     x     |   x   |    x     |     |
find/limit:11             |     x     |       |          |  x  |    x
find/limit:1/sort:-1      |     x     |   x   |          |  x  |    x
find                      |     x     |       |          |     |

1)update({"reference":"f0d3dba-278de4a-79a6cb-1284a5a85cde"}, ……….
2)findOne({"timestamp":"1466595571", "phone":"9112345678900"})
3)find({"timestamp":"1466595571", "phone":"9112345678900", "username":"a0001a"}).limit(16)
4)find({"timestamp":"1466595571", "key":"443447644g5fff", "address":"abc road, mumbai, india"}).limit(11)
5)find({"timestamp":"1466595571", "phone":"9112345678900", "key":"443447644g5fff", "address":"abc road, mumbai, india"}).sort({"_id":-1}).limit(1)
6)find({"timestamp":"1466595571"})

I am creating index
db.coll.createIndex( { "reference": 1 } ) //for 1st, 6th query
db.coll.createIndex( { "timestamp": 1, "phone": 1, "username": 1 } ) //for 2nd, 3rd query
db.coll.createIndex( { "timestamp": 1, "key": 1, "address": 1, phone: 1 } ) //for 4th, 5th query

Is this the correct way?
Please help me
Thank you
Answer: I think what you have done looks fine.  One way to check if your query is using an index, which index is being used, and whether the index is effective is to use the explain() function alongside your find().
For example:
db.coll.find({"timestamp":"1466595571"}).explain()

will return a json document which details what index (if any) was used.  In addition to this you can specify that the explain return "executionStats"
eg.
 db.coll.find({"timestamp":"1466595571"}).explain("executionStats")

This will tell you how many index keys were examined to find the result set as well as the execution time and other useful metrics.


Using composer - basics with localhost Mamp

Question: My goal is to try updating drupal/core with Composer and to learn composer basics. Composer is installed on OS X. From terminal i can run
My-MacBook-Pro:~ osxuser$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1.4.1 2017-03-10 09:29:45

Usage:
command [options] [arguments]...

So how do I make composer find my drupal8 install in Mamp?
I have tried both:
composer -d, --working-dir=/Applications/MAMP/htdocs/test_d8

and
cd /Applications/MAMP/htdocs/test_d8

If I am running several drupal 8 installs in /Applications/MAMP/htdocs/ how do I tell composer witch one to work on?
From what I've red you don't need drush any more. I am not sure Composer has all it needs to work with Drupal 8 either.
composer show
symfony/css-selector      v3.2.6  Symfony CssSelector Component
symfony/dom-crawler       v2.8.13 Symfony DomCrawler Component
symfony/finder            v3.2.6  Symfony Finder Component
symfony/polyfill-mbstring v1.3.0  Symfony polyfill for the Mbstring extension

composer diagnose
Checking composer.json: WARNING
No license specified, it is recommended to do so. For closed-source software you may use "proprietary" as license.
require.symfony/dom-crawler : exact version constraints (2.8.13) should be avoided if the package follows semantic versioning
Checking platform settings: WARNING
The OpenSSL library (0.9.8zc) used by PHP does not support TLSv1.2 or TLSv1.1.
If possible you should upgrade OpenSSL to version 1.0.1 or above.

Checking git settings: OK
Checking http connectivity to packagist: OK
Checking https connectivity to packagist: OK
Checking github.com rate limit: OK
Checking disk free space: OK
Checking pubkeys:
Tags Public Key Fingerprint: ---numbers here---
Dev Public Key Fingerprint: ---numbers here---
OK
Checking composer version: OK

Can someone point me in the right direction here? Do I need to use/install other composer submodules/libraries?
Is it possible to do some sort of composer update drupal/core --dry-run as a test?
Comment: You run composer from the project directory, which is where your composer.json lives. I can work on any project at any time in the CLI by `cd` ing to the project I want to work on. Composer understands that _that_ is the working directory in that execution. At least that is how I do it.
Comment: Composer just uses the composer.json and composer.lock files it finds in the current directory, as @Kevin said. Drupal doesn't need any other Composer plugins, if not the ones listed in its composer.json file. You could use the plugin that updates also some other files like index.php and update.php, but that is not strictly necessary. Just know that without that plugin there are files you should manually update.
Comment: Thanks. What's the name of the plugin for index.php/update.php file? This one? https://github.com/drupal-composer/drupal-scaffold – that drupal-scaffold/composer.lock looks massive (!?) Not exactly newbie stuff ;-) Hope I won't have problems with the old OpenSSL library.
Answer: Posting as an answer instead of a comment.
You run Composer from the project directory, which is where your composer.json and .lock file (which will get generated for you) lives.
I can work on any project at any time in the CLI by cd ing to the project I want to work on. Composer understands that this is the working directory in that execution.
So, no matter where you are in your server, Composer will execute in the directory you are currently in.
Comment: composer would need to exist on the server. See: http://stackoverflow.com/a/17013169/295112
Comment: Thanks. So I actually did it right already. I assumed composer did not work against the right folder when I got this message.

My-MacBook-Pro:test_d8 osxuser$ composer update drupal/core --dry-run
Package "drupal/core" listed for update is not installed. Ignoring.
Loading composer repositories with package information
Updating dependencies (including require-dev)
Comment: Updating drupal is a bit different. See: http://drupal.stackexchange.com/a/207502/57
Comment: I was able to do a test after updating the composer.json file and moving     "drupal/core": "~8.02" to require. Got a lot of messages about guzzlehttp/guzzle. Well I am several steps further now. Marking your answer correct. Thanks!
Comment: In theory: can I also CD into a drupal root on a remote server using SSH (shared hosting)? Or do I need to install stuff on the remote server before I use composer?


Get "Raw" views data (with no or little markup)

Question: One thing i hate about views is this bunch of markup and nesting it uses to render the data on a page.
What i usually do to remedy this problem is iterate through the content variable and trim all tags so i can them display it as i wish. I know this kind of a dirty shortcut and i was wondering if there was a better way to do so.
Can' t views only output the raw data from the DB without adding all that markup to it ?
Answer: Are you using Views 6.x-3.x or 7.x-3.x? If so, you can do a ton of the grunt work of stripping out views' default markup by adjusting fields' 'Style settings'. If you're using an older version of Views, Semantic Views does just about the same thing. One gotcha: If you don't want the default  wrapper around each field, you need to check the 'configure the field and label wrapper' option and set it to 'None' - that trips me up sometimes.
To adjust the other bits of markup around, for example, the entire view, or each row, you can copy the views template files (in the views/theme folder) into your theme's folder, and modify them to your heart's content.
The reason Views creates all this extraneous markup is that it is trying to be all things for everyone. I find, a lot of times, that it's easiest (and barely helps performance or page load times) to just go with the default markup, unless I'm trying to do some advanced theming. There's a class for just about everything!
On another note – if you're trying to return just some data, in a format like JSON or XML or something similar, you'll want to use a different display type for views besides one of the standard 'Unformatted fields' or 'list' types...
Comment: Thanks for your detailed answer, i think that would do the trick. I ll play around with it and let you know. One more thing though that would make this answer even more complete, is there a way to programatically grab a view result before it get populated to the view template?
Comment: See http://stackoverflow.com/questions/247991/displaying-a-drupal-view-without-a-page-template-around-it for more ideas... Don't have time to look into this more at this time :-(
Answer: The easiest way to accomplish this is:

Exclude the fields from displaying.
Add a field and choose "REWRITE RESULTS" to rewrite it with the fields that you want (from replacement patterns). You can add markup around the tokens if you want.
Uncheck "Provide default field wrapper elements" from show settings.
Comment: Thanks though i actually stoped using views as i found out that with just a little php/msql knowledge you get much cleanner and faster queries.
Answer: A combination of Views 3 and the Semantic Views module is usually enough for me.
Under extreme circumstances, when I want absolutely zero markup, e.g. to display a completely custom date, I create my own tiny template for the field in question (choosing the right file to create using the Theme Information part of the View). In your template, the default is print $output;, which is completely useless. For my date example, i replaced that with:
print my_pretty_date($row->{$field->aliases['field_date_value']},
  $row->{$field->aliases['field_date_value2']}
);

That will let me run my custom date formatting function with dates in MySQL datetime format.
IMPORTANT: I don't know if the $row data are sanitized. It is hard to find out too.


HAProxy "tcp-request connection reject" not functioning as expected

Question: I have tried to implement a system to block ips if they are acting maliciously. It appeared to be working in testing but I am having some mixed results in my live environment.
What I have tried to do is block connections if they have an error rate > 10, an error count > 50 or a request rate > 150.
Firstly my frontend proxy has the configuration:
stick-table type ip size 100k expire 30s store http_err_rate(10000),http_err_cnt,http_req_rate(10000)
tcp-request connection track-sc0 src
tcp-request connection accept if { src -f /etc/haproxy/whitelist.lst }
tcp-request connection reject if { sc0_http_err_rate gt 10 }
tcp-request connection reject if { sc0_http_err_cnt gt 50 }
tcp-request connection reject if { sc0_http_req_rate gt 150 }

I recently had an ip doing a scan which was not being blocked by haproxy, before blocking by ip-tables I checked the stats on the IP which showed:
echo "show table clientsecure key x.x.x.x" | socat stdio /var/run/haproxy.stat
# table: clientsecure, type: ip, size:102400, used:23
xx: key=x.x.x.x use=8 exp=0 http_req_rate(10000)=144 http_err_cnt=34235 http_err_rate(10000)=150

I feel like I have a fundamental misunderstanding of the haproxy configuration.
Am I using the stick-table correctly with the following tcp-request connection rules?
Does the "tcp-request connection reject" block incoming connections if one rule matches, or do they all need to match?
Edit:
Upon further testing I figured out the rules are only checked on new connections. So using "option httpclose" will stop people as soon as they hit the limits. Obviously not ideal for speed reasons, so going to experiment with keep-alive timeouts.
Answer: Because I was only tracking on the connection on the front end any connections kept alive would still be able to run requests. So the solution is:
Frontend:

create sticky table just with a gpc
track the connection src using sc1
create acl "is-abuser" if the gpc > 0
reject connection if "is-abuser"

Backends:

start sticky table with error rate, error count, request rate
track the content src using sc2
create abuse acls for the error rates, counts, request rate
create acl to increase the gpc tracked from the frontend stick-table using sc1_inc_gpc
reject content if abuse acls match and run then run increase gpc acl

Similar example I found directly from the manual (using sc0 and sc1 in place of sc1, sc2 in above explanation):
#Track per-frontend and per-backend counters, block abusers at the frontend when the backend detects abuse(and marks gpc0).
frontend http
    # Use General Purpose Couter 0 in SC0 as a global abuse counter
    # protecting all our sites
    stick-table type ip size 1m expire 5m store gpc0
    tcp-request connection track-sc0 src
    tcp-request connection reject if { sc0_get_gpc0 gt 0 }
    ...
    use_backend http_dynamic if { path_end .php }

backend http_dynamic
    # if a source makes too fast requests to this dynamic site (tracked
    # by SC1), block it globally in the frontend.
    stick-table type ip size 1m expire 5m store http_req_rate(10s)
    acl click_too_fast sc1_http_req_rate gt 10
    acl mark_as_abuser sc0_inc_gpc0(http) gt 0
    tcp-request content track-sc1 src
    tcp-request content reject if click_too_fast mark_as_abuser


my div tag is unclickable when there is an IMG tag underneath in ie7 ie8

Question: Let's uncomplicate things
http://jsfiddle.net/Hh7Mn/

dsdsfdsfsdfdsfdsf

Why is the center of the div unselectable and undetectable in ie?
EDIT:
Sorry the code is now the same on both jsfiddle and stack
Comment: Could you provide a screen shot or image of the current result? It would be much easier to assess your problem.
Comment: ...or a link to the page would be helpful.
Comment: your jsfiddle is completely different to the code you included in your question. How can we test accurately?
Comment: thanks. I changed it to match the jsfiddle.
Answer: You need to declare a position when using a z-index.

z-index only works on positioned elements (position:absolute, position:relative, or position:fixed)
Comment: declaring it doesnt make it work. Check out eh jsfiddle example
Comment: yes that doesnt work. It also doesnt account for why the border is selectable.
Answer: I would suggest giving your links a greater z-index value:
a{
z-index:12;
}

Answer: change the z-index of the image to -1 and it will work: http://jsfiddle.net/kPtkd/1/
Comment: hmm thats strange as I was testing in IE8 and it worked, and now it's only working in IE8 compatibility mode. It could be an issue with JSFiddle, I sometimes have issues with it and IE. Either way it's strange
Comment: im about to just use transparent png placeholders... bad idea?
Comment: Found a way around it, not a great way though. Set a background colour on the div and it'll work. Tried background:transparent; but it didn't work :(
Comment: yea im going to need it transparent. :(
Comment: yeah transparent png to the rescue. You can use an IE8 conditional though to lessen the impact http://www.quirksmode.org/css/condcom.html


Python merge pdfs without compression

Question: I want to merge a number of pdfs into one pdf using python, but the method I use right now seems to compress the output:
from pathlib import Path
import PyPDF2

merged_object = PyPDF2.PdfFileMerger()

for file_item in list_of_pdfs:
    if Path(file_item).suffix != '.pdf':
        file_item = f'{file_item}.pdf'
    merged_object.append(PyPDF2.PdfFileReader(str(file_item)))

merged_object.write(str(directory))

I haven't been able to find a way to merge pdfs compressionless, any solutions?
Comment: Strange... I tried you code with the files below:

https://docs.scipy.org/doc/scipy-0.10.1/scipy-ref.pdf
https://riptutorial.com/Download/matplotlib.pdf
https://buildmedia.readthedocs.org/media/pdf/pillow/stable/pillow.pdf

It resulted in a 13462055 Bytes output file which is even slightly bigger than the sum of each pdf taken individually which is 12577087 Bytes. I don't notice anything abnormal and I believe the slight increase is normal. Note: I use PyPDF2 version 1.26.0
Comment: @S_Bersier I just realized the compression occurred one step earlier, you are right
Answer: I use to work with PyMuPDF and never noticed compression. The insert_pdf method is suitable to merge pdfs. You can try the following:
import fitz
from pathlib import Path

merged_object = fitz.open(list_of_pdfs.pop(0))

for file_item in list_of_pdfs:
  if Path(file_item).suffix != '.pdf':
      file_item = f'{file_item}.pdf'
  to_append = fitz.open(str(file_item))
  merged_object.insert_pdf(to_append)

merged_object.save(str(directory))
merged_object.close();

Comment: The original method also works, the mistake was on my end, not PyPDF2
Comment: I will give it a shot tomorrow, thanks

Python merge pdfs without compression


Why is median the solution to absolute difference, but mean is the solution to squared difference?

Question: What is the value of $a$ and $b$ that minimizes the $L_1$ and $L_2$ norm, respectively?
$$
 \min_{a} \mathrm{E}
 \left| X - a \right|
$$
I was told that median is the solution. How do you solve it? Why can't it be the mean instead?
$$\mathrm{E} (X - b) ^ 2$$
I was told that mean $\mu$ is the solution to the squared error. How do you solve it?
Answer: Loss: $$L_1(\hat y,y)=|\hat y-y|$$
Suppose that $$\hat y=y+\varepsilon,$$
where the probability density of error $\varepsilon$ is $f(\varepsilon)$.
Now, we can solve the problem $$\min_{\hat y} E[L_1(\hat y,y)]$$ using first order condition (FOC):
$$\partial/\partial\hat y E[L_1(\hat y,y)]=0$$
$$\partial/\partial\hat y E[L_1(\hat y,y)]=
 E[\partial/\partial\hat y |\hat y-y|]=
\int_{-\infty}^{\hat y-y}f(e)de-\int_{\hat y-y}^{\infty}f(e)de
=F(\hat y-y)-(1-F(\hat y-y))=2F(\hat y-y)-1=0$$
Where $F(.)$ is the cumulative distribution function.
Also I substituted $\hat y-y$ with $e$ in the integrals.
You can see that the FOC is satisfied when $F(\hat y-y)=1/2$, i.e. when the $\hat y$ is at the median of the distribution.
You can do the same for $L_2$ to show that it requires the mean.
Loss: $$L_2(\hat y,y)=(\hat y-y)^2$$
Suppose that $$\hat y=y+\varepsilon,$$
where the probability density of error $\varepsilon$ is $f(\varepsilon)$.
Now, we can solve the problem $$\min_{\hat y} E[L_2(\hat y,y)]$$ using first order condition (FOC):
$$\partial/\partial\hat y E[L_2(\hat y,y)]=0$$
$$\partial/\partial\hat y E[L_2(\hat y,y)]=
 E[\partial/\partial\hat y (\hat y-y)^2]=
 E[2 (\hat y-y)]=
2 (\hat y-E[y])]=0$$
You can see that the FOC is satisfied when $\hat y=E[y]$, i.e. when the $\hat y$ is at the mean of the distribution.
Comment: Is there a course I can learn this technique? This is pretty cool. I'm taking a probability course using Casella and Berger textbook. But, the book example do not cover this.
Comment: This type of stuff is taught in forecasting courses but all you need to know is how to setup the problem. The rest is a simple calculus. I'll update the answer with a link to a book or paper later
Comment: [Granger's paper](https://books.google.com/books?id=RskN3Db7MlgC&lpg=PA8&ots=qxxAtbERjf&dq=forecasting%20granger%20cost&pg=PA366#v=onepage&q=forecasting%20granger%20cost&f=false) is a good intro into how cost function are used in forecasting to come up with optimal forecasts, he has example for quadratic loss (cost) leading to the mean
Comment: Can you explain why the middle number minimizes the absolute difference? Why not the mean?
Comment: @user13985 $F(x)=1/2$ is a definition of the median, i.e. when the probability below and above x is the same 50%. For symmetrical distributions such as Gaussian, median and mean are the same
Comment: That makes sense. But doesn't mean make more sense than median? Just wondering. Definition of median in wikipedia $P(X \le m) = \frac{1}{2} = P(X \ge m) = \int f(x)dx = \frac{1}{2}$ I need to think about it.
Answer: The squared error is the simpler case, and a classic argument, so I will reproduce it here.
Let $\mu = E[X]$.  Then we can use an add and subtract trick
$$ E[(X - b)^2] = E[(X - \mu + \mu - b)^2] = E[(X - \mu)^2] + 2 E[(X - \mu)(\mu - b)] + 2 E[(\mu - b)^2] $$
Now in the middle term, $\mu - b$ is a constant, so it can come outside of the expectation
$$ E[(X - \mu)(\mu - b)] = (\mu - b) E[X - \mu] = (\mu - b)(E[X] - \mu) = 0 $$
So, all together
$$ E[(X - b)^2] = E[(X - \mu)^2] + 2 E[(\mu - b)^2] $$
This is clearly minimized when $b = \mu$.
The median case is more elusive, but still elementary, and also deserving of classic status.  You can look here for some proofs.  Both André's and Brian's answers are simple and intuitive demonstrations.
Comment: Solution in link is using ordered statistics to get the median. I read it. It was very long. I was completely lost when I reached the end.
Comment: Ok... That tends to happen with mathematics.  You should always anticipate that you will need to read something multiple times and maybe take a walk and think about it.  I think the idea there is simple and beautiful, but you are free to disagree!
Comment: Why does the median minimize the absolute difference? What's the intuition in simple words?
Comment: Im not sure there is an intuition in simple words for either case, why do you believe a simple reason exists?
Comment: I read this post. They used median to explain it. https://math.stackexchange.com/questions/85448/why-does-the-median-minimize-ex-c
Comment: That's only a simple or intuitive explanation if calculus is simple and intuitive to you.  But, I'm glad you found one that you like!
Comment: Your L2 norm was explanation was pretty good, I must say!


Probability question about letters

Question: Let's say we had the word STATISTICS, and it was hanging on a wall, then two letters in the word blew off, and somebody found them. What are the chances they put them in the correct order?
here is what I have.
A = letters are put in order
I = letters are identical
P(A given I) = 1
P(A given I compliment) = 1/2
I want to use the total probability theorem, but I have a hard time grasping what P(I) could be, I'm sure it would be 1 / 10 NCR something, but I'm not sure what to put.
Any ideas?
Here is a solved version of this problem.
Word = CANAL
A = words are in the right order
I = words are identical
P(A given I ) = 1
P(A given I^c) = 1/2
P(I) = 1/5 choose 2
P(A) = 1*1/10 + 1/2*9/10
by the total probability theorem.
Comment: Lets look at the CANAL word as it is simpler.  Now only the letter A appears twice. So if both letters are A you get it correct.  Everything else could lead to a mistake.  Suppose you get C and L then the probability of getting it write is 1/2. Two possible spellings CANAL and LANAC.
Comment: Are you assuming the remaining letters stay in place?  Note that under this assumption if both lost are the same letter you automatically get the right order.
Comment: So this applies to T, S, I.  3 letters are S, 3 are T and 2 are  I.
Comment: If you get C and N  the probability is 1/2 with NACAL and CANAL the two possibilities.  The logic is the same for N and L.  But if you have A and C or A and N or A and L the story is different.  These three cases work the same way so lets take A and C,  The possible spellings are ACNAL, AANCL and CANAL.  The probability here is 1/3. Now these are conditional probabilities and must be multiplied by the probability of the choice of letters.  You (the OP) got 11/20.  Are you sure that you took all of this into account?
Comment: What are the chances conditional on?  The letters that blew off or the mere fact that two blew off?  If it's the former, the answer depends on the letters.  If it's the latter, the answer depends on the probability distribution you assume concerning which letters blow off.
Comment: @whuber We have been trying hard to see what the OP is assuming. The OP thinks he knows the answer in a special case when using the word CANAL. In his revised question the OP calculates P(A) to be 11/20. I haven't completed my calculation. But I am assuming that it is dependent on which letters blew off because the remaining letters stay in place.
Comment: Yeah the rest remain in place, only two fall off.
Answer: This was my first (incorrect) interpretation of the OP's question (I've added another answer which I believe has the correct interpretation).
Two letters fall off. Assuming the person replacing them knows how to spell, if different letters fall they will certainly be replaced correctly. If the same letters fall off, they must be replaced in the same order in which they were originally (correct spelling AND correct original position). Under these assumptions, the answer is:
$P(A) = P(different\,letters\,fell\,off) + P(same\,letters\,fell\,and\,replaced\,correctly)$
We have 4 bins (S, T, A, I, C) with (3, 3, 1, 2, 1) letters each.
$P(same\,letters\,fell\,off) = P(2\,S\,fell) + P(2\,T\,fell) + P(2\,A\,fell) + P(2\,I\,fell) + P(2\,C\,fell)$
$= 3 / 10 * 2 / 9 + 3 / 10 * 2 / 9 + 0 + 2 / 10 * 1 / 9 + 0 = 14 / 90$
$P(different\,letters\,fell\,off) = 1 - P(same\,letters\,fell\,off) = 76 / 90$
$P(same\,letters\,fell\,and\,replaced\,correctly) = P(same\,letters\,fell\,off) * P(replaced\,correctly) = 14 / 90 * 1 / 2 = 14 / 180$
$P(A) = 76 / 90 + 14 / 180 = 166 / 180$
Comment: I do not think this is correct, for example the answer to this question if the word was CANAL would be 55% I know this is true, and when I followed your steps it ended up being in the 90's
Comment: Then I have misinterpreted your question. Can you please clarify what constitutes a correct substitution? You can use CANAL as an example
Comment: Here is how it is solved in the book.
Comment: Look at original post I will edit it in.
Comment: If the assumption is that the other letters stay in place the person replacing doesn't have to know how to spell.
Comment: Something changed with this post.  There were more comments and at least 1 more answer the last time I looked.
Answer: So here goes another answer using a different interpretation of the question: two letters fall off. They will be replaced randomly. A correct replacement is such that the spelling of the word is unchanged.
$P(A) = P(same\,letters\,fell\,off) + P(different\,letters\,fell\,and\,replaced\,correctly)$
We have 4 bins (S, T, A, I, C) with (3, 3, 1, 2, 1) letters each.
$P(same\,letters\,fell\,off) = P(2\,S\,fell) + P(2\,T\,fell) + P(2\,A\,fell) + P(2\,I\,fell) + P(2\,C\,fell)$
$= 3 / 10 * 2 / 9 + 3 / 10 * 2 / 9 + 0 + 2 / 10 * 1 / 9 + 0 = 14 / 90$
$P(different\,letters\,fell\,off) = 1 - P(same\,letters\,fell\,off) = 76 / 90$
$P(different\,letters\,fell\,and\,replaced\,correctly) = P(different\,letters\,fell\,off) * P(replaced\,correctly) = 76 / 90 * 1 / 2 = 76 / 180$
$P(A) = 14 / 90 + 76 / 180 = 104 / 180$
Using the word CANAL, buckets are (C, A, N, L) with sizes (1, 2, 1, 1).
$P(same\,letters\,fell\,off) = P(2\,C\,fell) + P(2\,A\,fell) + P(2\,N\,fell) + P(2\,L\,fell)$
$= 0 + 2 / 5 * 1 / 4 + 0 + 0 = 2 / 20$
$P(different\,letters\,fell\,off) = 1 - P(same\,letters\,fell\,off) = 18 / 20$
$P(different\,letters\,fell\,and\,replaced\,correctly) = P(different\,letters\,fell\,off) * P(replaced\,correctly) = 18 / 20 * 1 / 2 = 18 / 40$
$P(A) = 2 / 20 + 18 / 40 = 22 / 40 = .55$
which agrees with your book answer.

Probability question about letters


How to find a eigenvector with a repeated eigenvalue?

Question: The eigenvalues of my matrix are $x_1= 1$
and
$x_2=3$
I get an eigenvector
$V = t~[ 4~~~~~~ 3 ~~~~~1 ]^T $ but how can I diagonalize the matrix if I have the same column repeated twice. Should I just use different values for t since they are all eigenvectors of the same matrix?
matrix
$\begin{bmatrix}
1 & 0 & 0\\
0 & 1 & 0\\
8 & 6 &3 \\
\end{bmatrix}$
Edit: I realized I had the eigenvalues wrong
, now fixed
Comment: Please show us the matrix.
Comment: Please check your definition of eigenvalues. The eigenvalues of your matrix are $1$ and $3$.
Answer: I will assume that you are capable of finding the eigenvector for the simple root, and only focus on the repeated eigenvalue $x_1=1$. Let $\mathbf{v}=\begin{bmatrix}
a \\
b \\
c \\
\end{bmatrix}$ and consider $(A-I)\mathbf{v}=\mathbf{0}$.
This yields $\begin{bmatrix}
0 \\
0 \\
8a+6b+2c \\
\end{bmatrix} = \begin{bmatrix}
0 \\
0 \\
0 \\
\end{bmatrix}$
in which case $8a+6b+2c=0$. Let $b$ and $c$ be free variables, giving us $\mathbf{v}=\begin{bmatrix}
-\frac{3}{4}b-\frac{1}{4}c \\
b \\
c \\
\end{bmatrix}=b\begin{bmatrix}
-\frac{3}{4} \\
1 \\
0 \\
\end{bmatrix}+c\begin{bmatrix}
-\frac{1}{4} \\
0 \\
1 \\
\end{bmatrix}$.  I hope this helps.
Comment: yes it does! thank you!


Alteração no texto do botão com a linguagem Kivy

Question: Não estou conseguindo encontrar uma forma de ficar alterando o texto do botão toda vez que eu clicar nele. Consigo alterar o texto somente uma vez. Toda vez que for clicado quero que apareça ligar lampada/desligar lampada.
]2
Answer: Você precisaria de uma variável auxiliar com um valor booleano para alterar entre ligado e desligado. Veja esse exemplo abaixo:
class MyButton(Button):
    __status = False

    def change(self):
        self.__status = not self.__status
        self.text = "Desligar" if self.__status else "Ligar"

class App(App):

    def build(self):

        button = MyButton(text = "Ligar", font_size = 40)
        button.on_release = button.change
        return button

No exemplo acima, o atributo __status alterna entre True e False a cada chamada do método change() e depois o texto do botão é definido de acordo com o valor desse atributo.


Is it possible to have three levels of subdomains in Drupal 7?

Question: I know how to create a sub domains for Drupal website (I create a sub domain and add its folder in /sites folder). Out websites are multilingual so for every language I create a subdomain and in the end the sites are going to be like this :

fr.example.com
en.example.com
ar.example.com

and so on. we have also a few sites that are subset of our main website and we want to display them like this
fr.subset1.example.com
en.subset1.example.com
ar.subset1.example.com
fr.subset2.example.com
en.subset2.example.com
is it possible to implement such thing in Drupal 7?
Answer: The answer is "yes". A Drupal multisite can use whatever structure it likes, including the one you propose.
To do this for subdomain http://fr.subset1.example.com/ just create the following subdrectory sites/fr.subset1.example.com/. Make sure it contains the file default.setttings.php and a copy this to settings.php. Then create subdirectories files  (and optionally, modules and themes), and make sure they're all writable by the web-server user.
Then:

Set up DNS for this subdomain (fr.subset1.example.com) to point to the same IP as the default domain.
Create a serveralias (details below) (or vhost entry) for that directory that has the same web root as the default domain.
Point your browser to the base URL of your website (http://fr.subset1.example.com/) to complete the web based part of the installation of a Drupal site.

To create a multi-site instance, you also need to make sure the DNS for that instance points to the same web root as the default domain. If you've set up a sub-domain, the simplest way to do this is to add it as a ServerAlias. This means putting something like this in the Apache vhost configuration:
<VirtualHost *>
  ServerName example.com
  ServerAlias fr.example.com
  ServerAlias fr.subset1.example.com
  …
  DocumentRoot /var/www/example.com/htdocs/
  …
</VirtualHost *>

Alternatively, you may create a separate vhost for the multi-site. This requires a separate entry, but allow you to tailor the web server settings for this particular multi-site instance.
Repeat this procedure for any other subdomains you want.
Comment: How? from what I know websites placed in /sites folder have only `files` , `modules` and `themes` . besides that how does Cpanel add sub domain to a sub domain?
Comment: @Drupalist, for user support for Cpanel, ask your hosting provider. Cpanel has nothing to do with Drupal.


Contextual filters help

Question: I have a page and a view. I have placed the view as a block on the page and there is a contextual filter defined as shown in the image
Basically when i print the arg(1) in header and see i get like 42 which is like the node id on which the view is placed. Can you please tell me what its is trying to do when its placed in the contextual filters as it was was done earlier by another person. I mean to ask how does it filter out the data based on the node id ?
Answer: I am not certain if I understand what you are asking here, but  this question is bound to come up on searches for anyone trying to figure out contextual filters.  The information you present leads to several questions and comments:
It is unclear what your contextual filter will due without knowing more about the view fields, setting and path definition you have given it.   Also, is this the contextual filter screen for the page or block?
To define the location for the argument in the path for a page view you place a % character at the location in the path definition for the view. eg. in Example.com/content/%/items, any URL value matching the path pattern will generate a view with the value of the % passed on to the contextual filter. IN this way you have a view definition performing generalized filters as defined in the first column of the view definition form page and additional filtering as defined in the third column under the 'Advanced' option.  For a page view you complete the requirements for the contextual filter under the heading "When the filter value IS available or a default is provided"
For a block you must complete the section "When the filter value is NOT available" and select "Provide default value".  IN the drop down list you can tell the filter whether it is intercepting a content, taxonomy or user id node number or you can tell it to select a raw value from the URL and which position in the string it is.  In the example I gave above the context value we are looking for (the % sign) is in the second position.
However the value is passed, the context filter will further filter the view as defined on the view form for that page or block.

Contextual filters help


Configurando o Dominio com o Google Compute

Question: Tenho uma máquina virtual no Google Compute Engine com Wordpress e tenho um registro cadastrado no Registro.br.
Quero registrar meu domínio no Google Compute, porem não estou tendo sucesso. Alguém sabe qual o procedimento?
Comment: Lembre-se que a resposta do Raffael precisa de cuidados. Quando for criar o IP estático, lembre-se de atribuir a Virtual Machine. Caso contrário, será cobrado do Google Cloud o uso desse IP que não está atribuído a nada.
Answer:
No Google Cloud, entre no Console.
No menu lateral, clique no item "Redes".
Entre em "Endereços IP Externos", localize a instância que deseja e crie um endereço estático para ela.
Copie esse endereço estático (externo).
Clique em "Cloud DNS" no submenu lateral.
Crie uma zona e entre na tela de detalhes dela.
6.1 - Na lista de registros NS, copie dois dos endereços e informe os mesmos no "registro.br" como DNS Master e Slave, respectivamente.
6.2 - Nessa mesma tela (detalhe de zona), clique em "Adicionar Conjunto de Registros" e adicione um registro do tipo "A".
6.2.1 - No campo IPv4, informe o endereço de IP estático (externo) de sua instância desejada criada no Compute Engine.

Opcional: dentro da sua instância (compute engine), configure seu servidor/proxy conforme seu domínio/subdomínio.


O que causa o erro [could not bind to "tcp://127.0.0.1:8843". Cannot assign request adress]?

Question: Vamos lá. Eu estou tenho um script em PHP que serve para criar um servidor websocket para minha aplicação. Estou utilizando o websocket do HTML5.
Para criar o servidor, estou utilizando a biblioteca Ratchet encontrada no site socketo.me.
Rodo esse código abaixo a partir da linha de comando:
 list ($host, $port) = $argv + ['localhost', 9000];

 $app = new Ratchet\App($host, $port, '0.0.0.0');

 $app->route('/chat', new Chat, array('*'));

 $app->route('/notificador', new Notificador, array('*'));

 $app->run();

Tudo estava funcionando perfeitamente. De repente, esse serviço parou de funcionar. Tentei utilizar o mesmo comando para iniciá-lo novamente, mas sempre é apresentada a seguinte mensagem:

Could not bind to tcp://127.0.0.1:8843: Cannot assign requested address

É importante explicar que eu não utilizo a porta 8843 para executar esse script, uso a porta 9000. Porém a biblioteca Ratchet utiliza.
Um trecho do  construtor da classe Ratchet\App:
    if (80 == $port) {
        $flashSock->listen(843, '0.0.0.0');
    } else {
        $flashSock->listen(8843);
    }

Por sua vez o método listen tem o seguinte trecho:
   $this->master = @stream_socket_server("tcp://$host:$port", $errno, $errstr);
    if (false === $this->master) {
        $message = "Could not bind to tcp://$host:$port: $errstr";
        throw new ConnectionException($message, $errno);
    }

O estranho é que já liguei para o pessoal da dialhost, onde temos a hospedagem e foi verificado que não existe nenhum serviço rodando nessa porta. Então não se trataria de um erro do tipo "a porta já está ocupada".
O que já tentei fazer?

Pedi para reiniciar o servidor (Linux).

O funcionário da dialhost disse que a porta não está ocupada. Ele alegou ter verificado isso via comando telnet.

Já executei o comando fuser -k 8843/tcp para tentar matar algum processo que estivesse ocupando nessa porta.

Já fiz o processo utilizando uma porta em uso (de propósito), pra ver se a mensagem retornada seria diferente, e retornou diferente:

Could not bind to tcp://0.0.0.0:9001: Address already in use

... porém nada resolveu!
E agora, quem poderá me ajudar?
Comment: [Ele!](http://2.bp.blogspot.com/-YWPz46T1Ln0/U3EobzrSmDI/AAAAAAAAGTI/8Lr_p55wC6w/s1600/Chapolin+1.gif)
Comment: Será que não tem nenhum firewall ou proxy bloqueando o tráfego dessa porta?
Comment: @Bacco acho que ele usou a buzina paralisadora nessa porta. Pede para ele apertá-la duas vezes pra desfazer o efeito :)
Comment: Estou votando para fechar esta pergunta como fora de escopo porque [o Wallace pediu](http://chat.stackexchange.com/transcript/message/30595966#30595966).
Answer: Problema resolvido. O funcionário da hospedagem disse que o problema era que a interface loopback não estava rodando.
O interessante é que, como desmontrado na pergunta, o trecho que utiliza a função listen demonstra que, se não passar o segundo parâmetro, seria adicionada o 127.0.0.1.
A função stream_socket_server só funcionava com 0.0.0.0.
Então, depois que ele iniciou o tal do loopback, tudo voltou a funcionar perfeitamente, como antes.


Create an animated 'stack' via AJAX

Question: I'm using ASP.NET for a search app web site.
My search retrieves exactly one result per fetch. Once the search is complete and the user selects "Update", a new result is fetched and the old result is written to the ViewState as a generic List. The new result replaces the old one.
I am trying to get the page to update (via partial post-back), so that the old result drops down the screen and the new result sits on top of it. Much like the concept of a stack.
Does anyone know how this can be achieved? I've started looking at the Ajax Control Toolkit.
Would appreciate any hints and tips with regard to my problem.
Thanks!
Answer: Using JQuery, this sort of things is fairly straightforward.  I would suggest avoiding the partial post-back and using a WebMethod (on a "page method"), then calling that to get the results.  In that scenario, JQuery can manage the request to the server and process the response data exactly how you wish.
Answer: I'm a just beginning to play around with AJAX animations, but this reference page has been a big help to me.
AJAX Animation Reference

Create an animated 'stack' via AJAX


C# foreign character handling

Question: I am trying to read a file and then write it in another format. I have some portuguese characters in my text which are getting mangled or question marks.
I have a sample text that we are reading :
PZO DISPONIVEL PARA VENDA CDB PàS P SNA

string[] lines = System.IO.File.ReadAllLines(@"C:\test_pcharacters.txt");
// Display the file contents by using a foreach loop.
System.Console.WriteLine("Contents of WriteLines2.txt = ");
foreach (string line in lines)
{
    Encoding iso = Encoding.GetEncoding("ISO-8859-9");
    Encoding utf8 = Encoding.UTF8;
    byte[] utfBytes = utf8.GetBytes(line);
    byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
    string msg = iso.GetString(isoBytes);
    // Use a tab to indent each line of the file.
    Console.WriteLine("\t" + msg);
    Console.WriteLine("\t" + line);
}

Comment: Don´t post images of code. Instead directly paste the code here.
Comment: I pasted image so it shows the debugging information.
Comment: How have you created this text file? Is it ANSI, UTF-8, or something else?
Comment: If you use a `StreamReader` instead it should detect the encoding from the file.
Answer: When using File.ReadAllines, if no specific Encoding is specified then the function will default to UTF8.
In your case, explcitily specifying the default encoding (Encoding.Default) will resolve the issue:
string[] lines = System.IO.File.ReadAllLines(@"C:\users\mparkin\desktop\test_pcharacters.txt", Encoding.Default);

The Default encoding uses the current code page where the software is running.  This is likely (but not guaranteed) to be Windows-1252 which includes ANSI characters (such as the one you have in your text file).
Output:
Contents of WriteLines2.txt =
    PZO DISPONIVEL PARA VENDA CDB PàS P SNA
    PZO DISPONIVEL PARA VENDA CDB PàS P SNA

As pointed out by @DmitryBychenko, it can be dangerous to rely on Encoding.Default because the returned encoding may differ depending upon the environment.
As an alternative, you could specify the exact code page used to produce your file.  On Windows, this code be 1252, which you can obtain using:
Encoding.GetEncoding(1252)

Comment: `Encoding.Default` can be dangerous: it is `UTF-8` in case of  .Net Core and Default Code Page (say, `Windows-1252`) in case of .Net FrameWork; I'd rather put it expilictly: `Encoding.GetEncoding(1252)`
Comment: @DmitryBychenko Thank you, an incredibly important point. I've added some more information about selecting specific code pages rather than relying on the default

C# foreign character handling


How to rewrite this theme call to drupal_render

Question: I have this call to the theme function in my code :
print theme('image_style',
            array(
                'path' => $news_image['uri'],
                'style_name' => 'thumbnail',
                'alt' => $news_image['alt'],
                'title' => $news_image['title'],)
            );

However in the theme function documentation you can read :

Avoid calling this function directly. It is preferable to replace
  direct calls to the theme() function with calls to drupal_render() by
  passing a render array with a #theme key to drupal_render(), which in
  turn calls theme().

So, how do I rewrite the above simple function using drupal_render ?
Answer: Something like this...
$build = array(
  '#theme' => 'image_style',
  '#path' => $news_image['uri'],
  '#style_name' => 'thumbnail',
  '#alt' => $news_image['alt'],
  '#title' => $news_image['title'],
);

$rendered = drupal_render($build);

Where every named variable you would normally pass to the theme() function as the second parameter becomes a #-prefixed key in the render array.


Apache Reverse Proxy For Specific Subdomain

Question: I'm have a an Apache HTTP server that has a reverse proxy to a tomcat server. However, I only want the reverse proxy to happen when the client uses the subdomain www. This is because I want to use other subdomains to point to other applications, such as email.
e.g. www.example.com will go display the apache tomcat webapp.
The way to do this, I presume, is to configure my DNS so that every subdomain I use will point to my server. Right now, in addition to www, that is server.example.com and posfixadmin.example.com. However, the issue is that all my subdomains end up pointing to tomcat.
So when I try to visit postfixadmin.example.com/setup.php to set up postfixadmin through its web setup, it ends up taking me to my tomcat webapp's 404.
Here is my virtualhost configuration:
<VirtualHost www.example.com:80>
    ServerName http://www.example.com
    ProxyPass / http://localhost:8080
    ProxyPassReverse / http://localhost:8080
</Virtualhost>

<VirtualHost server.example.com:80>
    ServerName server.example.com
    DocumentRoot /var/www/html/
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =server.example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>

<VirtualHost postfixadmin.example.com:80>
    ServerName postfixadmin.example.com
    DocumentRoot /var/www/postfixadmin/public

    ErrorLog /var/log/httpd/postfixadmin_error.log
    CustomLog /var/log/httpd/postfixadmin_access.log combined

    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>

    <Directory /var/www/postfixadmin/public>
       Options FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>
</VirtualHost>

EDIT
It looks like the proxy conf file doesn't do anything (??). I decided to experiment around and change the first virtualhost servername to the following:
<VirtualHost *:80>
    ServerName abcd.example.com
    ProxyPass / http://localhost:8080
    ProxyPassReverse / http://localhost:8080
</Virtualhost>

Then, I restarted and reloaded Apache...But for some reason, going to www.example.com STILL took me to the tomcat webapp! Does anyone know what drives this?
Answer: I got it!
It turns out that the problem was in the ssl configuration file - the :443 ports were overlapping.
Thanks for the help!
Answer: As to the DNS: I have set specific CNAME entries for each subdomain including www; all of them point back to the public IP of my server that houses my example.com domain (using @ in my case - possible with most DNS, I think). There may be some different strategies on this, but I believe you're on the correct path based on what you've suggested in the question.
As to Apache configuration:
I believe that the http protocol does not need to be specified in the ServerName directive and that, generally, the domain need not appear inside the <VirtualHost>...</VirtualHost> tags.
I should mention that I am relatively unfamiliar with Tomcat but am assuming it is listening at 8080 on the localhost, in which case this should help.
I'm not 100% certain that that is all that is snarling you, but try trimming that ServerName back and doing like so, including the change to the VirtualHost open tag:
<VirtualHost *:80>
    ServerName www.example.com
    ProxyPass / http://localhost:8080
    ProxyPassReverse / http://localhost:8080
</Virtualhost>

Your second <VirtualHost> probably requires similar changes, though it also seems that you are directing it to serve requests from the web/network which are coming in on port 8080 -- which I don't believe is your intent.
I think what you want is to also listen on port 80 from the web/network, but to follow these directives if addressed to server.example.com like so:
<VirtualHost *:80>
    ServerName server.example.com
    DocumentRoot /var/www/html/
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =server.example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>

And finally, similar change to the opening <VirtualHost> tag on the final one:
<VirtualHost *:80>
    ServerName postfixadmin.example.com
    DocumentRoot /var/www/postfixadmin/public

    ErrorLog /var/log/httpd/postfixadmin_error.log
    CustomLog /var/log/httpd/postfixadmin_access.log combined

    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>

    <Directory /var/www/postfixadmin/public>
       Options FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>
</VirtualHost>

Altogether, this seems more like what you're looking for:
<VirtualHost *:80>
    ServerName www.example.com
    ProxyPass / http://localhost:8080
    ProxyPassReverse / http://localhost:8080
</Virtualhost>

<VirtualHost *:80>
    ServerName server.example.com
    DocumentRoot /var/www/html/
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =server.example.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} {END,NE,R=permanent}
</VirtualHost>

<VirtualHost *:80>
    ServerName postfixadmin.example.com
    DocumentRoot /var/www/postfixadmin/public

    ErrorLog /var/log/httpd/postfixadmin_error.log
    CustomLog /var/log/httpd/postfixadmin_access.log combined

    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>

    <Directory /var/www/postfixadmin/public>
       Options FollowSymLinks MultiViews
       AllowOverride All
       Order allow,deny
       allow from all
    </Directory>
</VirtualHost>

Comment: Thank you for your response.
The 8080 thing was a typo in the post but not in my config file, thank you for pointing that out. However, the change did not seem to make a difference for me.
Also, I am using A records, not CNAME records, to point to my IP address. Each of my subdomains points to the server IP, however the naked domain does not. Should it?
Comment: I could not comment on your self-answer below, but glad to hear that you got it! I may have been incorrect in saying it was a CNAME, to be honest; I always reverse those and that's the one thing I did not look up in composing my answer. But all of mine do point to the server IP in my case, as well. (My particular install has the config spread across multiple .conf files so I can totally appreciate your predicament there as most instructions I see online point to solutions that don't exactly fit my install!)


phpmyadmin table values over 100 get changed to 99.99999999

Question: I am importing a table into a phpmyadmin database as a .CSV file, the file has some values within it which are greater than 100 for example 284.543536, 694.5435432, etc... When I import the file all values under 100 get imported fine and show on the table as expected however, the values over 100 get changed to 99.99999999. I have tried to find a solution for this but am struggling, could someone please explain to me why this keeps happening.
Comment: Show us the data type for that column. I suspect it is `decimal(n+2,n)`
Comment: the data type is automatically set to decimal(10,8)
Comment: See http://stackoverflow.com/questions/20548800/how-to-store-longitude-and-latitude-in-database-mysql-wont-go-further-than-99
Answer: Your column is set to only allow two decimal places before the dot causing your values to be truncated. You will need to change it to allow higher numbers. decimal(10,7) would solve this issue for the numbers you have used in your example but if you plan on getting values over 1000 you will need to adjust this further.
Comment: I changed this but it just changes the value from 99.99999 to 100, is there any way of doing this during the import?
Comment: You have to set that column up first with the right data type then do your import.


Error while simplifying boolean expression

Question: Expression $((A \oplus B) \land \lnot C) \lor (\lnot(A \oplus B) \land C)$ simplifies to $A \oplus B \oplus C$.
This is my attempt at simplification:
1. $((A \oplus B) \land \lnot C) \lor (\lnot(A \oplus B) \land C)$
2. $(((A \land \lnot B) \lor (\lnot A \land B)) \land \lnot C) \lor (\lnot ((A \land \lnot B) \lor (\lnot A \land B)) \land C)$
3. $((\lnot C \land A \land \lnot B) \lor (\lnot C \land \lnot A \land B)) \lor (C \land \lnot A \land B) \lor (C \land A \land \lnot B)$
4. $(\lnot A \land ((\lnot C \land B) \lor (C \land B))) \lor (\lnot B \land ((\lnot C \land A) \lor (C \land A))$
5. $(\lnot A \land (B \land (\lnot C \lor C))) \lor (\lnot B \land (A \land (\lnot C \lor C)))$
6. $(\lnot A \land B \land \top) \lor (\lnot B \land A \land \top)$
7. $(\lnot A \land B) \lor (\lnot B \land A)$
8. $A \oplus B$
What am I doing wrong? It looks to me like everything is correct, but my book says that $A \oplus B \oplus C$ is the simplest form of this expression. If you know the correct way to simplify this, please write it in step by step form.
Comment: Try replacing the two (A ⊕ B)  with D in the original expression and stare at it for a while.  Take note that XOR is associative. In your second step, if you wanted to expand ¬((A∧¬B)∨(¬A∧B)) then you would use Demorgan's law for the not of an and.
Comment: It looks like i made mistake in step 3 c~ab+ca~b isn't c~((a~b)+(~ab)), but I still don't know how to solve this.
Comment: I tried replacing A^B with D but it's not easier and I still don't get it. I also don't see how that XOR is associative helps.
Comment: What equivalence rule do you have for the $\oplus$?
Answer: Well.. Since you tried it's probably OK to give you the answer.
((A⊕B)∧¬C)∨(¬(A⊕B)∧C)
(D∧¬C)∨(¬D∧C)
D⊕C - Reverse XOR
A⊕B⊕C - OK because XOR is associative
Answer: Seeing how you go from line 1 to line 2, you must have the equivalence rule:
$$P \oplus Q \Leftrightarrow (P \land \neg Q) \lor (\neg P \land Q)$$
But if you have that, then $((A \oplus B) \land \lnot C) \lor (\lnot(A \oplus B) \land C) \Leftrightarrow A \oplus B \oplus C$ is just an instance of this very equivalence.
OK, but your question was: what did you do wrong?
Well, you made a mistake in going from line 2 to line 3.  The second conjunct of line 2 is:
$$(\lnot ((A \land \lnot B) \lor (\lnot A \land B)) \land C)$$
and on line 3 you rewrote that as:
$$(C \land \lnot A \land B) \lor (C \land A \land \lnot B)$$
but that is not correct. Instead you get:
$$(\lnot ((A \land \lnot B) \lor (\lnot A \land B)) \land C) \Leftrightarrow$$
$$(\lnot (A \land \lnot B) \land \lnot (\lnot A \land B) \land C) \Leftrightarrow$$
$$((\lnot A \lor B) \land (A \lor \lnot B) \land C)$$
So basically from line 3 on everything you had was wrong.
Answer: Do not try to jump over two hurdles at once.   Just take it one step at a time.
Also, keep the end goal in sight.
$\begin{split} & (A\oplus B)\oplus C
\\
=~&(~(A\oplus B)\land\lnot C~)\lor(\lnot(A\oplus B)\land C)
\\
=~&(~(~(A\land\lnot  B)\lor(\lnot A\land B)~)\land\lnot C~) ~\lor~(~\lnot(A\oplus B)\land C~)
\\
=~&{(A\land\lnot B\land\lnot C)\lor(\lnot A\land B\land\lnot C) ~\lor~(~\lnot(A\oplus B)\land C~)}
\\
\vdots~~&
\\
=~&(A\land\lnot B\land\lnot C)\lor(\lnot A\land B\land\lnot C) ~\lor~ (\lnot A\land\lnot B\land C)\lor(A\land B\land C)
\\
=~&A\oplus B\oplus C
\\ \hline
\therefore \quad &(A\oplus B)\oplus C = A\oplus B\oplus C
\end{split}\\ $


Limit of $\lim_{x\to\infty}\frac{\sin (x)+\cos (3x)}{x+2}\;\;?$

Question: What is the value of $$\lim_{x\to\infty}\frac{\sin (x)+\cos (3x)}{x+2}\;\;?$$
All I can comment on this is that the denominator will increase  to infinity while the expression $\sin (x)+\cos (3x)$ will be at most $2$ , since for all $x$ values $-1\leq \sin x\leq 1$ and the same goes for cosine function.
What is the value of this limit?
The answer is $0$.
Comment: Squeezing.${}{}{}{}{}{}$
Comment: In fact the question is like that : "Find the value of this limit." How can we find the value,instead of proving it to be 0 ?
Comment: It is clear from your post that you saw that the limit is $0$, and basically why.  In general, even for nicely convergent sequences, the value of the limit is usually quite unobvious. OK, maybe it is not clear that you saw. The guy on top can't more past $2$. and the bottom is blowing up. So for large $x$ the ratio is near $0$.
Comment: Thanks,so for such a question this conclusion is the solution,instead of an analytical solution.
Comment: It depends. In an "analysis" course you would be expected to provide full "$\epsilon$-$N$" detail. In an informal level calculus course, it would probably enough for you to show that you knew what was hapenning.
Answer: By the Squeeze Theorem, we have
$$\dfrac 0{x+2} = 0 \leq \left| \frac{\sin x + \cos 3x}{x + 2}\right| \leq \frac{|\sin x| + |\cos 3x|}{x + 2} \leq \frac{2}{x + 2}$$
$$\lim_{x\to \infty} 0 = 0$$
$$\lim_{x \to \infty} \dfrac 2{x + 2} = 0$$
Therefore, $$\lim_{x \to \infty} \frac{\sin x + \cos 3x}{x + 2} = 0$$
Comment: an absolute value is always $\ge0$, so you probably didn't need the $\frac0{x+2}$ :-)
Comment: Thanks,it is nice and clear for me.
Comment: @amWhy: Nice to get that feedback! +1
Answer: Let $\epsilon > 0$ be given and choose $N$ such that $x > N \implies \frac{2}{x + 2} < \epsilon$. Then
$$\left| \frac{\sin x + \cos 3x}{x + 2} - 0\right| \leq \frac{|\sin x| + |\cos 3x|}{x + 2} \leq \frac{2}{x + 2} < \epsilon$$
for all $x > N$. Thus, the limit is $0$ as desired. I will leave it to you to give an explicit $N$ in terms of $\epsilon$.
Comment: @guest: this is an epsilon-delta proof. You will encounter them shortly if you haven't already.
Comment: I'm totally unfamiliar to this solution.But thanks for your time and effort.


Sql server 2005 - Returning data as XML or datatables

Question: I have three classes
a) Parent - Which contains some properties, an Item collection and a collection of children
b) Child  - Which contains some properties and an Item collection
c) Item   - Which contains some properties
The relationship is depicted in the below XML structure.
<code><Parents>
    <Parent1>
        <Property1></Property1>
        <Property2></Property2>
        <Property3></Property3>
            <Parent1Children>
                <Child1>
                <Child1Property1></Child1Property1>
                <Child1Property2></Child1Property2>
                </Child1>
                <Child2>
                <Child2Property1></Child2Property1>
                <Child2Property2></Child2Property2>
                </Child2>
            </Parent1Children>
        <Parent1SomeCollection>
            <Item1>
                <Item1Property1></Item1Property1>
                <Item1Property2></Item1Property2>
            </Item2>
            <Item2>
                <Item2Property1></Item2Property1>
                <Item2Property2></Item2Property2>
            </Item2>
        </Parent1SomeCollection>
    </Parent1>
</Parents>
</code>
I need to use the data from the tables of the three classes above. I can think of the following options:-

Get this structure in the form of
XML from the database as depicted
above from a stored procedure and
use the same as XDocument in the
business layer.
Get in the form of 3 tables from the
database them in datatable and
establish relationship between the
to get the data?

Which one would give optimum performance and is a better way or are there better ways to do the same?
Answer: A DataSet would hold the data better (assuming you're referring to .NET code), and maintain the relationships, while allowing you the ability to save the data in XML, and perform mass inserts and updates. So performance should be very good. Using an ORM would also be a good idea, since the ORM would give yous similar functionality, plus it may support LINQ (ORMs like Entity Framework and Linq-to-SQL).
Comment: +1 However, I don't intend to update data in this XML. This would be a read-only structure. Thats one reason why I don't think I should create a datatable. Could you please throw some light here If I cant see the obvious?
Comment: The DataSet / DataTable (DataSet supports the 1:N relationships you show, DataTables don't) is what a SqlCommand objects return, so no casting or translating the data types. You don't have to use it for updates / inserts. You may even be able to mark it as read-only. Plus the .WriteXml method will generate the XML data structure for you.


How can I have multiple forms share a common menu in a Winforms app?

Question: I have an existing .NET Winforms app made with a few complex forms, and
1) the forms all live for the life of the app, and
2) only one form is displayed at a time (the user switches between the forms)
I need for these forms to share a common menu that will be processed by a single business-logic controller.  (The visible form is also continuously updated by the controller)
Is there a way to the same menu appear at the top of each form and have the menu processed by their common controller without having to make it an MDI application?
Many thanks!
Answer: Create a base form with the menu.  Then the individual forms can inherit this base form, automatically getting the same menu.  To refactor the existing forms, edit the form.cs and change
<code> public partial class Form1 : Form
</code>
to
<code> public partial class Form1 : MyBaseForm
</code>
Comment: I would also add your menu commands as private methods that call a virtual or abstract On function so you can handle the actions differently on each form.  For common actions you should be able to code them into your base form directly.
Answer: Build the menu in a custom control (user control), and then add that control to each of your forms.
Answer: Old question, I know, but my answer might still help people.
This class would be a good start to manage this:
Disclaimer: Not tested, but the idea should work.
<code>static class FormService{
    /*
     * Members
     */
    private static Dictionary<string, Form> _forms = new Dictionary<string, Form>();
    private static Dictionary<string, Type> _types = new Dictionary<string, Type>();
    private static Form opened_ = null;

    /*
     * Properties
     */

    public static Form Opened

    /*
     * Methods
     */
    public static void register(string className, Type type){
        if (_types.ContainsKey(className))throw new Exception("Form already registered");
        _forms.Add(className, Activator.CreateInstance(type));
        _types.Add(className, type);

    }

    // use with Convert.ChangeType
    public static Type getFormType(string className){
        return _types[className];
    }

    public static void open(string className){
        if (opened_ != null){
            opened_.Hide();
        }
        opened_ = _forms[className];
        opened_.ShowDialog(); // show dialog will block main menu actions
    }

}
</code>


Are persistent connections used?

Question: How can I find out whether my Drupal 7 site using persistent connection or not? And it's possible to turn it on if it's not?
Answer: If the persistent connections is not enabled you can enable it by adding this to settings.php:
ini_set('mysql.allow_persistent', TRUE);

And you just do like this to turn it off:
ini_set('mysql.allow_persistent', TRUE);

To check is persistent connections is enabled go to /admin/reports/status/php, and search for allow_persistent.
Comment: Thank you. How can I check whether it really works or not? Maybe somewhere in Drupal logs/reports

Are persistent connections used?


Google chrome not rendering the background gradient at all

Question: I was reading this article http://www.sitepoint.com/using-unprefixed-css3-gradients-in-modern-browsers/ .I created this little demo of what this article teaches.

        <title>Css Gradients</title>

Now the problem is,Firefox is rendering the background gradient correctly but Google Chrome(version 22) is not rendering the background gradient at all.See the screenshots
Comment: What is the point of this thread, do you have a question?
Comment: The question is loud and clear.I want to know why Chrome is not rendering the vendor prefixed free background gradient when Firefox is
Answer: At the moment (Chrome 24 / Safari 6) Webkit still have not added support for unprefixed css3 gradients yet.
This is a bit sad, if you consider the fact that even IE10(!) uses unprefixed syntax already.
Reference: http://caniuse.com/#search=grad


Substituting key with environment variable in HOCON

Question: I am using pureconfig to read config for my Scala application. The application.conf file is in HOCON format. My config file looks something like this
weathers {
    ${?HUMID_WEATHER_ID} {
      temperature = 10
      temperature = ${?HUMID_WEATHER_TEMPERATURR}
    }
    ${?HOT_WEATHER_ID} {
      temperature = 10
      temperature = ${?HOT_WEATHER_TEMPERATURR}
    }
}

weathers is a map whose keys should be substituted by the environment variables HUMID_WEATHER_ID and HOT_WEATHER_ID. But when I read the config I get this exception
 135) Unable to parse the configuration: expecting a close parentheses ')' here, not: '${'HUMID_WEATHER_ID'}'.

The substitutions work fine in values but not in Keys. Is this intended? Is there a way to get around this?
Comment: I believe it's intended. One way is to make the keys static like `humid_wheather` and put another key inside it with the value being the env variable. This will force you to adapt slightly the app code though.
Comment: Yeah the HOCON spec effectively disallows substitutions for keys.  It's not exactly clear what the intention here is for dynamic keys like this, what higher-level problem this proposes to solve.
Answer: Rather than use an ID for a key, might I suggest a static name such as hot and humid? Each element then has an id field that can be substituted.
weathers {
    humid {
      id = ${?HUMID_WEATHER_ID} {
      temperature = 10
      temperature = ${?HUMID_WEATHER_TEMPERATURR}
    }
    hot {
      id = ${?HOT_WEATHER_ID}
      temperature = 10
      temperature = ${?HOT_WEATHER_TEMPERATURR}
    }
}

Then change your code to do interpolation on the name rather than a numeric string
val weather = "humid"
val id = config.get(s"weathers.$weather.id")


Solved inverse Galois problem for $\mathbb{C}(z)$ seems to contradict the theory about Liouvillian extensions.

Question: The theory about Liouvillian extensions tells us that a Picard-Vessiot extension $L \supset k$ is Liouvillian if and only if the identity component $G^°$ of $G = Gal(L / k)$ is solvable.
I think I found a Liouvillian extension, that has a non-solvable subgroup, which seems to be absurd. I don't see any mistake, so please help me out!
Counterexample Let $k = \mathbb{C}$ be the complex functions with zero-derivation, i.e. it is its own field of constants. Let $\mathbb{C}(z)$ be the function field in one variable, with the common derivation $z' = 1$. According to Wikipedia or this paper the inverse Galois problem for $\mathbb{C}(z)$ is solved positively, so we can choose an algebraic extension $L \supset \mathbb{C}(z)$, such that $H := Gal(L / \mathbb{C}(z))$ is not solvable (e.g. $H = A_5$). Now $L$ is algebraic over $\mathbb{C}(z)$, so there is a unique way to extend the derivation to $L$. In particular, $L / \mathbb{C}(z)$ is a Picard-Vessiot extension, so $L / \mathbb{C}$ is one as well. Choosing a primitve element $t \in L$ such that $L = \mathbb{C}(z, t)$, we see that $L / \mathbb{C}$ is in fact Liouvillian.
Its differential Galois group $Gal(L / \mathbb{C})$ is connected, because $\mathbb{C}$ is algebraically closed, but it also contains the closed non-solvable subgroup $H = Gal(L / \mathbb{C}(z)) \subset Gal(L / \mathbb{C})$.
So this seems to contradict the fact that $Gal(L / \mathbb{C})$ should be solvable.
PS: If someone comes up with such an explicit extension $L \supset \mathbb{C}(z)$ I would be grateful, because this simplifies the argument quite a bit. I had not yet time to think about this.
Comment: I know next to nothing about differential Galois groups, so I'm the wrong person to judge. But, why should the group $H=Gal(L/\Bbb{C}(z))$ of *field automorphisms* be a subgroup of the group of *automorphisms of differential fields* $Gal(L/\Bbb{C})$? The extension $L/\Bbb{C}(z)$ is simple (it is finite and separable). Therefore any differentiation on $\Bbb{C}(z)$ has a unique extension to a differentiation of $L$. I'm probably very wrong about this, and missing something big. Something just does not compute (for me) :-(
Comment: @JyrkiLahtonen As you said, there is a unique differentiation of $L$, making $L / \mathbb{C}(z)$ into a differential extension. Thus $\mathbb{C}(z)$ is a differential field between $L$ and $\mathbb{C}$. By (differential) Galois theory we get an inclusion $Gal^d(L / \mathbb{C}(z)) \subset Gal^d(L / \mathbb{C})$ of differential Galois groups (here marked by ${}^d$). Now the point is that (classical) Galois extensions are also Picard-Vessiot extensions, and then the classical Galois group coincides with the differential Galois group, i.e. $Gal^d(L / \mathbb{C}(z)) = Gal(L / \mathbb{C}(z))$.
Comment: This is **Exercise 1.24** in van der Put's *Galois Theory of Linear Differential Equations*.
Comment: Thanks. I made category errors. Probably several :-/
Answer: So I discussed this with my professor, and our consensus was, that $\mathbb{C}(z,l) / \mathbb{C}$ is not a Liouvillian extension. To give a counterexample, take $l$ to be a square root of $z$, i.e. $l^2 = z$. Then $l$ generated $\mathbb{C}(z,l) = \mathbb{C}(l)$, but will never satisfy a differential equation
$$ l^{(n)} + c_1l^{(n-1)} + \dotsc + c_{0}l = 0,$$
because differentiating $l$ produces higher powers of $l^{-1}$.
Comment: *differentiating, not derivating


What is the equivalent of Sets.GetAllString() from BookSleeve in StackExchange.Redis?

Question: In BookSleeve there is a connection.Sets.GetAllString() method.
What is the equivalent in StackExchange.Redis?
Thanks!
Answer: StringGet has an overload that takes an array of RedisKey-instances, so this would be the best way to get more than one string at once :)
Answer: Found it: connection.SetMembers(...) gets all the strings for a set from a key.
Comment: oops, didn't see that you meant for a set, must've misread your question :)
Answer: After a lot of searching, the best I can come up with is to abuse SetCombine(...).  The basic concept is to ask redis to "combine" a single set and return the result.  This will return all the values in that single set.
Also Sets.GetAllString(...) returned an array of strings. SetCombine returns an array of RedisValue.  I wrote this little extension method to help refactor some code I am working on.
<code>internal static class StackExchangeRedisExtentions
{
    internal static string[] SetGetAllString(this IDatabase database, RedisKey key)
    {
        var results = database.SetCombine(SetOperation.Union, new RedisKey[] { key });
        return Array.ConvertAll(results, item => (string)item);
    }
}

// usage
string key = "MySetKey.1";
string[] values = database.SetGetAllString(key);
</code>
I am not a fan of this solution.  If I have missed something obvious, please let me know.  I would be glad to get rid of this...


Confused about PCA transformation vectors

Question: I'm trying to get the intuition of how PCA works. So far I understood that:

I start from the input matrix $X = [X_{1},...,X_{p}]$  where each $X_{i}$ is composed by $n$ elements that are the $n$ observations for those features ($X_{i}$, in fact) and therefore $X$ is a ($n$ x $p$) matrix.
In order to transform my starting problem into a lower-dimensional one I must define a transformation and thus transformation vectors like:
$w_{(k)} = (w_{1},...,w_{p})_{(k)}$
that are $p$-dimensional vectors.
I must compute the 1st PCA vector by means of:
$w_{1} = argmax _{||w||=1} {1 \over m} \sum_{i=1}^m [(x_{i}w^2)]$.
The other PCA vectors, in general, will be computed as:
$w_{k} = argmax _{||w||=1} {1 \over m} \sum_{i=1}^m [(x_{i}-\sum_{j=1}^{k-1}x_{i}w_{j}w_{j}^T)w]^2$

(in brief: for the k-th PCA vector I have to subtract all other components to the data input matrix in order to pick the feature with the higher variance).
My questions now are:

are those $w_{(k)}$ vectors composed by just all zeros except for that element that corresponds to the feature I want to consider? I mean: since I know that $w_{(k)}$ are unit vectors (lenght one) then they have to be composed by all zeros except for one component that will be 1. Is this 1 used to pick from the associated $x_{i}$ the feature I want to consider?
Are $w_{(k)}$ vector of dimension $(1$ x $p)$?
Comment: Tangential comment: For intuition, we can mention the big picture idea that although each point $X_i$ belongs to $\mathbb R^n$, and $n$ is very large, it often happens that all the points $X_i$ secretly belong to a low dimensional affine subspace.  Finding a low dimensional affine subspace that contains the points $X_i$ (as closely as possible) is simply a linear algebra problem (and it's similar to the problem of finding an optimal low rank approximation of a given matrix, which is solved by the SVD).
Answer:

are those $w_{(k)}$ vectors composed by just all zeros except for that element that corresponds to the feature I want to consider? I mean:
  since I know that $w_{(k)}$ are unit vectors (lenght one) then they have to
  be composed by all zeros except for one component that will be 1. Is
  this 1 used to pick from the associated $x_i$ the feature I want to
  consider?

For a vector $v = [v_1, ..., v_p]$, being a unit vector means that $v$ has length 1, i.e. $\|v\| = \sqrt{\sum_{i=1}^{p} v_i^2} = 1$. It doesn't mean that $v$ contains a single 1 and all other elements 0 (although that is one example of a unit vector).
You can think of the weight vectors $w_{(k)}$ as being the directions of maximal variance in your data space. For example, imagine you had an Gaussian/elliptical cloud of data points in a 2d space, centered at the origin. The first principal component would point in the direction along which the ellipse is longest. The second principal component would point in the orthogonal direction. The weight vectors will always be orthogonal to each other. So, the only way your weight vectors would contain a single one and the remaining elements zero is if the directions of maximal variance are parallel to the existing data axes.
You can think of PCA as a way to find the directions of maximal variance in the data, rotate the data s.t. these directions are aligned with the axes, then discard the dimensions along which the variance is small.

Are $w_{(k)}$ vector of dimension $(1 \times p)$?

In your setup (where columns of the data matrix correspond to features/dimensions), each $w_i$ will be a column vector, size $(p \times 1)$
A couple other points:

You're describing an iterative way to compute PCA. This is good for getting an intuition for what PCA is doing. In practice, you'd generally perform the computation another way. For example, you can solve for all weight vectors simultaneously as the eigenvectors of the covariance matrix of the data. This can be faster and more numerically stable than the iterative method you're describing, depending on the situation. There are many other ways to do it too.
Probably a typo: In your step (3), the square should be on the outside of the parentheses (you want the square of the projection of $x_i$ onto $w$, not the projection of $x_i$ onto $w^2$)
Comment: Thank you for the answer. However, about your second point on the end, I'm looking at https://en.wikipedia.org/wiki/Principal_component_analysis and the square is on the outside of the parentheses actually. It was out there also in the case of 1st PCA vector I found above.


how to use tilt to cache / precompile slim file in sinatra views

Question: Slim documentation says that the template/.slim file can be compiled and cached with tilt. I am using slim for the views in sinatra. How to precompile the file / is it possible to view the compiled file..
Also would it cache the precompiled file after the first call. should I do some setting for it in sinatra
Thanks
Answer: well, the Sinatra Readme is your friend. In :production all your views are compiled by default. Please also have a look at the Caching section in the Readme which will give you the different options.
Comment: there probably is a .slim dir somewhere


Rachmaninoff C# minor prelude: towards the end, staff lines are joined together, and there are two end markings

Question: I'm studying Rachmaninoff's Prelude in C# minor and towards the end, the staff lines are joined together, with two ending lines.
Do I need four hands? :)
Comment: Mandatory ["Rachmaninoff had big hands"](https://www.youtube.com/watch?v=ifKKlhYF53w).
Comment: Thanks, indeed he did. There are even resulting theories about the cause being a genetic disorder: https://www.rachmaninoff.org/articles/archive/30-on-the-phenomenal-hands-of-sergei-rachmaninoff.
Answer: Thankfully, four hands are not necessary. Nor are (both) feet.
The two lower staves are for the left hand, and the two upper staves are for the right hand: from bottom to top LH LH RH RH. Notice that in most cases, when the lower LH and RH staves have chords, the upper LH and RH staves have rests. Play the left-hand chords, then jump up to play the right-hand ones, then back down for the left-hand ones.
This arrangement is used to indicate two distinct parts: the lower, more ominous chords; and the upper bell-like chords. By splitting the staves, it can help illustrate the distinction. See also When does one use an extra stave for piano music?
In the case where the chords are simultaneous — the first and third in the indicated excerpt — treat the lower staff chords like grace notes slightly before the beat, and play the upper chords on the beat.
This is not a rule, but it is a strong convention. When arpeggiating chords, for example, the assumption is to start from the bottom. Similarly, when playing chords as in this piece, the working assumption is to go from low to high. The low-first convention is evident throughout this particular piece, with no indication in should be changed in these particular measures. As one example of many, here is Evgeny Kissin. The video is timed to the measures in question.
The below edition (Ray Alston, ISMLP) illustrates this explicitly.

For more on this notational convention/technique, see When does one use an extra stave for piano music?
Comment: @MihaiDanila Because there are two distinct parts: the lower, more ominous chords; and the upper bell-like chords. By splitting the staves, it can help illustrate the distinction. See also [When does one use an extra stave for piano music?](https://music.stackexchange.com/q/93312/70803).
Comment: How dare you leave such valuable information out of the answer? :P Joke aside, I've added both to your answer; feel free to edit to a more suitable form. And thank you for these great insights and contributions.
Comment: Umm… why was it coded that way?
Comment: Also, why are the lower-staff chords the grace notes and not the other way around? Is that the rule, or is it a case-by-case situation?
Comment: @MihaiDanila Not a rule, but a strong convention. When arpeggiating chords, for example, the assumption is to start from the bottom. Similarly, when playing chords as in this piece, the working assumption is to go from low to high. The low-first convention is evident throughout this particular piece, with no indication in should be changed in these particular measures. As one example of many, here is [Evgeny Kissin](https://youtu.be/SCm9O2KNEX4?t=196). The video is timed to the measures in question.
Comment: Note: The convention to arpeggiate from the bottom up is true for most classical-to-modern music, certainly including Rachmaninoff.  However, in Baroque era music and earlier, the reverse was true.  J.S. Bach, for example, would default to arpeggiating from the top down.  (There are cases where he would arpeggiate from bottom up, but these were
explicitly notated with an arrow on the arpeggio ornament.)  Just pointing out that it's not a hard rule on older music.
Comment: @DarrelHoffman I've always wondered about that. Can you point me to a source for further reading?
Comment: I've mostly seen it in the performance notes at the beginning of any sheet music books of Bach et. al.  If you want an online source, [Wikipedia](https://en.wikipedia.org/wiki/Ornament_(music)) covers some of it.  It turns out many ornaments were "flipped" from what we expect in more recent music, e.g. mordents and trills default to using the note below the principle instead of above, etc.
Comment: When the dynamic marking is *ffff* on each pair of staves, does that mean the total is really *ffffffff*?
Comment: @Theodore I believe it's exponential, not additive.
Comment: @MihaiDanila Thanks for putting in those edits. They really improve the post. Also preserved the info in case the comments are ever deleted. Much appreciated.
Comment: Of course! Your consistent selfless, high-quality help (not just with this question) is quite a motivator, though. I think you deserve much of the credit.
Answer: It is just easier to read as separate staves.  But no, you don't need 4 hands.  2 hands lower staff, 2 hands upper staff.


a measure for MAE (of a regression)

Question: I'm running a grid search, in order to fine-tune a NN hyper parameters.
the question is:
the MAE values I get from the trainings are too close.
since I have the statistical attributes of the target values, is there a way to somehow come up with a starting value for MAE, where the worst regression can achieve.
I think the question is not clear.
the value I'm looking for, is analogous to a probability for classification problems. (example: an neural network which is supposed to classify the inputs into 8 possible classes, will have accuracy of 0.125 just by random classification of inputs. so a 12.5% accuracy is the measure for such classifier)
now, I have the Mean, and StdDev (and other stats if need be) of the target values of my samples. how can I calculate a measure to judge my MAE (and not by comparing different MAE values from different trainings)?
Comment: yes, MAE:mean absolute error. I'm tryna come up with a value for MAE, that even a non trained NN can achieve. (like the worst MAE. I know it's worst possible value is +inf. but wht I'm looking for is a value like E where I can validly say: "my NN should have MAE values of less than E. because even a untrained NN can achieve a MAE of E or less.")
Comment: I tried to explain the thing I'm looking for, by exemplifying the classification analogue. (an 8-class NN classifier, should do better than %12.5 accuracy. cuz any damn classifier can achive that 12.5 accuracy just by randomly choosing a class. (of course, assuming the class probability distribution is uniform))
Comment: In a classification problem, the baseline scenario is not to emit randomly, but to emit default class distributions. For example if the distribution of classes in training data is 70% class A and 30% class B, then you output these figures not 50/50. In the regression case the baseline scenario is the mean only model for Squared Error, or median only model for Absolute Error. For this reason the most basic model always emits the median of the training values (median of the targets, not inputs), and you can calculate MAE from this value.
Comment: THANK YOU. exactly it is. median for MAE. I suggest this should better be posted as answer than a comment, as it is the exact answer for the question. I calculated the median of targets array and MAE (assuming all predictions are median value) and it gives a values close to MAE of my untrained NN. so this is correct.
Answer: I think what you're looking for is the median of the pooled data.
In a classification problem, the most naive way to find class probabilities is to use your training data. In MNIST, you have a 10% chance of each number. If you're a doctor specializing in disease X than affects 7% of people, say that everyone has a 7% chance of having disease X.
The point of regression is to tighten up that estimate for a subject with some other values measured. Perhaps the doctor knows that 7% of people have the disease but that 70% of people who work around asbestos have the disease. If a patient works around asbestos, then 7% probably is the wrong probability to assume. Maybe 70% is wrong, but it's probably more along the lines of the right answer than 7%. (Or maybe not. Maybe there's some other covariate like smoking status or genetics that should be included in the model.)
When you do a regression with a numeric  response variable, the naive way to guess the conditional parameter of interest (often the conditional mean, given covariates like asbestos), is to guess the estimate from the pooled distribution of all training data. This is why r-squared compares to the sum of deviations from the mean of the response variable, as that would be the most naive sensible way to guess. If your regression predicts median instead of mean, then your first guess would be the median of the pooled data. Ditto for other quantile regressions (e.g. 0.25 quantile).

a measure for MAE (of a regression)


Initializing const variables in C

Question: I want to build two libraries lib1.a and lib2.a.
lib2.a is in top level, it can only share with lib1.a global data and not the header files. Then lib1.a can use these data using the extern keyword.
The difficulty that I found is: I want to initialize a constant in a file1.c inside lib1.a with the data defined in lib2.a.
Example:
//lib1.a->file1.c:
const My_Var_Type My_Var = {
    .field1 = data1,
    .field2 = data2,
    .field3 = data3,
    .field4 = data4
};

Now data1 through data4 should be gotten from lib2.a, I declared them as a constant but no way! the compiler generates this error:
   initializer element is not constant

Is there any way to fix this error?
Comment: Please make an [mcve], including your makefile or otherwise complete information on how you build.
Comment: A simple solution is to use C++ compilation and declare `data1` etc.as `extern "C"`.  The semantics of `const` in C++ differ from that of C such that this code is then legal.  I'd post that as an answer except the language police would down-vote it for suggesting C++ as a solution to a C question.  They may be right, but perhaps C++ is what you need any way.
Comment: @Clifford interesting suggestion. To not bothered with compilation and to be very type relaxed I recommend PHP or JavaScript.
Comment: Instead of initializing a `const` with _data_, initializing a `const` with a _constant_.  For more info, best to see the code that defines/declares, `data1`, etc.
Comment: @P__J__ the suggestion of C++ was due to its simple interoperability with C since the libraries lib1.a and lib2.a have a C linkage interface. Using those libraries might not be so straightforward with other languages.
Comment: Do read the description of tags instead of wildly adding them. Your question has nothing to do with [tag:compiler-construction].
Comment: @P__J__, I should use C  compilation.
Answer: You cant initialize the static storage objects (global ones or having attribute static) using non constant expressions - ie another variables for example. It does not matter if those objects are const ot not
This is illegal:
extern int data1, data2, data3, data4;

typedef struct
{
    int field1;
    int field2;
    int field3;
    int field4;
}My_Var_Type;

  const My_Var_Type My_Var=
     {
      .field1 = data1,   /* wrong */
      .field2 = data2,   /* wrong */
      .field3 = data3,   /* wrong */
      .field4 = data4,   /* wrong */
     };

int main()
{
    /* .... */
    return 0;
}

You can initialize this way automatic variables.
int main()
{
  const My_Var_Type My_Var=
     {
      .field1 = data1,   /* good */
      .field2 = data2,   /* good */
      .field3 = data3,   /* good */
      .field4 = data4,   /* good */
     };

    /* ....*/

    return 0;
}

Comment: So it's impossible. Do you have any other suggestion?

Initializing const variables in C


Is a finite cyclic group a Poincare duality group?

Question: I am trying to understand whether the finite cyclic group of order $n$, $C_n$ is a Poincare duality group, i.e. whether it's classifying space $K(C_n,\,1)$ is a Poincare complex. I know that the classifying space of a cyclic group is a lens space, but am unsure whether a lens space is necessarily a Poincare complex.
Comment: As far as I know, for a Poincare duality group,G, there is a finite length projective resolution of Z by ZG modules. If G is cyclic, then any projective resolution of Z is infinite. The classifying spaces for cyclic groups are not finite dimensional manifolds. For instance, the classifying space for Z/2Z is the infinite real projective space.
Answer: No. A Poincaré duality space must in particular have vanishing cohomology above some degree, but a nontrivial finite cyclic group has nonvanishing cohomology in arbitrarily high degrees.


modifying json from php array

Question: I have an array where i fetch from php with variable $allresults :
Array
(
[1] => Array
    (
        [0] => 16/03/2018
        [1] => Friday
        [2] => 21.00
        [3] => Friendly Match
        [4] => Italy vs France
        [5] => Truesports HD
    )
[2] => Array
    (
        [0] => 17/03/2018
        [1] => Saturday
        [2] => 15.30
        [3] => Friendly Match
        [4] => Italy vs Pakistan
        [5] => Truesports HD
    )

[3] => Array
    (
        [0] => 17/03/2018
        [1] => Saturday
        [2] => 19.00
        [3] => Friendly Match
        [4] => Spain vs USA
        [5] => Truesports
    )
)

I have to convert it into json for other production use, so i use json_encode() with this code below
echo json_encode($allresults, JSON_PRETTY_PRINT);

and the result become
[
    "16\/03\/2018",
    "Friday",
    "21.00",
    "Friendly Match",
    "Italy vs France",
    "Truesports HD"
],
[
    "17\/03\/2018",
    "Saturday",
    "15.30",
    "Friendly Match",
    "Italy vs Pakistan",
    "Truesports HD"
],
[
    "17\/03\/2018",
    "Saturday",
    "19.00",
    "Friendly Match",
    "Spain vs USA",
    "Truesports"
]

My point is how can i change this into something like this in my json.
[
    "date":"17\/03\/2018",
    "day":"Saturday",
    "time":"19.00",
    "type":"Friendly Match",
    "value":"Spain vs USA",
    "tv":"Truesports"
]

Should i change this from php array or i could change this from json file directly?
Comment: Your expected output is not a valid json. And where are the other information go?
Comment: Update array key values before encoding into json
Comment: [JSON](http://json.org) is a text representation of some data structure (usually an array or an object). It is used to transfer the data between computers. At the destination it is decoded to get back the original data structure. It is not easily editable and it was not designed to be editable. Read about PHP [arrays](http://php.net/manual/en/language.types.array.php), generate the array you need then [`json_encode()`](http://php.net/manual/en/function.json-encode.php) it.
Comment: indeed i need to modify it from php.. thanks for all the answer
Answer: You have to update your array with key. Or second option is you can create new array with your expected output key:
$array = Array
(
"1" => Array
    (
        "0" => "16/03/2018",
        "1" => "Friday",
        "2" => "21.00",
        "3" => "Friendly Match",
        "4" => "Italy vs France",
        "5" => "Truesports HD",
    ),
"2" => Array
    (
        "0" => "17/03/2018",
        "1" => "Saturday",
        "2" => "15.30",
        "3" => "Friendly Match",
        "4" => "Italy vs Pakistan",
        "5" => "Truesports HD",
    ),

"3" => Array
    (
        "0" => "17/03/2018",
        "1" => "Saturday",
        "2" => "19.00",
        "3" => "Friendly Match",
        "4" => "Spain vs USA",
        "5" => "Truesports"
    )
);
$array_keys= array("date","day","time","type","value","tv");
$new_array = array();
foreach ($array as $key => $value) {
  $tmp_array = array();
  foreach ($value as $key1 => $value1) {
    if(isset($array_keys[$key1]))
        $tmp_array[$array_keys[$key1]] = $value1;
  }
  $new_array[] =$tmp_array;
}

echo json_encode($new_array, JSON_PRETTY_PRINT);

DEMO
Comment: thanks it works, but there is a php error on this line         $tmp_array[$array_keys[$key1]] = $value1;

error code  Undefined offset: 6

other than that it works great
Comment: ah i figured it out as i have more than 6 subarrays in another arrays..thank you
Comment: i cant upvote, but i have selected the answer thank you
Answer: Use This
$field = array("date","day","time","type","value","tv");

echo "
";

foreach($yourArr as $value){

    $j = 0;

    foreach($value as $arrVal){
        //Get Field Name
        $fieldKey = $field[$j];

        $newArr[$fieldKey] = $arrVal;

        $j++;
    }
    $finalArr[] = $newArr;
}
print_r($finalArr);

Comment: thanks it works great as answer above but i'm getting same php error for this line             $fieldKey = $field[$j];

getting   Undefined offset: 6
Comment: ah i figured it out as i have more than 6 subarrays in another arrays
Answer: I used array_combine() function -  Creates an array by using one array for keys and another for its values
$array = array
(
"1" => array
    (
        "0" => "16/03/2018",
        "1" => "Friday",
        "2" => "21.00",
        "3" => "Friendly Match",
        "4" => "Italy vs France",
        "5" => "Truesports HD",
    ),
"2" => array
    (
        "0" => "17/03/2018",
        "1" => "Saturday",
        "2" => "15.30",
        "3" => "Friendly Match",
        "4" => "Italy vs Pakistan",
        "5" => "Truesports HD",
    ),

"3" => array
    (
        "0" => "17/03/2018",
        "1" => "Saturday",
        "2" => "19.00",
        "3" => "Friendly Match",
        "4" => "Spain vs USA",
        "5" => "Truesports"
    )
);
$array_keys= array("date","day","time","type","value","tv");
$newArray=array();
foreach($array as $subArr){
    $newArray[] = array_combine($array_keys,$subArr);
}
echo json_encode($newArray, JSON_PRETTY_PRINT);

Demo

modifying json from php array


How to avoid Exif data rotating images automatically during approval selection?

Question: I've been doing some digging, but so far with no results. I'm currently looking for a way of forcing or letting the admin to dictate the orientation of an image. I came across an issue where some user uploaded content looked correct upon initial upload, but when the admin began the approval process, the files hidden orientation data changed it from a vertical to a horizontal orientation. Now mind you I know that I could simply re-save that file, pull down the submitted post data and re-upload with the correct image, but I'm trying to be as efficient as possible in the event someone outside myself is administrating the content one day.
Has anyone come across a module that could help force orientation away from EXIF content or other data?
Thanks in advance!
Comment: using ImageMagick as your image handler will allow you to access some really, really powerful image manipulation tools.  you should start there.
Comment: Thank you 5482. I'll give it a shot and update based on my findings.
Comment: Gave it a shot, but overall this option is not exactly what I'm going for. Nice tool though. This is more post upload, I'm looking to force the upload to respect from start. This may work as a secondary option.
Comment: I'll expand a bit in an answer below.
Comment: Could you please make title actual question? It would make it easier to find it, or to know if what you ask is in particular expert's area of knowledge - most people will not read a wall of text if they don't expect to be able to help from the very beginning, and nonspecific title does not help.
Answer: Once you've set up ImageMagick you can start using it in a file hook to process your images on upload.
Some good hooks to look at would be:
https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_file_presave/7
https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_file_insert/7
Watch for an image being uploaded, read exif data with imagemagick, adjust accordingly.
Comment: Right, I understand that. But I want it to ignore the EXIF data and maintain the orientation at which it's being uploaded, not the information that EXIF is stating it should be working with.
Comment: By the sound of it, you may have got this backwards. Generally it's the original client that respects EXIF data and rotates the image accordingly. The uploaded copy does not take EXIF into account, hence it appears to have been rotated although in fact it hasn't. The exif_orientation module mentioned by @Matt Glaman addresses the issue.
Comment: Nope, I am understanding it correctly. My current website is using EXIF mod – Note: I turn it of and on based on my testing of resolutions to this issue – and it is indeed an issue with the files EXIF data. I've saved different versions of the image so that the I have all of the possible options to work with.

The interesting point to me is that the file uploads correctly no matter what in the upload process, and in the file system shows up properly orientation wise, but when I accept and attach the file during the approval process, the image re-orients to the wrong orientation. Just strange.
Answer: We had this issue and put up a module that fixes orientation based on EXIF data. Link to project https://www.drupal.org/project/exif_orientation
No need for ImageMagick.
Comment: Hi Matt, and thanks for your post. I'd mentioned before it's the EXIF data that's actually causing the complication. What I'm looking to do is ignore the EXIF data and force it to upload the way it's put into the form instead of any underlying exif data that may be trying to rotate it.
Comment: Ah, I misunderstood the situation. In testing only viewing the picture directly in Chrome or proper picture viewer would images display properly. Otherwise (iOS uploads in our case) the image would not be rotated properly until we did so in our hook.