Magento2 : Random Facts or Funny Facts

Question: Magento2 already release its stable release. Can anyone share the Random facts Or Funny facts of Magento2 ? Same like this link Or this link.
Now its time to Magento2 :)
Comment: The answers in this should probably form a community wiki post
Answer: /vendor/magento/magento2-base/lib/web/requirejs/text.js
Line 311 - //Why Java, why is this so awkward?
Line 369 - //XPCOM, you so crazy
Line 56  - //Would love to dump the ActiveX crap in here. Need IE 6 to die first.
Line 353 - //Avert your gaze!
Comment: This is not really M2 code. It's require js code, not written by the M2 core team.
Answer: This one takes the cake in my opinion: Magento\Backend\Block\Widget\Breadcrumbs::_beforeToHtml()
protected function _beforeToHtml()
{
    // TODO - Moved to Beta 2, no breadcrumbs displaying in Beta 1
    // $this->assign('links', $this->_links);
    return parent::_beforeToHtml();
}

What's so funny about it is that the same thing exists in M1. So this is not about the beta 2 of M2. It's about beta2 of M1.
The other things below are just bonus:
In lib/web/css/docs/index.html
background-color: @nav__background-color; // ToDo UI: todo inline comment

In Magento/Catalog/Controller/Adminhtml/Product/Action/Attribute/Save.php
 // TODO why use ObjectManager?

I wonder the same. Why?
Comment: It means that they are doing only code conversion for M2 for specific module :)
Answer: src/vendor/magento/module-customer/view/frontend/web/js/zxcvbn.js
line 343
fuckyou, fuckme, fucker, fuckoff, fucking, fuckit, fuckyou2, fucked, fuckyou1, fuck_inside, fuckface, assfuck, fuckers, fuckthis

Seriously how many of us keep such passwords :)
Answer: In src/vendor/magento/magento2-base/lib/web/prototype/window.js there is a comment which says // Fucking IE select issue on line 1314.
Comment: this is not really from Magento. It's prototype.
Answer: src/vendor/magento/magento2-base/lib/web/prototype/window.js on line 1770
// Why?? Ask IE

and on line 860
// Check for this stupid IE that sends dumb events
Answer: setup/src/Magento/Setup/Module/Di/Code/Reader/FileScanner.php has 29 goto-statements. And that's not the only place in Magento that has goto-statements.
Answer: I quite like href="#TODO" in magento/theme-frontend-blank/web/js/navigation-menu.js. They're trying to send the user 'todo' it.
Answer: /vendor/magento/module-backup/Model/ResourceModel/Helper.php line 112
//TODO fix me
$sql = 'SHOW CREATE TABLE ' . $quotedTableName;
$data = $this->getConnection()->fetchRow($sql);
$script .= isset($data['Create Table']) ? $data['Create Table'] . ";\n" : '';

or this
/vendor/magento/module-configurable-import-export/Model/Import/Product/Type/Configurable.php line 651
// @todo understand why do we need this condition
if ($this->_getSuperAttributeId($linkId, $attrParams['id'])) {
    $productSuperAttrId = $this->_getSuperAttributeId($linkId, $attrParams['id']);
} elseif (isset($this->_superAttributesData['attributes'][$linkId][$attrParams['id']])) {
    $attributes = $this->_superAttributesData['attributes'];
    $productSuperAttrId = $attributes[$linkId][$attrParams['id']]['product_super_attribute_id'];
    $this->_collectSuperDataLabels($data, $productSuperAttrId, $linkId, $variationLabels);
} else {
    $productSuperAttrId = $this->_getNextAttrId();
    $this->_collectSuperDataLabels($data, $productSuperAttrId, $linkId, $variationLabels);
}

It was a little shock after hours of investigation...

Magento2 : Random Facts or Funny Facts


Google's UrlShortener, where can I find the list

Question: I shortened many URLs using Rest API, with 'API Project' which is one of my Google APIs projects.
However, I cannot find the list of shortened URLs in the page of https://goo.gl/.
(Here I can only find the list of shortened URLs I have created here on the webpage.)
I don't know why, but, I guess this is because I had several projects.
Where Can I find the list of shortened Urls with specific API-Key and project.
Plus, I cannot find it on the apis-explorer
(http://developers.google.com/apis-explorer/?hl=ko#p/urlshortener/v1/)
Answer: API keys are used to access public APIs.  Google URL shortener is in fact a public API you don't need to be logged in in order to use it.
If you are not logged in then Google has no idea who it was that shortened said url and now way of keeping the stats for you that you are looking for.
If you want to keep the stats on your own page then you should change your application to use OAUTH2 authenticate your application then use that to shorten the URLs.   The stats will then apear.
Comment: Yes I used Oauth2 with my key. I will retrieve the list by using this Oauth2 key and tell you.
Comment: I dont think you understand.   You must create your Shortened URLs using the access token created by Oauth2.    Using an a public API key will not store the data you are looking for.
Comment: Thank you. It seems like I can shortenUrl with both APIkey and Oauth2 Token.
Comment: I can use the API(urlshortener.url.insert) with API-key only (without Oauth2 Token)
Comment: However, I cannnout use urlshortener.url.list with API-key. I think this is a problem. I cannot list my previous shortened urls. Is it right?
Comment: I should have shortened url using Oauth2 in the first place.. I am gonna lose all of my previous works for 3 months data
Comment: No your not going to loose it because it doesn't exist. Yes you should have used Oauth to begin
Comment: Code snippet that use both API Keys and OAuth2 for Google URL Shortener API you can checkout here for reference http://niralar.com/google-url-shortener-api-integration-nodejs/


Trouble getting LME in R to work with within-groups (repeated Measures) data

Question: I'm having trouble figuring out how to apply the LME function to a set of data. What I have is a list of Stores and their respected customer count, by week, with various external factors for each store: Crime Rate per 100k people, % of people with a college Degree, Level of inequality, and so forth (sample Below.)
Store   Week    CustomerCount   CrimeRate   %collegeDegree  Inequality  Median Income
1       1         200              5        0.25            0.4         25000
1       2         259               5       0.25            0.4         25000
1       3         234              5        0.25            0.44        25000
…
5       1         106               1       0.2         0.43            26000
5       2         96                1       0.2         0.42            26000
5       3         101               2       0.21        0.42            26000

Now, what I'm trying to do is this: I'm trying to run an annual regression of the data to determine if some of the external factors (crime Rate, college rate, inequality level) have an effect on store customer count.
Now, I need to do a repeated measure analysis on this where the repeated measures are Week, and entity of store has the repeated values. I've set up the LME function thusly:
   Lme(CustomerCount ~ CrimeRate + CollegeRate + InequalitlyLevel, random = ???? , data=the.data)
So, my question is: what do I put in for the random variable? Is it "…,random = ~1|week/Store,…" or a variation thereof?
Answer: The problem you have is that the repeated measurements are not independent of each other, but correlated. One way to deal with this is by using random effects.
The first random effect you want to add is a random intercept:
lme(data= the.data, CustomerCount ~ Week + CrimeRate +  CollegeRate + InequalitlyLevel,
               random=~1|Store)

With this model, you assume that all the stores have the same linear time effect, but that the repeated measurements are correlated. It might be that you have to go a step further, being that the stores each have a random time effect. If so, you would have to model:
lme(data= the.data, CustomerCount ~ Week + CrimeRate +  CollegeRate + InequalitlyLevel,
               random=~Week|Store)

You can find out with a likelihood ratio test which model to use.


getelementbyid().click not working

Question: I am making a website that whenever I click and x is equal to a range of numbers, it plays audio, but after i click once when x is between those numbers, i will also play if x is not in the range. Please Help. Here is my code. by the way, i will not use jquery because it does not work on chrome.
    if(x<=1200&&x>=600){
    var n=true;
    };
    if(x<=1200&&x>=600&&n==true){
    document.getElementById('a').onclick = function(){
    audio.play();
    n=false;
        }
    }
    else{n=false}

Comment: What do you mean JQuery doesn't work on Chrome?
Comment: Further to millerbr's comment: jQuery works perfectly well on chrome. It's not necessary, by any means, but it certainly works.
Comment: @AMACB, why does it matter - OP says the audio is playing, even more than needed. :)
Answer: What your code does is that it adds a click event listener on that element, and afterwards the click handler will trigger regardless of your x value because you don't have any checks inside the click handler.
The solution is to do create the click listener only once, and check for the value of x inside it. Something like this:
document.getElementById('a').onclick = function(){
  if(x <= 1200 && x >= 600 && n == true) {
    audio.play();
    n = false;
  }
}

See this (I've replaced audio playing with div highlighting, but it's the same principle):

var r = document.getElementById('r');
var n = true;
var x = 1000;
document.getElementById('a').onclick = function(){
  if(x <= 1200 && x >= 600 && n == true) {
    r.className = 'playing';
    n = false;
    printN();
    setTimeout(function(){
      r.className = '';
    }, 2000);
  }
}

function toggleN(){n = !n;printN()}
function printN(){document.getElementById('n').textContent = 'n is set to ' + n;}
function randomizeX(){x = Math.floor(Math.random() * 1200);printX()}
function printX(){document.getElementById('x').textContent = 'x equals ' + x;}
#r {display: inline-block; width: 50px; height: 50px; background: #ccc; transition: background 1s linear;}
#r.playing {background: green}
<button id="a">Hit me</button>

x equals 1000

n is set to true

<button onclick="toggleN()">Toggle n</button>
<button onclick="randomizeX()">Randomize x</button>
Answer: I'm not quite sure what you're trying to do but here a guest :
if(x<=1200&&x>=600){
    var n=true;
    var clicked = false;
    };
    if(x<=1200&&x>=600&&n==true || clicked==true){
    document.getElementById('a').onclick = function(){
    audio.play();
    n=false;
    clicked = true;
        }
    }
    else{
        n=false;
        clicked = false;
   }

Answer: This should do what you want, if i understood you correctly.
no reason to check x several times + check n, since n is only true if x is between your ranges.
var ab =  document.getElementById('a');
var n = false;

if( x <= 1200 && x >= 600 ){
  n = true; //If x is within range.
} else {
  n = false; //Reset when x goes out of range.
}

ab.onclick = function(){
  if(n){
    audio.play();
  }
}

Note: If a user clicks the button several times,
the audio plays several times.
This can be fixed by adding another variable checking if a user already clicked once,
Since i don't know your setup, i.e. should users be able to click to play the audio several times in a row(when it finish playing once), i can't supply you with a solution for that without knowing.

getelementbyid().click not working


How to pull in dynamic content in hook_filter_FILTER_process?

Question: I'm creating a custom input filter to replace a placeholder html comment with a simple form. However, calling drupal_get_form within a hook_filter_FILTER_process function causes the php memory limit to be exceeded.
Elsewhere I tried to do a node_load within one of these functions and the same thing happened.  I was able to work around that but this indicates it's nothing to do with the form (which is displayed elsewhere on the site anyway, and only contains a couple of fields).
The memory limit is hit whether I actually perform any filtering or just return the unfiltered text.
Any ideas what I'm doing wrong?
Thanks
Answer: Depending on your settings, hook_filter_FILTER_process() will be invoked (albeit in a round-about way) from within the drupal_get_form() and node_load() functions.
So at the moment you're simply creating an infinite loop that PHP cannot escape from (node_load() calls hook_filter_FILTER_process(), which then calls node_load() again), hence the out of memory error.
Comment: Thanks Clive, that makes sense - but when you say 'depending on my settings' which settings do you mean? Is there a way I can get around this? Thanks!
Answer: Ok, solution was simple enough in the end :) I just added a static var to the start of the function to prevent infinite loop
    static $donethisalready;

