How do survival models "account for censoring"? (Do they?)

Question: Background
I'm teaching an intro stats class in our social / health sciences department and I'm finding myself tripped up on something I'd always taken for granted: namely, the claim that survival analysis methods (from Kaplan-Meier to Cox models) "account for" censored data, and that this is one of these methods' central advantages over other approaches.
(Relevant background: in my research I use, mostly clumsily, some applied statistical and quantitative methods, and I have a good grasp of and intuition for some of the basics. So I'm an okay generalist but I'm not a statistician nor even a statistics grad student by any means.)
The Problem
As I'm preparing some PowerPoint slides for these students, though, I'm realizing that while I have a pretty good grasp on what censoring is in survival methods, I have no idea how something like a Cox model "accounts" for it. I'd never thought about it, and instead assumed that this is just what happened. (It's possible that a professor walked us through this earlier in my education, but it's also possible that I used to struggle in 8am stats lectures.)
Censoring, as you all already know, is the state in which we have some information about individual survival time, but we don't know the survival time exactly (Kleinbaum & Klein, p. 5; I've always liked this plain English explanation). In my experience, sometimes when stats people are trying to sell someone on survival methods, they'll say things like "logistic regression doesn't account for censoring, but Cox regression does!"
There are some great posts on CV about censoring; this is maybe my favorite example. In it, user @Tim gives a terrific explanation of censoring:

Intuitive example of censoring is that you ask your respondents about their age, but record it only up to some value and all the ages above this value, say 60 years, are recorded as "60+". This leads to having precise information for non-censored values and no information about censored values.

I think this is brilliant (in fact I plan on borrowing liberally from it during the lecture, with credit given of course). But it doesn't get really get into how survival analyses actually deal with this, and whether it's a selling point for survival methods ("our methods can do this and yours do not") or just something that pops up when you try and ask survival-type questions ("how long will people live, on average, after being given treatment X?").
The Questions

Is it true, strictly speaking, that something like a Kaplan-Meier estimator or a Cox proportional hazards model is "accounting" for censoring?

If so, how is it doing that?

If a survival model indeed accounts for censoring, is this a "feature" of survival methods over others, or a "bug", an inevitable artifact of the sorts of questions one uses survival methods to answer?

My guesses
Well, not guesses, maybe more like very unclear intuitions I'm not too confident about:

Why is censoring a problem? I'm thinking that, if ignored, censoring is a huge potential source of bias in making survival estimates: if you don't know what happened after Mr. Smith dropped out of your study (i.e. was lost to follow-up, i.e. was censored) your estimates may be off in one direction or the other. Maybe he lived a long, long time -- or maybe he died the next day. If this is happening to lots of people in the same way, your estimates may be really, really off.

So maybe what survival models have found a way to do is keep in the analysis everyone who contributed survival time, regardless of whether we know about their outcome status, while other methods would simply drop all of those people's data as missing.

Am I way, way off here?
Comment: Is it really true that we have no information about censored values? This makes censored values sound suspiciously like missing values. Knowing that a value is ≥ 60 is partial information rather than no information.
Comment: Yes, if I read the following quote right: "This leads to having precise information for non-censored values and no information about censored values."
Comment: As long as your students are okay with a generous reading, all is good. I'm pointing it out because your question is about precise but correct phrasing.
Comment: "unknown unknown" (missing data) vs "known unknown" (censoring)?
Comment: Do I say in my question that we have *no* information about censored values? Sincere question; I took care to emphasize that censored data is *incomplete* and not *fully missing* data, but I may have overlooked something. Also: "this makes censored values sound suspiciously like missing values." From the reading I've been doing since I posted (see Leung et al 1997, as cited by EdM), censoring has a lot of overlap w/ missing data problems!
Comment: Oh, got it, that's a quote from Tim's answer in the post I linked to. Yeah his phrasing is off for sure, but the generous reading would be that we have no *outcome* information for censored values, which is true: we don't ultimately know what exactly happened to them after censoring.
Comment: I mean, I guess? I'm not having the students read CV posts; I'm just trying to give them the best information, which seems from general consensus to be "censored data is partially complete data, and the incomplete bits often have to do with the outcome".
Comment: That's a terrific way of slicing it. Thanks. (Also lol Rumsfeld)
Answer: Censoring is built into survival models by incorporating it into the likelihood function underlying the analysis.  The most common form of censoring occurs when we observe an item for a finite period of time $T$ and it does not fail in that time.  Below I will show you how the censoring is built into the likelihood function and how this affects the Cox proportional hazards model.

Incorporating censored data into the likelihood function: As a common example, suppose we have items where the time-to-failure has a survival function $S$ and corresponding density function $f$, both of which area parameterised by some parameter $\theta$.  If an item $i$ is observed to fail at time $0 \leqslant t_i \leqslant T$ then it is incorporated into the likelihood function using the density term:
$$f(t_i | \theta).$$
However, if an item $i$ is observed throughout the whole time $T$ and it does not fail then this is considered to be a "right-censored" data point (only known to fail at some time after $G$) and it is incorporated into the likelihoood function using the survival term:
$$S(T|\theta).$$
Suppose we have a survival model based on observation for a fixed period of length $T$, where the times-to-failure for each observation are IID conditional on some underlying parameters.  Without further loss of generality, we will have $n$ observed failures at times $t_1,...,t_n$ (all within the interval $[0,T]$) and we will will have $m$ right-censored values that did not fail in the oberved time $T$.  The overall likelihood function for this data is then given by:
$$L_\mathbf{t}(\theta) = \bigg( \prod_{i=1}^n f(t_i|\theta) \bigg) \times S(T|\theta)^m.$$
In this likelihood function you can see that the censoring of data is "built in" by the fact that right-censored values are incorporated through their survival function instead of the density function for the time-to-failure.

Extending to the Cox proportional hazards model: The Cox proportional hazards model still uses a likelihood function for the observed times-to-failure and survival times, but it now adds covariates to the data and uses an assumption of proportional hazards in how these manifest in the hazard function.  This does not change the underlying method of how censored values are built into the likelihood function --- e.g., right-censored values still enter through their survival function instead of the density of the time-to-failure.

Extension to other kinds of censoring: The above shows the common case where we have right-censored observations with the same censoring time $T$.  Of course, this is not the only kind of censorship that can occur.  Another possibility is that we might observe items up to different end-times, in which case the right-censored values would occur with different observation periods $t_{n+1},...,t_{n+m}$.  In this case the likelihood function would be generalised to:
$$L_\mathbf{t}(\theta) = \bigg( \prod_{i=1}^n f(t_i|\theta) \bigg) \times \bigg( \prod_{i=1}^m S(t_{n+i}|\theta) \bigg).$$
Another possibility (which is uncommon in survival analysis) is left-censorship, where we know that an item failed no later than some time $T_i$.  Left-censored observations enter into the likelihood function through the cumulative distribution function $F$.  If we extend our model to assume that we have $r$ left-censored observations with observation times $t_{n+m+i},...,t_{n+m+r}$ then the likelihood function would be further generalised to:
$$L_\mathbf{t}(\theta) = \bigg( \prod_{i=1}^n f(t_i|\theta) \bigg) \times \bigg( \prod_{i=1}^m S(t_{n+i}|\theta) \bigg) \times \bigg( \prod_{i=1}^r F(t_{n+m+i}|\theta) \bigg).$$
And of course, you can extend this event further to allow for more complicated kinds of censorship.  In general, if a censored observation is known to fall in some set $\mathscr{A}$ then it should enter into the likelihood function through the probability term:
$$\mathbb{P}(t_i \in \mathscr{A}|\theta) = \int \limits_\mathscr{A} f(t|\theta) \ dt.$$
Comment: Thank you very much, @Ben. I'm going to reread this a couple more times, but what I'm already getting from it suffices for a great understanding of the "how" here: censoring is *explicitly* a part of the likelihood function, namely *S(T|θ)*. Would it be fair to say that other (non-survival) methods are at a disadvantage for *not* building in censoring?
Comment: I don't think the latter is accurate.  Any statistical analysis in any field ought to capture the actual nature of the observations properly in the likelihood function.  So if data is censored *in any context* then the relevant probability for the set of possible values for the censored variable ought to be what is built into the likelihood function.  This is not just something that happens in survival analysis, but that is one context where it is common.
Comment: Some posts with further examples:  https://stats.stackexchange.com/questions/133347/ml-estimate-of-exponential-distribution-with-censored-data/133360#133360, https://stats.stackexchange.com/questions/87065/weighted-normal-errors-regression-with-censoring/276929#276929, https://stats.stackexchange.com/questions/49443/how-to-model-this-odd-shaped-distribution-almost-a-reverse-j
Comment: (+1) You write that without loss of generality consider the first $m$ observations to be non-censored - but that smuggles the assumption of independent censoring. It may be better to explicitly allow which units are censored to be data-dependent.
Comment: @Ben: Yes, good point.  I have edited the relevant paragraph to point out that the likelihood function given there assumes IID times-to-failure.  I won't go into the more complicated models here.
Comment: @logjammin: I think the last paragraph of my answer essentially says that, but I've noe made it more explicit that this is the probability of the time-to-failure falling within the relevant set .  So yes, if there is censoring in the data, in any context, it ought to be built into the likelihood function through appropriate probabilities.
Comment: > "Any statistical analysis in any field ought to capture the actual nature of the observations properly in the likelihood function" -- Sounds like you're saying that a method not capturing censoring, which in many datasets is "the actual nature" of things, wouldn't be doing its users much good. Would that be fair?
Comment: In fact @Ben, would you be willing to add "if data is censored in any context then the relevant probability for the set of possible values for the censored variable ought to be what is built into the likelihood function" to your answer? I think that would really make it hit all the points I raised in my question. Thanks!
Answer: Some students might benefit from the following way to represent the partial likelihood for a Cox model, as an example of Ben's answer (+1) about censoring in general. Display the partial likelihood under the proportional-hazards assumption (without tied event times) as follows:
$$\prod_{i=1}^{n}\frac{h_0(t_i)\text{exp}(\beta X_i(t_i))}{\sum_{j\in\mathcal{R(t_i)}}h_0(t_i)\text{exp}(\beta X_j(t_i)))}= \prod_{i=1}^{n}\frac{\text{exp}(\beta X_i(t_i))}{\sum_{j\in\mathcal{R(t_i)}}\text{exp}(\beta X_j(t_i)))}$$
where cases with events are indexed by i and their event times are $t_i$, covariate values are $X$ with corresponding regression coefficients $\beta$, and the risk set at time $t_i$, $\mathcal{R(t_i)}$, consists of all cases $j$ at risk of an event at that time. The baseline hazard $h_0(t_i)$ factors out.
The risk score for individual j at time $t_i$, based on that individual's covariate values, is $\text{exp}(\beta X_j(t_i))$. This form emphasizes that the risk for individual i having the event is effectively compared against the summed risks of all individuals at risk at that time.
So the way that the Cox model "accounts for censoring" is by letting all individuals contribute to the partial likelihood, via their risk scores in the denominator, so long as they are at risk of an event. It doesn't matter whether those in the risk set have an event at a later time; they provide information so long as they are at risk and are omitted from analysis thereafter. You can think about that as related to the survival-function contribution that Ben describes. The overall product, however, is only over individuals who have events; that's related to the density-function contribution that Ben describes.
There also can be truncated, as opposed to censored, survival times. That's, for example, how models with time-varying covariate values can be set up: left truncated before the new covariate value takes effect, with right censoring or an event thereafter. Contributions of truncated survival times to likelihood also can be expressed in terms of the density and survival functions; this page presents those contributions as described by Klein and Moeschberger.
One caution: both my and Ben's answers implicitly assume that censoring is non-informative; that is, the fact of censoring provides no information about survival except that survival was longer than the observed time of censoring. That's not always a safe assumption. The review by Leung et al on "Censoring Issues in Survival Analysis," Annu Rev Public Health 18:83–104 (1997) explains and illustrates those further considerations.
Comment: The last paragraph is pretty important!
Comment: @logjammin remember that my phrasing in terms of numerator versus denominator only holds strictly under the proportional hazards assumption. Otherwise the phrasing in terms of probability density and the associated survival function, as in Ben's answer (which I think you should accept), and in Klein and Moeschberger, is needed.
Comment: "letting all individuals contribute to the partial likelihood, via their risk scores in the denominator, so long as they are at risk of an event" -- This is great phrasing for a lecture, @Edm. Thanks. As for assuming censoring is non-informative, yes, understood. In fact shortly after I posted my question, I found that Leung et al article! It's really well done.
Answer: I thought I'd directly answer the specific questions above: this is intended to address the top-level conceptual issues with some thoughts on teaching to this kind of audience, rather than a technical explanation.

