Opening Office 2007 Documents from in memory storage - How?

Question: I'm a C++ developer wrestling with updating an application that had made extensive use of the IStorage interface to open pre-Office 2007 documents from in-memory storage (via ILockBytes).
If you are still following me so far, you probably know that the new Office Document formats are incompatible with IStorage containers.  The application I'm trying to update, relied upon the IPersistStorage interface that all Office applications have, and the code as written calls the load method of IPersistStorage to read in a document from IStorage interface.
So the question is:
What kind of COM interfaces are available to me to read in, from an in memory container, an Office 2007 document?
Comment: In Russian Delphi community the following sequence of calls is suggested: GlobalAlloc(GMEM_MOVEABLE, Stream.Size) => GlobalLock => CreateILockBytesOnHGlobal => StgOpenStorageOnILockBytes => OleLoad => IOleObject.DoVerb(0, nil, nil, 0, 0, Rect(0, 0, 0, 0))
Answer: I've been struggling with pretty much the same problem, to read and write from database storage.
Right now, I'm thinking that the best way might have to be to use the WebDAV-support, and use a web server to implement WebDAV to read from and write to the database.
It's a mess, though... other pointers would be welcome :-)


Difficult exam question - finding complex numbers from a quadratic.

Question:
This appeared in a past exam paper for the complex calculus course I am taking. I am required to find all solutions for it and plot them in the complex plane. Below is my working.

I stopped before calculating $z$ because it clearly won't get nicer. I did not try applying De Moivre's Theorem because the argument for $-3+4i$ does not seem to have a simple form. I am pretty sure I have gone wrong somewhere or am using the wrong technique to find solutions because this question is not worth many points and the exam is only two hours long so I should be able to work it out quite quickly. How would I go about doing this kind of question quickly? Thank you.
Comment: $(1+2i)^2=-3+4i$.
Comment: In addition to your one error ($17$ should be $25$), remember that there should only be two solutions to the square root, so you have to choose the signs right.
Answer: You made an error solving the quartic: $(-3)^2-4(-4)=25$ (not $17$). Note that
$$(1+2i)^2=1+(2i)^2+2(2i)=1-4+4i=-3+4i.$$
Therefore, according to your work
$$z_1=\frac{3+(1+2i)}{2}=2+i\quad\text{and}\quad z_2=\frac{3-(1+2i)}{2}=1-i.$$
Answer: You seem to have made a mistake in solving
$$-y^4+3y^2+4 =0$$
which should yield $y^2 = 4$ and $y^2=-1$.


Prove a property of a concave function

Question: Let $f$ be a concave function (and differentiable). Show that
$$\frac{f(y) - f(x)}{y-x} \ge f'(y) $$ where $y>x$.
I can see why this is true by drawing a graph. The LHS is the gradient of the line that passes through $(x, f(x))$ and $(y, f(y))$ while the RHS is the gradient of the tangent at $(y, f(y))$. Because the function is concave, its derivative is decreasing and so the LHS is greater than the RHS.
How do I prove this by converting my intuition from the graph into a more formal argument?
Answer: You case use Lagrange's theorem.
$$
\frac{f(y)-f(x)}{y-x} = f'(\xi), \quad \xi \in [x,y]
$$
Since $f$ is concave, $f'$ is non-increasing and so $\xi \leq y \Rightarrow f'(\xi) \ge f'(y)$. As it was pointed out in another answer, the previous conclusion depends on the fact that $x<y$.This gives you the desired inequality.
Answer: You forgot to sat $x <y$. This is necessary. Hint: try to show that $\frac {f(y)-f(x)} {y-x} \geq \frac {f(y)-f(y-h)} h$ for $0<h<y-x$. For this rewrite this inequality in the form $f(y-h) \geq af(x)+bf(y)$. Once you detremine what $a$ and $b$ are you will be able to prove this using deinition of concavity.

Prove a property of a concave function


kill autosys job using Talend

Question: i want to kill a autosys job using Talend tSystem component with 'sendevent' command.
the command is : sendevent -E KILLJOB -j
but i got error : sendevent : command not found
any help on this..
Comment: Please [edit] your question and add more information a)what Linux you are using b) how the tSystem content looks like c) if sendevent works if you call it on commandline.
Answer: you might find this link useful

kill autosys job using Talend


jQuery: Check if checkbox is checked

Question: i have an html page where i have multiple "checkboxes" each of them are call a function onclick.
Something like this
onclick="manage(val1, val2)

And in my function when i am getting the check status of checkbox, giving false every time.
function manage(val1, val2) {
    **if ($(this).is(":checked")) {** //returns false everytime
        //do something
        //
    }
    else {
        //do something
    }
}

please tell me where i am doing mistake...
Thanks in advance
Answer: $('input[type="checkbox"]').click(function(){  // append click to any checkbox
    if ($(this).is(':checked')) {
        // do something
    }
});

Comment: Just some suggestions, there is the `:checkbox` selector and `this.checked`. Your selector can take advantage of QSA without any pre-processing though :)
Comment: @alex: `[type="checkbox"]` is `qSA` friendly, unless I misunderstood you?
Comment: @amnotiam: I think you misunderstood me :)
Comment: @alex: Ah yes, very likely. I thought you were saying `[type="checkbox"]` would prevent `qSA`. Totally with you on using `this.checked` though. :)
Answer: When manage(val1, val2) is called, this will be the global object (or undefined if you're in strict mode)
But you can set the this value manually with call.  Try this:
onclick="manage.call(this, val1, val2);"

Now this will be whatever you just clicked on.
Just to clarify a bit more, inside the onclick="____" this is the thinig you clicked on.  But once you call functions from there, this becomes the global object in the functions.  So:
onclick="foo(this);"

function foo(val) {
    alert(this);
    alert(val);
}

Alerts [object DomWindow] (the global object) then [object HtmlInputElement] (the thing I clicked on)
Without getting into too much detail, this is a result of how "function invocation" works in JavaScript.
Comment: @ЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖ - :) (thanks to your username I never have to worry about having too short of a comment)
Comment: That is quite a beast of a name.  And I think that call and apply are quite integral to the design of JavaScript :p.
Comment: I could have sworn browsers did that automatically...  Edit:  Or maybe I'm thinking about when things are done like onclick="blah(this)"  :)
Comment: +1 for using `call` instead of just passing as a regular argument. Brings it into conformity with the expectation.
Comment: @ЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖ - and yeah, call and apply are some of my favorite tools to use in JavaScript
Comment: @AdamRackis: Finally someone who sees the glass half full! Though I'll be changing it shortly for the sake of the complainers. ;)
Comment: ...and I agree. I don't know what I'd do without `call` and `apply`.
Comment: @ЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖΞЖ - I always just copy paste the first few letters and let auto complete do the rest.  So, out of curiosity, is there an ascii value I can use to get that first character.  Does that name mean something in another language?  So many questions!!!
Comment: @AdamRackis: The name is meaningless. Just obnoxious noise. And there are unicode character mappings, though I'd have to find them again.
Answer: You'll need to set the context of your onclick, or pass it 'this' as a parameter:
<input type="checkbox" onclick="manage(this, 'val1', 'val2');"></input>

jsFiddle
Of course your best bet is to NOT use in-line javascript, go with something like TerryR wrote.

jQuery: Check if checkbox is checked


Define sequence $s_1 = \sqrt{2}$ and $s_{n+1} = \sqrt{2+s_n}$. Find $\lim_{n \rightarrow \infty} s_n$ and prove this convergence.

Question: I'm completely at a lost as to how to solve this. A few things I do know is that $s_n > -2$ because square root can't take negative values. So this implies that $s_n$ is bounded by -2? I also know of a theorem that says that $s_n$ will converge if it is a monotone bounded sequence. The sequence is also increasing. So by the theorem, it will converge to $\lim_{n \rightarrow \infty} s_n = \sup \{ s_n : n \in \mathbb{N}\}$?
I'm not sure if this is correct and I'm also struggling with trying to prove this. Help would be appreciated.
Comment: Special case of [$\sqrt{c+\sqrt{c+\sqrt{c+\cdots}}}$, or the limit of the sequence $x_{n+1} = \sqrt{c+x_n}$](https://math.stackexchange.com/questions/115501/sqrtc-sqrtc-sqrtc-cdots-or-the-limit-of-the-sequence-x-n1-sq)
Comment: Have you tried simulating it?  Easy to set up on a machine.  Should give you a good sense of how this performs.  Also:  if the limit, $L$, exists then $L=\sqrt {2+L}$.
Comment: what is $s_1$ ?
Comment: Side note:  a few trials should dispel the notion that "the sequence is also increasing".
Comment: @AbdallahHammam it's clearly written in the headline
Comment: Use $f(x)=\sqrt{2+x}$
Comment: "I also know of a theorem that says that sn will converge if it is a monotone bounded sequence" Excellent, thus, is $(s_n)$ a monotone bounded sequence?
Answer: You should consider two cases. One when $t \ge 4$ and the other when $t \le 4$.
For the first one using inductive reasoning we have that:
$$s_{n+1} \le s_n \iff \sqrt{2 + s_n} \le s_n \iff 2 + s_n \le s_n^2 \iff s_n^2 - s_n - 2 \ge 0$$
$$ \iff (s_n - 2)(s_n + 1) \ge 0$$
which is true as $s_n \ge 2$.
Also we can prove that:
$$s_{n+1} \ge 2 \iff \sqrt{2 + s_n} \ge 2 \iff 2 + s_n \ge 4 \iff s_n \ge 2$$
Therefore $s_n$ is decreasing sequence and bounded from above, so it's convergent.
If $t\le 4$, then prove that $s_n \le 2$ and $s_n$ is an increasing sequence.
To find the limit set $\lim_{n \to \infty} s_n = L$ and take the limit of the recursive relation to get:
$$L = \sqrt{2+L} \iff L^2 = 2 + L \iff (L-2)(L+1) = 0$$
Obviously we must have $L=2$, so $\lim_{n \to \infty} s_n = 2$
Comment: Thanks for the reply! I had a typo on the title. It should read $s_1 = \sqrt{2}$. What would change in this case? I'm guessing we would have only the case where $s_n > -2$?
Comment: @Nikitau Note that $s_n$ can't be negative. Now do the same, instead of $t\ge 4$ and $t<4$ check the cases when $t \ge 2$ and $t <2$
Comment: Quick follow-up question, is there way to show the entire seq is bounded above by 2? I'm not sure how to justify the cases
Comment: @Nikitau It depends. If $s_1<\sqrt{2}$ then the sequence is bounded by $\sqrt{2}$ from above, otherwise it's  bounded by the same bound, but from below.
Comment: Oh I get it. So in the first line of where you show $s_{n+1} \leq s_n$, we're showing that it is a decreasing sequence. The second line shows that $s_n$ is indeed bounded.


Não está aceitando clicar no annotation pela segunda vez

Question: Estou usando uma customização de annotation no meu projeto de mapkit (swift 3) para mostrar mais de um annotation(pin) no mapa. Está mostrando e podendo clicar nos annotationn, porém só na primeira vez. Para que possa clicar e no annotation e abri-lo, só se eu clicar em qualquer lugar do mapa e depois voltar a clicar no annotation novamente.
Alguém já passou por isso que poça dar um auxílio ? Desde já agradeço.
Segue as funções que estou utilizando:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation
    {
        return nil
    }
    var annotationView = self.map.dequeueReusableAnnotationView(withIdentifier: "Pin")
    if annotationView == nil{
        annotationView = AnnotationView(annotation: annotation, reuseIdentifier: "Pin")
        annotationView?.canShowCallout = false
    }else{
        annotationView?.annotation = annotation
    }
    if (indexPin > 0) {
        indexPin = indexPin - 1
        let pin : PinAnnotation = pinAnotationList[indexPin]
        annotationView?.image = UIImage(named: pin.imageName)
    }
    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView)
{
    if view.annotation is MKUserLocation
    {
        return
    }
    let pin = view.annotation as! PinAnnotation
    if pin.userType == "O" {
        if (currentLatitude == 0 || currentLatitude2 == 0) {
             self.showAlert(self, message: "It's necessary to set origin and destiny addresses")
            return
        }
        AppVars.DriverId = pin.userId
        AppVars.VehicleId = pin.vehicleId
        AppVars.LatitudeDriver = pin.coordinate.latitude
        AppVars.LongitudeDriver = pin.coordinate.longitude
        performSegue(withIdentifier: "callDriverPopupSegue", sender: self)
    }
    else {
        let customView = (Bundle.main.loadNibNamed("AnnotationView", owner: self, options: nil))?[0] as! CustomCalloutView
        var calloutViewFrame = customView.frame;
        let point = CGPoint(x: calloutViewFrame.size.width/2 + 15,y :calloutViewFrame.size.height - 10)
        calloutViewFrame.origin = point
        customView.frame = calloutViewFrame;
        customView.titleLabel.text = pin.title
        view.addSubview(customView)
    }
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    if (view.isKind(of: PinAnnotation.self))
    {
        for subview in view.subviews
        {
            subview.removeFromSuperview()
        }
    }

    if (view.isKind(of: AnnotationView.self))
    {
        for subview in view.subviews
        {
            subview.removeFromSuperview()
        }
    }

}