if (!$donethisalready){
    $donethisalready = true;

    // do whatever
}


How to use tokens in Integer field?

Question: I'd like to add a 'Year of birth' field to the build-in registration form. Therefore, I created the Integer field 'Year of birth' at Administration > Configuration > People > Account settings.
I set the minimum value to 1900 and the maximum value to 2013. But, instead of '2013', I'd like to use the token [site-date-yyyy], so that I don't have to change the maximum value every year. Unfortunately, only integers are accepted. How can I use a token?
Answer: Try the Date module

Install the module
Go to Configuration > Account Settings > Manage Fields tab
Add a date field of type "Date" and widget "Select list"
Hit Save
Deselect every date piece except year
Hit Save field settings.
Check Display on user registration form.
Expand More settings and values
Set starting year and ending year and default value.
Save settings

If you must use a text field, the date field gives you that option as well. You wont be able to set a start or end date, but the module will validate the date is in the proper format.
Comment: That works absolutely perfect! Thank you for your quick answer @Scott Joudry!

How to use tokens in Integer field?


Unable to get pop up in Windows Server 2003 machine

Question: I have a service and a Form application. In that, I am calling the Form Application using my Windows service. I have tested it on Windows 7 and it works fine for that, which means it shows pop up via calling Windows service as expected. But when I run the same thing on Windows Server 2003, it somehow doesn't show my Form Application when I run my Windows service.
I am unable to determine the cause of this here and would appreciate any help or guidance that can spread more light on this.
Comment: Impossible to answer without even knowing at least part of the code.
Comment: Why are you mixing UI and services?
Comment: It is my requirement. Please answer if you know
Comment: Have you looked at the event log to see if there are any error produced, are you catching any exceptions when showing the form and outputting them somewhere to review?
Comment: Actually it works perfect on Windows 7, but when I start it on Windows Server 2003 it doesn't give output/error
Comment: Exactly why you should look for errors. If it works on one platform and not on another, somewhere something is going wrong, generally that can produce an error.
Comment: @Thorsten Dittmar Please see url for complete code: http://www.codeproject.com/Articles/23176/Create-a-system-tray-icon-and-a-dialog-for-a-Windo
Answer: First, check the Task Manager's Processes tab to confirm that your service's executable is indeed running. If it isn't then heed the advice already given and check the Event Log.
Second, ensure that you are logging in to Session 0 (the console session) when you connect to your Windows Server 2003 box. If using RDP, add the "/admin" (or "/console") flag to get to Session 0.


Bandwidth requirement to live broadcast an event on the internet

Question: I am assigned on project to live broadcast an event on the internet which may have 50,000 user s. This will be a broadcast so all users will see same live video. My query is what would be my bandwidth requirement in terms of no of users viewing that stream.

I am little confused & the reason is does every users need a separate stream of bandwidth in broadcast ?
If i would install a server with streaming capability having Bandwidth of 50 Mbps would it be enough considering it is a broadcast.
Do i necessarily need to have Class C IP to run streaming server on rtmp protocol.
Is is possible to achieve this through RTSP. How do services like Google Hangout Air work? What protocol is used in Hangout and Youtube when live broadcasts are done.

I have read some where the broadcast needs only stream of data to transmit to all users compared to unicast which requires separate stream for every connection/user.
Kindly suggest me solution especially mention if you have such practical experiance.
Thanks in Advance
Answer: Normally you'd use multicast for this, but the Internet does not route multicast. So you need a separate stream for each user. You didn't provide a bitrate for your video so we can't tell you what your total bandwidth would be, but let's just say it's 500Kbps. Total bandwidth would be 500Kbps * 50,000 which far exceeds any Internet pipe I know of.
Comment: @Nawaz Talk to a CDN that provides streaming services. Google/YouTube have the infrastructure to do this in-house; you do not.
Comment: What would be your suggestion? Can it be achieved through any other model. What protocol or concepts used in Google Hangout Air or Youtube live broadcasts
Comment: How sites streaming cricket and football matches work. And as far as i know these site don't have such heavy bandwidth connections and they do have such quite good no. of viewers. Appologies if I have asked a wrong question. This is just my curiosity an example will be [link](http://gofirstrow.eu/watch/137666/1/watch-espn.html)
Comment: Can we use Hangout Air or youtube embed for my purpose


Limit of Ratio of Chebyshev Polynomials

Question: I have been trying to compute the limit
$$\lim_{n\to\infty}{{U_n(x)^2}\over{U_{n-1}(x)^2+U_n(x)^2}}$$
where $U_n(x)$ is the $n$-th Chebyshev polynomial of the second kind and $x\ge 1$.
Using software I have been able to compute these limits exist when $x$ a half-integer, but I would like to have an explicit formula of the limit as a function of $x$ (at least for $x$ a half-integer).
Comment: Just to simplify the task, it's easier to first look at just $\dfrac{U_{n-1}}{U_n}$.
Answer: First thing we should note is:
$$\lim_{n\to\infty}\frac{U_n(x)^2}{U_{n-1}(x)^2+U_n(x)^2}=\lim_{n\to\infty}\frac{1}{\left(\dfrac{U_{n-1}}{U_n}\right)^2+1}$$
So, we look at $\displaystyle\lim_{n\to\infty}\frac{U_{n-1}}{U_n}$. Wikipedia gives the formula:
$$U_n=\frac{\left(x+\sqrt{x^2-1}\right)^{n+1}-\left(x-\sqrt{x^2-1}\right)^{n+1}}{2\sqrt{x^2-1}}$$.
Thus, the limit becomes:
$$\lim_{n\to\infty}\frac{\frac{\left(x+\sqrt{x^2-1}\right)^{n}-\left(x-\sqrt{x^2-1}\right)^{n}}{2\sqrt{x^2-1}}}{\frac{\left(x+\sqrt{x^2-1}\right)^{n+1}-\left(x-\sqrt{x^2-1}\right)^{n+1}}{2\sqrt{x^2-1}}}=\lim_{n\to\infty}\frac{\left(x+\sqrt{x^2-1}\right)^{n}-\left(x-\sqrt{x^2-1}\right)^{n}}{\left(x+\sqrt{x^2-1}\right)^{n+1}-\left(x-\sqrt{x^2-1}\right)^{n+1}}$$
Now let's call $a=x+\sqrt{x^2-1}$. Then $\frac{1}{a}=x-\sqrt{x^2-1}$, so the limits is now:
$$\lim_{n\to\infty}\frac{a^n-a^{-n}}{a^{n+1}-a^{-(n+1)}}$$
This depends on whether $a$ is $>$, $=$ or $<1$:
I'll do $|a|<1$ first:
We multiply the denominator and the numerator by $a^{n+1}$:
$$\lim_{n\to\infty}\frac{a^{2n+1}-a}{a^{2n}-1}$$
Here $a^{2n+1},a^{2n}\to0$ as $n\to\infty$, so the limit is equal to $a$.
In this case, the original limit is equal to $\frac{1}{a^2+1}=\frac{1}{2\left(x+\sqrt{x^2-1}\right)}=\frac{x-\sqrt{x^2-1}}{2}$.
The case where $|a|<1$ is very similar to this one.
The two remaining cases are $a=1$ and $a=-1$.
When $a=-1$, the denominator equals $2$ and the numerator is 0, so the limit does not exist and neither does the original one for values of $x$ for which $a=-1$, which is only $x=-1$.
The only case left is $a=1$ which gives $\frac{0}{0}$ which is undefined, so the limit is also undefined. This is for $x=1$.
Comment: Should the cases at the end be for $a\gtrless 1$, or for $|a|$? Moreover, the case for $|a|=1$ i.e. $x\in [-1,1]$ seems nontrivial since neither power vanishes in the limit.
Comment: @Semiclassical Yes, you are right, it should be |a|. I will add the |a|=1 case.
Comment: Small correction: $a^{n+1}*a^{n+1}=a^{2n+2}$ so the last display equation should have $a^{2n+2}-1$ in the denominator
Answer: I will give a derivation using the recursive form of the Chebyshev polynomials of the second kind.  To facilitate our analysis, we will define the ratio:
$$R_n(x) \equiv \frac{U_{n}(x)}{U_{n-1}(x)}.$$
The Chebyshev polynomials of the second kind satisfy $U_n(x) = 2x U_{n-1}(x) - U_{n-2}(x)$, so you get the corresponding recursive equation:
$$\begin{aligned}
R_n(x)
= \frac{U_{n}(x)}{U_{n-1}(x)}
&= \frac{2x U_{n-1}(x) - U_{n-2}(x)}{U_{n-1}(x)} \\[6pt]
&= 2x  - \frac{U_{n-2}(x)}{U_{n-1}(x)} \\[6pt]
&= 2x  - 1 \bigg/ \frac{U_{n-1}(x)}{U_{n-2}(x)} \\[6pt]
&= 2x  - 1/R_{n-1}(x). \\[6pt]
\end{aligned}$$
The base cases for the Chebyshev polynomials of the second kind are $U_0(x) = 1$ and $U_1(x) = 2x$ so you get the corresponding base case $R_1(x) = 2x$.  Taking the limit as $n \rightarrow \infty$ gives you the limiting equation $R_\infty(x) = 2x - 1/R_\infty(x)$, which we can rearrange to obtain the quadratic equation:
$$R_\infty(x)^2 - 2x R_\infty(x) + 1 = 0.$$
You have $x \geqslant 1$ so some additional work allows you to narrow down to the lower solution to the quadratic equation, which is $R_\infty(x) = x - \sqrt{x^2 - 1}$.  Squaring this quantity gives the corresponding squared limit:
$$R_\infty(x)^2 = 2x^2 - 2x \sqrt{x^2 - 1} -1.$$
You now have:
$$\begin{aligned}
\lim_{n \rightarrow \infty} \frac{U_{n}(x)^2}{U_{n-1}(x)^2 + U_n(x)^2}
&= \lim_{n \rightarrow \infty} \frac{[U_{n}(x)/U_{n-1}(x)]^2}{1 + [U_{n}(x)/U_{n-1}(x)]^2} \\[6pt]
&= \lim_{n \rightarrow \infty} \frac{R_n(x)^2}{1 + R_n(x)^2} \\[6pt]
&= \frac{R_\infty(x)^2}{1 + R_\infty(x)^2} \\[6pt]
&= 1 - \frac{1}{1 + R_\infty(x)^2} \\[6pt]
&= 1 - \frac{1}{2x^2 - 2x \sqrt{x^2 - 1}}. \\[6pt]
\end{aligned}$$

Limit of Ratio of Chebyshev Polynomials


Calculation of reliable digits for one linear system

Question:
Example: the linear equation system $Ax=b$ has one approximation $\bar x$ and
  one exact $x^* \neq 0$ solutions. we also gives: $p>3,   ||x^* - \bar
 x || \leq 10^{-20} + ||A||  ||A^{-1}|| 10^{-p} ||x^*|| $ which $||A||