The Questions
1.  Is it true, strictly speaking, that something like a Kaplan-Meier estimator or a Cox proportional hazards model is "accounting" for censoring?

Yes!

2.   If so, how is it doing that?

See other answers for technical details on how implemented for a Cox PH model.
I find the intuition clearer for Kaplan-Meier estimation, and in my experience (short-course teaching of these methods to researchers/analysts in health sciences settings) this is reasonably approachable for the kind of audience you are dealing with, or at least a good starting point.
(I appreciate that this question is as much for your own understanding as being about what/how to teach your students)
This applies for both visualisation ("included in denominator up to point of event/censoring" with x marks the spot on the survival curve) and for calculation (showing how we calculate cumulative survival by multiplying the preceding interval's cumulative survival with the current interval's survival in the set of individuals at risk).

3.  If a survival model indeed accounts for censoring, is this a "feature" of survival methods over others, or a "bug", an inevitable artifact of the sorts of questions one uses survival methods to answer?

Very much by design – these methods were developed to address the problems that arise when survival times are censored (and to maximise efficiency, see below)

4.   Why is censoring a problem? I'm thinking that, if ignored, censoring is a huge potential source of bias in making survival estimates: if you don't know what happened after Mr. Smith dropped out of your study (i.e. was lost to follow-up, i.e. was censored) your estimates may be off in one direction or the other. [snip]

If your time data are subject to censoring, but you have ignored this in analysis, then results will be biased. In some cases (since there are lots of different ways in which one might inappropriately address this issue) you might be treating people with only short-term follow-up as though they had the full period of follow-up.
Different issues can arise depending on how that inappropriate "non-survival analysis" were to proceed: for example, issues arise if the risk set accrues across a period (e.g. infants born with a particular condition across a ten year study period) and outcomes tend to be in earlier periods (e.g. highest risk in first two years of life).

5.  So maybe what survival models have found a way to do is keep in the analysis everyone who contributed survival time, regardless of whether we know about their outcome status, while other methods would simply drop all of those people's data as missing.

I think this is a reasonable summary: basically you can include people in follow-up time up to that point at which their information runs out. So you can view survival analysis as being more efficient, as you can (loosely speaking) include all the available numerator and denominator information (subject to a whole heap of assumptions, well described elsewhere).
To run a valid "non-survival analysis" would require everyone to have the same amount of potential follow-up time (e.g. every cancer patient followed up to at least five years) irrespective of their event status.

As a final note on how things can go wrong when not accounting for censoring:
As with any research endeavour, there are lots of different ways in which people might naively approach this kind of analysis without accounting for censoring: for example, I've seen some ideas for analysis that would include people in follow-up if they had the event at any time, but only include people with no events in the denominator if they reached the final follow-up time (net result: bad news!)
Answer: Update: @James Stanley address the same point in his answer; see point #4.

This is not an answer but an extended comment to clarify some terminology and highlight an important assumption.
You write:

if you don't know what happened after Mr. Smith dropped out of your study (i.e. was lost to follow-up, i.e. was censored) ...

implying that "loss to follow-up" is the same as "censoring". Not exactly.
Say we do a randomized control trial (RCT) of treatment T vs control C and the outcome of interest Y is the time to event (or equivalently, survival). We start recruiting subjects on January 1st and the study concludes on December 31, the same year. We follow up with subjects every month from recruitment until Dec 31.

loss to follow-up: The patient A is recruited and randomized to T or C in Jan. We have data for the first 8 visits and no data for the last 3 visits from Oct to Dec. We know the patient survived for at least 8 months.
censoring: The patient B is recruited and randomized to T or C in Apr. We have data for all 8 visits until the study concludes in Dec. We know that the patient survived for at least 8 months.

You may ask: Why make the distinction between loss to follow-up and censoring? Patients A and B appear "similar" ("exchangeable"): we followed them for 8 visits post-treatment, so we know they survived at least 8 months, but we don't know how long they survived.
They would contribute the same term $\operatorname{S}(t_i = \text{8 months} | \theta)$ in the likelihood function. [Here I refer to the likelihood $L_t(\theta)$ in @Ben's answer.]
The subtlety is that we don't know why patient A dropped out from the study. Often in analysis when we don't know something, we assume that it happened randomly. This will make the math work out just as described in Ben's answer.
However, suppose that the treatment has severe side effects and patients who received the treatment have a higher probability to discontinue treatment early. This means that subjects in the treament group are more likely to be lost to follow-up. This (potentially) introduces bias in the estimate of the treatment effect, unless more sophisticated analyses come to the rescue.


Any difference between const Class& and Class const&?

Question: Is there any difference between that expressions :
const Class&
Class const&

for example, when those are parameters of function ?
Answer: There is no difference between const Class& and Class const&; similarly here is no difference between const Class* and Class const*.
And so,
void f1 (const Class& c)

and
void f1 (Class const& c)

are interchangeable with no difference.
Both versions denote a reference to a constant Class instance and can be used interchangeably.


How to prove that binary representation of number always has more digits than base 10 representation?

Question: I understand the idea intuitively that because the number of options for each digit is more "granular" for base $2$, we need more digits to represent the same number as opposed to base $10$.
How can I express this idea (or another proof) in mathematical terms?
Answer: Take an integer $n$, for some $p\in \mathbb{Z}$ it is true that $2^p < n\leq 2^{p+1}$. It is clear that $p+1=\lceil \log_{2}(n)\rceil$. Similarly, for some $q\in \mathbb{Z}$ it is true that $10^q< n\leq 10^{q+1}$ and therefore $q+1=\lceil \log_{10}(n)\rceil$. You can see that the first quantity is always at least as large as the second quantity which is what we want to show (The first quantity is the length of the binary representation and the second the length of the decimal representation).
Comment: I'm not sure what "monotonicity of logs" means, but could we just use change of base formula, and show that $log_{10} {2}$ in the denominator is less than $1$ and thus an "amplifying factor"?
Comment: You argue that $p + 1 = \lceil log_2{(n)} \rceil$, but let's consider a number at a boundary at the power of $2$ like $16$. It is the case that $2^{4} \leq 16 \leq 2^{5}$. Note that the quantity $\lceil log_2{(16)} \rceil$ is $4$ not $5$.
Comment: Also, how would you prove that the first quantity is at least as large as the second quantity?
Comment: @NeelSandell The first equality was meant to be strict. Also, the latter is just from the definition of the logarithm. The ceiling of $a$ is at least as big as the ceiling of $b$ if $a$ is at least as big as $b$. and you can show $\log_2$ is at least as big as $\log_{10}$ using the monotonicity of logs and their respective derivatives if you like.
Comment: That works! Since $\log_{10}(n)=\log_{2}(n)/\log_{2}(10)$ which since the denominator bigger than $1$ shows what you want to show :).


How to split different values in powershell by a line

Question: With this script i am able to fetch all the Tags that a VM has but i want that in output the each key and its value should be separated by a line in the way that each key and its value appears on different lines like this
reference image
 # Sign into Azure Portal
    connect-azaccount

    # Fetch the Virtual Machines from the subscription
    $azureVMDetails = get-azvm

    # Fetch the NIC details from the subscription
    $azureNICDetails = Get-AzNetworkInterface | ?{ $_.VirtualMachine -NE $null}

    #Fetching Virtual Machine Details
    $virtual_machine_object = $null
    $virtual_machine_object = @()

    #Iterating over the NIC Interfaces under the subscription
    foreach($azureNICDetail in $azureNICDetails){

        #Fetching the VM Name
        $azureVMDetail = $azureVMDetails | ? -Property Id -eq $azureNICDetail.VirtualMachine.id
        #Fetching the VM Tags
         foreach($azureDetail in $azureVMDetails) {
        $vm_tags = $azureVMDetail| Select-Object -Property (
        @{name='Tags'; expression = {($_.tags.GetEnumerator().ForEach({ '{0} : {1}' -f $_.key, $_.value }) -join ';')}}
    )

    }
        #VM Details export
        $virtual_machine_object_temp = new-object PSObject
        $virtual_machine_object_temp | add-member -membertype NoteProperty -name "name" -Value $azureVMDetail.Name
        $virtual_machine_object_temp | add-member -membertype NoteProperty -name "comments" -Value ($vm_tags.Tags -join ';')
        $virtual_machine_object += $virtual_machine_object_temp

  }

    #Report format and path
    $virtual_machine_object | Export-Csv "C:\Users\JOHN\Desktop\Inventory\Final Scripts\VM_details_$(get-date -f dd.MM.yyyy).csv" -NoTypeInformation -Force