Class PinAnnotation
import MapKit

class PinAnnotation: NSObject, MKAnnotation {

    var coordinate: CLLocationCoordinate2D
    var userId: Int!
    var vehicleId:Int!
    var userType: String!
    var imageName: String!
    var title: String!
    init(coordinate: CLLocationCoordinate2D) {
        self.coordinate = coordinate
    }
}

Class AnnotationView
import MapKit

class AnnotationView: MKAnnotationView
{
}

Answer: Acabei de achar uma solução ! A situação ocorria quando eu chamava o  "performSegue(withIdentifier: "callDriverPopupSegue", sender: self)" dentro do didSelect porque o annotation que era clicado permanecia selecionado. Então eu adicionei o código abaixo no mapview Controller para deselecionar o annotation. Com isso o annotation fica liberado para abrir novamente por click.
override func viewWillAppear(_ animated: Bool) {
        DispatchQueue.main.async {
            for item in self.map.selectedAnnotations {
                self.map.deselectAnnotation(item, animated: false)
            }
        }
}


Note duration in Hindemith's "Elementary Training For Musicians"

Question: On the recommendation of a friend (and Nadia Boulanger), I am working through Hindemith's "Elementary Training For Musicians", with an eye to using a small selection of its exercises for some of my students. However, I am confused by his use of rhythmic proto-notation in the first chapter. Here are the first three exercises:

He has the student beat out the vertical lines in a steady pulse while vocalizing the horizontal arches as "la". Further on, the preliminary skills gained in these exercises are used to introduce basic rhythmic notation:

While I understand the general process at work here, I am confused about his equivalences: the proto-rhythmic unit that is supposed to be equivalent to a whole-note appears instead to be equivalent to a dotted half-note. Or am I reading this wrong? It doesn't appear to be a misprint, as the other smaller units are proportionally related to the whole-note unit. But wouldn't the proto-rhythmic unit in Exercise A be more correctly said to equal a whole-note? And the unit in Exercise C a quarter-note?
In my library I couldn't find much elucidation on the subject, other than this passage from Daniel Kazez's "Rhythm Reading":

Which would seem to concur with my assessment of Hindemith's proto-notation. So, is he mistaken? Or am I failing to understand some fundamental aspect of duration in rhythm? How would these exercises be correctly performed? How does he get from the proto-notation to common rhythmic notation?
In Canada, "Elementary Training For Musicians" is in the public domain. So if you are Canadian, or a scofflaw, you can find a PDF of the book here. The exercises and notation equivalences are on pages 3-4 of the book. Don't know what page of the PDF.
Answer: This is an absolutely fantastic book for helping with aural training. But you're absolutely right, this small section is quite misleading. In his example it looks like you vocalise through three taps and stop vocalising on the fourth tap, which would make the length of the vocalisation only three beats! I don't think you need to read too much into this; he is simply equating a four-beat note (semibreve or whole-note) with four tapped beats, but as you say the example is rather misleading.
In fact, this is an issue that I find needs addressing quite early on when teaching pupils; ensuring that notes are held for their full duration, and not just until you count the last beat of a note.
Until they develop a reasonable sense of pulse, pupils will often intuitively count the notes shown on the top line of the following example, with the rhythm of the second line (particularly at slow tempi):

So, although I agree that Hindemith's example is misleading, it does show a common problem with equating length of notes with their number of beats (even if this is unintentional!)
In fact, for someone just getting started with learning about rhythm, the next few examples are a little difficult to understand too, with it difficult to see the relationship between different note durations and numbers of beats:

The proper relationship between beats and note lengths becomes clear on the next page, once the concept of rests is introduced:
Comment: In your first image, did you mean to imply 7/8 time or simply not playing legato?
Comment: I meant to imply 7/8 or anything shorter than 4/4. I find that beginners often play the next note as soon as they have counted the beats of the nite before.
Answer: The notation on the surface is understandable, but at first seems confusing especially since the beams used to denote what one would typically perceived as a tie between two notes looks like a beam for eighth notes which don't appear to be covered in the legend.
Pretty much in this notation every beam is a tie so if you just had "|" you would say la, if you had a "|‾|" you would hold it for two beats and so on.
The first exercise is actually odd because it groups 5 beats together which is an odd way to start an exercise to teach the basics of rhythm.
To simplify this for you, I've written this out in standard notation both with tied examples and what they would be represented in whole, half, and quarter notes.
a.
Tied:

Normal:

b.
Tied:

Normal:

c.
Tied:

Normal:

In response to your comment, the claps represent the pulse or the start of the beat which is an important aspect of music. Of course the clap will most likely decay before the duration of the beat like other percussion instrument, but that's fine as it's a device to keep the pulse where the "la" is what is meant to hold the value.
Comment: Great point about this notation looking like ties and being confusing as a result.
Comment: If only Hindemith had used ties... (!!)
Comment: Great point about the ties. In lessons, I tend to introduce quarter notes first and then use ties to explain half notes, which is how Kazez proceeds in "Rhythm Reading".
Comment: I see I should've been clearer about what I saw as confusing in the examples above. Hindemith precedes the exercises with the instruction "Tap or clap the rhythm as before but sing the tone only on the strokes connected by brackets." As there are no brackets on the proto-rhythmic quarter notes (by his definition), the student would not sing them. At the same time, they are asked to tap or clap those same quarter-notes. But unless they're tapping or clapping at light-speed, the taps/claps won't last a quarter-note in duration. I don't think the connection would be too clear to a young student.
Answer:
A note ends exactly when the next note or rest begins.

I believe you are misunderstanding this; you should consider the "la" as ending when the last connected note would end (not when the connecting bar itself ends).  I've highlighted the quarter notes of the first "la":

As you can see, it comprises 5 quarter notes, equivalent to a whole plus a quarter.  The final note begins at the end of the 4 quarters and continues until the next note begins, extending the "la" beyond the duration of a whole note.


On the optimal distribution for importance sampling

