MySQL 5.5.27 crashes randomly on centOS 5

Question:
I am using godaddy's dedicate server to host my web application, it's centOS 5(I have no idea why they are still using this.), I installed mysql in that server, everything was running OK since one day I got an report saying that the web application cannot connect to mysql, I checked and found out that mysql crashed, after restarting mysql, everything worked again. But the weird thing is that it will crash randomly, sometimes once in a month, sometimes once or twice in couple months. Please help me figure out what the problem is. The following is the most recent crash log from mysql. Thank you very much for your time.
<code>121015 08:59:35 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
121015  8:59:36 [Note] Plugin 'FEDERATED' is disabled.
121015  8:59:36 InnoDB: The InnoDB memory heap is disabled
121015  8:59:36 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
121015  8:59:36 InnoDB: Compressed tables use zlib 1.2.3
121015  8:59:36 InnoDB: Using Linux native AIO
121015  8:59:36 InnoDB: Initializing buffer pool, size = 128.0M
121015  8:59:36 InnoDB: Completed initialization of buffer pool
121015  8:59:36 InnoDB: highest supported file format is Barracuda.
121015  8:59:38  InnoDB: Waiting for the background threads to start
121015  8:59:39 InnoDB: 1.1.8 started; log sequence number 3584614
121015  8:59:39 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
121015  8:59:39 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
121015  8:59:39 [Note] Server socket created on IP: '0.0.0.0'.
121015  8:59:40 [Note] Event Scheduler: Loaded 2 events
121015  8:59:40 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.5.27'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL) by Remi
121019 23:42:58 [Warning] IP address '126.96.36.199' could not be resolved: Name or service not known
121024 05:58:39 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
121024  5:58:41 [Note] Plugin 'FEDERATED' is disabled.
121024  5:58:41 InnoDB: The InnoDB memory heap is disabled
121024  5:58:41 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
121024  5:58:41 InnoDB: Compressed tables use zlib 1.2.3
121024  5:58:41 InnoDB: Using Linux native AIO
121024  5:58:41 InnoDB: Initializing buffer pool, size = 128.0M
121024  5:58:41 InnoDB: Completed initialization of buffer pool
121024  5:58:41 InnoDB: highest supported file format is Barracuda.
InnoDB: The log sequence number in ibdata files does not match
nnoDB: the log sequence number in the ib_logfiles!
121024  5:58:41  InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
121024  5:58:43  InnoDB: Waiting for the background threads to start
121024  5:58:44 InnoDB: 1.1.8 started; log sequence number 3584963
121024  5:58:44 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
121024  5:58:44 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
121024  5:58:44 [Note] Server socket created on IP: '0.0.0.0'.
121024  5:58:45 [Note] Event Scheduler: Loaded 2 events
121024  5:58:45 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.5.27'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL) by Remi
121024  6:06:32 [Note] /usr/libexec/mysqld: Normal shutdown

121024  6:06:32 [Note] Event Scheduler: Purging the queue. 2 events
121024  6:06:34 [Warning] /usr/libexec/mysqld: Forcing close of thread 3  user: 'root'

121024  6:06:34 [Warning] /usr/libexec/mysqld: Forcing close of thread 2  user: 'root'

121024  6:06:34  InnoDB: Starting shutdown...
121024  6:06:37  InnoDB: Shutdown completed; log sequence number 3584963
121024  6:06:37 [Note] /usr/libexec/mysqld: Shutdown complete
121024 06:06:37 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended
121024 06:08:23 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
121024  6:08:23 [Note] Plugin 'FEDERATED' is disabled.
121024  6:08:23 InnoDB: The InnoDB memory heap is disabled
121024  6:08:23 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
121024  6:08:23 InnoDB: Compressed tables use zlib 1.2.3
121024  6:08:23 InnoDB: Using Linux native AIO
121024  6:08:23 InnoDB: Initializing buffer pool, size = 128.0M
121024  6:08:23 InnoDB: Completed initialization of buffer pool
121024  6:08:23 InnoDB: highest supported file format is Barracuda.
121024  6:08:23  InnoDB: Waiting for the background threads to start
121024  6:08:24 InnoDB: 1.1.8 started; log sequence number 3584963
121024  6:08:24 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
121024  6:08:24 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
121024  6:08:24 [Note] Server socket created on IP: '0.0.0.0'.
121024  6:08:24 [Note] Event Scheduler: Loaded 2 events
121024  6:08:24 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.5.27'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL) by Remi
121026 10:49:09 [Warning] IP address '184.108.40.206' could not be resolved: Temporary failure in name resolution
121027  7:00:57 [Warning] IP address '184.108.40.206' could not be resolved: Name or service not known
121028  1:42:49 [Warning] IP address '188.8.131.52' could not be resolved: Name or service not known
121029 11:48:07 [Warning] IP address '18.104.22.168' could not be resolved: Name or service not known
121031 10:52:46 [Note] Event Scheduler: scheduler thread started with id 99

121105 14:04:01 [Warning] IP address '22.214.171.124' could not be resolved: Name or service not known
121107 09:18:43 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
121107  9:18:44 [Note] Plugin 'FEDERATED' is disabled.
121107  9:18:44 InnoDB: The InnoDB memory heap is disabled
121107  9:18:44 InnoDB: Mutexes and rw_locks use InnoDB's own implementation
121107  9:18:44 InnoDB: Compressed tables use zlib 1.2.3
121107  9:18:44 InnoDB: Using Linux native AIO
121107  9:18:44 InnoDB: Initializing buffer pool, size = 128.0M
121107  9:18:44 InnoDB: Completed initialization of buffer pool
121107  9:18:44 InnoDB: highest supported file format is Barracuda.
InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
121107  9:18:44  InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
121107  9:18:48  InnoDB: Waiting for the background threads to start
121107  9:18:49 InnoDB: 1.1.8 started; log sequence number 3609300
121107  9:18:49 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
121107  9:18:49 [Note]   - '0.0.0.0' resolves to '0.0.0.0';
121107  9:18:49 [Note] Server socket created on IP: '0.0.0.0'.
121107  9:18:50 [Note] Event Scheduler: Loaded 2 events
121107  9:18:50 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.5.27'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Community Server (GPL) by Remi
121107  9:23:34 [Note] Event Scheduler: scheduler thread started with id 5
</code>
Comment: does it crash when no InnoDB tables are being used? rephrasing, could you check to see if it crashes WHEN an InnoDB table is being used?
Comment: @Gaia Thank you for your reply. All tables are InnoDB except two are MyISAM in my application database.
Answer: Check your dmesg output and look for OOM messages. If MySQL is taking up too much memory, the Out Of Memory killer will take it down.
Comment: Thank you very much for your reply Mark, I checked dmesg, there's nothing in there. Weird!
Comment: Anything in the mysqld log or /var/log/messages?
Comment: Nothing in messages, I do have the log for mysqld. There are the most recent mysqld log messages in my question. Thanks.
Comment: I will edit it to add more messages. If you could take a look, that will be great. Thanks for your time.
Comment: Do you have any core files in `/var/lib/` for mysqld? It definitely looks like it's crashing (hence the didn't shut down cleanly message)
Comment: In `/var/lib` there is a folder named `mysql` which stores database files. I don't see any other things related to mysql other than that.Thanks.
Answer: It looks like there is an issues with InnoDB corruption. If you can get it up and running, you can try running "mysqlcheck --all-databases --quick --fast --auto-repair --force ". If that will not work, you can try to dump the content and then import everything again as this will re-create the ibdata file. If you cannot run either of these before the server restarts again, you may need to look into using the Percona Data Recovery Tool for InnoDB.
Comment: Thanks for your suggestion, the server is up and running after restart. I will try to use the command you mentioned. Thank you very much for your time.
Comment: I used mysqlcheck, it reported that everything is OK?
Comment: That message means things were fixed.  If everything was fine to begin with the message would have been already current.  Let the server run for a while and see if it crashes again.
Comment: Glad you got this worked out. Let us know if you run into any more issues!


Sorting a column in pandas is just returning the same unsorted information

Question: I am using pandas_datareader to pull yahoo stock data and calculate percent change. Now i want to take those percent change values and sort them from lowest to highest and put the sorted data in a new column. The column is being made but the values are not sorted, just copied over.
import pandas_datareader as pdr
Ticker = 'SQ'

result = pdr.get_data_yahoo(Ticker)
result = result.drop(['Open','High','Low','Volume','Adj Close'], axis=1)
result['pct change'] = (result.pct_change(1,'pad'))*100
result['Sorted'] = result['pct change'].sort_values(inplace = False)

The first few lines of output however look like this
                Close  pct change    Sorted
Date
2017-07-17  26.190001         NaN         NaN
2017-07-18  26.590000    1.527297    1.527297
2017-07-19  26.360001   -0.864983   -0.864983
2017-07-20  26.389999    0.113801    0.113801
2017-07-21  26.850000    1.743088    1.743088
2017-07-24  26.750000   -0.372439   -0.372439

I am quite new to coding so im assuming this is something simple but i havent been able to find anything helpful by googling
Comment: This actually does not seem to make much sense. Why would you add a sorted column to your data. The sorted one has not much to do with the original one.
Comment: `result['Sorted'] = result['pct change'].sort_values()` is enough.
Comment: @cᴏʟᴅsᴘᴇᴇᴅ i just tried your suggestion and the sorted column did not change.
Comment: All you need is result.sort_values('pct change')
Answer: I don't think it necessarily makes sense for you to be adding the sorted values as a column in the same DataFrame, given that the index on your DataFrame is a DatetimeIndex - now the sorted values do not correspond to your index properly.
A better solution would just be to hold the sorted values Series separately from your DataFrame.

With that being said, the reason that you aren't seeing any results after sort_values is that the sorted result retains the original DataFrame index. Then when you add the sorted Series as another column, the original indices on the sorted values place your sorted values back into original order in your DataFrame.
So your issue is precisely because Pandas is cognisant of the original indices of your sorted values.
To solve this, you could reset_index on the sorted values before setting the column.
result['Sorted'] = result['pct change'].sort_values().reset_index(drop=True)

Comment: Thank you your explanation and solution helped me make sense of it and fixed my problem!


Rewrite Results -> Output as Link: How do I get this to work?

Question: Ok, so I have a table in my database that I am outputting by using a combination of the data module to bring in a table from my database and the views module so it can be displayed.  The page with the view can be seen at http://apollo.tbc.iit.edu/~spaceweather/?q=distribute-data.
I want to enable the column fileSymlink to output as a link.
Below is an example of what the table looks like.

In order to try to enable the links in the last column:

Go to edit view
Click on the column in the fields section that corresponds to that column (i.e., fileSymlink)
Open the drop down "Rewrite Results"
Check to enable "Output this field as a link"

There is then a text form I need to fill out and I'm unsure what to put there. The text in the column is the link. For example if someone clicked on the text in the column DownloadData/cases/grid108/297/txt/channel_dataout_2013_297_0100.log, it would take them to
apollo.tbc.iit.edu/~spaceweather/DownloadData/cases/grid108/297/txt/channel_dataout_2013_297_0100.log.
Am I doing this correctly, or should I be actually storing the HTML in the columns and having views render HTML instead?
I'm not set on one way of doing this, if there is any better way. Any suggestion is very appreciated.
Answer: Don't the instructions for the link field tell you that you can use tokens in the URL?  I do not have a view with a text field handy, but here is a screenshot from a view where I make a custom link for the title field:

Scroll down to the Replacement Tokens and expand that field.  One of the options should be something like [field_fileSymLink].  Then you should be able to use something like http://apollo.tbc.iit.edu/~spaceweather/[field_fileSymLink] or maybe just /[field_fileSymLink].
Comment: Great thanks. I have not dealt with tokens yet.   I used the "[fileSymlink]" token by putting that text into the Link Path form there.

The only issue now is that it automatically puts "/?q=" before the [fileSymlink] so that the final link looks like: http://apollo.tbc.iit.edu/~spaceweather/?q=DownloadData/cases/grid108/297/txt/channel_dataout_2013_297_0100.log.  This is not the right path. I need to remove that /?q=
Comment: I fixed it.  To remove that "?q=" I did two things. I checked "External Server URL" to trick it into not using the ?q=.  For some reason that removed that part of it and I also was more explicit with the "Link Path" that i used.  The path i used is: "apollo.tbc.iit.edu/~spaceweather/[fileSymlink]".
Answer: For those who are wondering why the hell nothing happens, just uncheck Link this field to its taxonomy term page.


xPath attribute equals operator not working as expected

Question: I have the following xml document.
<?xml version="1.0" encoding="UTF-8"?>
<LevelOne>
    <LevelTwo>
        <LevelThree class="MyClass">
            <Property name="FirstName">hello</Property>
            <Property name="SecondName">bar</Property>
            <Property name="ThirdName">world</Property>
            <Property name="FourthName">foo</Property>
        </LevelThree>
    </LevelTwo>
</LevelOne>

When I run the following xPath query,
xpath my.xml //Property

Gives me the following results where all the  nodes are selected.

Now, I want to select the  node where the name attribute of the  node is equal to FirstName.
I came up with following xPath query after going through some examples on the internet.
xpath my.xml //Property[@name='FirstName']

And I end up getting the following result on both macOS and Ubuntu terminals.

What am I possibly doing wrong here?
Comment: Your expression is fine. Probably just a quotation problem. Try `xpath my.xml "//Property[@name='FirstName']"`
Answer: As pointed out in @Robby Cornelissen's answer, the issue seems to be with the quotations. Wrapping the query inside quotations as follows solved the problem.
xpath my.xml "//Property[@name='FirstName']"


Dynamically Rendering AlloyUI Modal body HTML via Javascript

Question: I have a form created via javascript that will be set as the content of a modal widget provided by Alloy UI. However I don't feel I am approaching this the right way. This question applies for javascript-created HTML in general.
I realize I should avoid using global variables for the sake of namespace and collision, so maybe someone can help me out. I declare the global var myForm and initialize it with some HTML elements to create a form:
var myForm = '<form id="my-form" method="get">

  <select>
    <option value="option-1">Option 1</option>
    <option value="option-2">Option 2</option>
    <option value="option-3">Option 3</option>
  </select>

</form>'

And a modal widget that sets myForm as the body content:
var modal = new Y.Modal(
    {
        bodyContent: myForm,
        centered: true,
        modal: true,
        resizable: false,
        draggable: false,
        width: 350
    }).render();