Comment: Please [edit](https://stackoverflow.com/posts/74059826/edit) the question and add examples of the output you're currently getting, and the output you want. Your textual description isn't really clear.
Comment: Okay let me rephrase it. My VM has more then one Tags and the output im getiing is like this "Test : Server; Dev : Server" in one line. These are two values for two keys and are separated by a ";"
I want that each Tag value and its key should appear on separate lines like this
Test : Server
(on second line) Dev : Server
Comment: For reference i have added an image in description
Answer: I tried to reproduce the same in my environment and got the results successfully by using the below PowerShell script:
$vmdeatil = Get-AzVm -Name testvm | Select -ExpandProperty Tags
$value = $vmdeatil

foreach($i in 0..($value.Count -1))
{
$ErrorActionPreference = 'SilentlyContinue'
    [array]$report += [pscustomobject]  @{
            key = $key[$i]
        name = $value[$i]
            }
}

$report | Export-Csv -Path "C:\Users\ruk1.csv" -NoTypeInformation

Response:

The output is successfully exported in the csv file like below:


Making a Function-Activated Link Appear Without Having to Refresh Browser

Question: I'm trying to use the code below to make the Vote link appear if a user logs in and a user shows up in the function getEditorsList().  The vote link only appears if the browser is refreshed.
Any idea how I could make the vote link appear without having to refresh the browser?
Thanks in advance,
John
index.php:
<?php

 require_once "header.php";
 //content
 include "login.php";
 // more content
 require_once "footer.php";

?>

In header.php:
<?php
error_reporting(0);
session_start();
require_once ('db_connect.inc.php');
require_once ("function.inc.php");
$seed="0dAfghRqSTgx";
$domain =  "...com";

            $editors = getEditorsList();
            foreach($editors as $editor)
            {
            $editorids[] = $editor['loginid'];
            }

            if(in_array($_SESSION['loginid'], $editorids))
            {
            echo "
Vote
";
            }

?>

login.php:
<?php
if (!isLoggedIn())
{

    if (isset($_POST['cmdlogin']))
    {

        if (checkLogin($_POST['username'], $_POST['password']))
        {
            show_userbox();

        } else
        {
            echo "Incorrect Login information !";
            show_loginform();
        }
    } else
    {

        show_loginform();
    }

} else
{

    show_userbox();

}

?>

Answer: Do you set $_SESSION['loginid'] after your in_array query? If you render header.php first, in_array returns false (although the session has been started, but loginid will be set a few lines down the road in login.php).
Move this:
        if(in_array($_SESSION['loginid'], $editorids))
        {
        echo "
Vote
";
        }

from header.php to login.php like this:
else {

    show_userbox();
    if (in_array...
}

Answer: If the link is present but hidden you use some DHTML (JQuery / Scriptaculous) to set the display/visibility attributes correctly.
If the link is not present in the original html (preferable for security reasons) then when the login occures fire off an AJAX request that returns javascript that will insert the link in the correct location (parent element).


mysql_ to mysqli_ Conversion Problems

Question: I have a small website that I wanted to switch over to the mysqli_* functions, and after reading up a lot on it I decided to do the switch by doing a replace-all mysql_ with mysqli_ -- I then went through and verified that everything changed correctly...
Now, the mysqli_connect() works - (I get a valid resource connection back from it) but further down the PHP script I have a mysqli_query function that is returning NULL no matter what I put in as the SQL.
Any thoughts what could be happening?
The relevant code:
function connecti_database() {
  mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
}

connecti_database();
$sql = 'SELECT * FROM table where id = 5';
$result = mysqli_query($sql);
var_dump($result); // this returns NULL every time

Comment: +1 for [converting your `mysql_*` functions to `mysqli`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php#answer-12860140).
Answer: mysqli functions require two parameters, not one
...and a developer required to read documentation first.
function connecti_database() {
  global $mysqli;
  $mysqli = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_DATABASE);
  mysqli_select_db($mysqli,DB_DATABASE);
}

connecti_database();
$sql = 'SELECT * FROM table where id = 5';
$result = mysqli_query($mysqli, $sql);

PHP Manual: http://php.net/manual/en/mysqli.query.php
mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

Comment: hmm... still getting NULL in the var_dump. (To be honest, I did try this exact thing before posting, but stupidly forgot 'global'.)
Comment: boneheaded moment for me - i needed yet another global here. Thanks, it is working now.

mysql_ to mysqli_ Conversion Problems


Concept of instantiating

Question: If we have this:
class Car
{
    public void mCar()
    {

    }

}

class Audi : Car
{
    public void mAudi()
    {

    }
}

and in some other class, we have:
Car x = new Audi();

then we only have access to mCar(), but not to mAudi().
My question is, what is the difference between:
Car x = new Audi();
and
Car x = new Car();
Comment: Where's `mKola()` coming from?
Comment: @voidStern I'm sorry, I don't use English in my programs and when I write here I always forgot to translate something. It's edited now.
Answer: The difference is
Car = new Car();

gives you a new Car object (it knows only of mCar()), while
Car = new Audi();

gives you a new Audi object, which is also a car object (it knows of mCar() and mAudi()).
Comment: @user966638: It *can*. You can't write `x.mAudi()`, because the reference `x` is itself of type `Car`, but you can write `((Audi)x).mAudi()`, which casts `x` to type `Audi`. By contrast, if you had `Car x = new Car();`, then `((Audi)x)` would not be valid. (Note: the details of this depend on the language. Your code looks like C#, so I've gone with that.)
Comment: But if it's a Audi object, then why it can't access mAudi() ?

Concept of instantiating


Commutative permutations

Question: Observation: Two disjoint permutations are commutative. For e.g $\ (1357)\in S_8$ and $\ (2468)\in S_8$.
Formulate and prove(if possible) a Theorem generalizing your observation about commutative permutations.
Attempt of a solution:
Theorem: Two disjoint permutations commute
Proof: I am  unable to prove the theorem. I can only show examples. Can anyone prove this theorem without examples?
Comment: I think it would be a bit more standard to say "commuting permutations".  An operation may be commutative or not; a particular pair of members of its domain may commute with each other or not.
Comment: Write down two disjoint cycles as $(a_1 \ldots a_n)$, $(b_1 \ldots b_m)$, and show that they commute
Comment: How about using matrices to represents the permutation, (Defined on $S_8$) , with zero submatrices ,and showing these commute under multiplication?
Comment: I tried writing out the cycles,as both permutations and matrices but I'm not finding a logical way to show they actually commute
Answer: Hint: Look at some $a\in[n]$ and denote the permutation as $$\pi=c_{1}c_{2}$$
where $c_{i}$ are two disjoint circles.
Consider the following cases: $$c_{1}(a)\neq a,c_{2}(a)\neq a,c_{1}(a)=c_{2}(a)=a$$
Do you see why the cases are disjoint and it must be the case that
exactly one of them holds ?
Can you continue from here ?
Comment: @user159519 - Please try a little bit more and write down what you tried so I can better direct you. I think it will help more then writing down the answer..
Comment: @user159519 - I am working with $G=S_n$
Comment: No I can't, but thanks for sharing anyway.
Comment: Ok I will work on it , but I don't understand why u said a must be n element of [n]
Answer: You can write the permutations as matrices ("acting" on the vector (1 2...8)) , and show that the two commute when multiplied; not too hard, since you will have blocks of identity submatrices

Commutative permutations


Question about Validation Set for hyperparameter tuning

Question: Okay, I'm still a bit confused as to this Training/Validation/Test Set split. I might be wrong here, but from what I understand, the model is first applied to the Training set, to "learn" from it and figure out the parameters to be used. Then comes the Validation set, and this is where I'm confused. I've read that the validation set is used for Hyperparameter tuning, to figure out which value of hyperparameter would be appropriate to use in the model. (After which, you can then apply and assess your final model on the test set.) Now let's suppose the model I'm applying is a Lasso or Logistic Regression, and the hyperparameter I'm tuning is Lambda. Or the model could even be, say, a Polynomial SVM, and the hyperparameters I'm adjusting is Degree and C.
My question is, as I'm tuning/adjusting these hyperparameters, wouldn't the value of my parameters change in the process? For example, for a Lasso Regression, the lambda is used as a weight to restrict the coefficients from getting too large. Therefore as I adjust lambda, the coefficient values change. Therefore what was the point of initially fitting the model to the training set in the first place?
Answer: In this case it's going Validation $\rightarrow$ Training $\rightarrow$ Testing. The idea being:
1) first you find which parameters best fit your (first part) of the data.
2) Then you train your model on your (second part) of your data. While you are training you directly check whether these parameters are good, whether they return a good model. You can try different parameters which will return different results. You would then choose the parameters which return the best model results (on this part of the data).
3) At last you test your model on your (last part) of your data.


Serial mediation with 3 mediators and 2 predictors [lavaan]

Question: I have constructed a structural equation model in R using lavaan, with 2 exogenous predictor variables, 3 mediators, and 1 endogenous response variable:

I tried to label it as conventionally as possible but apologies if I didn't do a good job. I am now trying to calculate indirect effects, using past answers on how to set it up in lavaan (here), and how to incorporate more than one mediator (here and here). But I am unsure on how to go about it with two predictor values (X1 and X2), as this would count some indirect pathways twice, right? That doesn't seem statistically sound to me but I don't know how to go about it.
Here is what I've written so far, assuming just one X1 value and one indirect path of interest via M3:
<code>model1 <- '
  M1 ~ a1*X1 + e1*M2
  M2 ~ a2*X1
  M3 ~ a3*X1 + b1*M1 + b3*M2
  Y ~  c1*X1 + b2*M1 + b4*M2 + d1*M3
  ind_eff := a3 * d1
  tot_eff := c1 + a1*b1*d1 + a1*b2+ a2*e1*b1*d1 + a2*e1*b2 + a2*b3*d1 + a2*b4 + a3*d1
'
</code>
Have I written this correctly so far (particularly the total effect calculation)? Is it possible to combine this with a model for X2 together into just one model, even if it means some pathways like b1 will be used twice?
Answer: Yes, you can combine them.
As far as I can see, this looks correct.
A couple of suggestions though:
Do you intend to have a path from M2 to M1? If so, at a minimum I would rename them M1 and M2, and it would also be much clearer to make the position of M2 higher than M1, to show it comes first in the causal order.
Comment: Great, thank you very much! Yes the relationship does go from M2 to M1, I organized the model this way before adding that e1 pathway, but you're right it doesn't flow anymore so I will switch them. I appreciate your help!


Cannot read from Boost::mapped_file: Data buffer is NULL

Question: I'm trying to read from a mapped file in chunks (hence the buffer_size and file_offset arguments) but the buffer is always NULL. The file is opened successfully. My code is the following:
#include <boost/iostreams/device/mapped_file.hpp>
#include <iostream>
#include <boost/filesystem.hpp>

namespace filesystem = boost::filesystem;

using std::ios;
using std::ifstream;