Question: Let's say $X$ is a rv, $p(x)$ is its pmf.
I want to importance-sample $\mu := \mathbb E[f(X)]$,
for some bounded function $0<f<1$,
using another distribution $q(x)$.
Then what I should do is summing
$$\hat\mu := \frac{1}{n} \sum_{k=1}^n \frac{f(Y_k)p(Y_k)}{q(Y_k)}$$
where $Y_k$ are generated using $q$.
According to
this lecture note,
there is an optimal $q$, denoted by $q'$,
that minimizes the variance of $\hat\mu$:
$$q'(x)\propto f(x)p(x)$$
If I sample using $q'$,
then all summands in the definition of $\hat\mu$ becomes constant.
So yeah, it does give me a variance-zero experience.
Really appreciate.
What I don't seem to understand is that,
if I now want to importance-sample $g := 1 - f$ instead,
then suddenly $q'$ is not the best distribution,
as $q'$ is no longer $\sim gp = (1 - f)p$.
Yet another example is that, if I want to sample $h := 100 + f$,
then $hp$ is just $100(p \pm \epsilon)$,
so the optimal $q$ is very close to $p$.
Or maybe it's telling me that I shouldn't bother.
How is it possible that the optimal choice of $q$
depends on an affine transformation I applied to $f$?
Does that mean that I should optimize over
the affine transformations that can be applied to $f$?
Answer: If you choose the importance distribution ($q$) to be proportional to $f(x)p(x)$, i.e., $\rho(x) := f(x)p(x)/\mu$, you achieve a zero variance estimator:
$$
\frac{1}{n} \sum_{k=1}^n  \frac{f(x)p(x)}{f(x)p(x)/\mu} = \frac{\mu}{n} \sum_{k=1}^n  \frac{f(x)p(x)}{f(x)p(x)} = \mu.
$$
The idea essentially says the optimal estimator of the integral is obtained by drawing samples exactly from the integrand (normalized) and evaluating the weight, which is exactly equal to the integral itself.
RE first example: the optimal $q$ will no longer be $\rho(x) = f(x)p(x)/\mu$ because
$$
\frac{1}{n}\sum_{i=1}^n \frac{(1-f(X_i))p(X_i)}{q(X_i)} =
\frac{1}{n}\sum_{i=1}^n \frac{p(X_i)}{q(X_i)}q(X_i) - \frac{1}{n}\sum_{j=1}^n\frac{f(X_j)p(X_j)}{q(X_j)},
$$
you can see that if you use $\rho(x)$ as your proposal, the first term on the RHS will no longer be optimal; $\rho(x)$ is only optimal for the second term.
RE second example:
In this case,
$$
\frac{1}{n}\sum_{i=1}^n \frac{(100 + f(X_i))p(X_i)}{q(X_i)} =
\frac{100}{n}\sum_{i=1}^n \frac{p(X_i)}{q(X_i)} + \frac{1}{n}\sum_{j=1}^n\frac{f(X_j)p(X_j)}{q(X_j)},
$$
the same idea applies; the first term on the RHS should use a $p$ as $q$.
Comment: Ah, thank you for the clarification, looks like I totally misunderstood your question. Hopefully I understand now. So for the specific estimator $\hat \mu_f := (1/n)\sum f(X_i)p(X_i)/q(X_i)$, we have the optimal $q(x) \propto f(x)p(x)$. If you change your test function to say $h = 1-f$, then WITH RESPECT TO $\hat\mu_h$, sampling from the distribution proportional to $h$ is optimal. Alternatively, you can sample proportionally to $f(x)p(x)$, and change your estimator $\hat\mu_h$ to $\tilde \mu_h := 1- \hat\mu_f$, then this will also have 0 variance.
Comment: OK. It seems like I did not aware of that $\hat\mu_h$ is different from $\tilde\mu_h$ (not equivalent by an affine transformation), so it now makes sense that different estimators have different $q$.
Comment: I did get that the optimal estimator is different if I rewrite $f$ using an affine transformation.  What I didn't get is why the choice of a sampling distribution, $q$, should depend on the arbitrarily affine transformation I applied to $f$?
Comment: @Symbol1 could you explain why you think it shouldn't depend on a transformation? (So I can understand where you're coming from)
Comment: Because, although what I really want is $\mathbb E[f(X)]$, I can always claim that I am interested in $\mathbb E[1-f(X)]$. You then proceed to sample an empirical value of $\mathbb E[1-f(X)]$ for me. So depending on what I claim, you will use different $q$ for simulations, some of them must be better than the others.  The question is, how do I know if one $q$ is better than another $q$, if they are all *optimal* up to some affine transformation of $f$?
Comment: in one sentence: optimality is defined with respect to your estimator.
Let me know if I'm still not misunderstanding/not answering the question.
Answer: There's more than one optimal choice of $q$.
Suppose $p(x)=2x$ on $[0,1]$ and 0 otherwise, and $f(x)=x$. You can optimally choose $q(x)=p(x)$ and get zero variance or you can choose $q(x)=2-2x$ and estimate
$$E[f(X)]=1-\frac{1}{2}E_Q[2-2x]$$
and get zero variance that way.  There's a whole affine family of zero-variance estimators. The optimality proofs don't claim the optimum is unique.
Comment: But then which (estimator, $q$) is the best one in the following sense: Say I am allowed to query $f$ at 100 points generated $p$. From those data points I can define a $q$ and query $f$ at 10000 points generated by $q$.  How should I choose (estimator, $q$) then? (Maybe this question doesn't make sense but this is where I came from.)
Comment: Intuitively I know that I should sample from regions where $f$ is more unstable/fluctuating. But the so-called optimum $q$ doesn't address that. Instead it just says that I should bias toward a larger $f$ value, which make no sense to me because $1 - f$ is the other way around.


PyMongo's bulk write operation features with generators

Question: I would like to use PyMongo's bulk
write operation features which executes write operations
in batches in order to reduces the number of network round trips and increaseses rite throughput.
I also found here that it was possible to used 5000 as a batch number.
However, I do not want is the best size for batch number and how to combine PyMongo's bulk write operation features with generators in the following code?
from pymongo import MongoClient
from itertools import groupby
import csv

def iter_something(rows):
    key_names = ['type', 'name', 'sub_name', 'pos', 's_type', 'x_type']
    chr_key_names = ['letter', 'no']
    for keys, group in groupby(rows, lambda row: row[:6]):
        result = dict(zip(key_names, keys))
        result['chr'] = [dict(zip(chr_key_names, row[6:])) for row in group]
        yield result

def main():
    converters = [str, str, str, int, int, int, str, int]
    with open("/home/mic/tmp/test.txt") as c:
    reader = csv.reader(c, skipinitialspace=True)
    converted = ([conv(col) for conv, col in zip(converters, row)] for row in reader)
    for object_ in iter_something(converted):
        print(object_)

if __name__ == '__main__':
    db = MongoClient().test
    sDB = db.snps
    main()

test.txt file:
  Test, A, B01, 828288,  1,    7, C, 5
  Test, A, B01, 828288,  1,    7, T, 6
  Test, A, B01, 171878,  3,    7, C, 5
  Test, A, B01, 171878,  3,    7, T, 6
  Test, A, B01, 871963,  3,    9, A, 5
  Test, A, B01, 871963,  3,    9, G, 6
  Test, A, B01, 1932523, 1,   10, T, 4
  Test, A, B01, 1932523, 1,   10, A, 5
  Test, A, B01, 1932523, 1,   10, X, 6
  Test, A, B01, 667214,  1,   14, T, 4
  Test, A, B01, 667214,  1,   14, G, 5
  Test, A, B01, 67214,   1,   14, G, 6

Answer: You can simply do:
sDB.insert(iter_something(converted))

PyMongo will do the right thing: iterate your generator until it has yielded 1000 a documents or 16MB of data, then pause the generator while it inserts the batch into MongoDB. Once the batch is inserted PyMongo resumes your generator to create the next batch, and continues until all documents are inserted. Then insert() returns a list of inserted document ids.
Initial support for generators was added to PyMongo in this commit and we've maintained support for document generators ever since.
Comment: [Here](http://stackoverflow.com/q/26601352/977828) I updated the code so it uses now multiprocessing, but I do not whether PyMongo still is able iterate the generator until it has yielded 1000 a documents or 16MB of data, then pause the generator while it inserts the batch into MongoDB.
Comment: I doubt this will work, but I can't determine for certain without seeing your new code. In any case, this is a question and answer site, so I think you should ask a *new* question with the new code.
Answer: So you have a generator of documents, and you want to split it into chunks or groups of documents. This can be ellegantly done using the grouper generator, described in this answer.
Then, for each group of documents, use pymongo's insert to bulk-insert them.
You get:
def main():
    db = MongoClient().test
    sDB = db.snps
    ...
    for docs_group in grouper(iter_something(converted), BULK_SIZE):
        docs_group = [ doc for doc in docs_group if doc is not None ]  # filter out Nones
        sDB.insert(docs_group, ...)

As to the optimal BULK_SIZE, that depends on various factors, e.g. typical document size, network lag, etc. You'd need to experiment with it.

PyMongo’s bulk write operation features with generators


VBA TextToColumns - setting destination range from a variabe

Question: Having trouble using TextToColumns with variables.
This works:
IDCol = Rows(8).Find("ID", LookAt:=xlWhole).Column
Columns(IDCol).Select
Selection.TextToColumns Destination:=Range("J1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
   Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
   :=Array(1, 1), TrailingMinusNumbers:=True

This doesn't though, and since I don't always know where the column is going to be. The issue seems to be setting the Destination.
IDCol = Rows(8).Find("ID", LookAt:=xlWhole).Column
Columns(IDCol).Select
Selection.TextToColumns Destination:=Range(Cells(1, IDCol)), DataType:=xlDelimited, _
 TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
    Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo _
    :=Array(1, 1), TrailingMinusNumbers:=True

Thanks if anyone can help.
Answer: Try this:
Sub test()

Dim id_col As Long
id_col = Rows(8).Find("ID", LookAt:=xlWhole).Column

Dim target_col As Range

If id_col <> vbNullString Then

    Set target_col = Columns(id_col)

    With target_col
        .TextToColumns , DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
        Semicolon:=False, Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), TrailingMinusNumbers:=True
    End With

End If

End Sub

Comment: Doesn't do it, sorry. I checked and it does pick up the value for id_col but it doesn't do the operation. It only works if do the same thing:     .TextToColumns Destination:=Range("J1")
Comment: Removed `Destination` argument, it is optional, can you try again?


Better software for business forecasting?

Question: I'd like to know what software provides better performance in business forecasting for thousands of SKU. Here the data is taken through databases.
I know three alternatives:

Autobox
SAS
Forecast PRO

I'm looking for an option that can forecast every SKU automatically, given an error measure (preferably MAE).
I can't find a comparison for these sofware with the same data. What do you know about the performance of them?
Comment: Often one cannot find dataset to which many different software products are benchmarked against. I know only about this classic Airlines dataset:  http://calcnet.mth.cmich.edu/org/spss/V16_materials/DataSets_v16/airline_passengers.sav
Comment: According to each of the owners... they're all the best. ;)
Comment: at least forecast pro and autobox are open and compete in academic forecasting competition such as M competition. I have never seen SAS evaluated in a academic setting or evalauted by third party. Having said that it is always better to send a sample to all three vendors hold back some data, and compare it and do your own evaluation. You could do a rolling sample evaluation as these competition are usually single origin (which is flawed). You can look at this [article by Len Tashman](http://www.sciencedirect.com/science/article/pii/S0169207000000650)  on how to properly evaluate forecast.
Answer: SPSS Modeler can be used, it has not so much outlier detection facilities as Autobox but in few cases I have seen that it can do decent job.


Problem Nexus 1 running apps on my phone from Eclipse

Question: I have a Samsung Google Nexus S. Great phone.
I have been developing apps on it, not too much of a problem.
Since last week I can suddenly not run apps from Eclipse on the phone anymore. I get the following error:
[2011-04-11 20:12:48 - ImageBrowser] Android Launch!
[2011-04-11 20:12:48 - ImageBrowser] adb is running normally.
[2011-04-11 20:12:48 - ImageBrowser] Performing com.xxx.android.imagebrowser.ImageBrowser activity launch
[2011-04-11 20:12:48 - ImageBrowser] Automatic Target Mode: using device '34315B519D6000EC'
[2011-04-11 20:12:48 - ImageBrowser] Uploading ImageBrowser.apk onto device '34315B519D6000EC'
[2011-04-11 20:12:48 - ImageBrowser] Failed to install ImageBrowser.apk on device '34315B519D6000EC': An established connection was aborted by the software in your host machine
[2011-04-11 20:12:48 - ImageBrowser] java.io.IOException: An established connection was aborted by the software in your host machine
[2011-04-11 20:12:48 - ImageBrowser] Launch canceled!

I am going out of my mind. Everything has been working perfectly before. I have tried just running a Hello world app with the same problem.
My Machine: Apple i5, pretty standard.
OS: Windows 7 32 bit
Eclipse: Helios Service Release 2
ADB: Android debug birdge version 1.0.26
I have re-installed the google usb drivers as per this following link: http://developer.android.com/sdk/win-usb.html
USB debugging enabled on phone
I have hard factory reset my phone in case something is wrong.
I am at wits end. Everything worked just fine, and then it all suddenly went berserk and now nothing works.
2 things: about a week ago I plugged in an ipod which had been in someones windows machine and it appeared to have a virus on it. I have run AVG and removed one or 2 things that were on there, but has not helped.
Secondly I have a 3g dongle I use for internet, and that connects via usb. But I have been using this together with the phone no problem...
When I plug in the phone, it immediately appears in Eclipse in the devices panel, and it is online and showing Honeycomb android version. Then, when I hit run, the above message/error occurs and then immediately the device is offline. I can only unplug and plug in again, and start from the beginning with same results.
Very oddly, I have had issues when disabling USB debugging, and trying to copy photos from my phone. When I enabled the device as USB storage, it woudl connect fine, but not always stay on as stable. If I then went to the images folder, it would disconnect. Extremely odd.
Help???
Comment: Are you using windows 7 64 bit? I have a dual boot win7x64/ubuntu on my machine and I can't get it to work w/ windows but it works great in ubuntu. It could be a problem with windows.
Comment: @Stephen: I have Windows 7 x64 and have it working perfectly.
Comment: hi I just made sure , its 32 bit (I have 4gb ram but its only using 2.18Gb..stupid I know.) What is the weirdest is this worked perfectly for about a month. Not a hitch or glitch, but suddenyl since a few days it's just gone totally bonkers.
Comment: Joseph - do you have a google nexus S? When you are in usb storage mode can you access the photos folder no problem?
Answer: As you can see from the your log, the issue is An established connection was aborted by the software in your host machine.
So it wasn't caused by 1) a physical loss of connection or 2) the device itself.
The issue therefore lies with your computer. Have you tried:

Performing a full system scan with at least one different anti-virus package
Disabling the active-scanning of your anti-virus software while copying
Re-installing ADB
As a last resort: re-installing Eclipse and the Android SDK/AVD
Comment: sorry, cut short my comment. As for point 2 - while copying? what do you mean by this? Point 3, yes well I will try that now perhaps. I still have a query about the issue I have ALWAYS had - copying photos from the device. I tried using a usb extension the other day and then nothing worked. Also, I have always had problems with loss of connection while trying to copy photos. I have suspected my usb ports on my mac are finicky? I think I will try another usb cable first to see if that might help?
Comment: Just try to install it once with the anti-virus disabled, occasionally they can be *anal*. Possibly - try with a friends computer if you can.
Comment: ok. So I just used a friends little laptop. I plug in my nexus S, and allow the device to enable usb storage and voila. I can go and browse images on my phone. Same cable, same phone.
Comment: oops...entered again. THen I search around for macbook pro (the computer I use to develop) USB problems and it seems stacks of people have issues with them. Now someone might take this personally but I have to admit, I paid twice as much for apple mac, and so far it's been a piece of sh!t. performance is very average, battery life is nowhere near supposed 7 hours (even using macos, even using all battery saving etc) its maybe 3.5 hours, and now this issue with the usb. I'm selling it. Gnna install my dev environment on another pc tomorrow and try and do some work. 12 hours wasted. Thanks apple
Comment: COnfirmed. I bought a powered usb hub and all problems solved. Also, my 3G connection has improved. Data rates on mem sticks have imrpoved dramatically.
Comment: I hate to say it but I won't buy an Apple again. To sell hardware at such a premium then have problems with it is not really on. I also took it to the apple store and explained the problem to be met with blank stares. I emailed various departments in Apple about this and zero response. It cannot be ignored that thousands of ppl complain about usb problems...Apple needs to sort it out. But I will not buy their stuff again, as nice as it is...stylish etc, the reliability so far has been a serious let down. cheers all


Population count of rightmost n integers

Question: I'm implementing Bagwell's Ideal Hash  Trie in Haskell.  To find an element in a sub-trie, he says to do the following:

Finding the arc for a symbol s,
  requires finding its corresponding bit
  in the bit map and then counting the
  one bits below it in the map to
  compute an index into the ordered
  sub-trie.

What is the best way to do this?  It sounds like the most straightforward way of doing this is to select the bits below that bit and do a population count on the resulting number.  Is there a faster or better way to do this?
Answer: Yes. In particular, mask and popcount can be made to be quite efficient. Here is what the Clojure implementation does:
static int mask(int hash, int shift){
    //return ((hash << shift) >>> 27);// & 0x01f;
    return (hash >>> shift) & 0x01f;
}

...
static int bitpos(int hash, int shift){
    return 1 << mask(hash, shift);
}

final int index(int bit){
    return Integer.bitCount(bitmap & (bit - 1));
}

...
public INode assoc(int levelShift, int hash, Object key, Object val, Box addedLeaf){
    int bit = bitpos(hash, shift);
    int idx = index(bit);
    if((bitmap & bit) != 0)

Here is what I did in my implementation in Haskell:

type Key    = Word
type Bitmap = Word
type Shift  = Int
type Subkey = Int -- we need to use this to do shifts, so an Int it is

-- These architecture dependent constants

bitsPerSubkey :: Int
bitsPerSubkey = floor . logBase 2 . fromIntegral . bitSize $ (undefined :: Word)

subkeyMask :: Bitmap
subkeyMask = 1 `shiftL` bitsPerSubkey - 1

maskIndex :: Bitmap -> Bitmap -> Int
maskIndex b m = popCount (b .&. (m - 1))

mask :: Key -> Shift -> Bitmap
mask k s = shiftL 1 (subkey k s)

{-# INLINE subkey #-}
subkey :: Key -> Shift -> Int
subkey k s = fromIntegral $ shiftR k s .&. subkeyMask
Comment: It's interesting to note that more recent versions of clojure seem to not use this anymore.  They seem to just use the subkey as the index into the array.
Comment: Oh, it must have changed since the last time I updated my clojure checkout. Time to look...


Prove that, for each $n$, $\int^{1}_{0} f_n(x)dx=\frac{1}{2}$

Question: Problem: Define $f: [0, 1] \to [0, 1]$ by
$f(x)= 2x$ for $0 \leq x \leq \frac{1}{2}$
and $f(x) = -2x+2$ for $\frac{1}{2} \leq x \leq 1$
Let, $f_1(x)=f(x)$ and $f_{n}(x) = f(f_{n-1}(x))$ for all $n \in \mathbb{N}$.
Prove that for all $n$ $\int^{1}_{0} f_n(x)dx=\frac{1}{2}$
My attempt: I think it won't be required to find an explicit formula for $f_{n}(x)$ because I found $f_{2}(x)$, itself is pretty complicated.
I found $f_{2}(x)= 2(2x)$ for $0 \leq x < \frac{1}{2^2}$
$f_{2}(x)= -2(2x)+2$ for $\frac{1}{2^2} \leq x < \frac{2}{2^2}$
$f_{2}(x)= -2(-2x+2)+2$ for $\frac{2}{2^2} \leq x < \frac{3}{2^2}$
$f_{2}(x)= 2(-2x+2)$ for $\frac{3}{2^2} \leq x \leq \frac{4}{2^2}$.
Also, I am guessing the following must be true ( as it is true for $n=2$),
$\int^{\frac{k+1}{2^n}}_{\frac{k}{2^n}} f_{n}(x)=\frac{1}{2^{n+1}}$.
I tried to prove the above using induction but i could not.
So, please help
Comment: Have you tried to draw the graph $y=f_n(x)$ for small $n$?  What do you see?
Answer: An alternate way to think about your function $f(x)$ is as:
Double the $x$ value. If it is less than 1 (i.e. $2x\leq1$ from $x\leq\frac12$) then leave it. If it is more than one then reflect it in the line $y=1$ (from second half being $2(1-x)$ ). So double your input and fold it down if its over 1.
Using this definition and having knowledge of the first case should let you prove your induction easily.
Here is graphs of the first few iterations to help you visualize it.


ps output is not correlated to free command?

Question: I am getting sum of all reserved memory in KBs by using following command:-
ps aux --sort -rss | awk '{sum +=$6} END {print sum}'
OUTPUT: 14746220 i.e. 14.74GB

However free -g returns 27GB as used. Why there is a difference between the two outputs? Doesn't ps shows all the memory allocation?
Answer: I had the exact same situation. htop and top both reported about 95% usage (excluding buffers/caches) as did free -m. When summing up the processes from ps, I only was using about 10% of the memory. I found these sites to be invaluable:

http://www.linuxatemyram.com/
https://linux-mm.org/Low_On_Memory

In my case it was the dentries/inode cache that was filling the memory. I don't know why free didn't count these as buffers/cache. You can flush this information with the following command, which does not hurt the system:
echo 3 > /proc/sys/vm/drop_caches

Information about this is detailed here: https://linux-mm.org/Drop_Caches


Group Information in joined table for last available year

Question: So I have this table with an unique identifier and its group; I want to get totals of another table based on its Group
Table 1
UNIQUE_ID ! Group

      1     West
      2     West
      3     West
      4     West
      5     West
      6     East
      7     East

Then I have this second table from with which I join
Table 2

UNIQUE_ID ! NET PROFIT ! ASSETS ! EQUITY ! YEAR

       1     100           100     100     2016
       1     100           100     100     2015
       2     100           100     100     2016
       2     100           100     100     2015
       3     100           100     100     2016
       3     100           100     100     2015
    ***4     10            10      10      2015***
       5     100           100     100     2016
       5     100           100     100     2015
    ***6     10            10      10      2014***
       7     100           100     100     2016
       7     100           100     100     2015
       7     100           100     100     2014

I link the previous tables and I group by Group which then gives me the totals for NEt profit, assets and equity, the problem is that it sums all the years available in table 2 Or I do a where year is 2016 and I only get the totals from 2016 not including the rows which id have the latest year of 2015 or 2014
I need it to group by Group and sum only the last available year for each unique ID, so that I would get the following table
Query would give me this table

Group ! NET PROFIT ! ASSETS ! EQUITY
 East      410        410      410
 West      110        110      110

Can anyone help me? I've looked everywhere and tried a number of combinations but without success
Comment: First figure out which RDBMS you're using. Then, see https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query
Comment: Microsoft Access
Answer: Is this what you want?
SELECT Group_,
       SUM(NET_PROFIT) AS NET_PROFIT_YR,
       SUM(ASSETS) AS ASSETS_YR,
       SUM(EQUITY) As EQUITY_YR
  FROM Table1 AS T1
 INNER
  JOIN (SELECT T2_RAW.*
          FROM ( SELECT Unique_ID,
                        MAX(year) AS m_year
                   FROM Table2 AS T2
                  GROUP
                     BY Unique_ID
               ) AS MYR
         INNER
          JOIN Table2 AS T2_RAW
            ON MYR.unique_id = T2_RAW.unique_id
           AND MYR.m_year = T2_RAW.year
       ) AS TMP
    ON T1.unique_id = TMP.unique_id
 GROUP
    BY group_;

Answer: Consider a join of an aggregate derived table to the join of other two tables where essentially the last INNER JOIN acts as a WHERE clause to filter select years for each unique_id.
SELECT t1.`GROUP`, SUM(t2.NET_PROFIT) AS SUM_NET_PROFIT,
                   SUM(t2.ASSETS) AS SUM_ASSETS,
                   SUM(t2.EQUITY) AS SUM_EQUITY
FROM (`table2` t2
INNER JOIN `table1` t1
   ON t1.UNIQUE_ID = t2.UNIQUE_ID)
INNER JOIN
  (SELECT t2.UNIQUE_ID, MAX(t2.`YEAR`) AS MAX_YEAR
   FROM `table2` t2
   GROUP BY t2.UNIQUE_ID) g
ON t2.`UNIQUE_ID` = g.`UNIQUE_ID` AND t2.`YEAR` = g.`MAX_YEAR`
GROUP BY t1.`GROUP`;

Do note the parentheses used to wrap first join pairing of tables, required in MS Access.
Answer: I. The complete SQL statement:
SELECT
    ta.group,
    SUM(IFNULL(tb.net_profit, 0)) as sumNetProfit,
    SUM(IFNULL(tb.assets, 0)) as sumAssets,
    SUM(IFNULL(tb.equity, 0)) as sumEquity
FROM table_a AS ta
LEFT JOIN (
    SELECT
        unique_id,
        max(year) as maxYear
    FROM table_b
    GROUP BY unique_id
) AS tbMaxYears ON tbMaxYears.unique_id = ta.unique_id
LEFT JOIN table_b AS tb ON
    tb.unique_id = ta.unique_id
    AND tb.year = tbMaxYears.maxYear
GROUP BY ta.group;

II. Description:
The inner query:
SELECT
    unique_id,
    max(year) as maxYear
FROM table_b
GROUP BY unique_id

Selects the unique_ids from table_b and, for each unique_id, the corresponding latest year, e.g. the maximal year;
Groups the fetched records by the unique_id;
Is used in a LEFT JOIN statement under the alias maxYears and its results are filtered by the unique_ids fetched from the table_a.

The inner query results looks like following:
unique_id   maxYear
-------------------
1           2016
2           2016
3           2016
4           2015
5           2016
6           2014
7           2016

The outer query:
SELECT
    ta.group,
    SUM(IFNULL(tb.net_profit, 0)) as sumNetProfit,
    SUM(IFNULL(tb.assets, 0)) as sumAssets,
    SUM(IFNULL(tb.equity, 0)) as sumEquity
FROM table_a AS ta
LEFT JOIN (
    <THE-INNER-QUERY-RESULTS>
) AS tbMaxYears ON tbMaxYears.unique_id = ta.unique_id
LEFT JOIN table_b AS tb ON
    tb.unique_id = ta.unique_id
    AND tb.year = tbMaxYears.maxYear
GROUP BY ta.group;

Reads all table_a records;
Attaches the maxYears details (fetched through the inner query);
Attaches the table_b details;
Groups the records by the group column;
Calculates the corresponding sums (including NULL values validation).

The results of the outer query (e.g. the final results):
group   sumNetProfit    sumAssets   sumEquity
---------------------------------------------
East    110             110         110
West    410             410         410

The second LEFT JOIN in the outer query:
LEFT JOIN table_b AS tb ON
    tb.unique_id = ta.unique_id
    AND tb.year = tbMaxYears.maxYear

Attaches (joins) the table_b details to the records fetched from table_a;
Only the table_b records are attached, which have the same unique_id as the corresponding unique_id value from table_a AND (!) the same year as the corresponding maxYear value from maxYears table.

III. Used table structure:
I used a MySQL database with the following CREATE TABLE syntax:
CREATE TABLE `table_a` (
  `unique_id` int(11) DEFAULT NULL,
  `group` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `table_b` (
  `unique_id` int(11) DEFAULT NULL,
  `net_profit` int(11) DEFAULT NULL,
  `assets` int(11) DEFAULT NULL,
  `equity` int(11) DEFAULT NULL,
  `year` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I used the same data as you.
Good luck!
Comment: Love your answer, super detailed! now I understand why it wasn't working and how I should query it
Comment: Thank you, I appreciate it.


In how many ways can 20212022 identical balls be arranged in 2022 different bins [with conditions]?

Question: The problem goes like this:
in how many ways can 20212022 identical balls be arranged in 2022 different bins so that the number of bins with exactly 4 balls is 3, with exactly 7 balls is 4 (2), and the remaining bin have at least 19 balls each (1)?
I've attempted to approach it thus:
(1)
Let there be a vector $A=(x_{1}, x_{2}, ..., x_{2022})$, where $x_{i}$ is a positive integer and represents amount of balls in a bag $i$:
$x_{1}+x_{2}+ ... +x_{2022}=20212022$
We can arrange $20212022-4*3-7*4 = 20211982$ balls in $2022-3-4=2015$ bins (ignoring those $4*3+7*4$ balls for 7 bins with fixed amounts of balls) by building a bijection between $A_{1}=(x_{1}, x_{2}, ..., x_{2015})$ and $B=(y_{1}+19, y_{2}+19, ..., y_{2015}+19)$, where $x_{i}=y_{i}+19$.
Then  $y_{1}+y_{2}+ ... +y_{2015}=20211982-2015*19=20173697$.
\begin{equation}
    \binom{n-1+k}{k}=\binom{n-1+k}{n-1} \end{equation}
$n=2015, k=20173697$:
\begin{equation}
    \binom{2015-1+20173697}{20173697}=
 \binom{20175711}{2014}
\end{equation}
And that should be the answer to this part. However, there is still (2):

so that the number of bins with exactly 4 balls is 3, with exactly 7 balls is 4,

and that's where I am lost. How do I calculate this?
Answer: You got the hard part (i.e. the Stars and Bars part) correct, but had a problem with the $~7~$ special bins.

First, I need to be sure that I have correctly interpreted the constraints:

There are $~20212022~$ identical balls.

There are $~2022~$ different bins, and the bins are distinguishable from each other.

There must be exactly $~4~$ bins that have exactly $~3~$ balls in them.

There must be exactly $~7~$ bins that have exactly $~4~$ balls in them.

Each of the remaining bins have at least $~19~$ balls in them.

This is a Stars and Bars problem.  For Stars and Bars theory, see
this article and
this article.
First of all, you have to select the $~3~$ bins that will have $~4~$ balls each, and then the $~4~$ bins that will have $~7~$ balls each.
This can be done in
$$\binom{2022}{3} \times \binom{2019}{4} ~~\text{different ways}. \tag1 $$
So, the expression in (1) above is reserved as a factor to be applied, and I can now assume, without loss of generality, that the $~7~$ special bins have been determined.
Then you have $~(20212022 - 12 - 28) = 20211982 ~$ balls remaining, that must be placed in $~(2022 - 7) = 2015~$ (remaining) distinguishable bins, so that each bin has at least $~19~$ balls.
Letting $~x_k~$ denote the number of balls placed in bin-$k,~$ this implies that I need to compute the number of solutions to :

$x_1 + x_2 + \cdots + x_{2015} = 20211982.$

$x_1, ~x_2, ~\cdots, ~x_{2015} \in \Bbb{Z_{\geq 19}}.$

Basic Stars and Bars theory is based on the lower limit of the variables being $~0,~$ rather than $~19.$  The necessary adjustment is to let
$$y_i = x_i - 19 ~: ~i \in \{1,2,\cdots,2015\}.$$
Then the number of solutions to be computed is equivalent to the number of solutions to

$y_1 + y_2 + \cdots + y_{2015} = 20211982 - (19 \times 2015) = 20173697.$

$y_1, ~y_2, ~\cdots, ~y_{2015} \in \Bbb{Z_{\geq 0}}.$

Per Stars and Bars theory, the number of solutions is
$$\binom{20173697 + [2015 - 1]}{2015 - 1} = \binom{20175711}{2014}.$$
Therefore, the final enumeration is
$$\binom{2022}{3} \times \binom{2019}{4} \times \binom{20175711}{2014}.$$

Note:
The problem would have been much more difficult if any of the $~7~$ special bins had been required to have exactly $~r~$ balls, where $r \geq 20.$  Then, Inclusion-Exclusion would have been needed.
In this particular problem, there can be no intersection between the $~7~$ special bins, and the remaining bins that have at least $~19~$ balls each.
So, that complication was avoided.


Cannot Resolve Method isChecked()

Question: Estou tentando utilizar o método isChecked() mas está dando a mensagem "Cannot Resolve Method IsCheked().Alguém consegue me ajudar?
  @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = act.getLayoutInflater().inflate(R.layout.activity_layout_lista_amc, parent, false);

        final AvaliacaoMensal mensalAmc = mensal.get(position);

        //pegando as referências das Views
        TextView potencial = (TextView) view.findViewById(R.id.potencialLetra);
        TextView questao = (TextView) view.findViewById(R.id.questao);
        TextView titulo = (TextView) view.findViewById(R.id.titulo);

        //populando as Views
        potencial.setText(String.valueOf(mensalAmc.getPotencial()));
        questao.setText(String.valueOf(mensalAmc.getQuestao()));
        titulo.setText(String.valueOf(mensalAmc.getTitulo()));

        RadioGroup radioGroupAmc = (RadioGroup) view.findViewById(R.id.radioGroupAmc);
        flag = mensal.get(position);

        radioGroupAmc.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch(checkedId) {
                    case R.id.sim:
                        flag.radioButtonValues[0] = group.isChecked();
                        // trata radioValor1
                        break;
                     case nao:
                       flag.radioButtonValues[1] = group.isChecked();
                        // trata radioValor2
                        break;
                    case na:
                        flag.radioButtonValues[2] = group.isChecked();
                        // trata radioValor3
                        break;
                }

            }
        });

        return view;
    }

}

Comment: O que é que você pretende fazer?
Comment: @ramaral Salvar se o radioButton foi ou não selecionado
Answer: Esse método não existe na classe RadioGroup.
O RadioButton que foi seleccionado é aquele cujo id está em checkedId.  Aquele switch já identifica qual é.
Se quer guardar na flag terá de fazer assim:
switch(checkedId) {
    case R.id.sim:
        flag.radioButtonValues[0] = true;
        flag.radioButtonValues[1] = false;
        flag.radioButtonValues[2] = false;
        // trata radioValor1
        break;
     case R.id.nao:
        flag.radioButtonValues[0] = false;
        flag.radioButtonValues[1] = true;
        flag.radioButtonValues[2] = false;
        // trata radioValor2
        break;
    case R.id.na:
        flag.radioButtonValues[0] = false;
        flag.radioButtonValues[1] = false;
        flag.radioButtonValues[2] = true;
        // trata radioValor3
        break;
}

Um RadioGroup só pode ter um RadioButton seleccionado, ao atribuir true a uma posição do array deve atribuir false às outras.
Há maneira de fazer isso de forma "mais elegante" mas na falta de mais informações é o que posso sugerir.
Answer: Primeiro você precisa instanciar os RadioButton, não só o RadioGroup.
Uma solução com um RadioGroup com três RadioButton.
RadioGroup group = (RadioGroup) view.findViewById(R.id.radio);
final RadioButton[] rb = {
        (RadioButton) view.findViewById(R.id.sim),
        (RadioButton) view.findViewById(R.id.nao),
        (RadioButton) view.findViewById(R.id.na)
};
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
            switch (checkedId) {
                case R.id.sim :
                    flag.radioButtonValues[0] = rb[0].isChecked();
                    break;
                case R.id.nao :
                    flag.radioButtonValues[1] = rb[1].isChecked();
                    break;
                case R.id.na :
                    flag.radioButtonValues[3] = rb[2].isChecked();
                    break;
            }
        }
    });

