How to load Admob ad whenever it's possible?

Question: Ad shows only if WI-FI was enabled before app start (it won't appear if wi-fi was enabled during app session). How to load Admob ad whenever it's possible? I believe, it's got something to do with AdListener.
My current code:
   adView.setAdListener(new AdListener() {
       @Override
       public void onAdLoaded() {
           adView.setVisibility(View.VISIBLE);
           super.onAdLoaded();
       }

       public void onAdFailedToLoad(int errorCode) {
           adView.setVisibility(View.GONE);
       }
   });

Comment: Please checkout this link hope it will help you..
[Admob][1]

  [1]: http://stackoverflow.com/questions/30213313/admob-not-show-in-lollipop-5-1-device/30213955#30213955
Answer: If your question is to know, shall I load the ad or not based on the conditions required for the ad is satisfied like internet and GooglePlayServices, then you can use the method below:
public static boolean shouldLoadAd(Context context) {
        return (GooglePlayServicesUtil.isGooglePlayServicesAvailable(context) == ConnectionResult.SUCCESS && isNetworkAvailable(context));
    }

public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }

The method shouldLoadAd checks if internet and Googleplayservices is available then load ad else dont load ad, if it returns false you can hide the adView.
Answer: You need a BroadCastReceiver to know when the network status has changed so you can load your adds.
public class NetworkChangeReceiver extends BroadcastReceiver {

public static final int TYPE_WIFI = 1;
public static final int TYPE_MOBILE = 2;
public static final int TYPE_NOT_CONNECTED = 0;
public static final int NETWORK_STATUS_NOT_CONNECTED = 0;
public static final int NETWORK_STATUS_WIFI = 1;
public static final int NETWORK_STATUS_MOBILE = 2;

@Override
public void onReceive(Context context, Intent intent) {
    int status = getConnectivityStatusString(context);

    if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
        if (status == NETWORK_STATUS_NOT_CONNECTED) {
            Log.i("Receiver","not connected");

        } else {
            Log.i("Receiver","connected");
            //If the connectivity changes while using the app and it is connected to a network you can load your adds.
            AddListener.initialiseAds(mAdView, adRequest);
        }
    }
}

public int getConnectivityStatusString(Context context) {
    int conn = getConnectivityStatus(context);
    int status = 0;
    if (conn == TYPE_WIFI ) {
        status = NETWORK_STATUS_WIFI;
    } else if (conn == TYPE_MOBILE ) {
        status = NETWORK_STATUS_MOBILE;
    } else if (conn == TYPE_NOT_CONNECTED) {
        status = NETWORK_STATUS_NOT_CONNECTED;
    }
    return status;
}

public int getConnectivityStatus(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (null != activeNetwork) {
        if(activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return TYPE_WIFI;

        if(activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return TYPE_MOBILE;
    }
    return TYPE_NOT_CONNECTED;
}}

Then we have the AddListener class
public final class AddListener {

private AddListener(){
}

public static void initialiseAds(final AdView mAdView, AdRequest adRequest) {
    mAdView.loadAd(adRequest);
    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            if (mAdView.getVisibility() == View.GONE) {
                mAdView.setVisibility(View.VISIBLE);
            }
            Log.i("Receiver","Add loaded");
            // Code to be executed when an ad finishes loading.
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            super.onAdFailedToLoad(errorCode);
            mAdView.setVisibility(View.GONE);
            Log.i("Receiver","Add error "+errorCode);
            // Code to be executed when an ad request fails.
        }

        @Override
        public void onAdOpened() {
            Log.i("Receiver","Add opened");
            // Code to be executed when an ad opens an overlay that
            // covers the screen.
        }

        @Override
        public void onAdLeftApplication() {
            Log.i("Receiver","Add left app");
            // Code to be executed when the user has left the app.
        }

        @Override
        public void onAdClosed() {
            Log.i("Receiver","Add closed");
            // Code to be executed when when the user is about to return
            // to the app after tapping on an ad.
        }
    });
}}

And from the MainUI class we do this:
public class MainUI extends AppCompatActivity {

...
public static AdView mAdView;
public static AdRequest adRequest;
...

@Override
protected void onDestroy() {

    if (mAdView != null) {
        mAdView.destroy();
    }
    super.onDestroy();
}

@Override
protected void onResume() {
    super.onResume();
    if(mAdView!=null){  // Check if Adview is not null in case of fist time load.
        mAdView.resume();}
}

@Override
protected void onPause() {
    if (mAdView != null) {
        mAdView.pause();
        Log.i("pause","pause view");
    }
    super.onPause();
}

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

    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    mAdView = (AdView) findViewById(R.id.adView);
    mAdView.setVisibility(View.GONE);
    adRequest = new AdRequest.
            Builder()
            .addTestDevice("DDF43F508E61CBB2ACA96D879C66AB42")
            .build();
    AddListener.initialiseAds(mAdView,adRequest);

    ...} }

Also remember to register your BroadcastReceiver in your Manifest
<receiver
        android:name=".utils.NetworkTools.NetworkChangeReceiver"
        android:label="NetworkChangeReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
            <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
        </intent-filter>
    </receiver>

And ask for these permissions:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Comment: I wish you could tell me why the down vote, i did this and it worked fine on my app
Comment: After searching I found that on this thread https://stackoverflow.com/questions/29667164/admob-does-not-refresh-the-ad-after-hiding-showing-adview someone already asked something similar to this and the accepted answer is very similar to what I placed here so I don´t understand why the down vote, please someone clarify.
Comment: i think someone downvoted because it's an old question
Comment: The Ads visibility does not change when the network connection is ON / OFF when the App is running. It only checks on APP start.. How to change Ads visibility when the network connection changes, when the user is inside the APP..


What does params do in angular?

Question: export class D {
  public params: any;
  ....
  agInit(params: any): void {
    this.params = params;
  }
  get businessLine(): string {
   if (this.params.businessLine)
    return this.params.data.businessLine;
  }
 }

The caller does pass a businessLine to this class. However, it does not pass data. Is this data a keyword?
The sample caller code is here,
 cellRenderParams: {
   businesLine: this.businessLine
 }

Answer:
data is not any key word
the params passed to agInit(params: any) is an object  like {data:businessLine}
you could reaname params in agInit(params: any) like _params for just clearity sake
Comment: There is another case `this.params.businessLine`. Why does this one not have `.data`.
Comment: 4.  this.params.businessLine means you check  if you params has businessLine
 like this {businessLine:"examle data"} or  not  like {} ,{blabla :"",}

What does params do in angular?


Binary Image Not Visible From Live Sql Table ASP.NET

Question: On my page I have to upload image and immediate after saving it's displaying on ImageButton control and Image control. It's working local but not on Stagging Server.
I have tried by Image Datatype and VARBINARY(MAX). In both cases image is not displaying on stagging server.
Local Screenshot

Stagging Server Screenshot

Below is my code:
Html:
<!DOCTYPE html>

    <title></title>

    <form id="form1" runat="server">
        <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Submit" />

        <asp:Image ID="Image1" runat="server" Width="150px" />
        <asp:ImageButton ID="ImageButton1" runat="server" Width="150px"/>
    </form>

vb Code:
Imports System.IO
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Partial Class testingImage
    Inherits System.Web.UI.Page
    Dim imageurl As String = ""
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        bindrepeater()
    End Sub

    Sub bindrepeater()
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("dsejConnectionString").ConnectionString)
            Using cmdda As New SqlDataAdapter("select imagefile from testingPhototable where id=1", conn)
                Using ds As New DataSet()
                    cmdda.Fill(ds, "t")
                    If (ds.Tables(0).Rows.Count > 0) Then
                        If ds.Tables(0).Rows(0)("imagefile").ToString() <> "" Then
                            imageurl = "data:image/jpg;base64," & Convert.ToBase64String(CType(ds.Tables(0).Rows(0)("imagefile"), Byte()))
                            ImageButton1.ImageUrl = imageurl
                            Image1.ImageUrl = imageurl
                        End If
                    End If
                End Using
            End Using
        End Using
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim bytes As Byte()
        Using br As BinaryReader = New BinaryReader(FileUpload1.PostedFile.InputStream)
            bytes = br.ReadBytes(FileUpload1.PostedFile.ContentLength)
        End Using

        Dim constr As String = ConfigurationManager.ConnectionStrings("dsejConnectionString").ConnectionString
        Using conn As SqlConnection = New SqlConnection(constr)
            Dim sql As String = "UPDATE testingPhototable set imagefile=@imagefile where id=1"
            Using cmd As SqlCommand = New SqlCommand(sql, conn)
                cmd.Parameters.AddWithValue("@imagefile", bytes)
                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()
            End Using
        End Using
        bindrepeater()
    End Sub
End Class

Comment: Check the `src` property of the `img` tags on your stagging server, what values do you got in there?
Comment: It's binary path value of an image in SRC.
Answer: As the property name ImageUrl is maked from a word like "Url" presumes there needs an URL. So, it's not like the "src" attribute on Client Side where you can set a source as Base64 String as well.
ImageUrl needs a relative url like "root/images/image1.jpg" or absolute "//somewhere_in_the_net_or_server/anotherpath/specificpath/yourfile.jpg". Anyway, to achieve what you want, you can set the attribute "src" of your ImageButton1 as the follow line of code shows:
ImageButton1.Attributes("src") = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAMAAADDpiTIAAAAA3NCSVQICAjb4U....................."

You can do the same for Image1 as well
Comment: Make sure you have enabled your mime types by IIS for the extension you are trying to set (.jpg, .png, or .svg etc..) then if nothing happens seems something go wrong when you are getting to get your img by database or during the conversion in bytes.
Comment: Another thing I have to say is that: Why you are invoking a DataSet making things so heavy to do something you can do easily with SqlDataReader?
Comment: It's working perfectly on Local but still not working on stagging server. Is there any issue from Stagging Server which would stopping image from displaying? Or may be not publishing properly?
Answer: In my web.config
<httpProtocol>
      <customHeaders>
        <add name="X-Frame-Options" value="DENY"/>
        <add name="Strict-Transport-Security" value="max-age=31536000; includeSubDomains" />
        <add name="X-XSS-Protection" value="1; mode=block" />
        <add name="X-Content-Type-Options" value="nosniff" />
        <add name="content-security-policy: frame-ancestors 'none';img-src 'self';object-src 'self';frame-src 'self';child-src 'self';base-uri 'self';"/>
        <add name="Referrer-Policy" value="strict-origin" />
      </customHeaders>
    </httpProtocol>

As listed above under content-security-policy there is a property img-src 'self'; this would stopping binary image to get render on control. I have removed this and now it's working fine.


uniform continuity on compact sets