char *read_mapped_bytes(const char *file_path, boost::iostreams::mapped_file mapped_file,
                        unsigned long long int buffer_size, unsigned long long int file_offset) {
    const filesystem::path memory_dump_file{file_path};
    mapped_file.open(memory_dump_file, boost::iostreams::mapped_file::readonly, buffer_size,
                     static_cast<boost::iostreams::stream_offset>(file_offset));
    if (mapped_file.is_open()) {
        auto read_buffer = mapped_file.data();
        if (read_buffer == nullptr) {
            printf("The mapped file buffer was NULL");
            exit(-1);
        }
        return read_buffer;
    } else {
        printf("Failed to open mapped file");
        exit(-1);
    }
}

int main(int argc, char *argv[]) {
    boost::iostreams::mapped_file mapped_file;
    auto buffer = read_mapped_bytes((const char *) "/mnt/d/Cpp/MyProject/myFile.bin", mapped_file,
                                    (unsigned long long int) 1000, (unsigned long long int) 0);

    exit(EXIT_SUCCESS);
}

Note that the file path is correct since otherwise it would cause an exception:
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::ios_base::failure[abi:cxx11]> >'
  what():  failed opening file: No such file or directory: iostream error

The code has been ran on Ubuntu 18.04.01 LTS using WSL (Windows Subsystem for Linux).
The strace (strace -ff -o test.txt ./BoostTesting) of the code execution is the following:
execve("./BoostTesting", ["./BoostTesting"], 0x7fffc2c75168 /* 17 vars */) = 0
brk(NULL)                               = 0x7fffc5980000
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=48784, ...}) = 0
mmap(NULL, 48784, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f4759dab000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libboost_iostreams.so.1.65.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\300\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=105032, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4759da0000
mmap(NULL, 2200080, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f47597e0000
mprotect(0x7f47597f8000, 2097152, PROT_NONE) = 0
mmap(0x7f47599f8000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x18000) = 0x7f47599f8000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libboost_system.so.1.65.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000\30\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=18544, ...}) = 0
mmap(NULL, 2113856, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f47595d0000
mprotect(0x7f47595d4000, 2093056, PROT_NONE) = 0
mmap(0x7f47597d3000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x3000) = 0x7f47597d3000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/usr/lib/x86_64-linux-gnu/libstdc++.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\360\303\10\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=1594832, ...}) = 0
mmap(NULL, 3702816, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4759240000
mprotect(0x7f47593b9000, 2097152, PROT_NONE) = 0
mmap(0x7f47595b9000, 49152, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x179000) = 0x7f47595b9000
mmap(0x7f47595c5000, 12320, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f47595c5000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libgcc_s.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300*\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=96616, ...}) = 0
mmap(NULL, 2192432, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4759020000
mprotect(0x7f4759037000, 2093056, PROT_NONE) = 0
mmap(0x7f4759236000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x16000) = 0x7f4759236000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\260\34\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=2030544, ...}) = 0
mmap(NULL, 4131552, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4758c20000
mprotect(0x7f4758e07000, 2097152, PROT_NONE) = 0
mmap(0x7f4759007000, 24576, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1e7000) = 0x7f4759007000
mmap(0x7f475900d000, 15072, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f475900d000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libz.so.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\220\37\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=116960, ...}) = 0
mmap(NULL, 2212016, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4758a00000
mprotect(0x7f4758a1c000, 2093056, PROT_NONE) = 0
mmap(0x7f4758c1b000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1b000) = 0x7f4758c1b000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libbz2.so.1.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0`\23\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=66800, ...}) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4759d90000
mmap(NULL, 2161864, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f47587f0000
mprotect(0x7f47587ff000, 2093056, PROT_NONE) = 0
mmap(0x7f47589fe000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xe000) = 0x7f47589fe000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libpthread.so.0", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000b\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=144976, ...}) = 0
mmap(NULL, 2221184, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f47585d0000
mprotect(0x7f47585ea000, 2093056, PROT_NONE) = 0
mmap(0x7f47587e9000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19000) = 0x7f47587e9000
mmap(0x7f47587eb000, 13440, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f47587eb000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libm.so.6", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\272\0\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0644, st_size=1700792, ...}) = 0
mmap(NULL, 3789144, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f4758230000
mprotect(0x7f47583cd000, 2093056, PROT_NONE) = 0
mmap(0x7f47585cc000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x19c000) = 0x7f47585cc000
close(3)                                = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4759d80000
arch_prctl(ARCH_SET_FS, 0x7f4759d811c0) = 0
mprotect(0x7f4759007000, 16384, PROT_READ) = 0
mprotect(0x7f47585cc000, 4096, PROT_READ) = 0
mprotect(0x7f47587e9000, 4096, PROT_READ) = 0
mprotect(0x7f47589fe000, 4096, PROT_READ) = 0
mprotect(0x7f4758c1b000, 4096, PROT_READ) = 0
mprotect(0x7f4759236000, 4096, PROT_READ) = 0
mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f4759d70000
mprotect(0x7f47595b9000, 40960, PROT_READ) = 0
mprotect(0x7f47597d3000, 4096, PROT_READ) = 0
mprotect(0x7f47599f8000, 4096, PROT_READ) = 0
mprotect(0x7f475a008000, 4096, PROT_READ) = 0
mprotect(0x7f4759c27000, 4096, PROT_READ) = 0
munmap(0x7f4759dab000, 48784)           = 0
set_tid_address(0x7f4759d81490)         = 1491
set_robust_list(0x7f4759d814a0, 24)     = 0
rt_sigaction(SIGRTMIN, {sa_handler=0x7f47585d5cb0, sa_mask=[], sa_flags=SA_RESTORER|SA_SIGINFO, sa_restorer=0x7f47585e2890}, NULL, 8) = 0
rt_sigaction(SIGRT_1, {sa_handler=0x7f47585d5d50, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_SIGINFO, sa_restorer=0x7f47585e2890}, NULL, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [RTMIN RT_1], NULL, 8) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=8192*1024}) = 0
brk(NULL)                               = 0x7fffc5980000
brk(0x7fffc59a1000)                     = 0x7fffc59a1000
futex(0x7f47595c607c, FUTEX_WAKE_PRIVATE, 2147483647) = 0
futex(0x7f47595c6088, FUTEX_WAKE_PRIVATE, 2147483647) = 0
openat(AT_FDCWD, "/mnt/d/Cpp/MyProject/myFile.bin", O_RDONLY) = 3
mmap(NULL, 1000, PROT_READ, MAP_SHARED, 3, 0) = 0x7f4759db6000
fstat(1, {st_mode=S_IFCHR|0660, st_rdev=makedev(4, 1), ...}) = 0
ioctl(1, TCGETS, {B38400 opost isig icanon echo ...}) = 0
write(1, "The mapped file buffer was NULL", 31) = 31
exit_group(-1)                          = ?
+++ exited with 255 +++

Note that when compiling for Windows the same problem arises so it's most likely a bug in my code or Boost.
Comment: Can you provide the `strace` log file from WSL? Command `strace -ff -o test.txt your_command`.
Comment: @Biswapriyo: Thank you, good idea. I added the relevant strace results to the question
Comment: Short compilable example and full `strace` added now
Answer: It turns out I passed bad parameters. Also, the offset must be a multiple of the page size. Here is the platform independent example source code demonstrating iteration over a file in chunks using mapped_file:
#include <boost/iostreams/device/mapped_file.hpp>
#include <iostream>
#include <boost/filesystem.hpp>

#ifdef _WIN32

#include <sysinfoapi.h>

#endif

namespace filesystem = boost::filesystem;
namespace io_streams = boost::iostreams;

using std::ios;
using std::ifstream;

unsigned long long int get_page_size() {
#ifdef _WIN32
    SYSTEM_INFO sysInfo;
    GetSystemInfo(&sysInfo);
    return (unsigned long long int) sysInfo.dwPageSize;
#else
    return (unsigned long long int) sysconf(_SC_PAGE_SIZE);
#endif
}

unsigned long long int get_file_size(const char *file_path) {
    filesystem::path file{file_path};
    auto generic_path = file.generic_path();
    return filesystem::file_size(generic_path);
}

std::vector<char> read_bytes(const char *file_path,
                             unsigned long long int offset,
                             unsigned long long int length) {
    io_streams::mapped_file_params parameters;
    parameters.path = file_path;
    parameters.length = static_cast<size_t>(length);
    parameters.flags = io_streams::mapped_file::mapmode::readonly;
    parameters.offset = static_cast<boost::iostreams::stream_offset>(offset);

    boost::iostreams::mapped_file_source file;

    file.open(parameters);

    if (file.is_open()) {
        auto buffer = file.data();
        std::vector<char> vector_buffer;
        for (auto buffer_index = 0; buffer_index < length; buffer_index++) {
            auto byte = buffer[buffer_index];
            vector_buffer.push_back(byte);
        }

        return vector_buffer;
    } else {
        printf("Failed to open file\n");
        exit(EXIT_FAILURE);
    }
}

int main(int argc, char *argv[]) {
#ifdef _WIN32
    auto file_path = (const char *) R"(D:\Cpp\BoostTesting\test.bin)";
#else
    auto file_path = (const char *) "/mnt/d/Cpp/BoostTesting/test.bin";
#endif

    unsigned long long int offset = 0;
    unsigned long long int page_size = get_page_size();
    auto buffer_size = page_size;
    auto total_length = get_file_size(file_path);

    while (offset < total_length) {
        auto remaining_bytes = total_length - offset;
        if (buffer_size > remaining_bytes) {
            buffer_size = remaining_bytes;
        }

        auto buffer = read_bytes(file_path, offset, buffer_size);
        for (auto byte : buffer) {
            printf("%i ", byte);
        }

        offset += buffer_size;
    }

    return EXIT_SUCCESS;
}


Can I get a ActiveRecord::Connection instance from a ActiveRecord::StatementInvalid exception?

Question: I'm performing a complex data import task, using multiple PostgreSQL database connections. Under certain conditions I need to perform a rollback on a specific database connection if I encounter a ActiveRecord::StatementInvalid exception.
I cannot seem to get the ActiveRecord::Connection instance from that exception. What I can get is a PG::Connection instance from exception.cause.connection. Unfortunately I need the ActiveRecord instance to properly perform the rollback and keep the internal AR data structures in sync with the database. At the very least I need the name of the last savepoint, if any, to perform a manual rollback.
How can I get a ActiveRecord::Connection instance from a ActiveRecord::StatementInvalid exception?
Comment: Are you using multiple clients or how are you distinguishing between the connections via the client(s)?
Comment: Just one client, but multiple connections on the model classes, i.e. `MyModel.establish_connection` and `MyModel.remove_connection`
Answer: In case someone needs this, I'm posting my solution here.
In essence I'm just brute-forcing it by searching all connections from all pools and checking if the low-level connection matches my connection.
def ar_connection_from_pg_connection(pg_conn)
  ActiveRecord::Base.connection_handler.connection_pools.each do |pool|
    conn = pool.connections.find { |ar| ar.raw_connection == pg_conn }
    return conn if conn
  end
  nil
end


Why isn't there a pager and it doesn't show two records per page?

Question: $query = db_select('node', 'n');
$query->condition('n.type', 'article', '=')
  ->fields('n', array('nid', 'title', 'uid'))
  ->extend('PagerDefault')
  ->limit(2);
$result = $query->execute();

while($record = $result->fetchObject()) {
  echo $record ->title."
";
}

$output .= theme('pager', array('tags' => array()));

I put the above code in a page template file. Why isn't there a pager and it doesn't show two records per page?
Comment: Is [123](http://drupal.stackexchange.com/users/11921/123) a colleague of yours? You both keep asking questions about the same topic. `:)`
Comment: yes.:) we are in the same company, our company using drupal make site
Comment: @kiamlaluno,could you tell me why the above code not have pager and the result is not 2. thank you
Answer: The code is wrong: Once you extend the query object, you need to save it, or it will get lost. The correct code is the following one.
$query = db_select('node', 'n');
$query = $query->condition('n.type', 'article', '=')
  ->fields('n', array('nid', 'title', 'uid'))
  ->extend('PagerDefault')
  ->limit(2);

$result = $query->execute();

foreach ($result as $node) {
  echo $node->title . "
";
}

In my case, the result is the following one.

Appellatio Fere Huic MetuoLudus Vereor

See the note shown on Extenders:

Note that $query is not altered in place. The new object is returned from extend(), and if it is not saved to a variable it will be lost.


filter array by key value argument

Question: I have an array as:
  $data = array(
      array('title'=>'test','rendor'=>0),
      array('title'=>'test2','rendor'=>1)
  );

What I am looking for is using one of the array functions to return the title key value when rendor is 1;
Currently im doing:
    $tmp = array();
    $deadTmp = array();
    foreach($tmpCols as $col){
        if((int)$col['rendor'] !== 0){
            $tmp[] = $col;
        } else {
            $deadTmp[] = $col['title'];
        }
    }

I'm trying to learn the array functions.
Answer: An array_reduce will do:
$titles = array_reduce($data, function (array $titles, array $datum) {
    if ($datum['rendor'] == 1) {
        $titles[] = $datum['title'];
    }
    return $titles;
}, []);

Alternatively:
$titles = array_map(
    function ($datum) { return $datum['title']; },
    array_filter($data, function ($datum) { return $datum['render'] == 1; }
);

filter array by key value argument


How do I get R to Speak/Say The Current Time on MacOS?

Question: I can get R to speak/say phrases using the system command.
For example:
system("say What time is it?")
How do I get it so say the current time?
One would think something like:
system("say the current time is Sys.time()")
would work but it does not.
Comment: does ``system("say the current time is `date +%I`")`` work?
Comment: @Waldi it says 'the current time is 09'

When I type in the date command: `date()`, the output is "Thu Mar 11 21:54:15 2021"
Answer: As explained here, you could use the MacOS date function :
system("say the current time is `date +%H:%M`")

Comment: This works!

Thanks!

Where can I read up on things like +%H and %M and what they mean?
Comment: Great! Thank you!


Android in-app products import from csv file

Question: Can anyone make correct CSV file for importing in-app products?
All my attempts were failed.
First option:
test,published,managed_by_publisher,false,en_US;name;desc,false,US;1990000

The result is: "The first defined currency of Product IDs [test] has to be [ru]." (I am from Russia but it never sets some country settings).
After I set autofill to true:
test,published,managed_by_publisher,false,en_US;name;desc,true,default_price_in_home_currency;1990000

The result is: "There was a problem importing the CSV file."
Even if I export and then import the same file, I got these errors.
And I can't find any information, except android in-app import csv and http://developer.android.com/guide/market/billing/billing_admin.html#billing-bulk-add
Comment: In new version of developer console import work fine.
Answer: Try this:
test,published,managed_by_publisher,false,en_US;name;desc,true,1990000

Answer: this is for colombian values, and spanish latin american
onemarker,published,managed_by_publisher,false,es_419; One Payment; One Payment,true,11900000000

twomarker,published,managed_by_publisher,false,es_419; Two Payment; Two payment,true,23800000000

Answer: You can use my utils to create a valid csv to import in-app products into the Play Market
Good luck!


ParamConverter Attribute null

Question: I have a problem with my param converter.
Those are my routing:
CrmBundle routing.yml:
crm_partner:
    resource: "@PartnerBundle/Resources/config/routing.yml"
    prefix:   /{urlname}

PartnerBundle routing.yml:
crm_partner_dashboard:
    path:     /dashboard
    defaults: { _controller: PartnerBundle:Default:dashboard }

PartnerBundle DefautController:
    /*
     * @ParamConverter("department", options={"mapping":{"urlname":"urlName"}})
     */
    public function dashboardAction(Department $department)
    {
        ...
    }

And this is my error:
Unable to guess how to get a Doctrine instance from the request information for parameter "department".

and this my request attribute....
_controller
         "PartnerBundle\Controller\DefaultController::dashboardAction"
_firewall_context           "security.firewall.map.context.main"
_locale                     "fr"
_route                      "crm_partner_dashboard"
_route_params               "urlname" => "shbyjm-europe-1"
                            "_locale" => "fr"
urlname                     "shbyjm-europe-1"

Do you have an explanation for that?
Best regards
Comment: What was your request URI? `/something/shbyjm-europe-1/dashboard`?
Comment: /crm/shbyjm-europe-1/dashboard
Comment: Are you possibly just missing the placeholder in the URL route itself, something like `department/{urlName}`???
Answer: You need to also add class there. That's your issue. Check my code.
class PostsController extends Controller
{
    /**
     * @param Post $post
     * @ParamConverter("post", class="AppBundle\Entity\Post")
     * @return Response
     */
    public function showAction(Post $post): Response
    {
        if (!isset($post)) {
            throw $this->createNotFoundException('Post with the provided route not found!');
        }
        return $this->render(
            '@App/pages/post.html.twig',
            [
                'post' => $post
            ]
        );
    }
}

So it's throwing you error, that it can't guess, because you doesn't pass the class full namespaced name.
Answer: Thanks for your help.
To make my code successfull, i created my own ParamConverter and now it works perfectly.
I think that the problem was that my param is in the first level of my routing and was not correctly use or find.
Anyway, thanks again

ParamConverter Attribute null


Existe algum comando que faça a função recomeçar?

Question: Sou novo nesta área. Criei este bot para um site de apostas. Gostaria de saber se existe algum comando para que a função volte ao início quando sair verde.
$("input").val("10");
setTimeout(function(){
$(".btn-bet.green").click();
    }, 100);
setTimeout(function(){
$("input").val("11");
$(".btn-bet.green").click();
    }, 25000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 50000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 75000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 100000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 125000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 150000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 175000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 200000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 225000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 250000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 275000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 300000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 325000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 350000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 375000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 400000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 425000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 450000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 475000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 500000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 525000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 550000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 575000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 600000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 625000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 650000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 675000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 700000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 725000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 750000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 775000);
setTimeout(function(){
$("input").val("13");
$(".btn-bet.green").click();
    }, 800000);

Comment: Por favor, não digite textos em caixa alta, isso prejudica a leitura. Edite a pergunta acessando o link [edit] e escreva normalmente. Aproveite e faça o [tour] para aprender o básico do site e leia o guia de [ask]. Para formatar corretamente seu código, utilize o botão `{}` do editor.
Comment: Por que você começa com `$("input").val("10");`, depois tem `$("input").val("11");` e todos os outros são `$("input").val("13");`?
Comment: este script é só um teste na verdade quando acerto o verde a quantidade apostada é multiplicada po 14X
Comment: a razão é 1.1x a cada aposta, mas a questãoo é a seguinte, se der green eu gostaria que voltasse ao inicio da função
Comment: existe alum comando return que eu possa usar por exemplo if green return
Comment: este site é formado por 3 cores, vermelho, verde, e preto, no caso a aposta no verde ganha 14x o valor apostado, com esta função eu coloco o valor que quiser a jogar no verde, porém eu queria um comando que identificasse o verde e voltasse à base novamente, ou seja, na primeira aposta
Answer: Bem, o seu script seria algo equivalente mais ou menos a isso:
Tentativa 1:
function fazApostas() {
    var jaFoi = false;
    $(".btn-bet.green").click(function() {
        jaFoi = true;
        fazApostas();
    });
    var m = 10;
    for (var i = 0; i < 40; i++) {
        setTimeout(function() {
            if (jaFoi) return;
            $("input").val("" + ~~m);
            m *= 1.1;
            $(".btn-bet.green").click();
        }, i * 25000 + 100);
    }
}
fazApostas();

Tentativa 2:
function fazApostas() {
    var m = 10;
    function aumentarAposta() {
        $("input").val("" + ~~m);
        if ($(".btn-bet.green").length === 0) {
            m *= 1.1;
            setTimeout(aumentarAposta, 25000);
        } else {
            $(".btn-bet.green").click(fazApostas);
            $(".btn-bet.green").click();
        }
    }
    setTimeout(aumentarAposta, 100);
}
fazApostas();

O $(".btn-bet.green").click(fazApostas); define o que ele faz quando é clicado. No caso, ele começa tudo de novo.
O m *= 1.1; faz o valor da aposta aumentar a cada vez.
O "" + ~~m serve para converter o m para inteiro e depois para string.
Cada iteração da aumentarAposta chama uma outra iteração 25 segundos depois.
O $(".btn-bet.green").length === 0 verifica se o botão está presente ou não.
Comment: Mas não há possibilidade de usar uma return?
Comment: Teste o seu script no site e não funcionou, teria outra solução usando a mesma função, exem´plo quando sair o green eu uso uma função que identifica ele
Comment: @RamonSilva Acho que um `return` não vai fazer o que você quer. A função `fazApostas()` apenas cria um monte de eventos a serem executados no futuro e morre imediatamente. Qualquer coisa que ela retornasse não seria lá muito útil. Você até poderia usar um mecanismo de promises/callback, mas provavelmente isso não seria o que você quer.
Comment: @RamonSilva Talvez você queira usar isso: https://api.jquery.com/size/
Comment: @RamonSilva O que deu errado no script?
Comment: O site é este https://csgoatse.com/, notem que debaixo da roleta tem os resultados, por ali gostaria de usar o return, por exemplo quando um green surgir no histórico meu script recomeçasse.
Comment: Ele não aposta, simplesmente não aconteceu nada quando joguei ele no console
Comment: @RamonSilva Script editado. Olhe a última linha.
Comment: Agora eu achei amigão, deu mas ele ta apostando varias vezes seguidas eu gostaria de uma aposta a cada 25 segundos
Comment: Se sair green quero que retorne a base 10
Comment: @RamonSilva Resposta editada.
Comment: O essencial mesmo era um javscript que identificasse os green, gostaria que ele iniciasse a apostar após 30 rounds sem sair verde e quando o verde saísse ele esperasse mais 30 rounds para começar a apostar novamente
Comment: Este travou  a pagina pois ele repete inúmeras vezes as apostas
Comment: @RamonSilva Então ainda não sei. Deixei as duas tentativas aí para caso você ou outra pessoa encontre uma outra solução.
Comment: Obrigado pela ajuda mesmo assim


Scatterplot relationship between dependent and independent variable for linear regression analysis

Question: I am performing an univariate linear regression analysis. Before running the model I was always told to get an idea of the relationship between the independent and dependent variable through a scatterplot and look for a pattern.
I always do that but without understand the real consequences on my regression analysis.
What I would like to ask you is to "friendly" explain the purpose of making a scatterplot for the relationship between dependent and independent variable and, more importantly, how my analysis would change if I have a linear, logarithmic, exponential, quadratic, cubic or square root relationship coming out from my scatterplot.
Answer: A scatterplot is not only useful to detect potential non-linear relationships but also outliers/extreme values and possible heteroscedasticity (non-homogeneous variance) both of which may influence the regression results. In cases of a non-linear (e.g., u-shaped) relationship, we would consider modeling this relationship appropriately, for example, by adding a quadratic term to the regression model. Otherwise, the regression model would be misspecified and may lead to incorrect conclusions.
Comment: What about a logarithmic-shaped relationship? What the theory or the standard practice would suggest to do in this case?


Erro "1052 Column in where clause is ambiguous"?

Question: Estou com problemas na realização de um sistema. Estou fazendo um join e o seguinte erro aparece:

Error Number: 1052 Column 'id_doador' in where clause is ambiguous
  SELECT * FROM doacao JOIN doador ON doacao.id_doador =
  'doador.id_doadorWHEREid_doador` = '1'