O RadioButton não possui o método isChecked(), como consta na referência do RadioButton aqui, até porque o grupo possui vários botões, então não tem como saber se o grupo está todo selecionado, ficaria meio confuso até se existisse essa possibilidade.
Aí com os RadioButton instanciados tu consegue saber se ele está checado ou não com o método idChecked().
Imagino que seja menos gambiarra dessa forma.
Pode ser melhorado, mas a intenção é essa.
Comment: Muito obrigado!
Comment: Sem problemas =)

Cannot Resolve Method isChecked()


Delete a large chunk of lines from a huge file

Question: I have a 2.8GB sql dump file in linux. I need to delete lines 18 through 3000, but I can't open it in a text editor like vim because it's so big.
Any suggestions? I assume you could use sed or something similar, I just don't know the command.
Answer: I think that's what sed was designed for:
<code>sed '18,3000d' input.txt > output.txt
</code>
Resources

Sed man page
Comment: I love sed! sed rocks!


SpacePy: "ImportError: cannot import name irbempylib"

Question: I downloaded spacepy via easy_install.exe in the command prompt and this code doesn't raise an error:
import spacepy.coordinates as coord
from spacepy.time import Ticktock
import numpy as np
def geotomag(alt,lat,lon):
    #call with altitude in kilometers and lat/lon in degrees
    Re=6371.0 #mean Earth radius in kilometers
    #setup the geographic coordinate object with altitude in earth radii
    cvals = coord.Coords([np.float((alt/Re+Re))/Re,np.float(lat),np.float(lon)], 'GEO', 'sph',['Re','deg','deg'])
    #set time epoch for coordinates:
    cvals.ticks=Ticktock(['2012-01-01T12:00:00'], 'ISO')
    #return the magnetic coords in the same units as the geographic:
    return cvals.convert('MAG','sph')