||A^{-1}||=10^{4}$. The reliable digits of $\bar x$ for solutions of
  this system of equation is $0$.

Question: this example takes from my notes on Numerical Methods Course. anyone may describe it, which calculation reach the author to get $0$?

None of these two answers is not help me, I need some detail about
  this example. how we conclude that this example works?
Comment: Are you sure that you are not using the condition number $\|A\|·\|A^{-1}\|$? And since that is always $\ge1$, the last number should be the more realistic $10^4$? A relative error of $10$ or even $1$ would indeed invalidate the result.
Comment: The example is still impossible. By basic norm inequalities, $1=\|I\|=\|A·A^{-1}\|\le\|A\|·\|A^{-1}\|$. And $1\le 10^{-4}$ is always wrong.
Comment: Sorry this is a typo. I fix it @LutzL thanks for your nice inspection.
Answer: In the case of $p=4$ you get
$$
\|x-x^*\|\le 10^{-20}+\|x^*\|
$$
which means that in the worst case, the error is as large as the exact solution. In digits that means that any digit may be wrong.

For instance, if $x^*$ were one-dimensional with value $10$, then the possible values for $x$ would range from about $0$ to about $20$, which means that not even the leading digit is certain, even with rounding. In higher-dimensional cases you would get a ball around the exact solution, so for instance $x^*=(10,0)$ allows for $x=(5,-5)$ etc.
Comment: Would you please describe for me your example? what is $||xx-\bar x||$, and $p$ and $||x^*||$ in your example? thanks
Answer: Putting aside the remark by @Lutzl that $$1\le \|A\|\|A^{-1}\|$$ and hence there must a typo somewhere in the estimation $10^{-4}$, we can always examine the following case.
Let the true solution of an equation is $ x^*=1$. Let the admissible error be $10^{-n}$. We take an approximate solution $\bar x = 1 - 10^{-2n}$. The error is within acceptable bounds, yet no digits of $\bar x$ and of $x^*$ are the same.
Comment: Would you please describe which typo? is it possible to explain more? in which condition that $\bar x$ and $x^*$ be same?
Comment: @MichleJordan I think that estimate should be $10^4$. The values of $\bar  x$ and $x^*$ are the same only if the admissible error is zero.
Comment: would you please consider this is a typo and $10^4$ is correct, and describe a bit more about your answer?
Comment: @MichleJordan the exact value of $\|A\|\|A^{-1}\|$ is irrelevant for our problem as long as it is superior to $1$. Consider two numbers $1$ and $0.9999999$. The latter number approximates the former one with an arbitrary precision  - just put enough $9$s in decimal digits. Yet these two numbers have no common digits. Therefore, no matter how good your approximation is, you can not guarantee that the approximate solution has common digits with the true solution.
Comment: Now, I understand more, thanks, my last question is if the $10^4$ be correct, your answer is very short :) your answer is so nice for expert man, would you please add more context in your solution to add more learning for beginner like me?  you wrote very understandable, please add more detail for us in your answer to get familiar completely with this type of question. thanks
Comment: I study more, I know waste your time but I have one question, may I ask?


Algebraic structure for subtraction limited by 0 from below.

Question: Let's assume an algebraic structure with elements from non-negative real numbers with the operation $x - y := max(x - y, 0)$.
It fails at least 2 out of 3 group definition properties:

