SQL Server 2008 - split time intervals given by StartTime, EndTime at selected points

Question: In SQL Server 2008, I have a table containing time intervals in the form of columns StartTime, EndTime. The table also contains some other identifiers. The first 3 rows of this table look like this:
create table #ExampleTable (ID int, PRDATE date, RID int, StartTime DateTime, EndTime DateTime);
INSERT INTO #ExampleTable(ID, PRDATE, RID, StartTime, EndTime) VALUES
(10000422,'2014-09-01',6,'2014-09-01 04:08:15.000','2014-09-01 04:13:13.000'),
(10000457,'2014-09-01',103,'2014-09-01 04:08:23.000','2014-09-01 09:44:47.000'),
(20000432,'2014-09-01',116,'2014-09-01 04:08:51.000','2014-09-01 05:07:38.000');

I would like to split the intervals at the SplitPoints given in other table #SplitPoints,
create table #SplitPoints (SplitPoint DateTime);
INSERT INTO #SplitPoints(SplitPoint) VALUES
('2014-09-01 04:15:00.000'),
('2014-09-01 04:45:00.000'),
('2014-09-01 05:30:00.000');

creating new rows when appropriate. The desired result looks like
create table #DesiredResult (ID int, PRDATE date, RID int, StartTime DateTime, EndTime DateTime);
INSERT INTO #DesiredResult(ID, PRDATE, RID, StartTime, EndTime) VALUES
(10000422,'2014-09-01',6,'2014-09-01 04:08:15.000','2014-09-01 04:13:13.000'),
(10000457,'2014-09-01',103,'2014-09-01 04:08:23.000','2014-09-01 04:15:00.000'),
(10000457,'2014-09-01',103,'2014-09-01 04:15:00.000','2014-09-01 04:45:00.000'),
(10000457,'2014-09-01',103,'2014-09-01 04:45:00.000','2014-09-01 05:30:00.000'),
(10000457,'2014-09-01',103,'2014-09-01 05:30:00.000','2014-09-01 09:44:47.000'),
(20000432,'2014-09-01',116,'2014-09-01 04:08:51.000','2014-09-01 04:15:00.000'),
(20000432,'2014-09-01',116,'2014-09-01 04:15:00.000','2014-09-01 04:45:00.000'),
(20000432,'2014-09-01',116,'2014-09-01 04:45:00.000','2014-09-01 05:07:38.000');

How can I accomlish this?
Answer: Lets make the example data:
DECLARE @ExampleTable TABLE (ID int, PRDATE date, RID int, StartTime DateTime, EndTime DateTime);
INSERT INTO @ExampleTable(ID, PRDATE, RID, StartTime, EndTime) VALUES
(10000422,'2014-09-01',6,'2014-09-01 04:08:15.000','2014-09-01 04:13:13.000'),
(10000457,'2014-09-01',103,'2014-09-01 04:08:23.000','2014-09-01 09:44:47.000'),
(20000432,'2014-09-01',116,'2014-09-01 04:08:51.000','2014-09-01 05:07:38.000');

DECLARE @SplitPoints TABLE (SplitPoint DateTime);
INSERT INTO @SplitPoints(SplitPoint) VALUES
('2014-09-01 04:15:00.000'),
('2014-09-01 04:45:00.000'),
('2014-09-01 05:30:00.000');

Now I make a CTE to get all of the unique dates (both start and end) to order them:
;WITH MyDates AS
(
SELECT ID, PRDATE, RID, StartTime
FROM @ExampleTable
UNION
SELECT ID, PRDATE, RID, EndTime
FROM @ExampleTable
UNION
SELECT et.ID, et.PRDATE, et.RID, sp.SplitPoint
FROM @ExampleTable et
INNER JOIN @SplitPoints sp
    ON sp.SplitPoint >= et.StartTime AND sp.SplitPoint < et.EndTime
)

Now lets put the data in order and create the new records, with the preceding record as the start, and the next record as the endtime till we run out of records for each date/rid set.
SELECT * FROM
(
    SELECT *,
    LEAD(StartTime) OVER (PARTITION BY PRDATE, RID ORDER BY StartTime) AS EndTime
    FROM MyDates
) d
WHERE d.EndTime IS NOT NULL

Here is the output:
ID  PRDATE  RID StartTime   EndTime
10000422    2014-09-01  6   2014-09-01 04:08:15.000 2014-09-01 04:13:13.000
10000457    2014-09-01  103 2014-09-01 04:08:23.000 2014-09-01 04:15:00.000
10000457    2014-09-01  103 2014-09-01 04:15:00.000 2014-09-01 04:45:00.000
10000457    2014-09-01  103 2014-09-01 04:45:00.000 2014-09-01 05:30:00.000
10000457    2014-09-01  103 2014-09-01 05:30:00.000 2014-09-01 09:44:47.000
20000432    2014-09-01  116 2014-09-01 04:08:51.000 2014-09-01 04:15:00.000
20000432    2014-09-01  116 2014-09-01 04:15:00.000 2014-09-01 04:45:00.000
20000432    2014-09-01  116 2014-09-01 04:45:00.000 2014-09-01 05:07:38.000

Comment: If I add e.g. (10000422,'2014-09-01',6,'2014-09-01 06:08:15.000','2014-09-01 07:13:13.000') to @ExampleTable, this needs a little modification - adding column "type" 1 (StartTime), 0(EndTime), -1(SplitPoint) to the three select statements an then filtering out those with type 0. But I can use this with a little change, thanks!
Comment: do you expect records with a duplicate ids and rids?
Comment: Yes, those could be expected. But the (ID, StartTime) pair is unique, and the time intervals for given ID do not overlap.
Answer: I think your required functionality not possible directly from query it need something like looping.
I think below code will help you
DECLARE @ID NUMERIC
DECLARE @PRDDate DATE,
        @rid     NUMERIC
DECLARE @starttime DATETIME
DECLARE @endtime DATETIME
DECLARE @SplitPoint DATETIME

CREATE TABLE #desiredresult
  (
     id        INT,
     prdate    DATE,
     rid       INT,
     starttime DATETIME,
     endtime   DATETIME
  )

 -- declare cursor called firstcursor
  DECLARE firstcursor CURSOR FOR
  SELECT id,
         prdate,
         rid,
         starttime,
         endtime
  FROM   #exampletable
  ORDER  BY id

-- Open the cursor
OPEN firstcursor

-- Fetch the first row of the cursor and assign its values into variables
FETCH next FROM firstcursor INTO @ID, @PRDDate, @rid, @starttime, @endtime

-- perform action whilst a row was found
WHILE @@FETCH_STATUS = 0
  BEGIN
      DECLARE secoundcursor CURSOR FOR
             SELECT splitpoint
                FROM   #splitpoints
                WHERE  splitpoint < @endtime
                ORDER  BY splitpoint
      OPEN secoundcursor
      -- perform action whilst a row was found
      DECLARE @prev DATETIME
      FETCH next FROM secoundcursor INTO @SplitPoint
      DECLARE @index int = 0
      WHILE @@FETCH_STATUS = 0
        BEGIN
            IF( @index =0 )
              BEGIN
                  INSERT INTO #desiredresult
                          (id,
                           prdate,
                           rid,
                           starttime,
                           endtime)
                  VALUES     (@ID,
                          @PRDDate,
                          @rid,
                          @starttime,
                          @SplitPoint)
               END
            ELSE
               BEGIN
                  INSERT INTO #desiredresult
                              (id,
                               prdate,
                               rid,
                               starttime,
                               endtime)
                  VALUES     (@ID,
                              @PRDDate,
                              @rid,
                              @prev,
                              @SplitPoint)
              END

            SET @prev = @SplitPoint
            SET @index = @index + 1
           FETCH next FROM secoundcursor INTO @SplitPoint
        END

      CLOSE secoundcursor

      -- Free memory used by cursor
      DEALLOCATE secoundcursor

      if(@index =0)
      BEGIN
        INSERT INTO #desiredresult
                          (id,
                           prdate,
                           rid,
                           starttime,
                           endtime)
              VALUES     (@ID,
                          @PRDDate,
                          @rid,
                          @starttime,
                          @endtime)
      END
      else
          begin
              INSERT INTO #desiredresult
                          (id,
                           prdate,
                           rid,
                           starttime,
                           endtime)
              VALUES     (@ID,
                          @PRDDate,
                          @rid,
                         @prev,
                          @endtime)
      end

      -- get next row of cursor
      FETCH next FROM firstcursor INTO @ID, @PRDDate, @rid, @starttime, @endtime
   END

 -- Close the cursor to release locks
 CLOSE firstcursor

 -- Free memory used by cursor
 DEALLOCATE firstcursor

 SELECT *
 FROM   #desiredresult

Answer: By utilizing a UNION SELECT you may be able to accomplish what you are looking for I did a quick and dirty query to show how the dates can be pulled the way I thought you were looking to do this. The trickiest part in this was the Start_Date in the last select because unless you want the logic I put in the query you will need to adjust to fit your needs. I also added a splitstart and splitend time to your table this makes the union possible once again not sure if this is possible for you but thought I would try to help if I could.
DECLARE
    @Start_Date DATETIME = '2014-09-01 04:08:23.000'
    ,@End_Date DATETIME = '2014-09-01 09:44:47.000'

    create table #SplitPoints (SplitPointStart DateTime,SplitPointEnd DateTime);
    INSERT INTO #SplitPoints(SplitPointStart,SplitPointEnd) VALUES
    ('2014-09-01 04:15:00.000','2014-09-01 04:45:00.000'),
    ('2014-09-01 04:45:00.000','2014-09-01 05:30:00.000'),
    ('2014-09-01 05:30:00.000','2014-09-01 06:00:00.000');

    SELECT
            @Start_Date
    ,CASE
    WHEN DATEPART(m,@Start_Date) <= 15 THEN CAST(CAST(GETDATE() AS DATE)AS VARCHAR) + ' '+ CAST(DATEPART(hh,@Start_Date)AS VARCHAR) + ':15.000'
    WHEN DATEPART(m,@Start_Date) <= 30 AND DATEPART(m,@Start_Date) > 15 THEN CAST(CAST(GETDATE() AS DATE)AS VARCHAR) + ' '+ CAST(DATEPART(hh,@Start_Date)AS VARCHAR) + ':30.000'
    WHEN DATEPART(m,@Start_Date) <= 45 AND DATEPART(m,@Start_Date) > 30 THEN CAST(CAST(GETDATE() AS DATE)AS VARCHAR) + ' '+ CAST(DATEPART(hh,@Start_Date)AS VARCHAR) + ':45.000'
    WHEN DATEPART(m,@Start_Date) <= 60 AND DATEPART(m,@Start_Date) > 45 THEN CAST(CAST(GETDATE() AS DATE)AS VARCHAR) + ' '+ CAST(DATEPART(hh,DATEADD(hh,1,@Start_Date))AS VARCHAR) + ':00.000'
    ELSE ''
    END
    UNION
    SELECT
    SplitPointStart,SplitPointEnd
    FROM #Splitpoints
    WHERE
    SplitPointStart > @Start_Date
    AND SplitPointEnd < @End_Date
    UNION
    SELECT
    CASE
    WHEN DATEPART(m,DATEADD(mm,-30,@End_Date)) <= 15 THEN CAST(CAST(GETDATE() AS DATE)AS VARCHAR) + ' '+ CAST(DATEPART(hh,@End_Date)AS VARCHAR) + ':15.000'
    WHEN DATEPART(m,DATEADD(mm,-30,@End_Date)) <= 30 AND DATEPART(m,@End_Date) > 15 THEN CAST(CAST(GETDATE() AS DATE)AS VARCHAR) + ' '+ CAST(DATEPART(hh,@End_Date)AS VARCHAR) + ':30.000'
    WHEN DATEPART(m,DATEADD(mm,-30,@End_Date)) <= 45 AND DATEPART(m,@End_Date) > 30 THEN CAST(CAST(GETDATE() AS DATE)AS VARCHAR) + ' '+ CAST(DATEPART(hh,@End_Date)AS VARCHAR) + ':45.000'
    WHEN DATEPART(m,DATEADD(mm,-30,@End_Date)) <= 60 AND DATEPART(m,@End_Date) > 45 THEN CAST(CAST(GETDATE() AS DATE)AS VARCHAR) + ' '+ CAST(DATEPART(hh,DATEADD(hh,-1,@End_Date))AS VARCHAR) + ':00.000'
    ELSE ''
    END
    ,@End_Date

    drop table #SplitPoints