When I try to call the function, however, it gives me "ImportError: cannot import name irbempylib".
Irbempylib is supposed to be included in spacepy as evidenced by the help doc:
PACKAGE CONTENTS
    LANLstar
    coordinates
    data_assimilation
    datamodel
    empiricals
    irbempy (package)
    lib
    omni
    plot (package)
    poppy
    pybats (package)
    pycdf (package)
    radbelt
    rst
    seapy
    spacepy_EnKF
    time
    toolbox (package)

Any ideas?
Answer: I also had had this problem. I think it is to do with the missing source files for irbempylib with a particuar version of SpacePy.
To fix this I deleted all references to SpacePy (files and folder(s)) from the "site-packages" folder of your Python installation. Then did a fresh install of SpacePy using Pip: pip install spacepy. This downloaded and installed SpacePy version 0.1.5.
Re-run your Python code and it should work (it did for me!)


Solve $\frac{dy}{dx} + 4y = 5 \sin 3x$

Question: $$\frac{dy}{dx} + 4y = 5 \sin 3x$$
It is already in standard form, so our integrating factor is:
$$v(x) = e^{\int{4dx}}=e^{4x}$$
$$\frac{d}{dx} y \cdot e^{4x} = 5 \sin3x$$
$$y = \int {(5e^{-4x} \cdot \sin 3x)dx}$$
$$y = 5\int {(e^{-4x} \cdot \sin 3x)dx}$$
Well, that integral looks badly. I heard there is other metod which uses "guessing" the solution.
Answer: Regarding what you've heard, yes, there's a method with ``guessing'', and it involves, hm, guessing :) But first, you solve homogeneous part of the problem
$$
y' + 4y = 0
$$
which has a solution $y = C e^{-4x}$.
Now, you need to guess a particular (any solution that satisfies the equation) solution. Looking at the right hand side, you might guess it as $y_p = A \sin 3x + B \cos 3x$. Now substitute it to the equation,
$$
3A \cos 3x  - 3B \sin 3x + 4A\sin 3x + 4B\cos 3x = (3A + 4B) \cos 3x + (4A - 3B) \sin 3x = 5\sin 3x
$$
from which you can conclude that
$$
3A + 4B = 0 \\
4A - 3B = 5
$$
or
$$
A = \frac 45 \\
B = -\frac 35
$$
and finally
$$
y_p = \frac 45 \sin 3x -\frac 35 \cos 3x
$$
Now, you use principle of superposition to make a final conclusion, that the general solution of the problem given is
$$
y = C e^{-4x} + \frac 45 \sin 3x -\frac 35 \cos 3x
$$
Comment: Try "the method of undetermined coefficients".
Comment: I made some algebraic mistake... Fixing it.
Comment: Hopefully fixed it.
Comment: Thank you very much. Is there a general name of method you used?
Comment: @SimonRose at least that what I was taught. But google gives exactly this name for the method above. So, disregard my previous comment. If it's widely used under that name, then why not. It's just a matter of convention.
Answer: Try following: solution of each differential equation can be split in two parts - natural response  and particular response (RHS of your equation). Natural response is the one that you get while solving with RHS=0, and the particular response or solution can be guessed like this:
Assumption: $y_{natural}=Ce^{rx}$ where r denotes the solution of algebraic equation you get with RHS=0:
$\frac{d(Ce^{rx})}{dx}+4Ce^{rx}=0 $
$Cre^{rx}+4Ce^{rx}=0 /:Ce^{rx}$
$r+4=0$
$r=-4$
$y_{natural}=Ce^{-4x}$
excitation function: $=5sin(3x)$ so try with:
$y_{particular}=Asin(3x)+Bcos(3x)$, take care that $y_{particular}$ and  $y_{natural}$ must
be linearly independent - can't sum them up!
now calculate:
$y_{particular}=Asin(3x)+Bcos(3x)$
$\frac{dy}{dx}=3Acos(3x)-3Bsin(3x)$
put them in your equation and you get:
$3Acos(3x)-3Bsin(3x)+4Asin(3x)+4Bcos(3x)=5sin(3x)$
$(-3B+4A)sin(3x)+(3A+4B)cos(3x)=5sin(3x)$
and you get an A/\B system of equs:
$-3B+4A=5$
$3A+4B=0$
solving this you get:
$B=\frac{-3}{5}$
$A=\frac{4}{5}$
and finally:
$y_{particular}=\frac{4}{5}sin(3x)-\frac{3}{5}cos(3x)$
your total solution is now:
$y_{total}=y_{particular}+y_{natural}$
and that's about it.
Answer: This integral isn't nearly as bad as it looks. Integrate by parts twice to solve it.