Question: Suppose that $A$ and $B$ are compact subsets of $\mathbb{R}^{n}$. Consider the function $G(x;a,b)$ where $x\in\mathbb{R}^{n}$, $a\in A$ and $b\in B$. We also suppose that $G(x;a,\cdot)$ is uniformly continuous. If it holds that
\begin{align}
G(x;a,b)\geq \delta
\end{align}
is it possible for all $\lambda\in{\cal B}(b,r)\cap B$  (where  $r=r(b)>0$), to hold
\begin{align}
G(x;a,\lambda)\geq \frac{3\delta}{4}
\end{align}
where ${\cal B}$ stands for the open ball?
I think the answer is yes and has something to do with the uniform continuity. Any suggestion will be highly appreciated.
Comment: No measure-theory in there.
Comment: The answer to the question is YES: choose $\lambda=b$.
Comment: A counterexample to the question you probably *mean* to ask could be $G(x;a,c)=\exp(-c^2x^2)$ around $b=0$.
Comment: Listen, you might want to seriously revise your post. I tried to indicate that the existence of **some** $\lambda$ in $B(b,r)$ is trivial since $\lambda=b$ fits the bill. If you want the inequality to hold for **every** $\lambda$ in $B(b,r)\cap B$, please modify the question--and then ponder the example in my other comment.
Comment: @did, actually i did not understand your example. Could you be more clear please ?
Comment: In the example I mentioned, $G(x,0)=1$ but $G(x,b)\geqslant\frac34$ only if $|x|\leqslant K/|b|$ for some fixed $K$ hence $G(x,\lambda)\geqslant\frac34$ uniformly on $x$ is impossible for each $\lambda\ne0$, and a fortiori uniformly over every $|\lambda|\leqslant r(0)$ as soon as $r(0)\gt0$.
Comment: In think i got it. Actually, this counterexample is the answer to my question. Thank you very much. I appreciate your help.
Answer: A counterexample to the question you probably mean to ask could be
$$
G(x;a,c)=\frac1{1+|c\cdot x|},
$$
around $b=0$. Then, $G(x;a,0)=1$ but $G(x;a,b)\geqslant3/4$ only if $|x|⩽1/(3|b|)$ hence $G(x;a,λ)\geqslant3/4$ uniformly on $x$ is impossible for each $λ≠0$. A fortiori, $G(x;a,λ)\geqslant3/4$ is impossible uniformly on $x$ and on every $|λ|⩽r(0)$ as soon as $r(0)>0$.

uniform continuity on compact sets


What additional tackling rules could the NFL adopt from Rugby in order to improve safety?

Question: For many years the NFL has been changing the rules to improve player safety.
How do tackling rules differ between Rugby and NFL?
What additional tackling rules could the NFL adopt from Rugby in order to improve safety?
From this article, I assume that primary rule change would be to require "players [to] wrap up when attempting to tackle".
What might be other changes?
What is the likelihood that football would adopt these additional changes?
Comment: Your question title and your actual question don't seem to have anything to do with each other.  Could you please make them match?
Comment: Update title to the more specific focus of my inquiry.
Answer: The biggest safety improvement the NFL could make by far would be to remove helmets. While this sounds paradoxical, helmets allow the kind of plays which result in concussions. Remove them (and obviously forbid any tackles above the chest area) and you'd have a much safer game; see for instance, this article from The Guardian.
The chance of this happening is probably just about zero though.


Minifying JSON responses

Question: I have a web requests that returns thousands of JSON objects in the form of this:
<code> {
      "ID": [int],
      "IsActive": [bool],
      "FirstName": [string],
      "LastName": [string],
      "Type": [int],
      "COIStatus": [int],
      "Role": [int],
      "SubscriptionEmailAddress": [string],
      "TRDevice": null,
      "MobileVersion": null,
      "RoleDescription": [string],
      "Name": [string]
 }
</code>
The size of the responses can be 2.5 megs according to my Google toolbar and I want to reduce that payload. Considering that my server uses gzip to compress all of its data, would reducing the length of property names (eg: change "IsActive" -> "IA", "SubscriptionEmailAddress" -> "SEA") decrease the actual payload? I'm thinking this may not decrease the payload because gzip may already be doing this in the background to compress its data.
Comment: No, shortening your property names is unlikely to do much when you're already gzipping your output. But it's an easy thing to test!

Grab your json output, search replace your property names, and upload the new json-file to your server. When requesting it, it should also be gzipped, and you'll be able to compare the file sizes.
Answer: simple answer, yes. json is text only so the if your variables are named
"r1", "r2", "r3"... will be a lot smaller than "somehugename", "someotherlongname", "somethingelse"...
but if you're the data provider, i would suggest adding pagination to your results. instead of making one huge request to just get a few results, just requesting say the last 10 would greatly reduce your payload.

Minifying JSON responses


Legal class names in VB? Spec suggests `class foo!` should be legal

Question: I'm struggling with what seems to be a bug in Visual Basic.  I'm probably missing something.  Hopefully someone can point out what it is.
Section 7.5 of the Visual Basic Specification for version 10.0 says that this is the grammar for class declaration.  Forgive the lack of italics that indicates the difference between literals and grammar nodes.
ClassDeclaration  ::=
    [  Attributes  ]  [  ClassModifier+  ]  Class  Identifier  [  TypeParameterList  ]  StatementTerminator
    [  ClassBase  ]
    [  TypeImplementsClause+  ]
    [  ClassMemberDeclaration+  ]
    End  Class  StatementTerminator
ClassModifier  ::=  TypeModifier  |  MustInherit  |  NotInheritable  |  Partial

So the minimal class declaration would be
Class Identifier StatementTerminator
End  Class  StatementTerminator

The grammar for Identifier and some other supporting nodes are specified in 13.1.2,
Identifier  ::=
    NonEscapedIdentifier  [  TypeCharacter  ]  |
    Keyword  TypeCharacter  |
    EscapedIdentifier

NonEscapedIdentifier  ::=  < IdentifierName but not Keyword >

TypeCharacter  ::=
    IntegerTypeCharacter  |
    LongTypeCharacter  |
    DecimalTypeCharacter  |
    SingleTypeCharacter  |
    DoubleTypeCharacter  |
    StringTypeCharacter

IntegerTypeCharacter  ::=  %
LongTypeCharacter  ::=  &
DecimalTypeCharacter  ::=  @
SingleTypeCharacter  ::=  !
DoubleTypeCharacter  ::=  #
StringTypeCharacter  ::=  $

Based on my reading of this foo! should be a legal identifier because ! is a TypeCharacter.
So, based on the minimal legal class declaration above, this should be legal.
Class foo!
End Class

But Visual Studio 2010 gives this:

Type declaration characters are not valid in this context.

Am I missing something in the spec, or does the compiler disagree with the spec?
Comment: (Incidentally, section 13.1.2 is the wrong place to look at, since it contains only the *grammar summary* and is not normative in itself.)
Answer: The grammar doesn't stand on its own, the describing text is part of the specification. This is important here, because otherwise you'd be right. But as competent_tech has noted in his answer, the TypeCharacter production, while forming part of the Identifier production, is not a valid character in an identifier – except to denote a variable's (or function's) type.
This is specified in 2.2.1:

A type character denotes the type of the preceding identifier. The type character is not considered part of the identifier. If a declaration includes a type character, the type character must agree with the type specified in the declaration itself; otherwise, a compile-time error occurs.
Appending a type character to an identifier that conceptually does not have a type (for example, a namespace name) or to an identifier whose type disagrees with the type of the type character causes a compile-time error.

(Emphasis mine.)
The section even gives an explicit example of an invalid use that is equivalent to your use. So the specification explicitly forbids this.
Answer: The TypeIdentifier is used to declare the data type of a property, variable, function, etc.
Public Class Foo
    Dim x!
    ' The above declaration is the same as
    'Dim x As Single

End Class

Since there is no data type for a class, adding a type identifier does not make any sense. Based on this, it seems like the spec may not be entirely correct.


The topology of a $k$-dimensional ball in $\mathbb R^N$ and the definition of attaching a $k$-cell

Question: Let $B_k$ be the $k$-dimensional ball in $\mathbb R^N$:
$$
B_k = \left\{x \in \mathbb R^N \ : \ \sum_{i = 1}^k x_i^2 < 1, \ x_{k + 1} = \ldots = x_N = 0 \right\}.
$$
In the context of Morse Theory, it is called a $k$-cell.
The operation of attaching a $k$-cell to a topological space $Y$ is defined in my book (Morse Index of Solutions of Nonlinear Elliptic Equations, Damascelli and Pacella, 2019) as follows:
Take a continuous function $g: \overline{\partial B_k} \to Y$ which is homeomorphic to its image. The topological space obtained by taking the union of $Y$ and the closed $k$-cell $\overline{B_k}$ with the equivalence relation which identifies $x \in \overline{\partial B_k}$ with its image $g(x) \in Y$ will be denoted by $Y \cup_g B_k$ ($Y$ with a $k$-cell attached).
Milnor, in his classical book, takes $g$ defined on $S^{k - 1}$
My questions are:

What is $\overline{\partial B_k}$, and why do we take it as the domain of $g$?

For the first part, as I understand, $\overline{\partial B_k} = \overline{B_k}$. Indeed, the boundary is obtained by taking the closure and excluding the interior. But the interior of $\overline{B_k}$ is empty. What am I missing?

What does it mean to take the equivalence relation $x \sim g(x)$ in $Y \cup_g B_k$? Intuitively, what are we doing?

I'm quite clueless about this.
Comment: There are two different notions of boundary in topology: The one you are thinking about is from General Topology and is a wrong thing to use. The right notion of the boundary here is the boundary of the closed ball regarded as a manifold with boundary.
Comment: It must be a typo and should be read as $\partial B_k$. Do you know about quotient topology?
Comment: But even then, isn't $\partial B_k = \overline{B_k}$? And I know about the quotient topology, but my difficulty is to realize what happens to the guys in $Y$ who are not associated to any $x$, that is, outside the image of $g$.
Answer: If that is all what the authors say, it is poor notation.

One should emphasize that $B_k$ is topologically an open ball. But note that it is not an open subset of $\mathbb R^N$ unless $N = k$. Anyway, the closure $\overline{B_k}$ in $\mathbb R^N$ is always topologically a closed $k$-dimensional ball.

They authors do not define $\partial B_k$.

For $N > k$ the interpretation of $\partial B_k$ as the topological boundary of $B_k$ in $\mathbb R^N$ produces in fact $\partial B_k = \overline{B_k}$. Only for $N = k$ we get $\partial B_k = \overline{B_k} \setminus B_k$.

An interpretation of $\partial B_k$ which depends on $N$ does not make sense. Therefore the only reasonable interpretation is $\partial B_k = \overline{B_k} \setminus B_k$. But then $\partial B_k$ is closed and $ \overline{\partial B_k} = \partial B_k$. So it remains open why the authors write $\overline{\partial B_k}$. Anyway, $\partial B_k \approx S^{k-1}$. This agrees with Milnor.

The notation $Y \cup_g B_k$ is unusual because $g$ is defined on $\partial B_k$ which is disjoint from $B_k$. The standard notation is $Y \cup_g  \overline{B_k}$
Comment: $Y \cup_g \overline{B_k}$ (my notation!) is a quotient space of the disjoint union $Y + \overline{B_k}$ in which each $x \in \partial B_k$ is identified with $g(x) \in Y$. The quotient map $p: Y + overline{B_k} \to Y \cup_g \overline{B_k}$ maps $B_k$ *'homeomorphically* onto $B'_k = g(B_k)$. Thus $B'_k$ is a subspace of $Y \cup_g \overline{B_k}$ which is topologically an open $k$-cell. Consider an example: $Y$ = one-point space; then  $Y \cup_g \overline{B_k} \approx S^k$.
Comment: @DaniloGregorinAfonso The construction itself does not have any restriction concerning $g$. It may be an embedding or not.
Comment: Dear Paul, thanks for the answer, it has clarified very well the first of my doubts. But how to make sense of this equivalence relation they define? What happens to the guys outside the image of $g$?
Comment: Regarding the example: It makes total sense that identifying the boundary of a ball with a point gives a sphere in one more dimension. However, shouldn't the map $g: S^{k - 1}$ be an homeomorphism to its image? How can this be, in this case?
Comment: Thanks. Indeed, Milnor doesn't require the homeomorphism condition, but my book does, as do Ambrosetti and Malchiodi in *Nonlinear Analysis and Semilinear Elliptic Problems*. I will ask the authors why do they do it. Anyway, all is clear now, and I thank you again


