Data structure that inserts in O(log2n), yet searches in O(1)

Question: Currently working on an assignment that requries a  data structure to insert in O(log2n), yet be able to search for an element in O(1). I was thinking a BST because of the log2n insert, but will be unable to search in O(1). A hash table can insert at worst O(n), with a search of O(1), but unfortunately this doesn't fit the O(log2n) insert requirements.
Anybody have any suggestions? thanks!
Answer: You can insert element into a binary tree, and pointer to that element - into hashtable.
Or, you can just insert element into hashtable with O(1), and search with O(1), and for comply requirement "Insert O(lb N)" - just run LogBin(N) empty loops.
Regarding "worst case/average case": both hashtable and binary tree (non balanced like rb, just primitive) has O(N) in worst case. I think, your assignment is about "average case", this is usual. And hashtable can provide O(1). To preserve attack with selected dataset, use the "universal hashing".


NLTK v3.2: Unable to nltk.pos_tag()

Question: Hi text mining champions,
I'm using Anaconda with NLTK v3.2 on Windows 10.(client's environment)
When I try to POS tag, I keep getting a URLLIB2 error:
URLError: <urlopen error unknown url type: c>

It seems urllib2 is unable to recognize windows paths? How can I work around this?
The command is simple as:
nltk.pos_tag(nltk.word_tokenize("Hello World"))
edit:
There is a duplicate question, however I think the answers obtained here by manan and alvas are a better fix.
Comment: Possible duplicate of [Python NLTK pos\_tag throws URLError](http://stackoverflow.com/questions/35827859/python-nltk-pos-tag-throws-urlerror)
Comment: looks like yeah. I read that post prior.
Answer: EDITED
This issue has been resolved from NLTK v3.2.1. Upgrading your NLTK version would resolve the issue, e.g. pip install -U nltk.

I faced the same issue and the error encountered was as follows;
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\tag\__init__.py", line 110, in pos_tag
tagger = PerceptronTagger()
  File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\tag\perceptron.py", line 141, in __init__
self.load(AP_MODEL_LOC)
  File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\tag\perceptron.py", line 209, in load
self.model.weights, self.tagdict, self.classes = load(loc)
  File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\data.py", line 801, in load
opened_resource = _open(resource_url)
  File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\data.py", line 924, in _open
return urlopen(resource_url)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 391, in open
response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 414, in _open
'unknown_open', req)
  File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1206, in unknown_open
raise URLError('unknown url type: %s' % type)
urllib2.URLError: <urlopen error unknown url type: c>

The URLError that you mentioned was due to a bug in the perceptron.py file within the NLTK library for Windows.
In my machine, the file is at this location
C:\Python27\Lib\site-packages\nltk-3.2-py2.7.egg\nltk\tag\perceptron.py

(Basically look at an equivalent location within yours wherever you have the Python27 folder)
The bug was basically in the code to find the corresponding location for the averaged_perceptron_tagger within your machine. One can have a look at the line 801 and 924 mentioned in the data.py file regarding this.
I think the NLTK developer community recently fixed this bug in the code. Have a look at this commit made to their code a few days back.
https://github.com/nltk/nltk/commit/d3de14e58215beebdccc7b76c044109f6197d1d9#diff-26b258372e0d13c2543de8dbb1841252
The snippet where the change was made is as follows;
self.tagdict = {}
self.classes = set()
    if load:
        AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE))
          self.load(AP_MODEL_LOC)
        # Initially it was:AP_MODEL_LOC = str(find('taggers/averaged_perceptron_tagger/'+PICKLE))

def tag(self, tokens):

Updating the file to the most recent commit worked for me and was able to use the nltk.pos_tag command. I believe this would resolve your problem as well (assuming you have everything else set up).
Comment: Works like a dream. Thanks @MananVyas
Comment: FWIW I had the same error on Win10 python 3.4 (64Bit) with nltk installed via pip and up to date as of April 2nd. Finding the percepthon.py file and making the change in the snippet above worked after a restart for good measure. Wish I had seen this post 4 hours ago though because I thought it was my tokens that were the problem
Comment: Sorry for adding the edit to your answer, this is to avoid cross-platform communication and NLTK users starting new issues on the github repo on this resolved issue.
Answer: EDITED
This issue has been resolved from NLTK v3.2.1. Please upgrade your NLTK!

First read @MananVyas answer for the why:
https://stackoverflow.com/a/35902494/610569

Here's the how, without downgrading to NLTK v3.1, using NLTK 3.2, you can use this "hack":
>>> from nltk.tag import PerceptronTagger
>>> from nltk.data import find
>>> PICKLE = "averaged_perceptron_tagger.pickle"
>>> AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE))
>>> tagger = PerceptronTagger(load=False)
>>> tagger.load(AP_MODEL_LOC)
>>> pos_tag = tagger.tag
>>> pos_tag('The quick brown fox jumps over the lazy dog'.split())
[('The', 'DT'), ('quick', 'JJ'), ('brown', 'NN'), ('fox', 'NN'), ('jumps', 'VBZ'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'JJ'), ('dog', 'NN')]

Comment: Which OS are you using? What is your Python version? How did you install python? How did you install NLTK? Did you install through `pip` or `conda`? Where are you running Python? From the command prompt, terminal or in some IDE? Are you running it through a server or a cloud? Are you running it on your laptop/computer? Or in some school's lab where there might be a firewall? Where are you running the python script? Did you have any other file name call `nltk.py` in your directory?
Comment: After upgrading to NLTK 3.2 did you use the `AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE))` hack?
Comment: Sorry for the multiple questions, your short comment isn't enough to help us debug the problems, please answer each of the questions in the previous 2 comments and we'll try to find a solution afterwards. Actually, it'll also be easier if yo ask another question and state all the answers to those questions in the comments, it looks like it's another problem.
Comment: I ran the above code and it worked fine but when I try to run my nltk routine it still gives <> the code I am using is in http://stackoverflow.com/questions/36255291/extract-city-names-from-text-using-python/36255377?noredirect=1#comment60196241_36255377 I also ran Sarim Hussain's suggestion successfully but no luck.
Comment: try upgrading your nltk, `pip install -U nltk`
Comment: just tried that. Still same error. On pip command I get <<
writing dependency_links to nltk.egg-info\dependency_links.txt
warning: manifest_maker: standard file '-c' not found
reading manifest template 'MANIFEST.in'
warning: no files found matching 'Makefile' under directory '*.txt'
warning: no previously-included files matching '*~' found anywhere in distribution
writing manifest file 'nltk.egg-info\SOURCES.txt'

Successfully installed nltk-3.2>>
Comment: Thanks -I have created a new question for this at http://stackoverflow.com/questions/36349755/nltk-routine-gives-raise-urlerrorunknown-url-type-s-type-in-python
Answer: I faced the same issue a while back.
Solution:
nltk.download('averaged_perceptron_tagger')

NLTK v3.2: Unable to nltk.pos_tag()


Rhythmic/Metric Stress Patterns

Question: The attached images are a page from Jack Perricone's Melody in Songwriting.  I am familiar with the concept of stress patterns in music, but, honestly, have no idea what the hell he's on about here.
Firstly, how would you describe the difference between "Rhythmic Stress" and "Metric Stress"?  It seems to me the metric stress is just the stress outline for quarter-notes of different time signatures, while the rhythmic stress focuses on subdivisions as well.
Secondly, what sort of principles do you suspect the author is utilizing to come up with the "Summative" stress?  I came up with the following "addition rules" before getting frustrated:

Anything plus 0 is itself
Weak stress (-) plus strong stress (/) is a moderately strong
stress (//)
Moderately strong stress (//) plus strong stress (/) is a strong
stress (/)

Whether you agree with the author's analysis or not, can you at least help decipher the principles he is using?  Thank you.
286642
Comment: Both "Summational Stress" diagrams look oversimplified to me. Ideally, I think they both should look like "/--///--//--///--", with /// being between // and - stress-wise, but perhaps the author thought that diagram would look too complicated.
Comment: Does this answer your question? [Understanding Rhythm with Time Signatures](https://music.stackexchange.com/questions/99112/understanding-rhythm-with-time-signatures)
Answer: I have no disagreements with Aaron, Perricone, or you.  As you pointed out, however, Perricone didn't explain what he meant by 'summative'.  Your 'math' seems correct.  I'll offer another way to look at it, mathematically.  (This is not any standard methodology; just my interpretation of Perricone's work.)
<code>/ (strong stress) can be thought of as 'play this note loud' (forte)
// (moderately strong stress) can be thought of as 'no special dynamics for this note' (let's call this pianoforte)
- (weak stress) can be thought of as 'play this note soft' (piano)
</code>
Now if you want to think of how these add mathematically, you could assign a value of 1 to forte, 0 to pianoforte, and -1 to piano.  Think of a number line that only goes from -1 to 1, so anything above 1 is overflow, and results in 1.  Below -1 results in -1.   1+1 = 1, and -1 + -1 = -1.
So p + p = p; f + f = f; f + p = pf; f + pf = f; and p + pf = p.
Here's a re-interpretation of Perricone's example.  I've expanded the quarter-notes to tied triplets, to align them for easier addition.  Tied notes are assigned the neutral value 'pf', or 0.
Comment: Nice first answer! Welcome to the site!
Answer: How would you describe the difference between "Rhythmic Stress" and "Metric Stress"?
As you say, metric stress is the basic beat pattern for the given time signature. It's not strictly accurate to link this to the quarter-note, though. For example, in 3/16 time, the metric stress of strong-weak-weak goes with the sixteenth note.
What sort of principles do you suspect the author is utilizing to come up with the "Summative" stress?
He's saying here is that rhythmic stresses combine with the metric stresses. For example, in a triplet starting on the beat, the rhythmic stress pattern is strong-weak-weak, regardless which beat it occurs on. However, if you have a measure of 2/4 time comprising two triplets, the "strong" beat of the first triplet will be stronger that the "strong" beat of the second, because metrically beat 1 is stronger than beat 2.
Your three principles outline this correctly. Another way to picture it would be wave forms. The metric pulse creates a wave, and the rhythmic pulse creates a wave. The sum of the two waves gives you the stress pattern for the measure.

EDIT: Understanding, based on your comments below, that your intention is to fit lyrics to the "natural" metric/rhythmic stresses, I would say that the unstressed half-beats would remain that way, treating them as effectively equal. The on-beats would go [ strongest 3rd-strongest 2nd-strongest weakest ], which matches the standard 4/4 pattern. So the composite would be [ / u /// u // u //// u ].
Comment: Thanks @aaron.  I suppose what i'm having trouble visualizing is this:  when two waves line up, are they added together (2*wave) or do they simply overlap (wave*wave=1)?
Comment: what would weak plus moderately strong be?  What about a stress that is rhythmically weak but metrically indeterminate?
Comment: @286642 Overall, I would say the process is additive (i.e., 2*wave). In your second question, it would help to have some clarification on where you see those situations arising. However, I think there's a risk of taking the author too literally in his explanation. He's trying to offer some general, "all things being equal" guidelines for how different rhythmic aspects affect the shape and nuance of a measure. But in the end, it's the "feel" that determines the actual realization of the stresses. ...
Comment: @286642 (cont.) ... For example, a dramatic crescendo across a 4/4 measure could make beat 4 louder decibel-wise than beat 1, even though one is maintaining the relative stresses in the abstract.
Comment: that's a fair point (about the crescendo) but I am looking studying this concept to effectively organize lyrics.  My goal is not to subvert or contradict the expectations created by the metric/rhythmic stresses, but to match them with the appropriate words.
Comment: ex. weak plus moderately strong:                                                [ / u // u ] plus [ / u // u / u // u ] ; the former pattern is the metric stress notation for a bar in common time and the latter is the rhythmic stress notation for the same.   See beat 4.  What would you argue is the "summative" stress of beat 4 according to the above principles?
Comment: @286642 Just to clarify, is each element of the above rhythmic stress a half-beat? (i.e., the metric [ / ] on beat 1 corresponds to [ / u] in the rhythm?)
Comment: that is correct.  Imagine the former being 4  repeated quarter notes and the latter being 8 repeated eighth notes
Comment: Let us [continue this discussion in chat](https://chat.stackexchange.com/rooms/111942/discussion-between-aaron-and-286642).
Answer: Although I don't exactly disagree with the other answers, my opinion is that the question is looking for a perfect mathematical system where there likely is none.  I don't think the author of this book was intending an exact method with precise "rules" for "summing" the stress patterns.
Rather, as this is a book on songwriting, my guess is that it's just an ad hoc method for collapsing many levels of potential rhythmic/metric stress into the 2-3 levels of stress needed for text-setting.  I'm assuming the author chooses three levels, with the intermediate stress level able to take either a stressed or unstressed syllable, depending on the text.
My reason for saying this is because I think before worrying about the mathematical system to create summative patterns, one first would need a rigorous mathematical way of defining where the rhythmic stress indications come from.  And from the examples given, I'd say that's rather ad hoc, based on the local amount of rhythmic activity rather than the global "weight" of various beats or stresses.  In the examples given in the question, one could identify 4-5 levels of stress, but that isn't helpful as a guide for text-setting.  Instead, for text-setting, one needs to identify patterns of strong and weak syllables that roughly alternate.  (It's rare to have more than a few strong syllables in a row or a few weak syllables all in a row.)
Unless there are more clarifying examples in the book, the summative just appears to be a rather loose way of combining both the global (metric) stresses with local (rhythmic) ones that are more dependent on the specific rhythm.  Or, to state the principles in words, I think the author is basically saying, "Match stressed and unstressed syllables to the local rhythmic patterns, but don't forget to also give particular weight to the stresses created by the overall meter."
However, all of this is a tentative interpretation based on only the limited excerpt given in the question and some of the comments -- I haven't had a chance to look at the actual book for clarification.

Rhythmic/Metric Stress Patterns


(Many-valued logic) Disprove that A, not (A and not B) ⊨ (FDE) B by a Routley counter model.

Question: I have some problems disproving A,¬(A ∧ ¬B) ⊨ B in the logic of first degree entailment(FDE) by a Routley counter model. To do that, I firstly need
$$M_{FDE}⊨ A[w]$$
I also need
$$M_{FDE}⊨ ¬(A ∧ ¬B)[w]$$
That means $$M_{FDE}⊯(A ∧ ¬B)[w^*]$$ (The symbol ⊯ menas ⊨ slash I just do now know how to fix that)
That means either $$M_{FDE}⊯A[w^*]$$ or $$M_{FDE}⊯¬B[w^*]$$
However, if $$M_{FDE}⊯¬B[w^*]$$
then
$$M_{FDE}⊨ B[w]$$ (This cannot happen because we need to disprove the statement).
The other case is $$M_{FDE}⊯A[w^*]$$
That means $$M_{FDE}⊨ ¬A[w]$$. But we have $$M_{FDE}⊨ A[w]$$ in the first place.
My question is Can that happen?
Namely, can I have  $$M_{FDE}⊨ ¬A[w]$$ and $$M_{FDE}⊨ A[w]$$ at the same time?
In other words, can I have $$M_{FDE}={W,*,I}$$ where W={w,w*}, I(A)={w}, I(¬A)={w} as my counter Routley model to disprove the statement?
Answer:
May we have $M \vDash_{FDE} ¬A[w]$ and $M \vDash_{FDE} A[w]$ at the same time?

I think not; the rule for $\lnot$ in a Routley interpretation is :

$ν_w(¬A) = 1$ if $ν_{w^∗} (A) = 0$, otherwise it is $0$.

i.e. :

each world, $w$, comes with a mate, $w^∗$, its star world, such that $¬A$ is true at $w$ if $A$ is false, not at $w$, but at $w^∗$. [...] Note that $ν_{w^∗} (¬A) = 1$ iff $ν_{w^{**}} (A) = 0$ iff $ν_w (A) = 0$. In other words, given a pair of worlds, $w$ and $w^∗$, each of $A$ and $¬A$ is true exactly once.

I think that the sought counter-model is :

$w = \{ A \}$ and $w^* = \{ ¬B \}$.

Thus : $M \vDash A[w]$ and applying the rule : $ν_{w^∗} (¬A) = 1$ iff $ν_{w^{**}} (A) = 0$ iff $ν_w (A) = 0$ to the second one, we have : $M \nvDash B[w]$.
But : $M \nvDash A[w^*]$, and thus : $M \nvDash (A ∧ ¬B)[w^*]$, and thus : $M \vDash ¬(A ∧ ¬B)[w]$.
Conclusion : we have :

$M \vDash A[w]$, $M \vDash ¬(A ∧ ¬B)[w]$ and $M \nvDash B[w]$

that means :

$A, ¬(A ∧ ¬B) \nvDash_{FDE} B$.


Why is this solution in codewars Multiplication table for number not working?

Question: The task is:
Your goal is to return multiplication table for number that is always an integer from 1 to 10.
For example, a multiplication table (string) for number == 5 looks like below:
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50

My code:
def multi_table(number):
    for num in range(1,11):
        print (str(num) + " * " + str(number) + " = " + str(num * number))

user = int(input("Your number: "))
print(multi_table(user))

Here is what comes up:
1 * 5 = 5
2 * 5 = 10
3 * 5 = 15
4 * 5 = 20
5 * 5 = 25
6 * 5 = 30
7 * 5 = 35
8 * 5 = 40
9 * 5 = 45
10 * 5 = 50
None should equal '1 * 5 = 5\n2 * 5 = 10\n3 * 5 = 15\n4 * 5 = 20\n5 * 5 = 25\n6 * 5 = 30\n7 * 5 = 35\n8 * 5 = 40\n9 * 5 = 45\n10 * 5 = 50'

Comment: can u please explain what is ur error?
Answer: So the online judge seems to be checking for what you return and not what you print. So why don't you try replacing the print statement with return statement in your function definition and see if it works.
def multi_table(number):
    out = ""
    for num in range(1,11):
        out += (str(num) + " * " + str(number) + " = " + str(num * number))
    return out

Comment: Thank you for the answer, but it only returns the first line, because the "return" exits the for loop, so 2-9 will not be returned.
Comment: def multi_table(number):
    out = ""
    for num in range(1,11):
        out += (str(num) + " * " + str(number) + " = " + str(num * number))
    return out
Answer: I tried to solve this and it work somehow. I tweak @Anand Narayanan original code and below is the new code:
def multi_table(number):
    out = ""
    for num in range(1,11):
        out += (str(num) + " * " + str(number) + " = " + str(num * number ) + "\n")
    return out.strip()

I only added the "\n" and strip method on the out string.


differential equation of concentric circle and its rotate direction

Question: How can I define the direction of the normal form of concentric circle? $x(t)=C_1\left(
    \begin{array}{ccc}
       \cos\beta t\\
      -\sin\beta t\\
    \end{array}
  \right)
+C_2\left(
    \begin{array}{ccc}
       \sin\beta t\\
      \cos\beta t\\
    \end{array}
  \right)$
I have no idea how to define the circle direction of concentric circle
Comment: What do you mean by "the direction"?
Comment: @WalterJ The direction of rotation
Answer: I am not fully sure, but maybe this helps.
You have $x(t) = \begin{pmatrix} \cos(\beta t) & \sin(\beta t)\\ -\sin(\beta t) & \cos(\beta t) \end{pmatrix} x_0$, which is expressing a clockwise rotation for the usual $(\beta, t)$ (changed the $C$'s to $x_0$). Then by differentiation find that $\dot{x}(t) = \begin{pmatrix} -\sin(\beta t) & \cos(\beta t)\\ -\cos(\beta t) & -\sin(\beta t) \end{pmatrix} \beta x_0$ such that for example when given $x_0=(1,0)$ the corresponding derivative (direction) becomes $\dot{x}(t)|_{t=0}=(0,-\beta)$. Which, if you draw a radius $1$ circle with $x_0$ on it, is what you should expect.
Comment: you mean $\dot{x}(t) = \begin{pmatrix} -\sin(\beta t) & \cos(\beta t)\\ -\cos(\beta t) & -\sin(\beta t) \end{pmatrix} \beta x_0$ ?
Comment: Yes, typo. fixed it.
Comment: Thank you very much WalterJ.  It helped to understand.
Comment: If stuck with these kind of questions, drawing might do wonders!


Conditional entropy of sum of random variables

Question: How can be proven that for random variables $A$ and $B$, and $C = A + B$,
$$H(C\mid A) = H(B\mid A).$$
Also, would it be possible to determine if $H(C)$ would be greater than $H(A)$?
Comment: It is a homework, isn't it? Just think about what happens when $A=a$. What is the difference between $C|A=a$ and $B|A=a$. Anyway, looking at http://en.wikipedia.org/wiki/Convolution may help (if two probabilities have densities, the density of their sum is just a convolution of their densities).
Comment: any ideas will be appreciated.
Comment: Differential or Shannon entropy? The sum of absolutely continuous variables need not even be absolutely continuous. Given the poor behavior of differential entropy under sums, I would doubt there is such a relationship for differential entropy.
Comment: Some observations. The problem seems equivalent to showing $H(A,A+B)=H(A,B)$. Also, is there anything special about addition here? Would the relationship $H(A,AB)=H(A,B)$ also hold under suitable conditions?
Answer: This is an elementary exercise, when the random variables take only a numerable number of values: $A(\omega)\in \mathcal{A}$, $B(\omega)\in \mathcal{B}$ and $C(\omega)\in \mathcal{C}$. In that case:
$$H(C|A) = -\sum_{a\in\mathcal{A}}\sum_{c\in\mathcal{C}} p_A(a)p_{C|A}(c|a)\ln p_{C|A}(c|a), \tag{*}\label 1$$
Since $C = A + B$, we have that:
$$p_{C|A}(c|a) = p_{B|A}(c-a|a)$$
And substituting this into $\eqref 1$, the conditional entropy is:
$$H(C|A) = -\sum_{a\in\mathcal{A}}\sum_{c\in\mathcal{C}} p_A(a)p_{B|A}(c-a|a)\ln p_{B|A}(c-a|a), \tag{**}\label 2$$
restructuring the second sum over values of $b = c-a$, we have:
$$H(C|A) = -\sum_{a\in\mathcal{A}} p_A(a) \sum_{b\in\mathcal{B}} p_{B|A}(b|a)\ln p_{B|A}(b|a) = -\sum_{a\in\mathcal{A}} p_A(a) H(B|a) = H(B|A).$$


Ruby on Rails API that functions similar to WCF does on ASP.NET

Question: Is there an API similar to WCF for Ruby on Rails?
WCF has a lot of advantages and is a very easy method to have a service running and clients using it.
I want to know if there is an API similar to WCF for the Ruby on Rails platform
Answer: Ruby on Rails uses RESTful web services by default, and you can easily generate routes and controller actions for these "out of the box".  Like WCF, Rails supports both JSON and XML formats for data transfer.
If you need to support SOAP, there are Rails ad-ons, such as savon.
ian.


Opinions on a statistical error in p values (re: standard deviations, t-test/Kruskal-Wallis) in a published study?

Question: A large amount of computing resources is required to compute exact probabilities for the Kruskal–Wallis test. Existing software only provides exact probabilities for sample sizes less than about 30 participants. These software programs rely on asymptotic approximation for larger sample sizes.
Exact probability values for larger sample sizes are available. Spurrier (2003) published exact probability tables for samples as large as 45 participants.[7] Meyer and Seaman (2006) produced exact probability distributions for samples as large as 105 participants.[8]
Comment: Please don't make more work for other people by vandalizing your posts. By posting on the Stack Exchange (SE) network, you've granted a non-revocable right, under the [CC BY-SA 4.0 license](https://creativecommons.org/licenses/by-sa/4.0) for SE to distribute that content. By SE policy, any vandalism will be reverted. If you want to know more about deleting a post, consider taking a look at: [How does deleting work](https://meta.stackexchange.com/q/5221/295232)?
Comment: What is the question?
Answer: Perusing the paper. After beating my way through a jungle of ads (and an offer to subscribe to an ad blocker)
I found summary data in your link for the Neutrophil/lymphocyte ratio, expressed as
$2.136 \pm 1.31$ for 107 subjects in the tinnitus group and $2.061 \pm 1.43$ in an independent control group. From the body of the paper, it seems that the numbers
following $\pm$ are sample standard deviations, not 95% margins of error. ["For the descriptive data, the normally distributed values are expressed as mean ± standard deviation."]
The P-value 0.002 for this comparison is given in Table 1. From the description, it seems that this P-value is for a one-sided 2-sample t test, but it is not
clear whether it was a pooled or Welch 2-sample t test.
Failure to confirm significance. In Minitab software, it is possible to run t tests from summary statistics, in the absence of raw data.
Welch t test: Minitab output for the Welch test of $H:\mu_t = \mu_c$ against $H:\mu_t \ne \mu_c$ (not assuming equal population variances) is shown below. The P-value is 0.345, nowhere near significant.
    Two-Sample T-Test and CI
<code>Sample    N  Mean  StDev  SE Mean
1       107  2.14   1.31     0.13
2       107  2.06   1.43     0.14

Difference = μ (1) - μ (2)
Estimate for difference:  0.075
95% lower bound for difference:  -0.235
T-Test of difference = 0 (vs >):
   T-Value = 0.40  P-Value = 0.345  DF = 210
</code>
Pooled t test: With a balanced design (107 subjects in each group), the T-statistic for a pooled test
would be the same as for the Welch test, and with DF = 212, also not significant.
One-sided tests: Testing $H:\mu_t \le \mu_c$ against $H:\mu_t > \mu_c$ would give P-values half as large as above for Welch and pooled tests. Neither significant.
Doubts about normality assumption. The only substantial clue to the actual data is in the boxplots of Fig. 1. To my eye from these plots, significance of a 2-sample t test seems unlikely. Also, notwithstanding claims of having used Shapiro-Wilk tests to confirm that data are normal, the boxplots show heavy right skewness and many high boxplot 'outliers'. (Perhaps "Neutrophil' and 'lymphocytes', taken separately are normal, but certainly not their ratio. Ratios are often nonnormal.)
Furthermore, the degree of skewness may be too much for the legendary robustness of the t test against nonnormality to accommodate---even for samples as large as 107.
From here? So I am puzzled about the evidence for the most basic claim of the paper.
It may be that some of the other (dozen or so) measurements mentioned in Table 1 do show significant differences between tinnitus and control groups, as you suspect.
You might try to replicate P-values for t tests for some of them. (Knowing sample sizes, sample means, and sample standard deviations is enough to use standard formulas
for T statistics and methods for deciding whether to reject, if you don't have suitable software at hand.)
Comment: Depends on version of MTB. Use menus: `STAT > Basic > 2-sample t > Summary data >` fill in blanks.
Comment: Thanks for comment: Not _much_ different. I suspect differences due to rounding errors. Minitab often rounds too much for my taste in its ourput. They claim to keep 14 places or so behind the scenes.


Find $z$ s.t. $\frac{1+z}{z-1}$ is real

Question: I must find all $z$ s.t. $\dfrac{1+z}{z-1}$ is real.
So, $\dfrac{1+z}{1-z}$ is real when the Imaginary part is $0$.
I simplified the fraction to $$-1 - \dfrac{2}{a+ib-1}$$
but for what $a,b$ is the RHS $0$?
Answer: The idea is good:
$$
\frac{1+z}{z-1}=\frac{z-1+2}{z-1}=1+\frac{2}{z-1}
$$
Since $1$ and $2$ are real, you need that $z-1$ is real, which means…
Another way: write
$$
w=\frac{1+z}{z-1}
$$
Then $zw-w=1+z$ and so $z(w-1)=w+1$, which means
$$
z=\frac{w+1}{w-1}
$$
If $w$ is real, then also $z$ is real. And conversely.
Comment: For $z-1$ to be real this means that $Im(z-1)= 0$?
Comment: @Telon Since $1$ is real, also $z$ must be.
Answer: Try writing
$$\frac{2}{a+ib-1}=\frac{2}{a+ib-1}\cdot \frac{a - ib - 1}{a - ib - 1}$$
After you perform the multiplication, it should be pretty clear what values of $a,b$ make this fraction real...
Answer: Hint:
$$\begin{align}\frac{1+z}{z-1} \,\,\, \text{is real} \  &\iff \frac{1+z}{z-1} = \overline{\frac{1+z}{z-1}} = \frac{\overline{1+z}}{\overline {z-1}} \\&\iff (1+z)(\overline {z}-1) = (1+\overline {z})(z-1)\\&\iff \mathfrak {Im} (z) = 0\end{align}$$
Answer: Clearly, real $z$ (except $z=1$) make the expression real, and the map $z\mapsto\frac{1+z}{z-1}$ is a bijection from $\mathbb{C}\cup\{\infty\}$ to itself (with $1\leftrightarrow\infty$). The inverse map is the very same map.
Also, it maps $\mathbb{R}\cup\{\infty\}$ to itself. It follows that no other complex $z$ beyond real $z$ will give real values of the expression.
Answer: If $z+1 = r(z-1)$, then the complex numbers $0, z-1, z+1$ lie on a line.  But the line through $z-1$ and $z+1$ can only contain $0$ if $z$ is real.

Find $z$ s.t. $\frac{1+z}{z-1}$ is real


Spark 2.2: Load org.apache.spark.ml.feature.LabeledPoint from file

Question: The following line of code loads the (soon to be deprecated) mllib.regression.LabeledPoint from file to an RDD[LabeledPoint]:
MLUtils.loadLibSVMFile(spark.sparkContext, s"$path${File.separator}${fileName}_data_sparse").repartition(defaultPartitionSize)

I'm unable to find the equivalent function for ml.feature.LabeledPoint, which is not yet heavily used in the Spark documentation examples.
Can someone point me to the relevant function?
Answer: With the ml package you won't need to put the data into a LabeledPoint since you can specify which columns to use for labels/features in all transformations/algorithms. For example:
val gbt = new GBTClassifier()
  .setLabelCol("label")
  .setFeaturesCol("features")

To load the LibSVM file as a dataframe, simply do:
val df = spark.read.format("libsvm").load(s"$path${File.separator}${fileName}_data_sparse")

Which will return a dataframe with two columns:

The loaded DataFrame has two columns: label containing labels stored as doubles and features containing feature vectors stored as Vectors.

See the documentation for more information.


What test can be applied to Fixed Effect, Random Effect and Pooled OLS models?

Question: I am trying to see what model performs best. I am considering the Hausman test to see which model is the most stable out of the three. However, I am not sure what exactly my null hypothesis should be when I test three models, and if I should compare RE against Pooled OLS and then RE against FE.
Do you think that this is the correct way of testing?
Also, is there another test or statistic that can be used in my case?
Answer: If the time-invariant variation is relevant to your identification then RE of FE is needed.
You can compare OLS to FE with something like an F test of significance of the extra dummies.
The Hausman test compares a consistent estimator to an efficient one. In this context, fixed effects would be consistent (as it controls for the time-invariant OVB) while random effects is relatively efficient.
However be careful with the Hausman as it won't tell you whether the time invariant variation is orthogonal to your independent variables. That's more of a judgement call.


Find unit normal vector given speed-, acceleration- and jerk vectors, Calculus III

Question: So we have been given the following:
$$\frac{dr}{dt} = (-3,2,0)$$
$$\frac{d^2r}{dt^2} = (0,3,-3)$$
$$\frac{d^3r}{dt^3} = (0,0,1)$$
With the information above, I have found the unit tangent vector by doing: $T = \frac{\frac{dr}{dt}}{|\frac{dr}{dt}|} = \frac{(-3,2,0)}{\sqrt{3^2+2^2+0^2}} = (\frac{-3}{\sqrt{13}},\frac{2}{\sqrt{13}},0)$
I have found the curve $k = \frac{|\frac{dr}{dt} \times \frac{d^2r}{dt^2}|}{|\frac{dr}{dt}|^3} = \frac{3\cdot \sqrt{22}}{13\cdot \sqrt{13}}$
Lastly i have found the torsion to be $\tau =\frac{(\frac{dr}{dt} \times \frac{dr^2}{dt^2})\cdot\frac{dr^3}{dt^3}}{|\frac{dr}{dt} \times \frac{dr^2}{dt^2}|^2} = -\frac{1}{22}$
Now i  am having difficulties with finding the unit normal vector, N.
Answer: Since the acceleration is $a=(0,3,-3)$ and the unit tangent vector is $T=\left(-\frac3{\sqrt{13}},\frac2{\sqrt{13}},0\right)$, the normal acceleration is$$a_N=a-\langle a,T\rangle T=\frac1{13}(18,27,39).$$Since $N$ is a unit vector such that $a_N$ is a multiple of $N$, $N=\pm\frac1{\sqrt{286}}(6,9,-13)$. Which one? That can be determined using the fact that$$\langle a,N\rangle=\frac{\|v\times a\|}{\|v\|}\geqslant0.$$Since $\left\langle(0,3,-3),(6,9,-13)\right\rangle>0$,$$N=\frac1{\sqrt{286}}(6,9,-13).$$


Sample size and a study design: AI in shadow mode vs physicians

Question: My research group has a group of patients where we are comparing the performance of a Random Forest classifier for acute respiratory tract infections. We are designing a study where we compare the output of the classifier to a physician. The patients first answer questions before meeting with the physicians and we get the rest of the data needed from the text note from the consultation. The outcomes are the rate of antibiotic prescriptions and chest x ray orders (and if they are positive vs negative).
My questions are on sample size and on which kind of study would fit best. With an alpha of 0.05 and a power of 0.8, how would one calculate a sample size? I am guessing a type of observational study design would fit best to be able to reject the null hypotheses presented regarding the performance of the classifier vs the physicians.
I already found a reference for how to calculate the sample size if we were to observe two groups with different risk factors and measuring the outcome (http://njppp.com/fulltext/28-1567942207.pdf) but wouldn't it make more sense to have a single group and have the classifier work in "shadow mode"?
EDIT: We have done a retrospective analysis which shows that antibiotic prescriptions are prescribed in 25% of cases which can be reduced to mean 18% and the chest x-rays are at 12% and can be reduced to mean 9% (without missing a positive x-ray).
EDIT: The ground truth/gold standard for the antibiotics will be a panel of 5 physicians which will go through each case in the validation set. They will estimate if there was a need for antibiotics or not (by a majority vote). With each patient we have a binary value indicating if an antibiotic was prescribed or not. The ground truth for the chest x rays are if the x-ray is positive for pneumonia specific changes as per description of the radiologist.
EDIT: The study concludes by comparing the rates of chest x-rays orders and antibiotic prescriptions recommended by the classifier vs the physician who originally saw the patient. These are binary values. We also look at the results of the chest x-rays to determine if the image was positive for pneumonia or not which is also a binary value.
Comment: Please say more about the "ground truth" in this situation and how you estimate it. That's what you're using to evaluate both the random forest and the clinician results.  It's obviously desirable to minimize unnecessary antibiotics or imaging, but there's a tradeoff in making false-negative decisions, also.  So the nature and quality of the "ground truth" values is critical here. Please provide that information by editing the question, as comments are easy to overlook and can be deleted.
Comment: Can you give concrete examples of your outcomes? What would the data look like at the end of the study?
Comment: Thanks for the feedback, I have edited the question.
Comment: Thanks for the feedback. I added a bit more information, if it doesn't answer your question, could you be more specific?
Answer: Power analysis to determine sample size depends on having estimates of the variability in what you're trying to measure and the magnitude of any differences that you're trying to find (often called "effect size"). Generic methods like those in your linked resource or implemented in software like G*Power take such estimates together with your desired significance levels and power to find the sample size for particular types of design. Yes, as you are evaluating paired comparisons instead of separate groups you would use corresponding paired instead of independent-group values for variability and effect size.
You seem, however, to have a fair amount of preliminary data on this. I thus suggest that you do your power calculation by simulating a large set of new values based on your preliminary data, trying your intended analysis on those simulated data, and seeing what sample size is needed based on your simulations of your particular situation.
That might seem daunting at first, but it's pretty straightforward. You already have information on test results and clinical annotations on many patients, the associated probabilities that clinicians versus the random forest (RF) would "prescribe" antibiotics or imaging, and the corresponding true findings of pneumonia. As those tend to be all-or-none decisions or results, each of those could be modeled with logistic regressions based on the data that you have. Next, you use those logistic-regression models to "predict" pneumonia and clinician versus RF-based recommendations for antibiotics and imaging for a very large number of "new patients" based on the distributions of clinical data among the patients in your population of interest. That gives you a set of simulated data. Then you perform your intended analyses on the simulated data, evaluating the sample size needed to detect the differences of interest.
This approach has a few advantages. First, instead of depending on crude generic estimates of variability and effect sizes you use estimates based on the data most directly related to your study: the data you already have. Second, it forces you to think carefully in advance about the statistical tests that you want to perform on your data. A crude power calculation with a generic tool might not end up working well for the specific test that you really want to perform. Third, through the simulation and analysis steps it forces you to think carefully about the nature of your data and the (potentially hidden) assumptions that you are making.
As you do this, however, I would recommend that you focus on probabilities instead of all-or-none classifications as much as possible. Although a clinician's decisions to prescribe antibiotics or order imaging are all-or-none decisions, those decisions are based on the clinician (perhaps unconsciously, or based on years of training and experience) putting together an estimate of the probability of a patient's having pneumonia with the costs and benefits of the drug or the imaging. Your RF presumably returns a probability of needing antibiotics or imaging; the probability cutoff that you choose for the RF implicitly represents those cost/benefit tradeoffs. Even your "ground truth" for antibiotics doesn't need to be all-or-none. If only 3 out of 5 clinicians think that antibiotics should have been prescribed in a case, maybe you should represent that as a 60% probability of needing antibiotics instead of a fixed "antibiotics-YES" decision.
Implementing the sampling from the data
As noted in a comment, you can't just sample from the marginal distributions of the patient characteristics separately, as they are highly correlated. One way to approach this problem would be to model those correlations directly and sample from the correlation model. A second would be to stick with the set of patients for which you already have clinical data but sample from their probability distributions of having pneumonia and needing antibiotics or imaging.
Modeling correlations among variables is often done with copulas. Copulas provide the link between the marginal distributions of individual predictors and their joint distributions. Recent work has shown how to extend methods originally focused on continuous joint distributions and parametric copula forms to mixed discrete and continuous distributions and to non-parametric copulas that might be needed for your data set.
A second approach, which might be simpler to implement, would be to extend your current data set to incorporate the variability in predictions from your model. For example, build a binomial model of pneumonia yes/no based on your current (pre-diagnosis) clinical data set. For each case in your data set, get the modeled probability of pneumonia. Make 100 copies of each patient's clinical data. Then label those copies as pneumonia yes/no in proportion to the modeled probability. For example, if the probability of pneumonia for a certain patient is 63%, label 63 of the patient's 100 data copies as pneumonia-yes and 37 as pneumonia-no.
That will give you an extended data set 100 times the scale of your current data set that incorporates your uncertainty in modeling pneumonia. Then you sample with replacement from that extended data set to evaluate the sample size needed for your study.
It sounds like you might be modeling all 3 outcomes of pneumonia, need for antibiotics, and need for imaging. If that's the case it might be best to model those all together in a multivariate binomial model that takes the inherent correlations among those outcomes into account. Then you would construct your extended data set in a way that incorporates those correlations among those outcomes. But the general principle would be the same.
The major point is that working with simulated data based on your current information is very fast, inexpensive, and flexible--particularly compared to what you would face if you rushed into an underpowered prospective trial based on a crude power estimate.
Comment: A quick update. We've been experimenting with dataset augmentation by simulation with mixed results. Complicating the problem is choosing the right statistical test for the study. We've been looking at the McNemar and the binomial test with the latter one seeming to be a better fit.

We are using the RF score to predict for the need for AB and X-ray. We use a cutoff on the pneumonia prediction (e.g. all cases less than 0.1) for negative prediction for AB / CXR. In a sample of 500 people, we expect physicians to order 45 negative CXRs and RF around 35. Binomial test would fit well, no?
Comment: One even wonders if a statistical test is needed if you are running a simulated prediction on a single population?
Comment: @st0ne remember that you are running the prediction on an extended simulated population for power analysis, to try to estimate the sample size needed for your ultimate study. You do it on multiple simulated samples of specified sizes to see what sample size lets you detect the desired difference with a specified power. I prefer working in binomial (probability) scales as long as possible, avoiding cutoffs if possible. [This answer](https://stats.stackexchange.com/a/502004/28500) shows the cost/benefit tradeoffs implicit in choosing a probability cutoff.
Comment: Thank you for the feedback. I presume you are suggesting something similar as is described in the later part of [this](https://data.library.virginia.edu/simulating-a-logistic-regression-model/) article?
Comment: @st0ne yes, that's the idea. The closer you base your simulation on the type of data and patient population that you have, the better your estimate is likely to be. For example, that web page seems to have sampled from a uniform distribution for `age` and equal male/female probability for its simulations. You want to sample from your _actual_ distributions of ages, genders, clinical characteristics, pneumonia incidence, etc.
Comment: that makes sense. We would have to find a way to sample our data in a way so that the simulated data captures the correct probability distributions we are sampling our simulation data from. Using age as an example, if we are to simulate an 80 year old vs a 25 year old patient, the probability distribution for the same predictor would be quite different. I don't presume you have any good suggestions or links on how to execute this kind of sampling in python? Group by age and sex perhaps?
Comment: This approach is intriguing but a bit tricky to implement with the data we have collected. We tried sampling from age buckets (5 years apart) but the problem is that it's hard to sample in such a way that symptoms / clinical features correlate. E.g. if a patient has COPD, it is more likely he has low oxygen saturation. Our data has countless of these correlated examples. The logistic regression model therefore predicts that no images should be taken on the simulated patients. We would have to iterate over each feature and create a unique distribution for each. All suggestions appreciated
Comment: @st0ne you've already found one advantage of trying to do this: you found that you can't assume independence among the patient characteristics so you have to take the correlations among those characteristics into account. That also has implications for interpreting "predictor importance" and such in your models. I'll expand the answer in a day or so to suggest approaches based on copulas (to model the correlation structure) or, maybe more simply, working with your current set of patients but incorporating what you have already modeled with respect to probabilities of having pneumonia etc.


Fast method to rotate image in .NET Compact Framework in C#

Question: Is there a fast way to rotate images in .NET Compact Framework.
I've found ways to do 90, 180,270, rotations but would like to be able to rotate images free form in similar speed.
Comment: What language are you using?  C#, C++, VB.Net?
Comment: Good question, my oversight: C#
Answer: 90-, 180- or 270-degrees rotations require no calculation at all, only moving some data. On the other hand, free-angle rotations require heavy calculations. So this will never ba as fast as right-angle rotations...
Comment: That's not necessarily true: for example if the results of the rotation can be precalculated and cached, then a rotate might just be a bitblt of the precacluated rotation from cache; or, for example, if writing to video takes more time than anything, then doing the caculation in RAM (as opposed to using the video APIs) and then bitblitting might be only imperceptably slower.
Answer: You can use OpenNetCF's ImageUtil class to do this (it's the "Rotate" method you're looking for). The method takes the bitmap and a (float) angle as parameters. Find the specs here:
http://www.opennetcf.com/library/sdf/html/eb32a9ba-c1dc-7194-bd31-a5d035790a45.htm
Comment: You almost had me. See same link. It's tough.

"Amount of degrees to rotate image by. Must be 90, 180, or 270"


Chamar a função em PHP várias vezes ou armazenar a função em variável?

Question: Eu estava aqui criando alguns arquivos de loop quando eu me deparei com a seguinte dúvida:
Ao invés de eu chamar uma função que retorna o dado todas as vezes
Ex:
<?php
if (pegar_dado('nome_dado') != '') {
    echo pegar_dado('nome_dado');
} ?>

Não seria mais prático e talvez melhor e mais leve armazenar a variável e depois fazer o resto?
<?php
$pega1 = pegar_dado('nome_dado');
if ($pega1 != '') {
 echo $pega1;
}

O que eu quero saber é, com a experiência de vocês, qual dos dois métodos é melhor? Mais leve? Existe diferença no uso de memória nos dois casos?
E qual dos dois métodos vocês recomendam utilizar?
Comment: Se a função faz uma consulta ao banco, sim, será feito a consulta toda vez que chamar a função. Chamando apenas uma vez será apenas uma consulta feita.
Comment: Apenas uma vez. O resultado será armazenado em memória e reutilizado.
Comment: Depende do caso. Se a função for "pegarIdSequencial" (e fizer o que o nome diz) e vc armazenar o resultado, não vai ser um ID sequencial. Qual usar? Depende do resultado pretendido. Em geral, se quer sempre a situação atual tem que chamar a função sempre, se quer o valor original, mesmo que algo externo mude o resultado da função, armazene.
Comment: Certamente a 2ª opção, imagine ter que executar uma função 1000 vezes... pode ser pouca a alteração no processo, mas vai ocorrer, sendo que, uma vez executada e salva numa variável, não há necessidade de execução novamente.
Comment: Sabe me dizer se essa função "pegar dado" será executada TODAS as vezes que eu chamar essa função? digo, será que ela vai consultar meu banco de dados todas as vezes que eu chamar ela? e se eu armazenar em uma variável, será que o banco de dados será consultado apenas uma vez?
Comment: @AndersonCarlosWoss Se a função for chamada no arquivo várias vezes ela faz a consulta, ok, mas se a função for armazenada em variável e a variável for chamada varias vezes, a consulta será feita 1 ou várias vezes?
Answer: A segunda maneira não é aconselhável.
Existem estratégias de cacheamento para casos que envolvam IO. Mas a função sempre deve ser chamada.
De forma geral, execução de uma função é mais custosa que acessar uma região de memória com um valor já atribuído. Mas se isso não envolver IO, não fará muita diferença na execução do seu programa.
Contudo, você perde toda a flexibilidade do seu sistema. Imagine que no futuro você precise tomar uma decisão de buscar esse dado de formas diferentes durante a execução do programa. Ou que você precise desativar o cachê desse recurso no futuro. Utilizando a segunda abordagem, você teria que substituir em todos os lugares para remover esse cachê.
Enquanto que se você deixa a responsabilidade de fazer cachê dentro da função, você alteraria apenas em um lugar.
Exemplo de cachê dentro da função usando OO:
class interfaceAcessoDado {

  private static $dadoCacheado;

  public function pegaDado()
  {
       if( self::$dataCache === null )
           self::$dadoCacheado = $this->pegaDadoDoBD();
       return self::$dadoCacheado;
  }

  private function pegaDadoDoBD()
  {
       // Busca esse dado
       return $dado;
   }
}

Comment: Não consegui perceber a diferença/vantagem de se armazenar o valor "em cache" em uma variável dentro da classe com armazenar na variável como perguntado que você disse não ser aconselhável. Poderia detalhar melhor?
Comment: Vou apontar duas vantagens. A primeira é que quem está consumindo esse valor não precisa saber como ele está sendo recuperado. Ou seja, se você precisar usar essa função de diversos lugares, não precisa ficar repetindo código de verificação se a variável existe senão busca do BD. É sempre a mesma função que retorna o valor que você quer, independente de como isso seja feito. Fazendo isso, é como se você construísse uma interface para acesso ao dado.
Comment: @AndersonCarlosWoss a segunda vantagem é que se tiver dois lugares totalmente distintos que estão acessando o mesmo dado, o primeiro realiza a consulta e guardar essa info na variável estática. Se um segundo local do código precisar acessar o mesmo dado, ele já irá consumir o valor dessa variável. Mas tudo sem ter que ficar pensando se já buscou esse dado ou não. Essa variável estática existe em tempo de execução de uma processo. Mas se no futuro vc quiser algo mais sofisticado, pode deixar em um redis por exemplo e todos os pontos do consomem essa função nem "saberão" de onde tá vindo o dado.


How to enforce a specific aspect ratio for all Media images?

Question: With the core media Image type, I want to restrict uploaded images by aspect ratio.
I want to block images that have the wrong aspect ratio from being uploaded.  My question is the same as this Drupal 7 question, but for Drupal 8+.
Answer: This helpful blog post gave me an answer:
<code>/**
 * Implements hook_form_alter().
 */
function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  switch ($form_id) {
    case 'media_image_add_form':
    case 'media_image_edit_form':
      $form['field_media_image']['widget'][0]['#upload_validators']['_validate_image_aspect_ratio'] = [];
      break;
  }
}

/**
 * Ensures that images have a square aspect ratio.
 *
 * @param \Drupal\file\FileInterface $file
 *   File object.
 *
 * @return array
 *   Errors array.
 */
function _validate_image_aspect_ratio(FileInterface $file) {
  $errors = [];
  $image = \Drupal::service('image.factory')->get($file->getFileUri());
  if ($image->isValid()) {
    // Add your condition validations here!
    $width = $image->getWidth();
    $height = $image->getHeight();
    $aspect_ratio = $width / $height;
    if ($aspect_ratio != 1 / 1) {
      $errors[] = t("The image should be a square!");
    }
  }
  return $errors;
}
</code>


Ubuntu Create FTP account automatically upon user creation

Question: I will note that I am using Rails but this is not a Rails issue.
I'm not very experienced with Linux but I do know I'm running Ubuntu 10.04 LTS on Linode.
Upon user registration the user is created a folder in my RAILS_ROOT/public/users/[user_id]/
for their own personal use. Inside this folder is an albums directory.
How can I, at the same time this folder is created, password encode it and give them ftp access. Is this actually possible? What are the security concerns with something like this. I want to lock them in there and I have seen some people talk about vsftpd as a solution to this last part.
Answer: If you installed and configured ProFTPD then you can use the AuthUserFile directive to point to an alternate password file. This file can be managed by your application. You can also configure ProFTPD to use an sql database backend which your application could manage.
Comment: Thanks for this response lain. I am looking at ProFTPD now too. Appreciate it!
Answer: Are these real Linux accounts, or are they accounts created in your application?
vsftpd can work with either real accounts or virtual users (read from a file), as well as "chroot" their accounts to a specific directory so that they can't go up directories from there, but either way requires a certain amount of configuration (by default it appears to allow anonymous access only).  Regardless of whether you are using real or virtual users you'll need to enable "Local" users.  vsftpd uses PAM for all Local users, so for virtual users you'll need to completely erase and replace the /etc/pam.d/vsftpd with the information from the tutorial here.  If they're users created in the application, then your application will need to have write access to the virtual passwd file pointed to in the PAM configuration above, and each new user has their username and an appropriate hash of their password stored in the file.
If you're good with PAM and have the appropriate packages installed, PAM has any number of different modules that can be used for this, there may be one that can read the same database table your application uses to perform the username/password lookup.
Comment: thanks for the in-depth answer derf. The accounts that I am talking about having created are in the application itself. I will look into what you mentioned. Thanks!


ADF Tumpling Window Trigger Udpating the startTime property behaviour

Question: I will get straight to the point - I cannot update the startTime property of an existing ADF Tumpling Window Trigger, I get this error always:
{"code":"DeploymentFailed","message":"At least one resource deployment operation failed.
 Please list deployment operations for details. Please see https://aka.ms/DeployOperations for usage
details.","details":[{"code":"BadRequest","message":"{\r\n \"error\": {\r\n \"code\":
\"TumblingWindowTriggerStartTimeUpdateNotAllowed\",\r\n \"message\": \"Start time cannot be updated for
Tumbling Window Trigger.null\",\r\n \"target\": null,\r\n \"details\": null\r\n }\r\n}"}]}

To get around this issue, I have to re-create the Tumpling Window Trigger, point it to the same pipeline and use my new startTime value there.
The above behaviour is quite inconvienent IMHO, is there any workaround for the above error to apply the update automatically? This is extremely important because even in my CI scenario the current behaviour keeps spitting the above error. For CI the Microsoft guide does not even mention anything on this error.
Switching to Schedule Trigger is not an option because it doesn't allow having a retry policy.
Answer: We suffering because of the same issue and it seems the only way to override the tumbling window trigger start time is to remove the trigger, just before your ARM template with ADF deployment. And yes, you have to also stop trigger before removing. We didn't parameterize it yet, having currently different priorities but I'm pretty sure it's possible.
Stop and delete triggers happens in our deployment pipeline and we do something like this:
az extension add --name datafactory

$jsonTriggers = az datafactory trigger list --factory-name "###" --resource-group "###" --query "[].name"
$triggers = $jsonTriggers | ConvertFrom-Json

foreach($trigger in $triggers)
{
    az datafactory trigger stop --factory-name "###" --resource-group "###" --name "$trigger"
    az datafactory trigger delete --factory-name "###" --resource-group "###" --name "$trigger" --yes
}


Allow only one UIView to be touched

Question: I have three UIViews:

B1 and B2 are subviews of A.
B1 and B2 may have one or more subviews.
I want that only one view can be touched at the same time.
For example:
The user touches B1 (or one of its subviews) with the first finger, and then, without releasing it, touches B2 (or one of its subviews) with the second finger. Now, B1 should receive all events in its bounds, but not B2. Same thing the other way around.
Is this possible to accomplish without subclassing any of these views?
If not, what's the easiest/cleanest way to do it?
Edit:
About exclusiveTouch and multipleTouchEnabled:
These properties are not valid for my problem.
multipleTouchEnabled: Only determines whether or not multi-touch-events are delivered in the same view. So disabling multi-touch on view A (which is the default anyways) will not block view B1 and B2 from receiving touch events at the same time.
exclusiveTouch: Doesn't work either. As far as I have understood, exclusiveTouch only works inside the frame of the view. So setting exclusiveTouch on view B1 and B2 has no effect.
See these two stackoverflows for reference:
Why is UIView exclusiveTouch property not blocking?
Why doesn't UIView.exclusiveTouch work?
Comment: Disable multitouch on the views :)
Answer: Try setting both exclusiveTouch and multipleTouchEnabled to YES for B1 and B2.
Edit: I'm assuming from the wording "B1 should receive all events in its bounds" that you want to receive the touches directly in B1 & B2, not their subviews. If you want to restrict the touches to the B1/B2 bounds but handle them in the subviews I think you need to subclass B1 and B2, or A.
Edit 2: If it's the subviews of B1 and B2 that are interactive, you would need to set these properties to YES on all interactive subviews (recursively). But this also makes all subviews mutually exclusive to touch, not just between B1 and B2.
Comment: See my edit about `exclusiveTouch` and `multipleTouchEnabled`. About your edit: I want the touches to be handled normally. So say there is a tableView as a subview of view B1, I want to be able to interact with it normally. But when I first touch B2, I don't want the user to be able to interact with the tableView in B1.
Comment: @megamer `exclusiveTouch` means that the view gets touches exclusively for the _window_, so these settings would work if B1 and B2 were the views you wanted to receive the touches exclusively. However, since it appears that you want their subviews to receive the touches, it won't work. You could set these properties to `YES` in each subview of B1 and B2 (recursively if they have further subviews), but this would also make touching multiple subviews mutually exclusive.
Comment: edit2: Yes, that would be an option, but it's not what I want - everytime I add a new subview, I'd have to think about this. I'd like to find a solution that is permanent/valid for any subview inside of views B1 and B2.
Answer: When B1 or B2 is touched, set exclusiveTouch to YES for that view and then set it back to NO again when the touches end. This means only the exclusive view will receive touches.
If you want to get touches outside the exclusive view's bounds you can store a reference to the view somewhere when you make it exclusive (myExclusiveView), then override hitTest:withEvent: for view A (or any superview) and have it return [myExclusiveView hitTest:withEvent:].
Answer: I believe this is not possible without subclassing. If anyone can prove me wrong, I'd be happy to see their solution. Anyways, this is how I solved it:
First, I subclass view A, I call the subclass ParentView. ParentView needs pointers to B1 and B2. I also add an additional pointer that is later used to store the currently selected view. Then, I override hitTest:
@interface ParentView : UIView
    @property (nonatomic, strong) UIView* viewB1;
    @property (nonatomic, strong) UIView* viewB2;
    @property (nonatomic, strong) UIView* currentlyTouchedView;
@end

@implementation ParentView

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event {

    if (self.currentlyTouchedView == nil) {
        if (CGRectContainsPoint(self.viewB1.frame, point)) {
            self.currentlyTouchedView = viewB1;
        }else if(CGRectContainsPoint(self.viewB2.frame, point)) {
            self.currentlyTouchedView = viewB2;
        }
    }else {
        if (!CGRectContainsPoint(self.currentlyTouchedView.frame, point)) {
            return nil;
        }
    }

    return [super hitTest:point withEvent:event];
}

@end

So, what this code does is, it stores the view that is first touched. If one is already stored, it returns nil on hitTest, which eats up any additional touch events.
Now, where does currentlyTouchedView get set back to nil? For that, I subclass UIWindow and override sendEvent to check if all touches have ended:
@implementation EventOverrideWindow

- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];
    BOOL allTouchesEnded = YES;
    for (UITouch* touch in event.allTouches) {
        if (touch.phase != UITouchPhaseCancelled && touch.phase != UITouchPhaseEnded) {
            allTouchesEnded = NO;
            break;
        }
    }

    if (allTouchesEnded) {
        ParentView* parentView = ((AppDelegate*)[UIApplication sharedApplication].delegate).parentView;

        parentView.currentlyTouchedView = nil;
    }
}

@end

I know this is not a very elegant solution and I'd appreciate if someone has a better idea.

Allow only one UIView to be touched


SQL Server + Dynamic Query 'Invalid Column Name'

Question: I am trying to execute the following dynamic query but I got an error: Invalid column name 'cat'
 DECLARE @SQLDelQuery AS NVARCHAR(1200)
 DECLARE @MemberNames varchar(50)

 SET @MemberNames = 'cat'

      SET @SQLDelQuery = 'SELECT [Email] FROM [aspnet_Membership] am
      INNER JOIN [aspnet_Users] u
      ON (am.UserId = u.UserId)
      INNER JOIN [Member] m
      ON (am.UserId = m.UserId)
      WHERE u.UserName IN (' + @MemberNames + ')

  EXECUTE(@SQLDelQuery)

If I change it to the normal query I am fine:
SELECT [Email] FROM [aspnet_Membership] am
  INNER JOIN [aspnet_Users] u
  ON (am.UserId = u.UserId)
  INNER JOIN [Member] m
  ON (am.UserId = m.UserId)
  WHERE u.UserName IN ('cat')

Anyone can point out my error? Thanks.
Answer: Since cat is a varchar you need to include single quotes around it and you need to place the closing parentheses for the IN clause inside of the sql string.
The new code will be:
DECLARE @SQLDelQuery AS NVARCHAR(1200)
 DECLARE @MemberNames varchar(50)

 SET @MemberNames = 'cat'

      SET @SQLDelQuery = 'SELECT [Email] FROM [aspnet_Membership] am
      INNER JOIN [aspnet_Users] u
      ON (am.UserId = u.UserId)
      INNER JOIN [Member] m
      ON (am.UserId = m.UserId)
      WHERE u.UserName IN (''' + @MemberNames + ''')'

  EXECUTE(@SQLDelQuery)

See a SQL Fiddle Demo with the query string printed. This generates a query string like this:
SELECT [Email]
FROM [aspnet_Membership] am
INNER JOIN [aspnet_Users] u
  ON (am.UserId = u.UserId)
INNER JOIN [Member] m
  ON (am.UserId = m.UserId)
WHERE u.UserName IN ('cat') -- cat surrounded in single quotes

Comment: thank you bluefeet. it worked. i cant vote yet cos i need 50 points
Answer: Your string:
WHERE u.UserName IN (' + @MemberNames + ')

will evaluate to:
WHERE u.UserName IN (cat)

because the apostrophes you have are just encapsulating the string, and there are no extra apostrophes around the string literal.
You need:
WHERE u.UserName IN (''' + @MemberNames + ''')

Alternately, you can leave your query as is, and seperate each ID with apostrophes in your @MemberNames variable:
SET @MemberName = '''cat'''           -- for a single user
SET @MemberName = '''cat'', ''dog'''  -- for multiple users

Answer: You need to pass it as a string to the dynamic query
 SET @MemberNames = '''cat'''

Difference in the resulted query is
WHERE u.UserName IN (cat) -- cat is taking as a column name here
WHERE u.UserName IN ('cat') -- cat is taking as a string here

Answer: Your dynamic query uses ' characters as the string delimiter, so the last line ends up reading like this after the string is built:
WHERE u.UserName IN (cat)

According to this, cat reads like a column name.
To fix it, you need to include escaped ' characters in either the definition of
`SET @MemberNames = '''cat'''`

or in the string being used to build the sql:
`WHERE u.UserName IN (''' + @MemberNames + ''')'`


my code gives me a tuple error and i dont know why

Question: MAIN CODE

i dont really know why i keep on getting tuple errors the code looks fine
its just the loop part of my game for now, this is it

        import pygame

    # important window variables
    WIDTH, HEIGHT = 900, 500

    WIN = WIDTH, HEIGHT

    SCREEN = pygame.display.set_mode((WIN))

    pygame.display.set_caption('SPACE GAME')

    WHITE = (0,0,0)

    # display function (what shows up on the screen)
    def display():
        WIN.fill(WHITE)
        pygame.display.update()

    # main function/loop function
    def main():
        run = True
        while run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False

            display()
        pygame.quit()

    if __name__ == '__main__':
        main()

Comment: What is the error?
Comment: I noticed that you set ```WIN = WIDTH, HEIGHT``` and then in the ```display()``` you do ```WIN.fill(WHITE)```.  I think this is the error.   ```WIN``` is a tuple.  It doesn't have a ```fill``` method.  Shouldn't it be ```SCREEN.fill(WHITE)```?
Comment: Where is main?? I only see display() function
Comment: Welcome to Stack Overflow! Please post the full error along with the line number, etc.
Comment: Hi Nabeel, welcome.  I believe you have the answer from @ewong, but as an aside: Because the color is a tuple of three integers, they are encoded as RGB, or (R, G, B) where each is a val from 0 to 255.
White should be (255, 255, 255) and black is (0, 0, 0)
Answer: I'm confident this is the issue (though without the real error traceback, I could be wrong.)
The basic issue is that you set the value of WIN as a tuple
i.e.
WIN = BASE, HEIGHT
Then in the display() function, you do the following:
  WIN.fill(WHITE)

Which is what I think the error is.  WIN being a tuple,
has no fill method.  What I think you wanted to use is
SCREEN.fill(WHITE)
I took a gander at the documentation (as I don't know pygame),
and it confirms that: pygame.display.set_mode((WIN))
returns a Surface object which does have a fill()
method.
So, in conclusion:

    # important window variables
    WIDTH, HEIGHT = 900, 500

    WIN = WIDTH, HEIGHT

    SCREEN = pygame.display.set_mode((WIN))

    pygame.display.set_caption('SPACE GAME')

    WHITE = (0,0,0)

    # display function (what shows up on the screen)
    def display():
        SCREEN.fill(WHITE)
        pygame.display.update()

    # main function/loop function
    def main():
        run = True
        while run:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False

            display()
        pygame.quit()

    if __name__ == '__main__':
        main()


Proving linear independence of orthogonal complement of $W$ and $W$

Question: Show that $W \cap W^\perp = \{0\}$ and $W + W^\perp = R^n$
I know that $W^\perp$ contains vectors perpendicular to all vectors in $W$, and that means $W$ and $W^\perp$ are linearly independent, but I have no idea where to start the proof... Can somebody give me some idea of the proof sketch? Thank you all in advance for the responses.
Answer: HINT
For the first one we have
$$v\in W \cap W^\perp \implies v\cdot v=0 \implies v=0$$
For the second one let $\{v_1,v_2,\ldots,v_k\}$ a basis for $W$ then we can extend that to a basis for $R^n$ by $\{u_1,u_2,\ldots,u_r\}$ with $k+r=n$ and we need to show that the last one is a basis for $W^\perp$.
Comment: I'm sorry but I don't really understand the explanation for the first part D: could you elaborate?
Comment: @RaniceTan If we assume $v\in W \cap W^\perp$ then v must be orthogonal to it self and therefore $v=0$.
Answer: suppose that $H$ is a Hilbert space and $X$ is a closed subset of $H$.
we want to show that $X+X^{\bot}=H$ and $X \cap X^{\bot}=\emptyset$.
Suppose that $h \in H$ then there is $x \in X$ such that $\Vert h-x\Vert=$$inf${$\Vert h-p\Vert$ :$p \in X$}. ((Because the function $f(p)=||h-p||$ is continuous on $X$ so $f$ achieves $sup ,inf$ in $X$.))
Now Let $x^{\bot}=h-x$,we will show that $x^\bot$$\bot X$.
For each$e\gt0$ we have
$$||x^\bot||^2=<x^\bot,x^\bot>=<h-x,h-x>=||h-x||^2 \leq ||h-(x+ez)||^2=||x^\bot+ez||^2 \leq ||x\bot||^2+||ez||^2-2Re<x^\bot,ez>$$
so we have
$$2Re<x^\bot,z> \leq e||z||^2$$
Since $e$ is arbitrary then $Re<x^\bot,z> \leq 0$ for each $z \in X$.
Since $X$ is linear subspace then $<x^\bot,z>=0$,so $x^\bot\in X^\bot$.
Comment: oh my gosh... thanks so much for the answer haha but I haven't learnt till that sophisticated in school yet ( ´_ゝ`) we have only learnt till eigenvectors
Comment: oh...good.................in this proof it is sufficient that replace $H$ by $R^n$
Comment: If you want I change the proof for you
Comment: it is not just that haha.. Everything else I just completely do not understand at all


Prove that $x^3-2$ and $x^3-3$ are irreducible over $\Bbb{Q}(i)$

Question:
Let $F=\Bbb{Q}(i)$. Prove that $x^3-2$ and $x^3-3$ are irreducible over $F$.

How do I go about this? Should I just say that the roots of $x^3-2$ are $2^{1/3},2^{1/3}e^{i2\pi/3},2^{1/3}e^{i4\pi/3}$, which do not belong to $\Bbb{Q}(i)$?
Answer: For a cubic polynomial, that argument works: it's irreducible if and only if it doesn't have any roots.
Alternately for $X^3 - 3$, you can use the fact that $\mathbb{Z}[i]$ is a unique factorization domain and $3$ is prime in $\mathbb{Z}[i]$, and apply the Eisenstein criterion.
Comment: Nice answer. Note that you can use the same strategy for $f(X) = X^{3}-2$ with a little fiddling. Consider the shift $f(X-1) = (X-1)^3-2 = X^3- 3X^2+3X-3$, and now you can apply Eisenstein at 3 again.
Comment: Ah that's great!  I didn't think it would work at first because $2$ is associate to the square of a prime, specifically forbidden by eisenstein.
Answer: A lot of the other answers have espoused that your answer is ultimately ok, but you should be cautious with polynomials of higher degree. I can't say I fully agree with the first point - saying that the roots aren't in $\mathbb{Q}(i)$ feels to me like you are begging the question, because that is precisely what you are trying to prove. How do you know that $2^{1/3}$ is not in $\mathbb{Q}(i)$, other than because it seems like it should be true?
To me, there are two rigorous approaches that stick out here. The first is the Eisenstein argument given in D_S's answer, which is great and very beautiful. It also hinges on knowing a few (albeit elementary) facts about $\mathbb{Z}[i]$, which you may not feel comfortable using.
The other argument that comes to my mind is a degree argument. This, I believe, proves what you are trying to show is true. Let $\alpha$ be a root of $f(X) = X^{3}-2$. If $f$ is reducible over $\mathbb{Q}(i)$, then without loss of generality, $\alpha$ is in $\mathbb{Q}(i)$, so $\mathbb{Q}(\alpha)$ is a subfield of $\mathbb{Q}(i)$. By the multiplicativity of field extension degree, $[\mathbb{Q}(i):\mathbb{Q}] = [\mathbb{Q}(i):\mathbb{Q}(\alpha)][\mathbb{Q}(\alpha):\mathbb{Q}] = 2$, so $[\mathbb{Q}(\alpha):\mathbb{Q}]\mid 2$. But by Eisenstein at $2$, $f$ is irreducible over $\mathbb{Q}$, so $[\mathbb{Q}(\alpha):\mathbb{Q}] = 3$, a contradiction. An identical argument works for $X^{3}-3$.
Answer: If a cubic is reducible then one of the factors is linear, which means it should have a root in $\mathbb{Q}(i)$. So your method is correct. Notice that a factor of a quartic may be quadratic so simply showing it has no roots isn't enough.
Answer: Almost, except that the correspondence $$P(x) \text{ doesn't have roots }\leftrightarrow P(x) \text{ is irreducible }$$
Is false for an arbitrary polynomial over an arbitrary field, and false in your specific field, $\Bbb Q(i)$, as well (for example, $(x^2-2)^2$ has no roots but is clearly not irreducible).
However, the argument works for these polynomials, since their degree is $3$ (and therefore...?).
Answer: Here is an idea.
Notice that, by Eiseinstein, $X^3 - 2 $ is irreducible over $\mathbb Q$, and it has degree $3$. Consider the polynomial  $X^2 + 1$ responsible for the extension $\mathbb Q [i]$ over $\mathbb Q$, and take the extension $Gal (\mathbb Q[\sqrt 2 , i];\mathbb Q)$ we may conclude from the fact that $\gcd \{2,3\} = 1$ that $$[\mathbb Q[\sqrt 2 , i]:\mathbb Q[i]] = 3 $$ and $X^3-2$ is irreducible over $\mathbb Q[i]$.
Answer: If $x^3-2$ had a root $\xi$, we would have:
$$N(2)=4=N(\xi)^3.$$
However $4\,$ is not a cube in $\mathbf Z$. Same argument for $x^2-3$.


What does com.mathworks.mde.cmdwin.CmdWin means in MATLAB? Is there any documentation regarding this?

Question: function interrupt

import java.awt.event.KeyEvent
import java.lang.reflection.*

base = com.mathworks.mde.cmdwin.CmdWin.getInstance();
hCmd = base.getComponent(0).getViewport().getView();
cmdwin = handle(hCmd,'CallbackProperties');

argSig = javaArray('java.lang.Class',1);
argSig(1) = java.lang.Class.forName('java.awt.event.KeyEvent');

msTime = (8.64e7 * (now - datenum('1970', 'yyyy')));
args = javaArray('java.lang.Object',1);
args(1) = KeyEvent(cmdwin,KeyEvent.KEY_PRESSED,msTime,...
    KeyEvent.CTRL_DOWN_MASK,KeyEvent.VK_C,KeyEvent.CHAR_UNDEFINED);

method = cmdwin.getClass().getDeclaredMethod('processKeyEvent',argSig);
method.setAccessible(true);
method.invoke(cmdwin,args);

The above code was pasted from this answer. I just need to understand or find API/Documentation about this line:
com.mathworks.mde.cmdwin.CmdWin.getInstance();

I saw similar stuff all over the internet. What is it and where can I find any source?
Comment: It gets the Java handle to MATLAB's command window. MATLAB's underlying Java is not documented.
Answer: This is an unsupported and undocumented API to access the command window. More examples of its use can be found at Undocumented Matlab. You're accessing directly the Java components that MATLAB is built with, so it's best not to rely on these things to be stable or long-lived.
Comment: Thanks, for the info. I am trying to find a way to automatically execute/code Ctrl+C. I looked at everything and I am out of luck. So I decided to look into Java stuff. It works, but I want to do something that I understand.Can you suggest something?
Comment: @SimonMatt Have you looked into the `java.awt.Robot` class? A similar example can be found here: http://stackoverflow.com/questions/27933270/programmatically-press-an-enter-key-after-starting-exe-file-in-matlab - Modify for your own purposes.
Comment: Doesn't solve my problem using Robot. However, solution from @MattB works. But that solution is very hard to understand.


Minimizing a specific function over n variables

Question: Experimenting with something related to probability theory I came across the following $n$-variable function
$$ f(p_1,\ldots,p_n) = \sum_{i=2}^{n-1} \left ( (1-p_i)(1-p_{i-1})^i (1-p_{i+1})^{n-i} + p_i {n \choose i} \right ) $$
where $p_1,\ldots,p_n \in [0,1].$
The natural thing is to use the inequality $(1-p_i)^t \leq e^{-p_i t}$ but I don't know what to do from there.
Since I am very rusty with such optimizations I would like to ask if someone could find a good lower bound for this function ? What is a good approximation of the minimum of $f$ and how to obtain it?
If this is too hard, it would be nice to see if there is a set of values for $p_1,\ldots,p_n$ such that $f(p_1,\ldots,p_n)$ is sub-exponential?
Thank you.
Comment: I'd start by taking derivatives with respect to the various $p_i$'s.
Comment: Try $p_i=0$ for all $i$ sub-exponential. Or do you mean $\sum_i p_i = 1$?
Comment: Try the transformation $x_i = 1- p_i$. Then you're really minimizing $\sum x_i x_{i-1}^i x_{i+1}^{n-i} - C(n,i) x_i$. Before I take a crack at it, can you verify whether or not $\sum_i p_i = 1$ is a constraint?
Comment: I'll also note a slight asymmetry in the formula that causes me to wonder: the first and last terms involve $C(n,2)$ and $C(n,n-1)$, which are unequal. Do you mean for the summation to begin at $i=1$ and to allow a $p_0$? That'd make things more symmetric.
Comment: @WillNelson The sum of $p_i$'s has no such constraints. As for the asymmetry - the formula is indeed asymmetric in this way.
Answer: Use the transformation $x_i=1-p_i$. The problem is then equivalent to minimizing
$$
T = \sum_{i=2}^{n-1} x_i x_{i-1}^i x_{i+1}^{n-i} - \left(\begin{array}{c} n \\ i\end{array}\right) x_i
$$
for $x_i\in[0,1]$.
(This target function differs by a constant from the original.)
If $n\le 2$, $T=0$.
If $n=3$, then $T=x_2 x_1^2 x_3 - 3 x_2$. This is minimized when $x_2=1$ and $x_1x_3=0$. Thus,
$$
\mbox{If $n=3$, then $p_2 = 0$ and either $p_1=1$ or $p_3=1$.}
$$
Suppose $n\ge 4$. Take the derivative with respect to $x_i$:
\begin{eqnarray}
(1) \ \frac{\partial T}{\partial x_1} &=& 2 x_2 x_1 x_3^{n-2} \\
(2) \ \frac{\partial T}{\partial x_2} &=& 3 x_3 x_2^2 x_4^{n-3} + x_1^2 x_3^{n-2} - \left(\begin{array}{c}n \\ 2\end{array}\right)\\
(3)  \frac{\partial T}{\partial x_i} &=&  x_{i-1}^i x_{i+1}^{n-i} + (i+1) x_{i+1} x_i^i x_{i+2}^{n-i-1} + (n-i+1) x_{i-1} x_{i-2}^{i-1} x_i^{n-i} - \left(\begin{array}{c} n \\ i \end{array}\right) \ \ \ \mbox{ for } 2 < i < n-1\\
(4) \frac{\partial T}{\partial x_{n-1}} &=& x_{n-2}^{n-1} x_n + 2 x_{n-2} x_{n-3}^{n-2} x_{n-1} - n\\
(5) \frac{\partial T}{\partial x_n} &=& x_{n-1} x_{n-2}^{n-1}.
\end{eqnarray}
Equation $(3)$ is at most $n+3 - \left(\begin{array}{c} n\\ i\end{array}\right)$ whenever it is valid, i.e., when $n\ge 5$ and $3\le i\le n-2$. But this quantity is negative. We conclude that to minimize $T$,
$$
x_i=1 \mbox{ for } 3\le i\le n-2.
$$
Equations $(2)$ and $(4)$ are also negative, showing $x_2=x_{n-1}=1$ as well. Then equation $(5)$ reduces to $1$, so $x_n=0$. Finally, equation $(1)$ reduces to $2x_1$, so $x_1=0$ as well. Hence,
$$
\mbox{If $n\ge 4$, then $p_1=p_n=1$ and $p_i=0$ for other $i$.}
$$
Finally, compute the optimal (original) target function:
\begin{eqnarray}
n=3 &\mapsto& 0 \\
n\ge 4 &\mapsto& n-4.
\end{eqnarray}


Entity Metadata Wrapper Save VS Node Save when invoked from hook_node_insert

Question: Can somebody shine a light into the difference of the two, in terms of their interaction with database when hook_node_insert() is invoked? The documentation explains that

Note that when this hook is invoked, the changes have not yet been written to the database, because a database transaction is still in progress. The transaction is not finalized until the save operation is entirely completed and node_save() goes out of scope.

In fact, when I tried doing node_save($node), it will give me an error.
However, if I was doing it this way,
$nodeWrapper = entity_metadata_wrapper('node', node_load($node->nid));
$nodeWrapper->title->set(t("Saved by Entity Save"));
$nodeWrapper->save();

It works just fine. Has the new node being committed into the database after the $nodeWrapper->save() was executed?
Comment: I edited the title.  The code you posted is about `entity_metadata_wrapper()`.  `entity_save()` is different, and I think this matches you question better.
Comment: @rwaery What error message do you get?
Comment: @rwaery It seems strange to me to call `node_save()` inside a `hook_node_insert()`, as this hook is triggered from `node_save()`. Smells bad. If you just want to alter the data before it get's saved, use `hook_node_presave()`.
Comment: @berliner, thanks for looking into this. Exactly my thoughts. The error was about integrity constraint violation. Initially I had the same questions as the other posters on `hook_node_insert()` [doc page] (https://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_insert/7), on whether or not to trust the nid contains in $node object while still inside `hook_node_insert()` scope. Say, I wanted to trigger an email with link to the new node, and trigger it from `hook_node_insert()` can I be sure that the node is already committed to database?
Comment: Technically you can't. It is clearly stated that the node is not yet finally saved in the database at the time when `hook_node_insert` is invoked. Practically though, I'm not aware of any other hook based solution to react on a node being created, and usually there should be no reason for a transaction rollback. The note in the docs is also mostly about not tying to reload node data from the database, as it's not yet written.
Answer: Essentially, there really isn't much difference between node_save() and the save() method of the entity metadata wrapper. Neither should however be used to save the node passed in as the argument to hook_node_insert().
The argument is passed by reference (since it's an object and all objects are passed by reference in later PHP versions), and any changes you make to it will be included when the node is saved by Drupal just after it's done calling the hook implementation.
Trying to manually save the same node again could trigger endless recursion into your hook_node_insert() implementation.
Just alter the node object as you see fit (setting the title in this case, though that is not a translatable string, so replace t() with check_plain()) and do nothing more.
If you need to do more complex alterations, the entity metadata wrapper could come in handy, but you must remember that any alterations you want to have saved must be performed on the node instance passed as a reference to hook_node_insert(). This means you can't node_load($node->nid) here first, since that would actually just create a new node object with data that's already in the database. If you do manage to save that node copy without ending up in a recursion loop, it would either mean that the node save already in progress - the reason hook_node_insert() was originally called - overwrites any changes you made, or fails because the new node to be saved already exists.
Instead, pass the $node argument to entity_metadata_wrapper() directly and the wrapper will be able to alter the correct node object for you, but remember to not call save() on the wrapper!
Answer: Calling node_save() from inside a hook_node_insert() implementation is error prone and should only be done with sufficient condition checks as to not provoke function nesting.
When you call node_save() coming from an insert operation and saving the $node object that already has an nid, then this call of node_save probably creates an update operation, which might result in strange errors. I don't fully follow the information flow here, but I would guess that one of the two calls to node_save() can easily run into a problem.
The correct way in a scenario where you want to alter node properties (e.g. status, promote, title, ...) prior to inserting, is to use hook_node_presave(). If you want to update field values inside a hook_node_insert(), that should be done by calling field_attach_update('node', $node).
Using the save() method of entity_metadata_wrapper doesn't exactly feel good either. Looking at the code in EntityDrupalWrapper::save, which I suppose is the wrapper class that is responsible, it looks like node_save() is going to be called in the end too. But I have never really used Entity Wrapper, so I'm a bit short here.
Edit: Regarding entity_metadata:_wrapper, looking again at the code of entity_save() I realize that it's not node_save() that is called in the end, but that this is entirely handled by the Entity module. At the end it will end up here if I'm not mistaken: EntityAPIController::save. I'm not sure though how that explains the behavior described in the comment.
Comment: I did a couple of tests, and found the following results. `node_save($node)` and `entity_save('node', $node)` returns integrity violation error. We both see that `$nodeWrapper->save()` eventually calls `entity_save('node', $node)`, this is what confuses me, since `$nodeWrapper->save()` works okay. Oh and, doing `$nodeWrapper->save()`, and follows by `node_save($node)`, I thought, would be okay, thinking that the new node is already saved by entity wrapper, and will only be updated when `node_save()` is invoked but no, it also returned the same integrity violation error.
Comment: @rwaery I have updated the answer
Answer: The reason why node_save($node->nid) gives you an error is because node_save takes node object to be saved, not a node id. Just change it to node_save($node) and it will work just fine.
In simple terms, node save is just a specific kind of entity save and just makes your life easier by doing things that you would have to do yourself (like in your case, node_save is one line, saving the entity does the same thing but you have to deal with metadata wrapper unnecessarily).
Comment: Thanks for pointing that out. I didn't type it correctly, it did use `node_save($node)` inside `hook_node_insert()` it returns, Integrity Constraints Violation. I have updated the question accordingly.


Get the list from list of object using Linq dynamically in C#

Question: I have an app with a list of objects. I want to create multiple lists from that single list, based on one of the parameters, dynamically.
I have searched but could not find how to make it work exactly. Here are the details.
I have a class ProductDetails with these properties:
    public class ProductItem
    {
        public string StoreId { get; set; }
        public string StoreName { get; set; }
        public string ProductName { get; set; }
    }

The current list is filled like this:
List<Item> items = new List<Item>() {
     new Item() { ProductName = Pen, StoreId = 1, StoreName = Test },
     new Item() { ProductName = Pen, StoreId = 2, StoreName = Test2 },
     new Item() { ProductName = Pen, StoreId = 2, StoreName = Test2 },
     new Item() { ProductName = Pen, StoreId = 1, StoreName = Test }
};

My requirement is to create lists which have distinct store names.
In my example, I should get 2 lists as there are two unique store names in the sample data.
How can I write a Linq query to prepare dynamic lists from a list?
Answer: You can use LINQ to achieve this by grouping the items based on the unique StoreName, I think the following code should work for you:
using System;
using System.Collections.Generic;
using System.Linq;

public class ProductItem
{
    public string StoreId { get; set; }
    public string StoreName { get; set; }
    public string ProductName { get; set; }
}

class Program
{
    static void Main()
    {
        List<ProductItem> items = new List<ProductItem>(){
            new ProductItem(){ ProductName = "Pen", StoreId = "1", StoreName = "Test"},
            new ProductItem(){ ProductName = "Pen", StoreId = "2", StoreName = "Test2"},
            new ProductItem(){ ProductName = "Pen", StoreId = "2", StoreName = "Test2"},
            new ProductItem(){ ProductName = "Pen", StoreId = "1", StoreName = "Test"}
        };

        // LINQ query to group by StoreName
        var groupedLists = items.GroupBy(item => item.StoreName)
                                .Select(group => group.ToList())
                                .ToList();

        // Displaying the results
        foreach (var groupedList in groupedLists)
        {
            Console.WriteLine($"List for StoreName: {groupedList.First().StoreName}");
            foreach (var item in groupedList)
            {
                Console.WriteLine($"  ProductName: {item.ProductName}, StoreId: {item.StoreId}");
            }
            Console.WriteLine();
        }
    }
}

Comment: Thanks @Zohair: It worked like a charm... I made small modifications.
Comment: Glad to know that :)
Comment: Why `ToList` what is it gaining you?
Comment: You are right, it is redundant in this case. But to be honest I provided a solution, not the optimized one. There could be several other issues that I can find in my own code where there is some hope for improvement.


Filter views by field value equals another field of curent node

Question: D9.3. All fields are not referenced, they are just simple integers. All data was pulled from a huge external API and it was not possible to create all references.
Just for ease of understanding, imagine a book database website with books and characters. But each character is presented in one book only.
So, book node has field_book_id, which is numeric, unique, but not NID. Also it has a multiple field_character_ids with a list of ids of all characters in this book.
A character node has its own field_character_id (matching with field_character_ids from book node). And also field_my_book_id, which equals to book id field.
The goal is to create a View, which will show the current book's characters in a list.
I'm using Views Contextual Filter Default Entity Field Values module which allows to pull fields values from the current node, for example.
But in Views Filter Criteria I can't match the selected field with the value from the Contextual Filter. It has only pre-defined operators, such as is equal to, is less than, is greater than, etc. And I can't select something like "Match value from contextual filter". I tried to use twig and replacement patterns there without any luck.
It would be a very simple database query, but is it possible to get it working in Views?
Answer: Ok, it was rather simple.
In contextual filter add the field you want to target.
Then: When the filter value is NOT available > Provide default value > Field value from current entity > under Entity Field select field which will filter the view results.


C programming : How to get directory-name if I know the inode-number

Question: How can I get directory name if I know it's inode number? Need the code. Thanks.
the below code passed dir_name by .. , then I got it's i-node number, but what I also want is the directory name.
/*child stat*/
stat(dir_name, &sb);
if (stat(dir_name, &sb) == -1) {
    perror("stat");
    exit(EXIT_FAILURE);
}
childIno = (long) sb.st_ino;

Comment: inode to path is not a 1:1 mapping.
Answer: Unless you have an index mapping inodes to names, you will have to recursively walk the directory structure until you find a name with the inode you're looking for (which you may not find).
If one part of your program already knows the name of the directory, you should find a way to pass it into your code.
Answer: Not sure you can easily get it from the inode number alone.  Lots of things could point to the same inode (hard links, soft links etc).  If you are just looking for a path you can print out and feel good about, you could use realpath.  Example below compiles readily on Ubuntu 12.04 and should work on BSD systems as well.  realpath will eat all of the ../ and ./ stuff resulting in a decent looking path (though not necesarily a unique path.
#include <stdlib.h>
#include <stdio.h>

int main( int argc, char *argv[] ){
  char *test_path = realpath( "..", NULL );

  if ( test_path ) {
    printf( "Path resolves to %s\n", test_path );
  } else {
    printf( "Couldn't resolve path\n" );
  }

  exit(0);
}


Como utilizar o SUM para somar os valores entre uma determinada data

Question: Preciso da quantidade total vendida entre duas datas, estou tendo dificuldade com o SUM para essa soma em especifico meu conhecimento em SQL é limitadíssimo.
SELECT Qtde_Vendida, `Data`
FROM vendas, tempo
WHERE `Data` BETWEEN '2020-01-01' AND '2020-12-31'
SELECT SUM (Qtde_vendida) FROM vendas

Como utilizar o Select Sum para que ele some a quanditade de vendas dentro de um determinado período ?
Comment: Publique a estrutura das tabelas , Veja a síntaxe do JOIN e do GROUP BY ... https://dev.mysql.com/doc/refman/8.0/en/join.html ... https://dev.mysql.com/doc/refman/8.0/en/group-by-modifiers.html
Answer: Sua lógica estava correta. A única questão é que o sum vem direto no select, portanto o select acima não era muito necessário; exceto se você for realizar um inner join. Nesse caso, será diferente. O script me informa se ele relaciona com outra tabela que então realizo um script diferente para questão
create table dat (
    data datetime,
    qtde_vendida int,
    )

select sum(dat)

insert dat values (getdate(), 1000)
insert dat values (getdate(), 1500)
insert dat values (getdate(), 1700)

select * from dat

update dat set data = '2020-06-09 11:35:24.580' where qtde_vendida = 1700
update dat set data = '2021-06-09 11:35:24.580' where qtde_vendida = 1000

select sum(qtde_vendida) as soma from dat where data between '2020-06-09 11:35:24.580' and '2022-06-09 11:35:24.580'


Cannot programmatically click  tag in Greasemonkey script

Question: I'm trying to get Greasemonkey in Firefox to click this anchor which doesn't have an href attribute:

  Add Program

I already tried this line of code, but it won't work:
// ==UserScript==
// @name        Final Test
// @description Final test results
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js
// @include     https://qs-hub.qs.com/#/profiles/topuniversities/programs*
// ==/UserScript==
setTimeout(function () {
  document.querySelector('.btn.btn-primary.pull-right').click();
}, 5000);

Does anyone have the right solution to click this anchor?
Comment: can you console.log() that your `document.querySelector('.btn.btn-primary.pull-right')` is actually finding the element?
Comment: Is that line of code everything in your Greasemonkey script? You should probably wait until the page loads. What are the full contents of the script file?
Comment: I updated the code! I use a timer, so the page can load first.
Comment: What's the jquery for?
Comment: I used a similar script for Tweet deletions, I got the click code working with the jquery require.
Comment: Ah, cargo cult coding.
Answer: The code should work fine. You do need to ensure that you click the right element:
console.log(document.querySelector('.btn.btn-primary.pull-right'));

As per the comments, you actually want to click the fifth such element. That'd be:
document.querySelectorAll('.btn.btn-primary.pull-right').item(4).click();

document.querySelectorAll returns a NodeList of which we click the fifth item.

You are not using jQuery by the way, so there is no need to require that in your script.
Instead of having the timeout, consider waiting for the page to load:
window.addEventListener('load', function() {
    document.querySelectorAll('.btn.btn-primary.pull-right').item(4).click();
}, false);


What does python with statement do when opening files?

Question: I think that
with open('file.txt','r') as f:
    pass

closes file f, but, how can I prove it? My colleague thinks it will flush the file if it is open for writing.
Comment: https://docs.python.org/3.6/tutorial/inputoutput.html#reading-and-writing-files
Comment: actually, https://docs.python.org/3.6/reference/compound_stmts.html#with
Comment: see the answer: you are both correct
Answer: The documentation clearly states that files will be closed once the with statement is exited.
However, if that is not proof enough - here is a way you can check for yourself;
Files have a .closed property that you can check.
with open("file.txt", "r") as f:
    print(f.closed)  # will print False
print(f.closed)  # will print True

The same property can be used when dealing with files the non-with way.
f = open("file.txt", "r")
print(f.closed)  # will print False
f.close()
print(f.closed)  # will print True.

This should service as sufficient proof that the file is indeed being closed.
Comment: Yes: close implies flush. See https://stackoverflow.com/questions/2447143/does-close-imply-flush-in-python


Can somone please explain how this algebra works?

Question: When trying to solve for X, I saw the following.
Since we know that AX = B + CX, consequently X = B/(A - C).
I see this works in practice, but I don't understand what's happening. Can someone please explain?
Comment: Move the $CX$ term to the right hand side, give $X$ ans a factor.
Answer: $$\begin{align*}
AX &= B+CX\\
AX - CX &= B+CX-CX\\
AX-CX &= B\\
(A-C)X &=B\\
\frac{1}{A-C}\Bigl( (A-C)X\Bigr) &= \frac{1}{A-C}\Bigl(B\Bigr) &\quad\text{if }A-C\neq 0\\
\left(\frac{A-C}{A-C}\right)X &= \frac{B}{A-C} &\quad\text{if }A-C\neq 0\\
X &= \frac{B}{A-C} &\quad\text{if }A-C\neq 0.
\end{align*}$$
Comment: Thanks for the fast and clear answer.
Answer: Start
$$AX = B + CX$$
Subtract $CX$ from both sides
$$AX -CX = B$$
Factor left-hand-side
$$(A -C)X = B$$
Divide both sides by $A-C$ if it is not zero
$$X = \frac{B}{A-C}$$
Answer: $$AX=B+CX$$
Subtract $CX$ from both sides and divide by $A-C$
$$AX-CX=B$$
$$(A-C)X=B$$
$$ X=\frac {B}{A-C}$$