Solve $\frac{dy}{dx} + 4y = 5 \sin 3x$


JsonView annotation for all fields of a object

Question: How can I include all fields of Java object to the JSON response (view) with out specify @JsonView on every field of that Java object?
Edit: I need this to achieve with out the use of another external library.
Comment: possible duplicate of [JSON formatting for a Java Server](http://stackoverflow.com/questions/32331325/json-formatting-for-a-java-server)
Comment: I asked with out using a external library like gson
Comment: Without library? Write your own regex methods.but it can take a long time :)
Comment: i mean without additional library. I like to achieve it using @JsonView or any other way by utilizing jackson jar
Answer: This is a common problem with @JsonView. The annotation is applicable only on methods and properties, so you cannot just annotate the whole class and include all properties.
I'm going to assume you are using this with Spring. That behavior is due to the fact that Spring chooses to disable inclusion of all properties by default in the ObjectMapper. Because of that, only @JsonView annotated properties will be included, while other properties will not.
You can change this by setting MapperFeature.DEFAULT_VIEW_INCLUSION to true. Like this:
Plain Java:
mapper.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true);

Spring Boot (add this to application.properties):
spring.jackson.mapper.default-view-inclusion=true

This way by default all properties will be included during JSON seralization, and you can use @JsonInclude and other Jackson annotations to control inclusions and exclusions.
Comment: I have spring.jackson.mapper.default-view-inclusion=true but the properties are not included anyways. Also, spring.jackson.mapper.default-view-inclusion=true is by default
Comment: Worked for me with spring boot 2.3.3 version.
Comment: Checked for 2.5.x- works! All fields without view included for view marked controller response
Answer: Since Jackson 2.9, the @JsonView annotation can now be applied to the class level to have the behavior you are asking for.
https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.9

Allow use of @JsonView on classes, to specify Default View to use on non-annotated properties.

@JsonView(MyView.class);
public class AllInView {

    // this will be in MyView
    private String property1;

    // this will be in MyView
    private String property2;

    // this will be in DifferentView
    @JsonView(DifferentView.class)
    private String property3;

}

Comment: this didn't work for me for some reason and I have jackson 2.12


Find the domain and range of a rational function $f(x)=\frac{3x-1}{x+2}$ and its inverse

Question: This is a homework question from a precalculus class that I'm a TA for.

Let $f$ be the function given by
  $$ \tag{$\star$}
f(x) = \frac{3x-1}{x+2}
\,.
$$
  Calculate $f^{-1}(x)$. What are the domains and ranges of $f$ and $f^{-1}$? What do you notice about this?

I wanted to write up a thorough solution to this exercise for my class, and figured I'd post it online to help anyone else who may wander across it.
Answer: If you make a square of numbers indicating the four coefficients used here, you get
$$
\left(
\begin{array}{rr}
3 & -1 \\
1 & 2
\end{array}
\right)
$$
If you then interchange the two numbers on the "main diagonal," meaning the upper left corner to the lower right corner, then negate the other two numbers (off the main diagonal) you get
$$
\left(
\begin{array}{rr}
2 & 1 \\
-1 & 3
\end{array}
\right)
$$
Alright, you began with function
$$ \frac{3x-1}{1x+2}. $$
The inverse function comes from the new square of numbers,
$$ \frac{2x+1}{-1x+3}. $$
This works EVERY TIME.
Answer: Since $f$ is a rational function, it's defined for all real numbers except where the denominator is equal to zero, so the domain of $f$ is $\mathbb{R}\setminus \{-2\}$. Now the range of $f$ is all real numbers $y$ such that there exists some input $x$ where $f(x) = y$. So to find all such $y$, we may simply take $f(x)$ equal to some arbitrary output $y$ and rearrange the rational expression ($\star$) so find for which $y$ there exists such an $x$:
$$
\tag{$\star\star$}
\begin{align}
f(x) &= y
\\[1em]\implies\qquad
\frac{3x-1}{x+2} &= y
\\[1em]\implies\qquad
x &= \frac{2y+1}{-y+3}
\end{align}
$$
We see that such an $x$ exists for all outputs except $y=3$, where the denominator of the rational expression for $x$ is zero. So the range of $f$ is $\mathbb{R}\setminus\{3\}$.
Now thinking about $f^{-1}$, notice this: if $f(x) = y$, and since $f^{-1}$ is defined to be the function such that $f^{-1}(f(x)) = x$, we get that
$$
\begin{align}
&\quad f(x) = y
\\[1em]\quad\implies&\quad
f^{-1}(f(x)) = f^{-1}(y)
\\[1em]\quad\implies&\quad
x = f^{-1}(y)
\\[1em]\quad\implies&\quad
\frac{2y+1}{-y+3} = f^{-1}(y)
\,.
\end{align}
$$
This is an expression for $f^{-1}$! Now if were to insist that the input to a function be named $x$, we could change all the $y$s in that expression to $x$s (they are just names for variables after all, where we usually let $x$ be "inputs" and $y$ be "outputs") and get an honest expression for $f^{-1}(x)$ as
$$
\frac{2x+1}{-x+3} = f^{-1}(x)
\,.
$$
From this we see that the domain of $f^{-1}$ must be $\mathbb{R}\setminus\{3\}$. Now to find the range of $f^{-1}$ we'd do the same procedure we did before in  ($\star\star$). But this would just be doing the algebraic steps in ($\star\star$) in reverse order, and we come back to an expression for the outputs of $f^{-1}$ that just looks like the original function $f$. So the range of $f^{-1}$ will be all real numbers except for $-2$. So in this example, the range of $f$ is the domain of $f^{-1}$, and that the domain of $f$ is the range of $f^{-1}$.


Ruby valid license plate regex

Question: I am trying to write a function in RUBY that will determine if a given string is a valid license plate. A valid license plate has the following format: 3 capital letters, followed by a dash, followed by 4 numbers. EX: HES-2098.
I have written the following function but need some help for pattern matching.
def liscence()
  plate = "HES-2098"
  plateNo = plate.upcase
 if(plate.length == 8)

   if(plate == plateNo)

    if(/\A-Z\A-Z\A-Z\-\d{4}/.match(plate))
       puts "valid"
     else
       puts "invalid"
     end

   else
     puts "First 3 letter must be uppercase"
   end

 else
   puts "Only 8 char long"

 end

end

liscence()

Comment: `if(/\A[A-Z]{3}-[0-9]{4}\z/.match(plate))`
Comment: Thanks! Solved my problem
Comment: I posted the pattern with the explanations and demos.
Comment: [Rubular](http://rubular.com) is your friend. See http://rubular.com/r/9Kn4fk96ci
Answer: plate[/\A[A-Z]{3}-\d{4}\z/] ? 'valid' : 'invalid'

Comment: afaik `plate =~ /regex/` is ~30% faster than `plate[/regex/]`
Comment: @smefju it can't be just `N% faster` because it should strongly depend on the string length, plus it can't anyhow differ when there is no match. For _plates_ thinking about this is called "premature optimization." There should be used a variant, that is easier to read and in my opinion the one I introduced reads better. The latter is of course disputable :)
Answer: Your regex did not work because \A matches a string start position that can only be one in a string (and you have three). To match an uppercase ASCII letter, you may use a [A-Z] character class.
You can use
if /\A[A-Z]{3}-[0-9]{4}\z/ =~ plate

See the regex and Ruby demos.
Pattern details:

\A - start of string (not line, as with ^) anchor
[A-Z]{3} - exactly 3 (since the limiting quantifier {n} is used) uppercase ASCII letters (from the [A-Z] range in a character class)
- - a literal hyphen (not necessary to escape it outside the character class)
[0-9]{4} - exactly 4 ASCII digits
\z - the very end of the string anchor (not $ that matches the end of the line)

Ruby valid license plate regex


Make interaction between multisites

Question: Well look I have a multisite.
The main site that uses the sites/all folder for its modules and libreries
and another subdomain that uses sites/subdomain for its modules, themes, etc...
they are in separate databases but share the same drupal instance
In the main site I have an inventory app for a local store they manage their inventory and their invoices there.
In the subdomain I did a ecommerce store so they will sell their products through that subdomain, I need that when an user buys a product in the ecommerce store ten automatically I need that purchase to be registered in the inventory that is in the other site.
How to do this connection betweed the 2 sites in a multisite config?
Thanks for your help
Comment: This is a brilliant question and exactly what I want to do. Did you find a way to write the module yourself?
Comment: Hi I just wrote an answer, read it, look for it below. Regards
Answer: The "standard" way of creating multiple sites with some interaction between them using Drupal is to use the Domain Access module rather than multisites.
However, there is great write-up about using interacting multisites at Drupal Redesign: Domain Access vs. Multisite.
The gist of it is that you set up a multi-site as you normally do. Then you create a custom module to take care of interaction and sharing data between the sites.
Drupal can handle multiple databases, but you can avoid the complexity of managing multiple databases by using a single database and use a database table prefix to avoid namespace collisions.  If you already have created the databases as separate databases, you need to use hook_update_N (or SQL operations outside of Drupal) to refactor the schemas.
Comment: ho ok but what option do I have, because I already have the store in a multisite
Comment: @SsouLlesS, I do not understand the question. There is nothing stopping you from creating a custom module to take care of interaction and sharing data between the sites when you already have the store in a multisite.
Comment: I need to know how to share that data, there is a module for that prupose?
Comment: @SsouLlesS, there is no existing module. You need to write it yourself.
Comment: @Free Radical  Would this be too advanced for someone who has never created their own module before? I mean, what level of understanding is necessary to jump from the basics to such a thing as communication between sites?
Comment: @cea, AFAIK, there does not exist any *easier* way to do this. So if doing it this way is beyond your skills, you either hire a consultant, or you don't do it at all.
Comment: Well I think I will take it on as a learning project :)
Comment: I don't do computers for money- just for fun and for saving money if i need to help myself or others.
Answer: Hi look what I finally did, was to create an API using the services module, for the inventory and used the services module also in the ecommerce store for send requests to the inventory app API, however it was not very easy to manage, and the website become very big and very buggy so I finally gave up with Drupal for this project, so I decided to start from scratch with Ruby On Rails, if you want to learn Ruby On Rails from scratch I recommend you railskarate.com it is totally free, start from the first video and subscribe to their youtube channel, they upload a new lesson everyday.
Hope this help you! Regards!