What does conditional independence mean semantically?

Question: I've just spent the last 3 hours reading every post, question, Medium article, and textbook entry on conditional independence, and I still don't really understand it. Can somebody explain what it means in layman's terms? I think I'm getting tripped up on the semantics.
One example I frequently come upon is: a person's height and vocabulary are conditionally independent, given age. What I don't understand is, what are they when not given age? Dependent? Wikipedia says so.
This is preposterous to me. Height is not dependent upon vocabulary, nor vice versa. (This brings up another question: does dependent mean "caused by?" Does it mean "correlated with?") In any case, it seems to me that any two events are either empirically independent or they are empirically dependent. Their relationship to each other cannot magically change. Bart and Lisa are still brother and sister regardless of whether or not we know that Homer is father to both.
The existence of a notion called "conditional" independence doesn't make sense to me. And the semantics confuse me. Indeed, it seems that height and vocabulary are conditionally dependent--on age! In other words, age is a variable that binds these things (making them less independent).
My head is in knots.
Comment: Related Maths.SE post: [Could someone explain conditional independence?](https://math.stackexchange.com/questions/23093/could-someone-explain-conditional-independence) and links therein.
Comment: That was one of the things that I had read. Still had the questions above.
Answer: It may be useful for you to initially think of 'dependence' and 'independence' here as statements about correlation or association, rather than as direct statements of causality. They are useful in developing a framework in which to think about causality, though this is easier to comprehend once the basic idea is in place.
In your example, once we have accounted for our knowledge about the age of an individual, height tells us nothing about their vocabulary and vice versa. In other words: at any particular age, being taller or shorter should have no association with vocabulary (I'm going to ignore rare medical conditions that may cause reductions in both). This makes height and vocabulary conditionally independent.
But now assume that we do NOT know anything about age. If I told you that the individual was 40 cms tall, you could probably make a better than random guess about their vocabulary - simply because (i) very short individuals are likely to be infants and (ii) infants are likely to have a smaller vocabulary than adults.
Similarly, if I gave told you than an individual knew ~100 words in total, you would probably guess that they are very short - because (i) individuals with a tiny vocabulary are likely to be children, and (ii) children are likely to be shorter than adults.
This makes height and vocabulary conditionally dependent on each other. This means that though A and B do not affect each other directly, they both affect C, and so information about C and one of A or B therefore tells you something about the other.
Comment: @NaiveBae I'll try to address those one at a time. (i) *it's a description of a situation in which we have super limited knowledge*. I don't think our level of knowledge is particularly relevant. We know a lot about heights, vocabularies, and age, after all! (ii)  *It's not an empirical state, it's a state that describes only what we can surmise at a particular moment.* That may overstate things a little bit. It describes the nature of the relationships, which governs what it is possible to infer.
Comment: @NaiveBae (iii) *A and B can only ever be described as conditionally (in)dependent of each other but in relation to a third variable. It's meaningless to talk about conditional independence without a 3rd variable. Which would mean that A & B can be conditionally independent with respect to C, but not with respect to D.* Yes, this is correct.
Comment: @NaiveBae Does that address your questions?
Comment: Thank you, I'm grateful for your responses. This largely clarifies things.
Comment: Thanks for the response! So it's a description of a situation in which we have super limited knowledge. It's not an empirical state, it's a state that describes only what we can surmise at a particular moment. Also, A and B can only ever be described as conditionally (in)dependent _of each other_ but in relation to a third variable. It's meaningless to talk about conditional independence without a 3rd variable. Which would mean that A & B can be conditionally independent with respect to C, but not with respect to D. Is this all correct?


Replicating a plot from the ROCR Website

Question: I'm trying to reproduce this plot from the ROCR website:

I can get something similar as follows:
library(ROCR)
data(ROCR.simple)
pred <- prediction(ROCR.simple$predictions, ROCR.simple$labels)
plot( performance(pred, "acc"))

But I can figure out what ROCR metric to use to get postive vs negative densities, and I also can't figure out how to combine 2 ROCR curves into one plot.  I figure since this figure shows up in several different places in the ROCR documentation, the code to replicate it must exist somewhere, correct?
Answer: You just need to separate your predictions based on the labels: e.g.
good = rnorm(100, mean = 10)
bad = rnorm(1000, mean = 7)

Then use something like this:
plotOverlappingDensity <- function(a, b, cola='red', colb='darkgreen', ...){
  da = density(a, ...)
  db = density(b, ...)
  xlim = c( min(c(da$x, db$x)), max(c(da$x, db$x)) )
  ylim = c( min(c(da$y, db$y)), max(c(da$y, db$y)) )
  plot(da, col=cola, xlim=xlim, ylim=ylim, ...)
  lines(db, col=colb)
}

plotOverlappingDensity(good, bad)

Answer: Well, I answered my own question.  Running demo(ROCR) shows the code required to make the plot, but it's a little difficult to scrape out and parse.  I made a function for single-run, binary classifiers:
density_plot <- function(pred, pos=NULL,
                         legend=c('negative', 'positive'), colors=c("red", "green")){

  stopifnot(require('ROCR'))
  stopifnot(length(pred@predictions)==1) #Multiple runs not supported
  lev <- levels(pred@labels[[1]])
  stopifnot(length(lev)==2) #Only binary classification supported for now

  if (is.null(pos)){
    pos <- lev[2]
  }
  neg <- setdiff(lev, pos)

  neg_col = colors[1]
  pos_col = colors[2]

  neg_dens <- density(pred@predictions[[1]][pred@labels[[1]]==neg])
  pos_dens <- density(pred@predictions[[1]][pred@labels[[1]]==pos])
  top <- ceiling(max(neg_dens$y, pos_dens$y))

  plot(0,0,type="n", xlim= c(0,1), ylim=c(0,top),
       xlab="Cutoff", ylab="Density",
       main="How well do the predictions separate the classes?")
  lines(neg_dens, col=neg_col)
  lines(pos_dens, col=pos_col)
  legend(0, top, legend=legend, col=c(neg_col,pos_col), lty=1)
}

I'm curious to see if there's a more elegant solution, that perhaps supports multiple-runs (like the rest of the functions in ROCR)
Comment: According to ROCR manual - no, there is no elegant solution. There are no density function already built-in.


JSON Parser in Async Task with ListView

Question: I have an this code, i can parse from the XML but im having error on setting up the adapter, can anyone help me with this.
Thanks in advance
Here is my code
public class TestJSON extends Activity{

    public static ListView lv;
    public static ProgressDialog pDialog;
    public static LazyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_json_view);

        new AsyncInitial().execute();
        // Showing progress dialog before sending http request
        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Please wait..");
        pDialog.setIndeterminate(true);
        pDialog.setCancelable(false);
        pDialog.show();

        lv = (ListView)findViewById(R.id.list);
        //lv.setAdapter(new ArrayAdapter(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, items));
    }

    private class AsyncInitial extends AsyncTask<Void, Void, ArrayList<HashMap<String, String>>> {

            @Override
            protected void onPreExecute() {
            }

            @Override
            protected ArrayList<HashMap<String, String>> doInBackground(Void... arg0) {

                ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

                try {
                    //if (vid_num <= 0) {
                    // Get a httpclient to talk to the internet
                    HttpClient client = new DefaultHttpClient();
                    // Perform a GET request to YouTube for a JSON list of all the videos by a specific user
                    //https://gdata.youtube.com/feeds/api/videos?author="+username+"&v=2&alt=jsonc
                    HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/SP86E04995E07F6BA8?v=2&start-index=1&max-results=50&alt=jsonc");
                    // Get the response that YouTube sends back
                    HttpResponse response = client.execute(request);
                    // Convert this response into a readable string
                    String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
                    // Create a JSON object that we can use from the String
                    JSONObject json = new JSONObject(jsonString);

                    // For further information about the syntax of this request and JSON-C
                    // see the documentation on YouTube http://code.google.com/apis/youtube/2.0/developers_guide_jsonc.html

                    // Get are search result items
                    JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");

                    // Get the total number of video
                    //String vid_num = json.getJSONObject("data").getString("totalItems");
                    //System.out.println("vid_num-------->"+  vid_num);

                    // Loop round our JSON list of videos creating Video objects to use within our app
                    for (int i = 0; i < jsonArray.length(); i++) {

                        HashMap<String, String> map = new HashMap<String, String>();
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                        // The title of the video
                        String title = jsonObject.getJSONObject("video").getString("title");
                        System.out.println("Title-------->"+  title);

                        // The url link back to YouTube, this checks if it has a mobile url
                        // if it doesnt it gets the standard url
                        String url;
                        try {
                            url = jsonObject.getJSONObject("video").getJSONObject("player").getString("mobile");//
                            System.out.println("url-------->"+  url);
                        } catch (JSONException ignore) {
                            url = jsonObject.getJSONObject("video").getJSONObject("player").getString("default");
                            System.out.println("url-------->"+  url);
                        }

                        // A url to the thumbnail image of the video
                        // We will use this later to get an image using a Custom ImageView
                        //String TAG_thumbUrl = jsonObject.getJSONObject("video").getJSONObject("thumbnail").getString("sqDefault");
                        //System.out.println("thumbUrl-------->"+  thumbUrl);

                        String video_id = jsonObject.getJSONObject("video").getString("id");
                        System.out.println("video_id-------->"+  video_id);

                        map.put(title, title);
                        //map.put(url, url);
                        map.put(video_id, video_id);

                        menuItems.add(map);
                    }

                    adapter = new LazyAdapter(TestJSON.this,menuItems);
                    lv.setAdapter(adapter);

                } catch (ClientProtocolException e) {
                //Log.e("Feck", e);
                } catch (IOException e) {
                //Log.e("Feck", e);
                } catch (JSONException e) {
                //Log.e("Feck", e);
                }

                return menuItems;
            }

            @Override
            protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
                // TODO Auto-generated method stub
                //super.onPostExecute(result);

                pDialog.dismiss();
            }

    }

}

The adapter
public class LazyAdapter extends BaseAdapter {

    private Activity activity;
    private static LayoutInflater inflater;
    private ArrayList<HashMap<String, String>> data;

    public LazyAdapter(TestJSON testJSON, ArrayList<HashMap<String, String>> menuItems) {
        activity = testJSON;
        data = menuItems;
        System.out.println("Hash-------->");
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
       }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //ViewHolder holder;

