When does one omit the fifth in V7 chord in classical harmony?

Question: The textbooks on classical harmony that I have read (Leo Semlek, "Klassikalise harmoonia lühiõpik" ["A Short Textbook on Classical Harmony"] and "Klassikalise harmoonia õpik" ["A Textbook on Classical Harmony"]) say that if V7 is in root position the fifth can be omitted (and the root is doubled).
There are some cases where he says that one should only use V7 without the fifth:

After II7
After IV

How does one decide when to omit the fifth? What is special about II7 and IV? In the case of II7, wouldn't it be a good idea to use the root of II7 as the fifth of V7?
I'm curious about the possible reasons because I am trying to write a computer program for generating nice classical chord progressions. It would be cleaner to have some kind of general rule ("omit the fifth if...") instead of adding several just-because special cases.
And are there any reasons why the textbook tells that the fifth can be omitted in V7 in root position but explicitly tells to not omit the fifth in the inversions of V7 or in any other seventh chord (II7 and VII7)? What is special about V7 in root position?
Comment: +1 for specifying "in classical harmony". Btw, have you listened to the examples, with/without omitting the fifth, can you _hear_ any difference? I think if you cannot hear the difference, then it's a bit like cargo cult programming. ;) If you use rules, they should assist you in writing nice-sounding things straight away without having to go back to fixing the music all the time. But if you can't _hear_ the difference... maybe try to find someone who can? :) Good question anyway.
Comment: When you say II7, do you really mean the major-chord version (which is often better spelled in classical music as V7/V), or do you mean ii7?
Comment: @Dekkadeci Not all systems use capital vs. lowercase to denote mode. I would bet OP means the diatonic version, but hopefully they'll clarify.
Comment: @piiperi I can't hear the difference. But I'm not taking it as a sign that there is no difference, because I can also barely tell any difference between the tonic in root position and the cadential 6/4 chord, or between a dominant seventh and a dominant ninth chord... I'm also not bothered by parallel fifths unless there's a few of them in a row, but would still want my program to avoid them. (And strict rules would help contain the combinatorial explosion by pruning the less-desirable small fragments so that the number of longer fragments does not get out of hand.)
Comment: @Dekkadeci Those textbooks use capital roman numerals for all chords. When talking specifically about minor (which he does rarely), those chords that contain the VI and VII degrees have superscripts that indicate whether the chord is natural or melodic/harmonic. So I'm assuming that when he talks about II7, he means the chord built from degrees II, IV, VI, I - and it can be a minor seventh chord (in major or melodic minor) or a diminished seventh chord (in harmonic minor). But I might be wrong about that. Not completely sure.
Comment: Can you list all the rules you were able to obtain from the book? Did I understand correctly that there are three categories of situations: (A) the fifth MAY be omitted, (B) the fifth MUST be omitted, (C) the fifth MUST NOT be omitted? And for these MUST and MUST NOT cases, there's a supposed "...OR ELSE YOU WILL SUFFER THE CONSEQUENCES" clause that's been accidentally left out, or the book just fails to explain what the consequences are, and you are unable to hear what the bad thing might be?
Comment: @piiperi I don't have the books on hand right now, only the notes I made based on them. I'll be able to answer the question in a few days, when I visit the university library.
Comment: @Dekkadeci There was a mistake in my previous comment - not diminished seventh chord but half-diminished seventh chord. I forgot the "half". (Couldn't edit the comment 2 days later.)
Comment: @piiperi I re-read the textbooks. In addition to the two cases I mentioned in the original post (after II_7 and IV) there's another case where it's recommended to omit the fifth - before or after V_9. I guess it's because V_9 also lacks the fifth and if we omit the fifth in V_7 we can get away with moving only one of the voices instead of two?
Answer: The rule that the 5th can be omitted is developed of the voicing (voice leading) of the 4 parts:
ii7 = re,fa,la,do resolving:
re->so (bass root)
fa->fa (retain the common tone)
la->so (la->re could become a 5th parallel
do->ti (7th downwards)
similar problems with IV-V:
do->re (5th parallel with fa->so) ergo: do->ti
la->ti (ti would be doubling lead tone) ergo: la->so
if you keep the common tone and don't lead it downward  there is no fifth in the V7 or you'll have strange leaps in the other parts (parallels of 5ths or the 7th of the succeeding chord won't be prepared or the 7th wouldn't be resolved correctly as lead tone.
The trouble with this rules is:
You should first write songs in this classical style and the you'll find the development of the rules by yourself.  Other wise you have an amount of rules you should apply and don't know why. This means: Make first the mistakes and learn from your errors.
I suppose this program you want to write might already exist. Did you look it  up yet?
Comment: Thank you. I forgot about the common tone. It's nice to know that those rules are not some random exceptions for no reason but corollaries of other, more understandable and general rules. About the program - yes, something similar probably already exists. But it's always fun to have an excuse to learn more about music theory and write some code :)


Continued fraction question

Question: I have been given an continued fraction for a number x:
$$x = 1+\frac{1}{1+}\frac{1}{1+}\frac{1}{1+}\cdots$$
How can I show that $x = 1 + \frac{1}{x}$? I played around some with the first few convergents of this continued fraction, but I don't get close.
Comment: At the *formula* level, just write down the continued fraction, long-hand, as a long fraction. Then if we let the "value" be $x$, then $x-1$ is $1$ divided by $x$. A formal proof requires some theory.
Answer: Just look at it.  OK, if you want something more proofy-looking: if $x_n$ is the $n$'th convergent, then $x_{n+1} = 1 + 1/x_n$.  Take limits.
Answer: Doesn't this immediately follow from the definition of the $n+\frac1{a+}\frac1{b+}\cdots$ notation you are using?  Specifically, I thought that $\frac1{a+}Z\ldots$ was defined to be exactly the same as $\frac1{a+Z\ldots}$.
Then if $x=1+\frac1{1+}\frac1{1+}\cdots$ then $\frac1x = \frac1{1+}\frac1{1+}\cdots $ and $1+\frac1x = 1+\frac1{1+}\frac1{1+}\cdots = x$.
Comment: Please do not feel dumb. Sometimes a notation is good precisely because it hides this sort of detail so that you can concentrate on more important matters.  But until you get used to it, the notation can be paradoxically confusing precisely because it hides the detail.
Comment: I feel dumb -.-

Continued fraction question


Significance testing on Pearson's r when the data are non-normal but sample size is large

Question: Hypothetically let's say I'm doing a study in which the data on both variables are likely to be quite positively skewed, but the sample size is potentially quite large.
In this research context using significance tests on Pearson's r is the most common technique and it would be useful if I could use it. It's less desirable for me to use the Spearman correlation because of the audience this study is pitched at.
I know from this question that Pearson's r doesn't "assume normality" per se, but that I may not be able to trust the p-values if I have non-normal data and a small sample size.
Clearly the right way to proceed will depend on how non-normal my data are and how large my sample is. Are there any rules of thumb I can usefully apply?
Comment: Such rules of thumb are in my experience useless and in any sense outdated. The bootstrap is nearly 40 years old as a practical way to quantify uncertainty. Clearly, there aren't threshold sample sizes that guarantee anything.  If you are interested in the possibility of a linear relationship, fit a regression tailored to the situation. Fields differ but "my correlation is significantly different from zero" is usually a minimal discovery. If that's seriously in doubt, your sample size may be too small for anything much.
Comment: It's not clear whether you have a large sample (title) or a small sample (third paragraph). Either way, P-value calculations for correlation can always be supplemented with confidence interval calculations. You could use both the Fisher z method and bootstrapping. The real issue is not so much nonnormality as nonlinearity, so always plot the data and consider transformations. http://www.stata-journal.com/sjpdf.html?articlenum=pr0041 may help.
Comment: The third paragraph talks in general terms about the consequences of a small sample size, but the questions is about situations where a large sample has been, or could be, obtained. Basically I'm wondering how quickly the sampling distribution converges to something fairly normal in typical situations, and whether I can use some rule of thumb like "if n > 30 and the data isn't crazily non-normal you're probably OK".
Answer: Choosing a suboptimal estimator because of the audience is in my view not the best practice of statistics.  As statisticians we must use the best available methods or invent one that is better than what's available.  Our goal is never to give the audience what they want, but to give them what they need.  Without knowing more about your problem, and assuming that the only relationship you are interested in is a monotonic one, I would choose Spearman's $\rho$ as a default method.  It it robust, powerful, and does not assume linearity.  It is heavily used in most disciplines.
If you want to detect and quantify a relationship that is up and down (with one turn) you can use a generalization of Spearman's $\rho$ as implemented in the R Hmisc package spearman2 function.  For completely general relationships including a circle you can use Hoeffding's $D$ (see Hmisc hoeffd function).


Checkout a specific commit with NodeGit

Question: I have a NodeGit repository object, repo, and a string sha code which represents a commit id, commit_id. How can I perform a git checkout of this commit using NodeGit, which would act the same as the command git checkout $commit_id?
So far, I have tried creating a Reference using the commit id and then using checkoutRef to check out the reference. However, the repository does not change and I get the error Error: the given reference name '<my commit id>' is not valid.
NodeGit.Reference.lookup(repo, commit_id, (reference) => {
    repo.checkoutRef(reference)
});

Thanks for the help.
Comment: Hi @larsks - I have added my attempt to the question. The documentation does not provide any examples of checking out a commit ID, only the latest commit of a named branch. Thanks!
Comment: What have you tried so far? The [nodegit documentation](https://www.nodegit.org/api/) seems to be a good source of information.
Comment: Agree, very confusing
Answer: The only solution I've found so far is:
const commit = await repo.getCommit(commit_id);
await nodegit.Reset.reset(repo, commit, nodegit.Reset.TYPE.HARD);

But it's so slow... Especially for big repositories!

Checkout a specific commit with NodeGit


I want to vertical-align text in select box

Question: I want to vertically align the text in select box. I tried using
select{
   verticle-align:middle;
}

however it does not work in any browsers. Chrome seems to align the text in select box to the center as a default. FF aligns it to the top and IE aligns it to the bottom. Is there any way to achieve this? I am using GWT's Select widget in UIBinder.
This is currently what I have:
select{
    height: 28px !important;
    border: 1px solid #ABADB3;
    margin: 0;
    padding: 0;
    vertical-align: middle;
}

Thanks!
Comment: Your first example shows "verticle-align" rather than "vertical-align" - these sorts of typos will definitely cause you problems.
Comment: is `font-size:22px;` an option?
Comment: I already have font-size set to 14px. I don't think this will work throughout browsers...
Comment: Still an issue in 2016!
Answer: Your best option will probably be to adjust the top padding & compare across browsers. It's not perfect, but I think it's as close as you can get.
padding-top:4px;

The amount of padding you need will depend on the font size.
Tip: If your font is set in px, set padding in px as well. If your font is set in em, set the padding in em too.
EDIT
These are the options I think you have, since vertical-align isn't consistant across the browsers.
A. Remove height attribute and let the browser handle it. This usually works the best for me.

    <select>
        <option>option 1</option>
        <option>number 2</option>
    </select>

B. Add top-padding to push down the text. (Pushed down the arrow in some browsers)

<select>
    <option>option 1</option>
    <option>number 2</option>
</select>

C. You can make the font larger to try and match the select height a little nicer.

<select>
    <option>option 1</option>
    <option>number 2</option>
</select>

Comment: In my experience that doesn't work, as any padding you to the top not only lowers the text, it also lowers the button with the arrow that opens the list of options.
Comment: Thanks for the clear example. I tried option 2 and it added padding to the textbox part of the select, so the arrow button that is used to enlarge the list stayed the same... Increasing the font size is not an option so I need to find other solution.. Thanks.
Comment: AS J.Pablo Fernández stated, padding pushes the arrow down, which isn't an acceptable option for some people, especially aesthetically-focused clients.
Comment: if your select box is bigger, what worked for me is setting padding= box height/5, só my select box has a heigh of 35 and i set padding of 7px, that seemed to fix it
Comment: what is `padding: auto`?
Answer: I stumbled across this set of css properties which seem to vertically align the text in sized select elements in Firefox:
select
{
    box-sizing: content-box;
    -moz-box-sizing:content-box;
    -webkit-box-sizing:content-box;
}

If anything, though, it pushes the text down even farther in IE8.  If I set the box-sizing property back to border-box, it at least doesn't make IE8 any worse (and FF still applies the -moz-box-sizing property).  It would be nice to find a solution for IE, but I'm not holding my breath.
Edit:  Nix this.  It doesn't work after testing.  For anyone interested, though, the problem seems to stem from built-in styles in FF's forms.css file affecting input and select elements.  The property in question is line-height:normal !important.  It cannot be overridden.  I've tried.  I discovered that if I delete the built-in property in Firebug I get a select element with reasonably vertically-centered text.
Answer: My solution is to add padding-top for select targeted to firefox only like this
// firefox vertical aligment of text
@-moz-document url-prefix() {
    select {
        padding-top: 13px;
   }
}

Comment: I like your solution the best! This way the padding does not mess up webkit browsers. (I haven't tested on IE yet though)
Answer: So far this is working fine for me:
line-height: 100%;

Comment: Easiest solution and working!
Answer: display: flex;
align-items: center;

Answer: I found that simply setting the line-height and height to the same pixel quantity produced the most consistent result.  By "most consistent" I mean optimally consistent but of course it is not 100% "pixel-perfect" across browsers.  Additionally I found that Firefox (v. 17.x) tends to crowd the option text to the right against the drop-down arrow; I alleviated this with a small amount of padding-right set on the OPTION element only.  This setting does not affect appearance in IE 7-9.
My result:
select, option {
    font-size:10px;
    height:19px;
    line-height: 19px;
    padding:0;
    margin:0;
}

option {
    padding-right:6px; /* Firefox */
}

NOTE -- my SELECT element uses a smaller font, 10px.  Obviously you will need to adjust proportions accordingly for your specific UI context.
Answer: just had this problem, but for mobile devices, mainly mobile firefox. The trick for me was to define a height, padding, line height, and finally box sizing, all on the select element. Not using your example numbers here, but for the sake of an example:
padding: 20px;
height: 60px;
line-height: 1;
-webkit-box-sizing: padding-box;
-moz-box-sizing: padding-box;
box-sizing: padding-box;

Answer: I've tried as following and it worked for me:
select {
     line-height:normal;
     padding:3px;
}

Answer: you can give :
select{
   position:absolute;
   top:50%;
   transform: translateY(-50%);
}

and to parent you have to give position:relative.
it will work.
Answer: Try to set the "line-height" attribute
Like this:
select{
    height: 28px !important;
    line-height: 28px;
}

Here you are some documentation about this attribute:
http://www.w3.org/wiki/CSS/Properties/line-height
http://www.w3schools.com/cssref/pr_dim_line-height.asp
Comment: Line-height doesn't work on the select elements and setting the height doesn't vertically center the text on ie8.
Answer: I used to use height and line-height with the same values, but the proved to be inconsistent across the interwebs. My current approach is to mix that with padding like so.
select {
  font-size:14px;
  height:18px;
  line-height:18px;
  padding:5px 0;
  width:200px;
  text-align:center;
}

You could also use padding for the horizontal value instead of the width + text-align
Answer: I've had the same problem and been working on it for hours. I've finally come up something that works.
Basically nothing I tried worked in every situation until I positioned a div to replicate the text of the first option over the select box and left the actual first option blank. I used {pointer-events:none;} to let users click through the div.
HTML

      <select>
        <option></option>
        <option>option 1</option>
        <option>option 2</option>
      </select>

        Select an option

CSS
.custom-select{position:absolute; left:28px; top:10px; z-index:1; display:block; pointer-events:none;}

Comment: Pointer events is unsupported by Internet Explorer (all versions), so I advise to not use this solution.
Answer: I found that only adding padding-top pushed down the grey dropdown arrow box on the right, which was undesirable.
The method that worked for me was to go into the inspector and incrementally add padding until the text was centered. This will also reduce the size of the dropdown icon, but it will be centered as well so it isn't as visually disturbing.
Answer: This has now been fixed in Firefox Nightly and will be in the next firefox build.
Please see this bug for more information https://bugzilla.mozilla.org/show_bug.cgi?id=610733
Answer: You can also try margin:
.select{
    margin-top: 25px;
}

Answer: The nearest general solution i know uses box-align property, as described here.
Working example is here (i can test it only on Chrome, believe that has equivalent for other browsers too).
CSS:
select{
  display:-webkit-box;
  display:-moz-box;
  display:box;
  height: 30px;;
}
select:nth-child(1){
  -webkit-box-align:start;
  -moz-box-align:start;
  box-align:start;
}
select:nth-child(2){
  -webkit-box-align:center;
  -moz-box-align:center;
  box-align:center;
}
select:nth-child(3){
  -webkit-box-align:end;
  -moz-box-align:end;
  box-align:end;
}


how can i get fonts for text from client pc using Java Applet?

Question: i am trying to get system fonts in applet form client side but it does not work for applet help required.
Answer: Are you trying to get fonts using
<code>GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = e.getAllFonts(); // Get the fonts
</code>
In this applet, it seems to work correctly.


VBA subtracting two dates and creating a new column

Question: I have a function, I would like it to subtract dates from me and create a new column. However, it does not work, it shows me an error here
.Value = Evaluate("if({1}," & .Offset(, -2).Address & "-" & .Offset(, -1).Address & ","""")")
Set shtResult = Sheets.Add.Name = "NPE"

But not help. Below is my code :

  With wbMe.Sheets("NPE").Range("G2", Range("A" & Rows.Count).End(xlUp).Offset(, 1))
     .Value = Evaluate("if({1}," & .Offset(, -2).Address & "-" & .Offset(, -1).Address & ","""")")
  End With

Comment: Does the formula you try evaluating work on a cell? What data are there in G:G column?
Comment: @FaneDuru In cell G, I wanted to subtract columns E and D
Comment: OK. I was not enough attentive. Placing that formula in E:E, does it return anything?
Comment: You really need to qualify the worksheet before `Range("A" & Rows.Count).End(xlUp).Offset(, 1)`.
Answer: Solution for above:
With wbMe.Sheets("Arkusz3").Range("G2:G" & Range("A" & Rows.Count).End(xlUp).Row)
      .Value = .worksheet.Evaluate("if({1}," & .Offset(, -3).Address & "-" & .Offset(, -2).Address & ","""")")
   End With


Interview question of biased coin

Question: Here is a question of quantitative interview:
A, B are biased coins. Now we toss 100 times of A or B to determine which one has the larger probability of head. What's the  optimal strategy?
Actually i don't much understand the word optimal, for my opinion, 50 times toss A and 50 times toss B then the one with large frequency of head has the large probability of head.
Comment: The answer depends entirely on what you want to optimize. For example, treating this as a bandit problem (as others have suggested) tries to maximize the number of times you flip the 'greater' coin. On the other hand, if you want to minimize uncertainty about the probabilities (i.e. maximize information gained about the parameters), you could treat this as an active learning problem.
Comment: sounds like a multi-armed bandit question
Answer: Nothing frustrates me more than when someone tells you to do the "optimal" thing without telling you the criteria over which to optimize.  That being said, I'm betting that since it was an interview, they intended for you to determine what you wanted to optimize for.
Your approach might not be "optimal" if we wanted to optimize for statistical power.  If the difference in bias is small, 50 flips might not be sufficient to detect which coin has larger bias.
I suspect they were hoping you knew about bandit algorithms.  Given the constraint on flips and the goal of learning the coin with the largest bias, this sounds like an AB test one might run in industry. One way the algorithm is run is as follows:

Start with uniform beta priors  on each on the biases of the coin
Draw from those priors and select the coin who's draw was largest.
Flip the coin and update the priors (now posteriors)
Repeat

Here is a python implementation of the bandit.  The two coins have a bias of 0.4 and 0.6 respectively.  The bandit correctly identifies that coin 2 has the larger bias (as evidenced by the posterior concentrating on larger biases.
import numpy as np
from scipy.stats import beta, binom
import matplotlib.pyplot as plt

import numpy as np
from scipy.stats import beta, binom
import matplotlib.pyplot as plt

class Coin():

    def __init__(self):
        self.a = 1
        self.b = 1
    def draw(self):
        return beta(self.a, self.b).rvs(1)
    def update(self, flip):
        if flip>0:
            self.a+=1
        else:
            self.b+=1
    def __str__(self):
        return f"{self.a}:{self.b}={self.a/(self.a+self.b):.3f}"

#Unknown to us
np.random.seed(19920908)
coin1 = binom(p=0.4, n=1)
coin2 = binom(p=0.6, n=1)

model1 = Coin()
model2 = Coin()

for i in range(100):

    draw1 = model1.draw()
    draw2 = model2.draw()

    if draw1>draw2:
        flip = coin1.rvs()
        model1.update(flip)
    else:
        flip = coin2.rvs()
        model2.update(flip)

x = np.linspace(0,1,101)

plt.plot(x, beta(model1.a, model1.b).pdf(x))
plt.plot(x, beta(model2.a, model2.b).pdf(x))
print(model1,model2)

Comment: As the number of trials is fixed, this is not a bandit problem. You provided no argument for why flipping both 50 times isn't optimal, or why you're using the algorithm you're  using. And on a programming note, putting the same line of code in both branches of an if-then-else block is redundant.
Comment: "Nothing frustrates me more than when someone tells you to do the "optimal" thing without telling you the criteria over which to optimize." The question says "Now we toss 100 times of A or B to determine which one has the larger probability of head." This clearly states that the objective is to determine which one has the larger probability of a head. I don't see any ambiguity. "Optimal" means to maximize the probability of identify the coin with the higher probability.
Comment: The idea behind using the beta.rvs() is that you flip a coin with a probability proportional to its probability of being the coin with the largest probability to land on heads. It works with more than two coins/bandits, and is known as Thompson sampling.
Comment: I don't think this answers the question, for the reasons @Acccumulation also mentioned. Your proposed strategy seems to optimize the number of tosses of the "best" coin (largest p(head)), while our objective is to maximize our confidence that we identified the best coin. It's not obvious that we will do that by mostly flipping the coin that currently seems most promising. As the blog linked by John points out, regular Thompson sampling does not optimize this confidence (you can modify TS to explore more, but it's not clear to me that this optimizes the identification objective either).
Comment: `Ba+=1 A = beta(Aa, Ba)` here should `Ba` be `Ab`? And why do we use Beta distribution and could you explain the meaning of updating the parameter of Beta?
Comment: Having given the reference, I also doubt that TS is the optimal approach. As I understand it, TS is a balance between improving information and maximising, in this case, the number of heads. Since we only want the former, the heuristic is not clearly appropriate.
Comment: the last question why do we flip with the larger draw?
Comment: there is still an error in the code, line 29, else: Ba +=1 should be changed to else: Ab +=1
Comment: @john I've updated the code.
Comment: @John -- From reading the https://en.wikipedia.org/wiki/Thompson_sampling page and the https://web.stanford.edu/~bvr/pubs/TS_Tutorial.pdf ref I see that using the expectation is the 'greedy' method and the beta sampling is Thompson method.  The beta sampling helps narrow the uncertainty around the central estimate.  With the greedy expectation rule, the process may never sample coin1 if coin2's expectation stays above the priors.  For example, the code with a seed of 19920908+1.
Comment: many thanks. What's the bias of a coin? An how do you know 0.4 and 0.6?
Comment: Biases were 0.4 and 0.6 because I set them to be so.
Comment: Yes, that's just a typo.  I fixed it.  I'm not going to go into the details of why the beta was used.  The beta binomial model is probably the most popular model for motivating Bayesian statistics.  Its found very often on this forum and elsewhere.  A great place to start would be Bishop's "Pattern Recognition and Machine Learning".  This example should come up in the first few chapters.
Comment: @user6703592 You'd have to read more about the bandit algorithms for the rationale.
Comment: That's an important point about optimality being w.r.t. to a chosen objective. Along these lines, it would be nice to include a note about what your algorithm is optimizing.
Comment: separately, and to answer the actual question, I understand from your reply that the "optimal" strategy is to use beta.rvs(), i. e. to randomly toss another fair coin to decide which coin to toss? that doesn't make sense to me, can you explore that?
Comment: Thanks for the reference on Thomson Sampling
Answer: in addition to the prior reply and useful comments, and to answer the actual question, the best approach is leveraging Thomson Sampling, there is an excellent article  on sudeepraja's blog.
it iteratively samples from the current posterior, selecting the highest mean.

Interview question of biased coin


Syncing Google Sheets to Calendar App Script

Question: I have the below script and keep getting the error 'Cannot find method createEvent(number,string,string).' I can't figure out what's wrong. Any ideas?
function createCalendarEvent() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2;
  var numRows = sheet.getLastRow();
  var numColumns = sheet.getLastColumn();
  var dataRange = sheet.getRange(startRow, 1, numRows-1, numColumns);
  var data = dataRange.getValues();
  var complete = "Done";

    for (var i = 0; i < data.length; ++i) {
    var row = data[i];
    var aname = row[2]; //Agent
    var cname = row[3]; //Client Name
    var country = row[4]; //Client Country
    var phone = row[5]; //Phone Number
    var deposit = row[6]; //Type of Deposit
    var ewallet = row[7]; //Processor/eWallet
    var method = row[8]; //Deposit Method
    var amount = row[9]; //Amount/Percent
    var date = new Date(row[10]);  //Date of Next Contact
    var target = row[12]; //Target Amount
    var history = row[13]; //Deposit History incl. dates
    var currency = row[14]; //Currency
    var comments = row[15]; //Comments
    var stime = new Date (row[17]);
    var etime = new Date (row[18]);
    var added = row[19]; //Added to calendar

      if (added != complete) {
      var currentCell = sheet.getRange(startRow + i, numColumns);
      var event = CalendarApp.getDefaultCalendar().createEvent(cname, stime, etime);

      currentCell.setValue(complete)};
}
}

Comment: Check your variables `starttime` and `endtime`, seems to me like they're being treated as strings because of your use of the `+` operator. These need to be dates for them to work with `createEvent()`.
Answer: This error means that it cannot find that method, probably because you're using wrong arguments to the function createEvent(String,Date,Date) (see documentation here).
You are using function createEvent(number,String,String), which doesn't exist.
Your first variable cname should be transformed to string, and what you are also seing in the error is that 2nd and 3rd parameter are as string, while they should be dates.
The proper solution would be to transform starttime and endtime to date, and that depends on how is the format of your string date.
Comment: Thanks, I've corrected it, and it doesn't throw up any errors now. However, it currently doesn't create the event in the calendar, but it does update the sheet to say 'Done'. The code above has been amended to reflect the changes.


If 8 workers finish the work for 12 days, for how many days the same work will be finished from 4 workers?

Question: If 8 workers finish the work for 12 days, for how many days the same work will be finished
from 4 workers?
I think that the correct answer is 24 days but in a book where I copied this question the correct question that is marked is 6.
Thank you
Comment: BTW, $\,24\,$ days is the correct answer...
Comment: Depends on the kind of work. If the work is to wash every worker, $4$ workers will still take $12$ days. If the work is to shake every other worker's hand, $4$ workers will only take $\frac{3}{7}\cdot 12$ days.
Comment: @Enve: Even before starting to solve anything, that answer doesn't make any sence: more workers should do the job in less time (unless the additional 4 workers just prevent the others from working...)
Comment: @DennisGulko did you mean sense
Answer: Hints:
(1) In one day eight workers do $\,1/12$-th of the total work, so each workers does
$$\frac{\frac{1}{12}}{8}=\frac{1}{96}\,\text{- th of the work each day}$$
(2) Four workers thus do
$$4\frac{1}{96}=\frac{1}{24} -\text{- th of the work each day}$$
so how many days do they need to finish the work?
The above is the general setting for these problems. In the present case there is, of course, a much simpler approach: $\,4\,$ workers will finish the work in twice the time as $\,8\,$ workers...
Comment: this is a university sample... look at the 9 question https://docs.google.com/file/d/0ByK43DGHmBeYVlFYNmFGRVRqdFk/edit
Comment: Well, I guess anyone can call anything "university" or whatever exercises. The ones in that link are junior high school and high school mathematics, perhaps (and hopefully!) for people who is *not going* to study sciences in that university.
Comment: you are right, this is a test to get merit scholarship and issued by the university, that was the reason I called it "university". I didn't said that the mathematical tasks are for university students


Square and Rectangle Inheritance

Question: This is a question in-regards to basic inheritance in Java with two classes.
We have two classes, with the first one being a Rectangle:
private double length;
private double width;

public Rectangle(double length, double width)
{
    this.length = length;
    this.width = width;
}

Next we have an extension class called Square, which extends Rectangle, so through super() we know that it uses the constructor of the Rectangle class.
private double side;

public Square(double side)
{
    super(side, side);
    this.side = side;
}

public void print()
{
    System.out.println("I am a square of side " + side);
}

This is our main:
Square b = new Square(6.0);
Rectangle c = (Rectangle) b;
c.print();

We create a object of the type Square, which would contain two side variables the double 6.0
Next, we cast b to Rectangle c, which is where my question comes in.
Why would c.print() print out I am a square of side 6.0?
Comment: @cagirici not sure if this is the right time in OPs learning stage to learn about good practices and design patterns -- since he's only now understanding the language (and perhaps even general OOP).
Comment: What do you expect it to print?
Comment: That won't even compile
Comment: What's the point of using inheritance if all your class members are `private`?
Comment: For readers (including people who will try to answer this question): Assumming `print` is a non-virtual method declared in `Rectangle` class, then this code would have behaved as defined in `Rectangle` class. `virtual` allows polymorphism, and in Java all methods are virtual by default.
Comment: Unfortunately guys, this is an example taken from my basic Java practices. No arguments can be done on whether practice A or B is better, or why this code is sampled; it's all dictatorially asserted that "here is an example, tell me what it does". Welcome to the American CS education.
Comment: @theGreenCabbage Educational systems in most places have their own handicap. In some fields they have an advantage big enough to ignore the handicap (hardware technology, medicine, etc. they all require expensive learning "tools"). In some theoretical fields they have the advantage of having already digested the theory for you. But in CS the can offer you little hardware support which you don't already have access to, and little pre-digested theoretical support.
Comment: @theGreenCabbage none of us would be here in the CS field without passion, curiosity, individual research and lots of trial and error.
Answer: This assumes Rectangle declares a print() method.
This action
Rectangle c = (Rectangle) b;

doesn't do anything to the instance referenced by b. It only widens the reference variable b to its super type Rectangle.
Invoking print() will have polymorphic behavior and, at runtime, the implementation of Square will be used since c is referencing a Square object, which has set size to 6.
Square b = new Square(6.0);
...
private double side;

public Square(double side) {
    super(side, side);
    this.side = side;
}

Note that this is the expected behavior in Java since all methods are virtual by default. In other languages like C++ and C#, your cast would have worked since print method in Rectangle isn't declared as virtual.
More info:

Java inheritance vs. C# inheritance
Comment: @theGreenCabbage yes, you can assume that **for Java**.
Comment: Does this mean that even though `c` is the object-type `Rectangle`, it still references `b` because, as you said, `c` is referencing a `Square` object?
Comment: @theGreenCabbage You have to make the distinction between a variable and an instance. A variable references an instance. The variable has a declared type (or static type) and references an instance which has a dynamic type (runtime type). Since a `Square` is a `Rectangle`, you can reference it with a variable with a declared type of `Rectangle`, but the object is still a `Square`.
Comment: @theGreenCabbage this is the correct answer, but I've provided another answer to complement this info.
Comment: Could we assume that no matter how many extension classes we have, using cast would not change the behavior of that super class?
Comment: `Square b = new Square(6.0);`
`Dodecahedron c = (Dodecahedron) b;` Printing the above two lines would still return `Square is side 6.0` is that right?
Comment: @theGreenCabbage That's right. The lowest implementation in the inheritance hierarchy will be invoked. (I assume here `Dodecahedron` is really high in the hierarchy.)
Comment: @theGreenCabbage Ok wait. If `Dodecahedron` is a sub class of `Square`, what you just did with throw a `ClassCastException` because a `Square` is not a `Dodecahedron`.
Comment: @theGreenCabbage by the way, next time try to choose a better example. `Rectangle` and `Square` inheritance are a bad example because they don't pass the [Liskov substitution principle](http://en.wikipedia.org/wiki/Liskov_substitution_principle#A_typical_violation).
Answer: This is polymorphic behavior. The actual type of the class determines the method that is called. Square's version of print will be called since the actual type of the object is Square.
Answer: Polymorphy
In (Java) inheritance that is the intended behavior -- polymorphy -- it's a way for you (the developer) to design an application around a concept (rectangles) and allow other related concepts (squares) to be used in places where the original concept (rectangle) is used but with their own (square) behavior.
Practicality
Imagine you'd have a list or array of rectangles and you'd fill it with objects received from returns of functions from outside your own package. Then you'd iterate over the list and ask each object to do things -- it is normal to want those objects to behave as what they really are, not as what they're filling in for.
If you ask a rectangle what its area is it will multiply length and width and return the result. If you don't override that in the square class it will do the same thing but you could override it and it could calculate its area as Math.pow(this.side, 2).
How about this inheritance chain:
Shape > Polygon > Quadrilateral > Parallelogram > Rectangle > Square

You would definately need to implement different area calculation methods -- wouldn't you want each object to behave as its own underlying structure tells it to (instead of behaving like the type it's been cast to)?
Answer: Remember that c is just a reference to an object in memory. The object being referenced is of dynamic type Square, despite the reference c being of static type Rectangle. The distinction between static and dynamic typing is crucial to this example.
The static type associated with a reference to an object is what the compiler uses to ensure that our code makes sense from a type perspective. When one calls a method on an object, however, which method is called depends on the dynamic type of the object, i.e. the type of object actually being referenced at runtime. This is called dynamic binding.
It also bears mentioning that the Rectangle-Square example is a canonical one. It is usually used to illustrate how thinking of types and supertypes as categories and generalizations can be misleading. Following the Liskov substitution principle requires writing the classes the other way, with Rectangle extends Square. This is another important idea of Object-Oriented Programming being touched on.
Answer: Because of inheritance, every Square is also a Rectangle. When you cast it to shove a Square in a Rectangle variable, it retains its Squareness. In this example, either a Square or a Rectangle can be put in your c. Polymorphism means that the method on whatever class it really is will be used.
Answer: I think this is the modification you need:
public static class Rectangle
{
    protected double length;
    protected double width;
    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }
    public Rectangle(Rectangle r)
    {
        this.length = r.length;
        this.width = r.width;
    }
    public void print()
    {
         System.out.println("I am a rectangle of length " + length + " and width " + width);
    }
}
public static class Square extends Rectangle
{

    public Square(double side)
    {
        super(side, side);

    }

    public void print()
    {
        System.out.println("I am a square of side " + length);
    }
}

public static void main(String[] args)
{
    Square b = new Square(6.0);
    Rectangle c = new Rectangle(b);
    c.print();
}

Your Square class has nothing to do with Rectangle class. It has a different attribute. If you are using inheritence, base class should inherit something.
Moreover, there is no print() method in Rectangle.
A simple copy constructor, making your base class members protected and deleting the unnecessary derived class members should work.

Square and Rectangle Inheritance


How to solve this trigonomectric equation $2^{\lvert\sin x\rvert} + 2^{\lvert\cos x\rvert} = 3$?

Question: I thought its solutions are $(\sin x;\cos x) = \{(\pm1;0),(0;\pm 1)\}$
But how can I solve it?
Please help :(
Comment: Hint: For an idea, try to graph it.
Comment: The solutions are $k\frac{\pi}{2},\forall k\in\mathbb{Z}$ by direct exploration. There is no algebraic way to solve this equation
Answer: Give the periodicity and symmetries of the trig functions, it suffices to show that $f(u)=2^u+2^{\sqrt{1-u^2}}$ is increasing for $0\lt u\le1/\sqrt2$.  That will imply that the only solutions to $2^{|\sin x|}+2^{|\cos x|}=3$ occur at multiples of $\pi/2$.
We have
$$f'(u)=\ln2\left(2^u-{u\over\sqrt{1-u^2}}2^{\sqrt{1-u^2}}\right)$$
so it's enough to show that
$${2^u\over u}\gt{2^{\sqrt{1-u^2}}\over\sqrt{1-u^2}}$$
for $0\lt u\lt1/\sqrt2$. But notice that in this range we actually have $0\lt u\lt1/\sqrt2\lt\sqrt{1-u^2}\lt1$ (i.e., $0\lt\sin x\lt1/\sqrt2\lt\cos x\lt1$ for $0\lt x\lt\pi/4$). So we need only prove that the function $g(u)=2^u/u$ is decreasing for $0\lt u\lt1$. This is equivalent to showing that $h(u)=(\ln2)u-\ln u$ is decreasing on the same range, which is clear since
$$h'(u)=\ln2-{1\over u}\lt0\iff0\lt u\lt{1\over\ln 2}$$
and $\ln2\lt1$.
Comment: (+1) I restored an accidentally omitted square-root sign. Your answer is more straightforward than mine.
Comment: @JohnBentin, thank you!
Comment: Excuse me, please help me explain why $h(u) = \ln2 u - \ln u$
Comment: @PhướcVinh, $h(u)=\ln(g(u))$.
Answer: Clearly the function $f:\Bbb R\to\Bbb R:x\mapsto f(x):=2^{|\!\cos x|}+2^{|\!\sin x|}$ is periodic with period $\frac12\pi$. Also clear is that $f(0)=f(\frac12\pi)=3$, and $f(x)\equiv f(\frac12\pi-x)$. So we look to see if there are any solutions to $f(x)=3$ in the range $0<x<\frac12\pi$. It is convenient to transform the variable by writing $t:=x-\frac14\pi$ and $\alpha:=\frac12\surd2\ln2$, so that our function takes the form $$f(x)=g(t):=2\exp(\alpha\cos t)\cosh(\alpha\sin t)\quad(-\tfrac14\pi\leqslant t\leqslant\tfrac14\pi).$$Differentiation gives $$g'(t)=2\alpha[\cos t\sinh(\alpha\sin t)-\sin t\cosh(\alpha\sin t)]\exp(\alpha\cos t),$$from which it is clear that $f'(t)=0$ only when $$\tan t=\tanh(\alpha\sin t).$$Now $\sin$ is concave in the interval $[0\;\pmb,\,\frac14\pi]$ and $\tanh$ is concave in $[0\;\pmb,\,\infty)$, so the RHS of the above equation is concave in $[0\;\pmb,\,\frac14\pi]$ too ; at the same time, $\tan$ is convex in the same interval. So there is no solution to the equation beyond $0$ in the interval. By a similar argument, with convexity and concavity interchanged, there is no solution in $[-\frac14\pi\;\pmb,\,0)$ either. Thus $g$ has just the one stationary point at $0$, namely when $g(0)=2\mathrm e^\alpha\approx3.265>3$. Therefore $g$ decreases from this maximum at $0$ to a minimum value of $3$ at $\pm\frac14\pi$.
It follows that the only solutions to the equation $f(x)=3$ are of the form $x=\frac12k\pi\; (k\in\Bbb Z).$
Answer: For simplicity we have to solve for $x\ge 0$ $$f(x)=2^x+2^{\sqrt{1-x^2}}=3$$.
Note that $$f'(x)=2^x\log 2-\frac{2^{\sqrt{1-x^2}}x\log 2}{\sqrt{1-x^2}}$$
if   $x\le \frac{1}{\sqrt{2}}$ then $x\le \sqrt{1-x^2}$
then we have
Lemma: $$\frac{2^x}{x}\ge \frac{2^{\sqrt{1-x^2}}}{\sqrt{1-x^2}}$$
proof: show that $g(x)=\frac{2^x}{x}$ is decreasing in given interval.
From this lemma it follows that $f'(x)\ge 0$.Thus for all $x\in (0,\frac{1}{\sqrt{2}}]$ :$f(x)>f(0)=3$
Similarly prove that $f(x)$ is decreasing for all $x\in [\frac{1}{\sqrt{2}},1]$ hence deduce that $f(x)>f(1)=3$ for all $x\in [\frac{1}{\sqrt{2}},1)$.
The conclusion is now obvious:i.e $x=0,x=1$ are the only solutions.
Comment: I exaggerated, sorry
Comment: Your $x$ are not the $x$ of the original question. The solutions of the original question are $x=k\frac{\pi}{2}$ as I wrote in my twice downvoted answer. Furthermore why all the lemma gibberish? The solutions are $x=0,x=1$, it is obvius. Too bad that you are solving a different equation
Comment: @Raffaele yes i know that i replaced |sinx| by x ,  i left that for the OP to fill in
Comment: And why my solution was downvoted TWICE? Without any comment
Comment: @Raffaele i think you had to prove why they were the only solutions.OP has already guessed that
Comment: My solution is almost the same as @Datboi without the useless derivative thing
Comment: @Raffaele agreed Datboi's method does not help but isn,t my solution correct
Comment: Your "solution" is cheating because you already knew the solutions. Try and solve this $3^{\sin x}+2^{\cos x}=4$
Comment: @Raffaele even if the OP hadnt said it, it is still an easy guess following $2+1=3$,
Comment: Try and solve this $3^{\sin x}+2^{\cos x}=4$
Comment: @Raffaele if one goes off like that there are many unsolved problems by hand,this problem has been asked due to the elegance of its solution (i think)
Comment: @Raffaele also in response to 'cheating' many functional equations get solved by first spotting any obvious solution and then proving that no more exist.If that is also called cheating...i dont know
Comment: I have deleted my answer in response to  comments like "does not help" and "useless derivative thing"
Comment: @DatBoi so, that was my opinion maybe your solution was good and i was blind not to guess your idea.That shouldnt make you delete your answer.If ytou are convinced keep it!


Diffusion Models - modeling noise?

Question: In this post about diffusion models, IIUC, we want to use a neural network to approximate the mean of the reverse diffusion: $p_\theta(\mathbf{x}_{t-1} \vert \mathbf{x}_t) = \mathcal{N}(\mathbf{x}_{t-1}; \boldsymbol{\mu}_\theta(\mathbf{x}_t, t), \boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t))$. That is, we train a neural network to predict the quantity $\tilde{\boldsymbol{\mu}}_t = \frac{1}{\sqrt{\alpha_t}} \Big( \mathbf{x}_t - \frac{1 - \alpha_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_t \Big)$.
Instead, according to the article, since $\mathbf{x}_t$ is available at training time, we predict $\boldsymbol{\epsilon}_t$ instead.
But according to the forward diffusion process, we have:
$
\begin{aligned}
\mathbf{x}_t
&= \sqrt{\alpha_t}\mathbf{x}_{t-1} + \sqrt{1 - \alpha_t}\boldsymbol{\epsilon}_{t-1} & \text{ ;where } \boldsymbol{\epsilon}_{t-1}, \boldsymbol{\epsilon}_{t-2}, \dots \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) \\
&= \sqrt{\alpha_t \alpha_{t-1}} \mathbf{x}_{t-2} + \sqrt{1 - \alpha_t \alpha_{t-1}} \bar{\boldsymbol{\epsilon}}_{t-2} & \text{ ;where } \bar{\boldsymbol{\epsilon}}_{t-2} \text{ merges two Gaussians (*).} \\
&= \dots \\
&= \sqrt{\bar{\alpha}_t}\mathbf{x}_0 + \sqrt{1 - \bar{\alpha}_t}\boldsymbol{\epsilon} \\
q(\mathbf{x}_t \vert \mathbf{x}_0) &= \mathcal{N}(\mathbf{x}_t; \sqrt{\bar{\alpha}_t} \mathbf{x}_0, (1 - \bar{\alpha}_t)\mathbf{I})
\end{aligned}
$
So $\boldsymbol{\epsilon}_t$ is a random sample from the standard normal. I don't fully understand this; why are we trying to train a neural network to predict on the noise? If this is a random draw from a normal that is independent of any covariates, isn't this also available at training time, and so we can just directly compute a sample?
Answer: TLDR; the noise $\boldsymbol{\epsilon}_\theta$ is the only thing we need learn so we can model reverse diffusion process. We can use $\boldsymbol{\epsilon}_\theta$ to get mean and variance of the Gaussian distributions we are looking for!
I also had the exact same question when I encountered the DDPM paper! I think we need to clarify a few things to understand the purpose of predicting the noise!
1- Why are we learning the noise?

So in diffusion models (including DDPM), given the forward diffusion (i.e., $q(x_t | x_{t-1})$), our goal is to learn how to reverse that process (i.e., $p_\theta(\mathbf{x}_{t-1}|\mathbf{x}_t)$) with a neural network.
It turns out after doing lots of math, $p_\theta$ is Guassian in the form of $p_\theta(\mathbf{x}_{t-1} \vert \mathbf{x}_t) = \mathcal{N}(\mathbf{x}_{t-1}; \boldsymbol{\mu}_\theta(\mathbf{x}_t, t), \boldsymbol{\Sigma}_\theta(\mathbf{x}_t, t))$. Hence, all we need to do is to find $\mathbf{\mu_\theta}$ and $\mathbf{\Sigma_\theta}$
Now, we can do the math and see:
$$\begin{aligned}
\boldsymbol{\mu}_\theta(\mathbf{x}_t, t) &= \color{black}{\frac{1}{\sqrt{\alpha_t}} \Big( \mathbf{x}_t - \frac{1 - \alpha_t}{\sqrt{1 - \bar{\alpha}_t}} \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t) \Big)}
\end{aligned}$$
where $\mathbf{\alpha}_t$ depends on noise values and non-trainable. So the only part we actually need to learn, is $\boldsymbol{\epsilon}_\theta$.
Once we learn $\boldsymbol{\epsilon}_\theta$, we can put it in the equation and get $\mathbf{p}_\theta$

2- If the noise is independent of data, why/how can we learn it?

Well, I think given the algorithm 1 below, all our NN has to do is to predict $\mathcal{N}(0, I)$. But that's not possible. We can't predict random noise.

However, there's a catch! I think the confusion comes from the fact that we don't want to learn a random noise. We want to learn the noise component, given a noisy input!  So the input to our model is image + noise, and we want to learn the noise part.

So let's see a pseudo-code of DDPM for clarification:
def train_loss(denoise_model, x_0, t):
    noise = torch.randn_like(x_0)

    x_noisy = q_sample(x_0=x_0, t=t, noise=noise)
    predicted_noise = denoise_model(x_noisy, t)

    loss = F.l2_loss(noise, predicted_noise)

    return loss

We can see that the model is trained to denoise a noisy input in the code.
Comment: @NathanB I also didn't understand why the noise was added at each step. I wrote an answer in https://stats.stackexchange.com/a/624237/394779
Comment: But why is noise added at each timestamp during the reverse process?
Comment: So during the forward process, we add noise step by step. In the reverse process, we are trying to do the exact opposite. So at each step, we try to denoise.

The reason that we add noise at each step is that it will be extremely difficult to directly find a mapping between noise and real data. GANs try to do the same thing and their training is very unstable/difficult.
Comment: What will happen if we don't add this noise at every backward step?
Comment: I don't think I fully understand what you mean. If we're trying to get rid of the random noise component in the reverse process, why do we add a random noise component in every step of the backward process?
Comment: @NathanB
The reason is that (at least in the original DDPM paper) your NN is not learning to predict directly the noise, it learns to predict the *distribution* of the noise - so for example, in the case of an image, it will output a mean (or a mean and variance, depending on the implementation) of the (usually) Gaussian distribution. And then, you can use those values to sample randomly from that distribution.
This is essentially similar/the same as the "Reparameterization Trick" that is used in Variational Auto Encoder.
Answer: We begin by highlighting key facts and details about diffusion models:

The forward diffusion process is a step-wise deformation of the data
distribution caused by the injection of additive noise at each time
step.
The forward process assumes the latent dimension is exactly equal to the data dimension $x_{t+1} \equiv x_{t}$
The structure of the latent space of each time step is fixed as a linear Gaussian model (not learned) centered on the output of the previous time-step.
The transition probability kernel for a single time-step is $q(x_{t} \vert x_{t-1}) \: = \mathcal{N} \big( \: \sqrt{\smash[b]{\alpha_t}} \cdot x_{t-1} , \: (1 \: - \: \alpha_t) \cdot \mathcal{I} \: \big)$ where moments depend on the parameter $\alpha_{t}$ with mean $\mu=\sqrt{\smash[b]{\alpha_t}}$ centered on the previous timepoint and covariance $\Sigma=(1 \: - \: \alpha_t)$
To sample from the diffusion kernel distribution  $x_{t} \sim q(x_{t} \vert x_{t-1})$, we could use the re-parameterization trick, mathematically expressed as $x_{t} \: = \: \mu_{t} \cdot x_{t-1} \: + \: \sigma_{t} \cdot \epsilon_{t}$

These modeling constraints on the diffusion process labor the point that all that the encoding process $q(x_{t} \vert x_{t-1})$ requires is learning the $\mu_{t}$ and $\sigma_{t}^2$ of each latent Gaussian distribution.
Because each of these independent and sequential time-steps are analytically tractable (Gaussian), in consequence, so is the full chain as well. It can then be shown that the KL-divergence requiring optimization of the posterior is
$\underbrace{ \mathbb{E}_{ q(x_{1} \vert x_{0}) } \: \Big\lbrack \: \log p_{\theta}(x_{0} \vert x_{1}) \: \Big\rbrack }_{\text{reconstruction term}} \: + \: \underbrace{ D_{KL} \: \big( \: q(x_{T} \vert x_{0}) \: \Vert \: p(x_{T}) \: \big) }_{\text{prior matching term}}  \: - \: \sum_{t=2}^T \underbrace{ \mathbb{E}_{ q(x_{t} \vert x_{0}) } \: \Big\lbrack \: D_{KL} \: \big( q(x_{t-1} \vert x_{t}, x_{0}) \: \Vert \: p_{\theta}(x_{t-1} \vert x_{t}) \big) \: \Big\rbrack  }_{\text{denoising matching term}}$
With some math, the main computational challenge comes from the third KL-divergence term which is shown to be equal to
$D_{KL} \big( \: q( x_{t-1} \vert x_{t}, x_{0}) \: \big\Vert \: p_{\theta}(x_{t-1} \vert x_{t} ) \: \big) \: = \frac {1} {2 \sigma_{q}^2 (t)} \Big\lbrack \: \big\Vert \: \mu_{\theta} \: - \: \mu_{q} \: \big\Vert_{2}^2  \: \Big\rbrack$
where $\mu_{q}$ and $\mu_{\theta}$ are shorthand for $\mu_{q}(x_{t}, x_{0})$ and $\mu_{\theta}(x_{t}, t)$ respectively. Examination of this result explains why diffusion models train a parameterized neural network model to learn to predict the original training image $x_{0}$ from noisy image $x_{t}$ and sampled time index $t$. By extension, a simple application of the re-parameterization trick permits modeling the noise instead $\epsilon_{\theta}(x_{t}, t)$.
In summary, with a few modeling constraints, the forward diffusion (encoding) process serves as a way to calculate the parameters of the true posterior for the reverse (decoding) process for each time-step $p_{\theta}(x_{t-1} \vert x_{t})$.
The following excerpts are taken from my book on variational inference. Learn more on the topic by visiting https://www.thevariationalbook.com/

Diffusion Models - modeling noise?


Unique pair of positive real numbers satisfying $x^4 -6x^2y^2 + y^4 = 8$ and $x^3y - xy^3 = 2\sqrt{3}$

Question: (Mandelbrot) There is a unique pair of positive real numbers satisfying the equations
\begin{equation}
x^4 - 6x^2y^2 + y^4 = 8 \hspace{1em} \text{and} \hspace{1em} x^3y - xy^3 = 2\sqrt{3}\text{.}
\end{equation}
Determine $x$, writing your answer in the form $a \cos\theta$, with $\theta$ in degrees.
My answer is written below, but I would love to see any easier solutions!
I would like to note that this problem came from a problem set given to me in one of my classes,  the day we were learning about complex numbers and De Moivre's theorem. It would be great if any of y'all could find a solution to this problem using complex numbers :D
Comment: What do you get  for $(x+iy)^4 $  in terms of real $x,y \; \; ? \; \; $
Comment: @WillJagy thank you! could you please post this solution in full so I can accept the answer?
Answer: $$(x+iy)^4 = (x^4 - 6x^2y^2 + y^4) + 4i(x^3y - xy^3)  $$
we get $(x+iy)^4 = 8 + 8i \sqrt 3.$  As $\frac{8 \sqrt 3}{8} = \sqrt 3$  we see that the angle with the $x$ axis is $\frac{\pi}{3}.$  the angle of our $x +iy$  must then be  $\frac{\pi}{12}.$ Lots of questions today about this angle, including sine, cosine, and tangent
Comment: I would also note that the angle of $ x + iy $, after using De Moivre's theorem, could also equal $ \frac{\pi}{12} + \frac{1}{2} k \pi $ where $ k \in \mathbb{Z} $ such that $ 0 \le k < 4 $. But, all possible angles other than $ \frac{\pi}{12} $ result in a negative $x$, so $ \frac{\pi}{12} $ is our answer (you can also check $y$ and find that it's positive).
Answer: \begin{align*}
&x^4 - 6x^2y^2 + y^4 = 8 \\
\implies &(x^2-y^2)^2 - 4x^2y^2 = 8\\
\end{align*}
and
\begin{align*}
&x^3y-xy^3 = 2\sqrt{3} \\
\implies &xy(x^2-y^2) = 2\sqrt{3} \\
\end{align*}
Let $x^2 - y^2 = a$, $xy = b$. Then,
\begin{align*}
a^2-4b^2 &= 8 \\
ab &= 2\sqrt{3} \\
\end{align*}
Solving,
\begin{align*}
&a^2 - 4(2\sqrt{3}/a)^2 = 8  \\
\implies &a^4 - 8a^2 - 48 = 0 \\
\implies &a^2 = 12, -4 \text{, so } a = \pm2\sqrt{3}, \pm 2i
\end{align*}
Clearly, $x$ and $y$ won't be real if $a = \pm2i$, so $a = \pm2\sqrt{3}$ and $b = \pm 1$. And, because $xy = b$ is positive, $b = 1$, which means $ 2\sqrt{3} / b = a$ is also positive, so $a = 2\sqrt{3}$.
Then, $xy = 1 \implies y = 1/x$ so $x^2 - 1/x^2 = a \implies x^4 - 2\sqrt{3}x^2 - 1 = 0$
Using the quadratic equation,
\begin{equation}
x = \sqrt{\frac{2\sqrt{3} \pm \sqrt{16}}{2}} = \sqrt{ \sqrt{3} \pm 2}
\end{equation}
Because $ \sqrt{3} < 2 $, $x = \sqrt{2 + \sqrt{3}}$. Using basic algebra and setting $ x = \sqrt{m} + \sqrt{n} $, we find $ (m, n) = (3/2, 1/2) $. So, $ x = \sqrt{3/2} + \sqrt{1/2} = \frac{\sqrt{6} + \sqrt{2}}{2} $.  One can also notice that $ \cos 15^{\circ} = \frac{\sqrt{6} + \sqrt{2}}{4} $, so $ x = 2\cos 15^{\circ}. \hspace{0.5em} \blacksquare$


É realmente necessário criar uma 3ª tabela auxiliar em relacionamentos N-N?

Question: Em vários locais eu vejo que quando se tem um relacionamento N-N (se não me falha a memória) recomenda-se criar uma 3ª tabela auxiliar e então posteriormente irá ser transformado em um relacionamento 1 - 1 (também se não me falha a memória).

Por que dizem que precisa fazer isso? Tem a ver com normalização?
É realmente necessário? O que se perde fazendo isso? E o que se ganha?
Essa recomendação é somente para MySQL?
Comment: Exemplos bons são reais, porque coisas inventadas assim terão como resposta que isto é errado. Uma das vantagens do relacional é poder enxergar os dados de várias formas diferentes simplificadamente e com performance. Essa tabela auxiliar serviria para isso. Se não precisa acessar os dados assim, não precisa dela.
Comment: Cliente - Pedido, acredito que não seja N-N, ou um pedido vai ter mais de um cliente ?
Comment: Relacionada: [O que é normalização de banco de dados?](https://pt.stackoverflow.com/q/151323/91)
Comment: O seu caso ai de `Cliente` e `Pedido` ser N:N é só em casos bem específicos no qual o sistema exige isso. Já as tabelas `Pedido` e `Produto`, pode ter um relacionamento N:N criando a tabela `PedidoProdutos`.
Comment: Sem a tabela, como você faria esse relacionamento?
Answer:
Por que dizem que precisa fazer isso?

Para ter ganhos, veja abaixo.

Tem a ver com normalização?

Sim.

É realmente necessário?

Não, mas quase. É muito difícil fazer certo sem essa tabela de associação, e em alguns casos inviável, ainda que possível.

O que se perde fazendo isso?

Necessidade de um JOIN é o principal

E o que se ganha?

Flexibilidade de consulta, performance, facilidade de manutenção, consistência, só pra citar o principal.

Essa recomendação é somente para MySQL?

Não.
Detalhes
Um bom exemplo é produtos X fornecedores. Um fornecedor muito provavelmente fornece vários produtos e é muito comum que um produto seja fornecido por vários fornecedores, principalmente no atacado e varejo, mas também na indústria quando o produto é commodity ou tem substituição perfeita.
Como você pode vincular ambos? Uma forma é colocar no próprio produto todos os fornecedores que podem fornecê-lo. Pode parecer esquisito, mas muitas vezes funciona porque costuma ser poucos fornecedores. Pelo menos em tabelas com tamanho variável isso não é um grande problema. Claro que possui desvantagens. O acesso não é tão simples, pode ter que fazer certos contorcionismos para pegar o que deseja, saber quem fornece o que, pode ter problemas de performance, inclusive porque fica muito dado junto que não é necessário complicando o cache e as leituras que serão mais frequentes.
Piora muito do outro lado. Se colocar todos os produtos que um fornecedor fornece pode ser um monstro, é comum fornecedores terem milhares de produtos.
Uma outra solução é repetir os cadastros, ou seja ter uma linha para cada produto e fornecedor que o fornece. Isto fere a normalização, cria duplicação de dados, pode ser uma situação até pior em termos de performance, consumo de memória, e malabarismo para se chegar onde quer.
A solução que costuma funcionar melhor é ter uma tabela de amarração onde basicamente consta a relação de fornecedor e produto. É pequeno, com os índices certos é de acesso rápido, não prejudica o cache, não carrega demais, não duplica, não é tão difícil consultar os dados assim, permite acessos dos dois lados de forma simples na maioria dos casos.
Claro que perde um pouco a localidade, exige algum JOIN na maioria das consultas, é preciso garantir que a atualização de dados seja feita corretamente nessa tabela também, mas não muito diferente das outras soluções, pelo menos é uma operação dentro dos padrões normais relacionais que precisamos.
Isso vale para qualquer banco de dados relacional.
Pensa como funciona isso em objetos na memória. Você terá no fornecedor uma lista com os produtos que eles fornece. E no produto terá uma lista com os fornecedores dele. Eles fazem parte do mesmo objeto? Ao contrário do que muita gente pensa, não faz, a lista é outro objeto. Note que provavelmente terá duas listas. Dá para fazer o mesmo no banco de dados, mas não vejo vantagem, dá trabalho resolver assim. É melhor uma tabela só com dois índices.
Eu sou um pouco crítico do uso de bancos de dados não relacionais porque a imensa maioria dos problemas que resolvemos com DBs são relacionais. Quando começam usar tecnologias não relacionais para isso trazem problemas não encontrados quando a tecnologia é relacional, aí para resolver começam criar outras tecnologias e metodologias para consertar a escolha inadequada da tecnologia.
Não cabe aqui, mas modelos não relacionais costumam ser úteis em uma parte do problema, raramente no problema todo, por isso com a evolução dos DBs relacionais para trabalhar melhor com dados não relacionais se torna uma solução perfeita. Mesmo problemas que possuem essa característica, muitas vezes são adaptados para se encaixar no modelo, muitas vezes prejudicando a experiência com o usuário, ainda que possa simplificar um pouco o desenvolvimento.
Então se o problema é esse, quase sempre terá que fazer assim, salvo se o problema possa ser resolvido de outra forma com tranquilidade. Mesmo que não precise desta tabela de amarração, quem sabe um dia precisará e aí terá que fazer profundas mudanças no banco de dados. Poucas aplicações estão preparadas para trabalhar com estruturas físicas de base de dados diferentes, o que até é um erro conceitual, mas aí não sei o quanto o pragmatismo tem que prevalecer mesmo.
Coloquei no GitHub para referência futura.
Answer: Primeiramente irei citar, com base em livros, conceitos sobre cardinalidades N x N para depois responder suas perguntas:
Segundo os autores,  quando relações entre duas tabelas forem N x N:

Leite, Mario (2008):

Na cardinalidade N - N é muito difícil para o programador converter essa semântica em código, pois isso, o que se faz é criar uma nova entidade (Entidade-Associativa) que se relacionará com as entidades origens com a cardinalidade N - 1.

Costa, Rogério Luiz de C. (2006):

Quando a cardinalidade é N - N deve ser criada uma nova entidade para representar o relacionamento [...] entre as entidades envolvidas no relacionamento.

Morelli, Eduardo Terra (2009):

Inicialmente todo relacionamento N - N tem que ser quebrados em relacionamento N - 1, mais uma tabela. Isto porque resulta impossível relacionamentos múltiplas (N - N ) em banco de dados real.

Considerações
Em nenhum momento nos livros citados foi explicado, de fato, porque devemos utilizar a criação de mais uma entidade em relacionamentos N - N.
Entretanto, pude perceber que, todos concordaram que adotar a tratativa de mais uma entidade reduz a complexabilidade para implementar a lógica, tanto na hora de codificar, quanto ao fato de salvar, manter e extrair as informações dos SGBD RELACIONAIS.
Agora sobre suas perguntas:
Porque dizem que precisa fazer isso? Tem a ver com normalização?
Você não precisa fazer a adição de mais uma tabela em relacionamentos N - N. Na minha opinião é mais uma questão de uma melhor tratativa com o intuito de diminuir a complexabilidade na hora de codificar e trabalhar os dados nos SGBD's relacionais.
É realmente necessário? O que se perde fazendo isso? E o que se ganha?
Na hora de criar o modelo de relacionamento de um projeto eu tenho como foco/tratativa ou melhores práticas, chame como quiser, criar um sistema que atenda a necessidade da maneira mais simples possível para que a manutenção seja, também, mais simples possível.
Sobre o que se perde ou ganha. Acredito que, com base nas citações, seja a produtividade na hora de codificar. Posso estar errado, mas relações 1 - N ou N - 1 são menos complexas para se tratar. Se eu usar a "convenção" de criar uma entidade para relações N - N que diminua minha complexabilidade, estarei ganhando em produtividade.
Essa recomendação é somente para MySQL?
Não.

Segundo Morelli a modelagem relacional (MER) foi criado
  para descrever os dados armazenados em tabelas.

Ou seja, essa convenção é para qualquer SGBD relacional.


Servlets not being included in the maven project

Question: Can some one help me with this problem. jetty is not able to find my servlet. :(
I am getting the following error-

This is my directory structure -

This is my Web.xml  -
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
   <display-name>Archetype Created Web Application</display-name>
   <servlet>
   <servlet-name>loginSer</servlet-name>
   <display-name>loginSer</display-name>
   <description></description>
<servlet-class>launchpad.servlets.loginSer</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>loginSer</servlet-name>
  <url-pattern>/loginSer</url-pattern>
  </servlet-mapping>
 </web-app>

Answer: You kept it in wrong directory, it should go under
src/main/java

not in
src/main/resource

Maven takes into compilati
Update

when I add it to /src/main/java eclipse cannot import HttpServlet class.

It is because you don't have servlet-api in yoru classpath
add dependency to your POM, replace ${servlet-api-version} with the appropriate version matching your app environment
dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>${servlet-api-version}</version>
</dependency>

Comment: Thank you but when I add it to /src/main/java eclipse cannot import HttpServlet class.
Comment: The /src/main/java folder was created by me. It did not come with default maven-webapp settings. am I supposed to set some permissions?
Answer: Your exception reads com.launchpad.servlets.LoginServlet but your class is launchpad.servlets.LoginSer Are you sure that you're showing the accurate exception and source-code hierarchy?


How to increase size of OpenLDAP MDB database? (MDB_MAP_FULL Error Code)

Question: I'm getting this error related to slapd service (OpenLDAP server) in syslog

mdb_idl_insert_keys: c_put id failed: MDB_MAP_FULL: Environment
  mapsize limit reached (-30792)

This error comes when I'm trying to add new users on OpenLDAP, I'm using MDB database and After searching for cause of this error I came to know my MDB database size if full and I need to increase the maxsize of database but how I do that? I'm using cn=config style configuration and my OpenLDAP version is 2.44.
Answer: Turns out I need to change value of olcDbMaxSize attribute in my database configuration file.
I tried below and it worked.
Step 1: Create ldif file(filename.ldif) with below contents
dn: olcDatabase={2}mdb,cn=config
changetype: modify
add: olcDbMaxSize
olcDbMaxSize: 1000000000

Note : olcDbMaxSize : size in bytes

Step 2: Import above ldif file to OpenLDAP
ldapmodify -x -W -D 'cn=config' -H ldap:// -f filename.ldif

Step 3:Restart slapd service (This may not be required in cn=config style configuration)
service slapd restart


What is the probability that exactly k tosses are required to get exactly 2 Heads

Question: I have the following homework problem: What is the probability of that exactly k tosses are required to get exactly 2 Heads
I wanted to validate that my approach to solving this is correct and if my answer is on the right track:
$$A = P(\text{only 1 H in first $k-1$ tosses}) \cdot P(\text{H in last toss})$$
$$A=P(\text{only 1 H in first $k-1$ tosses}) \cdot .5$$
Is this the correct approach? Also below is my answer; is this correct or am I way off?
$$
\frac{k-1}{2 ^{k-1}} \cdot .5
$$
Thanks!
Comment: Welcome to CV, Ethan. Your instincts are spot on.  1) When you ask what is obviously a homework question, you should report how far you got. 2) Your approach is correct. 1 heads in $k-1$ tosses and a heads on the last toss. So two things: 1) please use the self-study tag when you are asking about a homework problem, and 2) please try to master $\LaTeX$, including the binomial coefficient, so others will be able to see more easily how you are doing your counting.    $\LaTeX$ *will take you places!*
Comment: Notice that your answer (+1), which simplifies to $(1-k)(1/2)^k,$ matches the result in my Answer. So your statement of the problem matches the formulation of the negative binomial distribution I chose in my Answer.
Comment: On the mathematics front (specifically MathJax's subset of LaTeX), see [MathJax basic tutorial and quick reference](https://math.meta.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference)
Comment: @PeterLeopold Thank you so much for all of the feedback. I will make sure to do both things you recommend. Kind Regards, Ethan
Answer: This problem is related to the negative binomial distribution, which has several versions (see the link under 'alternative formulations'). You should compare the various formulations with your text
to avoid confusion. For this Answer I use a formulation used in many
elementary texts.
In a sequence of independent Bernoulli trials with Success probability $p,$ let $X$ count the the number of trials $x = r, r+1, \dots$ until $r$ Successes have been observed.
Before the last trial (a Success), there must have been $x-1$ trials with $r - 1$ Successes. There are ${x-1 \choose r-1}$ possible
arrangements of Successes and Failures each with probability $p^{r-1}(1-p)^{x-r}.$ The final trial must be a Success with probability $p$. Thus
$$P(X = x) = {x-1 \choose r-1}p^r(1-p)^{x-r},$$
for $x = r, r+1, \dots .$
In your problem, $r = 2, p = \frac 12,$ so that
$$P(X = x) = (x-1)\left(\frac 12\right)^2\left(\frac 12\right)^{x-2}
= (x-1)\left(\frac 12\right)^x.$$
According to this formulation of a negative binomial random variable $X,$
we have $E(X) = \frac rp = \frac {2}{1/2} = 4$ and
$Var(X) = \frac{r(1-p)}{p^2}= 4,$ which can be found using moment generating functions (or some other argument using differentiation).
For your problem, we can use R to sum the first 100 terms of (three)
infinite series to get good approximations:
x = 2:102; pdf = (x-1)*(.5)^x
sum(pdf)
[1] 1                  # 100 terms include almost probability 1
mu = sum(x*pdf); mu
[1] 4                  # for E(X), aprx with 100 terms very close
sum(x^2*pdf) - mu^2
[1] 4                  # also for Var(X)

Note: The formulation of the negative binomial in R with functions dnbinom, and so on (not used above), counts the number of Failures before the $r$th Success. Examples:
mean(rnbinom(10^6, 2, .5) + 2)  # mean of sample of size 1 million
[1] 4.001245
x = 2:12; pdf = (x-1)*(.5)^x;  pdf
 [1] 0.250000000 0.250000000 0.187500000 0.125000000 0.078125000 0.046875000
 [7] 0.027343750 0.015625000 0.008789062 0.004882812 0.002685547
dnbinom(0:10, 2, .5)
 [1] 0.250000000 0.250000000 0.187500000 0.125000000 0.078125000 0.046875000
 [7] 0.027343750 0.015625000 0.008789062 0.004882813 0.002685547

Answer: It seems correct to me. I'd only be more consistent in the notation, either use decimals $.5$ or fractions $\frac{1}{2}$.
Comment: Thanks. When I wrote it I could not comment here yet, so answering was my only alternative to not helping at all.
Comment: Upvoting in spite of bot's comment. Direct response to question and helpful edit of question.


How are quadratic inequalities and absolute value equations related?

Question: $$|x+2|+|x-3|=5$$  and
$(x^2-x-6)\leq0$
Have the same real solution of $[-2,3]$
I was wondering if there was a formula to convert from one to the other, such as taking a quadratic inequality and turning it into an absolute values equation. I know it's not very practical as the method for solving both formats is similar, but I was wondering about the relationship between the formats.
Answer: $$|x-a|+|x-b|=|a-b|\Leftrightarrow(x-a)(x-b)\leq0$$
You can use even the following equation $$\sqrt{x-x^2}=\sqrt{x-x^2},$$
which gives $[0,1]$.
Also, the equation $$\sqrt{1-x^2-y^2}=\sqrt{1-x^2-y^2}$$ is an equation of the disc.
Also, $$|x+y|+|x-y|=2$$ is an equation of the square.
Comment: @Tom Rowaway Because the domain gives $x-x^2\geq0$ or $0\leq x\leq1$.


How to change the size of the system cursor in a macosx cocoa app programmatically using swift or objective-c?

Question: I've tried to set it's global size using this code:
-(void)setOption {
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSDictionary *olddict = [defaults persistentDomainForName:@"com.apple.universalaccess"];
   NSMutableDictionary *newdict = [olddict mutableCopy];
   [newdict setObject:@4.0 forKey:@"mouseDriverCursorSize"];
   [defaults setPersistentDomain:newdict forName:@"com.apple.universalaccess"];
   [defaults synchronize];
   NSLog(@"Cursor size set to %@", newdict);
}

And I can see in the NSLog that it changed it but I don't know how to restart/reset the system cursor in order for the cursor to change to the specified size.
Does anybody know a better way to change it's size programmatically or how to restart the system cursor after the defaults change?
EDIT(about duplication): My question is unique because I can't use applescript in resolving this like the answer provided in the other topic. Also the topic has been created in 2013, and seems outdated. Maybe things have changed a little since then. Also maybe Swift would be a viable solution for resolving this. Who knows? All these arguments make it clear that this is not a duplicate question.
Comment: Possible duplicate of [How to programatically change the cursor size on a Mac](http://stackoverflow.com/questions/14510870/how-to-programatically-change-the-cursor-size-on-a-mac)
Comment: I've followed that topic but didn't met my requirements. That's why I asked a different question. Thanks for your interest, though!
Comment: any updates ? did you try my suggestion below ?
Comment: Commented under your response. :)
Answer: CGError state = CGSShowCursor(CGSDefaultConnection) ;
    if (state != kCGErrorSuccess) NSLOG(@"error : %d",state);

maybe try it with CGSShowCursor(CGSMainConnectionID())
This might also help : https://github.com/alexzielenski/Mousecape/blob/1d534b1e076b07a01b80364be23c88c8439028bc/Mousecape/mousecloak/NSCursor_Private.h
Comment: I've tried your suggestion, but it didn't work. So basically now I'm searching for new solutions.
Answer: Warning. This code is not based on what is saved in preferences so combine it:
  float cursorScale = 2;
  cursorScale = MAX(1, MIN(cursorScale,4));
  int connectionID = CGSMainConnectionID();
  CGSSetCursorScale(connectionID, cursorScale);

to get the size
  CGSGetCursorScale(connectionID, &cursorScale);

,
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSDictionary *olddict = [defaults persistentDomainForName:@"com.apple.universalaccess"];
   NSMutableDictionary *newdict = [olddict mutableCopy];
   [newdict setObject:@4.0 forKey:@"mouseDriverCursorSize"];
   [defaults setPersistentDomain:newdict forName:@"com.apple.universalaccess"];
   [defaults synchronize];
   NSLog(@"Cursor size set to %@", newdict);

CREDITS: Alex Zielenski


How to respectfully learn "eastern" music for guitar

Question: I've always loved classical Indian / eastern / modal sounds on guitar, but I met a man whom I admire that believed that merely copying some of the outward tonalities and calling it "eastern" was disrespectful to the classically trained musicians who were following an art form with a thousand year's worth of tradition and experience.
My question is, how can I learn to properly and respectfully play/learn/interpret eastern music and apply it to the guitar?
Comment: There is at least one instrument designed for this sort of thing: http://en.wikipedia.org/wiki/Bazantar . As @slim advises, the inventor first learned the Sitar (and guest lectured at my high school!).
Answer: With due respect to traditions, you should be free to be inspired by any music. As long as you're not claiming to master that style, I don't think you should limit yourself just because of opinions like those in the question.
Just listening to the music you like, and trying to incorporate those ideas in your music should be fine. To satisfy traditionalists, there is likely no other way than to study with a real master.
Answer: The best advice, I suppose, would be to take some sitar lessons. This will teach you what you're dealing with.
You may find that you can't replicate the microtonal Indian scales to an acceptable level using a fretted instrument like a guitar. A slide, bends, alternate tuning might help. You would be trailblazing somewhat. Previous guitar wizards dabbling in Eastern music just used Eastern instruments (for example, George Harrison).
Comment: There are many other string instruments used in Eastern music that *aren't* the sitar - some of which are not fretted.
Answer: There are some Eastern scales that can be translated into Western notation and can be successful.  In these instances, since the scales can be translated verbatim, the only insulting thing about using them would depend on your intent.
That said, there are many other scales with slight microtonal adjustments that don't conform to the equal-tempered system (such as your guitar, or a piano.)  Therefore, it is the perception of the action of "shoe-horning" their culture / music through a Western Idiom that may come across as insulting.
If you are truly passionate then there are some ideas that can begin moving the ball along:

Learn more about their music / culture
Learn more about their music theory
Learn about the limits of Western instruments
Purchase / Build an Eastern instrument
Modify a Western instrument

Microtonal guitars do exist, so just realize that without utilizing any instruments with microtonal capabilities, you will only be able to represent a small portion of Eastern music in your playing and writing.
Composers all the way back to Mozart have looked to the East for sounds and have used them to different degrees of success in their music.


copy list of objects into another list of objects

Question: I have a question here. Lets say I have these two classes
class Person
{
    int person_id;
    string name;
    Address address;
}

class Address
{
    int address_id;
    string street;
}

and I have list of Person data in List<Person> people = new List<Person>();.
Now I want to copy all of the address in people to a new address list List<Address> addresses = new List<Address>();. How can I achieve this?
Answer: You want to do a projection which means take the objects and transform them into another form:
List<Address> addresses = people.Select(p => p.Address).ToList();

Comment: it seems what i'm looking for. i'll have a try. ta
Answer: (From p in people select p.Address).ToList()
Is that it?
Comment: i'm sorry but i dont really understand with that
Comment: Ah It is same like answer above, but using linq query, for me it's self explaining


Duvída em uma Query que faz SELECT em duas tabelas

Question: Boa tarde,
Eu estou fazendo um sistema de agendamentos de equipamento, para melhor entendimento vou colocar em tópicos algumas informações importantes.

Cada agendamento possui uma numeração do equipamento que vai ser utilizado naquela faixa de hora.
Não se pode utilizar dois equipamentos na mesma hora.
Cada equipamento possui um software específico instalado nele (pode estar instalado em mais que um equipamento).

E para isso estou tratando com uma função. (A minha função está uma bagunça e da vergonha de postar aqui)
Esta função está evitando os conflitos porém está ainda está com alguns problemas.
Estava tentando fazer uma query para selecionar Carros que CONTÉM o Software X e que não estão sendo usados no dia atual
SELECT DISTINCT ls_software.* FROM ls_agendamentos INNER JOIN ls_software ON ls_agendamentos.data = '2016-12-01' AND ls_software.carro != ls_agendamentos.carro AND ls_software.nome = 'abacate'
Esta foi a tentativa, porém essa query retorna todos os carros que estão sendo utilizado no dia de hoje
Segue a foto das tabelas para melhor entendimento exemplo.
Answer: Tente a seguinte Query:
SELECT ls_software.*
FROM ls_agendamentos
INNER JOIN ls_software
ON ls_software.id = ls_agendamentos.carro
WHERE ls_agendamentos.data != '2016-12-01'
AND ls_software.nome = 'Abacate'


How to fit non-linear matlab data?

Question: I'm working on a problem in scientific computing namely fitting data to this equation
$c(z) = 4800 + p_1 + p_2 \cdot z/1000 + p_3 \cdot e^{ -p4 \cdot z/1000}$
The data is in a background question that I asked previously to prepared for the problem
acoustics under water
Now to split my work into smaller units, I should first try and fit the data. In the answer to the question is says that I can use lsqnonlin but it doesn't say how. I look in the manual for lsqnonlin and it says that the syntax is
x = lsqnonlin(fun,x0)
but I don't fully understand how to use it. Ideally I would read my data from a file, can you tell me how to do it? The data is
z c(z)
0 5050
500 4980
1000 4930
1500 4890
2000 4870
2500 4865
3000 4860
3500 4860
4000 4865
5000 4875
6000 4885
7000 4905
8000 4920
9000 4935
10000 4950
11000 4970
12000 4990

and the problem specification is problem U8 in this text.
Update
Running the code from the answer gives the following result for me after 400 iterations:
ans = 3.6277   14.8277   22.2631   -0.1114
So this means p1=3.6277, p2=14.8277, p3=22.2631, p4=-0.1114
Is that correct?
Update 2
>> z=0:10:12000;
>> fittedfun=4800 + 3.6277+14.8277/1000*z+22.2631*exp(0.1114/1000*z);
>> plot(z, fittedfun);

It looks rather linear, so I'm not totally certain that I'm seeing what I expect.
Update 3
Comparing the fitted function with the actual data the approximation doesn't look very good so it leads me to think that there is something wrong with my method or the equation.

>> plot(z, fittedfun);
>> z=0:500:12000;
>> fittedfun=4800 + 3.6277+14.8277/1000*z+22.2631*exp(0.1114/1000*z);
>> plot(z, fittedfun);
>> data=[5050 4980 4930 4890 4870 4865 4860 4860 4865 4875 4885 4905 4920 4935 4950 4970 4990];
>> z=[0:500:4000 5000:1000:12000];
>> zu=z

zu =

  Columns 1 through 6

           0         500        1000        1500        2000        2500

  Columns 7 through 12

        3000        3500        4000        5000        6000        7000

  Columns 13 through 17

        8000        9000       10000       11000       12000

>> plot(data,zu)
>> plot(zu,data)
>> hold on;
>> plot(zu,data)
>> fittedfun=4800 + 3.6277+14.8277/1000*z+22.2631*exp(0.1114/1000*z);
>> z=[0:500:4000 5000:1000:12000];
>> plot(z, fittedfun);
>>

Comment: Yes, that is the correct answer (my MATLAB has the same output and the modeling is correct). Note, that I omited the (-) sign in the template function, so $p_4$ is to be read as $0.1114$. Whether the parameters are accurately fitted is up to you (I suggest plotting the function with those parameters and comparing the resulting curve to your expectations)
Comment: I've found [this article](http://www.mathworks.com/help/stats/examples/pitfalls-in-fitting-nonlinear-models-by-transforming-to-linearity.html) from the MathWorks on nonlinear fitting of exponential data helpful in the past. They use [`nlinfit`](http://www.mathworks.com/help/stats/nlinfit.html) rather than [`lsqnonlin`](http://www.mathworks.com/help/optim/ug/lsqnonlin.html), but I'd guess that the two are similar.
Comment: Your implementation still has the wrong sign in front of $p_4$. The function is supposed to read
$$\ldots + 22.2631\cdot \exp(-0.1114 z)$$
Comment: @909 Niklas: is the Question still relevant to you? Please mark it as solved.
Comment: @909Niklas I plotted the two on top of each other and it seemed okay to me. Also, MATLAB finished with "minimum found" and not with "maximal iterations reached", so I am confident, that this suffices.
Comment: @AlexR Thanks, I'm still working on the problem and I'm checking now that coeff. are correct but I'm getting different results. The coeff. I get now are: `p =  -20.2090   17.3368  272.9057   -0.7528` but those might be incorrect.
Answer: First, collapse the Function to only necessary parameters (all constants are "eaten up"):
$$ c(z) = p_1 + p_2 z + p_3 \exp(p_4 z) = p_1 + p_2 z + p_3 \tilde{z}^{p_4}$$
Now iterate over $p_4$ in plausible values and do a 2D-polyfit with $x=z, y=\exp(z)^{p_4}$, see http://www.mathworks.com/matlabcentral/newsreader/view_thread/17164
(google matlab 2d polyfit). Finding $p_4$ can be tricky, though.
For the nonlinear solver, use an error measure (i.e. fun = @(pVec)norm(data-c(p1, p2, p3, p4)) where pVec(i)=p_i).
EDIT:
The following MATLAB snippet did the work for me, though used maxFunEvals (400) so no idea how good the approximation is:
z=[0:500:4000 5000:1000:12000];
data=[5050 4980 4930 4890 4870 4865 4860 4860 4865 4875 4885 4905 4920 4935 4950 4970 4990];
fun=@(p)4800 + p(1)+p(2)/1000*z+p(3)*exp(p(4)/1000*z)-data;
x0=[0 1 1 -1]; % Guess
lsqnonlin(fun,x0)
Another option would be to exploit $z=0$ to reduce the dimension if we force $c(0) = 5050$, but only if that is justified.
EDIT 2
The following script gives excellent results:

z=[0:500:4000 5000:1000:12000];
data=[5050 4980 4930 4890 4870 4865 4860 4860 4865 4875 4885 4905 4920 4935 4950 4970 4990];
fun=@(p)(4800 + p(1))*ones(size(z)) +p(2)/1000*z+p(3)*exp(p(4)/1000*z)-data;
x0=[0 0 0 -1]; % Guess
opt = optimoptions('lsqnonlin', 'MaxFunEvals',1000);
p=lsqnonlin(fun,x0,[],[],opt)
fitf=@(t)(4800 + p(1))*ones(size(t)) + p(2)/1000*t+ p(3)*exp(p(4)/1000*t);
tt=linspace(0,12000,1000);
plot(z,data,'r-',tt,fitf(tt),'b-');

The only change was another guess in $x0$.
Comment: $e^{dz} \neq e^de^z = e^{d+z}.$

How to fit non-linear matlab data?


How to programmatically add options to list_text field

Question: I want to programmatically add a drop down list field to the user profile via a custom module. How do I go about adding select options for the field?
// Create Field
'suffix' => array(
  'field_name' => 'suffix',
  'type' => 'list_text',
),

//Create instance
'suffix' => array(
  'field_name' => 'suffix',
  'entity_type' => 'user',
  'label' => 'Suffix',
  'bundle' => 'user',
  'options' => array(
   '0' => 'Mr.',
   '1' => 'Mrs.',
  ),
  'widget' => array(
  'type' => 'list_text',
  );
),

The field shows up but the options do not. Anyone have a good guide they know of or tell me what I'm missing?
Comment: Where are your `field_create_field()` and `field_create_instance()`? These are just two arrays that have no specific meaning. Please post full code, not just arrays.
Answer: Your available widgets for a list field are these: options_select, options_buttons and options_onoff, not list_text, that is the field type.
The widget values can be found on list_field_widget_info_alter(), where the list types are coupled with the widgets.
So I suppose you should update your field instance to use:
'widget' => array(
  'type' => 'options_select', // Drop down button or multi-select
  // OR
  // 'type' => 'options_buttons', // Radio buttons or checkboxes.
);

The way the widget is rendered depends on the cardinality defined on your field. If cardinality is set to 1, the first type will be used (drop down or radio buttons), if cardinality > 1, you'll get a multi-select field or checkboxes.
Assuming this field can only hold one value, you should also add
'cardinality' => 1,

To your field definition.
Leaving you with:
// Create Field
$field = array(
  'suffix' => array(
    'field_name' => 'suffix',
    'type' => 'list_text',
    'cardinality' => 1,
  ),
);
field_create_field($field);

//Create instance
$instance = array(
  'suffix' => array(
    'field_name' => 'suffix',
    'entity_type' => 'user',
    'label' => 'Suffix',
    'bundle' => 'user',
    'options' => array(
     '0' => 'Mr.',
     '1' => 'Mrs.',
    ),
    'widget' => array(
      'type' => 'options_select',
    ),
  ),
);
field_create_instance($instance);

Comment: #Neograph734 All source code is in  list_field_widget_info_alter()


Interpolation using dynamic programming

Question: I'm having trouble on doing a homework exercise.
I need to describe an efficient algorithm which solves the polynomial interpolation problem:

Let P[i,j] be the polynomial interpolation of the points (xi, yi),...,(xj,yj). Find 3 simple polynomials q(x), r(x), s(x) of degree 0 or 1 such that:
P[i,j+1]={q(x)P[i,j](x)-r(x)P[i+1,j+1](x)}/s(x)
Given the points (x1,y1),....(xn,yn), describe an efficient dynamic programming algorithm based on the recurrence relation which you found in section 1 for computing the coefficients a0,...an-1 of the polynomial interpolation.

Well, I know how to solve the polynomial interpolation problem using Newton polynomial which looks quite similar to the above recurrence relation but I don't see how it helps me to find q(x), r(x), s(x) of degree 0 or 1, and assuming I have the correct q(x), r(x), s(x)- how do I solve this problem using dynamic programming?
Any help will be much appreciated.
Answer: q(x) = (x at {j+1}) - x
r(x) = (x at i) - x
s(x) = (x at {j+1}) - (x at i)

x at i or x at j mean their place in the ordered list of input points.
Some explanations:
First we need to understand what P[i,j](x) means.
Put all your initial (x,y) pairs in the main diagonal of an n x n matrix.
Now you can extract P[0,0](x) to be the y value of the point in your matrix at (0,0).
P[0,1] is the linear interpolation of the points in your matrix at (0,0) and (1,1). This will be a straight line function.
((x at 0 - x)(y at 1) - (x at 1 - x)(y at 0))
---------------------------------------------
              (x at 1 - x at 0)

P[0,2] is the linear interpolation of two previous linear interpolations, which means that your ys now will be the linear functions which you calculated at the previous step.
This is also the dynamic algorithm which builds the full polynom.
I highly recommend you have a look at this very good lecture, and the full lecture notes.
Comment: The links to the reccomended lecture in the answer are broken. Here are updated ones that works:
https://www.clear.rice.edu/comp360/lectures/old/Lagra.pdf
https://www.clear.rice.edu/comp360/lectures/old/LagrangeText.pdf

Interpolation using dynamic programming


could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device

Question: The posgtres sudden stopped on the server and the logs shows the following error:
2015-08-18 02:29:22 IST ERROR:  could not extend file "base/197893/198337": No space left on device
2015-08-18 02:29:22 IST HINT:  Check free disk space.
2015-08-18 02:29:22 IST STATEMENT:  UPDATE "data_shipmentpptlmapping" SET "pptl_id" = '2558', "time" = '2015-08-17 20:40:47.163837+00:00', "bag_seal_status" = 'close', "
shipment_id_id" = 728518 WHERE "data_shipmentpptlmapping"."id" = 557513
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary statistics file "pg_stat_tmp/pgstat.tmp": No space left on device
2015-08-18 02:29:25 IST LOG:  could not write temporary 2015-08-18 04:29:04 IST LOG:  database system shutdown was interrupted; last known up at 2015-08-18 03:35:25 IST

And here is the postgres details:
postgres=# SHOW data_directory;
        data_directory
------------------------------
 /var/lib/postgresql/9.1/main
(1 row)

postgres=# SHOW temp_tablespaces;
 temp_tablespaces
------------------

(1 row)

postgres=# SELECT version()
postgres-# ;
                                                version
--------------------------------------------------------------------------------------------------------
 PostgreSQL 9.1.14 on x86_64-unknown-linux-gnu, compiled by gcc-4.7.real (Debian 4.7.2-5) 4.7.2, 64-bit
(1 row)

Updating the information..
The disk usage result of the server..
➜  ~  df -h
Filesystem                                              Size  Used Avail Use% Mounted on
rootfs                                                  909G  465G  398G  54% /
udev                                                     10M   10M     0 100% /dev
tmpfs                                                   1.6G  616K  1.6G   1% /run
/dev/disk/by-uuid/9ca82aa1-92c7-458c-a76d-9ce5c074f5dd  909G  465G  398G  54% /
tmpfs                                                   5.0M     0  5.0M   0% /run/lock
tmpfs                                                   4.8G   24M  4.8G   1% /run/shm
tmpfs                                                   4.8G     0  4.8G   0% /tmp

Adding the information ls -l /var/lib/postgresql/9.1/main.
total 56
drwx------ 8 postgres postgres 4096 Sep 28  2014 base
drwx------ 2 postgres postgres 4096 Aug 18 04:29 global
drwx------ 2 postgres postgres 4096 Aug 13 23:58 pg_clog
drwx------ 4 postgres postgres 4096 Aug 12  2014 pg_multixact
drwx------ 2 postgres postgres 4096 Aug 18 04:29 pg_notify
drwx------ 2 postgres postgres 4096 Aug 12  2014 pg_serial
drwx------ 2 postgres postgres 4096 Aug 19 18:24 pg_stat_tmp
drwx------ 2 postgres postgres 4096 Aug 19 05:34 pg_subtrans
drwx------ 2 postgres postgres 4096 Aug 12  2014 pg_tblspc
drwx------ 2 postgres postgres 4096 Aug 12  2014 pg_twophase
-rw------- 1 postgres postgres    4 Aug 12  2014 PG_VERSION
drwx------ 3 postgres postgres 4096 Aug 19 17:34 pg_xlog
-rw------- 1 postgres postgres  133 Aug 18 04:29 postmaster.opts
-rw------- 1 postgres postgres   93 Aug 18 04:29 postmaster.pid
lrwxrwxrwx 1 root     root       36 Aug 12  2014 server.crt -> /etc/ssl/certs/ssl-cert-snakeoil.pem
lrwxrwxrwx 1 root     root       38 Aug 12  2014 server.key -> /etc/ssl/private/ssl-cert-snakeoil.key

Comment: Post the output of `df -i`.
Comment: Unfortunately that output did not include any of the clues I was hoping to find there. Could you include the output of `ls -l /var/lib/postgresql/9.1/main/pg_stat_tmp` and `df -h /var/lib/postgresql/9.1/main/pg_stat_tmp` as well?
Answer: You are out of storage where pg_stat_tmp/pgstat.tmp resides.
Which is exactly what the error message tells you.
Comment: not exactly, the system has a hell lot of memory. Its not full. thats why i am asking this question
Comment: If you think this is true, then update your question with the appropiate information.
Comment: I have the same problem, lots of space left, but postgres tells me otherwise.
Comment: it's actually an out of memory problem: its not the disk, but the ram
Answer: As @Tobias said it's likely an out of RAM issue so edit your postgresql.conf to reduce shared_buffers or effective_cache_size... you can trigger free at the time of the first "No space" log to see what the kernel thinks but really this is a PG bug. Restarting the service (temporarily) resolved the issue for me.