Android external data storage problems

Question: I'm trying to store data on the SD card, according to the guide here I should be using getExternalFilesDir() (API level 8), I would really like to do so but calling
File directory = context.getExternalFilesDir(null); keeps returning null, I'm using my main activity as context.
Then I tried reverting to use File dir = Environment.getExternalStorageDirectory();, it correctly returns the SD root, but apparently I am unable to create the suggested "/Android/data/package_name/files/" directory:
    File root = Environment.getExternalStorageDirectory();
    File dir = new File(root, GlobalConstants.EXTERNAL_SAVE_DIR);
    boolean canCreate = dir.mkdirs();

In this code canCreate is false (EXTERNAL_SAVE_DIR is obviously /Android/data/package_name/files/).
Is there anything I'm missing, maybe should I ask permissions on the Manifest (I found nothing about)? I've read getExternalFilesDir() has a bug where contents get deleted on application update, I don't really care about that and I would rather use it instead of the other one as I'm trying to conform to API 8 and I like the idea of it handling the directory name for me.
Any idea?
Answer: You need to add the following to your manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Otherwise you are not allowed to write to the SD card. If you are using Eclipse, you can use the permissions tab of the manifest editor do add it.
Comment: I don't understand why the documentation doesn't mention this permission in Data Storage section. +1 it solved my problem.
Comment: Dear reader - if you have the same problem with premissions in Manifest, try to reboot phone or enable then disable USB storage. I had situation on 2.2, where external storage was mounted and permission was in place, but app was unable to write anything. Connecting to computer and switching on->off USB storage mode fixed this issue for me.
Comment: Damn, I don't know how I missed it, and I checked!
Comment: It is also worth mentioning that the files aren't created automatically when writing but one has to use `file.createNewFile()`

Android external data storage problems


Why is my Javascript and JQuery scripting not working?

Question: I was looking at another question and saw that they had been told, when their Javascript was not working, to put a function that started on loading of the page.
I was trying to use this code:(well, not exactly this code, but something like it)

<!DOCTYPE html>

Did it work?

And nothing happened. All that it did was show the h1 code that I put in there.
Comment: Also, you're using jQuery's `append()` incorrectly. It must be applied to a jQuery object (`$('body').append()`).
Comment: Possible duplicate of [JavaScript that executes after page load](https://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load)
Comment: Ok, I will try that, because I just noticed that I was using JQuery on an element selected by Javascript.
Answer: You forgot to include the parenthesis to call the function in the body element's onload attribute:

<!DOCTYPE html>

Did it work?

A better/cleaner way to achieve the same thing would be to use addEventListener and DOMContentLoaded event. Something like below:

document.addEventListener("DOMContentLoaded", appendHtml.call(null, {
  selector: 'body',
  html: '
Yes it did.
'
}));

function appendHtml(config) {
  document.querySelector(config.selector).innerHTML += config.html;
}

Did it work?

Comment: I actually was trying to use JQuery, not just Javascript. The only reason this question's title contains the word Javascript is because I was confused and trying to use JQuery on a element selected by Javascript and it didn't work.
Answer: Since you are already including jQuery try this

<!DOCTYPE html>

Did it work?

Answer: Two reason why it doesn't work:

To select the body you have to use document.body or $("body") (in jQuery)
And you forgot to add parentheses to your function call doThisWhenLoaded()

Here is the code corrected:

<!DOCTYPE html>

Did it work?


How to set up SPDY Protocol over Nginx?

Question: I have a rails app and want to set up the google SPDY protocol support. But after installing Nginx with SPDY patch and then enabling the spdy in virtual host it does not allow me to restart the nginx instead throws following error.
Restarting nginx: nginx: [emerg] invalid parameter "spdy" in /etc/nginx/sites-enabled/default:112
nginx: configuration file /etc/nginx/nginx.conf test failed

I have compiled latest nginx 1.3.13 with spdy patch, here I am mentioning my steps of install
wget http://nginx.org/download/nginx-1.3.13.tar.gz
tar xvfz nginx-1.3.13.tar.gz
cd nginx-1.3.13

# Fetch the SPDY patch and apply it
wget http://nginx.org/patches/spdy/patch.spdy.txt
patch -p1 < patch.spdy.txt

 ./configure \
 --sbin-path=/usr/local/sbin/nginx \
 --prefix=/etc/nginx \
 --conf-path=/etc/nginx/nginx.conf \
 --error-log-path=/var/log/nginx/error.log \
 --http-client-body-temp-path=/var/lib/nginx/body \
 --http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
 --http-log-path=/var/log/nginx/access.log \
 --http-proxy-temp-path=/var/lib/nginx/proxy \
 --http-scgi-temp-path=/var/lib/nginx/scgi \
 --http-uwsgi-temp-path=/var/lib/nginx/uwsgi \
 --lock-path=/var/lock/nginx.lock \
 --pid-path=/var/run/nginx.pid \
 --with-debug \
 --with-http_addition_module \
 --with-http_dav_module \
 --with-http_gzip_static_module \
 --with-http_realip_module \
 --with-http_stub_status_module \
 --with-http_ssl_module \
 --with-http_sub_module \
 --with-http_xslt_module \
 --with-http_spdy_module \
 --with-ipv6 \
 --with-sha1=/usr/include/openssl \
 --with-md5=/usr/include/openssl \
 --with-mail \
 --with-mail_ssl_module \

 # wget https://you.googlecode.com/files/ngx_cache_purge-1.6.tar.gz
 --add-module=/software/ngx_cache_purge-1.6 \

 #http://www.openssl.org/source/openssl-1.0.1e.tar.gz
 --with-openssl='/software/openssl-1.0.1e'

 # Build and install nginx
 make && sudo make install

It compiles successfully without any error.
Result 0f nginx -V gives following
nginx version: nginx/1.3.13
built by gcc 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
TLS SNI support enabled
configure arguments: --sbin-path=/usr/local/sbin/nginx --prefix=/etc/nginx --conf-           path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-debug --with-http_addition_module --with-http_dav_module --with-http_gzip_static_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-http_spdy_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/software/ngx_cache_purge-1.6 --with-openssl=/software/openssl-1.0.1e

My /etc/nginx/site-enabled config has
server {
      listen 443 ssl spdy;

      ssl_certificate      server.crt;
      ssl_certificate_key  server.key;

      ...
  }

After all this successfull installation nginx does not restart with spdy param in server block of site-enabled file.
Any suggestions? I am sure of missing something here but can't figure out.
Comment: SPDY is not a big bonus by itself, but with conjunction to TCP/IP tuning https://coderwall.com/p/8igwqa
Comment: Follow this article http://www.liberiangeek.net/2014/10/install-latest-version-nginx-ubuntu-14-10/
Answer: UPDATE (November 19th, 2013):  Modified script for nginx 1.4.3 (Does not need spdy patch)
https://gist.github.com/deepak-kumar/7541199#file-compile_nginx_1-4-3_with-spdy-sh
I wrote Shell Script for the setup
https://gist.github.com/deepak-kumar/5069550#file-compile_nginx_with_spdy-sh
I have found the solution to the problem.
I already had nginx package installed on my ubuntu 12.04 even before compiling this 1.3.13 which was causing the problem.$ sudo apt-get install nginx
To solve this issue I made sure that /etc/init.d/nginx should use the correct binary.
I did following on terminal:
$ which nginx
$ /usr/local/sbin/nginx

Checked my existing /etc/init.d/nginx script it was using wrong DAEMON path so I changed it to look like this (works)
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx # $which nginx

Earlier above values were (does not work)
#PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
#DAEMON=/usr/sbin/nginx

Rest of the file remains same. So essentially I used the correct version binary.
Update: This blog is also a very good reference point in case you guys are interested.
http://blog.bubbleideas.com/2012/08/How-to-set-up-SPDY-on-nginx-for-your-rails-app-and-test-it.html
Comment: @marvindanig this should help, more over you don't need patch anymore just pass --with-http_spdy_module option while compiling http://blog.glaucocustodio.com/2014/11/24/adding-support-to-spdy-protocol-on-nginx-with-zero-downtime/
Comment: Might be easier to "apt-get remove nginx" .. although that sort of burns your bridge if working on production system.
Comment: Is there an update to this shell script for nginx 1.6.2. The script seems to fail on Ubuntu 14.0.4 for the latest nginx.
Comment: Thanks @DaveKirk I got this one covered and wrote about it here -> http://marvindanig.svbtle.com/how-to-set-up-ssl-with-spdy-and-nginx

How to set up SPDY Protocol over Nginx?


How to create a single page application in Drupal?

Question: I know Drupal and now I am creating a website by Laravel framework. I know what are single page application and how better they are. I used Angular and Backbone as JavaScript framework/library to creating a single page application with Laravel.
I am not professional in Drupal, How can I use these libraries to create a single page application (SPA) using Drupal?
Is there any module to make a (SPA) or automatically make the website Full-Ajax?
Comment: This should be handy: https://www.acquia.com/resources/acquia-tv/conference/how-create-single-page-apps-drupal-may-28-2013
Answer: You could use the Ajax Regions module.
It allows you to load specific regions via Ajax, thus load your whole website on one single page
Answer: Try one of the following modules. (They are very different despite similar names.) You can find demo links for each module in their pages.
Single Page Website

This module provide the ability to display the content of all menu items of the main-menu on the Single Page Website module.
On the page 'single_page' the links in the menu will be replaced to the links to the correspondent anchors.
You can set URL 'single_page' as front page of your site, and your visitors will see the Single Page Website. But it will be the site managed by Drupal 7.

Single Page Site

This module provides functionality to create a single page website.
It allows you to automatically create a single page from a menu. The module will render all the content from the links, that are configured in the menu, on one page. It will then override the menu links so that they refer to an anchor instead of a new page. With some additional css you can develop a horizontal single page site.
Comment: Single page site is not theme dependent, single page website is
Comment: What? This is not usable for me! I think you did't read the question as well.
Comment: @KrishnaMohan I am kidding, sorry if I make you sad. :)
Answer: To create a single page app using Drupal I would recommend you look into the services module to create your end points and then use the Backbone module for creating front end logic. Whilst there isn't a stable release of the Backbone module we are using it on a production website without any problems.
If you Google "Drupal single page app" there are a few talks about it that come up.
The company I work for has a Druapl 7 single page app and it works well, but it pretty complex stuff - not something to take on if you don't know what your doing.


Reading large CSV files using delayed (DASK)

Question: I'm using delayed to read many large CSV files:
import pandas as pd

def function_1(x1, x2):
    df_d1 = pd.read_csv(x1)
    # Some calculations on df_d1 using x2.
    return df_d1

def function_2(x3):
    df_d2 = pd.read_csv(x3)
    return df_d2

def function_3(df_d1, df_d2):
    # some calculations and merging data-sets (output is "merged_ds").
    return merged_ds

function_1: importing data-set 1 and doing some calculations.
function_2: importing data-set 2.
function_3: merge data-sets and some calculations.

Next, I use a loop to call these functions using delayed function. I have many CSV files, and every file is more than 500MB. Is this a suitable procedure to do my tasks using DASK (delayed)?
Comment: So you want `function_x` to be delayed? Why don't you produce a [mcve](/help/mcve)?
Answer: Yes, please go ahead and delay your functions and submit them to Dask. The most memory-heavy is likely to be function_3, and you may want to consider how many of these you can hold in memory at a time - use the distributed scheduler to control how many workers and threads you have and their respective memory limits https://distributed.readthedocs.io/en/latest/local-cluster.html
Finally, you I'm sure do not want to return the final merged dataframes, that surely does not fit in memory: you probably mean to aggregate over them or write out to other files.