This seems like a not-so-great way of using Javascript to render HTML, especially for my project since I will have many html elements created via javascript ('s,
's etc), so I don't want to get into a bad coding habit. Is there a more standard or acceptable way to dynamically render HTML (not speaking from a templating perspective, i.e., Thymeleaf, etc)? Thanks all
Comment: `createElement` comes to mind !
Answer: An alternative solution is to specify the modal and it's contents in your html, and then let AlloyUI take over and turn it into a modal dialog. You can do this by specifying the boundingBox and contentBox of the Modal as the divs which contain the html you want inside the modal:

YUI().use('aui-modal', function(Y) {
  new Y.Modal({
    boundingBox: '#bb',
    contentBox: '#cb',
    centered: true,
    modal: true,
    resizable: false,
    draggable: false,
    width: 350
  }).render();
});
<link href="http://cdn.alloyui.com/2.0.0/aui-css/css/bootstrap.min.css" rel="stylesheet" />

    <form id="my-form" method="get">

        <select>
          <option value="option-1">Option 1</option>
          <option value="option-2">Option 2</option>
          <option value="option-3">Option 3</option>
        </select>

    </form>

Answer: I would go with declaring the template inside a script tag as described here:
Explanation of


Weibull plot to assess goodness of fit

Question: I would like show whether my data fit a Weibull distribution.
What kinds of graph should I plot?
Is the following graph appropriate one and what is this called?
How can I plot it in R?
Comment: Please credit the source of your image (as required by stackexchange rules). It looks like it came from here: https://www.itl.nist.gov/div898/handbook/eda/section3/weibplot.htm in which case, some of the questions you ask are answered there.
Comment: Yes. your are right. this graph comes from: itl.nist.gov/div898/handbook/eda/section3/weibplot.htm. sorry for that.
Comment: You need to credit the source in the question where you show it. Did you read the page it came from? It has some useful information. I do plan to update my answer here soon; I'm investigating a good choice of quantiles for a two-point line fit for estimating the slope at the moment.
Answer: The plot you display is sometimes called a Weibull plot.
See, for example, https://www.itl.nist.gov/div898/handbook/eda/section3/weibplot.htm
(which shows your exact plot)
and also https://en.wikipedia.org/wiki/Weibull_distribution#Weibull_plot
While it's not the only sort of plot you might want to do*, it's a reasonably common way of looking at goodness of fit for a Weibull distribution, and performs reasonably well. If a Weibull model is suitable, it should look close to straight. You should be aware that with Weibull data the lower left end may be considerably more "wiggly" than the upper right end; its behaviour is not symmetric. The log-data values have the long left tail of a flipped Gumbel distribution.
To my mind it makes more sense to interchange the axes from the way around your plot has it, so the random quantity is on the y-axis, just as is usually done with a normal Q-Q plot, and as is common practice with scatterplots more generally. I will do this in my answer, but it's easy to swap them back if you're determined to have the logged data on the x-axis.
It's important to generate Weibull data across a variety of shape parameter values and sample sizes to see what Weibull data can look like, as well as to generate some non-Weibull data (e.g. gamma, lognormal, chi). It's useful to see what the plot tends to look like at a few different sample sizes as well.
These plots are easy to do in R, following the information at the above two links.
Here's some sample code. This is just a quick outline of how to do it, not what I'd suggest for a proper function:
<code>    x <- rweibull(67,4.1,5.25) # just some random Weibull data
                               # to show an example

    p <- ppoints(x, .3)
    q <- log(-log(1-p))
    o <- order(x)
    plot(q,log(x[o]))
    coeffs <- lm(log(x[o])~q)$coefficients
    ests <- c(1/coeffs[2],exp(coeffs[1]),quantile(x,1-exp(-1)))
    names(ests) <- c("shape.line","scale.line","scale.quantile")
    ests
</code>
This gives a plot something like this one:

You could interchange the axes if you felt the need.
When working by hand, it would be easier to use a line joining two quantiles rather than a linear regression fit. Indeed, because of the tendency of the lower-left end to be noisy, it may be worth the slight robustness this brings. With actual Weibull data, it looks like taking the slope of the line joining the points for the  $0.167$ and $0.975$ quantiles may work well, but making the intercept of the drawn line correspond to the quantile-estimate of the scale parameter, at the $(1-1/e)$ quantile. It may be worth giving up a little efficiency to gain more robustness by moving the slope quantiles in somewhat further. Another alternative would be to use the quartiles, as is done for the normal Q-Q plot in R.
If you need the axes scaled in a similar way to the plot you show, you could replace the 'plot' command above with something like:
<code>    qax=c(0.1,0.5,1,5,10,20,50,90,99,99.9)
    plot(q,x[o], ylab="x", xaxt="n", log="y",
           xlim=range(log(-log(1-qax/100))), ylim=range(x))
    axis(1, at=log(-log(1-qax/100)), labels=qax)
</code>

but it might be worth pulling the range on the theoretical-quantiles axis (what your plot calls "Weibull probability") in to just fit the data, which you can work out (e.g. from the number of data-values).
There are numerous posts relating to Weibull distributions, Weibull plots and other ways of looking at Weibull goodness of fit on site. e.g. try searches, such as a search for Weibull plot.

*(e.g. you might do a Q-Q plot or a P-P plot using fitted parameters, or an ecdf with fitted Weibull cdf)

Weibull plot to assess goodness of fit


Change the state of a checkboxes field

Question: I know how to use conditions with Form API for fields like text-field, checkbox, for example, showing or hiding a field when a checkbox is checked.
$form['lorem'] = [
  '#type' => 'checkbox',
  '#attributes' => [
    'name' => 'field_lorem',
  ]
];

$form['ipsum'] = [
  '#type' => 'checkbox',
  '#states' => [
    'visible' => [
      ':input[name="field_lorem"]' => ['checked' => true]
    ],
  ],
];

However, I can't manage to do it with a checkboxes field.
// This code does not work.
$form['lorem'] = [
  '#type' => 'checkbox',
  '#attributes' => [
    'name' => 'field_lorem',
  ]
];

$options = [
  'test_1' => 'Test 1',
  'test_2' => 'Test 2',
];

$form['ipsum'] = [
  '#type' => 'checkboxes',
  '#options' => $options,
  '#states' => [
    'visible' => [
      ':input[name="field_lorem"]' => array('checked' => true)
    ],
    // Ideally, I would like to have all the checkboxes checked but the problem is mainly at the state level
    'checked' => [
      ':input[name="field_lorem"]' => ['checked' => TRUE],
    ],
  ],
];

I can do it with custom JavaScript code, but I ideally wanted to do it with the form API.
What #states property do I need to achieve this?
Comment: Does this answer your question? [Using #states in a form with checkboxes](https://drupal.stackexchange.com/questions/290079/using-states-in-a-form-with-checkboxes)
Answer: You need to add #states to every checkbox.
$form['select_all'] = [
  '#title' => $this->t('Select all'),
  '#type' => 'checkbox',
  '#attributes' => [
    'name' => 'all'
  ]
];

$options = [
 'item_0' => 'value',
 'item_1' => 'value',
 'item_2' => 'value',
];

$form['my_checkboxes'] = [
 '#type' => 'checkboxes',
 '#options' => $options,
 'item_0' => [
    '#states' => [
      'checked' => [
        ':input[name="all"]' => [
          'checked' => TRUE
        ]
      ]
    ]
  ]
];

foreach ($options as $key => $item) {
  $form['my_checkboxes'][$key]['#states']['checked'][':input[name="all"]']['checked'] = TRUE;
}

Answer: You can wrap the checkboxes in a details (fieldset).
$form['controlling_checkbox'] = [
  '#type' => 'checkbox',
  '#title' => ['Switch me on and off'],
];
$options = ['a' => 'b', 'c' => 'd'];
$form['wrapper'] = [
  '#type' => 'details',
  '#title' => 'Wrap me up',
  '#collapsible' => TRUE,
  '#open' => TRUE,
  '#states' => [
    'disabled' => [
      ':input[name="controlling_checkbox"]' => [
          ['checked' => TRUE],
      ],
    ],
  ]
];
$form['wrapper']['lots_of_boxes'] = [
  '#type' => 'checkboxes',
  '#options' => $options,
];

Change the state of a checkboxes field


Is there a theorem like this in analysis?

Question:
If $f$ is continuous on $(a,b)$ (possibly at the end points as well), then $F(x) = \int_{a}^{x} f(x)dx$ is also continuous for some $x \in (a,b)$.

I know I am missing some other assumptions that are needed because the above statement is not true in general since the continuity of $f$ only yields differentiability, which we know is not enough for continuity (Weiestrass Functon)
Answer: I think you've gotten yourself confused - the Weierstrass function is continuous but not differentiable, not vice versa. In fact differentiability implies continuity in general; so the indefinite integral of any continuous function is always continuous by the fundamental theorem of calculus.
Comment: Well, the [Dirac delta function](http://en.wikipedia.org/wiki/Dirac_delta_function) is used in some applied mathematics, so while not strictly a function in standard analysis, it does have its uses and introduces a discontinuity in $F(x)$.
Comment: You are right. I have really made a blunder this time.
Comment: You mean to say the indefinite integral of any continuous function is differentiable, as a consequence of FTC?
Comment: @Arthur, I thought the Dirac Delta is not a function...
Comment: @treble, he probably does mean that.
Comment: @treble: oops, fixed.
Comment: @sidht And I do say it's not a function, if you read my comment. But it still has its uses, for instance as modeling the charge of an electron at a point in space.


Scala worksheet not evaluating the REPL and printing the result on right side

Question: I am learning spark and using eclipse ScalaIDE, in which i am creating a new worksheet.
As the worksheet in scala is evaluated when we save the file and result are printed in the right side, but this is not happening when i make some changes in the worksheet.
Here is my scala worksheet:
object LearingScalaWorkshheet1 {
  println("Welcome to the Scala worksheet")

  val hello: String = "Hello World!"
  println(hello)
}

Is there anything which i am missing?
Comment: Maybe you have a space in your project name? (From: https://stackoverflow.com/questions/17995704/how-to-get-inline-results-in-a-worksheet-in-eclipse-scala-ide-on-ubuntu-12-04)
Answer: Changing the scala installation to default version solved the problem for me.
Follow these steps:

Right click on project -> Properties -> Scala Compiler
Click the drop down against Scala Installation.
Change the value in drop down to "Latest 2.12 bundle (dynamic)"
Answer: If you write just this It Will write It properly:
val hello: String = "Hello World!"
println(hello)

Otherwise, you need yo use your object after defining it


Create custom table with pagination

Question: I have a function in a controller which shows data in table format. I need pagination and styling for the table. I tried as follow.
MyController.php
public function my_requests() {
        //load the current user details
        $user= User::load(\Drupal::currentUser()->id());
        $user_id = \Drupal::currentUser()->id();

        //Query the database
        $query = \Drupal::entityQuery('node')
           ->tableSort($header)
           ->condition('status', 1)
           ->condition('type', "forward_proxy_request")
           ->condition('uid', $user_id)
           ->sort('nid' , 'ASC');
        $table_sort = $query->extend('Drupal\Core\Database\Query\TableSortExtender')->orderByHeader($header);
        $pager = $table_sort->extend('Drupal\Core\Database\Query\PagerSelectExtender')->limit(10);
        $nids = $pager->execute();

        //creates the table header
        $header = [
                'Node id',
                'title',
                'Name',
                'Ip',
                'SLU',
            ];
        foreach ($nids as $nid) {
            $node = Node::load($nid);
            $row  = [
                'data' => [
                    $node->nid->value,
                    $node->title->value,
                    $node->field_p_name[0]->value,
                    $node->field_ip[0]->value,
                    $node->field_p_slu[0]->value,
                ],
            ];
            $rows[] = $row;
        }
        //Build the table
        $build = [
          'table'           => [
          '#prefix'        => '
My Requests
',
          '#theme'         => 'table',
          '#attributes'    => [
              'data-striping' => 0
          ],
          '#header' => $header,
          '#rows'   => $rows,
      ],
    ];
    //for pagination
    $build['pager'] = array(
        '#type' => 'pager'
    );

     return $build;
}

But it throws an error.

Error: Call to undefined method Drupal\\Core\\Entity\\Query\\Sql\\Query::extend() in
  /var/www-drupal/modules/ga/src/Controller/MyController.php on line
  196

Without extendQuery, it shows a plain table without any style.

How do I resolve this issue?
Answer: Here is an example of sortable table with pagination.
$header = [
  'id' => [
    'data' => $this->t('ID'),
    'specifier' => 'nid',
  ],
  'title' => [
    'data' => $this->t('Title'),
    'specifier' => 'title',
  ],
  'created' => [
    'data' => $this->t('Created'),
    'specifier' => 'created',
    // Set default sort criteria.
    'sort' => 'desc',
  ],
  'uid' => [
    'data' => $this->t('Author'),
    'specifier' => 'uid',
  ],
];

$storage = \Drupal::entityTypeManager()->getStorage('node');

$query = $storage->getQuery();
$query->condition('status', \Drupal\node\NodeInterface::PUBLISHED);
$query->condition('type', 'article');
$query->tableSort($header);
// Default value is 10.
$query->pager(15);
$nids = $query->execute();

$date_formatter = \Drupal::service('date.formatter');
$rows = [];
foreach ($storage->loadMultiple($nids) as $node) {
  $row = [];
  $row[] = $node->id();
  $row[] = $node->toLink();
  $created = $node->get('created')->value;
  $row[] = [
    'data' => [
      '#theme' => 'time',
      '#text' => $date_formatter->format($created),
      '#attributes' => [
        'datetime' => $date_formatter->format($created, 'custom', \DateTime::RFC3339),
      ],
    ],
  ];
  $row[] = [
    'data' => $node->get('uid')->view(),
  ];
  $rows[] = $row;
}

$build['table'] = [
  '#type' => 'table',
  '#header' => $header,
  '#rows' => $rows,
  '#empty' => $this->t('No content has been found.'),
];

$build['pager'] = [
  '#type' => 'pager',
];

Comment: Thanks, very useful
Answer: Query extenders is a concept of low-level database queries. Content entities don't have that. The extenders just add the ability to do something, they don't do anything out of the box.
Both the pager and table sort functionality is built-in in entity queries. Just call pager() and tableSort() as you would do with a raw query after adding the extenders.
Comment: Hows the working of **tableSort()** ? How can i add **ASC** and **DESC** buttons to table headers?   Pager is working well .
Comment: hi how can add sorting facility to headers(with up and down arrows) like in data tables ?

Create custom table with pagination


Essa é uma prática comum em orientação a objetos?

Question: Em orientação a objetos uma forma de garantir o encapsulamento é manter os atributos das classes privados e modificar seu estado via métodos. Além disso, para garantir a integridade dos objetos, é comum exigirmos que dados essenciais para a existência do objeto sejam passados no construtor.
Em classes com poucos atributos isso funciona bem. Acontece que em classes com um número muito grande de atributos, começa a ficar meio estranho. Um exemplo disso é essa classe PessoaJuridica (que eu reconheço não estar muito boa):
public class PessoaJuridica
{
    public string NomeFantasia { get; set; }

    public string RazaoSocial { get; set; }

    public string Endereco { get; set; }

    public string Bairro { get; set; }

    public string Cidade { get; set; }

    public string Estado { get; set; }

    public int CEP { get; set; }

    public int Telefone1 { get; set; }

    public int Telefone2 { get; set; }

    public int Fax { get; set; }

    public int CNPJ { get; set; }

    public int InscricaoEstadual { get; set; }

    public int InscricaoMunicipal { get; set; }

    public DateTime DataConstituicao { get; set; }

    public DateTime DataCadastro { get; set; }

    public string Observacoes { get; set; }
}

A classe tem diversos atributos. Passar tudo isso no construtor é inviavel, e mesmo que escolhesse um subconjunto para passar ainda ficaria ruim. Além disso construir um método para cada atributo não teria muita vantagem em relação ao que já está ai. Por isso pensei na seguinte melhoria:
Olhei na classe algumas coisas que fazem sentido agrupadas. Por exemplo:

Endereço, Bairro, Cidade, Estado, CEP formam coletivamente o Endereço da empresa
Telefone1, Telefone2 e Fax são tipos de telefones
CNPJ, InscricaoEstadual, InscricaoMunicipal e DataConstituicao formam uma documentação da empresa

Por isso codifiquei uma struct Endereco com propriedades "Rua, Numero, Bairro, Cidade, Estado, CEP", uma struct Telefone com propriedades "DDD, Numero, TipoTelefone" sendo "TipoTelefone" um enum que pode ser "Fixo" ou "Fax". Finalmente codifiquei uma struct DocumentacaoEmpresa com atributos "RazaoSocial, CNPJ, InscricaoEstadual, InscricaoMunicipal e DataConstituicao". Com isso fiquei com a classe
public class PessoaJuridica
{
    public string NomeFantasia { get; private set; }

    public Endereco Endereco { get; private set; }

    public IList<Telefone> Telefones { get; private set; }

    public DocumentacaoEmpresa Documentacao { get; private set; }

    public string Observacoes { get; set; }
}

E aí fica mais fácil usar métodos e o construtor para lidar com essa classe. O que eu fiz foi pegar uma classe com vários atributos, agrupar esses atributos em conceitos que fazem sentido e codificar esses conceitos. Além disso, agora tem uma lista de telefones para obedecer o Open/Closed principle.
O que eu gostaria de saber é: essa é uma prática comum na orientação a objetos? A construção dessas structs não foi feita por causa de serem conceitos do domínio, mas sim para melhorar a escrita de uma classe. Existe algum design pattern, ou algo do gênero sobre isso?
Eu sei que não é necessário um design pattern para algo ser válido, mas queria saber se isso é comum e se existe mais informação sobre isso para saber se em geral é uma boa ideia fazer esse tipo de coisa.
Answer: Solução para o problema
Primeiro vou te dar a solução simples para seu problema de inicializar uma quantidade grande de propriedades:
var pessoa = new PessoaJuridica {
    NomeFantasia = "abc",
    RazaoSocial = "abc",
    Endereco = "rua abc",
    Bairro = "abc",
    Cidade = "abc",
    Estado = "sp",
    CEP = "12345678",
    Telefone1 = "12345678",
    Telefone2 = "12345678",
    Fax = "12345678",
    CNPJ = "11111111000111",
    InscricaoEstadual = "11111111111",
    InscricaoMunicipal = "1111111",
    DataConstituicao = new DateTime(2000, 4, 15),
    DataCadastro = new DateTime(DateTime.Today),
    Observacoes = "abc"
}

Coloquei no GitHub para referência futura.
Isto é chamado de object initializer.
Mudei alguns para string porque não faz sentido eles serem inteiros. Só porque um dado possui apenas dígitos não significa que ele seja um número. CEP, telefone, CNPJ são identificadores e não números.
Note que o que você fez não facilita a criação da instância. Pelo contrário, você ainda terá que passar todos os dados, mas agora de uma forma uma pouco mais extensa. Não estou dizendo que não deva fazer para organizar, mas não simplifica a criação do objeto.
Por que existe o construtor?
Nem sempre um construtor é necessário, pelo menos em C#. Há linguagens que se viram bem sem um.
Construtores devem dar atomicidade na criação do objeto. Ou seja, ou cria o objeto em um estado válido ou não cria. Se você precisa validar os dados e se alguma validação falhar o objeto não deve ser criado, então um construtor ajuda bastante. O mesmo vale se algum "cálculo" é necessário com os parâmetros iniciais antes de criar o objeto.
Então podemos concluir que só faz sentido passar para um construtor dados que precisam existir obrigatoriamente em um objeto de forma válida ou que precisam ser processados antes de serem inseridos no objeto.
O inicializador de objeto ajuda nesta atomicidade, mas não substitui completamente o construtor. No C# 6 fica até melhor com os inicializadores de propriedade onde é possível fazer um public string NomeFantasia { get; set; } = "" (só exemplo, claro que essa inicialização não é desejável). Se a validação dos dados são individuais isto funciona bem, mas se a validação é interdependente aí só construtor vai resolver elegantemente.
O que substitui o construtor então? A princípio nada. Se você precisa do que ele oferece só ele pode fornecer. O que é possível é criar uma classe utilitária para criar um objeto temporário e depois jogar este objeto para a classe definitiva mas raramente isto faz algum sentido. Fiz uma pergunta sobre isto.
Separar algumas propriedades em classes auxiliares não ajuda em nada nisto. Pelo contrário, é possível - embora não acho que seja o caso do exemplo demonstrado - quebrar a atomicidade da criação do objeto fazendo desta forma. Mesmo que não quebre, possivelmente complicará a criação do objeto principal tendo que verificar coisas em dois lugares. Sem falar na criação de objetos intermediários que seriam descartados se a criação do objeto principal não for completada.
Mais informações sobre construtor.
Modelagem adequada
Se o problema era a quantidade de parâmetros do construtor e você resolveu encapsular algumas propriedades em outras classes e fazer uma composição só para resolver este problema, então posso dizer que isto não é comum. Você está organizando seus dados para atender uma necessidade do mecanismo da linguagem. Isto definitivamente não é comum, pelo menos em linguagens expressivas, como é o caso do C#.
Então tenho minhas dúvidas se fica mais fácil usar métodos e construtor para lidar com a classe como foi afirmado.
Se criou porque acha que faz sentido organizar os dados assim, ok, é comum fazer isto. Mas não quer dizer que sempre ou quase sempre é feito isto. Depende do caso, depende do seu problema, dos requisitos estabelecidos, do entendimento de como precisa modelar os dados.
Endereço
Existe alguma razão para organizar o endereço em uma classe? Tem alguma vantagem em ter esses dados separados? Você pode precisar desses dados de forma separada da pessoa jurídica? Em modelos simples em geral não precisa, em modelos mais complexos realmente pode ter vantagens em ter separado e bem definido. Quem é mais purista, vai dizer que deveria ser feito sempre.
Documentação
O mesmo pode ser dito para a documentação, mas a conclusão pode ser diferente. Acho que existe menos motivo para tê-los encapsulados. Eu consigo ver o endereço como uma entidade autônoma, mas eu acho que cada documento já é uma entidade autônoma e não consigo ver muito sentido em agrupá-los a não ser para atender a necessidade do mecanismo, o que já foi dito que não é o ideal. Mas a necessidade real do domínio trabalhado pode dizer o contrário. Por isto não gosto de boas práticas (o grande problema é que a maioria das pessoas não sabem usá-las adequadamente), elas ignoram o problema real.
Telefones
Não sei se entendi mas acho que criar uma lista de telefones não tem nada a ver com o princípio Open/Close. De qualquer forma o motivo para criar uma lista é o fato do número de telefones ser indeterminado. Note que a sua refatoração não produz o mesmo resultado. No primeiro você tem dois telefones quaisquer e um fax. No segundo modelo você pode ter de zero à infinitos telefones - embora dê para restringir isto com algoritmos - e você talvez não saiba o que cada um representa, todos são genéricos - claro que nesta classe, você pode ter uma propriedade extra que determina que tipo de telefone é.
Conclusão
Não sei dizer ao certo se é prática comum, nem sempre é fácil dizer isto com propriedade a não ser em casos bastante óbvios. Mas me parece ser a solução errada. Você mesmo deu o motivo quando diz que não está fazendo por causa do domínio.
Eu não sou purista e se eu realmente precisar, se tiver um motivo muito bom para fazer algo por causa do mecanismo, eu farei, mas sempre que puder eu evitarei, só não será a qualquer custo.
O design pattern que você está fazendo é composição. Mas se pensar bem todas as propriedades fazem composição. Não precisa ser uma propriedade complexa para compor a classe. Quando se fala em preferir composição a herança é que deve-se preferir o jeito mais óbvio e natural de montar um tipo que é a composição. Este é o jeito intuitivo que fazemos e nem percebemos que estamos fazendo ele em todo tipo sendo criado.
Nessa resposta eu já disse que padrões estão em todo lugar.
Comment: Obrigado pela resposta @bigown. O que eu falei do Open/Closed foi no seguinte sentido: no momento da codificação existem dois telefones e um fax. Posteriormente pode ser necessário adicionar mais um telefone, ou um segundo fax. Do jeito que estava seria necessário modificar o código, no jeito novo é possível adicionar a funcionalidade sem modificar: é só adicionar um novo número a lista. Essa é a ideia do Open/Closed não é? Ser capaz de adicionar coisas sem modificar o que já existe. Talvez eu tenha entendido mal o Open/Closed.
Comment: Não sigo esses princípios. Eu até sigo por coincidência. Por acaso eu faço algo que se encaixa em algum deles, mas não fico perseguindo-os. Tem situações que simplesmente é necessário mudar a classe, seria ridículo ficar criando uma classe filha toda vez que precisa de uma mudança. Então tenho minhas dúvidas se o princípio se aplica neste caso. De qualquer forma acho que ele fala mais sobre comportamento. Embora tecnicamente propriedades são comportamentos, no fundo eles são apenas *proxies* para o estado. Então não sei se é correto ou não. Mas *eu* não me preocuparia com isto. Não o aplicaria
Comment: O telefone não é o único problema. Quer dizer que não pode colocar uma propriedade nova? Porque se o O/C for isso, os sistemas ficam extremamente inflexíveis. A sua preocupação com o telefone faz sentido, conforme eu mostro na resposta, mas acho que ela é independente do problema do O/C que é um problema generalizado. Outra coisa que estava pensando que o problema do O/C só aparece de fato quando há herança. Classes que possuem filhas deveriam ser congeladas mesmo. Eu entendo que qualquer mudança pode gerar efeitos indesejáveis mas é impraticável ficar modificar certas classes.
Comment: Um dos motivos para se evitar herança é para evitar esses princípios que até válidos. Eles tentam resolver problema que a orientação a objeto possui. Eles mostram o caminho que você deve seguir para evitar o que é sabido que trará problemas. Por isso alguns acham que o *default* de classes deveriam ser as seladas, que não podem sofrer herança. No momento que você cria filhas da classe você passa ter responsabilidades e compromissos. Assim como na vida real, isto nem sempre é desejável. Você perde a liberdade de fazer o que quiser com a classe sob pena de quebrar as filhas.
Comment: Não totalmente relacionado mas mostra o problema desses princípios e "Boas práticas": http://sklivvz.com/posts/i-dont-love-the-single-responsibility-principle. Em geral essas coisas são quase arbitrárias. Por isso eu prefiro coisas mais genéricas que dê a ideia geral do que é para fazer. Quando fica muito específico agrada mais as pessoas porque dizem para elas que não sabem o que fazer o que elas devem fazer, mesmo isto sendo uma estupidez. Eles são criados para vender livros, consultorias, etc. É *snake oil*. A intenção e o fundamento deles são bons mas o resultado é desastroso.
Answer: Utilizo a linguagem Java, porém é muito parecido C#.
O que você poderia fazer é criar uma classe abstrata chamada Terceiro (classes abstratas não podem ser instanciadas) e criar uma classe chamada Juridica, não abstrata que extende de Terceiro (você pode instanciar a classe Juridica). E para os Telefones, como vi acima, você pode fazer uma classe somente para os telefones, sem precisar ter dois atributos Telefone1 e Telefone2.
No Java ficaria assim, acredito que não é muito diferente do C#:
public abstract class Terceiro{

}

public class Juridica extends Terceiro {

}

Atributos como RG, Nome, Telefone seriam colocados na classe abstrata Terceiro e atributos como o CNPJ na classe Juridica.
Outras classes como a Física poderiam extender de Terceiro e esta poderia ter um atributo chamado CPF.
Seria esta uma ótima prática de orientação a objetos, espero que ajude.
Comment: No C# você troca o `extends` pelo `:`, e vai adicionando as demais implementações com `,`, seguindo a ordem de `NomeClasse : ClasseBase, Implement1, Implement2, etc` :D
Answer: Sim, até tem um nome modelo anêmico.
O que não quer dizer que esteja certo.
Seu segundo exemplo já esta mais alinhado com a ideia da orientação a objetos.
Um link pra entender um pouco mais:
https://blog.caelum.com.br/o-que-e-modelo-anemico-e-por-que-fugir-dele/


Como ordenar uma lista de palavras fornecidas pelo usuário utilizando Python?

Question: Estou tentando criar um programa em que o usuário informa a lista de palavras a colocar em ordem alfabética, só que quando eu colocava o programa pra rodar, ele funcionava normalmente, porém a lista que ele me dava estava listando letra por letra e citando as vírgulas e o espaços também, só que eu queria que ele listasse só as palavras ordenadas em forma de lista.
Vou deixar o script e a imagem do resultado aqui:
print('Digite as palavras desejadas para colocar em ordem alfabética.')
resposta = list(input())
resposta.sort()
print('Aqui está sua lista de palavras em ordem alfabética:')
print(resposta)

Answer: O problema é que, quando você passa uma string para o construtor list, todos os caracteres da string serão convertidos em elementos individuais da lista.
Você precisa, então, pegar a string (com a função input), dividi-la por espaços para obter cada palavra e, enfim, sorteá-la.
Ficaria assim:
resposta = sorted(input().split(" "))

Aqui estamos, basicamente, pegando a resposta do usuário (com a função input), dividindo a resposta pelos espaços utilizando o método split (operação essa que já retorna uma lista) e, enfim, sorteando-a com a função sorted.
Em suma:
print('Digite as palavras desejadas para colocar em ordem alfabética.')

resposta = sorted(input().split(" "))

print('Aqui está sua lista de palavras em ordem alfabética:')
print(resposta)

Mas o problema com a abordagem acima é que ela não retorna somente as palavras. Se, por exemplo, passa-se como entrada "Olá, como vai a sua vida?", receberíamos:
['Olá,', 'a', 'como', 'sua', 'vai', 'vida?']

O que mostra que a classificação está funcionando corretamente, embora caracteres como a vírgula mantenham-se como parte de uma "palavra".
Para resolver isso, pode-se dividir a entrada fornecida por uma expressão regular. Assim:
import re

regex = re.compile("\W+", re.U)
resposta = sorted(filter(len, regex.split(input())))

print(resposta)

Desse modo, com a mesma entrada anterior, obteríamos:
['Olá', 'a', 'como', 'sua', 'vai', 'vida']

A diferença se dá porque estamos utilizando a expressão regular \W+ para dividir a string fornecida pelo usuário, de modo que a string será dividida por qualquer caractere que não seja alfanumérico. Também foi utilizado filter(len, <list>) para remover "strings vazias" do output, o que pode ocorrer em alguns casos.

E, só para não deixar de falar — por padrão, sorted (evidentemente) diferencia letras em caixa alta ou baixa. Se você quiser ignorar esse comportamento na ordenação, pode passar o argumento nomeado key como str.casefold, que converte todas as letras para lowercase. Assim:
sorted(<list>, key=str.casefold)

É muita coisa, eu sei, mas detalhes são importantes. :-)
Referência

https://stackoverflow.com/a/6314634/7445826
https://stackoverflow.com/a/3845453/7445826
https://stackoverflow.com/a/13209453/7445826
https://stackoverflow.com/q/10269701/7445826
Comment: Obrigado por responder :)


Como incluir uma nova nota em um array?

Question:
Faça um programa que calcule o mostre a média aritmética de N
  notas utilizando array.

Eu estou tentando fazer isso:
package exer4;

import java.util.Scanner;

public class Exer4 {

   public static void main(String[] args){

   int nota[] = {4,5,7,4,3,6,6,8,3,4};
       int perguntar = in.nextInt();
   int aluno = 0;
   int soma = 0;

   while(aluno < 10){
    System.out.println("Aluno: " + aluno + " Nota: " + nota[aluno]);
    aluno++;
   }
   for(int i = 0; i < nota.length; i++){
    soma = soma + nota[i];

    System.out.println(soma);
   }

}
}

Fiz desse outro modo:
package exer4;

import java.util.Scanner;

public class Exer4 {

    public static void main(String[] args){
       Scanner in = new Scanner (System.in);

    int nota[] = {1,4,5,7,6, inserir};
        int inserir = in.nextInt();
        System.out.println("Digite as suas notas e espere a media");
        System.out.println("Sua nota pode ser:" + nota);
        System.out.println("Sua media pode ser:" + nota / 2);

    }
 }

Estou tentando desse outro modo também:
package exer4;

import java.util.Scanner;

public class Exer4 {

    public static void main(String[] args){
       Scanner in = new Scanner (System.in);

    int nota[] = {1,4,5,7,6, inserir};
        nota[0] = in.nextInt();
        System.out.println("Digite as suas notas e espere a media");
        System.out.println("Sua nota pode ser:" + nota);
        System.out.println("Sua media pode ser:" + nota / 2);

    }
 }

Comment: Alguma das respostas resolveu sua dúvida? Acha que pode aceitar uma delas? Veja o [tour] como fazer isso, se ainda não o fez. Você ajudaria a comunidade identificando qual foi a melhor solução para você. Pode aceitar apenas uma delas. Mas pode votar em qualquer pergunta ou resposta que achar útil no site todo (quando tiver pontuação suficiente).
Answer: Não pode. Você tem algumas opções, criar um array bem grande suficiente para todas elas, o que pode provocar um consumo de memória desnecessário, mas que não faz diferença real em muitos casos, ainda mais em Java e para um exercício. Ou fazer o que todo mundo faz que é usar uma lista no lugar de um array, que costuma ser recomendado mesmo que não precise adicionar nada. Só não vou falar que em Java não tem desvantagem alguma porque é uma linguagem verbosa e exige escrever muito mais código para manipular uma lista do que uma array, mas em outras linguagens não tem desvantagem real sensível, e tem vantagens. Uma outra alternativa seria recriar o array maior e copiando tudo de uma para o outro toda vez que não couber novos elementos, que no fundo é o que a lista faz, só que ela faz de um jeito bem pensado que você provavelmente não fará.
Um array não pode crescer de tamanho, depois que cria ele fica com aquele tamanho.
Porém o enunciado não pede para adicionar novos itens no array, ele pede apenas para criar um array com uma certa quantidade de elementos. Então peça quantos elementos terá, o tal do N que o enunciado fala e depois crie o array com o tamanho necessário.
Todos códigos parecem ter coisas meio sem sentido como inicializar um array que não usará esses valores, ou pedir um dado que não é usado, ou preferir um while onde um for funciona melhor, ou usar um laço extra onde a operação pode ser resolvido no mesmo laço já usado antes e não fez a média, além de imprimir as uma acumulando em cada passo, enfim, o código está bem caótico. O segundo e terceiro código não fazem sentido.
Seria algo assim:
import java.util.Scanner;

class Exer4 {
    public static void main(String[] args) {
        Scanner in = new Scanner (System.in);
        int alunos = in.nextInt();
        int notas[] = new int[alunos];
        int soma = 0;
        for (int i = 0; i < alunos; i++) {
            notas[i] = in.nextInt();
            soma += notas[i];
        }
        System.out.println(soma / alunos);
    }
}

Veja funcionando no ideone. E no repl.it. Também coloquei no GitHub para referência futura.

Como incluir uma nova nota em um array?


SQL Server select and combine values across rows with equal columns

Question: There are a couple questions I have found that touch a little on this but nothing that fits all my needs in this case. Unfortunately, I am not good enough at SQL yet to put them all together.
Basically I am selecting several rows from a table and I want to only select rows with distinct values for one column but to combine values from all the rows that are hidden from a different column and show that sum in the row that does show.
Also, I would like to select the distinct values from just 1 column.
Here is an example:
ID  #_of_Items  Place   Description
=======================
1        2       CA     towels
2        4       NY     shirts
3        1       MA     hats
4        3       CA     shorts
4        2       CA     pins
4        4       NY     socks

and I want to select:
Place    #_of_Items
=======================
CA        7
NY        8
MA        1

only need the two columns - basically, the place is unique, but it takes the sum of all the #_of_items from the hidden rows and adds them to the one row for each state it shows.
Then, if possible, I would also like to select a combination of all the distinct entries in the description column, so:
"towels, shirts, hats, shorts, pins, socks"

I hope that makes sense.
Thank you so much in advance everyone!
Answer: If you can create user defined functions in your database (replace  with whatever your table name is):
CREATE FUNCTION dbo.JoinItems(@place nvarchar(10))
    RETURNS NVARCHAR(MAX)
AS
BEGIN
    DECLARE @list NVARCHAR(MAX)
    SELECT @list = CASE WHEN @list IS NULL THEN N'' ELSE @list + N', ' END + description
      FROM

     WHERE place = @place
    RETURN @list
END
GO

Which you can then use in your query to return the item count along with the list of items specific to that place (again, replacing  with your table name):
SELECT place,
       [#_of_Items] = SUM([#_of_Items]),
       Items = dbo.JoinItems(place)
  FROM

 GROUP BY place

Answer: SQL 2005+:
SELECT ID, Place, Description
      SUM(#_of_Items) OVER(PARTITION BY Place) AS #_of_Items
FROM Your_Table

Comment: thank you for your response! I will give it a shot along with the one above it.


Standard reference for Feynman-Kac Formula

Question: Is there any standard reference for the "modern" Feynman-Kac formula as stated here?
By modern I don't mean the earliest version by Kac (for which the reference seems to be this work: Kac, Mark (1949). "On Distributions of Certain Wiener Functionals". Transactions of the American Mathematical Society. 65 (1): 1–13.), but the version which is cited in the link above or used in most Finance Textbooks when it comes to solving the Black-Scholes PDEs.
The textbooks I read just use it without any reference and a short google-search couldn't reveal any helpful documents.
I'm thankful for any hints and links to this - admittedly not very precise - question.
Comment: I'm voting to close this question because the Feynman-Kac formula is treated in pretty much every book about SDEs by now. No one should have trouble finding his/her favourite modern source about it.
Answer: This should be in any book on stochastic calculus. For example Klebner's book Introduction to Stochastic Calculus with Applications has a proof on page 155, plus all the ingredients for it before. I'm sure other books like Oksendal etc. should likely have it as well. I just like Klebner's book.
If you're interested, I did my undergrad senior thesis on Feynman Kac formula which can be found here https://www.math.ucdavis.edu/undergrad/research/thesis/ or my website https://www.math.purdue.edu/~zselk/
ADDED: as of 22 July 2023 these links are dead.
Comment: Long live [this link](https://en.wikipedia.org/wiki/Feynman%E2%80%93Kac_formula)! I am really surprised that people have so much trouble finding material about such a famous formula which is handled in every book treating SDEs. Another one: Karatzas & Shreve, *Brownian Motion and Stochastic Calculus*.
Comment: Re: the dead link to Zachary Selk's thesis, the link leads to the title via the Wayback Machine, and a search on the title leads to a current link to the PDF: "Connecting Brownian Motion and Partial Differential Equations with Applications in Statistical and Quantum Mechanics," https://www.math.ucdavis.edu/~webfiles/undergrad_thesis/201410_Zachary_Selk_Deckert.pdf.
Comment: See also Google Scholar for a replacement for the dead link: https://scholar.google.ca/citations?view_op=view_citation&hl=en&user=p25XJPIAAAAJ&citation_for_view=p25XJPIAAAAJ:d1gkVwhDpl0C


Seeking a closed form for a posterior distribution

Question: In the book Bayesian Data Analysis by Gelman et al. (3rd edition, 2014), a hierarchical model (or one-way random-effects ANOVA) is presented in section 5.4 as follows,
\begin{equation}\label{eq:lme1}
y_{ij} = b_0 + \lambda_i + \varepsilon_{ij},
\end{equation}
where the data $y_{ij}$ come from the $i$th measuring entity (e.g., student performance in a school district) collected under the $j$th condition (e.g., a school within the district), $b_0$ is the population mean, $\lambda_i$ is the deviation of the $i$th measuring entity from the population mean, and $\varepsilon_{ij}$ is the measuring error ($i=1, 2, \ldots, k;\ j=1, 2, \ldots, n$).
A posterior inference is derived in the book for the effect of each measuring entity $\theta_i=b_0 + \lambda_i$ based on a Gaussian assumption with a known variance $\sigma^2$ for the residuals $\varepsilon_{ij}$ and a prior distribution $G(0, \tau^2)$ for $\lambda_i$. Specifically, the mean and variance for $\theta_i$ are estimated as below:
\begin{align}
{\rm mean}(\theta_i) &= \frac{\frac{n}{\sigma^2}\bar{y}_{i\cdot}+\frac{1}{\tau^2}b_0}{\frac{n}{\sigma^2}+\frac{1}{\tau^2}} \\[7pt]
{\rm Var}(\theta_i) &= \frac{1}{\frac{n}{\sigma^2}+\frac{1}{\tau^2}}
\end{align}
where $\bar{y}_{i\cdot}=\frac{1}{n}\sum_{j=1}^n y_{ij}$.
Even though the variance for the $\lambda_i$ is assumed to be known, I could solve the model as a mixed-effects model through, for example, function lmer() in the R package lme4, and use the estimated variances $\tau^2$ and $\sigma^2$ to obtain the posterior distribution using the formulation above. Is this a reasonable and solid approach?
I know that I could directly obtain the posterior distribution through R packages such as brms and rstanarm. However, the computational cost is too heavy in my case, and that's why I'm trying to see if the above closed form is a a reasonable approach to directly obtaining the posterior distribution by plugging the variance estimate $\hat{\sigma}^2$ from lmer, rather than going through the typical Bayesian route..
Comment: You might be better to split this up and ask question 1 first and then when you have a satisfactory answer ask 2 with a link back to 1, and so on.
Comment: Having been edited to constrain the question, I think this is no longer *too broad*. I'm voting to leave open.
Comment: should it be $j$ for measuring identity, $i$ for measuring condition (with $\lambda_i$ for the condition instead of identity)?

Currently it is $i$-th identity and  $k$-th  condition. With $\lambda$ for identity. Another point... the 'estimates' means maximum posteriori probability?
Comment: @MartijnWeterings Thanks. $k$ should be $j$, and think of $i$ and $j$ as indices for school districts and schools. What "estimates" are you referring to: mean and variance of $\theta_i$?
Answer: The general concept here is updating a conjugate prior.

I'm trying to see if the above closed form is a feasible solution

I'm not sure what you mean by feasible.  For the Gaussian distribution $N\left(\mu, \sigma^2\right)$, any specification of mean $\mu$ and variance $\sigma^2$ - with mean parameter unrestricted and variance parameter strictly positive $\sigma^2 > 0$ - defines a unique and valid Gaussian distribution.
I will assume you are asking if the proposed closed form solution is correct.
The closed form solution you provide is the correct posterior distribution given a Gaussian prior distribution $b_0+\lambda_i = \theta_i \sim N\left(b_0, \tau_0^2\right)$ and observed data with known observation variance $\sigma^2$.
Precision $K$ is the inverse of variance $\sigma^2$: $K = \sigma^{-2}$.
Considering the mean of the posterior distribution for $\theta_i$, the numerator in the formula $mean(\theta_i)$ you provide is the precision weighted combination of the prior distribution's mean $b_0$ and the observed data's mean $\bar{y_i}$. The denominator of the posterior mean formula simply scales the sum of these weights to one.
Considering the variance of the posterior distribution, the operation reflected in the formula $Var(\theta_0)$ you provide is the summing of the precision of the prior distribution and the precision of the observations; then inverting to obtain variance.
In summary, the formulas you provide for posterior mean and posterior variance are correct given the prior $\theta_i \sim N(b_0,\tau^2)$, and given $n$ observations with known observation variance $\sigma^2$.

If your concern has to do with specifying the prior distribution $N(b_0,\tau^2)$ parameters, note the influence of the prior mean $b_0$ in your formula for the posterior mean $mean(\theta_i)$ diminishes as the number of observations increases, and diminishes as the prior variance increases. Therefore, don't worry too much about the prior mean, just increase the prior variance as seems appropriate; and, increase the number of observations!
Comment: Thanks for the answer! Sorry that my original question was unclear. I was not questioning whether the close form of the posterior distribution is correct given the known variance $\sigma^2$ and number of observations, $n$. Instead, I want to know whether using estimated $\hat{\sigma}^2$ from solving LME through function `lmer` in the R package `lme4` is a reasonable approach to directly obtaining the posterior distribution, rather than going through the typical Bayesian route. Could you comment about this?
Comment: Ok, life just got more complicated :)You should look at Peter Hoff's [notes](http://www.stat.wisc.edu/courses/st692-newton/notes.pdf), particularly Ch. 7 & 8.
Comment: Apparently I'm not allowed to edit after 5 minutes.

The concept is now updating a *semi-conjugate* prior (unknown mean and unknown variance). The correct chapters in Hoff's notes are __*Ch. 6 & 7*__; or better yet, look at his book,

Hoff, Peter D. *A first course in Bayesian statistical methods.* Springer Science & Business Media, 2009.

Hoff provides simple R code in both his notes and book.  So, if you want to perform a Bayesian analysis, I would study Hoff's __*Ch. 6 & 7*__ in his notes, or similar chapters in his book (don't have the book handy today); and not use `lmer`
Comment: Why is using `lmer` to directly estiamte $\hat{\sigma}^2$
 problematic for the posterior distribution? Andrew Gelmen seems to hint that this would be fine in this paper: http://www.stat.columbia.edu/~gelman/research/published/multiple2f.pdf


usar redirect para página especifica após a validação em caso de erro - Laravel

Question: Bom tenho o código abaixo
Validação:
class FormNew extends FormRequest{

    public function authorize()
    {
        return true;
    }

    public function rules()
    {

        return [
            'email' => 'required|email',
        ];
    }

    public function messages()
    {

       return [
            'email.required'        => 'campo Email Obrigatório',
            'email.email'           => 'campo Email com formato inválido',
        ];
    }

}

Controller:
class FormularioController extends Controller{

    public function newsletter(FormNew $request)
    {

        $usuarioEmail = Usuario::Where('email', $request->email)->first();

        if($usuarioEmail == null){
            $usuario = new Usuario;
            $usuario->perfil_id         = '2';
            $usuario->email             = $request->email ;
            $usuario->password          = Hash::make(rand(1,100).'senha');
            $usuario->save();
            return redirect('?#newsletters')->with('SucessoCadastro', 'Email Cadastrado com sucesso!');
        }else{
            return redirect('?#newsletters')->with('Falha', 'Email já cadastrado no sistema');
        }

    }
}

Observem quem no meu redirect estou usando uma ancora, pra quando o usuário submeter o form ele ir direto onde se encontra o formulário, queria saber como usar esse redirect dentro  da minha validação, pra quando der o erro ele ir p formulário para mostrar os erros (FormNew).
Answer: A classe FormRequest possui propriedades especiais para redirecionamento, se ela não encontrar nenhum valor ela utiliza o redirecionamento default Illuminate\Routing\UrlGenerator::previous().
Você pode fazer o redirect utilizando qualquer uma das propriedades abaixo:
protected $redirect; // URL. ex: google.com
protected $redirectRoute; // Nome da rota.
protected $redirectAction; // Action do controller

class FormNew extends FormRequest{

    /**
     * Redirect route when errors occur.
     *
     * @var string
     */
    protected $redirect = '/teste#newsletters';

    public function authorize()
    {
        return true;
    }

    public function rules()
    {

        return [
            'email' => 'required|email',
        ];
    }

    public function messages()
    {

       return [
            'email.required'        => 'campo Email Obrigatório',
            'email.email'           => 'campo Email com formato inválido',
        ];
    }

}

Comment: deu certo, muito obrigado!


Postgres dump specific table with a capital letter

Question: I am trying to perform a postgres dump of a specific table using -t. However, the table has a capital letter in it and I get a "No matching tables were found." I tried using quotations and double quotations around the table name but they did not work. How can I get pg to recognize the capitals? Thanks!
pg_dump -h hostname dbname -t tableName > pgdump.sql

Answer: Here is the complete command to dump your table in plain mode:
pg_dump --host localhost --port 5432 --username "postgres" --role "postgres"  --format plain  --file "complete_path_file" --table "schema_name.\"table_name\"" "database_name"

OR  you can just do:
pg_dump -t '"tablename"' database_name > data_base.sql

Look to the last page here: Documentation
Comment: In windows it still doesn't work.

`> pg_dump --table="\"Accounts\"" -U postgres -d helium2 > dump.s
ql`

Gives the following error:

`pg_dump: [archiver (db)] query failed: ERROR:  invalid regular expression: parentheses () not balanced
pg_dump: [archiver (db)] query was: SELECT c.oid
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind in ('r', 'S', 'v', 'm', 'f')
  AND c.relname ~ E'^(\\accounts\\)$'
  AND pg_catalog.pg_table_is_visible(c.oid)`
Comment: Adding the Schema is VERY VITAL
Answer: The above solutions do not work for me under Windows 7 x64. PostgreSQL 9.4.5. But this does, at last (sigh):
-t "cms.\"FooContents\""
either...
pg_dump.exe -p 8888 --username=user -t "cms.\"FooContents\"" basdb

...or...
pg_dump.exe -p 8888 --username=user -table="cms.\"FooContents\"" basdb

Comment: This also worked in Windows 10 x64 and PostgreSQL 12. Thank you.
Comment: This is also the only solution that worked for me on a Raspbian, thanks.
Comment: I'm pretty sure you don't need to quote the full table spec unless it contains whitespace.  That's a command processor thing, not pg_dump.

  `pg_dump.exe -p 8888 --username=user --table=cms.\"FooContents\" basdb`
Answer: Inside a cmd window, I had to put three (!) double quotes around the table name if it countains upper case letters.
Example
pg_dump -t """Colors""" database > database.colors.psql
Comment: I forgot to mention this is Microsoft Windows 7. Single quotes don't work here.
Comment: The three double quotes thing also works perfectly well on Windows 10. None of the other solutions worked for me except for this one.
Answer: Powershell
the good (shortest)
& 'C:\Program Files\PostgreSQL\12\bin\pg_dump.exe' -d db_name -t '\"CasedTableName\"'

the bad (requires --%)
& 'C:\Program Files\PostgreSQL\12\bin\pg_dump.exe' --% -d db_name -t "\"CasedTableName\""

the ugly (requires `")
& 'C:\Program Files\PostgreSQL\12\bin\pg_dump.exe' -d db_name -t "\`"CasedTableName\`""

The main point of confusion for me was the absolute necessity of having \" in there.  I assumed that maybe there was a weird bug in the way powershell or psql was parsing the arguments, but it turns out it's explained in the docs:

Some native commands expect arguments that contain quote characters. Normally, PowerShell's command line parsing removes the quote character you provided. The parsed arguments are then joined into a single string with each parameter separated by a space. This string is then assigned to the Arguments property of a ProcessStartInfo object. Quotes within the string must be escaped using extra quotes or backslash (\) characters.

And of course ProcessStartInfo.Arguments Remarks tells us:

To include quotation marks in the final parsed argument, triple-escape each mark.
Answer: This worked for me:
pg_dump -f file.sql -U user -t 'schema.\"Table\"' database

Answer: As part of a node script I had to surround with single and double quotes, e.g.
` ... --table 'public."IndexedData"'`

The accepted solution worked in a bash console, but not as part of a node script, only the single quote approach.
Answer: Thanks to @Dirk Zabel suggestion, the following worked for me:
Windows 10 CMD
pg_dump -d "MyDatabase" -h localhost -p 5432 -U postgres --schema=public -t """TableName""" > TableName.sql

Bash
pg_dump -d "MyDatabase" -h localhost -p 5432 -U postgres --schema=public -t "\"TableName\"" > TableName.sql


Rolled chords: how much flexibility in the rhythm?

Question: Best way to illustrate this question is by example, looking at Liszt's transcription of Beethoven's 6th symphony, movement 2. Compare 9:30 in:

to the very start of:

In the second video, Glenn Gould really takes his time on the rolled chords. Is this okay? Or should I somehow try to get it up to the speed that Dalberto has it (first video)? Glenn Gould's kind of breaks the metronome, I'm wondering if that's common/acceptable practice.
There's a real challenge, especially in measure 7 (see screenshot below). Some of those jumps have to be rolled, as I don't have Liszt's hands, but rolled kind of sounds silly especially in Gould's version. How to make it sound natural?
Comment: It's all in the moment. Feel what is good, then do it. Every one will be different.
Comment: Related note: to authoritatively break the metronome one must have mastery of the metronome. When that is achieved, breaking it is an artistic, expressive act.
Answer: There are two questions here: general one first, then the specific one.
Rolled chords: how much flexibility in the rhythm?
A lot. The typical assumption is to play them very quickly, but musical context can dictate otherwise. For example, consider the rolled chord in the penultimate measure (62) of Chopin's Etude in thirds, Op. 25, No. 6. This chord is generally rolled quite slowly to help dissipate the energy of the rest of the etude. The Lento marking is also a major indicator.

Pollini recording from m. 58
On the other hand, the rolled chords in the Chopin's Op. 25, No. 5, are clearly intended to be rolled quickly.

Pollini recording
How should the Beethoven/Liszt transcription be played?
Glenn Gould version
This is certainly acceptable, but extreme, and does take some getting used to if one hasn't heard this approach before. Gould in particular was oft criticized for interpretations that pushed the boundaries of what was/is considered acceptable.
Note that Gould never breaks time. The melody always in right on the beat. In m. 7 his rolls are quite fast.
Michel Dalberto version
I don't think he's rolling the chords at all. Instead, he's playing the lowest octave, picking up the remaining notes in the next chords. This is also a perfectly fine way to do it. Note that in m. 7 it sounds like he's not rolling the chords. That's amazing and makes me question my ears.
How I would do it
I wouldn't roll the chords. I would break them into two parts, treating the lowest note as a short of grace note and then playing the remainder of the chord. To me, this is closest to Liszt's intent: the bass, and everything else.
For the chords that are not marked as rolled, but which are too big for my hand (e.g., the final LH chord in m. 3), I would do a true, fast roll. For measure 7, I would play the lower two notes with fingers 5 and 1 (rolled if necessary), and then cross over finger 2 for the topmost note.
Answer:
In the second video, Glenn Gould really takes his time on the rolled chords. Is this okay? Or should I somehow try to get it up to the speed that Dalberto has it (first video)?

It is really a matter of musical taste. Particularly when musicians with sky high reputations are involved :-)
These kind of questions and debates are not new. They have been around almost forever.
Back in the late 80's when Roger Norrington conducted Beethoven's 9th symphony in a sprightly 62 minutes some critics were outraged. For them Karl Böhm's 79 minute version was the pinnacle of performance for the master's work.


Fitting 3 different slopes to data subgroups or fitting a single mixed-effects model, and which values to report

Question: I'm new to the world of mixed-effects models and trying to understand what method/results to report for my given data/question. I am trying to determine the coefficients of a regression model but have three sub-populations within the data. My question is 1) whether y is predicted from x at a likelihood much greater than chance and 2) whether the slope is essentially the same for each sub-population. I've simulated some data that shows this:
set.seed(123)
dat <- data.frame(grp=gl(3, 10, labels = LETTERS[1:3]))
dat$y <- 1:10+as.numeric(dat$grp)*3
dat$x <- 1:10+as.numeric(dat$grp)[rep(c(1,3,2), each=10)]
dat$x <- ifelse(dat$grp=="A", dat$x+10, dat$x)
dat$x <- ifelse(dat$grp=="C", dat$x+6, dat$x)
dat$y <- ifelse(dat$grp=="A", dat$y*0.5, dat$y)
dat$y <- dat$y+rnorm(30, sd = 0.5)

library(ggplot2)
ggplot(dat, aes(x=x, y=y)) +
  geom_smooth(color="black", method="lm") +
  geom_smooth(aes(color=grp), method="lm") +
  geom_point(aes(fill=grp), pch=21)

Visually (and correctly, given that this data is simulated) the answers to my questions are relatively simple: yes, y is predicted from x, and kinda, with B ~ C > A. The question is how I should best show this statistically and what I should report for my actual data.
Obviously the overall (black) trend is incorrect and is an illustration of Simpson's paradox, so I'll ignore it entirely. The ggplot lines are fit independently to each sub-population with a basic call to lm that I can reproduce with a simple loop. These values then need to be corrected for multiple comparisons and I do so separately for the slopes and intercepts since I want to keep the FWER distinct for each.
loop_vals <- do.call(rbind, lapply(unique(dat$grp), function(grp_i){
  broom::tidy(lm(y~x, data=dat[dat$grp==grp_i,]))
}))
p.adj <- c(p.adjust(loop_vals$p.value[c(1,3,5)]), p.adjust(loop_vals$p.value[c(2,4,6)]))
loop_vals$p.adj <- p.adj[c(1,4,2,5,3,6)]

  term        estimate std.error statistic      p.value        p.adj
  <chr>          <dbl>     <dbl>     <dbl>        <dbl>        <dbl>
1 (Intercept)   -3.29     0.901      -3.65 0.00650      0.0130
2 x              0.459    0.0538      8.54 0.0000273    0.0000273
3 (Intercept)    5.52     0.399      13.8  0.000000722  0.00000217
4 x              0.936    0.0562     16.7  0.000000171  0.000000342
5 (Intercept)    0.879    0.614       1.43 0.190        0.190
6 x              1.07     0.0479     22.4  0.0000000167 0.0000000501

I can obtain these same values with an interaction-based model in a single call to lm(), but the significance values are wildly different (or I don't understand how the error propagates differently here). This likely just has to do with multiple comparisons?
broom::tidy(lm(y~x*grp, data=dat))

  term        estimate std.error statistic       p.value
  <chr>          <dbl>     <dbl>     <dbl>         <dbl>
1 (Intercept)   -3.29     0.883      -3.72 0.00106
2 x              0.459    0.0527      8.70 0.00000000686
3 grpB           8.81     0.959       9.18 0.00000000253
4 grpC           4.17     1.11        3.74 0.00100
5 x:grpB         0.477    0.0746      6.39 0.00000131
6 x:grpC         0.614    0.0746      8.23 0.0000000191

Alternatively, I can fit the data using a mixed effects model. Fitting this with random intercepts and fixed slopes gives reasonable results and an easily interpretable output, but I've heard mixed advice on trying to model a random effect with so few groups (less than 5):
lme_fixed_slope_mod <- lme4::lmer(y~x+(1|grp), data=dat, REML = FALSE)

Random effects:
 Groups   Name        Std.Dev.
 grp      (Intercept) 6.8323
 Residual             0.9156
Number of obs: 30, groups:  grp, 3
Fixed Effects:
(Intercept)            x
     0.3904       0.8171

Finally, I can fit the data with random slopes and random intercepts. I've been told this is generally a good idea but worry that it won't answer my overall question about the slope estimate as clearly. Doing this returns predictions that are very similar to (but distinct from!) the values obtained from the lm outputs above, and I can't figure out how to extract the slopes and intercepts themselves from the lmerMod object. I expected these predictions to be the same as the lm models and don't understand why they're different.
lme_rand_slope_mod <-lme4::lmer(y~x+(1+x|grp), data=dat, REML = FALSE)

Random effects:
 Groups   Name        Std.Dev. Corr
 grp      (Intercept) 3.6281
          x           0.2571   0.77
 Residual             0.4778
Number of obs: 30, groups:  grp, 3
Fixed Effects:
(Intercept)            x
     1.1102       0.8192

Comparison of the three slope/intercept values (boxes = fixed slope, circles = random). Note that while the circles appear to fall right on the line of best fit, they're actually deviating systematically.
dat$y_lmpred <- predict(lm_model, newdata=dat[,c("x", "grp")])
dat$y_lme_fixedpred <- predict(lme_fixed_slope_mod, newdata=dat[,c("x", "grp")])
dat$y_lme_randpred <- predict(lme_rand_slope_mod, newdata=dat[,c("x", "grp")])

ggplot(dat, aes(x=x, y=y)) +
  geom_smooth(aes(color=grp), method="lm") +
  geom_point(aes(x=x, y=y_lme_fixedpred, fill=grp), shape=22) +
  geom_point(aes(x=x, y=y_lme_randpred, fill=grp), shape=21)

Which of these four methods is most apt for my data and will help me answer 1) whether x is a good predictor of y and 2) whether the slopes are indistinguishable for the subgroups in my real data set? What values should I report to show this?
Comment: I agree with CaroZ , I would not use random effects for a clustering variable that only has 3 levels. Single-level regression with group * x interaction would seem to work well. You can test the significance of x's slopes for each subgroup for instance via emmeans emtrends.
Answer: The significant interaction between x and the subpopulation and is what tells you that the relationship between x and y differs according to the subpopulation, i.e. whether the slopes are different or not between subgroups.
I would first report the stat of the interaction. If this interaction is significant, i.e. if indeed the relationship between x and y differs according to the subgroup, it might mean that in some groups x is a good predictor of y but not in others.
To answer this I would produce an effect plot with the 'effects' package, look at the confidence intervals in each subgroup. If in some I could be fitting a flat line within the 95CIs, I would say x does not predict y in the given subgroup.
If not, x predicts y in the given subgroup.
I do not see the point of the random slopes and intercepts, but it might just be my opinion.
Comment: I agree in this situation, with only 3 groups, that the best approach would be to use a fixed slopes approach. But what if you had 300 groups? Would you still advocate for a regression with 299 fixed intercepts and another 299 interaction effects? The random approach reduces this to 5 parameters - 1) fixed slope mean, 2) fixed interaction, 3) random intercept variance, 4) random slope variance, 4) random slope-random intercept covariance.


LU Decomp without pivoting in Python

Question: I need to create the function [L,U] = lr(A) which will compute the LU decomp of matrix A without pivoting or the use of inv,lu,etc to solve the linear equation.
Just trying to work out the pseudo-code for now and understand it. Any ideas on how to get started?
Comment: First google result for "LU decomposition python" https://www.quantstart.com/articles/LU-Decomposition-in-Python-and-NumPy It uses numpy so if you need inspiration you can read the source for `numpy.linalg.lu`
Comment: I understand the process mathematically and can do it by hand, but I'm not sure how to avoid pivoting and get the same result
Comment: Does the numpy implementation use pivoting?
Comment: it doesn't, but I am not allowed to use the built in function lu
Answer: "Numerical Methods in Engineering with Python 3" by Kiusalaas is a great resource. Below is the code for Doolittle's decomposition method from the book.
# [L][U] = LUdecomp([A])
def LUdecomp(a):
    n = len(a)
    for k in range(0,n-1):
        for i in range(k+1,n):
            if a[i,k] != 0.0:
                lam = a [i,k]/a[k,k]
                a[i,k+1:n] = a[i,k+1:n] - lam*a[k,k+1:n]
                a[i,k] = lam
    return a

LU Decomp without pivoting in Python


Multithreading in Windows Phone 8.1

Question: public MainPage()
{
    Method_1();
    Method_2();
    Method_3();
    Method_4();
    Method_5();
    Method_6();

}

I am writing a Windows Phone 8.1 App (WINRT XAML). How to make these methods execute at a time from constructor? What I mean to ask is about multithreading, i want these methods to get executed sidebyside and not one after another.
Does this effect loading of application? will app load fast/slow?
Comment: Do you need to do it all in the constructor? If the operations are time consuming, how about postponing them until the page is loaded? Also, some of the things you're doing may be UI related, meaning that even if you dispatch the operation to another thread, you would still have to dispatch it back to UI thread for rendering. In other words, your question needs more details and context to get any kind of meaningful answer.
Comment: The above 6 methods are non UI related. Sir,  ok i will  execute it in this.Loaded += LoadMethods();  But in LoadMethods() function they will still load 1 by 1 . Can you show me how to execute these paralely, i mean using multithreading
Answer: First off, you don't want to execute those methods in the constructor if they are long running methods. Wait until the page is loaded:
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
    Task m1task = Method_1();
    Task m2task = Method_2();
    Task m3task = Method_3();
    Task all = Task.WhenAll(m1Task, m2Task, m3Task);
    await all;
}

The code will load off these operations to another thread and as long as your methods are properly implemented your UI will stay responsive (so don't use wait() for instance).
This is what a sample method could look like:
private async Task Method_1() {
    // Long running operation goes here
}

If you have some heavy computations to do, wrap them into Task.Run(() => { // Code });It's really essential that you're aware of the concepts of asynchronous programming. You might want to read on here:
Do you have to put Task.Run in a method to make it async?
await vs Task.Wait - Deadlock?
When correctly use Task.Run and when just async-await
But seriously, you're writing that your methods are not UI related. You might be better off running those somewhere else (e.g. in your ViewModels or even in a background task / service).
Answer: Mark the methods as Async with return type Task.
eg. public async Task method1(){}
You won't be able to fire any UI activities from them, but they'll run outside of the main thread.
Comment: Just marking a method async doesn't mean it goes on a different thread.
Comment: It's certainly enough to keep the UI responsive.
Comment: @Fred What do you mean? As igrali said aync Task also can run on UI thread and it can easily block UI with some heavy computation if it is not properly implemented.
Comment: @Romasz I think it doesn't if the method is awaited. The compiler will generate a state-machine and run the code on another thread. The UI should stay responsive. I wasn't talking about performance - just responsiveness.
Comment: @Fred No. If you don't redirect your Task to ThreadPool it will run on UI thread. Consider such a Task: `private async Task DoSomething() { Task.Delay(5000).Wait(); await Task.Delay(1000); }` and run it for example upon button click: `await DoSomething()` - it will block the UI thread for 5 seconds.

Multithreading in Windows Phone 8.1


Chi-boundedness and Upperbounds on Chromatic Number

Question: I am new to graph theory and am a bit confused about the difference b/w the chi-boundedness and upper-bounds on chromatic number. Can we say that chi-boundedness is a form of an upperbound of the chromatic number? Could someone help me to clarify?
Definitions

Chromatic Number: Minimal number of colours needed to colour the vertices in such a way that no two adjacent vertices have the same colour
Chi-boundedness: Ideal(I) is chi-bounded if there is a func f, called chi-bounding func, s.t. $\chi$(G) < f($\omega$(G)) for all G in I

I understand that chi-boundedness basically considers the following question: If a graph has bounded clique number and sufficiently large chromatic number, what can we say about its induced subgraphs?
Answer: The notion of $\chi$-boundedness is a generalization of perfect graphs. A graph $G$ is perfect if $\chi(H)=\omega(H)$ for each induced subgraph $H$ of $G$.
A graph $G$ is $\chi$-bounded if there exists a function $f:\mathbb{N}\to \mathbb{N}$ such that $\chi(H) \leq f(\omega(H))$ for each induced subgraph $H$ of $G$.
If we let $f:\omega \mapsto \omega$ (the identity function), then $G$ is a perfect graph iff $G$ is $\chi$-bounded by $f$. Hence, the above is a direct generalization.
In that sense, a $\chi$-binding function is an upper bound for the chromatic number, but in form of a function from its clique number $\omega$ to a natural number.
For example, the class of even-hole-free graphs, that is the class of graphs that forbidds induced cycles of even length, is $\chi$-bounded with $\chi$-binding function $\chi(G) \leq 2\omega(G)-1$ as shown by Addario-Berry et.al.
Question: Are all graphs $\chi$-bounded by some function $f$? No. There are graphs that are triangle free, thus clique number strictly smaller than $3$, but with an arbitrary large chromatic number (consider the Mycielskian construction for an example).
Answering the question whether or not a given family of graphs is $\chi$-bounded or not is far from trivial and an open research question for many classes of graphs.
There is another interesting problem.
Problem: Given a class of graphs that is $\chi$-bounded by some function $f$. Is $f$ the smallest $\chi$-binding function for this class?
Answering the above is an open research question for many graph classes.
Your second questions asks what we can say about the induced subgraphs of a graph $G$ if we are provided with a $\chi$-binding function. For a concrete graph, these are the usual answers one could provide if we know the chromatic number of a graph  (which is the same as saying we know the $\chi$-binding function of a specific graph that provides a tight bound). This is not interesting. It is more interesting to consider a whole family of graphs and its $\chi$-binding function, because it allows us to make certain statements about the structure of the whole family.
Is there a general answer about the structure of a class of graphs if we know its $\chi$-binding function? I am afraid that there is none, but I can't say this for sure.
If you want to dive deeper into this topic, I suggest the following two survey papers:
[1] "Schiermeyer, Ingo, and Bert Randerath. "Polynomial $\chi$-Binding Functions and Forbidden Induced Subgraphs: A Survey." Graphs and Combinatorics 35.1 (2019): 1-31."
[2] "Scott, A., & Seymour, P. (2020). A survey of χ-boundedness." (I have only found it on arXiv)
Comment: We call a class of graphs that is closed under the operation of taking induced subgraphs an "ideal". Thus this is the same thing. First of all, we require the $\chi$-boundedness for all induced subgraphs because we want to generalize perfect graphs. But, of course this has several advantages. We get some kind of recursive property! This has advantages when it comes to the design of algorithms for example. Keep this in mind: say we've proven a quadratic chi bound. This doesn't mean we can colour the graph using $\omega^2$ colours in quadratic time.
Comment: Thank you for your detailed answer! I was reading [2] but was not aware of [1]! I will definitely check it out soon! Btw, in the definition of $\chi$-boundedness for a graph, why do we need the condition that the binding fn applies to all the induced subgraph? I think in [2](as far as I've read), they didn't mention that... Or I think I might've just misread the context of the definition?
Comment: oh, do you think "Ideal" part of my definition above actually meant "Class of graphs that essentially are just subgraphs belonging to some given graph"?
Comment: Sorry, pls ignore my above question coming from the confusion of the part of the definition! I went over few more definitions on the internet and now it makes sense!!
Thank you for your help!
Comment: Got it! Thank you for your follow-up! I've also understood about that chi-boundedness is not about the computational complexity!
Comment: You are very welcome!


Cookie Compliance and Google Analytics

Question: I'm using Drupal 8.6.14 (going to upgrade soon) and I am trying to create a cookie consent page that actually works!
I've used many different modules like EU Cookie Compliance, GDPR Compliance and several others, but all of them fail to block Google Analytics cookies.
I even used a couple of patches on Google Analytics that I found in the community in order to support EU Cookie Compliance module, but all of them fail.
To be exact, EU Cookie Compliance seems to load the cookies as normal, then deleting them as the dialog of consent appears and then loading them all again once I pick any option.
All the other solutions seem not to work at all (I get the cookies any time the page loads). BTW, all the solutions I've found are for <8 versions.
Any idea how I can get this done? Am I missing something?
Comment: Welcome to Drupal Answers!  This request sounds a little bit broad and I'm also unsure if this now is a support request for the EU Cookie Compliance module or if you are asking how this can be done generally? Maybe also tell us how exactly you are placing GTM on your page and what exact string you've put in the library exclude section of EU Cookie Compliance settings. Are you aware that this module also offers a function that lets you determine if a user accepted or rejected cookies? Please edit your question for clarification. Many thanks
Answer: From the EU Cookie Compliance module's project page on drupal.org:

If you want to conditionally set cookies in your module, there is a
JavaScript function provided that returns TRUE if the user has given
their consent:
Drupal.eu_cookie_compliance.hasAgreed()
For example, if you want to disable Google Analytics, you could do the
following: http://drupal.org/node/1648286#comment-6145800
Another example of how it can be used with Google Analytics is
provided by dagomar in this comment.
Here is another example showing how to set cookies after user clicks
the accept button: http://drupal.org/node/1624876#comment-6111688

I've personally used this to prevent third-party code snippets from loading until after compliance is granted, by making a custom module and including the call to hasAgreed() in my module's JavaScript behaviors.
But as the first example above shows, a custom module is not necessary for Google Analytics in particular. You can simply add the following code to the Advanced Settings->Custom JS in Google Analytics:
if (!Drupal.eu_cookie_compliance.hasAgreed()){
window['ga-disable-UA-xxxx-1'] = true;
}

EDIT (2020-02-23):
It seems that you want to enforce extremely strict GDPR compliance, and prevent the cookies from being loaded at all unless consent has been granted.
You can do this with a custom module, leveraging the same hasAgreed() JavaScript behavior provided by the EU Cookie Compilance module.
First you need to remove the code snippet from your site completely and replace it with a placeholder.
If it's just one script in your HTML head, you might just edit html.html.twig.
If there are multiple scripts you need to prevent from running, it will be more work.
In my case, I replaced dozens of different third-party forms with an empty
 element:

My custom JavaScript behavior looks for all the DOM elements with that class:
document.getElementsByClassName('thirdPartyFormWrapper');

The 4-digit number in the div ID corresponds to the unique third-party form ID for each form.
If consent has not been granted (or if the EU Cookie Compliance module is not installed or enabled, and hasAgreed() is not available) then my custom module uses the 4-digit ID to look up substitute content (saying, e.g., "Please accept cookies to view the Yoyodyne, Inc. Gnomovision Mailing List Subscription Form," or whatever that particular form is.)
If consent has been granted, then my custom module uses the 4-digit ID to inject the JavaScript snippet for each of the third-party forms into the placeholder
.
Once the form snippet is injected, you can figure out how to handle the cookies. If consent is withheld, neither snippets nor cookies will load.
You can also use this technique to handle cookies for extremely strict GDPR compliance, following this pattern:
/**
 * Cookie handling for strict GDPR compliance
 */
  Drupal.behaviors.customCookieConsent = {
    attach: function (context, settings) {
      if (!Drupal.eu_cookie_compliance || !Drupal.eu_cookie_compliance.hasAgreed()) {
        // Bail out if cookie consent not given
        return;
      }
      // If cookie consent is given, set cookie (unless already set)
      if (!$.cookie('customConsentTime')){
        var consentTime = new Date();
        $.cookie('customConsentTime', consentTime.toISOString(), {expires: 3650});
      }
    },
  };

This approach is a bit more work, but it will prevent scripts and cookies from being loaded at all before consent is granted.
Finally, I should note that based on your comments here and on Drupal.org, I think you should do some research on "Drupal Behaviors" for your JavaScript. This will help you to avoid the "Uncaught ReferenceError: Drupal is not defined" message you have been seeing.
Comment: i think that i have found what cause the problem and i found a workaround, described here https://www.drupal.org/project/eu_cookie_compliance/issues/1648286#comment-13476318
But i still see the cookies __id and __ga on my cookies list without consent (even if there is no traffic in my analytics page. Any way to fix this?
Comment: when i insert the custom JS i get this error "Uncaught ReferenceError: Drupal is not defined" even my dependencies on both google analytics and eu cookie compliance module are using core/drupal and core/drupalSettings. any idea of whats going on?
Comment: @b10z I have edited my answer to give more details. If you find my approach effective, please let me know by accepting my answer.
Comment: For me, the EU Cookie Consent .hasAgreed() gives always false and then always true, even when the user has not opted for anything
Answer: There is a new module for Drupal 8 and 9: COOKiES. It supports the modules Google Analytics and Google Tag Manager (and more). Maybe that's what you are looking for.

COOKiES
User consent management module for Drupal 8 and 9.
The module enables existing third-party-integration modules continued
to be used in compliance with the GDPR (of course without manipulating
their code). It solves the basic problem that when using
third-party-integration modules, the user must agree to the use of
cookies (according to GDPR) before they are installed.
IMPORTANT: No liability is assumed regarding compliance with the GDPR.
The COOKiES module provides (with the Library Cookies JSR) a
fully configurable user interface for user decisions. It also supports
out-of-the-box some key third-party integration modules (especially
those included in the Thunder distribution):

Google Analytics module
Google Tag Manager
Video embed, core:media module
Twitter media module
Instagram media module
Answer: This is how I solved the issue.
Setup:

Drupal 8.9.6
Eu Cookie Compliance 8.x-1.x-dev
Google Analytics 3.1

Settings under /admin/config/system/eu-cookie-compliance/settings:

Consent method: Opt-in. Don't track visitors unless they specifically give consent. (GDPR compliant)
Disable Javascripts: https://www.googletagmanager.com/gtag/js

It behaves as expected:

Google Analytics cookies are not loaded unless the user accepts cookies
If the user rejects cookies, cookie banner is removed and no Google Analytics are placed in any subsequent page loads

Cookie Compliance and Google Analytics