How do you mock a function call that called multple times with changing parameters in PHPSpec?

Question: I'm trying to create a test in PHPSpec for an function that calls a function on another object with differing parameters. So far, my attempts have caused several different errors so I'll outline what I have so far.
The most recent error:
- it should find all realm data
  method call:
    - fetch(LeagueOfData\Adapters\Request\RealmRequest:000000001212f67d000000001262e5c6 Object (
      'apiDefaults' => Array &0 (
          'region' => 'euw'
      )
      'format' => null
      'data' => null
      'query' => null
      'where' => Array &0
  ))
  on Double\AdapterInterface\P51 was not expected, expected calls were:
    fetch(exact(Double\RequestInterface\P50:000000001212f607000000001262e5c6 Object (
      'objectProphecy' => Prophecy\Prophecy\ObjectProphecy Object (*Prophecy*)

The PHPSpec file:
class JsonRealmsSpec extends ObjectBehavior
{
    function let(AdapterInterface $adapter, LoggerInterface $logger, RequestInterface $request)
    {
        // fetch called with multiple request objects but we're not interested in the exact data it returns yet.
        $adapter->fetch($request)->willReturn(['test data']);
        $this->beConstructedWith($adapter, $logger);
    }

    function it_should_find_all_realm_data()
    {
        $this->findAll()->shouldReturnArrayOfRealms();
    }

    function getMatchers()
    {
        return [
            'returnArrayOfRealms' => function($realms) {
                foreach ($realms as $realms) {
                    if (!$realm instanceof Realm) {
                        return false;
                    }
                }
                return true;
            }
        ];
    }
}

And the actual function being tested:
class JsonRealms implements RealmService
{
    const REGIONS = ['euw', 'eune', 'na'];

    private $source;
    private $log;
    private $realms;

    public function __construct(AdapterInterface $adapter, LoggerInterface $log)
    {
        $this->source = $adapter;
        $this->log = $log;
    }

    public function findAll() : array
    {
        $this->realms = [];
        foreach (self::REGIONS as $region) {
            $request = new RealmRequest(['region' => $region]);
            $response = $this->source->fetch($request);
            $this->realms[] = new Realm($realm['cdn'], $realm['v'], $region);
        }
        return $this->realms;
    }
}

I'm sure I'm probably missing something really obvious but for the life of me I cannot see it right now.
Comment: Problem here is not that you are calling the function multiple times in SUS (system under spec; your class). The problem is that you are creating it inside that class whereas @ spec level you are asserting that you expect a call with the collaborator (and it is not the same object (type) and has not the same values). Take a look to my [answer](http://stackoverflow.com/questions/42307939/how-to-get-property-from-stub-function-argument) for a similar question.
Comment: Hi, I'm not certain I entirely understand your answer. The function I'm calling multiple times isn't what I'm testing. I'm merely mocking it. I'm testing if the findAll() function returns an array of Realm objects. This is from some refactoring where I've changed the function from fetching a single realm, to fetching all of them. I have solved the issue myself though and will add the answer.
Answer: So it turns out I was missing something obvious, I was trying to solve it with a single mock call, as opposed to one for each case:
function let(AdapterInterface $adapter, LoggerInterface $logger) {
    $request = new RealmRequest(['region' => 'euw']);
    $adapter->fetch($request)->willReturn([
        'cdn' => 'http://test.url/euw',
        'v' => '7.4.3'
    ]);
    $request = new RealmRequest(['region' => 'eune']);
    $adapter->fetch($request)->willReturn([
        'cdn' => 'http://test.url/eune',
        'v' => '7.4.3'
    ]);
    $request = new RealmRequest(['region' => 'na']);
    $adapter->fetch($request)->willReturn([
        'cdn' => 'http://test.url/na',
        'v' => '7.4.3'
    ]);
}

This appropriately sets up the mock adapter so that we can test the service is correctly creating the Realm objects.


How to load new profile in Network manager v0.9 without nm-connection-editor using cli?

Question: i'm working on migration of autoprovisioned Ubuntu 12.04 LTS servers from ifup (/etc/network/interfaces) to NetworkManager. /etc/network/interfaces is clean (only loopback interface is configured):
<code># cat /etc/network/interfaces
auto lo
iface lo inet loopback
</code>
In this scenario, NM list one 'Wired connection 1':
<code># nmcli c list
NAME                      UUID                                   TYPE              TIMESTAMP-REAL
Wired connection 1        e96b8486-3ea8-4f20-bd5a-2d532c2cf90a   802-3-ethernet    Wed 06 Aug 2014 03:13:29 PM CEST
</code>
/etc/NetworkManager/system-connections/ is empty:
<code># ls -la /etc/NetworkManager/system-connections/
total 8
drwxr-xr-x 2 root root 4096 Aug  6 15:11 .
drwxr-xr-x 5 root root 4096 Aug  6 11:37 ..
</code>
When i generate new profile and store it in /etc/NetworkManager/system-connections/ i'm not able to persuade NM to register/load this profile:
<code># cat /etc/NetworkManager/system-connections/DHCP
[802-3-ethernet]
duplex=full

[connection]
id=DHCP
uuid=25b9bcf8-1534-411d-8cb6-a3d4fcf33f5a
type=802-3-ethernet
timestamp=1407331036

[ipv6]
method=auto

[ipv4]
method=auto

# service network-manager restart
network-manager stop/waiting
network-manager start/running, process 1918

# nmcli c list
NAME                      UUID                                   TYPE              TIMESTAMP-REAL
Wired connection 1        df785c8f-67e6-44c0-8434-2ca2b96d6408   802-3-ethernet    Wed 06 Aug 2014 03:27:16 PM CEST
</code>
I'm able to create new profile only using nm-connection-editor, but we can't use this method in autodeployments. Can anyone suggest, how to accomplish import and successful load of manually created NM profile into the NM?
Thanks in advance
Comment: Wouldn't it be easier to just use `/etc/network/interfaces` instead? In my humble opinion Network Manager is just extra complexity here.
Comment: definitely would! but it's an application team requirement, devs needs to edit network profiles and want to avoid parsing /etc/network/interfaces as it's not well parsable (nor want to use augeas for parsing and its ruby bindings) so trying to find a way how to use NM and its .ini style profiles... + need to allow app operators access to nm-connection-editor instead of manually editing the interfaces file
Answer: Finally i found a way, how to succeed to import a NM profile with NM v0.9.4.0 installed  - add-connection.py example script in network-manager repo
http://cgit.freedesktop.org/NetworkManager/NetworkManager/tree/examples/python/dbus/add-connection.py
you only need to ensure 'python-dbus' (Debian/Ubuntu) or 'dbus-python' (RHEL clones) package is installed and everything work like a charm! :)


httpPost Method

Question: I am creating httpClient and when I send httpPost method, how can I attach a body to the httpRequest ??
 public String httpPost(String URL, String BODY) {

    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(URL);

    try {

        response = httpclient.execute(httpPost); // get response from executing client

        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == HttpStatus.SC_OK) {
            body.append(statusLine + "\n");
            HttpEntity e = response.getEntity();
            String entity = EntityUtils.toString(e);
            body.append(entity);

        } else {
            body.append(statusLine + "\n");
            // System.out.println(statusLine);
        }
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        httpPost.releaseConnection();
    }
    return body.toString();
}

For example, the string is
     "   Header   I am body   "
Where do I attach that string to the request message ?
Thank you :)
Answer: Have you tried httpPost.setEntity("   Header   I am body  ") before your try-catch?
I'm not entirely sure what "entity" refers to, but that's the best I can come up with from looking at the documentation here
Comment: setEntity method takes Entity only ... I am wondering how to parse a string into an entity..
Answer: You can create a StringEntity, set it to the HttpPost object, and set the correct Content-Type:
StringEntity entity = new StringEntity("data=" + java.net.URLEncoder.encode(body, "utf-8"));
entity.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(entity);

And then send your POST request as usual.

httpPost Method


How to write code just for debug mode in MFC vs2005

Question: I want to define some member variable and some code just in Debug Mode,
When change to Release mode, they will not appear.
I know I can use #ifdef xxx to do this task.
My question is : is there any handy macro provided by MFC to do this.
Answer: <code>#ifdef _DEBUG
</code>
Visual Studio defines the _DEBUG macro when building Debug flavor of your application.


Multivariate series expansion in sympy

Question: Does anyone know if there is a built-in function in sympy to obtain a multivariate series expansion of the form
f(x,y) = a + b*x + c*y + d*x**2 + e*x*y + f*y**2 + ...

i.e. by ascending order in all variables?
Thanks in advance.
Comment: What do you start with (a polynomial, some other (analytic) function)?
What kind of output do you expect? (A polynomial, a list of monomials, something different?)
Comment: Hi Piotr, I am interested in power series expansions of any multivariate function, as a multivariate variant of "series":
Comment: I had meant to continue with an example: something like (exp(x)*exp(y)).series() giving 1 + x + y + x**2/2 + x*y + y**2/2 + ...
Answer: It is maybe too late but here is what I would do.
It is not exactly a builtin function but it does the job.
The idea is to introduce a temporary variable (eps) using substitutions and to expand the series over it.
Here is an example:
import sympy
x, y , eps = sympy.symbols('x y eps')
f = sympy.exp(x-y)
f.subs(x,x*eps).subs(y,y*eps).series(eps).removeO().subs(eps,1)

Note that using this technique you can have "asymmetric" expansions in x and y.
For instance:
f.subs(x,x*eps).subs(y,y*eps**2) ...
Answer: The short answer is that currently (sympy build 0.7.5), there is no built-in function in sympy that will handle multivariate series expansions.
There appears to be support for series expansion of multivariate functions in one variable only. You can see this in the docstring of _eval_nseries in the function documentation here.  If this is important to you, you can comment on the Issue Tracker or join the mailinglist.
So, to be clear, this works:
In [1]: import sympy as sp
In [2]: x, y = sp.symbols('x,y')
In [3]: g = sp.exp(-x*y)
In [4]: g
Out[4]: exp(-x*y)
In [5]: g.series(x, 0)
Out[5]: 1 - x*y + x**2*y**2/2 - x**3*y**3/6 + x**4*y**4/24 - x**5*y**5/120 + O(x**6)
In [6]: g.series(y, 0)
Out[6]: 1 - x*y + x**2*y**2/2 - x**3*y**3/6 + x**4*y**4/24 - x**5*y**5/120 + O(y**6)

but there is none of your desired functionality in any of the following:
In [7]: g.series((x, y), (0, 0))
Out[7]: exp(-x*y)

In [8]: g.series((x, 0), (y, 0))
Out[8]: exp(-x*y)

In [9]: g.series(x, 0, y, 0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-21-20c1ab732928> in <module>()
----> 1 g.series(x, 0, y, 0)

/usr/lib/python2.7/dist-packages/sympy/core/expr.pyc in series(self, x, x0, n, dir, logx)
   2401                 return self
   2402
-> 2403         if len(dir) != 1 or dir not in '+-':
   2404             raise ValueError("Dir must be '+' or '-'")
   2405

TypeError: object of type 'int' has no len()

In [10]: g.series(x, y, 0, 0)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-32b57736cd3d> in <module>()
----> 1 g.series(x, y, 0, 0)

/usr/lib/python2.7/dist-packages/sympy/core/expr.pyc in series(self, x, x0, n, dir, logx)
   2401                 return self
   2402
-> 2403         if len(dir) != 1 or dir not in '+-':
   2404             raise ValueError("Dir must be '+' or '-'")
   2405

TypeError: object of type 'int' has no len()

Multivariate series expansion in sympy


Why is jQuery not working in the browser action popup of a google chrome extension?

Question: I've created a button whose function is to hide itself when clicked. But, it isn't working.Here's the code :

<button id="b">HIDE</button>

What's wrong with it?
Comment: [Works for me: JS Fiddle demo](http://jsfiddle.net/davidThomas/89ewe/).
Answer: That should work, but I'd strongly suggest you research the HTML for a valid document, i.e.  must appear before .
Answer: Along with what alex said, it's possible that Chrome is blocking your request to the CDN-hosted jQuery. You can either give that domain valid permissions in manifest.json or simply download the copy of jQuery and store it locally.
Answer: If you are using SSL for your site, then you should serve your javascript/js file via https, else chrome will block it and causing your site's feature that is using that script not working.
I noticed that you are using http to call jquery from google cdn. May be it is causing that problem.


Email attachment is not working in Vtiger Custom Code for email

Question: I am trying to send attachment in email using sen_email() of vtiger but it's not working.
I am using this library: modules/Emails/mail.php"
and send email function like this:
I am getting email using this function but the attachment is not working.
Can anyone help me here?
$attachment = "test/logo/navlogo.png";
send_mail('Potentials', "toemail id",'', "from email id", "test", "testtt","","",$attachment);

Comment: What do you mean bu not working? Are there any errors? Is the email sent without an attachment?
Comment: It is not showing any error, Email not receiving the attachment.
Answer: You may write custom function instead of using default function of send_email
Your custom function will look like
//In params pass necessary details like toName, fromName, emailBody, fileurl etc

 public function send_custom_email($params) {
        global $adb;

        $mail_body      = html_entity_decode($params['mail_body']);
        $serverSql      = 'SELECT server, server_username, server_password, smtp_auth  FROM  vtiger_systems WHERE server_type = ?';
        $result         = $adb->pquery( $serverSql, array('email') );
        $mailserver     = $adb->query_result( $result, 0, 'server' );
        $mailuname      = $adb->query_result( $result, 0, 'server_username' );
        $mailpwd        = $adb->query_result( $result, 0, 'server_password' );
        $smtp_auth      = $adb->query_result( $result, 0, 'smtp_auth' );

        $mail           = new PHPMailer();
        $initialfrom    = $from;
        $mail->Subject  = $params['subject'];
        $mail->Body     = $mail_body;
        $mail->Host     = $mailserver;
        $mail->IsHTML(true);
        $mail->IsSMTP();

        if ( $smtp_auth == 'true' )
            $mail->SMTPAuth = true;
        else
            $mail->SMTPAuth = false;

        $mail->Username = $mailuname;
        $mail->Password = $mailpwd;
        $mail->From     = $from;
        $mail->FromName = $initialfrom;

        $mail->AddAddress($to);
        $mail->AddReplyTo($from);

        // add attachment
        $file_name      = basename($params['file_path']);
        $file_to_attach = $params['file_path'];
        $mail->AddAttachment($file_to_attach, $file_name);

        if ( !$mail->Send() ){
            return false;
        }

        return true;
    }


How can I a revert an overridden template to its default in a sub theme?

Question: If a base theme overrides a certain template, how can I tell a sub theme to revert to the default template instead of using the overridden template?
Of course I could simply copy the default template to the sub theme's templates directory, but that's not an ideal solution because it duplicates code and one would not take advantage of future (security?) updates in the default template.
My ideal solution would be smart enough to undo the template override provided by the base theme, but still allow for other overrides on the module level.
Comment: It's very hard to give specifics without more details, but I'm pretty sure you can use [`hook_theme_registry_alter()`](https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme_registry_alter/7) to do that
Answer: I don't know if there is a perfect solution to do that without any inconvenient. Here are two proposals :

Altering the Theme Registry :
To prevent the base theme from overriding a specific template, an idea would be to alter the theme registry. The theme registry array is keyed by theme hook name, and contains informations returned from hook_theme().
As Clive suggested above, you can achieve this by implementing hook_theme_registry_alter(). Here an example of how to revert theme overrides to preserve the original "node" template :
function example_theme_registry_alter(&$registry) {
  // Default template file path we want to use (relative to the Drupal root directory).
  $default = 'modules/node';

  // In Drupal 6, we can retrieve default template path in 'theme paths' array. The 1st key should
  // contain it, however 3rd party code might already have altered the registry...
  // $default = $registry['node']['theme paths'][0]; -> should be 'modules/node'

  // Reassign the template file path and theme path accordingly.
  $registry['node']['path'] = $default;
  $registry['node']['theme path'] = $default;

  // Reassign the name of the template file if needed (could have been overriden too).
  $registry['node']['template'] = 'node';

  // You may also need to unset some preprocess functions to protect template variables as well.
  unset ($registry['node']['preprocess functions'][11], ... , ... );
}

This hook is executed as the theme cache is rebuilt, you will need to clear it to see the changes.
This solution still "allows for overrides" on the module/theme level. However, since this code will always hook after modules and themes, you will probably need to bypass it every time you expect a change (e.g. brought by a new overriding template), just to be sure that what you see is what you expect, and to know which theme do what in the rendered page. So, if allowing module/subtheme overrides while preventing base theme overrides is required, you may want to try the 2nd solution.

Bypassing specific theme overrides :
This is not the clean way but according to your needs and how specific it is, you may prefer to directly "tweak" your base theme :

Templates : prevent override by renaming the theme template file : in the node example, one would rename node.tpl.php to bypass.node.tpl.php (or whatever).
Preprocessing : comment out preprocess functions in the base theme's template.php, e.g: themename_preprocess_node()
CSS, JS, ... : depending on how well your theme is structured, it can be easy or simply horribly boring to comment out the "overriding" part in these files.

The inconvenient here is that you'll have to redo these 3 steps every time you update or change the base theme, but until such a case happens, this will make it easier for modules and subthemes to manage overrides without messing with the theme registry.
Comment: Thank you. You answer more or less confirms what I was expecting. Both methods are hackish (no offense to you, just an observation) and it's hard, if not impossible, to unset a specific template override without touching another part of the system in the process.
Comment: I managed to test both methods in a reasonable time and they works but I agree with you : it's hackish! Eventually one would prefer simply copy the default template to the sub theme's directory, as you suggested first, but in that case theme js/css would  still apply.. I'm afraid there is no proper solution here.
Comment: @marcvangend in my opinion its not hackish. the point of `hook_theme` and sub-themeing is to provide a mechanism these types of overriding. with regards to JS at least your subtheme can use drupal hooks to unset JS easily on certain urls for example.
Comment: @tenken I call this hack*ish* because there is no really clean way to control the priority of templates. I cannot tell Drupal "do not use template X but use whatever had the highest priority before that". The first methods requires intimate knowledge of template names. It does not really "undo" a certain override, but rather overrides the override with a hard coded value. The second method modifies contrib code, which is pretty similar to hacking core.
Comment: @n0tting I'm not marking this as accepted answer because it's not a complete solution to the problem. I did award you the +50 bounty because the answer was  helpful in understanding the essence of the problem.
Comment: @marcvangend Ok.. but how could one override a _certain_ template without having to mention it somewhere? Applying a template override for a specific need is just about settings (hard code if you want). The first proposal is the way to go, just hook into the theme registry alter function and apply your settings if you need them, that's all.
Comment: I guess it could be similar to how template suggestions work: you would have an array of template names, with the template with highest priority on top. An alter hook could then take out one specific template without touching the rest of the stack. The alter hook would not need any knowledge about the other template names.
Comment: @marcvangend hook_theme_registry_alter() is precisely designed for that purpose, have you tried the 1st proposal?? "An alter hook could then take out _one_ _specific_ _template_ without touching the rest of the stack" -> Given the _specific_ example (_node_ template) : you just need to know which template file you want to use ('modules/node'), you do not need any knowledge about other template names (there is not any other template mentioned in this working example).


trying to remove everything above a certain line


Can anyone confirm or discredit the term "Prime Scales" when dealing with the modern modes

Question: http://www.tonalcentre.org/Scales.html
In the linked webpage the author speaks of "prime scales" and says "Remarkably enough, out of all possible scales there are only five prime scales, in which every single note is a member of at least one major or minor triad and which contain no chromatic semitones. All of these scales contain seven notes." I havent been able to find this term anywhere else so I question the credibility of the page but I also wonder if there is anything to this or any knowledge I can draw about modes from this statement. Is there a (for lack of a better term) a best list of scales to create modes from?
Comment: I can't follow this theory, but I'd need more time to read it through. ;)
Answer: There is a section of that page called "what is this site for?"
http://www.tonalcentre.org/index.html#what%20is%20site%20for

...My theoretical style is unconventional and some of the concepts I introduce are quite novel...

I did quick searches at Google Books and Grove's Online and didn't find prime scale, just like you couldn't find it. I think this is maybe one of the author's unconventional concepts.
But I wouldn't necessarily say this discredits the idea. The author does say up front that he is presenting unconventional ideas.
Looking at scales from a linear and chord perspective is common: the octatonic scale is often described as two superimposed diminished seventh chords. The melodic minor is two superimposed dominant seventh chords. That prime scale idea is worded differently, but the idea is the same: creating scales from superimposed chords. In the case of prime scale the author only allows for major and minor triad chords.
You might be interested in scales produced from combinations of tetrachords (major, minor, phrygian, and harmonic tetrachords.) It is another common way to describe scale generation, and I think all the various combinations will result in prime scales or one of their modes.
Regarding terminology: scale and mode. There is the meaning of mode for the old Church modes. But there is also a usage that means the various rotations of a scale: Dorian is the second mode of the major scale, or the altered scale is the seven the mode of melodic minor, etc.
Comment: Discredit was a poor choice of words I should have said disprove . I was just wondering if this was an outlandish Idea or if there was somthing I could tak from it an apply to my own underatanding. What honestly got me here is I was told you can create modes from any scale available but then someone else rebutted why would you want to just because you can because you wont always get somthing musically usable. This got me thinking if there were any preffered scales or common scales most people used  to get common modes.
Comment: I just made an edit. Look at my last line about modes of a scale. Some of those modes or various scale are important enough to have names, other aren't.
Answer: I'd honestly say it's a bit of hokum.  "Prime scale" is not a standard theoretical term (to my knowledge), and the various associations of "prime" in theory don't really accord with anything this author is talking about.
But the real problem comes in the claim quoted in the question:

Remarkably enough, out of all possible scales there are only five
  prime scales, in which every single note is a member of at least one
  major or minor triad and which contain no chromatic semitones. All of
  these scales contain seven notes. [Bolding in original.]

This is the significant claim of the page, but it's false.  One could choose just about any six-note or five-note subset of many diatonic collections and achieve a scale that satisfies the conditions of the author.  The "no chromatic semitone" restriction effectively rules out scales with more than seven notes.
And that's an arbitrary restriction too: why is it "superfluous" to be able to build both a major and minor tonic triad on the same note of the scale, but only if that note is the root?  It's certainly allowed (by the author) to have notes in the scale that could function in two or three different triads, so why does it matter so much that you can't do that in a case where the triads have the same root?  If the author is indeed interested in "harmonic resources," why are some added triadic possibilities good, but others are "superfluous"?
So, having jettisoned scales with 8+ notes with an arbitrary constraint that isn't given adequate justification, the author then proceeds to ignore 6-note, 5-note, and fewer scales that satisfy his constraints, without explanation.  Why?
I'll grant that most of those scales with fewer notes don't satisfy his criterion of being a "proper mode" (a rather complicated constraint), but neither does the "double harmonic" scale he discusses and admits is not "proper."  And it's even possible to find "proper" scales with fewer notes, for example the diatonic collection minus the leading tone, e.g., C-D-E-F-G-A.  That satisfies all his constraints, including being "proper" (in his terminology).
And it's not even that he's tacitly excluding scales with fewer than seven notes: he explicitly earlier makes reference to the pentatonic scale as having importance.  So, it's simply not true that "out of all possible scales," there are only five that satisfy his conditions.
(And honestly, I can't be bothered to check if his claims are true even for heptatonic scales.  It seems reasonable that the five he quotes make sense, but I'm not convinced they are the only ones without checking.  Part of it may be in the sleight of hand from where he goes from the "no two chords with same root" criterion to the "no chromatic semitones" criterion, which I'm not sure are exactly the same thing, even though he claims they are.  Actually, I lie.  I just tried to make a scale, and C-C#-D-Eb-F#-Ab-A is a seven-note scale that satisfies his "prime" conditions in that no notes are "superfluous" and no note can serve as the root of both a major and minor triad, but nevertheless has chromatic semitones.  There are likely others.  The more one digs, the worse this whole business gets.)
When the big bolded claim turns out to not only have arbitrary constraints to get there but is also literally false, it's probably time to find a better theory source.  I mean, you can create whatever constraints you want to for preferring certain scales, however arbitrary.  But we should at least demand self-consistency in a theoretical system.
Lastly, as to the final part of the question and what "scales to create modes from": that's up to you to figure out what you want in a scale.  If you like the author's constraints and what scales the theory puts out, why not use those scales?  There are no abstract "rules" for how to create scales or modes, other than your own compositional priorities.
Comment: Exactly the answer I was looking for. I just wanted to find someone knowledgeable enough about this to tell me if his system had any truth to take away from it.
Answer: I had never heard the term "prime scales" before, but the article seems quite advanced. The author describes the 3 things he wants from a scale (melodic resource, harmonic resource and tonally effective) and then he goes on to explain what scales we can derive from those principles. That's more than what you usually get when authors describe modes.
Quoting from the article, the 5 scale are:

the diatonic
the harmonic minor
the harmonic major
the melodic
the double harmonic

And by following the links in the article you find all the modes that are possible from each.
I'm not in a position to criticize the term 'prime scales', but the author seems to be  following some very specific rules. I guess that he's using a more "classic" approach to music.
In the real world you can hear some scales that don't follow these rules. An example (and one of my favourite!) is the major blues scale, which is major and minor, ionian and mixolydian at the same time, and it also includes a b5 for extra fun.

Is there a (for lack of a better term) a best list of scales to create modes from?

If you want to use the most common modes, I had only heard of the 7 modes of the diatonic plus the harmonic and melodic minor scales.
If you want to create your own, it depends on whether you want to follow the 3 requirements mentioned by the author; if you do, I would follow the author's guidelines. If you don't, then you can pick and mix from the chromatic scale in any way you find aesthetically pleasing.


NFS: What is remote locking, and do I need it?

Question: I'm building a somewhat minimalistic system that collects serial port data and shoves it into a logfile on an NFS mount.
In an effort to reduce the system somewhat I decided to disable RPC as I couldn't see the use for it, and this resulted in the following warning:
mount.nfs: rpc.statd is not running but is required for remote locking.
mount.nfs: Either use '-o nolock' to keep locks local, or start statd.
mount.nfs: an incorrect mount option was specified

No suprise that RPC was involved in NFS, really, so my next step is to remove the warning. I have the choice of either re-enabling RPC, or use -o nolock as suggested.
So the question is then, what does remote locking do, exactly?
In this particular case, the NFS mount is only written to by the machine in question, and there is no potential for conflicts such as multiple processes writing to the same file. (although, there are several processes writing to each their own logfile)
Running debian buster, kernel 4.19
EDIT: I tried -o nolock, and things seem to be working fine, so at first glance, there are no adverse effects.
Answer: The answer lies in the good old man page of rpc.statd:

File locks are not part of persistent file system state.  Lock
state is thus lost when a host reboots.
Network  file systems must also detect when lock state is lost
because a remote host has rebooted.  After an NFS client    reboots,
an NFS server must release all file locks held by applications that
were running  on  that  client.   After  a    server reboots, a client
must remind the server of file locks held by applications running on
that client.
For NFS version 2 [RFC1094] and NFS version 3 [RFC1813], the
Network Status Monitor protocol (or NSM for short) is used    to
notify NFS peers of reboots.  On Linux, two separate user-space
components constitute the NSM service:
rpc.statd
A daemon that listens for reboot notifications from other hosts, and manages the list of hosts  to  be  notified when the local system reboots
sm-notify
A helper program that notifies NFS peers after the local system reboots

So basically, rpc.statd running on the NFS server prevents processes running on other NFS clients from accessing files you've locked from your client machine. As long as there's only one client, local locks are fine. But usually NFS is all about sharing.


What is the state-of-the-art regarding sampling from discrete distribution?

Question: After struggling with auto-Poisson model (a.k.a. Random Markov Network with conditional Poisson distributions) trying to force Gibbs sampler to obtain discrete sample of the network (since I know conditional distributions) I realized that I have never seen discrete version of this sampler.
I wonder what is the state-of-the-art regarding sampling from discrete distribution (not confined to Bayesian posterior sampling)? Is there something like Gibbs sampler to sample from conditional discrete distributions?
Comment: Gibbs sampling can be used for discrete distributions as well, under appropriate considerations. This in mentioned, for instance, in the [wikipedia entry](http://en.wikipedia.org/wiki/Gibbs_sampling#Implementation), "Implementation" section.
Answer: This is a very puzzling question: the Gibbs sampler draws its name from Gibbs random fields where it was originally used by the Geman brothers to generate realisations from such discrete models. For instance the Ising model is made of a vector of random variables each taking values in $\{-1,1\}$...
So indeed the Gibbs can be used for discrete models. And I do not understand why you have to force your "Gibbs sampler to obtain discrete samples". If you are using the proper full conditionals they should be on a discrete support.
Comment: Thank you Xi'an. I was just a bit confused and had some misunderstanding about Markov random fields. But know, thanks to your answer here and comment at my another question, all this seems to make perfect sense.


Special kind of a linear Linear Diophantine equation

Question: Could any one help me to point out some literature/ papers which solves a homogenous linear Diophantine  equation (one equation) of the form
$a_1 \times x_1+a_2 \times x_2 + a_3 \times x_3+....+a_n \times x_n=0$, where $a_1,a_2,...,a_n$ are positive or negative integer constants and $x_1,x_2,...,x_n$ are positive integer variables.
Such an equation may have no solution or  infinite number of solutions however there must exist be some minimal solutions or a set of base solutions which can be used to derive ALL other solutions of the equation.
1) Could you point out some work/literature which tries to find the set of  base solutions of the above equation.
Thanks in advance.
Comment: Reduce to the case where the $x_i$ are coprime and go from there.
Answer: Try this one on Google,
Parametric Solution of Linear Homogeneous Diophantine Equations by Wallace Givens.
Comment: Huh.  I think I reviewed this too hastily.  Thanks for mentioning that.  (I'll delete the above comment.)
Comment: Heh, I just saw your Comment on another Q about Google not working in your location, so I've deleted my Comment as well.
Comment: Please try to describe as much here as possible in order to make the answer self-contained. Links are fine as support, but they can go stale and then an answer which is nothing more than a link loses its value.


I'm using selenium undetected-chromedriver and I can't open multiple profiles at once and navigate through them

Question: it only works for first profile and for others it just creates and open them with no actions. here is my code
import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options
import time

open_from = 14
open_to = 16
chrome_debugger_base_port = 9000

for i in range(open_from, open_to + 1):
# create a new Chrome profile
    chrome_options = Options()

    chrome_options.binary_location = "C:\\Users...\\Chrome-bin\\chrome.exe"
    chrome_options.add_argument(r"--user-data-dir=C:\\Users\\..\\Profiles\\GoogleChromePortable\\Data")
    chrome_options.add_argument(f"--profile-directory=Profile {i}")

    # specify a different port number for each instance of Chrome
    chrome_debugger_port = chrome_debugger_base_port + i
    print(chrome_debugger_port)
    chrome_options.debugger_address = f"127.0.0.1:{chrome_debugger_port}"

    try:
        # start the Chrome browser with the new profile
        # time.sleep(5)

        driver = uc.Chrome(options=chrome_options)
        time.sleep(2)

        driver.get("chrome://version/")
        time.sleep(2)
        driver.get("https://www.gmail.com/")

        driver.maximize_window()
        time.sleep(5)
        driver.close()
    except Exception as e:
        print(f"An error occurred: {e}")

I tried to add a debugger_address and iterate throught dofferent ports but it gives me this error:
An error occurred: Message: unknown error: cannot connect to chrome at 127.0.0.1:9015
from chrome not reachable
Answer: try this please
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--user-data-dir=C:\\anyPathWhereYouWantToSaveYourUserData\\profile1")
driver = webdriver.Chrome(executable_path=r'C:\Windows\System32\chromedriver.exe', options=options)
driver.get("https://www.instagram.com")


Error installing dotnet framework 3.5 SP1 on windows 2008

Question: Getting a really wierd error.
One of the developers tried to install Windows 2008 as a Virtual PC. He has also run windows update.
When he tries to install dotnet framework 3.5 SP1 he gets the following error:

[09/25/09,12:48:26] Microsoft .NET
  Framework 2.0SP1 (CBS): [2] Error:
  Installation failed for component
  Microsoft .NET Framework 2.0SP1 (CBS).
  MSI returned error code 1
  [09/25/09,12:48:34] WapUI: [2]
  DepCheck indicates Microsoft .NET
  Framework 2.0SP1 (CBS) is not
  installed.

I though that dotnet framework was installed automatically with windows update on windows 2008. So how could it be missing?
Thanks.
Shiraz
EDIT
We also have the same problem on a VPC that had dotnet framework 3.5 installed and working OK.
I have tried removing all versions of dotnet framework, using the following clean up tool: http://blogs.msdn.com/astebner/pages/8904493.aspx
I then downloaded and tried to install dotnet framework 2.0 SP1, from this location: http://www.microsoft.com/Downloads/details.aspx?familyid=79BC3B77-E02C-4AD3-AACF-A7633F706BA5&displaylang=en
The error I now get is:

"This product is not supported on the
  Vista Operating System"

EDIT
Thanks for the help, have given an up vote to everyone.
In the end our problem was that we had installed Windows Server 2008 from an older ISO image, on this everything worked fine untill we tried to install framework 3.5 SP1. We reinstalled Windows from a new image, and it worked OK.
Answer: THe .Net features aren't enabled in a fresh install of win2008, it has to be enabled.
Run the following:
<code>servermanagercmd -i NET-Framework-Core
</code>
To enable .Net framework. Then, run windows update again.
Comment: We were having a very similar issue, and that command helped in our situation. Thanks.
Answer: .NET Framework 2.0 and 3.0 are included in Windows Server 2008, you don't need Windows Update to get them; 3.5 should instead be installed, manually or via Windows Update, because it doesn't come with the Windows setup.
Try turning on .NET-related features in Windows...
Comment: As a side note, Windows Server 2008 R2 (based on Windows 7) will also include .NET 3.5 SP1.
Answer: To add to what Trondh already said:
Rightclick "my computer" -> Manage
click on "Features"
In the right pane click on "Add Features"
Check the .NET Framework 3.5.1 Features
and follow the wizard to install the framework
Answer: Normally you just need to make sure that you have .NET installed and then Windows Update will lead you through all of the update.  However I've run into failures like yours on about 1 in 50 servers, so the trick is to figure out the best path to navigate for your situation.
You're on the right path.  Here's what I suggest:
For the WS08 server, I would uninstall the .NET Framework completely and then build back up again.  To do so, use Add/Remote programs to uninstall any of the service packs that you see there.  Then from Server Manager go to -> Roles -> Web Server (IIS) -> Remove Role Services -> and remove "ASP.NET" and ".NET Extensibility" (and accept the prompt to remove dependencies).  After that completes, perform a reboot if it asks for one.
Then, from Server Manager again, add a role service and add those back again.  Then use Windows Update, but step through the service packs in order.  Don't assume that the cumulative installs will work since you already know that you're a fringe case.
For the Vista computer, if you haven't already, I suggest the Windows Update path there too.  If that dead-ends, then start here: http://www.microsoft.com/net/Download.aspx.  Make sure that you do have .NET 2.0 installed.  (just check c:\windows\microsoft.net\framework\v2.0.50727 and make sure it has the full set of files.).
Then install 3.5 (not SP1 yet): http://www.microsoft.com/downloads/details.aspx?FamilyId=333325FD-AE52-4E35-B531-508D977D32A6&displaylang=en
Finally 3.5 SP1: http://www.microsoft.com/downloads/details.aspx?FamilyId=AB99342F-5D1A-413D-8319-81DA479AB0D7&displaylang=en
Hope that helps.
Answer: I don't think Windows Update automatically installs .NET. I would try installing .NET Framework 2.0SP1 going by your error message.
Comment: The update is indeed optional, but it would be the preferred way to install it unless you know what you are doing.
Answer: i recently had to deal with the out of office assistant in outlook 2007 not working with exchange 2007 because of this exact issue.  microsoft had me uninstall 3.5 then install .net 2.0 sp1 then .net 2.0 sp2 and reinstall .net 3.5.  the issue on my end had to deal with the file system.xml.dll not being properly updated during an install.  it was out of sync with what .net 3.5 was expecting and caused the error.
Answer: it's old but none of the steps here or anywhere else worked for me. in the end my gut told me to disable anti-virus software. and that did it...now before that I looked at the error message in the application log and it looked like all Server Manager is actually doing is enabling the already installed 3.5 update which AV was probably blocking access to.
so don't waste your time and disable anti-virus first.


Python graphing with numpy

Question: Okay so I'm trying to plot a function with an e to some expression but I keep getting an error at the lines that I have put (##) @ where the error message is
TypeError: unsupported operand type(s) for *: 'numpy.ufunc' and 'float'
#!C:\Users\msawe\Anaconda3 or C:\Anaconda3\python

import numpy as np
import matplotlib.pyplot as plt

plt.figure (figsize=(10,10), dpi=100)

ax = plt.subplot(111)

plt.title('Projectile Motion: Goround given by h(x) ', size=24)

ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

def h(x):
"This function will return the y-coordinate for a given x-coordinate launch."
##return 1 - np.exp(-1* x/1000) + 0.28 *( 1 - np.exp(-0.038*x**2))*(1 - np.cos*(20*x**0.2))

X = np.linspace(0, 10, 101, endpoint=True)

##plt.plot(X, h(X), color="black", linewidth=3, linestyle="-", label="h(x)")

plt.xlim(0,10)
plt.xticks(np.linspace(0,10,11,endpoint=True))

plt.ylim(0,20.0)
plt.yticks(np.linspace(0,20,11,endpoint=True))

plt.legend(loc='upper left', frameon=False)
plt.savefig("Ch5_P4_lowRes.png",dpi=60)
plt.savefig("Ch5_P4_hiRes.png",dpi=200)
plt.savefig("Ch5_P4_plotting.pdf",dpi=72)

plt.show()

If I could just get a general idea on how to make it work that would be great.
Comment: You have a typo: "np.cos*(" should just be "np.cos("
Comment: matplotlib probably expects the second argument to plot as a array of values, not a function/ufunc. Internal operations might need some operators defined on this like * which works on an array, but not on a custom-function. Generate an array a-priori from your function and try again (maybe it would work too by using [np.vectorize](http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html))!
Comment: Tracebacks always include the line number(s) the error occurred on. All you needed to do was look at the line at the bottom of the traceback and carefully examine your code on it, and you would have caught the typo. Next time, please include the **full text** of the traceback.
Answer: Assuming that I'm interpreting your equation correctly, there's a bug in your implementation of np.cos() in your definition of h(x): you wrote np.cos*(...) rather than np.cos(...). After fixing that, the code is able to plot- hopefully it's giving the right result!
This would turn your definition of h(x) from:
def h(x):
    return 1 - np.exp(-1* x/1000) + 0.28 *( 1 - np.exp(-0.038*x**2))*(1 - np.cos*(20*x**0.2))

Into:
def h(x):
    return 1 - np.exp(-1* x/1000) + 0.28 *( 1 - np.exp(-0.038*x**2))*(1 - np.cos(20*x**0.2))

The difference is subtle but important! You can easily check for bugs like this by running successively smaller segments of your code to see which operation is throwing the error- this let me quickly hone in on the np.cos*(...) typo.
Comment: Tracebacks always include the line number of the error. All the OP needed to do was read the full error message and carefully examine the lines listed, as well as the lines before them.

Python graphing with numpy


Density plot with epanechnikov with exceedance data

Question: I'm trying to replicate empirical density plot from the paper "Computing Maximum Likelihood Estimates for the Generalized Pareto Distribution".
The data is Exceedance (in kg/mm2) of the Testing Threshold in Tensile-Strength Tests for a Random Sample of n = 15 Nylon Carpet Fibers. Below is the density estimate using Epanechnikov kernel. The paper is about generalized pareto distribution using exceedance data, related to extreme value theory.

I tried to replicate/reproducing this figure in R using density, but unable to do this, see below for reproducible example based on the data in the paper.
tensile_strength <- (0.051,0.140,0.365,0.561,0.030,0.268,0.184,0.1,0.876,0.092,0.011,0.2,0.518,0.338,0.056)

plot(density(tensile_strength, bw=0.18, kernel=c("epanechnikov"),from=0.01, to=1))
lines(x=tensile_strength,y=rep(0,length(ans)),type="p")

Here is the density plot.

The plot is nowhere close to what is in the article.
My question is how does one account for "Epanechnikov kernel with boundary kernel modifications (since the exceedances must be greater than 0)"
Comment: Uff... messy. (+1). It relates to the fact that as we go closer to the boundaries the weights of the kernel window falling outside the support are set to 0 and the remaining non-zero weights (i.e. the one still in the support) are upscaled such that they sum up to 1. Quite common for all kernel regression approaches actually; e.g. see the Wikipedia article on [Kernel regression](https://en.wikipedia.org/wiki/Kernel_regression) and notice the normalisation in the Nadaraya-Watson estimator's denominator.
Comment: @usεr11852 thanks, your comment helped me solve the question.
Answer: I was able to follow @usεr11852 comments and was able to reproduce from the original paper. For truncated/bounded data one would have to used bounded density estimation.
Below is the code to reproduce the plots from the paper.
library(bde)

## Data
tensile_strength <- c(0.051,0.140,0.365,0.561,0.030,0.268,0.184,0.1,0.876,0.092,0.011,0.2,0.518,0.338,0.056)

## Density plots with bounded data
 plot(bde(tensile_strength,estimator="boundarykernel",lower.limit = 0,upper.limit = 1,options = list(mu=1)))

Comment: Sweet! Thank you for sharing. (+1)
Comment: Thansk for pointing it out @usεr11852 I have fixed the answer.
Comment: You are correct let me fix it.


Can the bails be kept in the hands when a wicketkeeper attempts a stumping?

Question: If the wicketkeeper removes the bails, but keeps them in their hands, is it out or not?
Do the bails have to land on the ground or can they remain off the ground once removed?
Answer: The wicket is down and the striker is out, Stumped.
From the Laws of Cricket, Law 29 The Wicket Is Down:

29.1 Wicket put down
29.1.1  The wicket is put down if a bail is completely removed from the top of the stumps, or a stump is struck out of the ground,
...
220.127.116.11 by a fielder with his/her hand or arm, providing that the ball is held in the hand or hands so used, or in the hand of the arm so used.
...
29.1.2 The disturbance of a bail, whether temporary or not, shall not constitute its complete removal from the top of the stumps, but if a bail in falling lodges between two of the stumps this shall be regarded as complete removal.

with emphasis added.
This makes it clear that the wicket is down at the moment the bail(s) have been removed, provided such removal is permanent and not merely a "disturbance".
Law 39 Stumped then only requires

39.1.1 The striker is out Stumped, except as in 39.3, if
a ball which is delivered is not called No ball
and  he/she is out of his/her ground, other than as in 39.3.1
and  he/she has not attempted a run when his/her wicket is fairly put down by the wicket-keeper without the intervention of another fielder. ...

that is, the requirement that the wicket is put down has no further conditions beyond the definition in Law 29.
There is never a requirement that the bails go anywhere particular, or in fact that they be removed, or that they are even present at all.
If a bail is removed, it does not matter where it goes next, as long as it's not simply back onto the top of the stumps.


Inserir dados em parte especifica de um arquivo

Question: Estou usando o arquivo base de feed do google, e preciso alterar as informações de forma dinâmica para gerar um arquivo xml, estou usando essa abordagem, com file_puts_contents(), para colocar as tags <item> no arquivo:
file_put_contents('feed.xml', $xml, FILE_APPEND);

só que o final do arquivo xml tem que terminar com essa estrutura:
</channel>
</rss>

e o FILE_APPEND está colocando abaixo disso, existe alguma forma de inserir o conteúdo num local específico ou sempre acrescentar o </channel></rss>
    ao final do loop?
EDIT: Xml padrão google:
    <?xml version="1.0"?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0">
    <channel>
        <title>O nome do feed de dados</title>
        <link>http://www.example.com</link>
        <description>Uma descrição do conteúdo</description>

        <item>
            <title>Suéter de lã vermelho</title>
            <link> http://www.example.com/item1-info-page.html</link>
            <description>Confortável e macio, este suéter manterá você aquecido nas noites frias do inverno.</description>
            <g:image_link>http://www.example.com/image1.jpg</g:image_link>
            <g:price>25</g:price>
            <g:condition>novo</g:condition>
            <g:id>1a</g:id>
        </item>

    </channel>
</rss>

Comment: E por quê não coloca fora do laço, depois do mesmo? Só será executado quando o laço terminar.
Comment: O que seria "abaixo disso"? Se deseja colocar ao final do arquivo, eles não ficariam, de fato, abaixo do resto do conteúdo? Ficou bem confuso essa parte.
Comment: abaixo do fechamento do xml , com as tags channel e rss
Comment: Tem como [edit] a pergunta e colocar como o XML deverá ficar e como ele vem do *feed* do Google?
Comment: Certo, mas o link que eu coloquei é o modelo do google.
Comment: Mas nesse modelo já existem as tags que você deseja inserir. Qual é o código que você está utilizando para gerar esse novo XML e como ele está ficando?
Comment: Eu vou usar um loop para inserir dados do banco de dados.
Comment: Posta ele para tentar deixar o problema mais compreensível. Por enquanto está muito confuso.
Comment: é um foreach simples usando o file_put_contents, o problema é só colocar o fechamento no último laço
Comment: nem tinha pensando nisso, vou testar, obrigado rs
Answer: Considerando que você pretende adicionar inúmeras tags <item> seguidas de </channel></rss> uma possível solução é:
<?php
$xmlstr = '<?xml version="1.0"?><rss >...</description>';
// Coloca o início do xml no arquivo
file_put_contents('feed.xml', $xmlstr);

// itens recuperados do banco de dados
$items = [
            "<item><title>título 1</title><description>descrição 1</description>",
            "<item><title>título 2</title><description>descrição 2</description>",
            "<item><title>título 3</title><description>descrição 3</description>",
];

// Coloca os itens no arquivo
foreach ($items as $item) {
        file_put_contents('feed.xml', $item, FILE_APPEND);
}

// Coloca o fim do xml no arquivo
file_put_contents('feed.xml', '</channel></rss>', FILE_APPEND);

// imprime o conteúdo do arquivo na STDOUT
echo file_get_contents('feed.xml');

Comment: Fiz de forma parecida, obrigado pela resposta


using group_concat with other selection criteria - mySQL

Question: I have two tables, arrc_PurchActivity and arrc_Voucher. The purchase activity table contains multiple records per purchase, tied together by the credit card authorization. I need to return a single row per purchase, plus pull in a field from the voucher table.
If I just grab a few fields from the purchase activity table, like this:
SELECT group_concat( VoucherID ) , CcAuthCode FROM arrc_PurchaseActivity GROUP BY CcAuthCode

it works fine, returning something like this:
group_concat( VoucherID )  | CcAuthCode
=========================================
610643,611139,610642       | 8LUPDN

What I need to do is pull in another contatenated field (VoucherNbr), this time from the arrc_Voucher table, where the voucher table's VoucherID is equal to the purchase table's VoucherID. In this case, because VoucherID is a concatenation, I need to return a concatenated column of VoucherNbr for each VoucherID in the concatenated column. Clear as mud, right? What I need would look like this:
group_concat( VoucherID )  | group_concat( VoucherNbr)  |  CcAuthCode
===========================|============================|=============
610643,611139,610642       | 123,456,789                |  8LUPDN

In other words, the VoucherNbr for VoucherID 610643 is 123, VoucherNbr for 611139 is 456, etc.
Can anyone help me out? This is way over my head...
Answer: Use:
   SELECT pa.ccauthcode,
          GROUP_CONCAT(DISTINCT pa.voucherid) AS voucherids,
          GROUP_CONCAT(v.vouchernbr) AS vouchernbrs
     FROM ARRC_PURCHASEACTIVITY pa
LEFT JOIN ARRC_VOUCHER v ON v.voucherid = pa.voucherid
 GROUP BY pa.ccauthcode

I specified the DISTINCT in the GROUP_CONCAT for the voucherid's because it's possible you'd have multiple vouchernbr's to a voucherid.  If not, remove the DISTINCT.
The LEFT JOIN ensures you'll get ARRC_PURCHASEACTIVITY records that do not have supporting records in ARRC_VOUCHER.  Change "LEFT JOIN" to "JOIN" if you don't want this behavior.
Comment: Thank you! Up until these past few weeks, my need for sql has been fairly limited, and my experience is also fairly limited - to basic select from where-type statements.
Comment: can you recommend a good book or website tutorial that would help me get past the basics? I'm pretty sure you've answered a few of my other questions this last month. I've done the www.w3schools.com tutorial, but it's pretty simple. I know there are a lot of reference sites, but you kind of have to know what you're looking for to make use of a reference.
Comment: @EmmyS: You're welcome.  Sorry, I'm not familiar with more advanced tutorials.  The problem is that answers depend on the database, table design (incl. data types), data and the expected result. I'm still tweaking [my list of categorizing SQL functionality](http://stackoverflow.com/questions/2119859/questions-every-good-database-sql-developer-should-be-able-to-answer/2120339#2120339), but I'd start with focusing on problems dealing with the basics.
Comment: Thanks. That's a great thread; although I have no desire to be an actual database developer, as a web developer I do need to be able to write queries on databases provided by clients. Your list (and a few of the other posts) give me some specific things to look at.
Comment: @EmmyS: Come to the Dark.. er, Database Side.  We have cookies =)


Why do I have conflicts in namespacing?

Question: I'm building a traderbot for drupal and I have problems with the namespacing.
Something is not working or I do not understand it correctly. I looked on several pages and questions but could not understand it.
The start
I runned all code without namespaces. Then it is working well !!!
I included a project called ccxt. In this project there are classes with the same name. They have a class called kraken and I have as well. There classes are in namespace ccxt.
So mine class should be namespaced. Which is very obvious.
As I do call from drush I need to have a own autoloader.
function autoloader_traderbot( $class_name ){

  // Because of namespaces $class_name could be string/string therefore only the last string should be taken
  if (($pos = strpos($class_name, '\\')) !== FALSE) {
    $class_name = substr($class_name, $pos+1);
    (new log)->add(4,'Class name adjusted. Namespace removed from class path.');

  }

  (new log)->add(4,'Searching for loading class  name  : ' . $class_name );

  $dir_iterator = new RecursiveDirectoryIterator(CLASS_DIR);
  $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);

  foreach ($iterator as $file) {
    if($file->getFilename() == $class_name . ".class.php"){
      (new log)->add(4,'Loading class file name (.class.php) : ' . $class_name . ' in filename ' . $file );
      require_once tb_adjustfilename($file);
    }

    if($file->getFilename() == $class_name . ".php"){
      (new log)->add(4,'Loading class file name (.php): ' . $class_name . ' in filename ' . $file);
      require_once tb_adjustfilename($file);
    };
  }

}

So I add in kraken.class.php
namespace traderbot;

Then calling this object is working fine.
But then the problems are there
I also get
Error: Class 'traderbot\log' not found in traderbot\kraken->update_ohlc() (line 48 of C:\www\tb\modules\custom\tb\src\exchanges\kraken.class.php) #0                                          [31;40m[1m[error][0m
I checked log.class.php.
This is not namespaced so I add
namespace traderbot;

But then I get from php
Error: Class 'traderbot\SplFileInfo' not found in traderbot\log->add() (line 66 of C:\www\tb\modules\custom\tb\src\library\log.class.php).
Drush command terminated abnormally due to an unrecoverable error.

But now I'm lost. Because this is just a normal php object so why is it going wrong?
Addition
Thxs, with the help of the answers it is working now. I saw the excellent video, change the statements, disable my own autoloading procedure.
As a result of this I should add in my classes the use statement for every object I use. The only thing I find a little bit strange is the following.
In every php file with a class I should add the use statement for every object.
Example 1.
// Define namespaces and objects
namespace Drupal\tb\library;
use SplFileInfo;
class log{
}

Example 2
namespace Drupal\tb;

use Drupal\tb\library\log;
use Drupal\tb\datacollector\datacollector;
use Drupal\tb\datacalculator\datacalculator;
use Drupal\tb\scenario\scenario;

class traderbot{

    public function start(){

        (new log)->add(0,"Start data collector");
        $collector = new datacollector;
        ...

        (new log)->add(0,"Start data calculator");
        $indicators = new datacalculator;
        ...

        (new log)->add(0,"Start scenario handler");
        $sc_handler = new scenario;
        ...

    }

}

I red some other documentation and QA and understand dat wild card loading of namespacing is not possible.
https://stackoverflow.com/questions/29851099/how-to-load-all-classes-of-a-namespace
but I also red somewhere that as long as the class is in the same namespace it is not needed to put the use statement for this object. So then I come to the following questions for verification:
Q1) Adding a standard use statement for SPL looks very strange for me. Is this correct?
Q2) Adding a long list of several use statements from unique objects in the same namespace is very strange for me as well. Is this correct?
Comment: Please show us the following to be able to debug your issue: the error, the namespace declaration and class declaration, the code that is calling that class, and any use statements implemented in the file for the code calling that class.
Comment: The namespacing is wrong. Read about PSR-4 autoloading and the doc pages on the same topic in Drupal.
Comment: I still don't understand why you need to do all of that code like that. Once you properly define your class, you should be able to add a `use ...` statement in the drush command to pull in the class and use it. Read the documentation on drupal.org about creating modules and classes, like a simple FormBase form or Controller. It's all on display there.
Answer:
This is not namespaced so I add
namespace traderbot;

Clearly you don't understand the concept of namespaces, so I suggest you watch this short video.
After watching the video, you should come to the realization that you're telling PHP to open the "traderbot" folder which does not exist.
According the official Drupal docs on Namespaces, you need to structure it like this for Drupal:

namespace Drupal\<module name>\SubFolder1\SubFolder2;

Note: Since Drupal uses autodiscovery the \modules\custom nor \src paths are not need to be declared here, since Drupal will automatically look inside of these.
Note2: the file name matters, needs to be YourClassName.php (case sensitive).

Class Drupal\example_module\Foo\Bar in namespace
  Drupal\example_module\Foo should be in a file named
  example_module/src/Foo/Bar.php

so you should remove the .class part from your file names. In Drupal the convention is to create a file for each class.
In /modules/custom/tb/src/library/log.php you need to put:
namespace Drupal\tb\library;

Then on /modules/custom/tb/src/exchanges/kraken.php to load a class that is in a different name space, you need to put at the top
use <name_space>\<class_name>;

so it would be:
use Drupal\tb\library\log;

Comment: Namespaces are effectively class/path mappings for the autoloader and also assist in preventing naming collisions of classes.
Comment: I think the file name also has to be updated to reflect the actual class name as well.

Why do I have conflicts in namespacing?


Coefficient of determination applied to difference in means?

Question: Assume we have one random variable X, and two groups A and B have different mean X values. Let's say we have another variable Y that has a correlation with X of 0.5.
If I wanted to estimate a Y value difference between these groups based on the X values, would I multiply the difference by 0.5, or would I multiply it by 0.25 ($R^2$) and convert that value into Y SD units?
Answer: Multiply by 0.5 and convert the value into Y SD units. The difference is one dimensional, no need to do square. Loosely speaking, we have,
$$
\frac{\hat{y_0}-\bar{y}}{\sigma_y}=\rho\frac{x_0-\bar{x}}{\sigma_x}
$$
Also think of the special case when the correlation is negative, squaring would make the direction wrong.


Unable to scrape other namespaces when using kube-prometheus

Question: Prometheus deployed using kube-prometheus can't scrape resources on namespaces other than default, monitoring and kube-system. I added additional namespaces on my jsonnet as described in kube-prometheus README but no success...
I also tried to create a new ServiceMonitor manually, but no success...
I appreciate any help.
Thanks.
Comment: Hi @RickRackow I created [PR](https://github.com/coreos/kube-prometheus/pull/114) to help clarify how to create the `ServiceMonitor` resources...
Comment: can you validate the permissions of the service account so that it's actually capable of getting all resources of type `ServiceMonitor` in other namespaces?
Comment: great work! Thanks for taking the time.
Answer: If you used the pre-compiled manifests here you will only have your service account with 3 rolebindings allowing access to the namespaces you mentioned.
You can add more namespaces for example by applying the same roleBinding in more namespaces.
This is more secure as opposed to using a clusterRoleBinding since it allows for more finegrained permissions.


Handling long inequalities on short width paper

Question: I have a long inequality in a single line
$(1+a)^{k+1}=(1+a)^k(1+a)\ge(1+ka)(1+a)=1+(k+1)a+ka^2\ge 1+(k+1)a$
But I find it difficult to show when the width of my paper is short.  What can I do to make the lines readable for short width papers.
Comment: https://latex-tutorial.com/align-equations/
Comment: I do not mean how to align in latex, but to align for readability of proof.  Otherwise I start wondering where the equality or inequality applies to which expression.
Comment: For instance, if I align $=1+(k+1)a+ka^2$ with the first equal sign, one might think that the first expression is equal to the fourth expression, which they are not.
Comment: @Rrinpoche Even on wide paper your chain of equations and inequalities is a bit hard to read. A typical style is that you align **every** sign with the first sign so that it is relatively easy to spot the $\geq$ signs. If the $=$ signs are confusing, maybe you don't need so many equations: $(1+a)^{k+1}\ge(1+ka)(1+a)\ge 1+(k+1)a$ seems clear enough to me.
Comment: You can also break your chain into pieces interspersed with a few words so that you don't have so much alternation of equality and inequality.
Comment: Right.  You decided to keep just the inequalities.  That makes things easier as you say.  And intersperse with some words is also of same value.  I find the usual  presentation of mathematics hard to read (not like a novel).  I am trying to see whether I can change the presentation so that for the most part I do not have to write anything on paper, but just follow the document by looking at the symbols only.
Comment: I thought the question was about **writing** math, not reading it. This question gets less and less clear with each comment. But I think removing the words from a math paper and using only symbols would make it much harder to read in most cases. (There are some exceptions for very short works called "proofs without words", but I've never seen that style successfully used for anything longer.)
Comment: It is about writing it so one can read it better.
Answer: It is common to chain equalities and inequalities together line by line, especially in analysis proofs. See for example this proof of the triangle inequality on Wikipedia:

I think this usage is common enough that you don't need to worry about someone misinterpreting an inequality for equality.

For your example specifically, since you asked in the comments:
\begin{align}
(1+a)^{k+1} &= (1+a)^k(1+a)
\\
&\ge (1+ka)(1+a)
\\
&= 1+(k+1)a + ka^2
\\
&\ge 1 + (k+1)a.
\end{align}
I think mathematicians would unequivocally interpret this as $(1+a)^{k+1} \ge 1+(k+1)a$, despite the equality signs in the intermediate steps. I can't speak for other fields, where perhaps other conventions might exist.
Comment: How would you do it for my example ?
Comment: I see that the last equality looks like it is equality with $||u+v||^2$.  But it is not.  I agree that the usage for triangle inequality ins not something to worry about, and generally people will understand it.  But for complicated mathematical physics one is not very acquainted with, I am not so sure.
Comment: @Rrinpoche Updated my answer to show how I would write your example specifically.
Comment: I'm not entirely happy about the word "interpret", but certainly anyone with any experience of inequalities should be able to **deduce** from the four steps given that $(1+a)^{k+1}\ge1+(k+1)a$, and that, surely, is the point.
Comment: I am saying something else though.  Suppose you do not have experience of the material (and the expressions of the inequalities are unfamiliar).  Would one interpret them correctly ? Or could it be ambiguous ?


Seeming inconsistency in scipy.integrate.quad operation under change of variable

Question: I have an algorith that seeks to multiply two functions and integrate the result.  One is a small normal distribution around 1.0, with a small standard deviation of abouty .1; the other is also a normal, but much larger, with mean 100,000 and stdev 15,000.
The code looks like so:
def integrand(x, z, mu, sigma) :
    # goal is to return return  f_x(x)*f_y(z/x)*1/abs(x)
#   return normal(x, mu, sigma) * normal(z/x, 100000, 15000)/abs(x)
    return normal(z/x, mu, sigma) * normal(x, 100000, 15000)/abs(x)

    pResult = quad(integrand, -10000000,10000000,
                       args=(_z, MU, SIGMA),
                       points = [1.0, 2.0, 100000.0],
                       epsabs = 1, epsrel = .01)

This is a formula for multiplying random variables, Z = XY.  It would seem that I could swap where the z/x goes in the integrand, but in trying to see which way runs faster (by commenting out one of the two return statements in integrand())  I was surprised to get different results (one is 114,221.4 and the other is 116,174.2).  Any idea why?
Also, when I cycle through a wide range of z values and plot P(z) vs z, the graphs look different,  One looks right (like a normal distribution - using the first return statement) and the other looks like a top-hat with soft corners.
The math for this algorithm: https://en.wikipedia.org/wiki/Product_distribution
Answer: Scaling matters, as I noted in the answer to your previous question, and recommended to use points to help quad locate  relatively small features.
You rescale the function but keep the same interval of integration. Assuming the function essentially "fits" in the interval (the tails outside of the interval are negligible), the difference is due to the algorithm being able or unable to locate the small feature of the function (the narrow Gaussian) within the interval of integration. The more narrow is the function support, the more likely is quad to get integration wrong.
I also recommend putting accuracy as the top priority; speed comes after that. It's easy to write a function that returns "0" quickly.


How does Pinsker's inequality provide "a lower bound for the error of certain hypothesis testing problems"?

Question: Whilst flicking through Concentration Inequalities: A Nonasymptotic Theory of Independence by Boucheron, Lugosi and Massart (2016), I came across the following incidental remark:

The importance of Pinsker's inequality in statistics stems from the fact that it provides a lower bound for the error of certain hypothesis testing problems.

The authors do not specify the hypothesis testing problems whose error can be lower bounded by Pinsker's inequality, presumably because their focus in the book is not statistics. Nor do they specify how this might work. I would therefore greatly appreciate if members of the community could shed some light on the following question:
What are the authors referring to when they say that the utility of Pinsker's inequality "stems from the fact that it provides a lower bound for the error of certain hypothesis testing problems? And how would this work?

Context.
Here is an extract from Section 4.11 Pinsker's inequality of the monograph, where the remark can be found:

Pinsker's inequality relates the relative entropy of two probability distributions to their variational distance. Let $\mathcal{P}$ and $\mathcal{Q}$ be two probability measures on a measurable space $(\Omega, \mathcal{A})$. The total variation or variational distance between $\mathcal{P}$ and $\mathcal{Q}$ is defined by
$$V(\mathcal{P}, \mathcal{Q}) = \sup_{A \in \mathcal{A}} \vert \mathcal{P}(A) - \mathcal{Q}(A) \vert.$$
It is a well known and simple fact that the total variation is half the $L_1$-distance, that is if $\lambda$ is the common dominating measure of $\mathcal{P}$ and $\mathcal{Q}$ and $p(x) = d\mathcal{P}/d\lambda$ and $q(x) = d \mathcal{Q} / d \lambda$ denote their respective densities, then
$$V(\mathcal{P}, \mathcal{Q}) = \mathcal{P}(A^*) - \mathcal{Q}(A^*) = \frac{1}{2} \int \vert p(x) - q(x) \vert d \lambda(x),$$
where $A^* = \{x: p(x) \geq q(x) \}$. We note that another important interpretation of the variational distance is related to the best coupling of the two measures
$$V(\mathcal{P}, \mathcal{Q}) = \min \mathcal{P} \{X \neq Y \},$$
where the minimum is taken over all pairs of joint distributions for the random variables $(X, Y)$ whose marginal distributions are $X \sim \mathcal{P}$ and $Y \sim \mathcal{Q}$. (The proof of these well-known facts is left as Exercise 4.5).
The importance of Pinsker's inequality in statistics stems from the fact that it provides a lower bound for the error of certain hypothesis testing problems [my emphasis]. We use Pinsker's inequality for a completely different purpose, namely for establishing a transportation cost inequality that may be used to prove concentration inequalities. The proof of Pinsker's inequality derives easily from Hoeffding's inequality via the transportation cost bound of Lemma 4.1.8:
Theorem 4.1.9 (Pinsker's inequality) Let $\mathcal{P}$ and $\mathcal{Q}$ be probability distributions on $(\Omega, \mathcal{A})$ such that $\mathcal{Q} \ll \mathcal{P}$. Then
$$V(\mathcal{P}, \mathcal{Q})^2 \leq \frac{1}{2} D(\mathcal{Q} || \mathcal{P}).$$
[Proof omitted.]
Answer: Say you have two hypothesis - Null hypothesis and an Alternate hypothesis. Under the null hypothesis, your test statistic follows a distribution $P$, say  $\mathcal{N}(0,1)$ and under the alternate hypothesis, it follows a distribution $Q$, say $\mathcal{N}(\mu,1)$.
Now say you want to calculate the maximum probability of error (between type I and type II).  If type I error is when your test statistic falls in the region $A$ under Null Hypothesis, then type II error is when your test statistic falls in region $A^c$ under alternate hypothesis.
Note that $1 - V(P,Q) \leq P(A) + Q(A^c)$ for any $A$, by definition of Total variation distance. Hence, the errors can be bounded as follows:
$\begin{align} \max (P(A),Q(A^c)) \geq \frac{P(A)+Q\left(A^{c}\right)}{2} & \geq \frac{1}{2} (1 - V) \\ &
\geq \frac{1}{2} (1-\sqrt{\frac{D}{2}})
\end{align}$
The first inequality follows from $\max(x,y) \geq \frac{x+y}{2}$ and the third one follows from Pinskers inequality. For standard distributions, there will be closed form expression to compute the KL-divergence $D$.


Should I expect ICMP transit traffic to show up when using debug ip packet with a mask on a Cisco IOS router?

Question: So I am trying to trace an ICMP conversation between 192.168.100.230/32 an EZVPN interface (Virtual-Access 3) and 192.168.100.20 on BVI4.
<code># sh ip access-lists 199

  10 permit icmp 192.168.100.0 0.0.0.255 host 192.168.100.20
  20 permit icmp host 192.168.100.20 192.168.100.0 0.0.0.255

# sh debug

  Generic IP:
    IP packet debugging is on for access list 199

# sh ip route | incl 192.168.100

    192.168.100.0/24 is variably subnetted, 2 subnets, 2 masks
C       192.168.100.0/24 is directly connected, BVI4
S       192.168.100.230/32 [1/0] via x.x.x.x, Virtual-Access3

# sh log | inc Buff
  Buffer logging:  level debugging, 2145 messages logged, xml disabled,
  Log Buffer (16384 bytes):
</code>
OK, so from my EZVPN client with IP address 192.168.100.230, I ping 192.168.100.20.   I know the packet reaches the router across the VPN tunnel, because:
<code>policy exists on zp vpn-to-in
 Zone-pair: vpn-to-in

  Service-policy inspect : acl-based-policy

   Class-map: desired-traffic (match-all)
      Match: access-group name my-acl

   Inspect

      Number of Half-open Sessions = 1
      Half-open Sessions
        Session 84DB9D60 (192.168.100.230:8)=>(192.168.100.20:0) icmp SIS_OPENING
          Created 00:00:05, Last heard 00:00:00
          ECHO request
          Bytes sent (initiator:responder) [64:0]

   Class-map: class-default (match-any)
     Match: any
     Drop
       176 packets, 12961 bytes
</code>
But I get no debug log, and the debugging ACL hasn't matched:
<code># sh log | inc IP:
#

# sh ip access-lists 198

Extended IP access list 198
10 permit icmp 192.168.100.0 0.0.0.255 host 192.168.100.20
20 permit icmp host 192.168.100.20 192.168.100.0 0.0.0.255
</code>
Am I going crazy, or should I not expect to see this debug log?
Thanks!
Comment: The reason I'm trying to trace the packet is that I never get any traffic back through the zone-pair - it *never* progresses beyond half-open, for any traffic, to any internal address.  The only packets the EZVPN client *receives* are arp packets.  I have more success debugging TCP traffic (I can see the debugging ACL get hit), but not enough (only the replies get logged).  And even then, when debug ip packet shows that it's delivering the full packet to virtual-access 3, nothing shows up on the client end.
Answer: Yes, it is possible to debug transit traffic.  However, it will only show up in the debug it it is 'routed in software'.  Traffic to/from the router itself automatically qualifies, but transit traffic is usually 'process switched' using 'fast switching' or 'Cisco Express Forwarding' and is never handled by the router CPU.
In order to force it to be shown in a debug, it is necesary to disable fast-switching, using 'no ip route-cache' and/or 'no ip route-cache cef' in interface configuration mode.
See How to Verify Cisco Express Forwarding Switching


Winning probability in a game with multiple players

Question: I do not come from a mathematical background, and hope you can answer this (probably very basic?) question.
I got a game group with friends where we play board games, normally 3-6 players each night. I have created an application which keeps track of game history, and keeps track of a Elo rating for each player. These Elo ratings can be used to determine the winning probability between two players.
However, how can I use these winning probabilities to get the probability of a player winning a multi-way game?
Let's say we have a three way game, two players with a Elo rating of 1200 and one with 1400. The two with 1200 have 50% chance of winning against each other and the 1400-player has a 76% chance of winning in a heads up match against a 1200-player. How can I find the probabilities that each player wins the three-way game?
Comment: I am guessing someone who wins both his matches wins the three-way game, but what happens if everybody gets one win and one loss? What's the tie-breaking rule?
Comment: It is not multiple matches, just multiple players in one match. Think three players playing Monopoly. You know the probability of each person winning over the other player in a heads up Monopoly match, and want to transform that into the probability of winning a three (or more) player game.
Comment: Do you have data for the three way games? So, for example, can you see how often a player with an ELO rating of around 1400 won against two opponents with ELO ratings of around 1200?
Comment: A,B,C are three players, A,B=1200 and C=1400, Then probability of A winning a game with B is 50% i.e `P(A/B')=0.5`. Similarly `P(B/A')=0.5` and the probability of C winning is `P(C/A'&B')=0.76` where `A'` (for example) is the complement of `A`. That is your question?
Comment: Hmm. Either you are stating the obvious, or your comment went over my head. What is my question?
Answer: If you assume that the weaker players won't gang up on the stronger player (a very strong assumption!), then a reasonable model would be the following.  (I'm following the notation of the "theory" section of the Wikipedia article on Elo.)

let $R_A, R_B, R_C$ be the ratings of the three players.
let $Q_A = 10^{R_A/400}$; define $Q_B, Q_C$ similarly.
the probability of $A$ winning is $Q_A/(Q_A + Q_B + Q_C)$ and similarly for the other two players.

With the numbers you gave, $C$ has a probability of about $0.613$ of winning, and $A, B$ each have probability $0.194$ of winning.
This seems like the "obvious" generalization of the Elo math.  The most obvious problem, to me, is that I wouldn't know how to update these ratings after a multi-player game is played.
Comment: "The most obvious problem, to me, is that I wouldn't know how to update these ratings after a multi-player game is played." From what I understand, it seems that rankade algorithm manage this issue well: https://rankade.com/ree#ranking-system-comparison Unfortunately it's not open source at the moment.
Answer: Would the weaker players not band together against the stronger players? That is what happens in the truel (three player pistol fight with one shot per person).
I observed that in monopoly and in three-player chess also. That said, if one  assumes the winning or losing of A over B and C are independent, then with the probabilities you mentioned
P(A wins against B and C) = P(A wins against B) * P(A wins against C) = .5 * .24 = .12
= P(B wins against A and C)
and P(C wins against both A and B)= .76 * .76 = .5776 .
The remainder is the probability of no-one winning.
Note that in three-way chess, no-one winning because the situation "A wins against B, B wins against C and C wins against A" is not possible, as the first one to check-mate anyone is the overall winner, and the other two lose equally. So there the whole calculation would not hold.
Summarizing I would say there is no unique answer to your question, you need to specify how ties are broken, if collaboration can occur, etc. This is not a statistics question, it is game theory.
Comment: Disregard concepts like allying and ties. Let's say all players are of equal skill, with a 50% chance of winning in a heads up game vs every other player. Logical thinking gives that each player got a 1/3 chance of winning in a three-way game. With this method you get a 1/4 chance of winning for each player, and 1/4 chance of getting no winner?
Comment: Yes, under independence. The problem is that even if you don't allow two-player ties, the 1/4 probability of a three way tie arises out of the assumption of independence: A wins over B, B over C, and C over A has a positive probability of 1/8, and the opposite cycle also. If the game rules exclude ties, then this computation cannot hold. Do ties occur at all?


UPDATE campo BIGINT[] a partir de um SELECT

Question: Tenho realizar um update em um campo
permissao_ver
Sendo o mesmo o tipo bigint[], resposta do campo : '{1,2,3,4,5,11,44,56,75,11}'
Minha Query
UPDATE callcenter.pausa
SET permissao_ver = '{"(SELECT cod_grupo FROM crm.usuariosgrupos
                        WHERE habilitar = 1)"}'::bigint[]
WHERE habilitado = 1 AND permissao_ver is null

Preciso pegar os codigos que o select vai me retornar de dar um update nesse campo bigint[] '{XX,XX,XX,X,XX,X}'
Comment: O campo cod_grupo já é um array (bigint[])? No update você quer simplesmente substituir um array pelo outro ou unir o valor existente com o resultado do subselect?
Answer: Resolvido .
UPDATE callcenter.pausa
SET permissao_ver = array(SELECT cod_grupo
                          FROM crm.usuariosgrupos
                          WHERE habilitar = 1)
WHERE habilitado = 1
AND permissao_ver is null


Como exibir código HTML?

Question: Como posso exibir meu código HTML? Tenho um campo onde o usuário pode alterar o nick dele colocando cores, e quero exibir o código usado como dica para o usuário.
Meu código:

    Guizoio

Só que não funciona ele não exibe o HTML e sim Guizoio em vermelho.
Answer: Consegui resolver, para exibirmos um código HTML em um site ou blog, não podemos simplesmente inserir o código na página normalmente (mesmo dentro de uma caixa de texto), porque o navegador tentaria interpretar o código e ele não seria exibido.
Para exibir um código devemos substituir as chaves:
< por <
e chave
> por >
Fonte http://pc286.blogspot.com.br/2009/01/como-exibir-cdigos-tags-html-ou.html
Answer: Como alternativa a solução de escapar/substituir os caracteres pode-se usar <textarea> para exibir o código. Basta utilizar a propriedade readonly e remover as bordas do elemento:

textarea {
  border: none;
  resize: none
}
<textarea readonly rows='10' cols='40'>

<!doctype html>

    <title>StackOverflow pt</title>

</textarea>

Somente a como curiosidade: existia a tag <xmp> que permitia exibir texto sem interpretá-lo como HTML. Em alguns navegadores, o código abaixo ainda pode funcionar. Lembrando que é uma tag obsoleta e seu uso é desencorajado, o snippet é somente a título de curiosidade mesmo:

<xmp>
  <!doctype html>

      <title>StackOverflow pt</title>

</xmp>
Comment: Opa renan, consegui resolver, achei até um plugin `run_prettify.js` e tudo funcionou perfeitamente. Mas obrigado pela sua resposta.

Como exibir código HTML?


Docker Container Logs for short lived containers

Question: I have a docker swarm stack and an application that run various docker containers to perform certain services. Sometimes those services take 10 minutes, some times, 3 seconds.
Looking for a solution to be able to read the docker logs from all of those services. For the containers that run longer, it can be easily achieved with docker log xxx but if the container starts up and dies shortly after, I can't do the same.
Appreciate any ideas on how I can log logs of all containers that start and stop. docker events don't get me any info.
Answer: Consider using a different logging driver, so that logs are persistently stored. The default, json-file, does not keep logs after containers are deleted. If you are using Docker CE, switch to journald and you can view Docker logs in the systemd journal (and syslog, if you have journal entries copied to syslog as most people do) as well as with docker logs. You have more logging options with Docker Enterprise, which are listed on Docker's web site, or you can configure systemd-journald to export logs to a wide variety of external destinations.
Comment: At least on my system (Ubuntu 18 server) the default logs are persisted after the container is stopped. Only after I remove the container I cannot access them anymore.
Comment: @JurajMartinka Short lived one time containers are usually started with the `--rm` option, which automatically deletes the container after it exits.
Comment: That's a good point but I appreciate you updated your answer to be more precise :).