Getting a bus error trying to switch letters in a string

Question: Below is a snippet of my code. My code is supposed to reverse alphabet chars only and skip any special characters. I am currently trying to use "ab-cd" and hope to print "ba-cd", but when I run my code I get a bus error.
I have narrowed down the issue to lines 31 and 32. I malloc'd, but unsure why it will not let me switch chars.
Although it does not show, I will make sure to free the memory using free.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cytype.h> // used for isalpha()

int main(int argc, const char *argv[])
{
    // declaring a pointer of type char to tempString
    char * tempString = (char *)malloc(sizeof(char)* 6);
    tempString = "ab-cd";

    char temp;
    temp = tempString[0];
    tempString[0] = tempString[1];
    tempString[1] = temp;

    printf("%s\n", tempString);

    return 0;
}

Comment: Please post all code as text, not a picture of text.
Comment: `tempString = "ab-cd";` discards the pointer you got from `malloc`, and replaces it with a pointer to a string literal which isn't writable.  You could copy the string `"ab-cd"` into the malloced space, if you allocated enough (currently you have allocated only enough for 1 character, and you need 6).
Comment: `tempString` points to a string literal. String literals are constants and cannot be changed. As a side note, the call to `malloc` is unnecessary and leads to a memory leak.
Comment: The `malloc` isn't necessary.  `char tempString[] = "ab-cd";` suffices.
Answer: The line tempString = "ab-cd"; is not doing what you think it is.  Just one line earlier you had stored in tempString the address of an allocated buffer.  In this line, however, you store the address of "ab-cd" in tempString.  You're not writing to the buffer but rather causing tempString to point somewhere else entirely.
Specifically, you're directing it a string literal which is stored in a read-only section of memory.  Therefore, trying to alter it as you're doing is going to crash your program.
What you want is to write the string into the buffer you already have.  This can be done by
strcpy(tempString, "ab-cd");

EDIT (thanks to @NateEldredge for catching this):
Your allocated buffer is not big enough to hold "ab-cd".  You need to change the size of the allocation:
tempString = malloc(sizeof(char)*6);

Comment: You'd also need to increase the size of the malloc'ed buffer to at least 6.
Comment: Yeah I was missing the * 6 in my malloc. Thank you. Also , your suggestions helped. I forgot that the way I was attributing tempString was making it a string literal. I used strcpy and saw the difference. Thank you for the help. It is greatly appreciated. Wishing good karma your way.
Comment: Code like `... malloc(sizeof(char)*6)` is a very bad idea, sorry no upvote from me.
Comment: I will endeavor to console myself.
Answer: For starters you allocated not enough memory to store the string "ab-cd"
char * tempString = (char *)malloc(sizeof(char));

You allocated only one byte that can store only one character.
Then in the following statement you reassigned the pointer tempString with the address of the first character of the string literal "ab-cd".
tempString = "ab-cd";

As a result the program produces a memory leak because the address of the allocated memory is lost.
Then you are trying to change the string literal pointed now by the pointer tempString. But any attempt to change a string literal results in undefined behavior. From the C Standard (6.4.5 String literals)

7 It is unspecified whether these arrays are distinct provided their
elements have the appropriate values. If the program attempts to
modify such an array, the behavior is undefined.

There is no need to allocate memory dynamically. You could just declare a character array and initialize it with the string literal like for example
char tempString[] = "ab-cd";

If you want to allocate an array dynamically you need to write
char *tempString = malloc( sizeof( "ab-cd" ) );
strcpy( tempString, "ab-cd" );

and then when the array will not be needed any more you should free it like
free( tempString );

Comment: thanks! This explanation really helped!
Comment: Having the hardcoded string `"ab-cd"` looks like a bad idea to me.


Algorithms for generating $A_n$ and $S_n$ from specific generators