        View vi=convertView;
        if(convertView==null)vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title);
        TextView id = (TextView)vi.findViewById(R.id.artist);

        HashMap<String, String> menuItems = new HashMap<String, String>();
        menuItems = data.get(position);

        title.setText(menuItems.get("title"));
        id.setText(menuItems.get("video_id"));

        System.out.println("Adapter Title from Hash-------->"+  title);
        System.out.println("Adapter video_id from Hash-------->"+  id);
        return vi;
    }

}

and this is the error on log cat
07-08 17:46:39.567: E/AndroidRuntime(13049): FATAL EXCEPTION: AsyncTask #1
07-08 17:46:39.567: E/AndroidRuntime(13049): java.lang.RuntimeException: An error occured while executing doInBackground()
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at java.lang.Thread.run(Thread.java:856)
07-08 17:46:39.567: E/AndroidRuntime(13049): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.view.View.invalidate(View.java:10250)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.view.View.invalidate(View.java:10205)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.widget.AbsListView.resetList(AbsListView.java:1954)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.widget.ListView.resetList(ListView.java:502)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.widget.ListView.setAdapter(ListView.java:442)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at com.jpact.youtubeapp.TestJSON$AsyncInitial.doInBackground(TestJSON.java:122)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at com.jpact.youtubeapp.TestJSON$AsyncInitial.doInBackground(TestJSON.java:1)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-08 17:46:39.567: E/AndroidRuntime(13049):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
07-08 17:46:39.567: E/AndroidRuntime(13049):    ... 5 more

Comment: never touch UI thread from background thread. Follow @geet's comment and place your `lv.setAdapter(adapter);` code inside `onPostExecute()`.
Answer:  adapter = new LazyAdapter(TestJSON.this,menuItems);
 lv.setAdapter(adapter);

Updating ui fro doInBackground is not possible. this causes the exception.
You need to update in onPostExecute or use runOnUiThread.
doInbackground is invoked on the background thread. so you can't update ui from background thread. Ui should be updated on ui thread.
You can return result in doInBackground. The result of doInBackGround computation is a parameter toonPostExecute . SO return result in doInbackground and update ui in onPostexecute.
 @Override
 protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
  super.onPostExecute(result);
  pDialog.dismiss();
  adapter = new LazyAdapter(TestJSON.this,result);
  lv.setAdapter(adapter);
 }

Answer: Put the following code:
adapter = new LazyAdapter(TestJSON.this,menuItems);
lv.setAdapter(adapter);

into the following method:
@Override
protected void onPostExecute(ArrayList<HashMap<String, String>> result) {
    // TODO Auto-generated method stub
    //super.onPostExecute(result);

    pDialog.dismiss();
}

Answer: You cannot set the Adapter in the doInBackground() method. The Adapter can only be set from the UI thread.
You can either move your code to the onPostExecute() method or use the runOnUiThread(action) method.
Answer: As i have seeing your code you are trying to set the Adapter for your lazy list in
doInBackground(Void... arg0) {
    //Do not set your adapter here in this method

}

Rather you can set your adapter in
onPostExecute(Void result){
adapter = new LazyAdapter(TestJSON.this,menuItems);
                lv.setAdapter(adapter);
}

And remove the line which is responsible for setting the adapter in doInBackground method. Never try to work on UiThread from a background Thread. It will always throw an exception. May be this will help you.

JSON Parser in Async Task with ListView


Como funciona o comando git rebase?

Question: O que acontece quando se faz um rebase na seguinte situação:

Branch master com 3 commits (A, B e C).
Após fazer o commit C, criei um novo branch com o nome design.
Fiz um commit (D) no branch design e voltei ao branch master.
Fiz um commit (E) no branch master.

Atualmente o meu histórico de commits é:

master: A, B, C, E
design: A, B, C, D

Após fazer o rebase no branch master, o histórico ficará da seguinte forma?

master: A, B, C, D, E
design: A, B, C, D

Se a resposta é sim, qual o objetivo de refazer o histórico quando uma das vantagens do Git é poder ver o histórico tal como ele é? Se posso manipular o histórico, então posso não ver o que realmente aconteceu.
Comment: o rebase costuma ser útil quando somente você mexe num determinado projeto, mas isso quando você não quer perder tempo fazendo merge, pois ele sobrescreve o histórico com as atualizações e ignora os conflitos, mantendo sua atualização como a mais recente e também despreza mudanças no meio do projeto feito por outras pessoas.
Answer: Respondendo as suas perguntas.

1 - Após fazer o rebase no branch master, o histórico ficará da
  seguinte forma?

Sim

2 - Se a resposta é sim, qual o objetivo de refazer o histórico
  quando uma das vantagens do GIT é poder ver o histórico tal como ele
  é?

O problema do rebase é que ele altera o histórico, assim como outros comandos do git (como os que levam o atributo --hard). Por isto ele é recomendado apenas em casos bem específicos. O Git não tem a premissa de proteger a qualquer custo o histórico de alterações mas de, por padrão, preservar isto.
Veja a seguir o que aconteceu em cada commitque fez.
Lembrando que HEAD é para onde o seu repositório está apontando no momento (pode ser uma branch, tag ou commit específico).
Entendendo o cenário passo-a-passo
A seguir, vou detalhar o que ocorreu em cada passo que descreveu até o rebase.
Vamos lá:

Branch master com 3 commits (A, B e C).

A<---B<---C
          |
          |
       |Master|
          |
         HEAD

Comando:
git checkout master

Após fazer o commit C, criei um novo branch com o nome design.

        HEAD
          |
       |design|
          |
          |
A<---B<---C
          |
          |
       |Master|

Comando:
git commit -m "Commit C"
git checkout -b design      # cria a branch design a partir da branch atual (master)

Fiz um commit (D) no branch design

                  HEAD
                    |
                |design|
                    |
                    |
                .---D
               /
A<---B<---C<--´
          |
          |
       |Master|

Comando:
git checkout design
git commit -m "Commit D"

e voltei ao branch master.

                |design|
                    |
                    |
                .---D
               /
A<---B<---C<--´
          |
          |
       |Master|
          |
         HEAD

Comando:
git checkout master

Fiz um commit (E) no branch master.

                |design|
                    |
                    |
                .---D
               /
A<---B<---C<--´-----E
                    |
                    |
                |Master|
                    |
                   HEAD

Comando:
git commit -m "Commit E"

E fazendo o rebase:
                |design|
                    |
                    |
                .---D<----E'
               /          |
A<---B<---C<--´           |
                       |Master|

Comando:
git checkout design
git rebase master

Como pode notar, com o rebase da master na branch design, os commits a mais (E) da master vão para o topo da branch design.
Assim, como pode notar ao final, os commits ficaram como você disse:

master: A, B, C, D, E
design: A, B, C, D
Comment: Uma das melhores explicações que li... Pra completar colocaria os comandos referentes aos commits e ao rebase no seu exemplo.
Comment: @DiegoBorges, done :)
Comment: @Dherik, Acho que esta errado o ultimo desenho. O "COMMIT E" vai pra BASE da branch "DESIGN". Por isto o nome "rebase". Ou seja, o COMMIT E fica atras do "COMMIT D". Sendo o "COMMIT D" o "HEAD."
Comment: @GBisconcini é isso mesmo, as branches também ao final apontam para o mesmo commit (creio que por isso ninguém notou o problema até agora). Vou corrigir depois. (fique à vontade para editar, se quiser)
Answer: O principal benefício do rebase é que começa uma abordagem muito mais "limpa".
Primeiro, elimina a intercalação desnecessário, compromisso esse exigido pelo git merge.
Em segundo lugar, rebasing também resulta num histórico de projeto perfeitamente linear onde pode acompanhar o inicio do percurso de todo o caminho do projeto, sem quaisquer problemas. Isto torna mais fácil para navegar no seu projeto com comandos como git log, git bisect e gitk.
Mas, há duas soluções de compromisso para esta história:

Segurança e rastreabilidade: se não seguir a regra de ouro de rebasing, re-escrever a história do projeto pode ser potencialmente catastrófico para o seu fluxo de trabalho de colaboração. E, menos importante, rebasing perde o contexto fornecido por um merge commit, isto se não conseguir ver quando as alterações a montante foram incorporadas ao recurso.

A melhor forma de compreender todo o processo é por exemplo ler o seguinte Artigo:
Merging vs Rebasing
Também deixo aqui imagens que resumem um bom exemplo de aplicação:
Estado Inicial do Repositório

Dev-B: Sincronização da Origem

Dev-B: rebase

Dev-B: rebase final

Dev-B: Sincronização Final para a Origem

Answer: É exatamente isso que acontece. A principal vantagem do rebase em relação ao merge é que você terá um histórico bem mais limpo (e como realmente é) do que um projeto com merges, já que ele vai primeiro aplicar todos os commits do branch que está "rebaseando", para depois aplicar os commits do branch que está sendo "rebeseado". Entretando, rebase pode acarretar com algumas perdas de código que não acontecerão no merge, por isso é indicado que iniciantes não utilizem o rebase, mas sim o merge.
Além disso, a regra de ouro do rebase define quando fazer ou não um rebase. O rebase é aconselhado apenas para branches privados, ou seja, ele deve ser evitado em branches públicos, como o master, por exemplo. Isso evita que você bagunce branches públicos reorganizando os seus commits.
Em resumo, o rebase serve justamente pra manter um histórico mais limpo e linear, evitando commits "desnecessários" ocasionados por um eventual conflito de merges. Vale lembrar que algumas ferramentas funcionam somente com o rebase, como é o caso do gerrit, que cria uma linha de dependências dos commits enviados para com o branch principal (geralmente o master).

Como funciona o comando git rebase?


TypeError: an integer is required with buffer

Question: I'm trying to build a packet sniffer but everytime a try to run this python i get a TypeError an integer is required when i'm trying to read the first 20 byte of the packet.
this is the part of the code that gives me an error:
<code>try:
while True:

    #read in a packet
    raw_buffer = sniffer.recvfrom(65565)[0]

    #create an IP Header from the first 20 bytes of the buffer
    ip_header = IP(raw_buffer[0:20])

    #print out the protocol that was detected and the hosts
    print "Protocol: %s %s -> %s" % (ip_header.protocol, ip_header.src_address, ip_header.dst_address)
</code>
Answer: You are assuming that the IP header is the first of the received buffer, but maybe it is not. Normally, an Ethernet header is preceding it, so that may be causing the IP constructor to fail.


Pandas, most efficient way to apply a two functions on entire row

Question: I have the following DataFrame:
       Date  Label                                               Top1  \
0  2008-08-08      0  b"Georgia 'downs two Russian warplanes' as cou...
1  2008-08-11      1  b'Why wont America and Nato help us? If they w...
2  2008-08-12      0  b'Remember that adorable 9-year-old who sang a...
3  2008-08-13      0  b' U.S. refuses Israel weapons to attack Iran:...
4  2008-08-14      1  b'All the experts admit that we should legalis...
                                                Top2  \
0            b'BREAKING: Musharraf to be impeached.'
1        b'Bush puts foot down on Georgian conflict'
2                 b"Russia 'ends Georgia operation'"
3  b"When the president ordered to attack Tskhinv...
4  b'War in South Osetia - 89 pictures made by a ...
                                                Top3  \
0  b'Russia Today: Columns of troops roll into So...
1  b"Jewish Georgian minister: Thanks to Israeli ...
2  b'"If we had no sexual harassment we would hav...
3  b' Israel clears troops who killed Reuters cam...
4  b'Swedish wrestler Ara Abrahamian throws away ...
                                                Top4  \