Segue abaixo o meu método no model.
    public function minhasDoacoes(){
    $id_doador = $this->session->userdata('id_doador');
    $this->db
    ->select('*')
    ->from('doacao')
    ->join('doador', 'doacao.id_doador = doador.id_doador')
    ->where('id_doador', $id_doador);
    return $this->db->get()->result();
}

Comment: Olá tudo bem? Não seria `->where('doador.id_doador', $id_doador);` ?
Answer: No where inclua o nome da tabela (doador.id_doador) igual nesse exemplo:
public function minhasDoacoes(){
    $id_doador = $this->session->userdata('id_doador');
    $this->db
    ->select('*')
    ->from('doacao')
    ->join('doador', 'doacao.id_doador = doador.id_doador')
    ->where('doador.id_doador', $id_doador);
    return $this->db->get()->result();
}

o problema é que ele não sabe qual id_doador está fazendo o where por ter dois campos com o mesmo nome. Nada impede que seja colocado doacao.id_doador nesse caso trazendo o mesmo resultado.


How to make HTTP PUT the inverse of GET?

Question: I would like to tell Apache to treat a HTTP PUT as the inverse of GET. The server should store the file sent with PUT at that place, where a file would be read by a GET request containing the same location.
I found only examples for HTTP PUT, which require CGI or PHP scripts and mod_put seems to be dead. First I would like to avoid scripts: CGI because of Shellshock and PHP because of PHP. And second even if I would try to write some CGI I am not aware of Apache's location-directory mapping in the CGI. I would not know how to map the location from the URL to a path in the file system in that way, that Apache would perform the inverse during a GET.
How to implement a HTTP PUT being exactly the inverse of a GET without any CGI or script engine?
Answer: The apache core has the feature to handle PUT requests, but unfortunately this can only forward the request to some of its handler modules (f.e. PHP or to a cgi script).
To the feature you have, this PUT feature had to be implemented in an apache module, too. The apache modules are the same things as the java servlets or cgi scripts: they get http requests, process them, and give back the results.
Unfortunately, the apache modul with made possibly a file upload function by PUT requests, simply wasn't developed. I must mention: even you could do that, although the apache interface isn't the most beautiful I've seen, it is possible and weren't a really big work. But it wasn't done. Or the mod_put module could be re-animated. I could do this in a week, but nobody pays me to do that, or I don't have a free week for the job. Every programmer of the world said the same, if you could ask them.
There are a lot of tools/modules with very similar features in arbitrary, especially WebDAV which were very interesting to you. With them you can reach these, although they are working not by PUT, but with multiparted POST queries. And WebDAV is an overkill because it uses hard (and mostly unneeded) xml processing even for simple tasks.
Comment: WebDAV is overkill for a simple PUT and it bloats the server with all the XML processing necessary for WebDAV.
Comment: @ceving Yest, it is right. If I looked for the best solution, I revived mod_put.