Question: Is there a simple algorithm to generate the elements of the alternating group $A_n$ in terms of some small set of generators?
For example, when $n = 4$, I'm looking for an algorithm whose output is a generating set, say $\{ a = (1,2,3), b = (1,2)(3,4) \}$ together with a list of 12 words in $a$ and $b$ that represent that elements of $A_4$.
What about such an algorithm for the symmetric group? For example, when $n = 4$ an algorithm that outputs, for example, $\{ a = (1,2,3,4), b = (1,2) \}$ and a listing of $S_4$ as 24 words in $a$ and $b$.
Comment: If you want an algorithm that will work for _arbitrary_ generating sets, that can be actively difficult (for instance, that would solve most Rubik's Cube-style puzzles).  If you want an algorithm that will work in terms of a generating set that it can provide, that's much easier.
Comment: Having the algorithm choose an easy generating set is OK. I'll update my question.
Comment: I can turn this into a proper answer when I get home (and can flesh it out with a few details so it isn't link-only), but this is covered in vol. 4 of Don Knuth's _The Art Of Computer Programming_, specifically section 188.8.131.52, 'Generating all Permutations'; you can find the original fascicle text at http://www.cs.utsa.edu/~wagner/knuth/fasc2b.pdf .  In particular, there are some 'Gray Code' style algorithms for $S_n$ where by taking every other answer you get an algorithm for $A_n$.
Comment: @StevenStadnicki - But does the algorithm you have in mind express $S_n$ in terms of some generating set? I'm looking for something a little different than just enumerating everything: I'd like to enumerate the elements of these groups in terms of some small generating set.
Comment: David: it does, though the lengths of elements could be comparable to the size of the group itself (since each group element is generally built from applying one generator or the other to the previous element (for instance, in 'Ringing Changes'-style enumerations, one alternates a particular two-element transposition with repeated applications of an $n$-cycle).
Answer: For the symmetric group $S_n$, you can generate it with the two elements $a=(1,2)$ and $b=(1,2,\dots,n)$: By conjugating $a$ by $b^k$, you get $(1+k,2+k)$; this allows you to swap any two adjacent elements $1,\dots,n$ using only the generators $a$ and $b$. Given an arbitrary permutation, you can then bring it to the identity permutation by using the bubble-sort algorithm; if you keep track of the steps in this process, it will show you how to express the permutation as a word in $a$ and $b$.
This technique can also be adapted to show that the alternating group $A_n$ can be generated by two elements, $a=(1,2,3)$ and $b=(1,2,\dots,n)$ (if $n$ is odd) or $b=(2,\dots,n)$ (if $n$ is even).
Comment: Note, BTW, that the $\Theta(n^2)$ bound you get here on the maximum length of words is optimal (modulo constant factors) for these generators.  See http://mathoverflow.net/questions/102737/diameter-of-symmetric-group - I don't know how much better you can do with a 'constant' set of generators.


How to append value in an existing object without change the first object with javascript?

Question: My javascript code like this :
var data= {product_id: 4, name: 'nike'}
console.log(data)

var dataNew = data
dataNew.id = 1

console.log(dataNew)

Demo is like this : https://jsfiddle.net/oscar11/69dw8dv1/
I append value in the object like that
The result of console.log(data) and console.log(dataNew) is there exist id
I want var data is no id
So there is only var dataNew that has id
How can I do it?
Comment: `const dataNew = Object.assign({}, data)`
Comment: `delete data.id` but this would delete the property from `dataNew` too. You need to clone the object before doing that.
Comment: @Werner, How can I clone the object?
Answer: In JavaScript, object references are assigned, instead of the object values, unless the object is of primitive data types.
That's why dataNew and data refers to the same object when you do this:
var dataNew = data;

To prevent this, you need to create a copy of data like this:
var dataNew = Object.assign({}, data);

Now, the changes in dataNew won't affect data.


Hook for Node Add Field?

Question: For my use case, this question revolves around the entities node and paragraph.
Fields in my projects are created under the following structure:

content_h2
paragraph_h2
content_p
paragraph_p
...

etc...
For example, I'd like to create field_name on a node, check if it exists in paragraph entity storage and if not create it. And the other way around.
I've had some success but it seems like more coding than needed.
Does a hook exist for node add field?
Example form class: field-ui-field-storage-add-form.
Comment: which like any entity has plenty of CRUD hooks, see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Entity%21entity.api.php/group/entity_crud/8.7.x
Comment: Aren't storage and instances stored differently for different entities? I don't see user fields for nodes or paragraphs and Vice versa.
Comment: @Kevin precisely, so OP wants to avoid two of the same.
Comment: This looks like it is about the config entity FieldStorageConfig, see https://drupal.stackexchange.com/questions/222260/add-content-type-field-programmatically
Answer: By the way, all the hooks are listed here there doesn't appear one that fits the task tho.
You can always add a custom validation to the field add form.
/*
 * Implements hook_form_FORM_ID_alter()
 */
function mymodule_form_field_ui_field_storage_add_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $form['#validate'][] = 'mymodule_custom_add_field_validation';
}

/*
 * Custom Validate Callback
 */
function mymodule_custom_add_field_validation(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  // the code that performs the check
  // if (......) {
  //   $form_state->setErrorByName('label',
  //     t('This field already exists in another entity'));
  // }
}

Hook for Node Add Field?


Set a main-menu item as active with custom code

Question: I want to set a menu item of the main menu as active (class="active").
I'm specifically looking for a hook or function, not a contrib module.
I have seen recommendations for Context, and I have looked in the module code, but it uses hooks which I cannot find on the API-Site.
Answer: If you simply want to add the 'active' class to a menu item you could do it in the code before printing the menu.
If you want the menu item to also act like an active item you could use menu_tree_set_path() that was supposedly added in Drupal 7.9.
In earlier versions and Drupal 6 I used this:
function mymodule_init() {
  $item = menu_get_item($_GET['q']);
  $item['href'] = 'node/1';
  menu_set_item(NULL, $item);
}

However both options just add the 'active-trail' (not 'active') class(es), but that should suffice in most cases.
Comment: This seems not to work. In the `navigation` menu the tree is expanded, but the entry in the `main-menu` is not *active*.
Comment: If you mean 'active' class then yes that one doesn't get added with this code, but it gets 'active-trail' that should be helpful. If you mean something else then I'd need a bit more information of what you are trying to achieve.
Comment: I have two taxonomies which splits the page in two parts. With seo stuff I managed that the two taxonomies have their own path e.g. example.com/tag1 and example.com/tag2 which is linked in the main-menu in the header. A example for content which is in the taxonomy tag1 is aviable at example.com/tag1/welcome. Now I want that the link in the main menu in the head is still active.
Answer: You could use menu_set_active_trail to set which menu should be rendered as active.
Another way is to mess with $_GET['q'], but you really want to avoid that if at all possible, since it can lead to some strange bugs.
Comment: Could you add a small example for `menu_set_active_trail`?
Comment: @rekire look at [`book_build_active_trail`](http://api.drupal.org/api/drupal/modules--book--book.module/function/book_build_active_trail/6) for inspiration (D6);
Answer: My solution is this:
function mymodule_init() {
    $link=menu_link_load(346);
    $link["options"]["attributes"]["class"]="active";
    menu_link_save($link);
}

The id 346 is the mlid no idea what this mean but I got this id with this little dump:
echo('
');
dump(menu_tree_all_data('main-menu'));
echo('
');

Comment: Surely your solution will give the item with `mlid` (Menu Link Identifier for your reference) 346 the "active" class regardless of what is ACTUALLY the active link?  Is this your intended behaviour?
Answer: You can also use hook_translated_menu_link_alter()
/**
 * Implements hook_translated_menu_link_alter()
 */
function Module_translated_menu_link_alter(&$item, $map) {

    if ($item['menu_name'] == 'menu-name') {

        //check first two arg from url
        $path = arg(0)."/".arg(1);

        //add class active-trail if path match
        if (strpos($item['link_path'], $path) !== false) {
            $item['in_active_trail'] = true;

        }

    }
}


There is a easy way in R to check dimensionality in IRT with principal component analysis on standardized residuals?

Question: I suspect that a test done by thousands of students has a dimensional problem (there are two or more dimensions influencing the students' chance of getting the items right).
I have a dichotomous matrix of responses from these students. I thought about testing this hypothesis with Principal Component Analysis on Standardized Residuals in R. However, I can't find any straightforward way to implement this analysis. Is there any package that can do it? Are there any other (or better) way for test this hypothesis?
Comment: You can fit a single factor CFA  model using lavaan.
Answer: Good question. To be precise, the number of students in your sample should not change the number of hypothesized dimensions. Instead, this should be driven by the purpose of the analysis (e.g., test scoring, analysis group differences) and the item content. That being said, unidimensional models typically can be improved upon (in terms of model-data fit) in large samples when a moderate number of items are observed.
I agree with @Jeremy Miles that lavaan is a good choice. Below is some code for fitting a confirmatory factor analysis (CFA) model in R and extracting residuals.
library(lavaan)
HS.model <- ' visual  =~ x1 + x2 + x3
              textual =~ x4 + x5 + x6
              speed   =~ x7 + x8 + x9 '

fit <- cfa(HS.model, data = HolzingerSwineford1939)

lavResiduals(fit)


Draw multiple graphical objects in a spark itemRenderer using MXML?

Question: I would like my spark ItemRenderer to be able to render a varying number of graphical objects that depends on the user input. As an example, let's say that I want to render a set of ellipses on a line.
I've been using MXML for my most recent batch of ItemRenderers and have loved every minute of it, but I'm not sure how to accomplish the above goal with an MXML IR. In Actionscript I can acquire the list of ellipses locations and draw them programmatically in the updateDisplayList method. Is there an equivalent in MXML?
thank you for your help!
Answer: Of course you can place a DataGroup inside your item renderer and place there some graphical primitives like Ellipse driven by data provider. But from the performance point of view this implementation will be very problematic. I suggest you to override updateDisplayList() in your MXML item renderer and perform all the drawings there.


Unable to use setOnClickListener on button in fragment

Question: I have tried to add an setOnClickListener in my Fragment. But both methods i tried didnt work. There is no readction to my button press in the App.
class LoginFragment: Fragment(), View.OnClickListener {

    //AuthVariable for global use
    private lateinit var myAuth: FirebaseAuth

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var view = inflater.inflate(R.layout.content_login, container, false)

        view?.login_button?.setOnClickListener(this)
        return view
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

    }
    override fun onClick(p0: View?) {
        Log.i("BUTTON123","TEST")
        loginUser()

    }
}

Activity:
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

    //AuthVariable for global use
    private lateinit var myAuth : FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        val toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

        nav_view.setNavigationItemSelectedListener(this)

        //Initializing FirebaseAuth Instance
        myAuth = FirebaseAuth.getInstance()
        var user = myAuth.currentUser
        if(user != null){
            updateUI(user)
        }else{
            val test = supportFragmentManager
            val frag  = LoginFragment()
            test.beginTransaction().replace(R.id.placeholder,frag).addToBackStack(null).commit()
        }
    }

    override fun onBackPressed() {
        if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
            drawer_layout.closeDrawer(GravityCompat.START)
        } else {
            super.onBackPressed()
        }
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        when (item.itemId) {
            R.id.action_settings -> return true
            else -> return super.onOptionsItemSelected(item)
        }
    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        // Handle navigation view item clicks here.

        when (item.itemId) {
            R.id.nav_addShop -> {
                //Activity to Add your Shop
                val addshopIntent = Intent(this,AddShopActivity::class.java)
                startActivity(addshopIntent)

            }
            R.id.nav_searchShop -> {
                //SearchFragment
                val test = supportFragmentManager
                val frag  = test()
                test.beginTransaction().replace(R.id.placeholder,frag).addToBackStack(null).commit()

            }
            R.id.nav_shopMap -> {
                //MapActivity
                val mapintent = Intent(this, ShopMap::class.java)
                startActivity(mapintent)
            }
            R.id.nav_manage -> {

            }
            R.id.nav_share -> {

            }
            R.id.nav_logout -> {

                myAuth.signOut()
                updateUI(myAuth.currentUser)
            }
        }

        drawer_layout.closeDrawer(GravityCompat.START)
        return true
    }
}

Comment: Can you paste your activity code?
Answer: Did you forget the onClick() method in LoginFragment?
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val view: View = inflater.inflate(R.layout.content_login, container, false)

    view.login_button.setOnClickListener {
        Log.i("BUTTON123","TEST")
        loginUser()
    }

    return view
}

Comment: replace your onCreateView() with the one from my edited code
Comment: sry i somehow delted this part while coping the code
Comment: sadly not i looked on multiple pages, but still its not working
Comment: Did you put any code like a toast inside the onClick?
Comment: i put a Log.I in it which doesnt give any output. I added a toast. Stil no reaction
Comment: Is the id of the button login_button?
Comment: Remove all `?` from `view?.login_button?.setOnClickListener(this)` and see if you get error.
Comment: Remove then '?' Throws not Error at all, if i press the button. And still nothing happens
Comment: Move this line `view.login_button.setOnClickListener(this)` in `onViewCreated()`
Comment: Nothing changes when i move the line to 'onViewCreated()'
Comment: I'm glad_______


Accessing member of compound literal using Visual Studio

Question: Excuse me for the title of this question, I will probably update the title once I have my answer.
I have this ancient code compiling and working with gcc 4.x:
struct S {
    int a;
    int b;
};

int main(void)
{
    return (struct S){1,2}.a;
}

As you can see I cast the {1,2} to struct S to access the member a. How is this feature called in the C language? Or what are the correct terms to apply for this construct?
Is this feature supported in any of the visual studio versions?
Comment: It is like GCC [6.25 Compound Literals](http://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html)
Comment: I found this question, please help me close mine: http://stackoverflow.com/questions/3869963/compound-literals-in-msvc
Comment: @Patrick B: My copy of MSVS 2012 Premium does *not* support it.  I get `error C2059: syntax error: '{'`.  I understand C99 *will* be supported in MSVS2013.  You can download it for free and try it: [MSVS 2013 Express Preview](http://www.microsoft.com/en-us/download/details.aspx?id=39313).
Answer: It's called compound literal, which is introduced in C99. The relevant part in the standard is 18.104.22.168 Compound literals
The bad news is Visual Studio only supports C89 right now, so this feature is not supported in Visual Studio.
Comment: VS 2013 introduced support for C99 compound literals in C mode.


How to convert Blob to Mat in java opencv?

Question: I am trying to get images from MySQL database stored as blob and convert them into Mat object for image processing. I have a template image and want to know if that image is part of db image or not using matchTemplate() function.  Here is my code:
String sql="select * from image_data where image_id="+1
try
        {
            query obj_query=new query(obj_connect);
            ResultSet rs = obj_query.runSimpleQuery(sql);
            while (rs.next())
            {
                Blob image_blob=rs.getBlob("original_image");
                Mat img=Imgcodecs.imread(image_blob.toString());
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
Mat templ=Imgcodecs.imread("templ.png");

Passing these images in findTemplate function:
MatchTemplate.findTemplete(img, templ, "ress.png", Imgproc.TM_SQDIFF);

And here is my findTemplate function:
public static void findTemplete(Mat img, Mat templ, int match_method)
     {
        double minVal;
        int result_cols = img.cols() - templ.cols() + 1;
        int result_rows = img.rows() - templ.rows() + 1;
        Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
        Imgproc.matchTemplate(img, templ, result, match_method);
        MinMaxLocResult mmr = Core.minMaxLoc(result);
        minVal = mmr.minVal;
        if(minVal<=0.75){
            //do what you want
        }
        else
        {
            //do what you want
        }
    }

When I run it, it gives me an error:
OpenCV Error: Assertion failed (s >= 0) in cv::setSize, file C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp, line 306
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\core\src\matrix.cpp:306: error: (-215) s >= 0 in function cv::setSize
]
    at org.opencv.core.Mat.n_Mat(Native Method)
    at org.opencv.core.Mat.<init>(Mat.java:37)

Line on which it gives me the error is:
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);

How can I handle Blob image to pass it in Imagecodecs.imread() function? Or is there another technique to convert Blob to Mat?
Comment: You should probably use `imdecode` instead of `imread`. Have a look also [here](http://stackoverflow.com/q/32542853/5008845)
Comment: @Miki link that you provided is in c++ code. i am using java and mysql. can you give me java  example.
Answer: Simple as this.
byte[] decodedString = Base64.decode(imageBlob, Base64.DEFAULT);
Bitmap img = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Mat mat_img = new Mat();
Utils.bitmapToMat(img1, mat_img1);


Python class attributes decorator

Question: not sure about the proper wording for that, but essentially what I would like to ask is if it is possible in python do something like this:
class Customer:
    self.name = 'Freddy'
    @reportMarketing
    self.surname = 'Krueger'
    self.eyes = 'Blue'
    @reportMarketing
    self.address = 'Elm Street'
    @reportAccounting
    self.tax_id = '8ab9a66cf'
    ...

where @reportMarketing  and @reportAccounting would be decorators (of sorts). The idea is that an instance like the one above could be then passed to some 'reporting class' that - based on whether the instance's attribute is decorated with @reportMarketing or a @reportAccounting, would then consider it for a specific 'reporting'. For instance, imagine you have customers database with instances like the one above, and you want data/attributes decorated with reportMarketing to be included in a report that goes to your marketing department, while the data/attributes decorated with reportAccounting to be included in a report that goes to your accounting department.
I guess conceptually, my question is can I even have decorators for attributes? I understand they are 'typically' (always?) meant for functions.
Also, to give more background, the classes I deal with have plenty of attributes (each), so going through all of them manually is quite error-prone, and so I thought dealing with it by simply decorating the couple of ones I'm after, is the cleanest and most extensible in the future.
Comment: `reportMarketing = ['surname', 'address']` and so on?
Comment: Decorator syntax is just a shorthand for function application. `self.surname = reportMarketing("Krueger")`, assuming that `reportMarketing` is defined to accept a string and return the same string, while having some other interesting or useful side effect. That said, the shorthand *only* applies to `class` and `def` statements, not assignments.
Comment: @chepner Meaning effectively there's no notion of attributes decoration? A class's methods (via the `def` which you mention) can be a 'subject' of an annotation, but not this class's attributes?
Answer: As @chepner alredy pointed out in their comment, decorators don't apply to attribute assignments, only to class and def statements.
Independent of that, I find it a big hard to imagine how the decorators in your example would work, since they are functions called with the decorated class or definition as argument.
One method to have "labels" of sorts for your attributes is this:
class Customer():
    attrs_reportMarketing = ('surname', 'address')
    attrs_reportAccounting = ('tax_id',)

    def __init__(self, name='', surname='', eyes='', address=''):
        self.name = name
        self.surname = surname
        self.eyes = eyes
        self.address = address
        self.tax_id = tax_id

def generate_Marketingreport(cust):
    report = {attr_name: getattr(cust, attr_name)\
              for attr_name in cust.attrs_reportMarketing}
    return(report)

customer1 = Customer(name='Freddy', surname='Krüger', eyes='blue',\
                     address='Elm_Street', tax_id='8ab9a66cf')

report1 = generate_Marketingreport(customer1)
# Produces a dictionary: {'surname':'Krüger', 'address':'Elm_Street'}

The way it works: The customer class defines fixed tuples which document which of its attributes should be included in particular reports. Whatever method generates the reports can access those and compile the respective attributes into a dictionary (or whatever else you'd do with them).
This will allow you do include the same attribute in more than one "report" group, and because attrs_reportMarketing are class attributes (defined with the class, not with the instance), you can even access them without any customer being defined, using e.g. Customer.attrs_reportAccounting. That sort of thing can be useful if a class has a lot of optional inputs, and you have some code that needs to figure out which of them need to be provided in order to allow certain functionality.
The same strategy can be expanded by specifying dictionaries as class attributes which contain useful information about the class. I find that sometimes useful if some generic code needs to create some object, but may have to select a (sub-)class dependent on the current situation, or if the code gets passed an object and needs to figure out how it needs to be handled. Example: your person class has a customer subclass and a partner one. All of them have names, but only the customer class has a get_payment_history() method, and only partners have a contact attribute.
One word of caution, though: Hard-coding class attributes like this, in the source code, can also get messy. Depending on the complexity of the whole thing, it can be better to move this kind of information to a separate file, close to the input data, and separate from the code, because you might want to change the contents of your marketing report at some point, and wouldn't it be easier to do that by editing a report definition in a plaintext file, than having to wade through the hole source code to sift through all the classes that may or may not contain information that is relevant for marketing?
Then you could set up a small separate file like this:
keys_marketingreport = {'cust': ('surname', 'address'),
                        ...}

...and have your report generator parse that, and know which attributes of the cust object instance it needs to include, and which attributes of whatever other inputs it may have available.

Python class attributes decorator


What is the "Mean Sq" column of "Residuals" in "anova" of a linear model in R?

Question: Here is anova output from a linear model:
> model<-lm(mpg ~ wt + cyl, data = mtcars)
> anova(model)
Analysis of Variance Table

Response: mpg
          Df Sum Sq Mean Sq F value    Pr(>F)
wt         1 847.73  847.73  128.60 3.535e-12 ***
cyl        1  87.15   87.15   13.22  0.001064 **
Residuals 29 191.17    6.59
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sum Sq is the sum squared error between mpg and the model prediction.  So, for example, overall residuals:
> sum((predict(model, data=mtcars) - mtcars$mpg)^2)
[1] 191.172

What exactly is Mean Sq?
Comment: It's the sum of squares divided by thee degrees of freedom. I suggest going through the JBStatistics ANOVA videos: https://youtube.com/results?search_query=jbstatistics+anova.
Comment: Thanks. If you submit this as an answer, I'll accept it. I'd add "191.172/29=6.59 to relate it to the particular output.
Answer: "Mean Sq" is the sum of squares divided by the degrees of freedom. In your example:
> 191.17 / 29
[1] 6.592069

The mean square for the residuals is related to the variance of the distributions of your groups. In fact, the mean squares of the residuals is an unbiased estimator for that variance!
(At a somewhat higher level, you may see machine learning people talk about mean squared error as a loss function or performance metric, and they might just divide by the sample size instead of the slightly smaller degrees of freedom. That approach does not give an unbiased estimator of the variance of the response distributions, but it results in the same model and can be quicker to calculate. When they say error, they mean the residuals. Technically, this terminology is incorrect, and "residuals" is the proper term, though this abuse of terminology does not seem to cause problems.)
JBStatistics has some good ANOVA videos: https://youtube.com/results?search_query=jbstatistics+anova.


Reference parent pseudo selector with Sass.

Question: This here is what I want to do in plain CSS. Note that I only want to hover apply to the .parent:hover and not any parent with :hover.

.container {
  padding: 1em;
  border: solid 1px black;
 }

.parent {
  position: relative;
  width: 3em;
  height: 3em;
  background-color: #4d4;
}

.child {
  position: absolute;
  right: 0;
  top: 0;
  height: 1em;
  width: 1em;
  background-color: red;
  opacity: 0;
  transition: opacity 0.5s linear;
}

.parent:hover .child {
  opacity: 1.0;
}

Question is how I do this in SCSS?
ie. if my SCSS is looking like:
.container {
    //...
}

.parent {
   //...

   .child {
      //..
      opacity: 0;
      transition: opacity 0.5s linear;

      //Hover selector here?
   }
}

I've been playing around with Sassmeister but I can't get this to work.
Answer: Your .parent selector:
.parent {
  position: relative;
  width: 3em;
  height: 3em;
  background-color: #4d4;

  &:hover {
    .child {
      opacity: 1.0;
    }
  }
}

Comment: This is what I ended up doing - but I don't like that I have to define `.child` behaviour in two different places.


PDO - Insert new row when duplicate detected based on 3 values

Question: Currently I run a SELECT query on a table and if the num_rows is zero i then run a new function that INSERTs the record into the table. This seems very inneficient and I am struggling to find a good solution.
Current set up (simplified):
Select Function -
$sql = "SELECT * FROM myTable WHERE col1 = ".$val1." AND col4 = ".$val4." AND col6 = ".$val6
// rest of code returns $numRows variable

If no $numRows is 0 then run a fresh query inserting the new record.
if($numRows == 0) {
    try {
        $dbh = $this->_dbSite;
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $stmt = $dbh->prepare("INSERT INTO myTable (col1, col2, col3, col4, col4, col6, col7) VALUES (:val1, :val2, :val3, :val4, :val5, :val6, :val7");
        $stmt->bindParam(':val1', $val1, PDO::PARAM_STR);
        $stmt->bindParam(':val2', $val2, PDO::PARAM_STR);
        $stmt->bindParam(':val3', $val3, PDO::PARAM_STR);
        $stmt->bindParam(':val4', $val4, PDO::PARAM_STR);
        $stmt->bindParam(':val5', $val5, PDO::PARAM_STR);
        $stmt->bindParam(':val6', $val6, PDO::PARAM_STR);
        $stmt->bindParam(':val7', $val7, PDO::PARAM_STR);
        $stmt->execute();
    } catch (PDOException $e) {
        return $e->getMessage();
    }
} else {
    $message = "Sorry this record already exists";
}

Table structure:
col1   | col2 | col3 | col4 | col5 | col6 | col7
________________________________________________
user1    abc    sol    red    doo     3a    def
user2    abc    ast    Blue   doo     4a    def
user1    abc    ast    blue   doo     3a    def
user4    abc    ast    red    doo     6a    def
user1    abc    ast    Green  doo     3a    def
user2    abc    ast    red    doo     7a    def
user1    abc    ast    red    doo     3a    def

The above example should be avoided as the first and last rows are identical in terms of col1,col4 and col6
This does not seem the most efficient way of doing this. Has anybody encountered this before? Any advice would be greatly appreciated.
Answer: If the combination of col1, col4, and col6 is intended to be unique -- meaning that it should only appear once, then you can do this with insert on duplicate key update and a unique index:
CREATE INDEX idx_myTable_col1_col4_col6 on (mytable, col1, col4, col6)

INSERT INTO myTable(col1, col2, col3, col4, col4, col6, col7)
    VALUES (:val1, :val2, :val3, :val4, :val5, :val6, :val7)
    ON DUPLICATE KEY UPDATE col1 = VALUES(col1);

The update part is a no-op -- it just prevents an error from returning.
You can also do:
INSERT INTO myTable(col1, col2, col3, col4, col4, col6, col7)
    SELECT :val1, :val2, :val3, :val4, :val5, :val6, :val7
    FROM dual
    WHERE NOT EXISTS (SELECT 1
                      FROM myTable t2
                      WHERE t2.col1 = :val1 and t2.col4 = :val4 and t2.col6 = :val6
                     );

This will work regardless of whether or not the columns need to be unique.
Comment: The first option looks efficient. The `cols` will be unique to that user ID. I'm not sure it would work in this instance.
Comment: @AlwaysLearning: In that case you would put a `user_id` column in the table and make it part of the `UNIQUE KEY`.
Comment: @GordonLinoff Shouldnt that be `CREATE UNIQUE INDEX idx_myTable_col1_col4_col6 on mytable  (col1, col4, col6);`?
Comment: I've added a table structure example to my question. As you can see the cols are not unique within the whole table. Only unique per user within the table.
Comment: Yeah so `col1` is you user so by adding a unique key with `(col1, col4, col6)` you are doing the correct thing - ie. a row is only a duplicate if all 3 of those columns have the same value in another row. So his advice still stands though i think he may have gotten the syntax incorrect for creating the index/key.
Comment: I have just tried this and it did not work. After looking into it I think this is because one of the columns I am inserting into needs to be a primary key or have a unique constraint.


dl.example.com/file1.zip instead of example.com/sites/default/files/file1.zip

Question: How can I convert address of uploaded files from example.com/sites/default/files/file1.zip to dl.example.com/file1.zip?
Is there a module for this purpose? If not what's the solution?
Answer: In my opinion if you can create a subdomain dl.example.com then you can also redirect all requests to default/files subfolder using symbolic link in /home/domains/dl.example.com/ linking to /home/domains/example.com/sites/default/files/
depending on your .htaccess or httpd.conf setup.
Please note I don't think this is a Drupal-specific question, I am not an expert on this and you'd better asked on https://serverfault.com/
I think using this method will be faster then creating a custom module to do this for you because the method I explained doesn't even require loading Drupal for the request handling.


$\operatorname{pd}(M)\leq n\implies\operatorname{Ext}_{R}^{n+1}(M,N)=0$

Question: Let $M,N$ be $R$-modules. Since $\operatorname{pd}(M)\leq n$, we have a projective resolution, $$\cdots\rightarrow0\rightarrow P_n\rightarrow\cdots\rightarrow P_0\rightarrow M\rightarrow0.$$
Then $\operatorname{Ext}_{R}^{n+1}(M,N)=\ker d^{n+1*}/\operatorname{im}d^{n*}$ where $$d^{n+1*}:\operatorname{Hom}_R(P_n,N)\rightarrow \operatorname{Hom}_R(P_{n+1},N).$$
Since $P_{n+1}=0$, $\operatorname{Hom}_R(P_{n+1},N)=0$. Then $\ker  d^{n+1*}=\operatorname{Hom}_R(P_n,N)$.
Is $\operatorname{im}d^{n*}=\operatorname{Hom}_R(P_n,N)$?
Answer: There is a problem with notation. We have a projective resolution
$$
0 \xrightarrow{d^{n+1}} P_n \xrightarrow{d^{n}} P_{n-1} \xrightarrow{d^{n-1}} \cdots \xrightarrow{d^{2}} P_1 \xrightarrow{d^{1}}P_0 \rightarrow M \rightarrow 0
$$
and the $\operatorname{Ext}_R^j(M,N)$ groups are computed as the homology of the complex
$$
0 \rightarrow \operatorname{Hom}(P_0, N) \xrightarrow{{d^{1}}^\ast} \operatorname{Hom}(P_1, N) \xrightarrow{{d^{2}}^\ast} \cdots \xrightarrow{{d^{n}}^\ast} \operatorname{Hom}(P_n, N) \xrightarrow{{d^{n+1}}^\ast} 0 \cdots
$$
Then (in this notation) $\operatorname{Ext}_R^{\color{red} j}(M,N) = \ker {d^{\color{red}{ j+1}}}^\ast / \operatorname{im}  {d^{\color{red} j}}^\ast$ for every $j\geq 0$. Just need to note that $\operatorname{Ext}_R^{\color{red} 0}(M,N) = \ker {d^{\color{red} 1}}^\ast$.
Clearly $\operatorname{Ext}_R^{n+1}(M,N) = \ker {d^{n+2}}^\ast / \operatorname{im}  {d^{n+1}}^\ast= \{0\}$ since the relevant modules are $P_{n+1} = P_{n+2} = \{0\}$.


How to selectively apply JavaScript block based on profile in JHipster

Question: I try to include HeapAnalytics javascript block in index.html.
HeapAnalytics gives me two version of javascript block, one meant for development and one for production. It looks like this:
// dev

// prod

I want to know how to include the right one when I use "-Dspring.profiles.active=prod"?
Currently I do it manually, which is error prone.
Answer: This information can be obtained in the http header, X-Application-Context, which is returned by the server. For example, the following were returned for prod and dev profiles, respectively:
X-Application-Context:application:prod:8080

or
X-Application-Context:application:dev:8080

Comment: Thanks for the reply. I am checking if there's anyway, that change the tag in html for different profile, perhaps using grunt task?


Recuperar valor da chave adicionada no formData no backend

Question: Galera tenho a seguinte requisição no frontend
  formData.append("file", this.file);
  formData.append("name", "test");

  axios
    .post(`${baseApiUrl}/exame`, formData)

No backend eu consigo recuperar o valor do campo "file" com essa função:
const upload = multer({ dest: 'uploads/' }).single("file")

function save(req, res) {
    upload(req, res, function (err) {
        console.log(req.file) // Acesso ao conteúdo de file
        console.log(req.name) // Undefined :(
    })
}

Porém minha dúvida é, como faço para recuperar o campo "name" e outros caso eu decida adicionar?
OBS: Estou chamando a função upload dessa forma pois preciso capturar os erros (caso existam) na hora de gravar o arquivo, segundo a documentação doc multer
Answer: O correto é req.body para pegar o conteudo de um POST, então seria isso:
console.log(req.body)

Agora se esta usando multer e na verdade deseja pegar trabalhar o upload de um unico arquivo deveria fazer algo como:
const upload = multer({ dest: 'uploads/' })

app.post('/profile', upload.single('file'), function (req, res, next) {
  console.log("arquivo", req.file) is the `avatar` file
  console.log(req.body)
})

Isso aqui parece errado: upload(req, res, function (err) { e você escreveu o retorno dentro do terceiro parâmetro do objeto do mutler, quando deveria ser o terceiro parâmetro do objeto do app no método .post()

Se for upload múltiplo:
const upload = multer({ dest: 'uploads/' })

app.post('/photos/upload', upload.array('file', 12), function (req, res, next) {
     console.log(req.files);
     console.log(req.body);
})

Isso supondo que você esteja usando Express
Comment: Olá guilherme, obrigado por ajudar! No caso eu usei dessa forma pois preciso capturar os erros na hora que salvar o aquivo (caso exista), como mostra [link](https://github.com/expressjs/multer#error-handling). Sobre o req.body ele sempre vem como undefined :/
Comment: @Retiorts isso não foi informado na pergunta, parecia mais um erro de digitação, isso você sempre deve explicar. Vou editar
Comment: Verdade, esqueci de mencionar. Valeu, fico no aguardo :D


Calculate remainder of $12^{34^{56^{78}}}$ when divided by $90$

Question:
Calculate remainder of $12^{34^{56^{78}}}$ when divided by $90$

First of all,I'm not sure about the order of calculation of powers!
Secondly I don't know the rules of finding remainder moulus 90
Answer: Hint $\,\ N>0\,\Rightarrow\,18\mid 12^{4N}\!\Rightarrow 12^{4N}\!\bmod{90} \,=\,18\underbrace{\left(\dfrac{(12^{4})^N}{18}\bmod 5\right)}_{\equiv\ \dfrac{1^N}{3}\ \equiv\ \dfrac{6}3\equiv\ \Large\color{#c00} 2} =\, 18(\color{#c00}2)\ $
Comment: We used the [mod Distributive Law](http://math.stackexchange.com/a/2059937/242) $\ ca\bmod cn = c(a\bmod n)\ \ $
Answer: $90=2 \times 3^2 \times 5$, so let us divide the cases. First note $$12^{34^{56^{78}}} \equiv 0 \pmod {2}$$$$12^{34^{56^{78}}}=4^{34^{56^{78}}} \times 3^{34^{56^{78}}} \equiv 0 \pmod {3^2}$$
Also, since $56^{78}>2$, $34^{56^{78}} \equiv 0 \pmod {4}$. Let $34^{56^{78}} =4k$ where $k$ is a natural. So $$12^{34^{56^{78}}}=12^{4k}=(12^{4})^k \equiv 1^k \equiv 1 \pmod {5}$$
From Fermat's Little Theorem. Now use CRT. So we have that $$12^{34^{56^{78}}} \equiv 36 \pmod {90}$$
Comment: What about the order of power calculation??
Comment: @HamidRezaEbrahimi If you see my answer, you can see the order in which he calculate powers. First calculate $56^{78}$. Then calculate $34^{56^{78}}$. Then calculate $12^{34^{56^{78}}}$.
Comment: Start from the exponents downwards: $(12)^{34^{56^{78}}} = (12)^{34^{(2.28\cdot 10^{136})}} = ...$ etc
Comment: Isn't CRT helpful further, by this 3 must be remainer
Comment: @SubhashChandBhoria What do you mean?
Comment: @S.C.B. your solution seems fine just CRT will give further remainder as 3
Comment: @HamidRezaEbrahimi Do you understand?
Comment: @S.C.B. Yes, thanks.
Comment: I have already upvoted. Just waiting for a reasonable amount of time,don't hurry up
Comment: Why the downvote?
Comment: @S.C.B. Curiously, my answer was downvoted 37 seconds after you posted the above comment in this old, inactive thread. Explanation? $\ \ $
Comment: @BillDubuque I will upvote then.
Comment: @S.C.B. Thanks, but it is not so much votes that I care about but, rather,  understanding what might be perceived as deficient, so that I can improve the exposition.
Answer: As $34^{56^{78}}$ is divisible by $4,$ let us write this as $4(m+1)$
As $(12^n,90)=18$ for $n\ge2$ let us find $12^{4(n+1)-2}\pmod{90/18}$
i.e., $12^{4n+2}\pmod5$
Now as $12\equiv2\pmod5,12^2\equiv2^2\equiv-1$
$12^{4m+2}=(12^2)^{2m+1}\equiv(-1)^{2m+1}\equiv-1\pmod5$
$\implies12^{4(m+1)}\equiv12^2\cdot-1\pmod{12^2\cdot5}\equiv-144\pmod{90}$ as $90|12^2\cdot5$
Finally $-144\equiv36\pmod{90}$ as $12^{4(m+1)}>0$
Comment: See also : http://math.stackexchange.com/questions/1385396/last-two-digits-of-145532/1386230#1386230  OR http://math.stackexchange.com/questions/808747/what-is-the-remainder-when-241202-is-divided-by-1446/808751#808751   OR http://math.stackexchange.com/questions/1952810/finding-the-last-two-digits-of-5312442 OR   http://math.stackexchange.com/questions/1844558/how-to-find-last-two-digits-of-22016
Answer: Working through this slowly:
For the sequence $12^k \bmod 90$, we can see this will quickly settle onto multiples of $18$, since $12$ and $90$ share $2$ and $3$ as prime factors and $2\cdot 3^2$ is the largest factor of $90$ using these prime factors.
So the sequence $12^k \bmod 90$ looks like $\{12, \color{blue}{54}, 18, 36, 72, \color{blue}{54}, \ldots\}$ with a cycle length of $4$, and thus $12^{4i}\equiv 36 \bmod 90$ for $i\ge 1$.
Then for $j>1$, $4 \mid 34^j$ and thus $12^{34^{56^{78}}}\equiv 36 \bmod 90$
Comment: Nice and concise
Answer: My solution is rather routine and makes good use of Euler totient theorem and modulus properties.
$$\begin{align*}12^{34^{45^{78}}} \pmod{90} &\equiv 6\left[\frac{12^{34^{45^{78}}}}{6} \pmod{15} \right],\qquad\qquad\gcd(12,90)=6\\
&\equiv 18\left[\frac{12^{34^{45^{78}}}}{18} \pmod{5}\right],\qquad\qquad\gcd(12,15)=3\\
&\equiv 18\left[\frac{2^{34^{45^{78}}\pmod 4}}{3} \pmod{5}\right],\qquad\qquad\phi(5)=5-1=4\\
&\equiv 18\left[\frac{2^0}{3} \pmod{5}\right]\\
&\equiv 18\left[\frac{6}{3} \pmod{5}\right]\\
&\equiv 18\left[2\right]\equiv 36
\end{align*}$$


Least Squares Derivation

Question: I was reading this to review the derivation of the ordinary least squares estimator but I'm having trouble differentiating (4). Can someone please help explain why
$ \dfrac{\partial (\hat{\beta}'X'X\hat{\beta})}{\partial \hat{\beta}} = 2X'X\hat{\beta} $
I understand all the other steps.
Thanks!
Answer: First we need to note that $X'X$ is a symmetric matrix as $(X'X)'=X'(X')'=X'X$.
Now,Using basic matrix differentiation,we know that for symmetric matrix $A$,$\dfrac{\partial (x'Ax)}{\partial x} = 2Ax = 2x'A$ where $x$ is a vector and dimensions are proper.
hence result follows trivially.
Note:Actually we can say more,if $A$ is any $n \times n$ matrix, $x$ is $n \times 1$ vector,then,
$ \dfrac{\partial (x'Ax)}{\partial x} = x'(A'+A)$
Proof:$$ x'Ax = \sum_{j=1}^n \sum_{i=1}^n a_{ij}x_ix_j$$
Differentiating wrt to $x_k$,we get,
$$\dfrac{\partial (x'Ax)}{\partial x_k}= \sum_{j=1}^n a_{kj}x_j + \sum_{i=1}^n a_{ik}x_i\ ;  \forall k = 1,...,n$$
hence,$$\dfrac{\partial (x'Ax)}{\partial x} = x'A'+x'A$$
Hence result follows.
More details on Matrix differentiation can be found on http://en.wikipedia.org/wiki/Matrix_calculus .
Thanks.
Answer: Notice first that you might guess this formula because if $X$ and $\hat{\beta}$ are $1 \times 1$ then it reduces to the formula for the derivative of $x^2$ from calculus.
Let's derive a multivariable product rule that will help us here.  Suppose
$f:\mathbb{R}^n \to \mathbb{R}$ and suppose
$$f(x) = \langle g(x), h(x) \rangle$$ for some functions $g:\mathbb{R}^n \to \mathbb{R}^m$ and $h:\mathbb{R}^n \to \mathbb{R}^m$.  Then if $\Delta x \in \mathbb{R}^n$ is small (and $g$ and $h$ are differentiable at $x$), we have
$$\begin{align*}
f(x + \Delta x) &\approx \langle g(x) + g'(x) \Delta x, h(x) + h'(x) \Delta x \rangle \\
&= \langle g(x),h(x) \rangle + \langle g(x), h'(x) \Delta x \rangle
+ \langle g'(x) \Delta x, h(x) \rangle + \langle g'(x) \Delta x, h'(x) \Delta x \rangle \\
&\approx \langle g(x),h(x) \rangle + \langle g(x), h'(x) \Delta x \rangle
+\langle g'(x) \Delta x, h(x) \rangle \\
&= \langle g(x),h(x) \rangle + \langle h'(x)^T g(x), \Delta x \rangle
+ \langle g'(x)^T h(x), \Delta x \rangle \\
&= f(x) + \langle h'(x)^T g(x) + g'(x)^T h(x), \Delta x \rangle.
\end{align*}$$
Comparing this result with
$$f(x + \Delta x) \approx f(x) + \langle \nabla f(x), \Delta x \rangle$$
we discover that
$$\nabla f(x) = h'(x)^T g(x) + g'(x)^T h(x).$$
This is our product rule.  (I'm using the convention that the gradient is a column vector, which is not completely standard.)
Now let $g(x) = Ax$ for some matrix $A$.  So $g'(x) = A$.
What's the gradient of the function
$$\begin{align*}
f(x) &= \langle g(x),g(x) \rangle \\
&= \langle Ax, Ax \rangle \\
&= x^T A^T A x \quad \text{?}
\end{align*}$$
By our product rule the answer is
$$\begin{align*}
\nabla f(x) &= 2g'(x)^T g(x) \\
&= 2 A^T A x.
\end{align*}$$
This is the result that you wanted to derive.

Least Squares Derivation


special outfile mysql with string inserted?

Question: I have 3 columns in a mysql table like that
COL1       COL10      COL11
longblob1 longblob10  longblob11

I want to output everything with the following format :
 @COL1 COL10
 +
 COL11

(It's called a fastq file for lil' biochemist like me out there ..)
So I thought querying the output like that, but it doesn't go to next line it just prints out /n as a character ..  :
SELECT '@',COL1, COL10,'/n','+','/n',COL11 FROM MYTABLE
INTO OUTFILE '/MYPATH/MYFILE.TXT';

Answer: You are searching for string concatenation (additionaly, you are escaping wrong, it's \n, not /n):
SELECT CONCAT('@', COL1, COL10, '\n+\n', COL11) FROM MYTABLE
INTO OUTFILE '/MYPATH/MYFILE.TXT';

Learn more about CONCAT() here.
Answer: It should if you use the concat() function:
SELECT CONCAT('@',COL1, COL10,'\n','+','\n',COL11) FROM MYTABLE
INTO OUTFILE '/MYPATH/MYFILE.TXT';


Paginação com Eager Loading no Laravel

Question: Estou tendo um pequeno problema de ordenação envolvendo paginação e Eager Loading.
O que eu preciso fazer:

Em uma parte do meu site, eu preciso trazer resultados paginados de uma subcategoria
A subcategoria é filha de uma categoria, então eu tenho que exibir em uma tabela o pai que é a categoria, a subcategoria e os outros dados dela.
Estou usando Eager Loading para trazer a categoria

O problema é que eu preciso ordenar pelo nome da categoria, e depois pelo nome da subcategoria, e os dados não estão sendo ordenados corretamente.
Segue o código:
<code>// EloquentSubcategoria
public function byPage($page=1, $limit=10)
{
    $result = new \StdClass;
    $result->page = $page;
    $result->limit = $limit;
    $result->totalItems = 0;
    $result->items = array();

    $obj = $this->subcategoria->with(array('categoria' => function ($query)
            {
                $query->orderBy('categoria.cat_nome', 'ASC');
            }))
            ->orderBy('subcategoria.sbc_nome', 'ASC');

    $articles = $obj->skip( $limit * ($page-1) )
                    ->take($limit)
                    ->get();

    $result->totalItems = $this->totalSubcategorias();
    $result->items = $articles->all();

    return $result;
}

protected function totalSubcategorias()
{
    return $this->subcategoria->count();
}

// SubcategoriaController
public function index()
{
    // Valores inteiros definidos no app
    $page = Input::get('page', 1);
    $perPage = Config::get('app.perPage');

    // retorna dados para paginacao
    $pagiData = $this->subcategoria->byPage($page, $perPage);

    // paginar
    $subcategorias = Paginator::make($pagiData->items, $pagiData->totalItems, $perPage);

    return View::make('subcategorias.index')->with('subcategorias', $subcategorias);
}
</code>
Comment: Coloque sua solução como resposta e aceite ela. Assim fica organizado para ajudar outras pessoas que passaram pelo mesmo problema e você ainda pode ganhar reputação com isso.
Comment: O problema foi resolvido.

`$this->subcategoria->with('categoria')....`

Nos modelos, a subcategoria está com um relacionamento "belongsTo" Categoria.

Mas no meu caso, eu não posso usar o belongsTo, pois ele vai retornar apenas um.

Para resolver, eu utilizei o JOIN ao invés do Eager Loading

O código ficou o seguinte:

`$query = $this->subcategoria->join('categoria', 'cat_id', '=', 'sbc_cat_id')....`
Comment: Mas para um caso oposto, o código acima iria funcionar, só alterar de subcategorias para categorias: `$this->categoria->with(array('subcategoria' => function ($query)
            {
                $query->orderBy('subcategoria.sbc_nome', 'ASC');
            }))
            ->orderBy('categoria.cat_nome', 'ASC');`

Pois a relação e de 1-n entre Categorias e Subcategorias.
Me desculpem se eu não consegui ser mais claro, eu não sou muito bom para explicar as coisas xD.
Answer: No meu caso, eu tenho um relacionamento entre Categorias e Subcategorias
Categorias hasMany Subcategorias
Subcategorias belongsTo Categorias
Para trazer os resultados em ordem alfabética (Nome da categoria seguido de nome da subcategoria) da forma como está no meu código, não irá funcionar:
<code>$obj = $this->subcategoria->with(array('categoria' => function ($query)
{
    $query->orderBy('categoria.cat_nome', 'ASC');
}))
->orderBy('subcategoria.sbc_nome', 'ASC');
</code>
Pois a Subcategoria BelongsTo Categoria (isto retorna apenas um registro)
Mas se for feito ao contrário, irá funcionar
<code>$obj = $this->categoria->with(array('subcategoria' => function ($query)
{
    $query->orderBy('subcategoria.sbc_nome', 'ASC');
}))
->orderBy('categoria.cat_nome', 'ASC');
</code>
Porém, no final eu acabei optando por utilizar JOIN.
Pois no meu caso... na estrutura da minha aplicação, fazer como está escrito acima iria aumentar muito a quantidade de código que eu iria ter que adicionar, então a estrutura final que eu acabei utilizando é a seguinte:
<code>public function byPage($page=1, $limit=10)
{
    $result = new \StdClass;
    $result->page = $page;
    $result->limit = $limit;
    $result->totalItems = 0;
    $result->items = array();

    $query = $this->subcategoria
            ->select('cat_nome', 'sbc_id', 'sbc_nome')
            ->join('categoria', 'cat_id', '=', 'sbc_cat_id')
            ->orderBy('cat_nome', 'ASC')
            ->orderBy('sbc_nome', 'ASC');

    $articles = $query->skip( $limit * ($page-1) )
                    ->take($limit)
                    ->get();

    $result->totalItems = $this->totalSubcategorias();
    $result->items = $articles->all();

    return $result;
}
</code>

Paginação com Eager Loading no Laravel


Is this function well defined and continuous?

Question: I'm studying of a function $T:=T_1+T_2$. Here
$$
T_1(t)=\int_0^t \left([t(1-\tau)]^{\frac{1}{2}}-(t-\tau)^{\frac{1}{2}}\right)k(\tau)d\tau,
$$
$$
T_2(t)=t^{\frac{1}{2}} \int_t^1 (1-\tau)^{\frac{1}{2}} k(\tau)d\tau,$$
and $k$ is a non-negative continuous function on $(0,1]$ satisfying
$$\int_0^1 \tau ^{\frac{1}{2}} k(\tau)d\tau<\infty\label{1} \tag{1}.$$ For example, $k(t)= t^{-\frac{3}{4}}$.
I was wondering if the following assertions are true:
$(1)$ $T$ is well defined on $(0,1)$ and $$\lim_{t \to 0^+}T(t)=\lim_{t \to 1^-}T(t)=0.$$
$(2)$ $T$ is continuous on $[0,1].$
For $$T\left(\frac{1}{2}\right)=T_1\left(\frac{1}{2}\right)+T_2\left(\frac{1}{2}\right),$$ it is easily see that $T_2\left(\frac{1}{2}\right)$ is well defined, but it is not obvious that $T_1\left(\frac{1}{2}\right)$ is well defined. I don't know how to use the assumption \eqref{1}. Also, I think, by using Lebesgue dominated convergence theorem, $$\lim_{t \to 0^+}T(t)=\lim_{t \to 1^-}T(t)=0$$ can be proved, but I couldn't.
I would be grateful if you could give any comments on my questions.
Answer: Obviously $T_2$ is well defined and continous in $t$. Also if we replace $T_1$ by $T_1^a$ where we replace the lower integration bound by some $a>0$ then $T_1^a$ is also well defined and continuous in $t$. (This follows from the continuity of all occuring functions on $[a,1]$, as a continuous functions on $[a,b]$ is bounded and thus the integral is finite, and we get continuity quite trivially.)
Thus we need to study the integral close to $0$. We know from $\int_0^1 \tau^{1/2}k(\tau)\,d\tau<\infty$ that $\int_0^a \tau^{1/2}k(\tau)\,d\tau\xrightarrow{a\to 0}0$.
So let's study $B(t,\tau)=[t(1-\tau)]^{1/2}-(t-\tau)^{1/2} = (t-t\tau)^{1/2} - (t-\tau)^{1/2}$ for $\tau$ close to $0$. At $\tau=0$ this is $0$. Furthermore the derivative to the square of that expression is
$$ -\frac{{\left((t - \tau)^{1/2} t - (t-t\tau)^{1/2}\right)} {\left((t-t\tau)^{1/2} - (t - \tau)^{1/2}\right)}}{((t-t\tau)(t - \tau))^{1/2}} =: D(t,\tau) $$
If $\tau$ is small the denominator is close to $t$, and the numerator is close to $(t^{3/2}-t^{1/2})0=0$. Thus the derivative around $0$ is close to $0$, and at the very least bounded (so $|D(t,\tau)|<C(t)$ for $\tau$ close to $0$).
This means that around $0$ we get $((t-t\tau)^{1/2}-(t-\tau)^{1/2})^2<C(t)\tau$ and thus $(t-t\tau)^{1/2}-(t-\tau)^{1/2}<\sqrt{C(t)}\tau^{1/2}$.
We can then even get this constant independent of $t$: For each $t$ $D(t,\tau)$ is monotonic in $\tau$ for $\tau < t$, as even the numerator is a product of montonic functions and the denominator is falling:
The first factor $t(t-\tau)^{1/2} - (t-t\tau)^{1/2}$ is falling (and thus by incorporating the $-$ sign increasing) as it's derivative is
$$ -\frac{t {\left((t-t\tau)^{1/2} - (t - \tau)^{1/2}\right)}}{2 \, \left((t-t\tau) (t - \tau)\right)^{1/2}}$$
which is negative, as for $0<t<1$ and $0< \tau\leq t$ we have
$ t-t\tau > t-\tau $ (and in the cases $t=1,\tau=0$ we have equality).
The second factor is simply $B(t,\tau)$. As clearly $D(t,\tau)\geq 0$ we have $B^2(t,\tau)$ is increasing in $\tau$, and as $B(t,\tau)\geq0$ we thus also get $B(t,\tau)$ is increasing in $\tau$.
In the denominator we have $(t-t\tau)$ and $(t-\tau)$, both of which are decreasing in $\tau$. Thus $D$ is a product of nonnegative, increasing factors. Thus $D$ is increasing in $\tau$.
But this then implies that for each $t$ we have $B^2(t,\tau)$ is convex in $\tau$. This means that $B^2(t,\tau)$ lies under the line that is $B^2(t,0)=0$ for $\tau = 0$ and $B^2(t,t)=t(1-t) = t-t^2$.
Then for $0\leq t\leq 1$ we have $0\leq t-t^2\leq t$. So we get: For each $t$ we have $B^2(t,\tau)<\tau$ for $0\leq\tau\leq t$.
Then we get
$$ T_1 \leq \int_0^t \tau^{1/2}k(\tau)\,d\tau$$
This implies that $T_1$ is well defined. Also as the integrand of $T_1$ is bounded on $[0,1]$ by $\tau^{1/2}k(\tau)$ it is continuous in $t$ by dominated convergence.
Also this gives us that $T_1(t)\xrightarrow{t\to0}0$. So take a look at $T_2(t)$. Note that for $t<b$ we get
$$ \int_0^b \tau^{1/2}k(\tau)\,d\tau\geq \int_t^b \tau^{1/2}k(\tau)\,d\tau \geq t^{1/2}\int_t^b k(\tau)\,d\tau $$
Take $b$ small enough so that the left side is smaller than some $\epsilon$. Then for $t<b$ we have
$$ T_2(t) = t^{1/2}\int_b^1 \ldots + t^{1/2}\int_t^b \ldots \leq t^{1/2}\int_b^1 \ldots + \epsilon$$
As the first part goes to $0$ we get
$$ 0\leq \liminf_{t\to0}T_2(t)\leq \limsup_{t\to0} T_2(t)\leq \epsilon $$
With $epsilon\to 0$ you get $\lim_{t\to0}T_2(t)=0$.
Finally
$$\lim_{t\to1} T(t) = T(1) = \int_0^1 ((1-\tau)^{1/2}-(1-\tau)^{1/2})k(\tau)\,d\tau = 0$$
Comment: Thanks for your answer. I think your answer is good enough. However, I don't understand the fact that $C(t) $ is independent of $t$, i.e., $C=\sqrt{2}-1$. Please give the proof more clearly.
Comment: @04170706 I've updated the answer to incorporate these details.
Comment: It seems that the proof is almost done. Thanks a lot.  However, $R_a(t) \to 0$  as $t \to 0$ is okay, but  $T_1^{a(t)} \to 0$ as $t \to 0$ is not trivial, since $a(t) \to 0$ as $t \to 0$. Also, the first part of your answer is mentioning for fixed constant $a$. Finally, I'd like to ask you to denote the set of $t$ explicitly. i.e., either $t \in (0,1]$ or $t \in (0,1).$
Comment: @04170706 I've updated the improvements to the answer.
Comment: I doubt if $B^2(t,\tau)$ is convex in $\tau$, i.e. $D(t,\tau)$ is increasing in $\tau$. Please give the reason of it in detail. If only that part is proved, the proof seems to have been completed.
Comment: I've provided these details to the proof.
Comment: Thanks a lot. With your answer, I learned a lot about the function $T$. I posted another question about the complete continuity of $T$ (see https://math.stackexchange.com/questions/4305540/is-this-function-absolutely-continuous ). If you have any idea for the question,  I would appreciate it if you could let me know.
Answer: For the specific case where $k(t)= t^{-\frac{3}{4}}$
The two integrals do not present  much problems
$$T_1=-\frac{8 \, _2F_1\left(-\frac{3}{4},\frac{1}{2};\frac{1}{4};t\right)}{3 t^{1/4}}+\frac{4}{3}
  t^{3/4} \sqrt{1-t} -\frac{2 \sqrt{\pi } \, \Gamma \left(\frac{5}{4}\right)}{\Gamma
   \left(\frac{7}{4}\right)}t^{3/4}+\frac{8 \sqrt{1-t}}{3 t^{1/4}}$$
$$T_2=\sqrt{t} \left(\frac{2 \sqrt{\pi } \,\Gamma \left(\frac{1}{4}\right)}{3 \Gamma
   \left(\frac{3}{4}\right)}-\frac{2}{3} \left(2 t^{1/4}\sqrt{1-t}
   +B_t\left(\frac{1}{4},\frac{1}{2}\right)\right)\right)$$ and the surprise
$$T=T_1+T_2=\frac{2 \sqrt{\pi } \, \Gamma \left(\frac{1}{4}\right)}{3 \Gamma
   \left(\frac{3}{4}\right)}\left(1-t^{1/4}\right) \sqrt{t}$$  So, assertions $(1)$ and $(2)$ are true.
No problem anywhere for $0 \leq t \leq 1$.
Comment: Thanks for your answer. What is $_2F_1$?
Comment: @04170706. The gaussian hypergeometric function. If you want, I can try with another function $k(t)$ of your choice.


PyLint: Using possibly undefined loop variable warning

Question: Having read this question I can understand why this warning might be output, but I have a specific case when the loop variable can not be undefined...
for i in range(0, 2):
    print i

print i

PyLinting the above I get
 W:  4,6: Using possibly undefined loop variable 'i'

Is PyLint not clever enough to note that the built in range() function will always produce a populated list in this case and so i will always be set for the last print statement? I can understand if it was a under-defined function, because PyLint could not possibly know what the function does... but in this case it is a well known function and surely this usage would be common?
If so, is it better to supress the warning or define i before the loop (which seems wasteful)?
Comment: What if you redefined `range` somewhere in your code?
Comment: Good point, thanks for replying
Answer: This is because your range might be empty depending on the intermediate execution steps involved, or it may be have redefined within the code as pointed by @tobias_k (though that will throw an additional W:  1, 0: Redefining built-in 'range' (redefined-builtin)), and hence, variable i may not be defined.
Note that pylint doesn't at all execute code, it just does static analysis, so it won't check the value within the range. So when it sees a variable defined within a for block being re-used possibly out of scope, it complains.
Consider the examples below, where the same Warning is shown by pylint but one runs, while other throws an exception:
W:  4, 6: Using possibly undefined loop variable 'i' (undefined-loop-variable)

Example (Not Working):
$ cat test.py
for i in range(0):
        print i
print i

$ python test.py
Traceback (most recent call last):
  File "test.py", line 4, in <module>
    print i
NameError: name 'i' is not defined

Example (Working):
$ cat test.py
for i in range(0, 2):
        print i
print i

$ python test.py
0
1
1

As the answer to your question, is it better to suppress the warning, my answer would be no. If you do want to suppress it, make sure you re-enable it after the code block in question.
Comment: Okay, thanks, so it is because PyLint doesn't inspect the `range` function (I understand it *could* return an empty list - in my particular case it does not) or other built-in functions to figure it out. Thanks.


(VBA) I am looking to automate the update of my inventory form when I activate a Macro to add the count from one sheet to another

Question: I am working on an Inventory workbook that has the inventory in one sheet and another sheet where I can scan barcode into and activate a macro to update the inventory sheet.
The goal is to read each cell in a column that has a value and then find the matching value in the other sheet and update the count by using the sum of the corresponding count values.
The VBA code I have so far updates the first item on the list, but just continues to update it endlessly. I am not sure what the best approach is and looking for a better route to update it in an efficient manner.
This is what I have so far
`Sub Inventory_Update()
Dim i As Integer
Dim b As Integer

i = 2
Do While Cells(i, "D").Value <> ""
 If Cells(i, "D").Value <> "" Then
  b = 1
  Do While b < 346
  If Sheet1.Cells(b, "B").Value = Cells(i, "D").Value Then
  Sheet1.Cells(b, "C").Value = Sheet1.Cells(b, "C").Value + Cells(i, "F").Value
  Else
  b = b + 1
  Loop
  i = i + 1
  End If
  Loop

End Sub
`
Answer: Like this - a For Next loop is better for b
Sub Inventory_Update()
    Dim i As Long, b As Long, wsInv As Worksheet, wsEntry As Worksheet

    Set wsEntry = ThisWorkbook.Worksheets("Entry") 'for example
    Set wsInv = ThisWorkbook.Worksheets("Inventory") 'or Sheet1

    i = 2
    'use a worksheet reference whenver you reference a range
    Do While Len(wsEntry.Cells(i, "D").Value) > 0
        For b = 1 To 346
            If wsInv.Cells(b, "B") = wsEntry.Cells(i, "D").Value Then
                With wsInv.Cells(b, "C")
                    .Value = .Value + wsEntry.Cells(i, "F").Value
                End With
            End If
        Next b
        i = i + 1
    Loop
End Sub

Comment: Thank you for your answer! I used your formula and added my worksheet info. I get a "loop without do" error. I attempted to add an 'Exit For', but that did not help. I even attempted to see what would happened if I removed one of the loops and got the same error.
Comment: I figured it out. I was missing the 'Next' statement.  Thank you again!!
Comment: Sorry that was my typo - fixed above


How can I speed up a site that is slow because a huge locales_source table?

Question: I'm working on a site that slows down to an unbearable crawl when you view it in a different language (specifically "zh-hans"). When looking at the Devel query log, this appears to be because this query takes about 5 minutes.
<code>SELECT s.source, t.translation, t.language
FROM locales_source s
LEFT JOIN locales_target t
  ON s.lid = t.lid
  AND t.language = 'zh-hans'
WHERE s.textgroup = 'default'
  AND s.version = '6.25'
  AND LENGTH(s.source) < 75
</code>
The reason it takes so long is that the locales_source table has almost 1 million rows, even though there are only 1 thousand nodes on the site. This seems like an extremely large amount to me. So my questions are:

Is this normal?
If not, how can I go about identifying which of the data in locales_source is not applicable or bad? I can't find the way to map it back to the original source (node body, title, menu item, etc.) so I'm not sure how to determine if the original source exists and is valid.

The locales_source table looks like this, if it helps.

Field
Type
Null
Key
Default
Extra

lid
int(11)
NO
PRI
NULL
auto_increment

location
varchar(255)
NO

textgroup
varchar(255)
NO
MUL
default

source
blob
NO
MUL
NULL

version
varchar(20)
NO

none
Answer: Yes, 1 million is an extremely high amount. That's not normal.
The amount of nodes should have nothing to do with that table, because content translations happens by creating separate nodes for each language that are linked together.
What is added to that table are all strings which are passed through t(), and everything that is registered through the various i18n modules (menus, terms, ...).
t() must not be used for user generated content but this is often done and I suspect that might be responsible for your amount of rows in that table. The problem with something like t($term->name) is that the identifier is the term name. If you change your original term name, then your translations are lost and the source entry will stay there forever. See below on how to identify problems here.
The interesting column to check where something is coming from is the location, there are 3 different possible types of values in there:

When imported from a .po file (e.g. official translations from localize.drupal.org), the location is filename:line. These are most likely ok and should be much much less than 1 million.
Translations managed by i18n_string have some sort of context/identifier in there, that looks like this: taxonomy:term:1:name. That allows to look up a translation based on a unique identifier of something that won't change. There could be many of these, if you have many taxonomy terms/huge menus but again, not 1 million (except you have 1 million or close terms). You can also identify them by the text group, case 1 and 3 is both textgroup default, while this has a textgroup for each source type (taxonomy, menus, ...).
Every string passed to t() that is not yet in that table is automatically added. In that case, the location is the URL of the page where this string first occured. Those will probably be the most interesting ones for you.

What you need to find out is which pages are adding these dynamic translations and then track down the exact module/call and fix it. There two ways to start. First, look at the most recent additions to that table to see what URL's you have in there (last page on the translate overview, or you can look directly in the database). The second option is group by location and then sort by count(), which might forever on a table of that size. But it's likely that a large amount of sources have been added on the same page. Once you know the page, it's time to do some profiling there and track down who exactly is calling t() often.
Two contrib module examples that I recently identified while doing a performance review on a site:

The site had two hierarchy/relation select boxes using hierarchical select, which currently falls back to t($term->name) if i18n_taxonomy is not installed. That was a free tagging vocabulary with 10k terms and this resulted in 40k calls to t(), which obviously also makes it extremely slow. This issue is still there, I need to find the time to provide a patch.
The other case was the print module, which "translated" the node title on the print page.  I think this was recently fixed, but the already added sources will stay in that table.

Once you identified the problem, you need to fix it and then remove the existing strings from that table, e.g with a like 'taxonomy/term/edit/%' condition for the term edit pages. Just removing the strings can be useful to quickly check how it affects the site load time but they will get re-added as soon as the problematic code is executed again.
For the record, the site that I looked into had "only" 40k entries in that table but even that resulted in cache entry of the short translations of 10MB (in PHP, not serialized).
Comment: Awesome, awesome reply. I found a `t()` call around $block->content in the legacy theme's block.tpl.php so it was apparently adding a new row every time a block was displayed. Removing that `t()` and deleting all the rows from locales_source that matched the pattern of block markup fixed it right up. Thanks so much.


Does this property characterize monomorphisms?

Question: Is requiring that $f:\mathrm{Hom}(A,B)$ is mono the same as requiring that the pullback $A\times_BA$ of $f$ along itself is isomorphic to $A$?
In Sheaves in Geometry and Logic, I read the following definition: $f$ being mono means that the projections out of $A\times_BA$ to $A$ (kernel pair) are both the identity. But I guess that can be weakened to saying they should be isos and the same up to iso. However, I can't quite find what "up to isomorphism" formally means for arrows (as opposed to objects, where it means they are connected by an invertible arrow). It might be that them being isos with the same codomain already means that are "the same up to iso" - could anyone clarify?
Answer: The following are equivalent:

$f : A \to B$ is a monomorphism.
The diagram below is a pullback square:
$$\require{AMScd}
\begin{CD}
A @>{\mathrm{id}}>> A \\
@V{\mathrm{id}}VV @VV{f}V \\
A @>>{f}> B
\end{CD}$$
There exists a pullback diagram of the form below,
$$\require{AMScd}
\begin{CD}
K @>{k_0}>> A \\
@V{k_1}VV @VV{f}V \\
A @>>{f}> B
\end{CD}$$
where at least one of $k_0$ and $k_1$ is an isomorphism.
There exists a pullback diagram of the form below:
$$\require{AMScd}
\begin{CD}
K @>{k}>> A \\
@V{k}VV @VV{f}V \\
A @>>{f}> B
\end{CD}$$

I leave the proof as an exercise for you.


Attach a simple custom handler to a Webform

Question: I'm trying to create a custom webform handler. I'd like to be able to attach my handler to any webform and have it display the message 'xyz' when the (multi step wizard) form is finally submitted. I have this file in my custom module at:  my_module/src/Plugin/WebformHandler/PostWebformHandler.php.
<?php

namespace Drupal\test_b\Plugin\WebformHandler;

use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Webform test b handler.
 *
 * @WebformHandler(
 *   id = "test_b",
 *   label = @Translation("Test B"),
 *   category = @Translation("Test B"),
 *   description = @Translation("Test B webform submission handler."),
 *   cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
 *   results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
 *   submission = \Drupal\webform\Plugin\WebformHandlerInterface::SUBMISSION_OPTIONAL,
 * )
 */
class TestBWebformHandler extends WebformHandlerBase {

  /**
   * {@inheritdoc}
   */
  public function preSave(WebformSubmissionInterface $webform_submission) {
    \Drupal::messenger()->addMessage(t('xyz'));
  }

}

On navigating to my Webform settings, I see:

but when I click Add handler for Test B it does not attach to my form handlers?
Comment: The file and class names don't match (they need to)
Answer: The above code works. My mistake was in the naming of things. Name all files, classes etc. foo and it will attach.


External validation of clustering requires labels, but why cluster at all if you have labels?

Question: There are two types of validation in clustering, using:

Internal indexes: Used to measure the goodness of a clustering  structure  without respect to external information (e.g., sum of squared errors)
External indexes: Consists in comparing the results of a cluster analysis to an externally known result, such as externally provided class labels (e.g., Rand index, purity, etc.)

I'm confused on the use of external validation indexes in clustering. Since the class labels are known, why use clustering (i.e., unsupervised learning) instead of supervised learning (e.g., SVM, etc.)?
Comment: The external indices are used also to compare two partitions in general case, i.e. when there is no "true" one among the two. And, of course, as said in the answers below, they are used to test the quality of results of a clustering or of a classification algorithm.
Comment: What ttnphns said is that this is *not* the *only* use case for these measures. You can also compute, e.g., stability of a method by evaluating the similarity between different unsupervised results. The "evaluation" use case is indeed academic and not applicable in many real scenarios. But stability is.
Comment: thank you @ttnphns but what do you mean by "TRUE" partition ? because the way I understood it, external validation it's when you validate a partition (result of your clustering) by comparing it with the "correct"/"true" partition (externally provided class labels)
Comment: By "true" I mean the same partition as you just did
Answer: External validity indices are used when you propose a new clustering technique and you want to validate it or you want to compare it to existing techniques. In these cases, you get a bunch of datasets for which you know the ground truth and see if your clustering technique is able to produce clustering solutions that are similar to it.
Comment: Class labels are only a specific way to cluster your data. You might want your clustering algorithm to reproduce that view, or you might not. If you do, you can use external validity indices.

There are also other possible views of a data set. Have a look at alternative clustering and multiview clustering.
Comment: Thank you @Simone for your help. I am reassured to know that there are no rule that says "if you have labels you must use supervised learning and not clustering"
Comment: Yes, if you have labels you can still use clustering techniques to find other views of the data.
Comment: Only in this case ? so it's always frowned upon to use clustering when we have class labels? Why can't we not argue that clustering algorithms are less complex since they use notions like distance, mean ,median etc; whereas supervised learning you have to adjust complex parameters and it's time consuming and so on....
Answer: Clustering is generally done for data which has no labels.
The Validation method you can use depends on the data and for the problem for which you are using for.
External indexes:- Can be used when your Clustering model will create a valid classes and you are able to make out the classes and hand label the data.
Internal indexes:- when the data is unknown and can not be labeled you can use this approach for validation.
Supervised Approach:- If you have data which is already labeled then of-course you can use supervised approaches.
Answer: Data is just a bunch of measurements. What constitutes the "ground truth" and "external labels" is determined by people.
Take this data for example:

First image is of a wild dog, then some measurements of its characteristics
Second image is of a wild cat, then some measurements of its characteristics
Third image is of a domesticated cat, then some measurements of its characteristics

If you want to perform a supervised learning using you could choose to take wild vs domesticated as ground truth labels. Or you could also choose to take "dog vs cat" as ground truth labels. They are just labels.
If you don't necessarily want to train a machine learning model, but want to find out if there are some natural groupings, you might do clustering. Now, if you want to check if the grouping that "naturally" arose is animal species, you could check against "an outside label" "dog vs cat". Similarly, you could choose to use "wild vs domesticated".
I argue that labels are often made up notions in some contexts. Take the hypothetical image of a wild dog, for example, in an image classification context. There could be a mountain in the background of the wild dog, and, in a completely different image classification problem, the "ground truth label" for the same image could be "mountain" and not "ocean".


Can you disable automatic mdadm startup?

Question: I am creating a RAID 10 array over four iSCSI targets.  I want to control everything manually though so I can run it through heartbeat.  I've unlinked the open-iscsi and mdadm scripts from /etc/rc#.d/ but the raid array is still recreated on boot up.  Once the server boots up I have to do a mdadm --stop /dev/md0 and then /etc/init.d/mdadm stop to make sure it doesn't fire it up again.  I commented out my array from /etc/mdadm/mdadm.conf but it just created a new one later.  How can I put mdadm into a manual process so it only attempts to start or rebuild the array when I tell it to?
Answer: From a RHEL 6.7 setup adding AUTO -all in /etc/mdadm.conf to disable all auto-assembly, except for ARRAYs/DEVICEs otherwise specified, seems to do the trick.
Answer: The only solution that worked for me was (see man mdadm.conf):
# /etc/mdadm/mdadm.conf
ARRAY <ignore> UUID=xxxxxxxx:xxxxxxxx:xxxxxxxx:xxxxxxxx

Answer: /etc/mdadm/mdadm.conf
   # by default, scan all partitions (/proc/partitions) for MD superblocks.
   # alternatively, specify devices to scan, using wildcards if desired.
  DEVICE partitions

what about scan something like /dev/null ? I mean some devices without superblocks. So mdadm cant find arrays.
Comment: DEVICE /dev/null does not work - for anyone who reads this later, don't bother, mdadm will still auto-assemble
Answer: You don't mention what flavour of Linux you are using (I'm assuming Linux?). You can control startup behaviour on debian/ubuntu with
dpkg-reconfigure mdadm

Comment: Ubuntu 11.04.  I tried that and the only startup it asks about is the monitor which I had already turned off.
Answer: let me share my solution, just uninstall mdadm from your system. the uninstall script will auto trigger hook for update ramfs.

apt remove mdadm
Comment: The question asked specifically how to disable *automatic* array startup, and the intention was to be able to start it manually, which your suggestion will preclude.


How to add a node for failover in Elasticsearch

Question: I currently have single node for elasticsearch in a windows server. Can you please explain how to add one extra node for failover in different machine? I also wonder how two nodes can be kept identical using NEST.
Answer: Usually, you don't run a failover node, but run a cluster of nodes to provide High Availability.
A minimum topology of 3 master eligible nodes with minimum_master_nodes set to 2 and a sharding strategy that distributes primary and replica shards over nodes to provide data redundancy is the minimum viable topology I'd consider running in production.


Select from Select MSSQL

Question: <code>select count (*)
  from

  (SELECT
count (c.ID_CENTRAL)
FROM dbCENTRAL c
LEFT JOIN GRUPO_CLIENTE GRUPO ON GRUPO.CD_GRUPO_CLIENTE = C.CD_GRUPO_CLIENTE
WHERE 1=1
AND C.FG_ATIVO = 1
AND C.CTRL_CENTRAL = 1
GROUP BY C.ID_CENTRAL,GRUPO.NM_DESCRICAO) as r
</code>
Bem no código acima estou tentando contar quantos ids de clientes eu tenho ativo, porem, por algumas particularidades do sistema, preciso usar o grupo by para trazer o resultado correto
Pensei em utilizar a forma que esta no código, porem o SSMS acusa que tem um erro na consulta e não consegui achar o que pode ser este erro. Se não estiver correta qual o jeito certo para fazer? Obrigado =)
Print Screen do Codigo com o Erro
Comment: Parece provável que seja possível algo diferente nessa consulta, mas especificamente sobre o erro (acho que faltou postar a mensagem de erro) experimente adicionar aliases para os 2 Count
Comment: Ola Imex, realmente esqueci a print do erro, vou tentar colocar os Aliases e ver se da certo, ai posto o erro tbm =).. obrigado
Comment: Imex, obrigado cara, realmente era so os ALIES, obrigado de verdade...
Answer: Veja se dessa formas resolve, vc vai ter a qtde por Central;
SELECT C.CTRL_CENTRAL, count(c.ID_CENTRAL) AS QTDE
FROM dbCENTRAL c
LEFT JOIN GRUPO_CLIENTE GRUPO ON GRUPO.CD_GRUPO_CLIENTE = C.CD_GRUPO_CLIENTE
WHERE C.FG_ATIVO = 1
GROUP BY C.CTRL_CENTRAL
Comment: W.V, não deu certo....não trouxe a informação que eu precisava ainda, mas a info que trouxe vai me ajudar em outro lugar kkkkkkk...
Comment: Que bom, que ajudou!!! top

Select from Select MSSQL