0  b'Russian tanks are moving towards the capital...
1  b'Georgian army flees in disarray as Russians ...
2  b"Al-Qa'eda is losing support in Iraq because ...
3  b'Britain\'s policy of being tough on drugs is...
4  b'Russia exaggerated the death toll in South O...
                                                Top5  \
0  b"Afghan children raped with 'impunity,' U.N. ...
1      b"Olympic opening ceremony fireworks 'faked'"
2  b'Ceasefire in Georgia: Putin Outmaneuvers the...
3  b'Body of 14 year old found in trunk; Latest (...
4  b'Missile That Killed 9 Inside Pakistan May Ha...
                                               Top25  VIX Open  VIX High  \
0           b"No Help for Mexico's Kidnapping Surge"     21.15     21.69
1  b"So this is what it's come to: trading sex fo...     20.66     20.96
2  b"BBC NEWS | Asia-Pacific | Extinction 'by man...     20.64     21.51
3  b'2006: Nobel laureate Aleksander Solzhenitsyn...     21.57     22.11
4  b'Philippines : Peace Advocate say Muslims nee...     22.30     22.30

the top 1 to top 25 are news articles
I want to perform sentiment analysis on every date's articles and create a mean of those scores
so is there a way I can efficiently check if a column contains the word Top calculate the score and create a column of mean for every date?
What I tried so far:

def scorer(row, col):
    date_scores = []
    if col.contains('Top'):
        date_scores.append(get_sentiment_score(row[col]))
    else:
        pass
    sentiment_daily_mean = np.mean()
    return sentiment_daily_mean
df['date_score'] = df.apply(lambda x: scorer(x), args=list(df.columns))

but this won't work since I'm passing all the columns to the function at once
Answer: You need to pass in the rows to the apply-function. Try this:
def scorer(row):
  date_scores = []
  for col in row:
    if 'Top' in col:
      date_scores.append(get_sentiment_score(row[col]))
  sentiment_daily_mean = date_scores.mean()
  return sentiment_daily_mean

df['date_score'] = df.apply(scorer, axis=1)

Comment: Does the use of lambda here add anything? could you not simply put `df.apply(scorer, axis=1)` ?
Comment: Thanks @ezekiel, I agree the lambda defn isnt required here. Making the minor change


Rate of Convergence: Competing Poisson Processes

Question: I am analyzing one of the properties of the Poisson Process, that of Competing Processes:
Suppose $N_1(t), t \geq 0$ and $N_2(t), t \geq 0$ are independente Poisson processes with respective rates $\lambda_1$ and $\lambda_2$.  Let $S_n^i$ be the time of the $n$th event of process $i, i = 1,2$.
We know that $P[S_n^1 < S_m^2] = \sum_{k=n}^{n+m-1} {n+m-1 \choose k} \left(\frac{\lambda_1}{\lambda_1 + \lambda_2}\right)^k \left(\frac{\lambda_2}{\lambda_1 + \lambda_2}\right)^{n+m-1-k}$.  I'm interested in the case where $n=m$ and $ n \rightarrow \infty$.  This converges to 1, and my intuition is that the rate of convergence depends on the ratio $\frac{\lambda_1}{\lambda_2}$, but I'm having a hard time proving it out.  Any insights into a closed-form rate of convergence would be great.
Answer: First write
$$
S_n^1  = X_1^1  + X_2^1  + X_3^1  +  \cdots ,
$$
where the $X_k^1$ are i.i.d. exponential random variables with density function $f_{X_1^1 } (x) = \lambda _1 e^{ - \lambda _1 x} $, $x > 0$, and
$$
S_n^2  = X_1^2  + X_2^2  + X_3^2  +  \cdots ,
$$
where the $X_k^2$ are i.i.d. exponential random variables with density function $f_{X_1^2 } (x) = \lambda _2 e^{ - \lambda _2 x} $, $x > 0$, independent also of the $X_k^1$.
Now let $Y_k = X_k^1 - X_k^2$. Then,
$$
{\rm P}(S_n^1  < S_n^2 ) = {\rm P}(Y_1  + \cdots  + Y_n  < 0).
$$
Next note that the $Y_k$ are i.i.d. random variables with mean
$$
\mu  = {\rm E}(Y_1) =  {\rm E}(X_1^1 ) - {\rm E}(X_1^2 ) = \frac{1}{{\lambda _1 }} - \frac{1}{{\lambda _2 }}
$$
and variance
$$
\sigma^2 = {\rm Var}(Y_1) = {\rm Var}(X_1^1 ) + {\rm Var}(X_1^2 ) = \frac{1}{{\lambda _1^2 }} + \frac{1}{{\lambda _2^2 }}.
$$
It holds
$$
\frac{\mu }{\sigma } = \frac{{\lambda _2  - \lambda _1 }}{{\lambda _1 \lambda _2 }}\frac{{\lambda _1 \lambda _2 }}{{\sqrt {\lambda _1^2  + \lambda _2^2 } }} = \frac{{\lambda _2  - \lambda _1 }}{{\sqrt {\lambda _1^2  + \lambda _2^2 } }}.
$$
Now let $\tilde S_n = Y_1 +  \cdots  + Y_n$ and $Z_n = \frac{{\tilde S_n  - n\mu }}{{\sigma \sqrt n }}$. Then,
$$
{\rm P}(Y_1  +  \cdots  + Y_n  < 0) = {\rm P}\bigg(\frac{{\tilde S_n  - n\mu }}{{\sigma \sqrt n }} < \frac{{ - n\mu }}{{\sigma \sqrt n }}\bigg) = {\rm P}\bigg(Z_n  < \frac{{\lambda _1  - \lambda _2 }}{{\sqrt {\lambda _1^2  + \lambda _1^2 } }}\sqrt n \bigg).
$$
By the central limit theorem, $Z_n$ converges in distribution to the ${\rm N}(0,1)$ distribution. (So if $\lambda_1 > \lambda_2$, the last probability, which is equal to ${\rm P}(S_n^1  < S_n^2 )$, tends to $1$ as $n \to \infty$.) This may help you deduce the asymptotic behavior of  ${\rm P}(S_n^1  < S_n^2 )$.
EDIT:
So the above gives rise to the approximation
$$
{\rm P}(S_n^1  < S_n^2 ) \approx \Phi \bigg(\frac{{\lambda _1  - \lambda _2 }}{{\sqrt {\lambda _1^2  + \lambda _2^2} }}\sqrt n \bigg),
$$
where $\Phi$ is the distribution function of the ${\rm N}(0,1)$ distribution. Of course, the quality of the approximation depends on the parameters involved. We can check it by comparing to results obtained from Monte Carlo simulations. [The probability ${\rm P}(S_n^1  < S_n^2 )$ can be approximated easily and accurately using Monte Carlo simulation, noting that an exponential random variable with density function $f_{X} (x) = \lambda e^{ - \lambda x} $, $x > 0$, can be generated as $-\ln(U)/\lambda$, where $U$ is uniformly distributed on $(0,1)$.] Define the quantity $\zeta = \zeta (n,\lambda_1,\lambda_2)$ by
$$
\zeta  = \frac{{\lambda _1  - \lambda _2 }}{{\sqrt {\lambda _1^2  + \lambda _2^2 } }}\sqrt n ,
$$
and let $\hat p = \hat p(n,\lambda_1,\lambda_2)$ denote the Monte Carlo approximation obtained (accurately enough) for the probability ${\rm P}(S_n^1  < S_n^2 )$. The following results were obtained: 1) $n=50$, $\lambda_1=2$, $\lambda_2 = 1$ ($\zeta = \sqrt{10}$): $\hat p = 0.999678$, $\Phi(\zeta) \approx 0.999217$; 2) $n=100$, $\lambda_1=4$, $\lambda_2 = 3$ ($\zeta = 2$): $\hat p = 0.978802$, $\Phi(\zeta) \approx 0.977250$. Finally, letting $\rho = \lambda_1 / \lambda_2$, it holds
$$
\frac{{\lambda _1  - \lambda _2 }}{{\sqrt {\lambda _1^2  + \lambda _2^2 } }} =  \frac{{\rho - 1}}{{\sqrt {\rho^2  + 1} }},
$$
and thus the original approximation can be written
$$
{\rm P}(S_n^1  < S_n^2 ) \approx \Phi \bigg(\frac{{\rho  - 1 }}{{\sqrt {\rho^2  + 1} }}\sqrt n \bigg).
$$
Comment: @Avneesh: So the last approximation agrees with your intuition...
Comment: This is excellent stuff, thanks so much Shai! Sorry I can't vote your answer up, seems like I need more reputation points.
Comment: @Avneesh: Thanks, glad to help!
Comment: @Avneesh: You can formally accept Shai's answer, though, by clicking on the little check mark next to it.
Comment: @Mike: Thanks for pointing this out!


Debugging Ajax Callback

Question: Goal: Print/dump $formState values to debug
I am altering a form and the functionality works:
$form['elements']['location']['deparment']['#ajax'] = [
  'callback' => 'myAjaxCallback',
  'event' => 'change',
  'progress' => [
    'type' => 'throbber',
    'message' => t('Verifying entry...'),
  ]
];

but what I want is to be able to print out $formState in myAjaxCallback
function myAjaxCallback(array &$form, FormStateInterface $formState){
 $response = new AjaxResponse();
 print_r($formState); //I've tried, with memory_limit=-1 . it just hangs
 dump($formState); //gives me File upload exceeds. This comes from issue with using dump in ajax
                     https://drupal.stackexchange.com/questions/250078/ajax-submission-leads-to-an-unrecoverable-error-occurred-the-uploaded-file-l

 return $response
}

I have also read https://www.drupal.org/docs/drupal-apis/javascript-api/ajax-forms and tried their Debugging AJAX Callback Functions, which has:
$response = new AjaxResponse();
$debugOut = @Kint::dump($formState); //but this uses dump too, which gives me ajax errors

so dd($formState->getValues()); kind of works. This is what it shows me. So, as you can see, I can now see the variables. It's not the best, but readable.

whereas if I go to [website]/webform?ajax_form=1 , it's not even readable.