Make interaction between multisites


Is $[x,0]=0 $ for all $x \in L$

Question: I am following a book for lie algebra and i am completely new to this field. I have to show following
Let L be a lie algebra with [,] as bracket on it. Actually I want to show that $\{0\}$ is ideal in L. For this purpose all I need is to show that
$\forall\; x\; in\; L \; and \;y=0 \in \{0\} $ we have $[x,y=0]=0$.
I am not sure how to argue? what I am thinking is $[x,0]
=0$ for all x in L but how? . Which I have not read in book. Need guidance. Thanks in Advance.
Answer: The map $(x,y)\mapsto[x,y]$ is bilinear. In particular, for each $x\in L$, the map $y\mapsto[x,y]$ is linear. And every linear map maps $0$ into $0$.
Comment: So nice. So this means can I conclude for every bilinear map say $\phi: U \times V \rightarrow W$ that $ \phi(u,0)=0\; \forall u\; \in \; U$ and $ \phi(0,v)=0\; \forall v\; \in \; V$
Comment: Yes, that is correct.
Comment: Thank so you much it helps a lot a lot !!!
Comment: Thank You so much sir

Is $[x,0]=0 $ for all $x \in L$


Unhandled promise rejection while using Microsoft bot framework

Question: I am using microsoft bot framework and trying to integrate it with external HTTP calls.
However when i invoke the BotWorker.say in the handler BotKitConversation's ask i start getting
 (node:5711) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot perform 'get' on a proxy that has been revoked

Below is my code
In my handler function's callback i get some values returned from external function.
Then i try to perform bot.say on the response returned and i recieve the above mentioned error.
 myDialog.ask('What would like to hear?', [
    {
        pattern: '.*',
        handler: async (response, convo, bot) => {
            await YoutubeHelper.getChannel(response, convo,async function(channels){
            console.log("value returned " + channels.length);
            try {
            await bot.say('Printing values'); //error comes here

            if (channels.length == 0) {

                await bot.say('No items found.');
              } else {
                await bot.say('This items\'s ID is %s. Its title is \'%s\', and ' ,
                            channels[0].id,
                            channels[0].snippet.title
                            );
              }
            }catch (err) {
                console.log('error occurred' , err);

            }
            });
        }
    }

],  {key: 'name'});
}

where myDialog is an object of BotkitConversation.
Below is the code for my external utility class
 /**
 * Lists the names and IDs of up to 10 files.
 *
 *
 */

var {google} = require('googleapis');

var {myDialog} = require("./bot")

const getChannel = function getChannel(searchTerm, convo,callback) {

    var service = google.youtube({
      version : 'v3',
      auth : '<client id>'});

    service.search.list({
      part: 'id,snippet',
      q: searchTerm
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No items found.');
      } else {
        console.log('This items\'s ID is %s. Its title is \'%s\', and ' ,
                    channels[0].id,
                    channels[0].snippet.title
                    );
      }
      console.log(channels.length);
      callback(channels);
    });
    //

  }

  module.exports ={
    getChannel
}

I found this document regarding the error . I am coding according to the guidelines mentioned.
Best Regards,
Saurav
Comment: Can you post the package name and code you are using for your `YoutubeHelper" class?
Comment: @StevenKanberg thanks for looking it up...i have posted the code for my utility class..please have a look
Answer: Try to put first await function in a try-catch block:
myDialog.ask('What would like to hear?', [
  {
    pattern: '.*',
    handler: async (response, convo, bot) => {
      try {
        await YoutubeHelper.getChannel(response, convo, async function (channels) {
          console.log("value returned " + channels.length);
          try {
            await bot.say('Printing values'); //error comes here

            if (channels.length == 0) {
              await bot.say('No items found.');
            }
            else {
              await bot.say('This items\'s ID is %s. Its title is \'%s\', and ',
                channels[0].id,
                channels[0].snippet.title
              );
            }
          }
          catch (err) {
            console.log('error occurred', err);

          }
        });
      }
      catch (error) {
        console.log('----::error::----', error)
      }
    }
  }

], { key: 'name' });

Comment: Ok. Follow [this](https://github.com/howdyai/botkit/issues/1951) issue or create new one in its repo. I can't help here. Good luck. @saurav
Comment: thanks for the reply ...i assume you wanted to check the error ....but doing this is giving me the same error
Comment: you get `UnhandledPromiseRejectionWarning` again?! Are you sure the problem is this function?! @saurav
Comment: error comes when i invoke bot.say function...if i remove the call to bot.say then there is no error...
Comment: did you check [this](https://github.com/Microsoft/botbuilder-js/issues/606) or [this](https://stackoverflow.com/questions/52969703/bot-framework-v4-typeerror-cannot-perform-get-on-a-proxy-that-has-been-revo)?? does `bot.say` return promise?
Comment: yes it does https://github.com/howdyai/botkit/blob/77e309b9522f1c654dcc68332c11d80fae3d2a53/packages/botkit/src/botworker.ts#L99
Answer: Problem was with my helper class which was not an awaitable function.
I made changes in my helper function to make it return a promise and it started working.
Below is the updated code
 function askMusicPreferences(answer, convo, bot){

myDialog.ask('What would like to hear?', [
    {
        pattern: '.*',
        handler: async(response, convo, bot, message) => {
            try {
            var channels = await YoutubeHelper.getChannel(response);

            if (channels.length == 0) {

               await bot.say('No items found.');
            }
               else {
                await bot.say(`This items\'s ID is ${channels[0].id}. Its title is ${channels[0].snippet.title}`);
              }

            }catch (error){
                console.log( 'error occurred ', err);
            }

       // }
        // catch (err){
        //     console.log('error occurred', err);
        // }

    }
}
],  {key: 'name'});
}

My helper class

    /**
 * Lists the names and IDs of up to 10 files.
 *
 *
 */

var {google} = require('googleapis');

var {myDialog} = require("./bot");

const getChannel = function getChannel(searchTerm) {
    return new Promise(function(resolve,reject) {

    var service = google.youtube({
      version : 'v3',
      auth : '<client id>'});

    service.search.list({
      part: 'id,snippet',
      q: searchTerm
    }, function(err, response) {
      if (err) {
        console.log('The API returned an error: ' + err);
        reject(err);
        return;
      }
      var channels = response.data.items;
      if (channels.length == 0) {
        console.log('No items found.');
      } else {
        console.log('This items\'s ID is %s. Its title is \'%s\', and ' ,
                    channels[0].id,
                    channels[0].snippet.title
                    );
      }
      console.log(channels.length);
      resolve(channels);
      //callback(channels);
    });
    //
  });

  }

  module.exports ={
    getChannel
}


Possible error in Wikipedia article for $(p,q)$ tensor space notation

Question: I am currently studying tensor space theory (over a field) and was reading through the generic Wikipedia article for tensors when I cam across the following:

A type $(p,q)$ tensor $T$ is a multilinear map, $T: \underbrace{V^*\times...\times V^*}_\text{p-copies}\times\underbrace{V\times...\times V}_\text{q-copies} \to K$ where $K$ is the underlying field.

Where the author(s) then goes onto define the notation for the  tensor product as

$\underbrace{V\otimes...\otimes V}_\text{p-copies} \otimes \underbrace {V^* \otimes...\otimes V^*}_\text{q-copies}$

My question: why did they flip the usage of the $p$'s and $q$'s? Namely, they first called copies of the dual space $p$ and copies of the vector space $q$ $-$ then went on to reverse this in the notation for the tensor product (calling copies of the vector space $p$ and copies of the dual $q$). Is this an error that should be corrected? I also noticed this in a lecture by Frederic P. Schuller on tensor space theory. He did the exact same thing when defining what a tensor is and then the tensor product (at minute $29:23$).
Please correct me if I am wrong, as I am new to differential geometry and tensors.
Comment: Let's start with what a $(0,1)$-tensor $T$ is. By the definition given, it is a linear function $T: V \rightarrow K$ and therefore is an element in the dual vector space $V^*$. On the other hand, a $(1,0)$-tensor is a linear function $T: V^* \rightarrow K$ and therefore is an element in $(V^*)^* = V$. Etc.
Comment: @Deane : pedantically crossing tees: assuming that our vector spaces are finite dimensional... as they are meant (of course) to be here and in Wikipedia, one supposes.
Comment: @Deane - ahhh, I see what you mean. Thank you very much for clarifying! I think I have been staring at this stuff for too long - probably time for a coffee break!
Answer: Note that a $(0,1)$-tensor is a linear map $V \to K$, so $V^*$ is the space of such tensors. Similarly, a $(1,0)$-tensor is a linear map $V^* \to K$, so $V^{**}$ (which can be canonically identified with $V$ if we are in finite dimensions) is the space of such tensors.
Now, if $v \in V$ and $f \in V^*$, define $v \otimes f : V^* \times V \to K$ by $(v \otimes f)(g,w) = g(v)f(w)$. Note then that $v \otimes f \in V \otimes V^*$, not $v \otimes f \in V^* \otimes V$. Thus $$v \in V,\, f \in V^* \implies v \otimes f \in V \otimes V^*$$ as naturally one would like it to happen.
Answer: No, this is correct. The asterisk denotes the dual, i.e. if $V$ is a $K$-vector space, $V^*$ is the vector space of linear maps $V \to K$. Intuitively, in the first case, you have something very similar (except with multilinear maps). In the second case, you are missing this step (the "$\to K$" part), so you have to dualize everything in the domain, which gives you the swapped asterisks (as $V^{**} = V$ for finite-dimensional vector spaces).


Alternative Proof that ln(2) is Irrational