Bayesian approach to classification task

Question: Сame across this equation for training in classification task
$$P(\theta | X_{tr},Y_{tr}) = \frac{P(Y_{tr}|X_{tr},\theta)P(\theta)}{P(Y_{tr}|X_{tr})}$$
Can't understand how we came to it from this(Bayes theorem):
$$P(\theta | X_{tr},Y_{tr}) = \frac{P(X_{tr},Y_{tr}|\theta)P(\theta)}{P(X_{tr},Y_{tr})}$$
(Do I have to use chain or sum rule?)
And more questions about prediction equation:
$$P(Y_{ts}|X_{ts},X_{tr},Y_{tr})= \int{P(Y_{ts}|X_{ts},\theta)P(\theta | X_{tr} ,Y_{tr})d\theta}$$
what's the theory behind it? and what the whole training process looks like?
thanks!
Answer: You need chain rule to arrive at first equation from second since $P(X_{tr}, Y_{tr})=P(Y_{tr}|X_{tr})P(X_{tr})$ and $P(X_{tr}, Y_{tr}|\theta)=P(Y_{tr}|X_{tr},\theta)P(X_{tr}|\theta)$, and additionally note that obviously in machine learning any particular event $X_{tr}$ as training feature input doesn't depend on your classifier model parameter $\theta$ you want to estimate, thus you can claim the first equation. To further elaborate my second equality above using the reference added, we have $$P(X_{tr}, Y_{tr}, \theta)=P(\theta)P(X_{tr}|\theta)P(Y_{tr}|X_{tr},\theta)$$ On the other hand, $P(X_{tr}, Y_{tr}, \theta)=P(X_{tr}, Y_{tr}|\theta)P(\theta)$ from the usual conditional probability product rule which is a special case of chain rule and you can simply treat the conjunction of two events $X_{tr}, Y_{tr}$ as a composite event. Then you can arrive at your first equation and clearly see the application of chain rule here.
As for your further question about the prediction equation during testing, it's mainly calculating the marginal probability unconditioned on the nuisance parameter $\theta$ not unlike the usual denominator evidence in standard Bayes theorem.


logits and labels must have the same first dimension, got logits shape [2048,10] and labels shape [32]