I am running out of ideas to be able to debug the variable $formState .
Comment: Have you tried using [xdebug](https://xdebug.org/)?
Comment: @sonfd I have not. I'll give it a go
Comment: @sonfd can you submit your comment as the solution. Just installed [Xdebug](https://xdebug.org/wizard) for phpstorm on XAMP, it's amazing! exactly what I've always needed!
Comment: Armando Silva - go ahead and add it as an answer and feel free to add any helpful details you can about setting it up.
Answer: @sonfd Xdebug helps print out the values beautifully. No more print_r() or kint() errors.

Debugging Ajax Callback


How to include a patch to a JS library?

Question: I'm using the Superfish library in my Drupal 8.7.x project. I included it in composer.json like this:
 "repositories": [
        {
            "type": "composer",
            "url": "https://packages.drupal.org/8"
        },
        {
            "name": "mehrpadin/superfish",
            "version": "2.1",
            "type": "drupal-library",
            "source": {
                 "url": "https://github.com/amclin/Superfish-for-Drupal.git",
                 "comment": "use amclin because of pull request 29",
                 "type": "git",
                 "reference": "c11a5ac10f8c99f809858c0384a4c415411778ce"
            },
            "require": {
                 "composer/installers": "~1.0"
             }
         }
    ]
.....

Now I found a patch to a problem I have, so I'd like to apply the patch to the JS file automatically. Much like the way I can apply patches to Drupal modules.
Specifically this one:
https://www.drupal.org/files/issues/smallscreen_patch.diff
Any idea how to do that?
Comment: Look into composer-patches. Does your composer file include it?
Comment: Yes, I'm already using it to patch Drupal modules. I didn't know I could use it to patch the JS libraries as well.
Answer: As Kevin already mentioned: it can be done using 'composer-patches' in a similar way to patching a Drupal module.
The mentioned patch contained more than I wanted to patch, so I downloaded the patch and added it to a local 'patches' directory. Also, the patch uses a path that starts at 'libraries', while the patch configuration starts one level deeper.
So the patch configuration becomes:
....
patches {
    "mehrpadin/superfish": {
        "Fixed nested UL accordion functionality": "./patches/smallscreen_patch.diff"
    }
}
...

And the patch itself is changed from:
diff --git a/libraries/superfish/sfsmallscreen.js b/libraries/superfish/sfsmallscreen.js
index 9765fed..f5722a9 100644
--- a/libraries/superfish/sfsmallscreen.js
+++ b/libraries/superfish/sfsmallscreen.js

to
diff --git a/superfish/sfsmallscreen.js b/superfish/sfsmallscreen.js
index 9765fed..f5722a9 100644
--- a/superfish/sfsmallscreen.js
+++ b/superfish/sfsmallscreen.js

How to include a patch to a JS library?


Prove $x+y+z$ divides $x^2+y^2+z^2$ where x,y,z are consecutive terms in a geometric series

Question: The natural numbers $x,y,z$ are consecutive terms in a geometric series. Proves that $x+y+z$ divides $x^2+y^2+z^2$.
$x = ar\\
y = ar^2 = xr\\
z = ar^3 = xr^2$
So..
$\begin{align}
x + y + z & = x + xr + xr^2 \\
 & = x(1+r+r^2)\end{align}$
$\begin{align}
x^2 + y^2 + z^2 & = x^2 + x^2r^2 + x^2r^4\\
& = x^2(1+r^2+r^4)\end{align}$
I can see that x is a factor for both but I've no idea where to go from here
Answer: Remark that $1+r^2+r^4=(1+r+r^2)(1-r+r^2)$.
Comment: You're welcome! It's something you learn by experience. One way to notice this is that $1+r+r^2=(r-\omega)(r-\omega^2)$, where $\omega=e^{2\pi i/3}=-\frac{1}{2}+\frac{\sqrt{3}}{2}i$ is a non-real solution to $\omega^3=1$. You can check that $\omega$ and $\omega^2$ are both roots of $1+r^2+r^4$, which means that $1+r+r^2$ must be a factor of $1+r^2+r^4$.
Comment: Thank you very much for the help, that solved it! How did you spot this so easily? I've just started university mathematics and I'm trying to get better at factorising because often I fail to spot them. Do you have any tips? I can spot easily $x^2 - y^2 = (x-y)(x+y)$ but fail at anything more complex.
Answer: An alternate solution is to observe that
$$x^2+y^2+z^2=(x+y+z)^2-2(xy+xz+yz)$$
and
$$xy+xz+yz=x^2r(1+r+r^2)$$
Comment: Thanks! I was working with $(x+y+z)^2$ from the start but I didn't spot that $xy+xz+yz = x^2r(1+r+r^2)$
Answer: WLOG let $x=\frac 1r, y=1, z=r$.
Then  $x+y+z=r+1+\frac 1r$.
Consider
$$\left(\frac 1r+r+1\right)\left(\frac 1r+r-1\right)=\left(\frac 1r+r\right)^2-1^2\\
=\left(\frac 1{r^2}+2+r^2\right)-1\\
=\frac 1{r^2}+1+r^2\\
\Rightarrow \quad (x+y+z)(x-y+z)=x^2+y^2+z^2$$


How do I select the first of a number of rows sharing a single field in common using SQL Server 2005

Question: I have a table of users, some of which share an email address. I want to select the first record for a given email address, based on a rank column that is assigned depending on their occupation. For example:
name              occupation           email                  rank
====================================================================
test user         accountant           firstname.lastname@example.com      5
test user         sales rep            firstname.lastname@example.com      4
test user         ceo                  firstname.lastname@example.com      1
test user2        janitor              email@example.com     10
test user2        secretary            firstname.lastname@example.com     6
test user2        principal            firstname.lastname@example.com     3

How can I select only the records with the highest rank per email address using SQL Server 2005? In other words, how can I transform the table above to this:
name              occupation           email                  rank
====================================================================
test user         ceo                  email@example.com      1
test user2        principal            email@example.com     3

Rank is not guaranteed to be unique. In the case where two people share the top rank, either row is sufficient.
Answer: select *
from my_table
join (select email, min(rank) as rank from my_table group by email) min_ranks
on min_ranks.email = my_table.email and min_ranks.rank = my_table.rank

Comment: What if 2+ rows match the value of min(rank)?
Comment: If two rows have the same rank, how would you choose between them? Your question said "the first record". Without an ID or some other indication of order, what does "first" mean?
Comment: It means I only want one of them. Which one doesn't matter.
Answer: This breaks a tie with [rank] but will not break a tie on [rank],occupation. If that tie is possible, just add more columns to the ORDER BY.
;WITH x AS
(
    SELECT name,occupation,email,[rank],
      rn = DENSE_RANK() OVER (PARTITION BY email ORDER BY [rank],occupation)
      FROM @x
)
SELECT name,occupation,email,[rank]
 FROM x
 WHERE rn = 1;


Coherence :Issue while evicting key from a cache using Entry Processor

Question: I am trying to evict cache key using EvictProcessor, Does anybody know what this error means? I am getting this error with Coherence 3.7 while this code was working with Coherence 3.5.
Error:
The internal map of a ReadWriteBackingMap has an unexpected EvictionApprover; custom maps should accept and use the supplied approver
public class EvictProcessor extends AbstractProcessor implements
    ExternalizableLite, PortableObject {

static ValueExtractor s_extractorMap = new ReflectionExtractor("getMap");
static ValueExtractor s_extractorKey = new ReflectionExtractor("getBinaryKey");

public EvictProcessor() {
}

public Object process(com.tangosol.util.InvocableMap.Entry entry) {
    if (entry.isPresent())
    {
        final ReadWriteBackingMap bm = (ReadWriteBackingMap) s_extractorMap.extract(entry);
        final ConfigurableCacheMap local = (ConfigurableCacheMap) bm.getInternalCache();
        local.evict(s_extractorKey.extract(entry));
    }
    return null
}
public void readExternal(PofReader pofreader) throws IOException {}
public void writeExternal(PofWriter pofwriter) throws IOException {}
}

And then I am using above EvictProcessor to evict the entries from cache.
cache.invoke(key,new EvictProcessor ());

When I upgraded Coherence to 188.8.131.52 above error disappeared but still eviction not working.
Comment: see: http://docs.oracle.com/cd/E24290_01/coh.371/e22623/technotes.htm#BABGCGDD. There is an info about fixed bug with `EvictionApprover`. Maybe you should upgrade Coherence version.
Comment: @KrzysztofKosmatka When I upgraded to Coherence 188.8.131.52 this error message disappeared but Manual Eviction is still not working.
Comment: What is the actual class of `entry`? Does it have methods `getMap` and `getBinaryKey`? Does `process` method ends without exception? I didn't search thoroughly, but I don't see `getMap` method on any common `InvocableMap.Entry` implementations. Maybe between 3.5 and 3.7 implementation of `InvocableMap.Entry` has been changed or `getMap` method has been removed. I'm just guessing here, but maybe it will help you.
Comment: @KrzysztofKosmatka After upgrading to 188.8.131.52 eviction is still not working for few distributed caches. I don see any error or warning.
Comment: I'm affraid I cannot help you any further.
Comment: I'm just curious why are you using reflection and trying to access Coherence internals, which, as you can see, can change from version to version? Is there anything wrong with calling `InvocableMap.Entry#remove`?
Comment: @SergeyKhudyakov As part of manual eviction we just want to evict entries from Local Cache of backing map. Problem is this eviction is very random. When I debug the code I can see entry is getting evicted everytime but again it is getting added. I also applied listener on backing map cache where I can see one delete event first time while in other attempt I see two events of Add and delete.
Comment: @SergeyKhudyakov calling InvocableMap.Entry@remove will call its erase method of cache store which I don't want.
Answer: There was bug in Coherence v220.127.116.11 with eviction using BackingMap. This issue got fixed when upgraded to Coherence v12.


What sequences in $\Bbb Z[\frac16]$ converge to the fixed point $0$ in $\lvert\cdot\rvert_2$ while being fixed in $\lvert \cdot\rvert_3$?

Question: Suppose some uniformly continuous (under the 2-adic topology) function $f:\Bbb Z[\frac16]\to\Bbb Z[\frac16]$ converges under composition with itself to $0$ (in the 2-adic topology) for all inputs, and also satisfies $f(0)=0$.
Also let $f$ preserve the $3$-adic valuation, i.e. $\lvert f(x)\rvert_3=\lvert x\rvert_3$

Is there some reason why such a function can approach $0$ only through some sequence terminating in $2^m3^n:m\to\infty$?

I can't show that some counterexample is a counterexample, but then my skills are sadly lacking.
Answer: I'm not sure that I understood all the conditions, but I think that the following function may be of interest here:
$$
f(x)=10x.
$$
Denoting the $n$-fold iterate by $f^{[n]}$.
Because $2\mid 10$ we have $$|f^{[n]}(x)|_2=(1/2)^n|x|_2$$ for all $x$. Therefore, for all $x$, $$\lim_{n\to\infty}f^{[n]}(x)=0$$ with respect to the $2$-adic metric. For the same reason $f$ is uniformly continuous w.r.t. the $2$-adic metric.
I'm uncertain what you mean by can only approach zero through some sequence terminating in $2^m3^n$ means, but starting with the initial point $x=1$
the sequence $(f^{[n]}(x))_{n\in\Bbb{N}}$ has no entries of the form $2^n3^m$.
Because $\gcd(3,10)=1$ we have $|f(x)|_3=|x|_3$ for all $x$.
Comment: Fully prepared to have misunderstood something, but you should describe the conditions more precisely.
Comment: Not misunderstood at all, thank-you, this appears to be perfect, in fact you've shown something stronger than what I asked for.  Really helpful to see the significance of picking $f(x)=2\cdot px$ where $\gcd(2p,3)=1$.
Comment: You mean *surjective*? My example $f$ is injective: $f(x)=f(y)\implies x=y$.
Comment: Hmm. That may prove to be a problem. May be not with your domain, but in $\Bbb{Z}_2$ surjectivity, uniform continuity and convergence to zero may be a difficult menage a trois due to compactness.
Comment: I was assuming we need to be in a subset of $\Bbb Q_2$ due to fractions $x/2^m$ in $\Bbb Z[\frac16]$. No counterexample for surjective $f$ is equivalent to the Collatz conjecture. If I could tempt you into a chat about it, I think you could really help me a lot but I also understand why you would not want to do that.
Comment: Wait a second! What's wrong with $f(x)=2x$. Because $1/2$ is in your domain that is surjective. With initial point $x=5$ we get the sequence $f^{[n]}(x)=5\cdot2^n\to0$.
Comment: Yes, that would seem to be a fairly obvious counterexample. I wasn't so confident about the implications of being uniformly continuous but it appears to be fairly straightforward.


Simplify infinitely recursive function $f(a,b) = a+b\sin( a + b\sin( \cdots ( a + b\sin(a) ) \cdots ) )$

Question: Is there a mathematical method to simplify this infinitely recursive function?
I have tried to approximate it, but for work with the ranges I am considering, precision becomes an important factor.
the recursion is as follows
$f_i = a + b \ \sin( f_{i-1} )$       with $f_0 = a$
For example, 3 stage recursion:
$a + b \ \sin( a + b \ \sin( a + b \ \sin( a ) ) )$
Variable ranges if it helps for an approximation
$a \in [-2\pi , 2\pi]$ and $b \in [0,1]$
P.s. I apologize for the poor formatting. This is my first time in StackExchange.
Comment: Let $F(x) = a + b \sin(x)$. Since $|F'(x)| = |b\cos x| \le |b|$ independent of $x$, the map is a contraction when $|b| < 1$ and your sequence $f_{i+1} = F(f_{i})$ will converge to the unique fixed point for $F(x) = x$. ( Not 100% sure about the case $b = 1$ but I believe your sequence also converge to a unique fixed point). You problem reduces to finding the fixed point of $F(x)$.
Comment: Yes, I understand. Well, I was trying to come up with a good method to approximate $E$ without iterations for use in a semi-analytical method. I even tried hard-coding 2 iterations of Newton-Raphson as an equation with $E_0 = M + e \sin(M)$. But since the equations became fairly complex (without the iterations), I wanted to see if I can simplify in other ways.
Comment: Welcome to MSE. For some basic information about writing mathematics at this site see, *e.g.*, [basic help on mathjax notation](/help/notation), [mathjax tutorial and quick reference](//math.meta.stackexchange.com/q/5020), [main meta site math tutorial](//meta.stackexchange.com/a/70559) and [equation editing how-to](//math.meta.stackexchange.com/q/1773).
Comment: thank you. I'll go through it and edit my post.
Comment: Recursion $f_i = a + b \ \sin( f_{i-1} )$ is of the general form $f_i=\varphi(f_{i-1})$ with $\phi$ continuous (and even differentiable). Assuming convergence to a finite limit $x$, this $x$ will satisfy equation $x=\varphi(x) \iff x=a+b \sin x$ which has no simple expression (intersection of a line with equation $y=(x-a)/b$ and sine curve).
Comment: I believe the series converges to $c$, where $c$ is related to $a$ with the equation: $c - b \sin(c) = a$. I only realized this with experimentation.
Comment: I think I understand. Just to clarify, I am trying to solve "Kepler's Equation" for elliptical orbits $E-e\sin(E)=M$.  Through experimentation I found that $E=M+e\sin(M+e\sin(  \cdots M+e\sin(M) \cdot ))$. The series with just 3 or 4 recursions provides a good approximation of $E$ with good precision for $e\leq 0.5$. But for cases with $e$ close to $0.9$, a large number of recursions are required. Thank you again.
Comment: Indeed, "fixed point" iteration methods $f_i=\varphi(f_{i-1})$ are known to have slow convergence in general. You should turn to more efficient ones like Newton-Raphson. it's important not to re-invent the wheel : many people have been working on efficient ways of solving Kepler's equation. See for example http://jgiesen.de/kepler/kepler.html
Comment: Thanks for your answer to my comment ; I discovered it by chance. Think to prefix any comment by @pseudo.
Comment: @jeanmarie Ohh of course. Sorry I'm still getting used to math stack exchange.
Answer: If your goal is to solve for $E$ Kepler's equation
$$E-e\sin(E)=M$$ there should not be any problem using high-order iterative methods. The only problem is to have a reasonable starting guess.
The solution being bounded $M < E <M+e$, expand as a series around $E=M$ and use series reversion to get
$$E_0=M+\frac{e \sin (M)}{1-e \cos (M)}-\frac{e^3 \sin ^3(M)}{2 (1-e \cos (M))^3 }+\cdots$$ Now, to find the zero of function
$$f(E)=E-e\sin(E)-M$$ use Newton, Halley or Householder method with this guess $E_0$.
In fact; if I properly remember, there are explicit solutions in terms of Lambert hyperfunction writing the equation in the exponential form
$$E \exp\Bigg[\log \left(1-e\frac{ \sin (E)}{E}\right)\Bigg]=M$$
Comment: Thank you. But as I stated in the comments, I was looking for an analytical approximation without iterations. I was aware of Lagrange and Bassel series solutions but requires a lot of trig evaluations. But generally yes, I use a Newton-Raphson with a guess solution for any numerical simulations. (Unfortunately I do not have enough reputation other than to comment at the moment)


How do I override a function in a module

Question: I want to batch override (there are hundreds of terms) menu paths generated by the Taxonomy Menu module for a custom menu I require.  I know not to edit the module itself. I assume I create a separate module containing the replacement function to substitute the desired path I want, but I am not certain how to direct Drupal to use it instead of the original.  Or am I using the wrong strategy for this?
UPDATE:
I have solved the specific example in the question using a panel tied to a view. However, I am still interested in the question itself. A theme function can be overridden by including the replacement function with my theme name replacing the original theme name in the function name. Drupal handles the substitution from there; the same renaming option is not available for module functions.
I have continued to research this and found several posts at Drupal.org, that suggests it is not possible to override module functions in the same way you override theme functions.  There was a module for this in Drupal 6 (Override_Function), but it required additional software and appeared to have some problems working with PHP 5.2 & 5.3. The only workaround I have found so far is to hack it, but keep track of your changes so that it can be reinserted after updates.
Answer: It sounds more like you want to use hook_menu_alter to override what the Taxonomy Menu module's hook_menu function does.
Comment: `hook_menu_alter` **is** what you want. You can change the 'page callback' and that will change the path to which it goes. You'll also save yourself the maintenance headache come upgrade time.
Comment: +1 I was doing some housekeeping on older questions and missed crediting your answer. Thanks for helping.
Comment: I don't think so.  I do not want to remove an item from the menu tree, I want to change the path the menu item calls when it is selected.  Taxonomy or Taxonomy Menu does not allow altering the path through the UI. I want the menu item to go to a view rather than a taxonomy term.  I only want to override a single function in the Taxonomy Edit module to create this specialized menu and then turn off my custom code.
Comment: wizonesolutions.  I agree with you and actually solved my problem using a panel redirect.  In the meantime I have also been exploring modules and have a situation unrelated to the menu example that requires a modification to a function in a module.  I forgot the specific example in the question and simply addressed the question itself. IF you know of a way to override a function I'd love to hear it. I modified the answer I gave to make that a bit clearer for anyone who reads this question.


Show that the range of matrix $A$ is perpendicular to null space of tranpose of $A$

Question: $R(A)$ is range of $A$
$N(A)$ is nullspace of $A$
$R(A^T)$ is range of $A^T$
$N(A^T)$ is nullspace of $A^T$
Suppose $y \in R(A)$ and $x \in N(A^T)$.
How would one go about showing that $x^Ty=0$ (aka $x$ is perpendicular to $y$)?
Answer: Hint: By the definition of $R(A)$, there exists $z$ such that $y=Az$. Then note that $x^\top y = x^\top (Az) = (A^\top x)^\top z$.
Answer: With $y=Az$,
$$ \langle x, y\rangle =\langle x, Az\rangle =\langle A^Tx,z\rangle=\langle 0,z\rangle =0.$$
Answer: Hint:  The range is the span of the columns.


A simple-looking diophantine equation

Question: Consider the diophantine equation $Q(x,y,z)=0$, where $x$, $y$ and $z$
are nonnegative integer unknowns and
$$
Q(x,y,z)=x^3 + (-2y + 2)x^2 + ((z - 6)y + (2z + 1))x + ((2z - 4)y + 3z)
$$
Since the degree of $Q$ in $y$ and $z$ is $1$, the equation seems tractable. If
$t\geq 0$, then $(x,y,z)=(t,t,t)$ is a solution. Are there any others ?
Answer: Modulo mistakes in my algebra, the equation can be rewritten as $$((x+2)y+2x+3)(2(x+1)-z)=(x+1)(x+2)(x+3)$$ So my suggestion would be, test $x=1,2,3,\dots$ in turn. For each $x$, you only have to try values of $z$ with $z\lt2(x+1)$ and with $2(x+1)-z$ a factor of $(x+1)(x+2)(x+3)$. Then you just have to see whether $y$ comes out integral. You should be able to test lots of values of $x$ pretty quickly and get some idea of what the solution space looks like.
EDIT: Perhaps a better idea is to look at the equation modulo $x+2$. It becomes $z\equiv-2\pmod{x+2}$. Combined with $0\lt z\lt2(x+1)$, this proves $z=x$. So now we have $$(x+2)y+2x+3=(x+1)(x+3)$$ which is $y=x$, QED.
Comment: Amazing...I would have spent days seeing that!
Comment: It is a solution...the LHS is $((3)*1 + 2 + 3)(2(2) - 1) =8*3 = 24$ and the RHS is $2*3*4 = 24$.
Comment: Gerry's equation is just a rearrangement of the original equation...he probably just noticed that if you isolate the right things then you get a nice factorisation. Then the rest of the argument is pure genius.
Comment: @fretty: you are right, I had a type, thank you

A simple-looking diophantine equation


How to select the latest dateTime of every week ? C#

Question: I have to implement an algorithm who receive a list of DateTime and its recovers the latest DateTime of every week.
Example:
29/06/2016 -> Lastest date of the last week of the month
27/06/2016
24/06/2016 -> Lastest date of the before last week of the month
22/06/2016
21/06/2016
15/06/2016 -> Lastest of the third week
13/06/2016
...

Expected result
29/06/2016
24/06/2016
15/06/2016

I dont ask the code who done that but some directions or indication for resolve this problem.
Comment: What do you count as a "week"? Monday to Sunday? Sunday to Saturday? Something else?
Comment: In other words, are you need the end of every week to be returned?
Comment: @milletantoine in case of  `01/01/2015` and `01/01/2016` should both appear in your resultset or should `01/01/2016` be the only result, because both are week 1
Comment: If they are the lastest date of the week present in the list. the both should.
Answer: Note: this answer assumes that you're interested in "natural" weeks, which don't really care about the year. For example, December 31st 2015 was a Thursday, and January 1st 2016 was a Friday - to my mind (and using the algorithm in this answer) those would count as being in the same week... the Monday before/on both days is the same, after all.
The simplest solution to this is probably to group by "start of week", then take the latest value within each group. Finding the start of the week is slightly awkward, and best done in a helper method:
var latestPerWeek = dates.GroupBy(StartOfWeek)
                         .Select(g => g.Max())
                         .ToList();

...

private static DateTime StartOfWeek(DateTime date)
{
    date = date.Date; // Just remove any time parts...
    // 0=Sunday, 1=Monday... 6=Saturday
    int sundayToSaturdayDayOfWeek = (int) date.DayOfWeek;
    // 0=Monday, 1=Tuesday... 6=Sunday
    int mondayToSundayDayOfWeek = ((sundayToSaturdayDayOfWeek - 1) + 7) % 7;
    return date.AddDays(-mondayToSundayDayOfWeek);
}

Note that the .NET "week of year" handling is tempting to use, but basically doesn't handle this scenario. In Noda Time 2.0 this will be a lot simpler, but that isn't available yet...
Comment: I played a bit with your code, should be in case of `30.12.2015`, `31.12.2015`, `01.01.2016` be one or two valid results? I'd say `31.12.2015` is the latest of 53th week and `01.01.2016` is the latest of 1st week
Comment: @fubo: Well there's no indication that that's what the OP wants - I would personally assume that they'd want each week to consist of exactly 7 days, so treat 2016-01-01 and 2015-12-31 as being in the same week. I'll add that assumption into the answer though.
Answer: This works only for dates which are in the same year
I would use CultureInfo.InvariantCulture.Calendar.GetWeekOfYear to determine the week number. After that you can GroupBy this number and order each group descending. Now you just have to pick the First() item of each week/ group
  List<DateTime> dtList = new List<DateTime>();
  dtList.Add(new DateTime( 2016, 6,29));
  dtList.Add(new DateTime( 2016,6, 27));
  dtList.Add(new DateTime( 2016,6 ,24));
  dtList.Add(new DateTime( 2016,6,22 ));
  dtList.Add(new DateTime( 2016,6,21 ));
  dtList.Add(new DateTime( 2016,6, 15 ));
  dtList.Add(new DateTime( 2016, 6,13));

  List<DateTime> dtResult = dtList
      .GroupBy(x => CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(x, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday))
      .Select(x => x.OrderByDescending(y => y).First())
      .ToList();

Comment: That will fail if there are multiple years involved, as they'll be grouped by just week-of-year. Unfortunately .NET's week-year handling isn't terribly nice.
Comment: @JonSkeet so you would GroupBy Week + Year
Comment: No, you can't - because you really want to group by "week year" which isn't exposed, and weeks across year boundaries get split in .NET. Basically, the week-of-year handling in .NET just isn't up to the job here. See http://nodatime.org/unstable/userguide/weekyears.html for more on this.
Comment: That's irrelevant. Basically, two days in the same Monday-to-Sunday week can have different week numbers in .NET and be in different years... but you'd want them to group together. I've been doing a lot of work on this recently for Noda Time - please trust me when I say the BCL *really* isn't being helpful here.
Comment: If you want an example, consider 2014-12-29 (Monday) and 2015-01-01 (Thursday). They should be counted in the same week - they're not using your algorithm.
Comment: ok got it. i'll comment my answer to be valid only with dates within the same year


Como faço para saber se o usuário realmente deseja voltar, quando clica no botão voltar do celular?

Question: Tenho uma activity que guarda determinados dados, e quando o usuário aperta o botão de voltar (Botão de Hardware do Avd, por exemplo), a aplicação não volte para a Tela anterior, mas sim que abra uma tela de diálogo questionando se o usuário deseja retornar à tela anterior sabendo que perderá os dados salvos.
Como fazer a captura desse evento?
Comment: O título da sua pergunta é diferente do corpo da pergunta. No primeiro você está perguntando sobre UX, e  no segundo sobre programação.
Comment: Apenas comentando que isso não é uma prática recomendada na plataforma Android. O botão voltar deve SEMPRE voltar imediatamente. Salve os dados em algum estado se preciso, ou simplesmente deixe o usuário perder os dados.
Answer: Experimente usar esse código:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Handle the back button
if(keyCode == KeyEvent.KEYCODE_BACK) {
    //Ask the user if they want to quit
    new AlertDialog.Builder(this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle(R.string.quit)
    .setMessage(R.string.really_quit)
    .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {

            //Stop the activity
            YourClass.this.finish();
        }

    })
    .setNegativeButton(R.string.no, null)
    .show();

    return true;
}
else {
    return super.onKeyDown(keyCode, event);
}

}

Ou esse para Android 2.0+
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
    .setIcon(android.R.drawable.ic_dialog_alert)
    .setTitle("Closing Activity")
    .setMessage("Are you sure you want to close this activity?")
    .setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();
    }

})
.setNegativeButton("No", null)
.show();
}