Question: Is the following a correct proof that $\ln(2)$ is irrational without using the fact that $e$ is transcendental?
The power series $\ln(1+x)=\sum_{k=1}^\infty \frac{(-1)^{k+1}x^k}{k}$ for $x=1$ gives the alternating harmonic series $\ln(2)=\sum_{k=1}^\infty \frac{(-1)^{k+1}}{k}$. Consider the sequence of partial sums $x_n = \sum_{k=1}^n \frac{(-1)^{k+1}}{k}$.
Odd terms are decreasing: $x_1>x_3>x_5...$ while even terms are increasing: $x_2<x_4<x_6<...$. All odd terms are greater than even terms: $x_{2n+1}>x_{2m}$ for all $m,n \in \Bbb N \cup \{0\}$. It can be proven that $ \{x_n\}$ is a Cauchy sequence (omitted since it is relatively long):
$$ \forall 0<\epsilon \in \Bbb Q \ \ \exists N \in \Bbb N: \lvert x_n - x_m \rvert < \epsilon \ \ \forall n,m \geq N$$
Let $q$ be the limit of $\{x_n\}$, which we know exists by the Alternating Series Test.
If $q \leq x_{2m}$ for some $m \in \Bbb N \cup \{0\}$ consider $\epsilon = x_{2m + 2} - x_{2m} > 0$ and any $N \in \Bbb N$. Choose $n \in \Bbb N$ such that $2n > 2m+2 + N$. Then
$$ \lvert x_{2n} - q \rvert = x_{2n} - q > x_{2n} - x_{2m} > x_{2m+2} - x_{2m} > \epsilon $$
So $q \nleq x_{2m}$ and similarly prove $q \ngeq x_{2n+1}$. So $x_{2m} < q < x_{2n+1}$ for all $m,n \in \Bbb N \cup \{0\}$.
I am concerned that this part is insufficient to show $q = \ln(2)$ is irrational:
Since sequence $\{x_n\}$ is Cauchy, we have:
$$\forall 0 < \epsilon \in \Bbb Q \ \ \exists N \in \Bbb N: \lvert x_{2n+1} - x_{2m} \rvert < \epsilon \ \ \forall \ \ 2n+1,2m \geq N$$
Let $0 < \epsilon \in \Bbb Q$ be arbitrary. Then for $2n+1,2m \geq N$,
$$ 0 < q- x_{2m} < x_{2n+1}-x_{2m} = \lvert x_{2n+1}-x_{2m} \rvert < \epsilon$$ That is, $\forall 0 < \epsilon \in \Bbb Q, \ \ 0 < q- x_{2m} < \epsilon$. So $q = \ln(2)$ cannot be rational, else $q-x_{2m}$ is rational.
Thanks for your help in advance.
Comment: Consider the series: $\sum\limits_{n=0}^{\infty} \frac{1}{2^n}$. The above series converges to 2 is a known fact. Now, given any $\epsilon$ with $0<\epsilon \in \mathbb{Q}$, there exists an $N$ such that for all $n\geq N$, we would have $0<2-x_n <\epsilon$, where $x_n$ is the $n$-th partial sum of the series. Can we now conclude that $2$ is not rational? What is missing?
Comment: @YathirajSharma I think that an even better example (since it's more similar to $\displaystyle\sum_{n=1}^\infty\frac{(-1)^{k+1}}k$) would be $\displaystyle\sum_{n=1}^\infty\frac{(-1)^{n-1}}{2^{n-1}}$, whose sum is $\displaystyle\frac23$.
Comment: I doubt that there is an easier proof than using the Lindemann Weierstrass theorem. A sequence of rational numbers can tend to a rational or irrational number. Without something additional , we cannot conclude anyhing. I read some years ago a "proof" that $\gamma$ is irrational containing the fundamental flaw that it assumed that a sequence of irrational numbers cannot converge to a rational number. This is a counterpart to the flaw here.
Comment: @YathirajSharma So it is possible for a rational number $a > 0$ to be less than all rational numbers including itself? I am confused.
Answer: Firstly, we should be careful with some terminologies. In the place of '$\forall 0<\epsilon \in \mathbb{Q}$', we should have used, 'given any $\epsilon, 0<\epsilon \in \mathbb{Q}'$. This is because the value of $N$ depends on the $\epsilon$ chosen.
Now coming to the confusion: Assuming that $\{x_n\}$ is a sequence of rational numbers and it converges to $q$ and that $x_n \leq q$ for all $n$. You are assuming that there exists an $N$ such that for all $n\geq N$, the following inequality holds good for all rational numbers $\epsilon$:
$$0< q-x_n < \epsilon.$$
No, this is not the case. It cannot even happen. Fix an $n\geq N$. Then there must exist a positive rational number $\epsilon$ which is strictly less than $q-x_n$. Isn't it? Forget about rational or irrational, choose any positive real $\epsilon$. If there exists an $N$ such that for all $n\geq N$ we have $q-x_n < \epsilon$, then it  simply means that $q-x_n< \epsilon$ and that there are possibilities that it can even be rational say $\epsilon^{\prime}$ for some $n$. Now, we cannot argue that $q-x_n$ should have been less than $\epsilon^{\prime}$, which is what you are doing.
Answer: When you say $$\forall 0 < \epsilon \in \Bbb Q, \ \ 0 < q- x_{2m} < \epsilon$$
you are treating $2m$ as if it was fixed as $\epsilon$ changes, and yet in deriving your inequality you required $2m\ge N$, and $N$ may change as $\epsilon$ changes.
And in fact (though we haven't proved it), it is almost certain that $N$ gets unboundedly larger as $\epsilon$ gets smaller, eventually exceeding any fixed value of $2m$.
I.e. for any fixed value of $2m$, as $\epsilon $ gets small, $N$ gets large, and eventually it gets larger than $2m$, and you can no longer say that $\lvert x_{2n+1}-x_{2m} \rvert < \epsilon$.
Comment: Your answer is excellent. Thank you.
Comment: @monkeyking - You can always change which answer to accept with the check mark. :-)


python dataframe group rows based on row num

Question: I have a dataframe with 40 rows,
and I want to iterate over it so I will have 4 iteration with 10 rows each, serially.
So group#0 will be rows 0-9 , group#1 will be rows 10-19 and so on.
How can I do it?
Comment: You should write your question in [a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example)
Answer: import pandas as pd
import numpy as np

df1 = {
    'State':['Arizona','Georgia','Newyork','Indiana','Florida'],
   'Score1':[4,47,55,74,31]}

df1 = pd.DataFrame(df1,columns=['State','Score1'])
print(df1)

We need to add value (here 430) to the index to generate row number and the result is stored in a new column as shown below.
df1['New_ID'] = df1.index + 430
print(df1)

Comment: Could you explain in your answer how to iterate over the dataframe now with your proposed solution, as was asked in the Question?
Answer: 2 solutions from this stackoverflow question : How to iterate over consecutive chunks of Pandas dataframe efficiently
I advise you to check the link.
Solution from DSM :
for k,g in df.groupby(np.arange(len(df))//10):
    print(k,g)

Solution from Ryan :
def chunker(seq, size):
    return (seq[pos:pos + size] for pos in xrange(0, len(seq), size))

for i in chunker(df,5):
    print i


How to share image using Branch-io by email in android

Question: I am using Branch-io to share content like Text, image(from URL), and Link.
I am all done and working perfect with Facebook, Whatsapp. I mean I am able to share Text and Link on Gmail also but unable to attach image.
This problem occurred only with Gmail.
Here is my code:
BranchUniversalObject branchUniversalObject = new BranchUniversalObject()
                        .setCanonicalIdentifier("item/12345")
                        .setTitle(title)
                        .setContentDescription(message)
                        .setContentImageUrl(image)
                      //.setContentImageUrl(Uri.parse("file://"+downloadedImagePath).toString())
                        .setContentIndexingMode(BranchUniversalObject.CONTENT_INDEX_MODE.PUBLIC);

                LinkProperties linkProperties = new LinkProperties()
                        .addControlParameter("$always_deeplink", "true")
                        .setFeature("sharing")
                        .setStage("1");
                String body = "Text Message";
                ShareSheetStyle shareSheetStyle = new ShareSheetStyle(mContext, mContext.getResources().getString(R.string.app_name), body)
                        .setAsFullWidthStyle(true)
                        .setSharingTitle("Share With");

                branchUniversalObject.showShareSheet((Activity) mContext,
                        linkProperties,
                        shareSheetStyle,
                        new Branch.BranchLinkShareListener() {
                            @Override
                            public void onShareLinkDialogLaunched() {
                                LogUtils.v("Share Link", "Launched");
                            }

                            @Override
                            public void onShareLinkDialogDismissed() {
                                LogUtils.v("Share Link", "Dismissed");
                            }

                            @Override
                            public void onLinkShareResponse(String sharedLink, String sharedChannel, BranchError error) {
                                LogUtils.v("Share Link", sharedLink);
                            }

                            @Override
                            public void onChannelSelected(String channelName) {
                                LogUtils.v("Share Link", channelName);

                            }
                        });
            }
        }, false);

Let you know again, this code working perfect with Facebook, whatsapp but not with Gmail(only issue not attaching image).
If anyone knows then please let me know what I'am doing wrong and what is the perfect way to do this.
Thanks in advance.
Answer: I just tested this on our test application, and everything works correctly when sharing with Gmail. Here's my test link: https://appsolutely.test-app.link/Mw2qFHRMxJ
Whenever you share a link via showShareSheet(), all the Branch Universal Object (BUO) information is attached to the link. If you keep sharing links for the same data, but across different channels, the only difference between the links for each channel will be "~channel": "Gmail".
Looking at your code, I do not see any issues you could encounter with attaching the image URL, as you do call .setContentImageUrl(image) for the BUO object, so the issue may be in the way you are testing. After sharing your link via Gmail, I would suggest pasting it into the URL bar of your browser and attaching the following query parameter: ?debug=true.

E.g.: https://appsolutely.test-app.link/Mw2qFHRMxJ?debug=true

You could then view all of the data the link contains on the page that opens. If you see that the data contains the "$og_image_url" with the correct URL address, it means that the image was actually attached, as shown in the example below for my test link https://appsolutely.test-app.link/Mw2qFHRMxJ
Comment: Thanks for your reply. I have tested my sharedLink with debug enabled and can see `"$og_image_url":` there. But still image not attached on Gmail.

But I have done this using follwoing code: `shareLinkIntent_.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+imagePath)); // I first download the image then attached here`

This code can be found inside **ShareLinkManager**. And Now The problem is solved :)
Comment: I have this same issue. The image is shown in Facebook Messenger and WhatsApp but not in Gmail. I debugged it as suggested and the image_url is populated and correct. Has anybody at Branch addressed this yet or issued a suggestion for dealing with it?


How can you show this is a proper distribution ??

Question: I derived a posterior distribution, which looks like this:
$$p(\alpha, \beta | y,t) \propto \frac{exp( - \sum_i (\alpha + \beta t_i))\prod_i (\alpha + \beta t_i)^{y_i}}{\prod_i y_i!}$$
How can I show this is a proper distribution of $\alpha$ and  $\beta$?
What distribution would it be to make it integrate to 1?
Comment: Since it's non-negative, integrate it out and show that the result is 1?
Comment: My question is how do you integrate it to 1
Comment: Defined $I = \int p$, then $f = \frac{p}{I}$ is a density function of some distribution. The question is however whether $I$ is finite
Comment: I m looking for an answer which can show me computation steps. If you have any idea, please elaborate a little bit more. I spent the whole afternoon to do this but failed
Comment: Got something from the answer below?
Answer: Assume that every $t_i$ is positive and every $y_i$ nonnegative, and consider the function
$$
q(a,b)=\exp\left(-\sum_{i=1}^n(a+bt_i)\right)\cdot\prod_{i=1}^n(a+bt_i)^{y_i}.
$$
The question seems to be to show that the integral of $q$ on $(0,+\infty)\times(0,+\infty)$ is finite.
To do so, call $t$ the maximal $t_i$ and note that $a+bt_i\leqslant a+bt$ and $\sum\limits_{i=1}^n(a+bt_i)\geqslant a+bt$ for every $(a,b)$ hence $q(a,b)\leqslant r(a+bt)$ with
$$
r(x)=\mathrm e^{-x}x^y,\qquad y=\sum_{i=1}^ny_i.
$$
The change of variable $x=a+bt$, $z=a$, yields
$$
\iint r(a+bt)\mathrm da\mathrm db=\int_0^{+\infty}r(x)\int_0^x\frac{\mathrm dz}{t}\mathrm dx=\frac1t\int_0^{+\infty}xr(x)\mathrm dx=\frac{(y+1)!}t.
$$
Thus there exists some constant $c$ such that $cq$ is a PDF. Note that $c\geqslant t/(y+1)!$ and that, when $n=1$, $c=t/(y+1)!$.