Question: I am trying to use the following code to for a CNN to classify images.
essentially, this code takes in a directory with folders and images, and trains a CNN to classify them.
This is my code:
        self.model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same', input_shape=(32, 32, 3)))
        self.model.add(BatchNormalization())
        self.model.add(Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
        self.model.add(BatchNormalization())
        self.model.add(MaxPooling2D((2, 2)))
        self.model.add(Dropout(0.2))
        self.model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
        self.model.add(BatchNormalization())
        self.model.add(Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
        self.model.add(BatchNormalization())
        self.model.add(MaxPooling2D((2, 2)))
        self.model.add(Dropout(0.3))
        self.model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
        self.model.add(BatchNormalization())
        self.model.add(Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_uniform', padding='same'))
        self.model.add(BatchNormalization())
        self.model.add(MaxPooling2D((2, 2)))
        self.model.add(Dropout(0.4))
        self.model.add(Flatten())
        self.model.add(Dense(128, activation='relu', kernel_initializer='he_uniform'))
        self.model.add(BatchNormalization())
        self.model.add(Dropout(0.5))
        self.model.add(Dense(10, activation='softmax'))

    def processModel(self, split, epochs=10):
        datagen = keras.preprocessing.image.ImageDataGenerator(
            rescale=1./255,
            rotation_range=5,
            zoom_range=(0.95, 0.95),
            horizontal_flip=False,
            vertical_flip=False,
            data_format='channels_last',
            validation_split=split,
        )

        train_generator = datagen.flow_from_directory(
            directory=self.path,
            color_mode='rgb',
            class_mode='sparse',
            shuffle=True,
            subset='training',
            seed=7
        )

        test_generator = datagen.flow_from_directory(
            directory=self.path,
            color_mode='rgb',
            class_mode='sparse',
            shuffle=True,
            subset='validation',
            seed=7
        )

        self.model.compile(
            optimizer='adam',
            loss='sparse_categorical_crossentropy',
            metrics=["accuracy"]
        )

        self.model.fit(train_generator, epochs=epochs, verbose=2)
        self.score = self.model.evaluate(test_generator)

I keep getting this error:
logits and labels must have the same first dimension, got logits shape [2048,10] and labels shape [32]

on line:
        self.model.fit(train_generator, epochs=epochs, verbose=2)

does anyone know why this may be happening?
Answer: Without knowing your data and data preparation it is hard to reproduce, but I think the problem could be your loss function
self.model.compile(
    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=["accuracy"]
)

Try using categorical_crossentropy instead sparse_categorical_crossentropy:
self.model.compile(
    optimizer='adam',
    loss='categorical_crossentropy',
    metrics=["accuracy"]
)


Calculate $\lim_{|\lambda| \to +\infty} \int_{\mathbb{R}}f(x)|\sin(\lambda x)|dx $

Question: For every integrable function $f \colon \mathbb{R} \to \mathbb{C}$ I have to calculate the limit
$$\lim_{|\lambda| \to +\infty} \int_{\mathbb{R}}f(x)|\sin(\lambda x)|dx
$$
We know that this limit exists because it is bounded by the integral of $f(x)$. From the lemma of Riemann-Lebesgue, we know that
$$\lim_{|\lambda| \to +\infty} \int_{\mathbb{R}}f(x)\sin(\lambda x)dx = 0$$
So I assume that the limit that I need to calculate is bigger than zero, and is somewhere close to $$\int_{\mathbb{R}}f(x)dx$$
but I have no idea how I can prove this.
Comment: Maybe try starting with the case where $f$ is the characteristic function of an interval, then a step function, then a simple function...?
Comment: Or: try using the Fourier series of $|\sin(\lambda x)|$?
Comment: This blog post of mine may be relevant: https://www.math.tolaso.com.gr/?p=1195
Comment: Very similar to https://math.stackexchange.com/questions/328314/computing-lim-n-rightarrow-infty-int-ab-left-fx-left-sinnx
Comment: See also: https://math.stackexchange.com/questions/2616207/proof-of-fejers-lemma#comment5967393_2616207
Answer: Consider the family of functions
$$\left\{u_{\lambda}=|\sin (\lambda x)|\right\}_{\lambda>0}\subset L^{\infty}(\mathbb{R}) $$
This is a bounded subset of $L^{\infty}(\mathbb{R})$, with
$$\|u_{\lambda}\|_{\infty}\leq 1,\qquad \forall \lambda>0 $$
Our goal is to find a function $u\in L^{\infty}(\mathbb{R})$ so that
$$\lim_{\lambda \to +\infty}\int_{\mathbb{R}}fu_{\lambda}=\int_{\mathbb{R}}fu,\qquad \forall f\in L^1(\mathbb{R})\qquad (*) $$
(if you know some functional analysis, thanks to the Frechet-Riesz representation theorem $u$ represents the weak-star limit of the net $\left\{u_{\lambda}\right\}_{\lambda>0}$ as $\lambda\to +\infty$, which exists because $\left\{u_\lambda\right\}$ is bounded and hence weakly-star compact in $L^{\infty}(\mathbb{R})$ by the Banach-Alaoglu theorem).
Now, if the above limit holds for all $f$ in a dense subspace $D\subset L^1(\mathbb{R})$, then it holds for all $f\in L^1(\mathbb{R})$.  Let me prove this. Let $f_k\to f$, with $f_k\in D$, be an approximating sequence. Then
\begin{align*}\left|\int fu_{\lambda}-\int fu\right|&\leq
\left|\int fu_{\lambda}-\int f_ku_{\lambda}\right|+\left|\int f_ku_{\lambda}-\int f_ku\right|+\left|\int f_ku-\int fu\right|\leq \\
&\leq \|f-f_k\|_1+\left|\int f_ku_{\lambda}-\int f_ku\right|+\|f_k-f\|_1\|u\|_{\infty}
\end{align*}
and all the three summands vanish by assumption.
The dense subspace $D$ we choose is the space of step functions, i.e. of the form
$$f=\sum_{i=1}^{N}\alpha_i\chi_{[a_i,b_i]} $$
where $\alpha_i\in \mathbb{R}$ and $\left\{[a_i,b_i]\right\}_{i=1}^{N}$ is a family of pairwise disjoint bounded intervals. It is not hard to show, using the fact that $\int_{n\pi}^{m\pi}|\sin x|dx=2(m-n)$ for all $m,n\in \mathbb{Z}$ and proceeding by approximation, we have
$$\lim_{\lambda \to +\infty}\int_{\mathbb{R}}\chi_{[a_i,b_i]}u_{\lambda}= \lim_{\lambda\to+\infty}\int_{a_i}^{b_i}|\sin (\lambda x)|dx=\lim_{\lambda\to+\infty}\frac{1}{\lambda}\int_{\lambda a_i}^{\lambda b_i}|\sin x|dx= \frac{2}{\pi}(b_i-a_i) $$
 and therefore
$$\lim_{\lambda\to +\infty}\int_{\mathbb{R}}\chi_{[a_i,b_i]}u_{\lambda}=\int_{\mathbb{R}}\chi_{[a_i,b_i]}u$$ holds true when we choose
$$u=\frac{2}{\pi} $$
By linearity this extends to all step functions, and hence by the above density argument to all integrable functions. Therefore we may substitute $u=\frac{2}{\pi}$ in $(*)$ to obtain
$$\lim_{\lambda \to +\infty}\int_{\mathbb{R}}fu_{\lambda}=\frac{2}{\pi}\int_{\mathbb{R}}f,\qquad \forall f\in L^1(\mathbb{R})$$
EDIT: More in general, the above argument shows that if $g:\mathbb{R}\to \mathbb{R}$ is a bounded periodic function such that its integral mean over a period is $\alpha$, then
$$\lim_{\lambda\to +\infty}\int_{\mathbb{R}}f(x)g(\lambda x)dx=\alpha \int_{\mathbb{R}}f(x)dx,\qquad \forall f\in L^1(\mathbb{R}) $$
Answer: There's a major gap in the accepted answer; at the end we assume without proof that the limit of an infinite sum is the sum of the limits. If that didn't require proof analysis would be easier...
Say $(c_n)$ are the Fourier coefficients of the $2\pi$-periodic function $|\sin(t)|$:$$|\sin(t)|\sim\sum_{-\infty}^\infty c_ne^{int}.$$

Lemma. $\sum|c_n|<\infty$.

Waving of the Hands: the function is Lip${}_1$, so the derivative is in $L^\infty(\Bbb T)\subset L^2(\Bbb T)$. So $\sum n^2|c_n|^2<\infty$, which implies $\sum|c_n|<\infty$ by Cauchy-Schwarz.
Now say $f\in L^1(\Bbb R)$. Up to an irrelvant $2\pi$ somewhere we have $$\int f(t)|\sin(\lambda t)|=c_0\int f+\sum_{n\ne0}c_n\hat f(\lambda n).$$
And $\sum_{n\ne0}c_n\hat f(\lambda n)\to0$ as $\lambda\to\infty$ by Riemann-Lebesgue plus Dominated Convergence (in $\ell_1$).
Answer: Elaborating on the idea of the Fourier series. This idea I have presented it at my blog here.
The Fourier series of  $|\sin x |$ is of the form:
$$|\sin x | = \frac{2}{\pi} + \sum_{n=1}^{\infty} a_n \cos nx$$
Hence,
$$ |\sin \lambda x | = \frac{2}{\pi} + \sum_{n=1}^{\infty} a_n \cos n \lambda x  \tag{1}$$
Thus,
\begin{align*}
\int \limits_{\mathbb{R}} f(x) \left | \sin \lambda x \right | \, \mathrm{d}x &=  \int \limits_{\mathbb{R}} f(x)\left ( \frac{2}{\pi} + \sum_{n=1}^{\infty} a_n \cos n\lambda x \right ) \, \mathrm{d}x\\
 &=\frac{2}{\pi} \int \limits_{\mathbb{R}} f(x) \, \mathrm{d}x + \int \limits_{\mathbb{R}} f(x) \sum_{n=1}^{\infty} a_n \cos n \lambda x \, \mathrm{d}x
\end{align*}
Hence, $\require{cancel}$
\begin{align*}
\lim_{\lambda \rightarrow +\infty}\int \limits_{\mathbb{R}} f(x) \left | \sin \lambda x \right | \, \mathrm{d}x &=\frac{2}{\pi} \int \limits_{\mathbb{R}} f(x) \, \mathrm{d}x + \cancelto{0}{\lim_{\lambda \rightarrow +\infty}\int \limits_{\mathbb{R}} f(x) \sum_{n=1}^{\infty} a_n \cos n \lambda x \, \mathrm{d}x} \\
 & = \frac{2}{\pi} \int \limits_{\mathbb{R}} f(x) \, \mathrm{d}x
\end{align*}
The last limit is $0$ due to the Riemann - Lebesgue Lemma. Conclude!
Comment: The Riemann-Lebesgue step needs justification: you've interchanged the limit and integral with the summation. Why is this permissible?
Answer: This can be analyzed  The approach we follow is based on a simple extension of Fejér's formula

Let $g$ be a bounded measurable $T$-periodic function, $\sigma_n\xrightarrow{n\rightarrow\infty}\infty$, and $\alpha_n$ any sequence in $\mathbb{R}$. For any $\phi\in L_1(\mathbb{R},\lambda)$, where $\lambda$ is Lebesgue's measure,
$$\begin{align}
\lim_{n\rightarrow\infty}\int_{\mathbb{R}} \phi(x)g(\sigma_nx+\alpha_n)\,dx=\Big(\frac{1}{T}\int^T_0 g(x)\,dx\Big)\Big(\int_{\mathbb{R}} \phi(x)\,dx\Big)\tag{1}\label{one}
\end{align}$$

A proof of this result can me found here
In the case of the OP, $g(x)=|\sin x|$ which is $\pi$-periodic and $\int^\pi_0|\sin x|\,dx =2$. Thus, for any $f\in\mathcal{L}_1(\mathbb{R})$
$$
\lim_{\lambda\rightarrow\infty}\int_\mathbb{R} f(x)|\sin \lambda x|\,dx = \frac{2}{\pi}\int_{\mathbb{R}}f(x)\,dx$$


Passing Variables across 2 views/controllers in Rails

Question: I am trying to create a 2 page sign-up form whereby the user first enters their email and password using the UsersController, then directed to a second page for personal information (e.g. name, age) using the DetailsController. The two tables are linked in a one-to-one relationship.
How do I pass the user_id field from the UsersController to the DetailsController so I can keep the relationship?
User.first has been hard coded in the example. But, I want to pass the new user_id into the @detail.user variable.
def create
  @detail = Detail.new(detail_params)
  @detail.user = User.first
  if @detail.save
    flash[:notice] = "Your Details has been successfully saved"
    redirect_to detail_path(@detail)
  else
    render 'new'
  end
end

Answer: One of many ways to implement this is to make Detail as nested resource of User:
# config/routes.rb
resources :users do
  resources :details
end

Then a route for a new detail page will look like this: /users/:user_id/details/new. It will allow you to redirect user to step 2 page from a create action in UsersController:
# app/controllers/users_controller.rb
def create
# ...
if @user.save
  redirect_to new_details_path(user_id: @user.id)
else
  render 'new'
end

In your DetailsController you will able to obtain user_id from params:
# app/controllers/details_controller.rb
def create
  @user = User.find(params[:user_id])
  @detail = @user.detail.build(detail_params)
  # ...

Answer: Use sessions. They are free. Example =>
session[:foo] = 'bar'
and you can access it on the next page like this session[:foo]. If you want to clear the session, just set it to nil.


Unexpected 'a' json o/p while running in postman

Question: <?php
    $connect = mysqli_connect("localhost","root","","test");

    echo "aa";

    if(isset($_GET['emailid'])) {
        echo "aa";
        $emailid = $_GET['emailid'];
        $sql  = "SELECT emailid, phoneno, fname FROM registered WHERE emailid = '$emailid'";

        $query = mysqli_query($connect,$sql);
        $resultfetch = mysqli_fetch_array($query);
        $result = array();

        array_push($result, array(
                              "Emailid" => $resultfetch['emailid'],
                              "Phoneno" => $resultfetch['phoneno'],
                              "first name" => $resultfetch['fname']
        ));

        echo json_encode(array("result"=>$result));
        echo json_last_error_msg();
        mysqli_close($connect);
    }

?>

After running the above script the outer echo statement is been printed but inner echo isn't printed and the o/p in json is also unexpected u.The column name are also right
you can check the images of database and o/p in postman
 o/p
Comment: How could you pass emailid in your header?
Comment: unexpected 'a' or 'u'? post the response
Comment: it shows unexpected 'a' but when i place cursor on the cross mark it popups unexpected 'u'
Comment: what url you are using ?
Comment: it most likely is a problem with your table structure, are you sure the structure of the table is good? post the api(url link) for that webservice api
Comment: is your api response of type json?
Comment: i'm running it on localhost but haven't specified encoding type to json
Comment: i have uploaded the image as  link in o/p you can check it @ScottS
Comment: try moving this line:  $emailid = $_GET['emailid']; above if and change this line "if(isset($_GET['emailid']))" to if(isset($emailid))
Comment: change your url to: "http://localhost/1/b1.php?emailid=aa" or replace aa for the id you want to get info from.
Comment: Moving the line above will generate a notice undefined index and have also executed the link error is same @ScottS
Comment: It is an s/w that allows you to check your php files and other related stuff @Nawin
Comment: Your sentence doesn't make any sense.  Please use proper english. "Moving the line above will generate a notice undefined index and have also executed the link error is same"?????
Answer: Try like this and change your method get to post, pass your email in body
if(isset($_POST['emailid'])) {
        echo "aa";
        $emailid = $_POST['emailid'];
        $sql  = "SELECT emailid, phoneno, fname FROM registered WHERE emailid = '$emailid'";

        $query = mysqli_query($connect,$sql);
        $resultfetch = mysqli_fetch_array($query);
        $result = array();

        array_push($result, array(
                              "Emailid" => $resultfetch['emailid'],
                              "Phoneno" => $resultfetch['phoneno'],
                              "first name" => $resultfetch['fname']
        ));

        echo json_encode(array("result"=>$result));
        echo json_last_error_msg();
        mysqli_close($connect);
    }

Comment: Body option is been disabled for get method
Comment: Yes you should change in your left side dropdown for post method... If you want to in Get method means you should pass your value in Url only Not in header
Comment: Ohh sorry man haven't read your code throughly you replaced GET with post and yes after changing it provides the desired o/p thanx budyyy :)
Comment: But i want to get data from user so its appropriate to use POST ??
Comment: Did you get your result?
Comment: yup i have also  done that answered to you answer :)


Finding the total derivative of $g(x)=(x,f(x))$ step by step.

Question: Suppose that $f:\mathbb R^n\to \mathbb R^k$ is differentiable. Define $g:\mathbb R^n\to \mathbb R^{n+k}$ as $g(x)=(x, f(x))$. Then, the total derivative of $f$  at some $x\in \mathbb R^n$ is to be determined.
I think the question above needs a little explanation. $x\in \mathbb R^n$ is an $n-$ tuple and therefore looks like $(x_1,x_2,...,x_n)$ and similarly for $f(x)$ so $g(x)=((x_1,...,x_n), (f_1(x),...,f_k(x))\in \mathbb R^n\times \mathbb R^k.$ But here in order that the question makes sense, let's identify $\mathbb R^n\times \mathbb R^k$ by $\mathbb R^{n+k}$ (I actually don't understand why it can be done. I do understand that $\mathbb R^{n+k}$ is isomorphic to $\mathbb R^n\times \mathbb R^k$ as vector spaces but that doesn't seem the reason for such replacement. One possible reason could be: norms on a finite dimensional vector spaces are equivalent. But here we have two isomorphic vector spaces so I don't understand how to see the validity of this step. So let me take such replacement for granted for now.).
Let $f(x)=(f_1(x),f_2(x),...,f_k(x))$, where $f_i:\mathbb R^n\to \mathbb R$. $g$ is differentiable as each of its coordinate map is differentiable.
So $g(x)=(x_1,x_2,...,x_n, f_1(x),f_2(x),...f_k(x))$.
Writing Jacobian matrix for $g$ at $x$ (it will be an $(n+k)\times n$ matrix) gives:
$\begin{bmatrix}1 &0&0&\cdots &0\\
0&1&0&\cdots&0\\
\cdots&\cdots&\cdots & &\cdots\\
\cdots&\cdots&\cdots & &\cdots\\
 0&0&0&\cdots &1\\
\frac{\partial f_1(x)}{\partial x_1}&\frac{\partial f_1(x)}{\partial x_2}&\cdots&\cdots& \frac{\partial f_1(x)}{\partial x_n}\\
\frac{\partial f_2(x)}{\partial x_1}&\frac{\partial f_2(x)}{\partial x_2}&\cdots&\cdots& \frac{\partial f_2(x)}{\partial x_n}\\
\cdots&\cdots&\cdots & &\cdots\\
\cdots&\cdots&\cdots & &\cdots\\
\frac{\partial f_k(x)}{\partial x_1}&\frac{\partial f_k(x)}{\partial x_2}&\cdots&\cdots& \frac{\partial f_k(x)}{\partial x_n}\end{bmatrix}$
But this does not look correct as it has $f_i$'s, which I created for convenience and they were not given in the original problem. So how do I get the correct total derivative? I'm having difficulty understanding this subject.
Comment: Avoid coordinates as much as possible. We have $$\begin{align}d_pg(v)&=\frac{d}{dt}\bigg|_{t=0}
g(p+tv)\\
&=\frac{d}{dt}\bigg|_{t=0}\bigl(p+tv,f(p+tv)\bigr)\\
&=\bigl(p,d_pf(v)\bigr).\end{align}  $$
Comment: "But this does not look correct as it has $f_i$'s, which I created for convenience and they were not given in the original problem". You also created the symbols $x_1$, $x_2$, and so on for your convenience. You know such $f_i$'s exist due to the existence of $f$. If you want you can make that argument explicitly, but doing that is not much different from just using the coordinates for $x$.
Comment: The bottom part of that matrix is just the Jacobian of $f$. If you have ever written that down entry by entry before, you will have called upon the $f_i,\,i=1,\dotsc,k$, which are the components of $f$. These are *determined* by $f$ (and, conversely, the entirety of them determines $f$), so them not being "given" is simply untrue. They're just not explicitly mentioned.
Comment: @Thorgott: Yes, I had noted that. The matrix I wrote is simply $\begin{bmatrix}I_{n\times n} \\ Df(x)\end{bmatrix}$. The confusion arose as I somehow wrongly thought that the correct answer should look like $\begin{bmatrix}I_{n\times n} & O\\ O& Df(x)\end{bmatrix}$, which is not correct.
Answer: Let $f=(f_1,\ldots,f_k)$. First of all your function $g$ actually looks like this:
$$g=(g_1,\ldots,g_{n+k}):\mathbb{R}^n \to \mathbb{R}^{n+k}, (x_1,\ldots,x_n) \mapsto (x_1,\ldots,x_n,f_1(x_1,\ldots,x_n),\ldots,f_k(x_1,\ldots,x_n)).$$
What the source you have your question from means by "identifiy" $\mathbb{R}^n\times\mathbb{R}^{k}$ with $\mathbb{R}^{n+k}$ is the following. Given the idenitity $id$ on $\mathbb{R}^n$ and $f:\mathbb{R}^n \to \mathbb{R}^k$ we get a canonical map $id\times f:\mathbb{R}^n \to \mathbb{R}^{n}\times \mathbb{R}^k, x\mapsto (x,f(x))$ now we use the identification and hence get our map $g$.
By the definition of the Jacobi matrix we have for $x\in \mathbb{R}^n$:$$Dg(x)=\left(\frac{\partial}{\partial x_j} g_i(x)\right)_{i,j}$$ where $i$ ranges from $1$ to $n+k$ and $j$ ranges from $1$ to $n$. Let $x=(x_1,\ldots,x_n)\in \mathbb{R}^n$. By definition $g_i(x)=x_i, \forall i=1,\ldots,n$ so $\partial_j g_i(x)=\delta_{i,j}$ for all $i,j=1,\ldots,n$ and for $\ell=1,\ldots,k$ and $j=1,\ldots,n$ we have $\partial_j g_{n+\ell}(x)= \partial_j f_{\ell}(x)$. So your formula looks fine.
Answer: By total derivative definition, we have
\begin{align}
g(x+h )
=&
\Big(x+h, f(x+h)\Big)
\\
=&
\Big(x+h,f(x)+Df(x)h+\|h\|\cdot \rho(h)\Big)
\\
=&
\Big( x , f(x) \Big) + \Big(h,Df(x)h \Big) + \|h\|\Big(0,\rho(h)\Big)
\\
=&
g(x)  + \Big[I \,| \, Df(x)\Big]\cdot h + \|h\|\Big(0,\rho(h)\Big)
\end{align}
Here $I$ is the identity matrix of order $n\times n$ and $Df(x)$ is the total derivative of $f$.


can anybody help in finding number of ways the letters of the word 'PERMUTATION' be arranged so that consonants are in alphabetical order?

Question: I had tried the question and got the answer 11!/(6!2!) but the answer given is 11!/6! if any body can explain that why 2! is not in the answer or the answer is wrong.
Comment: Perhaps the consecutive double T's has to do something.
Comment: Welcome to Math.SE.  Please review [ask], and in particular try to make the body of your Question as self-contained as possible (not relying on the title alone to state a problem).  Note that if the *consonants* must be in alphabetic order, the only difficulty is with placement of *vowels*.   The vowels are all distinct in this setting.
Answer: Well, what are the consonants? They are P, R, M, T, T, and N. They must be arranged in the following order: M, N, P, R, T, and T (notice the Ts are indistinct). We can now use distributions to determine the vowel-consonant order. Treating the consonants as indistinct (there's only one way to place them), we have $\dbinom{11}{6}$ ways to arrange. There are now $5!$ ways to arrange the vowels, as there are no constraints on vowels. The answer is $\boxed{\dbinom{11}{6} \times 5!}.$
Answer: So you have MNPRTT, and you need to "stick" each one of AEIOU at any location.
In other words, all you need to do is:

Choose $5$ out of the $11$ available slots
Permute the $5$ letters AEIOU in any order

The total number of ways to do it is:
$$\binom{11}{5}\cdot5!=\frac{11!}{6!}$$
Comment: Well, second thought, the statement "we have $\binom{11}{6}$ to arrange" is wrong in the context of this question, even though it "somehow" gets the correct answer, as $\binom{11}{6}=\binom{11}{5}$.
Comment: @Nikita: You can only accept one answer, and I've noticed that the other answer (which you have originally accepted) was there first, and is also quite similar to my answer. So unless you think that my answer is favorable over the other one, you should stick to your previous choice :)
Comment: "$\binom{11}6$ ways to arrange" has been taken by Jiang for first fixing the *consonants*.
Comment: @trueblueanil: Yep, I get it... So it's more or less the same answer, and I'll leave it for OP to decide... Thanks.