Comment: Eu nem consideraria a primeira opção. Apps abaixo de 2.2 nem sequer tem acesso a play store.
Comment: Mas ainda podem instalar APK's por transferência.
Comment: Sim, mas se alguém tiver um aparelho rodando Eclair, vende pro museu que dá uma grana :)
Comment: JP funcionou perfeitamente a segunda forma, muito obrigado!
Answer: Um pouco de Google me deu o seguinte resultado:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){
return true;

}
return super.onKeyDown(keyCode, event);
}

Ele apenas checa se o keyevent é igual ao keycode dado!
Lembrando que se você formatar um pouco, você pode usar o código para detectar qualquer keypress!
(fonte: http://nisha113a5.blogspot.com.br/2012/01/intercept-home-key-android.html)


Consistência nos resultados em Python

Question: Olá, estou estudando código em python e me deparei com esta linha:
np.random.seed(0)

no qual diz para consistência de resultados. Eu pesquisei, mas ainda não consegui compreender a função desse comando. Alguém poderia explicar ou indicar o local onde posso encontrar documentação?
Valeu ajuda.
Comment: O método `seed()` serve para iniciar o gerador de números pseudoaleatórios. Documentação: [https://docs.python.org/3/library/random.html](https://docs.python.org/3/library/random.html)
Comment: Possível duplicata de [Como é gerada a randomização pelo computador?](https://pt.stackoverflow.com/questions/9026/como-%c3%a9-gerada-a-randomiza%c3%a7%c3%a3o-pelo-computador)
Answer: np.random.seed(0) define que a geração randômica de números seguirá um padrão pré definido.
Ao fazer np.random.seed(0), a próxima chamada de numpy.random.rand(n), sendo n o tamanho do vetor a ser gerado, seguirá um padrão fixo.
Por exemplo, sem definir uma seed, chamadas iguais de numpy.random.rand(n) retornariam resultados diferentes.
>>> numpy.random.rand(5)
array([0.79172504, 0.52889492, 0.56804456, 0.92559664, 0.07103606])
>>> numpy.random.rand(5)
array([0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152])

Porém ao setar a seed antes de chamar numpy.random.rand(n), você garante que a sequência gerada será igual / seguirá o mesmo padrão todas as vezes.
>>> numpy.random.seed(0)
>>> numpy.random.rand(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
>>> numpy.random.seed(0)
>>> numpy.random.rand(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

Note que ao chamar numpy.random.rand(5), a seed é resetada, sendo necessário seta-la novamento com numpy.random.seed(0).

Consistência nos resultados em Python


Determinant of a tridiagonal Toeplitz matrix

Question: Let $A_n$ denote an $n \times n$ tridiagonal matrix.
$$A_n=\begin{pmatrix}2 & -1 & & & 0 \\ -1 & 2 & -1 & & \\ & \ddots & \ddots & \ddots & \\ & & -1 & 2 & -1 \\ 0 & & & -1 & 2 \end{pmatrix} \quad\text{for }n \ge 2$$
Set $D_n = \det(A_n)$. Prove that $D_n = 2D_{n-1} - D_{n-2}$ for $n \ge 4$.
Comment: What have you tried so far? If you don't know where to begin, perhaps you should make use of the variable "n."
Comment: I've "unclosed" this question as duplicate of http://math.stackexchange.com/q/266998/18880, because none of the answers to that question make it even remotely obvious that for this specific case the determinant is $n+1$. (I know this question does not explicitly ask to compute it, but [another question](http://math.stackexchange.com/questions/1148581/determinat-evaluation-for-matrix-with-1-2-1-below-on-above-diagonal) marked as duplicate of this one does.) Much as I like closing for abstract duplicates, I think this question is better not marked as duplicate of a considerably harder question.
Comment: See also: http://math.stackexchange.com/questions/1148581/determinat-evaluation-for-matrix-with-1-2-1-below-on-above-diagonal
Comment: For a more general version, see [this answer](http://math.stackexchange.com/questions/266998/special-determinant-formula-for-a-specific-matrix/267466#267466).
Answer: Expand with respect to the first column: you get
$$
D_n
=2 \times
\begin{vmatrix}2 & -1 & & & 0 \\ -1 & 2 & -1 & & \\ & \ddots & \ddots & \ddots & \\ & & -1 & 2 & -1 \\ 0 & & & -1 & 2 \end{vmatrix}_{(n-1)}
- (-1) \times
\begin{vmatrix}-1 & 0 & & & 0 \\
-1 & 2 & -1 & & 0 \\
 & \ddots & \ddots & \ddots & \\ & & -1 & 2 & -1 \\ 0 & & & -1 & 2 \end{vmatrix}_{(n-1)}
$$
Now expand according to the first line the second determinant: you get
$$
=2 \times
\begin{vmatrix}2 & -1 & & & 0 \\ -1 & 2 & -1 & & \\ & \ddots & \ddots & \ddots & \\ & & -1 & 2 & -1 \\ 0 & & & -1 & 2 \end{vmatrix}_{(n-1)}
- (-1)^2\times
\begin{vmatrix}2 & -1 & & & 0 \\ -1 & 2 & -1 & & \\ & \ddots & \ddots & \ddots & \\ & & -1 & 2 & -1 \\ 0 & & & -1 & 2 \end{vmatrix}_{(n-2)}
$$
Conclusion:
$$
D_n = 2D_{n-1} - D_{n-2}
$$
Answer: Here is a way to compute the determinant without using induction. Multiply the matrix $A_n$ to the left by the $n\times n$ upper triangular matrix $U_n$ with all entries on and above the diagonal equal to$~1$:
$$
  \begin{pmatrix}1&1&1&\ldots&1\\ 0&1&1&\ddots&\vdots\\0&0&1&\ddots&1\\
  \vdots & \ddots & \ddots & \ddots & 1 \\0&0& \ldots &0 & 1
 \end{pmatrix}
 \begin{pmatrix}2 & -1 & & & 0 \\ -1 & 2 & -1 & & \\
 & \ddots & \ddots & \ddots & \\ & & -1 & 2 & -1 \\
 0 & & & -1 & 2 \end{pmatrix} =
\begin{pmatrix}1&0&0&\ldots&0&1\\ -1&1&0&\ddots&0&1\\0&-1&1&\ddots&0&1\\
  \vdots & \ddots & \ddots & \ddots & 0&1 \\
  \vdots & \ddots & \ddots & -1 & 1&1 \\0&0& \ldots &0&-1 & 2
 \end{pmatrix}.
$$
Since $\det(U_n)=1$, we shall get $\det(A_n)$ as the determinant of our resulting matrix$~R$.
Now recognise  that $R=I-C_P$ where $C_P$ is the companion matrix of the polynomial $P=X^n+X^{n-1}+\cdots+X^2+X^1+X^0$. Since it is well known that $P$ is the characteristic polynomial $\det(IX-C_p)$ of $C_P$ we get
$$
 \det(A_n)=\det(R)=\det(I-C_P)=\det(IX-C_P)[X:=1]=P[X:=1]=n+1.
$$