Associativity: $(5 - 6) - 2 \neq 5 - (6 - 2)$
Identity: $x = x - 0 \neq 0 - x = 0, \forall x>0$ (i.e. there's only right identity)
Inverse element equals to itself (unless one requires two-side identity to exist)

This algebraic structure is at least magma (i.e. it's closed under the operation), can it be further sharpened to a less general algebraic structure?
Comment: Your question is rather vague. Do you have any expectations of any particular properties of this operation? Have you explored around to try to discover any particular properties? Any specific property you can prove sharpens the operation to something more special, i.e. less general.
Comment: let's consider this operation given, can I move this algebraic body higher on the [scale](https://en.wikipedia.org/wiki/Magma_(algebra)#/media/File:Magma_to_group4.svg) from magma to group? if there are any properties of this operation that would help to move up the scale, would be great to find it out.
Comment: @Suzet could you please give an example that shows it?
Comment: @MaxLi My bad, I misread your operation. I deleted my comment.
Comment: You performed the associativity incorrectly.  The equation you wrote does not hold for $\mathbb{R}$.  Correct it and see if it holds for the algebra that you defined.
Comment: @RadialArmSaw I do not understand, the counter example given for associativity seems to work fine. We have $(5\ominus 6) \ominus 2 = 0 \ominus 2 = 0$ and $5 \ominus (6 \ominus 2) = 5 \ominus 4 = 1$, where I denote by $\ominus$ the operation defined in the question.
Answer: I will denote your operation by $\ominus$ so that we do not mix it with the ordinary minus sign. Following this chart that you posted in the comment section, and because the operation is not associative and does not admit a unit element as you proved, we are led to check whether $(\mathbb R_+,\ominus)$ is a quasigroup. That is, given any $a,b \in \mathbb R_+$, can we find a unique $x\in \mathbb R_{+}$ such that $a\ominus x = b$ (left divisibility) and a unique $y\in \mathbb R_+$ such that $y\ominus a = b$ (right divisibility).
Let us check left divisibility first. We have $a\ominus x = \max(a-x,0)$. In particular, because $x$ is non-negative, we see that $a\ominus x \leq a$. Therefore, if $b > a$ then the equation $a\ominus x = b$ does not have any solution $x\in \mathbb R_+$. It follows that left divisibility fails in general.
At this stage, we already understand that $(\mathbb R_+,\ominus)$ is not a quasigroup, and according to the previous chart it can not be anything else than a magma.
For sake of completeness, let us now check right divisibility. We have $y\ominus a = \max(y-a,0)$. It is not difficult to find out that $y = a+b \in \mathbb R_+$ is a solution, but is it unique ? Let us distinguish a few cases.

Case $a=b=0$ : In this case, the equation we need to solve is $\max(y,0) = 0$. There is only one solution in $\mathbb R_+$, that is $y = 0 = a+b$.

Case $b=0$ and $a > 0$ : Notice that if $y$ is any number satisfying $0\leq y < a$, we have $y\ominus a = 0$. Therefore, any such $y$ is a solution. There is an infinite number of them.

Case $a,b > 0$ : Let $y$ be a solution of $y\ominus a = b$. In particular, $\max(y-a,0) > 0$, so we must have $y-a > 0$ and $\max(y-a,0) = y-a$. The equation becomes $y-a = b$ so that $y = a+b$.

Thus, in general, the solution $y$ fails to be unique. Therefore right divisibility fails too.


How does one model a differential equation?

Question: Consider the following problem:
A city has a population of 1 000 000 people. The population increases by 1 percent each year. Furthermore, every year there are 1 000 people moving to the city.
Now, if one is to create a function describing the population, initially if would be $f(0) = 1 000 000$. Seeing as it increases by 1 000 every year, the function should be $f(x) = 1 000 000 + 1 000x$. Also, there is another function that describes the increase in the population: because it is a differential equation, should that be the derivative of $f$? Intuitively is seems the function should be something like $x \mapsto 0.01x$, where $x$ is $f(x)$. Also, if function $f$ is specified as mentioned above, its derivative could not be $f'(x)=0.01x$
$y'+ay=0$
This is the form it should be written on, right?
How should one should approach these problems?
Answer: Initially the population is $y_0$. At the end of year $i$ it is
$$y_i=y_{i-1}+a\,y_{i-1}+b$$ where $a$ is the percentage increase ($a=1\%$), and $b$ is the number of people that moved in. The equation can be rearranged to be
$$
y_i-y_{i-1}=a\,y_{i-1}+b\,.
$$
In the next round we use smaller time steps $\Delta t$ betweeen $t_{i-1}$ and $t_i$. To keep the the increases by $a$ and $b$ consistent it is reasonable to assume
that between those time steps the percentage increase is only $\Delta t\,a$ and the
people that move in are only $\Delta t\,b$. This gives
$$
y_i-y_{i-1}=\Delta t\,a\,y_{i-1}+\Delta t\, b\,.
$$
Dividing by $\Delta t$ and taking the limit leads to the ODE
$$
y'=a\,y+b\,.
$$
Comment: @DeltaFoxtrot Was this answer helpful ?


What is the best way to validate whether a set of given scores are a good predictor of a success metric?

Question: For example, I have a table of items each with the given scores (no unit) and success metric (quantity value). The success metric is the real-life performance indicating number of items sold, etc. whereas the given scores were the predicted success (unitless metric).

What is a good data science method for validating whether these scores do a good job indicating the success metric? I can create a prediction model with mediocre accuracy.
Are there other methods that can be used specifically to validate as in this case?

Item ID
Score
Success Metric

1
60
982

2
88
1000

3
46
70
Answer: The simplest approach would indeed be to use your Score to model your Success Metric in appropriate ways. For instance, you could use OLS if you are looking for unbiased predictions of the Success Metric, or quantile regression if you are looking for quantiles (common in sales prediction, to automatically include safety stocks). You could use spline transforms for your Score if there is nonlinearity. Or use a nonlinear method with an appropriate error metric (Kolassa, 2020).
Of course, this kind of begs the question of why you are modeling a Score to predict the Success Metric, and not modeling the Success Metric directly in the first place.
What is good will depend on your context. A sales forecast with an MSE of 100 may be "good enough", or it may not be worth the effort of setting up the model. A good way to look at this is to compare your model to a simple naive benchmark model, like the overall historical mean or quantile. If you can't beat that, then you indeed have to rethink what you are doing.


Retornar dados de um Json onde existe uma chave específica em uma variável PHP

Question: Tenho um JSON com uma estrutura padrão.
Dentro da estrutura eu tenho o codTipo que vareia entre A e B
{
    "codSeq": "001224",
    "codTipo": "A",

} {
    "codSeq": "001244",
    "codTipo": "A",

} {
    "codSeq": "023444",
    "codTipo": "B",

} {
    "codSeq": "0012444",
    "codTipo": "A",

}

Gostaria de saber se tem como eu passar um parâmetro ex: "codTipo": "B", e o JSON que for retornado para mim seja somente com os dados do tipo B.
{
        "codSeq": "023444",
        "codTipo": "B",

}

Hoje estou usando o seguinte código para me retornar esses dados:
$jsonAcademy = json_encode($jsonAcademy, JSON_PRETTY_PRINT);

echo $jsonAcademy;

Comment: Você fala em passar isso no url e já vir te retornado esse valor ou depois de você capturar o json com o php você passar somente essa chave e retornar os dados de codTipo B?
Comment: Quero apenas passar a chave em php mesmo. esse Json não é tratado por url.
Answer: caso tu tenha esses dados antes do json, ou usando json decode isso virá um array, desta maneira tu pode fazer um foreach, e ir comparando com o conteudo até achar o codigo que tu quer.
no momento que tu achar o codigo que tu quer tu separa e coloca em outro array e da json encode.
foreach($jsonAcademy as  $i){
    if(strcmp($i['0'],""codTipo": "B"")==0){
      $arraynovo['0'] = $i['0'];
      $arraynovo['1']= $i['1'];
    }
}

Answer: Trabalhando somente com o objeto JSON e PHP direto, sem convertê-lo para array, um exemplo de como você pode fazer é:
(explico no código o passo-a-passo)
<?php
// String JSON
$str_json = '[{"codSeq": "001224","codTipo": "A"},{"codSeq": "001244","codTipo": "A"}, {"codSeq": "023444","codTipo": "B"}, {"codSeq": "0012444","codTipo": "A"}]';
// Analisa a string codificada JSON e converte-a em uma variável do PHP.
$obj = json_decode($str_json);

// Função que recebe como parâmetro a $codTipo e a variável php com a string JSON
function getCodigosPorTipo($codTipo, $obj){
    // Separa com o explode e list a chave 'codTipo' do valor do tipo 'A' ou 'B'
    list($index, $tipo) = explode(":", $codTipo);
    // Remove aspas das variáveis $index e $tipo
    $trimIndex = trim($index, ' " ');
    $index = $trimIndex;
    $trimTipo = trim($tipo, ' " ');
    $tipo = $trimTipo;

    echo "Dados de tipo de código: $tipo
";
    foreach ($obj as $index => $dados) {
        // Se o coTipo for igual ao tipo desejado ele imprime as informações
        if ($dados->codTipo === $tipo){
        echo "
";
        echo "CodTipo: " . $dados->codTipo . "
";
        echo "CodSeq: " . $dados->codSeq . "
";
        }
    }
}

// Podemos definir a busca por codTipo A ou B
$busca_por_codTipo = '"codTipo": "B"';
// Chamamos a função 'getCodigosPorTipo' passando os parâmetros necessários para busca e exibição do resultado
getCodigosPorTipo($busca_por_codTipo, $obj);

Ao executar o código irá exibir:

Convertendo a string JSON para array, é mais fácil de se trabalhar, assim deixo este outro exemplo de código:
(Fiz o código sem função mesmo para realizar o proposto, mas você pode criar uma função similar ao do primeiro código que postei)
<?php
// String JSON
$str_json = '[{"codSeq": "001224","codTipo": "A"},{"codSeq": "001244","codTipo": "A"}, {"codSeq": "023444","codTipo": "B"}, {"codSeq": "0012444","codTipo": "A"}]';
// Converte a string codificada JSON para um array do PHP.
$array_codigos = json_decode($str_json, TRUE);

echo "Dados de tipo de código: B

";

    foreach ($array_codigos as $index => $dados) {
        // Remove aspas da chave codTipo e armaze numa variável $tipo para comparação
        $tipo = trim($dados['codTipo'], ' " ');
        // Se tipo for igual ao tipo desejado, no caso 'B', ele imprime as informações
        if ($tipo === 'B'){
        echo "CodTipo: " . $dados['codTipo'] . "
";
        echo "CodSeq: " . $dados['codSeq'] . "
";
        }
    }

Irá imprimir o mesmo resultado:
Comment: Muito obrigado amigo. As duas soluções funcionaram!
Deus te abençoe!
Comment: Tmj meu camarada. 'Deus te abençoe': Amém, Deus te abençoe também! Bora ficar bão na programação! =)
Comment: kkkk... estou nessa luta para me aperfeiçoar mais a cada dia... de vez em quando as coisas travam ai tenho que recorrer aos experts kkk
Comment: Tá certo eu migrei tem pouco tempo para programação também (5 meses de estágio + 6 meses de contratado), mas esta dando pra ajudar no que posso e aprender aqui na comunidade tbm.


Joining data from three views

Question: I need to bring in data from 3 different views into my results.
View A has almost all the data I need but it uses primary keys for two fields and I need to pull in the identifiers from the UI so it looks like this:
View A:
PortfolioID,
SecurityID,
then 20 other points I need
View B:
PortfolioID,
PortfolioCode,

View C:
SecurityID,
Symbol
I need to select all the data from View A but instead of PortolioID I need the corresponding PortfolioCode from View B and instead of SecurityID I need the corresponding symbol from View C.
Sorry if this is very basic, I'm a complete newby to SQL.
Thanks!
Answer: You need to use joins in order to accomplish what your looking for.
 SELECT ViewA.PortfolioID, ViewA.SecurityID, ViewA.*, ViewB.PortfolioCode, ViewC.Symbol
    FROM ViewA
    INNER JOIN ViewB on ViewB.PortfolioID = ViewA.PortfolioID
    INNER JOIN ViewC on ViewC.SecurityID = ViewA.SecurityID

You can learn more about the different types of joins here
Answer: Try something like this:

    SELECT a.*, b.PortfolioCode, c.Symbol
    FROM ViewA a
    INNER JOIN ViewB b ON (a.PortfolioID = b.PortfolioID)
    INNER JOIN ViewC c ON (a.SecurityID = c.SecurityID)

Comment: I was able to get this to work. One issue I'm running into that I didn't think of is that every transaction has a debit and credit. Therefore I actually have securityID1 and securityID2 in view A. Both need to pull a symbol from the same view (c) hich has securityid as the key. The inner join of both ID1 and ID2 to c.SecurityId doesn't work.
Comment: Try this, if I'm understanding the structure of the views correctly.

`    SELECT a.*, b.PortfolioCode, c1.Symbol AS SymbolID1, c2.Symbol AS SymbolID2
    FROM ViewA a
    INNER JOIN ViewB b ON (a.PortfolioID = b.PortfolioID)
    INNER JOIN ViewC c1 ON (a.SecurityID = c1.SecurityID1)
INNER JOIN ViewC c2 ON (a.SecurityID = c2.SecurityID2)`

Joining data from three views


Eigenvalues in PCA

Question: When I carry out Principal Component Analysis, the outputs are the Eigen-values and Eigen-vectors for each PC.
Question: are the Eigen-values directly proportional to the Variance explained by the PC? For example, for a three-dimensional data-set, my Eigen-values came out as: 0.007, 0.002, 0.001. Does that mean that the first PC captures 0.007 / (0.007 + 0.002 + 0.001) ~ 77% of the variance?
Comment: Does this answer your question? [PCA and proportion of variance explained](https://stats.stackexchange.com/questions/22569/pca-and-proportion-of-variance-explained)
Comment: Does [https://stats.stackexchange.com/questions/22569/pca-and-proportion-of-variance-explained?rq=1](this) answer your question?
Comment: the answer is always YES
Answer: There are 2 concepts to look out for and it can get confusing at first. Let me put it the best way you can understand:

Variance of one PC:
The variance for the $i^{th}$ principal component is equal to the $i^{th}$ Eigenvalue. In your case, the variance of the $1^{st}$ PC is equal to the $1^{st}$ Eigenvalue which is 0.007.

Proportion of Variance by one PC:
The proportion of variation explained by the $i^{th}$ principal component is then defined to be the eigenvalue for that component divided by the sum of the eigenvalues. In other words, the $i^{th}$ principal component explains the proportion of the total variation for total number of factors let's say $n$ as:
$x_i$/$\sum$$x_{i\to{n}}$

Your interpretation is the 2nd one which explains the proportion of variance captured.

If you divide the $1^{st}$ PC by the sum of all EVs, you will get the proportion of variance captured.
Hope this helps!
Comment: Great, very clear: many thanks, Vishva!

Eigenvalues in PCA


How to work with controls inside a user control from a shared (static) DependencyPropertyChangedCallback in WPF?

Question: I'm quite a beginner in WPF. I worked myself through a few tutorials and now I wanted to try my first own project and am stuck right at the beginning.
I try to create a user control with a bindable property Items.
The user control's XAML contains just a simple TextBox, nothing else.
<UserControl x:Class="UserControl1" xmlns:local="clr-namespace:TestControl" ...>
    <TextBox x:Name="txtContent" />
</UserControl>

In the code behind of the user control I created a DependencyProperty called Items.
Public Shared ReadOnly ItemsProperty As DependencyProperty = DependencyProperty.Register("Items", GetType(IList(Of String)), GetType(MyUserControl), New FrameworkPropertyMetadata(Nothing, AddressOf MyUserControl.OnItemsChanged))

Public Property Items As IList(Of String)
    Get
        Return MyBase.GetValue(MyUserControl.ItemsProperty)
    End Get
    Set(ByVal value As IList(Of String))
        MyBase.SetValue(MyUserControl.ItemsProperty, value)
    End Set
End Property

Private Shared Sub OnItemsChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
    ...
End Sub

In the OnItemsChanged procedure I'd like to add the content of the items collection to the TextBox of the user control. But since the DependencyProperty is Shared (static for C# users) and the user control itself is not I don't know how to get a grip of the TextBox inside my user control and work with it.
I tried to call a non-shared procedure in the setter of the .NET property, but this .NET property seems to be only cosmetic and of no use.
I also tried to cast the sender to UserControl and get the Content of the user control (which should be my TextBox). But the Content is always Nothing (null for C# users).
So, how do I work with controls inside my user control from the static DependencyPropertyChangedCallback?
Comment: Insulting c# users while you use VB isn't the best way to get assistance.
Comment: If you read carefully, then you'll see, that's it's not an insult, it's pity or sympathy...
Answer: After stripping down my test project to the essentials everything I tried before without success (casting the sender to UserControl and getting it's Content or casting the sender to the specific control type and getting the TextBox by it's name) suddenly worked.
Then I did the stripping down again step by step until I found the step, after that the problem disappeared.
The problem was, that I created a UserControl file named "TestControl.xaml" but the UserControl tag inside this file still had the attribute x:class="UserControl1".
I could use this control with it's proper name (TestControl) in the MainWindow, I could compile my project and I could debug my project, but apparently this prevented to work with the user control in the code behind.
So, renaming the attribute x:class="UserControl1" to x:class="TestControl" did the trick.


Locked to version problem when upgrading

Question: Every time I run the composer command to upgrade Drupal 8.9.16 to Drupal 9, it notifies me that there is a problem with a module.
For example, I run:
composer require drupal/core --with-all-dependencies

I get:
Using version ^9.2 for drupal/core
./composer.json has been updated
Running composer update drupal/core --with-all-dependencies
Gathering patches from patch file.
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - drupal/twig_tweak is locked to version 2.9.0 and an update of this package was not requested.
    - drupal/twig_tweak 2.9.0 requires drupal/core ^8.7 || ^9.0 -> satisfiable by drupal/core[9.2.0-alpha1, ..., 9.3.x-dev].
    - drupal/core[9.2.0-alpha1, ..., 9.3.x-dev] require symfony/routing ^4.4 -> satisfiable by symfony/routing[v4.4.0-BETA1, ..., 4.4.x-dev].
    - symfony/routing v4.4.25 conflicts with symfony/routing v4.4.25.
    - symfony/routing v4.4.24 conflicts with symfony/routing v4.4.24.
    - symfony/routing v4.4.22 conflicts with symfony/routing v4.4.22.
.....
.....

In this case, twig_tweak is already at 2.9.0, which supports Drupal 9, but it is complaining about it. I have been removing the problem module from composer.json and then running composer update, but every time I run composer require drupal/core --with-all-dependencies another module shows up with the same issue. I feel like I may be doing something wrong.
Comment: Assuming your project is checked in to version control, try deleting `composer.lock` and `/vendor` and then running `composer update`.  This is the nuclear option but I've used it on several sites to update to D9.
Comment: Are you using `core-recommended` as described in the docs? https://www.drupal.org/docs/updating-drupal/updating-drupal-core-via-composer
Comment: My composer.json doesn't currently use `core-recommended`.
Comment: You need to update all the contributed modules before attempting the core upgrade - see https://www.drupal.org/docs/upgrading-drupal/how-to-prepare-your-drupal-7-or-8-site-for-drupal-9/upgrading-a-drupal-8-site. [Upgrade status](https://www.drupal.org/project/upgrade_status) is very useful to get a list of what needs updating and what's available
Comment: Hi @Clive, I did run Upgrade Status and everything should be updated but I'm still getting these errors. For example, in this case, twig-tweak is at 2.9.0 which should support Drupal 9.
Comment: Did you use composer to update the modules? If not then composer won't know you've done it
Comment: @Clive I did update all with composer.
Answer: Once you need to update the current code follow the below steps:
Step 1: Run command:
composer update
Step 2: You need to update the current code with an update to Drupal 8 and update using custom theme and module version
core_version_requirement: ^8.9 || ^9
SO current code and modules are ready to be upgraded with Drupal 9. This way we can reduce errors.
Step 3: Create a default folder and set the file to be editable.
chmod 777 web/sites/default
find web/sites/default -name "*settings.php" -exec chmod 777 {} ;
find web/sites/default -name "*services.yml" -exec chmod 777 {} ;
Step 4: Run These Commands (This Command Edits Your Composer File to Be Upgrade Ready)
composer require drupal/core-recommended:^9 drupal/core-composer-scaffold:^9 drupal/core-project-message:^9 --update-with-dependencies --no-update
If you have drupal/core-dev installed.
composer require drupal/core-dev:^9 --dev --update-with-dependencies --no-update
Step 5: Run command
composer require "drupal/core:9.2.1 as 8.9.16" --no-update && composer update
Step 6: Run Drush updatedb command
If you face any problem with core then
Step 6.1: Run Composer outdated "drupal/*" and update this listed module into the Drupal 9 version and upgrade the appropriate custom modules and themes with Drupal 9.
Step 6.2: $config_directories['sync'] change to $settings["config_sync_directory"]
https://www.drupal.org/docs/upgrading-drupal/upgrading-from-drupal-8-to-drupal-9-or-later


Increase the performance of sort command on a multi-processor Sun Solaris box

Question: I have a bunch of (rather large i.e. >100MB) comma separated files which needs sorting on 4 processor box with SunOS 5.10
Sort appears to run rather slow (minutes)
I am wondering if there is any way to speed things up, for example making use of more than one processor/core or perhaps by just using some clever sort options?
PS: I am using the entire line as a key so just sort filename > filename.sorted
Answer: Here is the script I wrote for this purpose. On a 4 processor machine it improved the sort performance by 100% ! (Thanks Bash for the tip!)
#! /bin/ksh

MAX_LINES_PER_CHUNK=1000000
ORIGINAL_FILE=$1
SORTED_FILE=$2
CHUNK_FILE_PREFIX=$ORIGINAL_FILE.split.
SORTED_CHUNK_FILES=$CHUNK_FILE_PREFIX*.sorted

usage ()
{
     echo Parallel sort
     echo usage: psort file1 file2
     echo Sorts text file file1 and stores the output in file2
     echo Note: file1 will be split in chunks up to $MAX_LINES_PER_CHUNK lines
     echo  and each chunk will be sorted in parallel
}

# test if we have two arguments on the command line
if [ $# != 2 ]
then
    usage
    exit
fi

#Cleanup any lefover files
rm -f $SORTED_CHUNK_FILES > /dev/null
rm -f $CHUNK_FILE_PREFIX* > /dev/null
rm -f $SORTED_FILE

#Splitting $ORIGINAL_FILE into chunks ...
split -l $MAX_LINES_PER_CHUNK $ORIGINAL_FILE $CHUNK_FILE_PREFIX

for file in $CHUNK_FILE_PREFIX*
do
    sort $file > $file.sorted &
done
wait

#Merging chunks to $SORTED_FILE ...
sort -m $SORTED_CHUNK_FILES > $SORTED_FILE

#Cleanup any lefover files
rm -f $SORTED_CHUNK_FILES > /dev/null
rm -f $CHUNK_FILE_PREFIX* > /dev/null

Answer: See: need high performance /bin/sort; any suggestions?
Answer: I found this script a while ago: distsort.sh
I don't remember  what I used it for or if it worked, so let me know if it works for you.


Can long run variance of a time series be used to test mean of the series?

Question: Let $Y_t$ be a stationary time series with $Y_t \sim N(\mu, \gamma_0) \,\, \forall t$.
Further define, $$\bar Y_T \equiv (1/T)\sum\limits_{t=1}^TY_t$$
Further, let $\sigma^2_l$ be the long-run variance this time series. From this answer, I think we can say:
$$\lim_{T \to \infty} \frac{\bar Y_T-\mu}{\sigma_l/\sqrt{T}} \sim N(0,1)$$
This is because $\bar Y_T$ is a sum of $T$ normal random variables with (unconditional) mean $\mu$. Now say we have a consistent estimate of $\sigma^2_l, \hat \sigma^2_l$. Can we use the following statistic to test for hypothesis such as $H_0: \mu = \mu_0$?
I feel that if the true dependence structure of the time series was known (say for example, we know that $Y_t$ follows $AR(p)$), then the standard error of the residuals of the $AR(p)$ model, estimated using MLE, is nothing but an estimate of long-run variance. If true, then the above  allows us to test for mean without having to fit a model (although the bandwidth selection is perhaps a similar exercise?).
Comment: In the limit, divide by standard deviation, not variance.
Comment: @RichardHardy: thanks for pointing out the error. Corrected now.
Answer: Basically, yes and yes - you can replace the long-run variance with a consistent estimator thereof and, by Slutzky's theorem, the test statistic will still be standard normal under the null.
And indeed, kernel-based long-run variance estimators are sometimes also referred to as nonparametric estimators that do not (there still are some assumptions, of course) require you to postulate a parametric model for the dependence.
That said, indeed, if you knew that your series follows a specific structure, you could exploit that. Why if in OLS the autocorrelation between residuals is positive, it will lead to inflated t-stats? discusses that the long-run variance of an AR(1) is
$$
\sigma^2/(1-\phi)^2,
$$
which you could estimate parametrically.
And indeed, as discussed for example in Newey-West t-stats and critical values, there is a price to pay for the above nonparametric inference, namely relatively poor finite-sample performance.
Comment: Yes, the conditions on the errors are indeed far weaker than normality. The papers in the linked answers by Andrews and Newey and West provide (technical) detail. As to the first part, I am not sure I completely understand - yes, indeed, poor finite-sample inference can pop up in other contexts, too.
Comment: The book by White (Asymptotic Theory for Econometricians) for example discusses a wide range of CLTs covering many dependent and heterogeneous processes.
Comment: Thanks for including some imp details in the answer. On your comment about exploiting dependence structure if it's known, I have had a related question [here](https://stats.stackexchange.com/q/399681/239481) which unfortunately remains unanswered. Although the finite sample performance be poor, I am inclined to use this for practical reasons. Model selection while fitting a time series is often based on information criteria which often selects model with insignificant coefficients (which maybe good for forecasting though). When the interest is to check statistical significance of mean...
Comment: ...that is often not helpful. Then we end up selecting a model based on PACF ad even after that the standard errors of the coefficients are based on asymptomatic distribution. Wouldn't there also we have problem of finite sample performance?
Comment: Also, can I relax the normality assumption? Since this is a limiting distribution, CLT would apply nevertheless?
Comment: I posted an answer to your other question there.
Comment: Sorry, I am still confused about the normality assumption. In the first line of my question, I have assumed that Yt follows normal distribution. If we relax this assumption, how can I still claim z-stat will follow normality? I am unable to see how (and which) CLT can be used here.
Comment: See e.g. here https://personal.utdallas.edu/~d.sul/Econo2/andrews91.pdf
Comment: Sorry for bothering again but this seems to be about *estimation* of LRV. I am saying even if we knew true LRV, can make make assertion about normality of the z-statistic? Again, I am sorry I couldn't see where this is addressed in the Andrews paper.
Comment: Thank you so much for the reference. I had been looking for a book on exact title!


Logistic regression with binomial data in Python

Question: This is probably trivial but I couldn't figure it out. I want to fit a logistic regression model, where my dependent variable is not a Bernoulli variable, but a binomial count. Namely, for each $X_i$, I have $s_i$, the number of successes, and $n_i$, the number of trials. This is completely equivalent to the Bernoulli case, as if we observed these $n_i$ trials, so in principle I can use, e.g., statsmodels logistic regression after I unravel my data to be Bernoulli observations. Is there a simpler way?
Comment: GLM with family=Binomial estimates the count model where the dependent variable is the number of successes and failures.
Answer: The statsmodel package has glm() function that can be used for such problems. See an example below:
import statsmodels.api as sm

glm_binom = sm.GLM(data.endog, data.exog, family=sm.families.Binomial())

More details can be found on the following link. Please note that the binomial family models accept a 2d array with two columns. Each observation is expected to be [success, failure]. In the above example that I took from the link provided below, data.endog corresponds to a two dimensional array (Success: NABOVE, Failure: NBELOW).
Relevant documentation: https://www.statsmodels.org/stable/examples/notebooks/generated/glm.html
Comment: Vishal.. I think you should put in your answer that you provide a Nx2 matrix for the dependent variable with the counts
Comment: I've added some clarification per your feedback. Thanks.
Answer: Alternatively using R-style formula
import statsmodels.api as sm
import statsmodels.formula.api as smf

mod = smf.glm('successes + failures ~ X1 + X2', family=sm.families.Binomial(), data=df).fit()
mod.summary()
```

Comment: `sm` is needed by `family=sm.families.Binomial()`
Comment: Is there any reason to import both sm and smf?


How to show a php or html file with output buffering

Question: I am trying to display a index file using the ob functions. I am very close to getting it,  the only problem is not everything is correct. When the file loads only some of the css files are being used. They both seem to be included correctly but only one is applied not all of them. Is there something wrong with the set of functions I am using? Maybe I need to use different ones?
here is the method that gets the contents.
public function get($file)
{
    if(file_exists($file))
    {
        ob_start();

        include $file;
        $out1 = ob_get_contents();

        return $out1;
    }
    else
    {
        return "File not found.";
    }
}

$file can be any html file like the one below:
<!doctype html>

        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
        <link rel="stylesheet" href="css/foundation.css" type="text/css"/>
        <link rel="stylesheet" href="css/screen.css" type="text/css"/>
        <link href='http://fonts.googleapis.com/css?family=Lekton:400,700,400italic' rel='stylesheet' type='text/css'>
        <link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300' rel='stylesheet' type='text/css'>

Email: email@example.com

Phone: 123345355

Quality products & hassle-free service.

Who We Are

                        We like to keep things simple. Contact us today using our form to request more information, or call us directly. We appreciate your business!

                    <form action="contact.php" method="post">

                                Name<input class="input_custom" name="name" type="text">

                                Practice<input class="input_custom" name="practice" type="text">

                            E-mail<input class="input_custom" name="email" size="50" type="text">

                            Subject<input class="input_custom" name="subject" size="50" type="text">

                        Message
                            <textarea name="message"></textarea>
                        <input class="button small radius" value="Send" type="submit">
                    </form>

when the get() method return the content it simple echoed thats all. When I inspect element on the page the html source looks exactly like the code above. All the images show just fine but not all the css shows only certain files. The files are included and I have no errors saying they can not be found.
Comment: You aren't doing anything with `$out1`. Your code might as well not use the output buffering functions at all. You'll need to provide examples of what you expect to see and what you're actually seeing in order to even begin to debug your problem.
Comment: @Phil sorry about that fixed the error. Still no go on the css :(
Comment: Why are you even bothering doing this? You seem to be performing the tasks that a web server does quite well (fetching files by name and showing them via HTTP).
Answer: You aren't ending or flushing the output buffer so you're most definitely going to run into trouble.
I suggest you use ob_get_clean() instead, eg
ob_start();
include $file;
return ob_get_clean();

Comment: unfortunately when I use the code above then none of the css works at that point :(
Comment: @zachstarnes You'll have to explain what that means. Do you have any console errors for missing CSS files? What does the rendered HTML source look like (assuming this has something to do with HTML)?
Comment: there are no errors about missing css files that is what is confusing me what shows is just plain html and images with no css applied. The HTML source looks normal.
Comment: @zachstarnes There's no way to help you debug this if you're not going to provide the necessary information. What's in `$file`? What do you do with the value returned from `get()`? **What does the rendered HTML source look like?**
Comment: I apologize for not giving more info. Please see the update above
Comment: @zachstarnes Ok, so you've shown what a `$file` can look like. For the last time, **what does the rendered HTML source look like?** Don't use use the DOM inspector / *Elements* tab, view the actual page source. Also, check your *Network* tab for the CSS files and inspect the responses.


Setting section headers in R file that get rendered with spin()

Question: Traditionally I declare section headers in Rmd files like this:
# header 1
```{r, echo=FALSE}
print("foo")
```

## header level 2
```{r}
print("bar")
```

So that when it gets rendered to HTML, it looks like this. And I can put in a table of contents.
header 1
foo

header level 2
bar

Using the spin() function, I would like to generate a Rmd file from a R ('Main.R') file that has the proper headers in knitr. Currently if I have this R file:
#+ section_1, echo=F
# what do I put here so that "header 1" gets inserted into the spun Rmd?
print("foo")

#+ section_2
print("bar")

Spinning with spin('Main.R', knit=F) produces this:
```{r section_1, echo=F}
# what do I put here so that "header 1" gets inserted into the spun Rmd?
print("foo")

```{r section_2}
print("bar")
```

I could not find any documentation that suggests how to insert the section names?
Comment: The idea of vignettes is that you can mix readable text with executable code.  Rmd files are close enough to vignettes for this:  just use `knitr::purl("source.Rmd")` to produce `source.R`, and source that.  The `source.Rmd` file is the true source, the `source.R` file is an intermediate.  You only ever edit `source.Rmd`.
Comment: I doubt if there's a way. The theory would be, if you want to write a full markdown document, then run `spin` and subsequently edit the output. It sounds as if you want to mix R text and markdown... but for this we have .Rmd files. What's the use case?
Comment: The use case is the same as the driver for using spin(). Have written R file, would love to continue to be able to source() it, but would also like to generate a report from it. The addition of section headers would allow the creation of a proper table of contents, which improves usability of the HTML report significantly. Editing the Rmd is not a good solution because it would mean maintaining 2 files - the .R and the .Rmd.
Answer: You can include Roxygen comments in .R files.  For example,
#' ## Section 1
print("foo")

#' ## Section 2
print("bar")

If you want to, you can include a full YAML header such as
#' ---
#' title: My document in a .R file
#' output:
#'   html_document:
#'     toc: true
#' ---
#'

but you'll probably need to use rmarkdown::render instead of knitr::spin.
Comment: Can the header include a variable R object, and how do you format that?
Comment: @user42485:  Yes, e.g. `#' title: "`r thetitle`"`. (where there are backticks just inside the two double quotes; they're rendering as blanks for me).


Odoo 13 API how to save null search results as a string

Question: I am using the Odoo 13 api as such.
  wanted_ids= models.execute_kw(db, uid, password,
                                        'res.partner', 'search_read',
                                        [[['name', '=',False]]],
                                            )
    print(wanted_ids)

as expected nothing is returned.
[]

What I want instead of null
['Nothing found']

I am not sure how to approach this. I have tried to raise exceptions but this does not save to the wanted_ids variable.
Answer: if not wanted_ids:
    wanted_ids = ['Nothing found']

This is pure Python, I think so


mod_rewrite image problems

Question: I currently have this in .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/(.*)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L]

When I visit http://localhost/type1/page/home/play it works fine, however I would like to take the /page/ out therefore being http://localhost/type1/home/play
I have tried the following:
RewriteRule ^(.*)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L]

however some images seem to vanish, and some don't.
Also, I find hard links in my nav do not work either as they are asking for variables used in internal pages.
I am not sure if this is a absolute/relative problem as my CSS is written as
<link href="http://localhost/type1/global.css" rel="stylesheet" type="text/css" />

and links seem to be full links when I inspect them with my browser!?
Any help would be great
Comment: sorry i have just looked at my question again today... was pretty tired last night when i wrote this... i would like the url to be http://localhost/type1/home/play
Answer: As I understand the .htaccess file is located in /type1/ subfolder. Try these rules:
Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(.*)$ /type1/internal.php?parentFolders=$1&pageTitleID=$2 [L,QSA]

The (.*) pattern you are using is too broad for first parameter, I use ([^/]+) instead (any character except slash).
Added 2 rewrite conditions .. so only requests to non-existing files and folders will be rewritten. This should help with disappeared images (or may not -- it all depends how how the links are written).
Added QSA flag to preserve any existing query string.
Comment: LazyOne you are god in web development format!!! thanks so much! I have been busting my b's for a while on this now!... all works but the new ([^/]+). The parentFolders value is similar to "/home/blog/article1" e.g. the url of the web page (excluding //localhost/ etc. The cms creates this to put in for links and so on... also helps for things such as bread crumbs and dynamic title tags... i have replaced that with ([^*]+) is that ok, or is there a better way of doing it?     Once again thanks so much!
Comment: @Theo `([^/]+)` means "at lest one ANY character, but not slash `/`". This is perfect pattern for separating path segments, as it will ALWAYS match only ONE segment, while `(.*)` means "ZERO or more of ANY characters". Also consider: This pattern `^(.*)/(.*)$` will match `help/delivery` as well as `help/pink/kitten` as well as `help/me/please/anyone`. If that is OK for you -- they use your original pattern `(.*)` as `([^*]+)` does not make much sense.
Comment: Ok great stuff! essentially I use the parentFolders value for other things, as said before, breadcrubs etc. All information on the page is pulled bassed on the pageTitleID, so if somoene puts /this/is/some/random/text/page-number-1, it wont matter as long as the pageTitleID is pulled through e.g page content is $pageTitleID['pageContent'], or $pageTitleID['internalImage'].   Would you say that this is an ok way of formating my cms? Do you think it will be ok from and SEO point of view?
Comment: @Theo If you want some authoritative answer, better ask it at http://webmasters.stackexchange.com/ (a sister site of StackOverflow). But if you ask me -- I do not really like such approach (variable length path with ID at the end): http://webmasters.stackexchange.com/questions/17002/why-does-stackoverflow-question-urls-have-the-question-id-before-seo-friendly-qu/17052#17052 **P.S.** If you find my answer useful, please consider marking it as accepted (if it solves your issue, of course).
Comment: Nice one! Thanks a lot LazyOne you have been a great help to me! Much appreciated!

mod_rewrite image problems


Load a small random sample from a large csv file into R data frame

Question: The csv file to be processed does not fit into the memory. How can one read ~20K random lines of it to do basic statistics on the selected data frame?
Comment: One [previous answer](http://stackoverflow.com/questions/15532810/reading-40-gb-csv-file-into-r-using-bigmemory/18282037#18282037)
Answer: You can also just do it in the terminal with perl.
perl -ne 'print if (rand() < .01)' biglist.txt > subset.txt
This won't necessarily get you exactly 20,000 lines. (Here it'll grab about .01 or 1% of the total lines.) It will, however, be really really fast, and you'll have a nice copy of both files in your directory. You can then load the smaller file into R however you want.
Comment: @pomber you could first copy the header line (e.g. `head -1 file.txt > sample.txt`) and then run the perl operation with `>>` instead to append
Comment: nice, any way to keep the csv header?
Comment: Is there a way to do this with Python?
Comment: For Windows you'd need to change the `'` to `"`
Comment: Tried this using a csv as the bigFile, but it copied the whole file.
Comment: I tried with windows and csv and it worked fine. Thanks!
Answer: Try this based on examples 6e and 6f on the sqldf github home page:
library(sqldf)
DF <- read.csv.sql("x.csv", sql = "select * from file order by random() limit 20000")

See ?read.csv.sql using other arguments as needed based on the particulars of your file.
Answer: This should work:
RowsInCSV = 10000000 #Or however many rows there are

List <- lapply(1:20000, function(x) read.csv("YourFile.csv", nrows=1, skip = sample(1, RowsInCSV), header=F)
DF = do.call(rbind, List)

Comment: Doubt it. Takes about 6 seconds on my machine, so it doesn't really make a difference unless you have to do it all the time.
Comment: could it be that the arguments in the sample function are inverted? sample(RowsInCSV, 1)? Furthermore, I think a bracket in the end of the lapply command is missing.
Comment: is it as fast as via Perl?
Answer: The following can be used in case you have an ID or something similar in your data.
Take a sample of IDs, then take the subset of the data using the sampled ids.
sampleids <- sample(data$id,1000)
newdata <- subset(data, data$id %in% sampleids)

Comment: Not at all helpful if, as OP says, "The csv file to be processed does not fit into the memory".


Visual Studio extensions for code generation...what's the best way

Question: So we have this tool, it's a web page, we drop a large piece of text in textBox a (say sql) run the tool
and it generates the guts of a code file in TextBoxb (say a custom view class model).
it's written in C#.
I know there are several ways to create visual studio extensions.
What I'd like to know is, what's the best/easiest/fastest way to take a c# dll that has a method that takes text in and returns text out, and turn it into a VisualStudio extenson, that takes text in and creates a files, adds it to the project and puts the text into it.
We're using Vs2008 and VS2010, and I'm okay the best soloution only work on 2010.
The closest I've found by googling so far is this:
http://visualstudiomagazine.com/articles/2009/03/01/generate-code-from-custom-file-formats.aspx
but they are for custom file formats only, i want to generate*.cs and *.rdlc and similar files.
I'd also like to be able to add methods to an existing class.
tutorial walkthroughs or better approaches woud be greatly appreicated.
Comment: well for example, we paste in the SQL to retreive the object, and the the generator cals the sql and write the code for the CustomView Class for that retreive. then that is used to generate the Controller methods, the View etc. all classes and snippets of code get generated, then all we have to is write our custom Buissness objects and make and cusomt changes to the View.
Comment: Off the top the easiest approach to extention writting in many cases is using MEF or you may just be able to use a T4Template instead if code generation is your only requirement but, that could be a bit more difficult. But I'm not totally clear on exactly how your extension is to work.
Comment: So you are taking a web form and placing text in a text box on the form. That text is actually the code you wish to incorporate in your generator. Past that something gets put into a dll and run in .NET?
Comment: So for your example you would return a dll that contains an implementation of a common interface that when used runs the SQL given? Or am I off?
Comment: Oh sorry just saw the first part of your comment
Comment: Well i am still not really sure what it is you are wanting to do but.... for code generation i recommend t4 templates http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx, for SQL Object Relational Mapping, use the Entity Framework http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework, and for the simplest way to write an extension for Visual Studio (provided you have vs 2010 ) use MEF http://channel9.msdn.com/Blogs/VSIPMarketing/VSX101-An-Introduction-to-Visual-Studio-2010-Extensibility.
Answer: VS package Builder is the answer. Lots easier.


index.html is not showing up (Request method 'GET' not supported)

Question: Note: The code can be found here: https://github.com/silentsnooc/ema-server

So, I am cleaning up a quite messy project in which I try to serve static content as well as a small REST API. However, the application gets started as any other:
@SpringBootApplication
public class Server {
    public static void main(String[] args) {
        SpringApplication.run(Server.class, args);
    }
}

The REST API works for me but the web client is not. I've moved the web client resources into src/main/resources/static/

However, if I visit http://localhost:8080 what I get is 405 (Request method 'GET' not supported).
org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
    at org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping.handleNoMatch(RequestMappingInfoHandlerMapping.java:207) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lookupHandlerMethod(AbstractHandlerMethodMapping.java:374) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:314) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.getHandlerInternal(AbstractHandlerMethodMapping.java:61) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMapping.getHandler(AbstractHandlerMapping.java:352) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.getHandler(DispatcherServlet.java:1145) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:936) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) ~[spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) [spring-webmvc-4.3.6.RELEASE.jar:4.3.6.RELEASE]
...

I do not understand what's missing - maybe I got the folder structure wrong?
Please let me know if you need any further information from my side.
Answer: As stated here @RestController is itself annotated with @ResponseBody which makes the response of your RequestMapping methods to be their return value. In your case that is the index.html string you're seeing in the web browser.
Replacing the @RestController annotation with @Controller should do the trick.

As for your error you have another class WordEmbeddingRestController which defines mappings for "/" as well, as "/" (or "" to be exact) is the default path value. Removing the @RestController annotation from this class will enable you to GET the desired html file.

Ignore that, GET is the default method if non was specified
For your edit:
You have to add the method you want to support to the @RequestMapping annotation for each of your methods. See this for more information. As an example:
@RequestMapping(path = "/", method = RequestMethod.GET)
public String index() {
    return "index.html";
}

Instead of using @RequestMapping with an explicit method you can also use shorthand annotations like @GetMapping and @PostMapping. Using these would reduce your code to someting like this:
@GetMapping("/")
public String index() {
    return "index.html";
}

Comment: That definitely sounds correct. However, visiting http://localhost:8080 will still give me back a 405 error .. The log message (on the server) would be `Request method 'GET' not supported`
Comment: Do I actually need this `index()` endpoint? Shouldn't the application serve that by default? It's said that it looks automatically in `static/`, `public/`, etc in order to look for such content.
Comment: In any case: I just tried `GetMapping("/")` - unfortunately with the same result. :/
Comment: Btw. I have pushed the code to https://github.com/silentsnooc/ema-server
Comment: You do not actually need the mapping if you just want to serve static files. You only need it if you (as an example) want to fill in a template file instead of always serving the same static file. As for your error you have another class `WordEmbeddingRestController` which defines mappings for "/" as well, as "/" (or "" to be exact) is the default path value. Removing the `@RestController` annotation from this class will enable you to `GET` the desired html file.
Comment: Thank you! I'll check this out later this afternoon and let you know! :) A refactoring mistake from my side ..
Comment: Thanks - that was in fact the problem. It works now!


Object oriented programming in C

Question:
Possible Duplicate:
Can you write object oriented code in C?

Hi, can someone point me to a tutorial explain me how  OOP concepts can be implemented in ANSI C:

virtual functions
inheritance
best practice

A book about OOP programming ANSI C would be great too.
Comment: Trying to hammer a square OO peg into a round C hole will result in rewriting C++.  Just use C++ or another language.
Comment: I did not ask *is possible?* but *how to achieve*. And as GTK+, GGlib are done in ANSI C in OOP style, it probably has some right to exist.
Answer:
Objective-C isn't C - it's a different language.
C isn't an object-oriented language. It's possible to do some OO-style stuff, but it's not what it's built for.
Best practices in C are procedural.
Comment: 1. I know that, and the question was not about Objective-C nor did I even note it.
2. That's my point. And I know it was made forprocedural programming, but nobodysaid i onlywant to use it what it was designed for.
3. see 2.
4. If you don't answer myquestion at all, which I think I specified well enough to get the point: I want to know _how_ and not _is it sensible_, so eitherawser my question or just don't answer. This is Hijacking.
Comment: @penguinpower: The reason I said about Objective-C is that one of your tags was [objective-c]. I wasn't saying it wasn't sensible to do OO in C, it's just that it seemed from your question that you didn't realise the distinction. I didn't mean to hijack your question - I was trying to help with the basics.
Comment: Must be a typo, started the thread from my phone which unfortunatly hast autocomplete functionalility for the german language :/ If you felt my comment was offense, it was not supposed to be offense.
Comment: @penguinpower: ok - no worries!
Answer: Here is the link of a book on this topic: http://www.planetpdf.com/codecuts/pdfs/ooc.pdf (Object Oriented Programming in Ansi-C - exacty what you were looking for).
Good luck and have patience. Writing OOP in C is not an easy task, but can be somewhat rewarding if you like extreme things !
Another starting point will be to check GObject: http://library.gnome.org/devel/gobject/ and as @unwind said GTK is a good example of how to use OOP concepts in C. You can also take at GLib, is a nice library that will certainly make your C programming life a lot more easier, it is programmed in OOP manner, and as a plus is portable!
Comment: GTK+ is the UI I want to use and therefore I _want_ to learn how to properly use it. But unfortuanetly i 'd rather like to do it lecure by lecture than reverse engineering which takes a lot more time.
Comment: If you want to use GTK+ you are not only limited to C. There are bindings for other languages, so you can develop GTK applications in: C++, python, vala, C# and even Java.
Comment: I know that too, but I want/need/prefer to reuse code of a existing GPLv2 project written in OOP style C, thats the point, and I am _not_ going to use extern or C++ hackz. In the longterm I see forward to the linux kernel (long long term)
Comment: I'd suggest you to try out [COOP](https://github.com/ShmuelFine/COOP)
It features Classes, Inheritance, Exceptions, unit tests, and more.
Answer: I would recommend looking at the internals of the GTK+ GUI toolkit. It's an object-oriented system written in C, showing off the techniques you're after. It is open source, so there's nothing stopping you from reading it and learning.
Answer: Object-oriented mechanisms aren't defined as features of the C language. You'll have to emulate object-orientation by adding your own logic on top of the procedural nature of C.
I wouldn't recommend applying every aspect of OOP in C. While encapsulation is relatively easy to achieve, implementing inheritance would be really ugly in a language that wasn't made for that.
A good tutorial on that: http://www.planetpdf.com/codecuts/pdfs/ooc.pdf

Object oriented programming in C


Using Apache htaccess and PHP, how can I route requests to non public directories?

Question: I have a web structure like
php/
public_html/
   .htaccess

How can I route anything to
www.mywebsite.com/app/

to my actual php controller
php/App.php

So far I have
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^app/*$ ./php/App.php?*

But this is wrong because

The directory to App is wrong. Not sure how to move to move up to the /php dir
I need to pass all request params (GET and POST) to App.php but I'm not sure how, i tried ?*
Answer: A better and more common approach is to create an index.php in your public_html directory where you include PHP scripts from your php directory which is above the web root.
Good frameworks like Laravel use this approach. Here you can see Laravel's .htaccess.
And you can use GET and POST just you would normally do.
Comment: Isn't this bad? I thought I must keep my PHP out of public. If I put App.php into public_html, and it has includes (with file paths of course) to things like Users.php etc, doesn't that defeat the purpose?
Comment: @user3822370 - no, it doesn't defeat the purpose. The code is included into the index file for execution purposes. So long as your code is above the web root, nothing can access it but the index file. So this is the correct approach. In fact, there is no other way to reference your code, as `.htaccess` cannot see above the directory it resides in.
Comment: Just to clarify something: the use of an index file, in this case, is known as *bootstrapping* as it loads the necessary stuff for your application to run - thus *booting* it. In the Laravel world, the bootstrapper is somewhat complex and uses Autoloading etc.
Comment: Thanks.. so my question still stands then - how do I route all traffic using htaccess? I want all requests to mysite.com/app/ to go to mysite.com/app.php including all post and  get


Is it possible to calculate R-squared on a total least squares regression?

Question: I am using the Deming function provided by Terry T. on this archived r-help thread.  I am comparing two methods, so I have data that look like this:
y  x     stdy   stdx
1  1.2   0.23   0.67
2  1.8   0.05   0.89
4  7.5   1.13   0.44
... ...  ...   ...

I have done my Deming regression (also called "total least squares regression") and I get a slope and intercept. I would like to get a correlation coefficient so I've start calculating the $R^2$. I have manually entered the formula:
R2 <- function(coef,i,x,y,sdty){
    predy    <- (coef*x)+i
    stdyl    <- sum((y-predy)^2)   ### The calculated std like if it was a lm (SSres)
    Reelstdy <- sum(stdy)          ### the real stdy from the data  (SSres real)
    disty    <- sum((y-mean(y))^2) ### SS tot
    R2       <- 1-(stdyl/disty)    ### R2 formula
    R2avecstdyconnu <- 1-(Reelstdy/disty) ### R2 with the known stdy
    return(data.frame(R2, R2avecstdyconnu, stdy, Reelstdy))
}

This formula works and gives me output.

Which of the two $R^2$s makes more sense? (I personally think of both of them as kind of biased.)
Is there a way to get a correlation coefficient from a total least squared regression?

OUTPUT FROM THE DEMING REGRESSION:
Call:
deming(x = Data$DS, y = Data$DM, xstd = Data$SES, ystd = Data$SEM,     dfbeta = T)

               Coef  se(coef)         z            p
Intercept 0.3874572 0.2249302 3.1004680 2.806415e-10
Slope     1.2546922 0.1140142 0.8450883 4.549709e-02

   Scale= 0.7906686
>

Comment: Since a correlation coefficient can be computed without any reference to regression at all, could you clarify what *you* mean by "correlation coefficient"?  I'm trying to figure out whether the Pearson $\rho$ will answer your question or if you are perhaps looking for some kind of "coefficient of determination" to serve as an analog for the least squares $R^2$.  If it's the latter, what are you hoping this analog will tell you?
Comment: I am trying to see how well y correlate with x.

I've added  my output to the question from the deming regression(total least squared regression)

I want to be able to say that the two method give similar results in other words.
Answer: To elaborate on whuber's answer above - Pearson will give you what you want. It determines how well y correlates with x using an approach that is independent of regression model:
$\rho_{X,Y}=\dfrac{cov(X,Y)}{\sigma_{X}\sigma_{Y}}$
gx.rma from the rgr package will do total least squares and calculate Pearson for you (or you can continue with Deming and do it manually).
require(rgr)
set.seed(3)
x<-rnorm(101,mean=2.3,sd=4.2)
x<-x+seq(0,100)
set.seed(3)
y<-rnorm(101,mean=4.9,sd=1.9)
y<-y+seq(6,206,length=101)

rma<-gx.rma(x,y)
rma$corr
[1] 0.9922014

So, the basic answer to your question is, when doing total least squares, forget R-squared and just use Pearson. You can always square that if you want a result between 0 and 1. This will do everything you need.
Having said that, I will elaborate a little as I understand it feels like we should be able to calculate an R-squared equivalent.
First, let's try a normal sum of squares regression on the data using lm. Notice that it gives the same correlation coefficient as Pearson (after square rooting and only worrying about magnitude, obviously).
ols<-lm(y~x)
sqrt(summary(ols)$r.squared)
[1] 0.9922014

This is calculated from the lm model result using the traditional sum of squares approach
$R^{2}=1-\dfrac{S_{res}}{S_{tot}}$
So, provided you use the model given by lm, (Pearson)-squared and R-squared are equivalent.
However, if you use the model from the total sum of squares regression, and try to use the latter equation, you will get a slightly different result. That's obvious because normal and total least squares use different minimisation functions so give models with slightly different gradients and intercepts. (Remember, the first equation will still give the same result as it is looking at the data only.)
This is where I get hung up though. If the two equations give the same result when using the lm model, then surely there must be an equivalent formulation for the latter equation, but when using the total least squares model, which also gives the same result?
I had a quick play around with different approaches using the appropriate minimisation function (as has the poster here: Coefficient of determination of a orthogonal regression), but cannot find a way to do it - if there is a way.
Perhaps we are both getting hung up on the fact that Pearson and R-squared give the same result when using normal least sqaures - and there simply isn't a way to do R-squared on total least squares, which will give the same result as Pearson. But I don't know enough about this to say why not.
Comment: The point is that you can calculate a correlation coefficient between x and y (which you mention) *without the need for any regression at all*. I think you are hung up on the difference of a correlation between the data, and an assessment of the quality of the regression - they are not necessarily the same thing. Pearson^2 gives you the former and, in the case of normal-LS (with lm), is the same as R^2. For orthogonal-LS, they're not. So asking about correlation coeff. and R^2 (especially now you mention std error) are actually two different questions. You might want to rephrase the OP.
Comment: Thanks for your answer but ... I still think the gx.rma is not appropriate for what I am trying to do that's why I've use deming regression. Because I don't get how the gx.rma actually account for the known standard error...But it might be me misunderstanding something here... I'll have to check it out when I get time
Comment: I might not have been clear enought....what I want to do is the Pearson. But what is bugging me is that there's no R^2 for the total least square regression. I use the total least square to adjust my coefficient (slope). But I'll use the Pearson for the correlation as you suggested . Thanks
Comment: I understand, it's something that is bugging me too! I think there should be an equivalent formula to give an R2 for total least squares too, but probably I am not understanding fully. Although, if you just want to adjust your slope, then you could just use the minimisation function that is the basis of total LS in Deming. Try http://mathworld.wolfram.com/LeastSquaresFittingPerpendicularOffsets.html or http://arxiv.org/pdf/math/9805076.pdf. You could also consider principle component analysis with prcomp or princomp - it's basically the same and maybe they kick out something that helps.
Answer: Using the package "mcr"
and using function to generate your deming regression model
yourmodel<-mcreg(x, y, ...) # you need to be familiar with the various types of deming constant SD or CV%. these can give very different results. But that's different question.

and producing a plot using the function
MCResult.plot(your model)

This displays the Pearson's production moment correlation on the plot for the model, which tells you the strength and the direction of the linear relationship between your two x,y variables, but does not give the proportion of the variation that is explained.
Hope that helps.


Regex Get result if found and if not get another result

Question: I'm trying to get the max resolution from m3u8 file.
Here's the sample m3u8 content:
#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=149000,RESOLUTION=320x180,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://URL/index_0_av.m3u8?null=0&id=AgBuNs5OuBzaVwrmOF+jXvWJ+HkjL%2fShsITuRsvq00%2f+IYwrOKyjWNrak9RN+Vq0bDNLLJZ47ZVL1w%3d%3d&hdntl=exp=1597650826~acl=%2f*~data=hdntl~hmac=9a46c5ffe594c611b03e0dfbd8e5981aab024fae8492abc40f0fcd7c854af59a
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=298000,RESOLUTION=320x180,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://URL/index_1_av.m3u8?null=0&id=AgBuNs5OuBzaVwrmOF+jXvWJ+HkjL%2fShsITuRsvq00%2f+IYwrOKyjWNrak9RN+Vq0bDNLLJZ47ZVL1w%3d%3d&hdntl=exp=1597650826~acl=%2f*~data=hdntl~hmac=9a46c5ffe594c611b03e0dfbd8e5981aab024fae8492abc40f0fcd7c854af59a
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=499000,RESOLUTION=640x360,CODECS="avc1.64001f, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://URL/index_2_av.m3u8?null=0&id=AgBuNs5OuBzaVwrmOF+jXvWJ+HkjL%2fShsITuRsvq00%2f+IYwrOKyjWNrak9RN+Vq0bDNLLJZ47ZVL1w%3d%3d&hdntl=exp=1597650826~acl=%2f*~data=hdntl~hmac=9a46c5ffe594c611b03e0dfbd8e5981aab024fae8492abc40f0fcd7c854af59a
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=800000,RESOLUTION=640x360,CODECS="avc1.64001f, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://URL/index_3_av.m3u8?null=0&id=AgBuNs5OuBzaVwrmOF+jXvWJ+HkjL%2fShsITuRsvq00%2f+IYwrOKyjWNrak9RN+Vq0bDNLLJZ47ZVL1w%3d%3d&hdntl=exp=1597650826~acl=%2f*~data=hdntl~hmac=9a46c5ffe594c611b03e0dfbd8e5981aab024fae8492abc40f0fcd7c854af59a
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1062000,RESOLUTION=960x540,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://URL/index_4_av.m3u8?null=0&id=AgBuNs5OuBzaVwrmOF+jXvWJ+HkjL%2fShsITuRsvq00%2f+IYwrOKyjWNrak9RN+Vq0bDNLLJZ47ZVL1w%3d%3d&hdntl=exp=1597650826~acl=%2f*~data=hdntl~hmac=9a46c5ffe594c611b03e0dfbd8e5981aab024fae8492abc40f0fcd7c854af59a
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1361000,RESOLUTION=960x540,CODECS="avc1.77.30, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://URL/index_5_av.m3u8?null=0&id=AgBuNs5OuBzaVwrmOF+jXvWJ+HkjL%2fShsITuRsvq00%2f+IYwrOKyjWNrak9RN+Vq0bDNLLJZ47ZVL1w%3d%3d&hdntl=exp=1597650826~acl=%2f*~data=hdntl~hmac=9a46c5ffe594c611b03e0dfbd8e5981aab024fae8492abc40f0fcd7c854af59a
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1503000,RESOLUTION=1280x720,CODECS="avc1.64001f, mp4a.40.2",CLOSED-CAPTIONS=NONE
https://URL/index_6_av.m3u8?null=0&id=AgBuNs5OuBzaVwrmOF+jXvWJ+HkjL%2fShsITuRsvq00%2f+IYwrOKyjWNrak9RN+Vq0bDNLLJZ47ZVL1w%3d%3d&hdntl=exp=1597650826~acl=%2f*~data=hdntl~hmac=9a46c5ffe594c611b03e0dfbd8e5981aab024fae8492abc40f0fcd7c854af59a

I'm using this regex to get the maximum resolution which is the last URL "https://URL/index_6_av.m3u8?".
(https.*\w*index_6\w*.*)"

My question is, if the "index_6 which is 720p" is not found, how I can get the "index_5" instead so that the result is not empty?
Answer: You may use the following pattern:
^https.*index_\d+.*\Z

Demo.
What's changed:

Replaced the 6 with \d+ which will match one or more digits.

Added \Z (i.e., End of String Anchor) at the end to only match the very last occurrence.

The capturing group is redundant so it was removed.

The two \w* also seem redundant (because they're either followed or preceded by .*). You may add more restrictions if you need them.

Note: The solution above will only work if the indices are in ascending order. It's mentioned in the question that it should match the last URL so I'm assuming they are in order. If that's not the case, you could write something like this instead:
var matches = Regex.Matches(input, @"^https.*index_(\d{1,9}).*$", RegexOptions.Multiline);
if (matches.Count > 0)
{
    var targetMatch = matches
                      .OfType<Match>()
                      .OrderByDescending(m => int.Parse(m.Groups[1].Value))
                      .First();
    string targetUrl = targetMatch.Value;
}


Guide to changing int main()

Question:
Possible Duplicate:
Calling functions from main() in c++

what are some guidlines to changing int main to call other functions in your code I was told not to use int main2(), int main3() and so on also how do I declare them so that the compiler calls them and recognizes them would it be
#include <iostream>
int main()
int main 2()
using namespace std;

int main()
  code here ..... 1st function
int main2()
  code here .... 2nd function

or would it be
#include <iostream>
int anothername()
int seconddifferentname()
using namespace std;

int anothername()
  code here ..... 1st function
int seconddifferentname()
  code here .... 2nd function

Comment: Do you want to know how to name other functions besides "main" itself?
Comment: yes cristian K i do that would be very helpful
Answer: You create new functions and call them from main:
void foo() {}
void goo() {}

int main()
{
  foo();
  goo();
}

There's nothing technically wrong with having main1 or main2 methods, but there's only one entry point of a valid C++ program, and that's main.

Guide to changing int main()


DNS Forwarders based on Source IP Addresses(Bind 9)

Question: Sites A,B  ---> Site C (Win2012R2 DNS Server) --> forwards external DNS queries to our "BIND Server" (where DNS forwarders are set) when surfing the Net.
Our clients in both Site A and Site B relies on Site C's DNS server for internal DNS resolution.
Since Site A and Site B are from different places, I would like to have separate DNS forwarders based on IP addresses of Sites A and B, which are then to be set in the Bind Server to improve the performance.
Is it possible to achieve that? Or are there any workarounds?
Answer: I am not familiar with Win2012R2 DNS Server but with Bind9 you can achieve what you want pretty easily with Bind9 acl + views
Define two acl to match each site based on source address
acl siteA {
    10.1.1.0/24;
    fde1::/64;
};

acl siteB {
    10.2.2.0/24;
    fde2::/64;;
};

Then define two views based on acl and for each view define forwarders you want
view "siteA" {
    match-clients {
        siteA;
    };
    recursion no;
    forwarders {
      22.214.171.124
      2001:4860:4860::8888
    };
};

view "siteB" {
    match-clients {
        siteB;
    };
    recursion no;
    forwarders {
      126.96.36.199
      2001:4860:4860::8844
    };
};

This setup works perfectly, usually relevant for internal / external zones. For example allow recursion for internal clients and act as authoritative name server for external clients
Comment: any other settings required? I have tested the configuration using a client from site B. From the log it shows "view siteB: query (cache) 'xxxxx.local/A/IN' denied." I did a nslookup and "query refused" error occurred. Thanks in advance.
Comment: you are trying to resolve an internal name ```.local```, the zone should be handled by Win2012R2 DNS Server. Is BIND the first server receiving the query ? Do you want to forward the query to windows or do recursion to resolve it ?