How to create a tick list which one role can alter for 'lesser' roles to see?

Question: I would like some pointers as to what might be the best approach to this issue please?
An admin role needs to be able to interact with a list of nodes for a fitness site. They need to be able to show/hide these nodes in a list for 'lesser' roles.
In addition to this the admin needs to be able to mark the node as complete so that the 'lesser' role can see this update. ie
Admin sees options to amend node list:
Lose 10 lbs - Show - Mark as complete
Attend 2 sessions - Hide - Not marked as complete
Run 3 miles this week - Show - Not marked as complete
'Lesser' role sees:
Lose 10 lbs - Complete.
Run 3 miles this week - Incomplete.
Obviously, I would like to be able to extend this list so that each user is able to see a list that is unique to them, which the admin role has interacted with to show/hide and complete/incomplete different sections.
I'm puzzled as to what approach I ought to take and would really welcome some suggestions to get me started.
Thanks.
Answer: From the interface that you have outlined, I could see that the 'Flag' module might be really nice for the administrator and user to interact with this kind of list.  In a list of nodes, flags would allow in place editing, changing the status, without going into each individual node.
You could configure the names of flags, status, and transition.  It would also be possible to assign permissions as to who has permission to change flags between states, and to allow users to change some of the flags themselves.
http://drupal.org/project/flag
To restrict what is seen by the users in the lists, perhaps you could build two similar views blocks: one showing all of the nodes (for administrator), and the other filtered only to show those with flag set to 'show' (for normal users).
The options in each view can be configured to selectively shown each block only to the appropriate role, and then both blocks could be embedded at the appropriate place.
Please note, that is not really enforcing permission to view the hidden nodes, and that a user could find them if they really try hard. It's just being selective about what is shown in the lists.
I was not able to quickly spot an existing module in drupal.org which would set node access by flag status.  You may have some luck with some further searching.
However  you can use 'Rules' to take actions on flag changes.  I think you might be able to pull something together by setting up a series of rules which then assign individual node permissions using 'content access' or another node access module of your choice when the show/hide flag changes state.
Otherwise you could use node_api and write your module to enforce your particular access rules  based on flag status.
Comment: Thanks for taking the time to reply.

I'm really unfamiliar with the flags module - and Drupal as a whole if the truth be told, but I have a further question if that is okay?

I want to be able to 're-use' the nodes that each user is going to be looking at. What I mean is that I intend to set up 20 nodes with different exercises that a customer might perform. The site admin would then flag the hide/show AND complete/incomplete against these nodes for EVERY user of the site.

Do you think that this is possible? Might using a taxonomy be good for setting up the exercises?

Thanks again.
Comment: It would be possible to use a taxonomy to manage a set of predefined tasks, and then to attach them to a particular person's plan by having a field which is a selection list.
Comment: However this becomes difficult when you want to attach more complex information to each target. Although there is some support for field grouping which might do this, I think it would seem likely that the number of associated bits of information with each users' target might grow, as the application develops - eg completed status, start date, trainer, etc.  I think it would be simpler to take each person's targets as being separate nodes, and allow you to expand later.  You may find it useful to take a look at "automatic node title" and "node clone" to make it easier to create new targets.


Use "where" in group expressions in sql

Question: I need make such request: choose list of cities which include letter i and have appropriate number of employees. Also I need take to attention that some city could have 0 employee and that for some employee field DEBT could be null.
Table depart
debt    city
-------------
43      odesa
23      kiev
79      lviv
78      lviv
12      rivne

Table empl
ide     fn      ln      debt
----------------------------
3421    jed     trt     43
354     jed     res     43
43      ged     hjkhg   79
73      ghghg   gfgf    79
456     jkl     gdfg
532     kkhg    vjv     23
45      ki      vt
243     ki      vt      78

I wrote this query:
select depart.CITY, count (*) as numb
from depart
   inner join empl on  empl.DEBT=depart.DEBT
where depart.CITY like '%i%'
group by depart.CITY;

But I do not know how take care about that some city could have 0 employee (for example this request does not show city rivne which have 0 employee) and that for some employee field DEBT could be null.
I use oracle with toad.
Expected results
city    numb
kiev    1
rivne   0
lviv    3

Answer: Do this:
select depart.CITY, count (*) as numb
from depart inner join empl on empl.DEBT=depart.DEBT
where depart.CITY like '%i%'
group by depart.CITY;

WHERE comes first before GROUP BY
Comment: @lamwaiman1988 one hour ago, it's reversed. GROUP BY, then WHERE. see the OP's edit
Answer: WHERE clause comes before GROUP BY clause.
Like this:
SELECT depart.CITY, COUNT (*) AS numb
FROM depart INNER JOIN empl ON empl.DEBT=depart.DEBT
WHERE depart.CITY LIKE '%i%'
GROUP BY depart.CITY;

See this fiddle for WHERE clause.
Or you can also use HAVING clause
Like this:
SELECT depart.CITY, COUNT (*) AS numb
FROM depart INNER JOIN empl ON empl.DEBT=depart.DEBT
GROUP BY depart.CITY
HAVING depart.CITY LIKE '%i%';

See this fiddle for HAVING clause.
It is more likely to use HAVING clause.
See this link for more details
Answer: To take care of criterion move where before group by. To allow for non-existing employees or null DEBT, use left outer join.
select depart.CITY, count (empl.DEBT) as numb
  from depart
  left join empl
    on empl.DEBT = depart.DEBT
 where depart.CITY like '%i%'
 group by depart.CITY;

To get all employees grouped by department you would reverse the outer join:
select depart.CITY, count (empl.ide) as numb
  from empl
  left join depart
    on empl.DEBT = depart.DEBT
 -- Note: condition is now part of a join. This is required
 --       as part of outer join because otherwise left hand
 --       table row would be filtered out.
   and depart.CITY like '%i%'
 group by depart.CITY;

(Presumably ide is primary key of employee).
But this would not return departments without employees, and CITY would be null where department's city would not match %i% or empl.DEBT would be null from the start.
To solve the problem, one might extend first query with union all designed to retrieve employees without departments. But there is a question: do we want only departmentless employees, or we consider or employees not working in a city whose name contains i departmentless also. I've opted for second possibility.
select depart.CITY, count (empl.DEBT) as numb
  from depart
  left join empl
    on empl.DEBT = depart.DEBT
 where depart.CITY like '%i%'
 group by depart.CITY;
select '(unknown or unmatched city)', count (*) as numb
  from empl
  left join depart
    on empl.DEBT = depart.DEBT
   and depart.CITY like '%i%'
 where depart.DEBT is null;

If you need distinction between employees without departments and those who work in a city not containing i, you might use case:
select case when depart.CITY like '%i%'
            then depart.CITY
            when depart.DEBT is null
            then '(No department)'
            else '(City does not match %i%)'
        end as CITY,
       count (*) as numb
  from empl
  left join depart
    on empl.DEBT = depart.DEBT
 group by
       case when depart.CITY like '%i%'
            then depart.CITY
            when depart.DEBT is null
            then '(No department)'
            else '(City does not match %i%)'
        end

This query will count employees from matching cities, unmatching cities and those who have no department.
Comment: after such request I get rivne into list of cities, but numb is 1, should be 0.
Comment: @khris Please check my answer again. I've changed count(*) part.
Comment: how modify request if i want display also quantity of employee which have no department ( like 45 in empl)?
Comment: @khris Please refresh the page. There is another version of my answer.
Comment: second select gives me record where are 8 emlpoyyes, must be only 1, maybe I should write smtg like "where empl.Debt=null"
Comment: Also I'm interested if it is possible to done all this in one request like, in field city show line unknown and where store quantity of people which have no department?
Comment: this script do not show rivne where quantity of employee is 0, and displays city which does not match %i%, it shouldn't by the condition
Comment: @khris Then I didn't understand your previous comment. Could you please extend your question with sample data and expected output?
Comment: sorry, I already understand, i need change join (select from department) and change count (empl.DEBT instead of *)
Comment: the only thing is, is it possible do not display such line  "else '(City does not match %i%)'", if I take off it from script, line with quantity of employees (for which city doesn't much %i%) and empty value in City is shown.
Comment: @khris To stop this chase I'd still very much like you to show me a table with expected results from data you posted in question.
Comment: @khris Why are we talking about employees who do not have a department? I don't see them in results, so the first query should answer that question.

Use "where" in group expressions in sql


Is $L_1 = \{w ∈ {0,1}∗ | \text{w has at least as many occurrences of (110)'s as (011)'s}\}$ regular?

Question: Let $L_1 = \{w ∈ \{0,1\}^∗ | \text{w has at least as many occurrences
                                  of (110)'s as (011)'s}\}$.
Let
$L_2=\{w ∈ \{0,1\}^∗ | \text{ w has at least as many occurrences
                                 of (000)'s as (111)'s}\}$.
Which one of the following is TRUE?

$L_1$ is regular but not $L_2$
$L_2$ is regular but not $L_1$
Both $L_2$ and $L_1$ are regular
Neither $L_1$ nor $L_2$ are regular

My attempt :
We cannot have two $110$'s in a string without a $011$ or vice verse.
let us consider the string $011011011011$
In this string, number of occurrences of $011$ are $4$ but when we see here $110$ is also occurred and the number of occurrence of $110$ is $3$. Yes, if a binary string starts with $11$, it can never have more no. of $011$ than $110$.

So we can observe here that whenever $110$ will be there string will be accepted
So with this idea we can build an automata for this. Therefore, it is regular.
But, L2 is not regular.(I guessed).

Can you explain please, with pumping lemma or other way to prove/disprove classes of given languages?
Answer: To reiterate your argument about $L_1$: Any string not ending in $11$ is in $L_1$, and a string ending in $11$ is in $L_1$ iff it starts with $11$. So as a regular expression, $$L_1=(\{0,1\}^*(0|01))\mid 11\{0,1\}^*11\mid 111\mid 11\mid 1\mid \epsilon $$
$L_2$ on the other hand is not regular. Assume it is and let $n$ be its pumping length. Consider $1^n0^n\in L_2$ can be written as $uvw$ with $|uv|\le n$ (so consisting only of $1$'s) and $|v|\ge 1$ and such that among others $uv^3w\in L_2$. But that has more occurances of $111$ than of $000$.
Comment: Thanks for nice explanation.
Answer: $L_2$ is can be easily seen to be non-regular using the Myhill-Nerode theorem.
Your automaton for $L_1$ seems to be right, so the language is regular, but it would probably need a more explicit argument that the automaton actually recognizes $L_1$ before it constitutes a proof.
I suggest annotating the states with all combinations of "last two symbols seen" and "difference between number of 110s and 001s ever seen" that can happen in that state. Then it's a purely local matter to verify that those invariants are preserved by every transition.
Comment: Thanks for beautiful explanation.

Is $L_1 = \{w ∈ {0,1}∗ | \text{w has at least as many occurrences of (110)’s as (011)’s}\}$ regular?


SVN info shows old Revision # as the Current Revision

Question: I have an SVN repo. When I recently committed a change, the reflected current revision # is 620:
Transmitting file data ...
Committed revision 620.

However, if I then runsvn info I get the following:
Path: .
URL: http://www.site.com.testing/repos/site.com/trunk
Repository Root: http://svn.server/repos/site.com
Repository UUID: a9c3a166-39f6-4cfb-8622-fd24eb11034b
Revision: 584
Node Kind: directory
Schedule: normal
Last Changed Author: bob
Last Changed Rev: 584
Last Changed Date: 2012-03-26 18:32:55 -0400 (Mon, 26 Mar 2012)

So committing something tells me that the current SVN repo is 620 but svn info tells me that the current rev is 584. Also svn log shows 584 as the current (most recent) rev as well.
If I look at the SVN Log via TortoiseSVN in Windows it correctly shows revision 620 as the current revision.
Am I missing something regarding how svn info and svn log work?
Answer: What Subversion is showing you is that you've checked out revision 584. The information is taken directly from the .svn/entries file, and not from the server.
This stems from Subversion's basic design of not requesting information from the repository server whenever possible. It was a decision made to allow users to have a lot of Subversion's power if they happen to be working remotely and have no network connection to the repository server.

If you do a svn info on one of the files just committed, you'll see that file is at revision 620.
If you did a svn info http://www.site.com.testing/repos/site.com/trunk, you'll see the last revision was revision 620 because it is talking to the server.
If you did a svn update and then a svn info, you'll see your working directory is at revision 620.

Just a quick question: Did you commit a few files, or did you commit the entire working directory? If you specified in your commit the files that were changed, this would make sense. Subversion wouldn't have known to update the .svn/entries file in the root of your working directory. However, I've seen this even when I commit all changes. I've taken to doing an svn up whenever I commit all of my changes.
Remember: It's not a bug. It's a feature.
Comment: Yeah I committed just one file in a sub-directory somewhere. Not the root folder. I know it's not a bug. It makes sense to me know. Also I didn't know I could `svn info` the repo's trunk itself, so +1 for that.
Comment: Thanks for the explanation!  I thought I was going nuts.    I know this is an old post - but I just stumbled across this "feature" today while writing a build/packaging script.  After coding/committing locally I wanted to build the package using my local code, but the version number was the Previous one.  While performing "update" fixes it - that assumes I want to update ALL files too - which may not be what I want to do when creating Beta packages from my local dev PC.  That's my problem - not yours :-)
Answer: Likely the svninfo command is showing you the revision of the current working directory, whereas when you commit a changed file or files, then the output of that commit shows the new revision of those files.  584 will be the last changed/updated revision of the folder.
Try running an svn update on the folder and the re-running svn info. It should bring the rest of the folder up to 620


Change php error reporting to hide warnings for specific site only [Debian|Ubuntu]

Question: Imagine a couple of sites-enabled available from /etc/apache2/sites-available. (Only Debian-based distros work like this.)
Is it possible to mute warning messages from php scripts from a specific site, without touching the actual htdocs?
Normally there are a couple of solutions to achieve someting related:

Add an error_reporting() directive e.g. error_reporting(E_ERROR); to the scripts executed.
Set php_flags in .htaccess files like so: php_flag display_errors off
Use ini_set inside scripts:
ini_set('display_errors', 'Off');
ini_set('display_startup_errors', 'Off');
error_reporting(0);

Prepend @ to functions that throw warnings or errors.
Change php.ini to actually say
error_reporting = E_ALL ^ E_WARNING
display_errors  = 1

However, these all mean touching the htdocs or having the change applied globally. Imagine the htdocs are mounted read-only. How would I suppress warnings (or notices) for certain sites only?
I am assuming Debian/Ubuntu has a2ensite specific configurations for a reason and I am hoping I can alter those. There is a log_level directive in the example 'site available' configuration, but this handles the amount of messages logged. Not the messages output by the php scripts.
Manually adding sections in php.ini or apache2.conf or httpd.conf will also work. If it is possible.
See also:

http://www.php.net/manual/en/function.error-reporting.php
Answer: If you have access to the apache vhost configurations, of course you can also use php_flag directives inside a Virtual Host in the sites-available directory. This way, they're only applied to this vhost.
Add something like this inside your Virtual Host:
<IfModule mod_php5.c>
    php_admin_flag display_errors off
</IfModule>

EDIT:
If you want only actual errors displayed, you can use the php directive error_reporting with an integer value that describes the types of error levels you want to be displayed:
<IfModule mod_php5.c>
    php_admin_flag display_errors on
    php_admin_value error_reporting 22517
</IfModule>

This is equivalent to a setting of
error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED;

in php.ini. More possible values for example here:
https://web.archive.org/web/20131009000719/http://www.websitefactors.co.uk/php/2013/01/php-error-reporting-settings
Error reporting value calculator:
http://www.bx.com.au/tools/ultimate-php-error-reporting-wizard
Comment: Yes, this is what I am looking for. The vhost configuration is site-specific and inside `/etc/apache2/sites-available`. Do you know any trickery to change this so that errors are in fact displayed, but warnings are not?
Comment: I've edited my answer.
Comment: This is [Thrift Shop] awesome! Accepted.
Comment: You can also calculate the number just by running this in the command line: `php -r 'echo E_ALL & ~E_NOTICE & ~E_WARNING & ~E_STRICT & ~E_DEPRECATED; echo "\n";'`
Answer: I added :
<IfModule mod_php5.c>
 php_admin_flag display_errors on
 php_admin_value error_reporting 30711
</IfModule>

to /etc/apache2/sites-enabled/work and thats working just fine.
Yes I know I should fix those notices, but I'm working on a old script, and will need to come back to that, for now this has solved my problem, and was what the op was looking for. Hope this helps someone.
Comment: Can you please explain the meaning of 30711?  I assume this is an or'ed bitmask of the E_* error reporting constants.  If so, it's horrible practice to write it as you did.
Comment: @AndrewSchulman - The constants E_* are only available *within*  php, and apache doesn't know what they are.

See "Note: PHP Constants outside of PHP"
 on http://php.net/manual/en/errorfunc.configuration.php
Answer: There is no way to do this without .htaccess or applying the configuration globally. Apache cannot "see" files outside its root folder (htdocs) and .htaccess files only work when in the site's folder. php_flag doesn't function outside of .htaccess, either.
So basically, it's not possible to do on a site-by-site basis without some modification or an .htaccess file.
Comment: Actually, Apache knows exactly what site we are talking about because we can specify sections for specific directories in the configuration. The answer by etagenklo is close to doing just that. :) Just need to find a way to alter it so it applies to warnings.


Find the condition on αϵℝ that ensures p1,p2,p3 is a basis for ℙ2ℝ.

Question: In this question we denote by
$\mathbb{P}_{2}\mathbb{R}$ is the set of functions {${ax^{2}+bx+c: a,b,c \epsilon
\mathbb{R}}$}, which is a vector space under the usual addition and scalar multiplication of functions. Let $p_{1}, p_{2}, p_{3} \varepsilon$ $\mathbb{P}_{2}\mathbb{R}$ be given by $p_{1}(x)=1, p_{2}(x) =x+2x^{2}, and
 p_{3}(x) = \alpha x+4x^{2}$
(a) Find the condition on $\alpha \epsilon \mathbb{R}$ that ensures $p_{1}, p_{2}, p_{3}$ is a basis for $\mathbb{P}_{2}\mathbb{R}$. You are free to assume 1, x, $x^{2}$ are linearly dependent
(b) In the case $\alpha =5$, write the function $p(x)=1+x+x^{2}$ as a linear combination of $p_{1}, p_{2}, p_{3}$
Hi!
For (a) i know for it to be a basis, it would have to be linearly independent and a span, so far i'm thinking $\alpha$ = 0...
and to check span i get the equations from equating the variables...
2b-4c = $\alpha$,
b+$\alpha$c = $\beta$ and
a= $\gamma$
Pretty lost for both parts :( so would appreciate any hints or help on this question
Thanks!
Comment: Are you expected to be proficient in the use of determinants?
Comment: We haven't really went into depths with it, but we did learn the basics of it doing matrices and everything (assuming thats not in depth haha)
Answer: (a) If you look at $p_2$ and $p_3$ you should see that $\{p_2,  p_3\}$ is lin. independent $ \iff \alpha \ne 2$.
In this case it is easy to see that $\{p_1, p_2,  p_3\}$ is lin. independent !
(b) If $p=ap_1+bp_2+cp_3$, then show that
$a=1, b+5c=1$ and $2b+4c=1$. It is your turn to solve this linear system !


removing a space from a string by awk

Question: How can I remove a space located between name and lastname from a string by using awk?
most of the examples are about redirecting data from command line to awk but I need to manipulate a string inside an awk script.
Convert this:
"steve john"

to:
"stevejohn"

I have a string variable which I asked user to input inside an awk script. I need to remove the spaces.
Comment: `awk '{print $1 $2}' filename` OR `echo "hi gh"| awk '{print $1 $2}'`
Comment: If you have an answer about building shell scripts, please edit your question to include what you have tried.
Answer: gsub is your friend. The following command basically does a global substitution of a regular expression (a single space in this case), replacing it with an empty string, on the target $0 (the whole line).
pax> echo "steve john" | awk '{ gsub (" ", "", $0); print}'
stevejohn

You can use any target, including one input by a user:
pax> awk 'BEGIN {getline xyzzy ; gsub(" ","", xyzzy) ; print xyzzy}'
hello there my name is pax
hellotheremynameispax

Answer: Use sed:
$ echo "steve john" | sed 's/ //g'
stevejohn

If you must use awk, do this:
$ echo "steve john" | gawk '{print $1 $2}'
stevejohn

Edit:
Inside a bash script, you can do this:
s="steve john" # user input
t=$(echo $s | gawk '{print $1 $2}')
echo $t

Comment: I have a string variable which I asked user to input inside an awk script. I need to remove the spaces
Comment: remember to quote your variables ( "$s" not $s ) and you wouldn't really call an external command to do that in bash, you'd use the shell builtins:  s="steve john"; echo "${s// }"
stevejohn
Answer: echo "john smith aaa " |\
awk 'BEGIN {FS=" "; OFS=""} {for(i=1;i<=NF;++i) {out = out OFS $i}} END {print out;}'

Comment: or you can use "sed" command. sed is commonly used to remove or replace strings from input.
Comment: awk is usually a tiny bit faster, though :)

removing a space from a string by awk


How to limit the terms of the view to the terms used?

Question: On the product I created a "tags" taxonomy terms field. I have created a hundred "tags".
My products use around 10 "tags".
I created a pageview of my products with an exposed filter criterion "tags".
My problem :
The filter displayed on the product page displays all the terms. I only want to display the terms used by my products. How to do this ?
Answer: If doing it manually is acceptable you can do it on the Exposed Filter Settings by ticking the Limit list to selected items and then selecting only the 10 tags from the list above (hold Ctrl key to add to the selection). This works if you'll always or for long periods of time need only those 10 tags.

If you want Views to filter automatically and only show what's within the results without you having to choose the tags manually you have to use the Contextual filter instead of the Exposed regular Filter. Contextual filters are under Advanced options.
Add the Contextual filter for the same field "Tags" and configure it:
When the filter value is NOT available
Display a summary
Format List
Base path (you must enter the Page path if this isn't the first Page Display in your Views)
Display record count with link
More
Transform spaces to dashes in URL
Adjust the options for when the value is not available to your needs (Tags Taxonomy term etc.).
This way you get a list of term IDs that link through to a list of nodes tagged with those terms. That is because the Tags field only stores the TID information, not the term name. When you first load the page you only see the Summary, no results, you have to click through to see the results. But if you create the Summary as Attachment on a Page Display you can get it both ways, the Summary and the Results at the same time (keep the path the same).
But you probably want the term name to be displayed instead of the term ID. In that case remove that Contextual filter, and add the one for Taxonomy term Name, but you need a Relationship first for that information to become available.
Add a Relationship Taxonomy term referenced from field_tags (or whatever the field is called in your case), require it becasue you don't want summary results without tags!
Then add a Contextual filter Name from Taxonomy term Category, it will use the Relationship we created, and configure it like the above Contextual settings.

Alternatively you can use an additional module, this one did it in D7 but not yet ported to D8: Views Selective Filters.
Facets module provides an easy filtering interface from what's in the results.
Comment: For this question, manually, "hard coded", you're settingup for failure. People forget and adds an extra un-necessary step. Not sure how using a contextual filter answers the question when OP needs an exposed filter so users can pick the tag they want to filter by.
Comment: Opting for the manual method can work depending on your particular business case. You could be building a short-term campaign for only 3 products that won't restock after sell-out, or similar. It is better to go with automatic if the number of tags used will expand organically. Contextual filter with the **Summary** acts like an Exposed filter as far as the user is concerned, it just looks different, it's links you click on instead of a form you submit.


Loop Through ListBox and Determine ListBox Item Order/Position

Question: I have a ListBox that looks like the attached picture. You can see that Hasbro is the first item in the ListBox, then Matt, then Bif Ban, then Mike's Awesome Stuff. These items are being generated from a SQL table. In that SQL table I have a listBox sort_order column which is just the order that each item appears when they are thrown into listBox. I wrote a Procedure which updates the table that contains the items and sets the listBox sort_order to @sort_order.  When I move items around in the listBox and click the Update Sort Order button I want to loop through the listBox and set the listBox sort_order to whatever the current position the item is in the listBox. For example, Hasbro is currently in listBox sort_order 1, and if I move it to the bottom(after Mike's Awesome Stuff), then it will be in listBox sort_order 4. How could I loop through each item and assign it this new listBox sort_order number value?
I think that it would be something like this but I am unable to get the int id and int sort_order values:
foreach (RadListBoxItem item in LBcarDetail.Items)
{
    int id; //how do I access this value? //this is the primary key from my SqlDataSource table
    int sort_order; //and this value? this would be the position in the listBox

    using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["iConnectionString"].ConnectionString))
    using (SqlCommand comm = new SqlCommand("sp_car_detail_updateSort", conn))
    {
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Add(new SqlParameter("@id", id));
        comm.Parameters.Add(new SqlParameter("@sort_order", sort_order));

        conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();
    }
}

here is what my RadListBox and SqlDataSource look like:
<telerik:RadListBox ID="LBcarDetail" runat="server" DataSourceID="SqlDataSource1" AllowTransfer="true" AutoPostBackOnTransfer="true" OnSelectedIndexChanged="LBcarDetail_SelectedIndexChanged"
    Height="700px" SelectionMode="Multiple" Width="615px" Skin="Outlook" EnableDragAndDrop="true" AllowReorder="true" AppendDataBoundItems="true" AutoPostBack="true">
    <ButtonSettings VerticalAlign="Top" ShowTransferAll="false" ShowTransfer="false" ShowDelete="true"></ButtonSettings>
    <ItemTemplate>

                        display_text:

                        <%# Eval("display_text") %>

    </ItemTemplate>
</telerik:RadListBox>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:iConnectionString %>"
    SelectCommand="SELECT * FROM [intranet].[dbo].[TableItems] ORDER BY sort_order"></asp:SqlDataSource>

Answer: for the sort_order you can use
sort_order = LBcarDetail.Items.IndexOf(item);


Android - No hardware menubutton

Question: I concluded the newer android tablets doesn't have a hardware Menubutton anymore.
So I was wondering if I have to insert an menu button in my own (fullscreen) app or does android offer a software included menubutton itself (even when my app runs fullscreen)?
(I don't have an android phone/tab myself to test, so I asked here)
Thanks,
Dennis
Comment: You probably want to read [Say Goodbye to the Menu Button](http://android-developers.blogspot.de/2012/01/say-goodbye-to-menu-button.html) on the dev blog.
Answer: You should move away from menu buttons. It's the new style of Android applications. See this post from Android developer with the reasons to remove the button and what applications should do instead of offering a menu button.
Answer: Most of devices which are not having menu hardware button runs on android 3.0 or greater version
You just need to add one parameter as showAsAction.
Which will show menu item on action bar.
Concider reading ActionBar tutorial
Comment: The ActionBar is the bar above the app when it is NOT running in fullscreen, right?
But my app is running in fullscreen so the ActionBar is invisible.

Or is this wrong?
Answer: Change the manifest file: you should change targetSdkVersion to <= 10.
Like this:
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="9"/>

Android - No hardware menubutton


Do I need to package a MySQL server/client with my pyinstaller app?

Question: I built an application using pyside and sqlalchemy.  I package this application with Pyinstaller.  It works great with the sqlite database engine, but I am starting to deploy it now and want to do so with the mysql engine.  Do I need to package a Mysql server/client with the application?  And if so can I do so with Pysinstaller?
Comment: No `Pyinstaller` is just for python packages, if you want to install other softwares like `MySQL`, then you need to do that with software installation programs. [Here](http://en.wikipedia.org/wiki/List_of_installation_software) is a list of [them](http://stackoverflow.com/questions/3767/what-is-the-best-choice-for-building-windows-installers).
Comment: Thanks, if you add that as an answer I'll accept it.
Answer: No, Pyinstaller or py2exe are only use for python scripts, that just converts your python script and their's 3rd-party packages into stand-alone executable program without requiring a python installation.
If you want to install other softwares like MySQL, then you need to do that with software installation programs.
Here is a list of them.
Comment: So the steps are: 1. Make the py files 2. Make executable using pyinstaller 3. Make the setup that imports mysql server if needed.


Dehn surgery for non-compact manifolds

Question: Lickorish theorem states that every closes, orientable, compact 3-manifold can be obtained by surgery on $S^3$. What do we know about surgery for non-compact manifolds? I.e. can we obtain $\mathbb{R}^2 \times S^1$ from surgery on $S^3$, $\mathbb{R}^3$, or at least from surgery on some non-compact manifold?
Comment: Surgery preserves compactness, so no, there is no surgery on $S^3$ that produces $\mathbb R^2 \times S^1$.
Comment: For noncompact manifolds, the trivial surgery on $\mathbb R^2 \times S^1$ produces $\mathbb R^2 \times S^1$; that's probably not what you wanted to know, but it answers your question as currently asked.
Comment: @LeeMosher, could you provide a link for a proof about Dehn surgery preserving cpctness?
Comment: Start with a compact manifold $M$ and an embedded solid torus $T \subset M$. Step 1 of surgery is to remove the interior of $T$, leaving $M - \text{int}(T)$, and a compact space minus an open subset is compact. Step 2 of surgery is to glue in a different solid torus $T'$, using some homeomorphism between $\partial T$ and $\partial(M - \text{int}(T) = \partial T$, thus forming the result of the surgery as the quotient of the disjoint union of $M - \text{int}(T)$ and $T'$. But the two pieces of that disjoint union are compact, and their union is compact, and its quotient is compact.
Answer: I'll turn my comments into an answer, since your comment indicates that this is what you want to know.
Surgery preserves compactness, so no, there is no surgery on $S^3$ that produces $\mathbb R^2 \times S^1$.
To prove this, start with a compact manifold $M$ and an embedded solid torus $T \subset M$. Step 1 of surgery is to remove the interior of $T$, so $M - \text{int}(T)$ is a closed subset of $M$, hence compact. Step 2 of surgery is to glue in a different solid torus $T'$, using some homeomorphism
$$f : \partial T \to \partial(M - \text{int}(T) = \partial T
$$
The resulting surgered manifold is thus the quotient of the disjoint union of $M - \text{int}(T)$ and $T'$ by the relation $x \sim f(x)$, $x \in \partial T$. Since the two pieces of that disjoint union are compact, their union is also compact, and its quotient is compact.

Dehn surgery for non-compact manifolds


Views merge rows: merge uniques values and show the first one

Question: I'm using Views merge rows to grouping rows by some fields, It worked good for me, but in some fields I want to show just the first filled value that means: merge uniques values+use the first value.
View after using VMR

Orginal view

Is there a way to do that using views merge rows? or I have to use an other solution?
Or how I can hide/remove rows which have cycle smaller than the max cycle value  for every every container -see the image above-? I couldn't do it with views filter.
I'll explain my use case below to show the purpose of creating this view:

The objective was tracking the containers, so I have to show
  containers and there currents and previous places. The container
  pass by a number of positions (in depot, at port of delivery, at port
  of loading...) until being in the vessel( position: on board), or it
  can be in an exceptional position like "reformed". I called the
  passage through those positions from the start to the end a
  "cycle" If the container is on board we have to fill all
  information about previous positions in a form "bill of lading". What
  I did as solution, I created 3 content type : container,position,bill
  of lading. The position content type reference the container and
  contains a select list field "position container" and 10 field
  collection field, every field collection contains a specifics fields
  for  a single position. So for every node position, I fill the field
  container, I select "position container" , I fill the field collection
  fields corresponding to the position selected. For example if I
  created a new position for the container "blk001" in position "in
  depot", I fill the field collection fields "name depot" and "date in
  depot". For the bill of lading content type it reference the
  containers also, I redirect to it every time the node position is
  saved and "position container" is "on board". Now the challenge is
  providing a list of containers,with the current bill of lading "N°
  B/L", the current cycle and merging all the positions fields
  corresponding to this cycle.
Answer: Sure there is probably some way to do that with PHP or an advanced SQL query. I recommend looking at Views Aggregator Plus as it allows you to aggregrate over Views PHP values.
Views is a simple query builder. When you want to do something really really complex, it can be useful to try to do some business logic outside of the views admin ui.
Comment: My view is really complex , the fields are from many content type and field collections and there will be some other calulated fields, Views merge rows and views php were achieve most of my objectif,my question is how to show just the only filled value by using this 2 modules. I have tried VAP, It's not allow me to compress by two fields.
Answer: I am providing a 2nd answer, as it's drastically different from the 1st. I believe my 1st answer may still help others.
I repeat -- you're trying to do something that is essentially too complex for Views by itself, and you'll have a hard time finding a magical set of Views related modules that will be allow you to create all the business logic you want within Views SQL.
If you could try to make the raw (non-Drupal SQL) you wanted that achieved this query with the data you have as-is, you could then try to use Views Extra Handlers to contain the custom SQL needed, or hook_query_alter to change the query Views uses to achieve the result you want.
I suspect this would difficult, and I suspect you don't know what raw SQL would achieve the list you want.
So I keep saying your task is difficult as-is, with the data you have on hand and the format of the data. Make you're life easier and give yourself more data. You merge results provides multiple date of loading. From all possible entries for a Container (such as BLK001).
Here's a suggestion:

Use Rules
Add a custom field called is_first_load_date as boolean value to your content default.
Default it to FALSE for all entries of your content type.
Using Rules on node insert scan all Containers of title BLK001 and examine date_of_loading -- if the newly inserted Node is the earliest date to be loaded set is_first_load_date to TRUE, set all other NIDs of this Container to FALSE.
Update your query to use this new piece of data, which is simpler to compute using Views.

So that's my new solution for you. Save or compute new additional data you store per-record to make your Views query easier. If you dont like Rules, then use Computed Field, but Rules is more flexible.
I dont know if my exact field you want to aggregate or merge is correct from all the details you've provided. I don't really care what your exact SQL problem is. I'm recommending you approach the solution from a different direction outside of the Views Admin UI.
Comment: Thanks tenken for your responses.I agree with you that is too difficult or impossible to do this via UI admin,I'm trying your approach as last chance to produce this views, before I move to do custom SQL queries for views -I know SQL but I never use it for drupal-. In order to show you how my view is complicated I edited my question and I add a short explanation of my use case.


Quick exponentiation of bit matrices

Question: Is there a method for quickly rising to a power a matrix with only 0s and 1s? I am aware of the diagonalization method. However, it is general and requires a lot of work. Due to the constraint I imposed, can a faster method be devised? I need it for computing the initial states of LFSRs in cryptography.
Comment: @lonzaleggiera Can you please expand on this thought?
Comment: Might [SciComp.SE] be a better home for this question?
Comment: I don't think so. Only motivation for this problem is related to computer science. I need to be able to do computation by hand.
Comment: If your matrix is the companion matrix of a polynomial over $\ GF(2^n)\ $ there is indeed a very efficient method of raising it to a large power.
Comment: @lonzaleggiera I don't know what G is but the matrix's entries are taken mod 2.
Comment: Yes, I was a bit over-enthusiastic in raising $\ GF\big(2^n\big)\ $.  When the entries in the matrix are $0$s and $1$s and the arithmetic is performed mod $2$, the matrix would be the companion matrix of a polynomial over $\ GF(2)\ $—i.e. with $\ n=1\ $.  The expression $\ GF(2)\ $ is just shorthand notation for referring to the set $\ \{0,1\}\ $ equipped with the operations of addition and multiplication mod $2$.  It will be a day or two before I'll be able to write up an explanation of the method I have in mind for calculating the powers of such a matrix.
Comment: @lonzaleggiera Please remember. I have a very important exam soon. It could be of great help.
Comment: If the power is very large you can make use of [binary exponentiation](https://en.wikipedia.org/wiki/Exponentiation_by_squaring) repetetively applying the modulo. With exponent $e$ you would need $\mathcal{O}(\log e)$ matrix multiplications instead of $\mathcal{O}(e)$.
Answer: $\def\eqdef{\stackrel{\text{def}}{=}}$
There are a couple of things that will help you quickly raise a binary companion matrix to a large power using arithmetic mod $2$.
First, exponentiation by squaring and multplying will reduce the number of multiplications needed to raise an element of any semigroup to a power $\ q\ $ to $\ \left\lfloor\log_2q\right\rfloor+b-1\ $, where $\ b\ $ is the number of $1$s in the binary expansion of $\ q\ $. If $\ A\ $ is any semigroup element, and $$ q=\sum_{i=1}^b2^{\alpha_i}\ ,$$
where $\ 0=\alpha_1<\alpha_2<\dots<\alpha_b\ $, then
$$
A^q=\prod_{i=1}^bA^{2^{\alpha_i}}\ .
$$
If
\begin{align}
A_0&=A\hspace{3em}\text{ and}\\
A_j&=\big(A_{j-1}\big)^2
\end{align}
for $\ j=1,2,\dots,\alpha_b\ $
then $\ A_j=A^{2^j}\ $, and
$$
A^n=\prod_{i=1}^bA_{\alpha_i}
$$
All the factors in this product can be computed with $\ \alpha_b=\left\lfloor\log_2q\right\rfloor\ $ multiplications—namely, the $\ \alpha_b\ $ successive squarings needed to compute $\ A_1,A_ 2,\dots,A_{\alpha_b}\ $—and computing the product itself then requires another $\ b-1\ $ multiplications.
Second, the companion matrix of a polynomial is very sparse, and the result of multiplying a column vector by it can be obtained very efficiently using multiplication of polynomials over $\ GF(2)\ $ modulo the matrix's polynomial.  The companion matrix $\ A\ $ of a polynomial $\ f(x)=x^n+\sum_\limits{i=0}^{n-1}f_ix^i\ $ over $\ GF(2)\ $ is given by
$$
A=\pmatrix{0&0&0&\dots&0&f_0\\1&0&0&\dots&0&f_1\\0&1&0&\dots&0&f_2\\\vdots&&\ddots&&\vdots&\vdots\\\vdots&&&\ddots&&\vdots\\0&0&0&\dots&1&f_{n-1}}
$$
The relevance of this to linear feedback shift registers is that if $\ s_0,s_1,\dots,s_t,\dots\ $ is a sequence of bits satisfying the recursion
$$
s_{t+n}=\sum_{i=0}^{n-1}f_is_{t+i}\pmod{2}
$$
then
$$
\big(s_{t+q},s_{t+q+1},\dots,s_{t+q+n-1}\big)=\big(s_t,s_{t+1},\dots,s_{t+n-1}\big)A^q\ ,
$$
with all arithmetic being carried out modulo $2$.
If $\ s=\big(s_0,s_1,\dots,s_{n-1}\big)^T\ $, and $\ S(x)\eqdef\sum_\limits{i=0}^{n-1}s_ix^i\ $, then the entries of $\ A^qs\ $ are given by the coefficients of the polynomial
$$
x^qS(x)\pmod{f(x)}\ .
$$
Computation of the polynomial $\ x^q\pmod{f(x)}\ $ can be carried out very efficiently with the square and multiply technique explained above. Because the arithmetic in all the computations is being carried out mod $2$, however, there are a couple of other minor efficiencies available.  In particular, the square of a polynomial $\ \Theta(x)=\sum_\limits{i=0}^{n-1}\theta_ix^i\ $ is given by
$$
\left(\sum_{i=0}^{n-1}\theta_ix^i\right)^2=\sum_{i=0}^{n-1}\theta_ix^{2i}\
$$
because $\ \sigma^2=\sigma\ $ when $\ \sigma\in\{0,1\}\ $ and $\ 2\equiv0\pmod{2}\ $.  Since the computation of $\ x^q\pmod{f(x)}\ $ will entail lots of squaring and multiplying of polynomials mod $f(x)$ when $\ q\ $ is large, some work can also be saved by precomputing $\ x^i\pmod{f(x)}\ $ for $\ i=n+1,$$\,n+2,$$\,\dots,$$\,2(n-1)\ $.
If $\ e_i, i=1,2,\dots,n\ $ are the columns of the  $\ n\times n\ $ unit matrix (over $\ GF(2)\ $) then $\ A^qe_j\ $ is the $\ j^\text{th}\ $ column of $\ A^q\ $ and $\ e_j=Ae_{j-1}\ $, for $\ j=2,3,\dots,n\ $, which means that the $\ j^\text{th}\ $ column of $\ A^q\ $ is $\ A^{q+j-1}e_1\ $, or, equivalently, its entries are the coefficients of the polynomial
$$
x^{q+j-1}\pmod{f(x)}\ .
$$
Using the method of exponentiating by squaring and multiplying explained above, these polynomials can be calculated very efficiently, even for quite large values of $\ q\ $.
Example
Even with the above-described techniques for reducing the work, I wouldn't care to carry out this procedure by hand for $\ n\ $ much above $\ 10\ $ or $\ q\ $ much above $\ 2^{10}\ $.  To illustrate the method, I'll calculate $\ A^{100}\ $, where $\ A\ $ is the companion matrix of the polynomial $\ f(x)=x^5+x^3+x^2+1\ $:
$$
A=\pmatrix{0&0&0&0&1\\1&0&0&0&0\\0&1&0&0&1\\0&0&1&0&1\\0&0&0&1&0}\ .
$$
First, we precompute
\begin{align}
x^6&= xx^5\\
&\equiv x^4+x^3+x\pmod{f(x)}\\
x^7&=xx^6\\
&\equiv x^5+x^4+x^2\pmod{f(x)}\\
&\equiv x^4+x^3+1\pmod{f(x)}\\
x^8&=xx^7\\
&\equiv x^5+x^4+x\pmod{f(x)}\\
&\equiv x^4+x^3+x^2+x+1\pmod{f(x)}\ .
\end{align}
Now
$$
x^{100}=x^{64}x^{32}x^4\ ,
$$
and
\begin{align}
 x^{16}&=\big(x^8\big)^2\\
&\equiv x^8+x^6+x^4+x^2+1\pmod{f(x)}\\
&\equiv x^4\ .
\end{align}
As it happens, the randomish $5$-degree polynomial  I chose for $\ f(x)\ $ happened to be a divisor of $\ x^{12}-1\ $ (and, yes, this was completely accidental) which makes the remaining computation much easier than it might otherwise have been.
\begin{align}
x^{32}&=\big(x^{16}\big)^2\\
&\equiv\big(x^4\big)^2\pmod{f(x)}\\
&\equiv x^4+x^3+x^2+x+1\pmod{f(x)}\\
x^{64}&=\big(x^{32}\big)^2\\
&\equiv x^8+x^6+x^4+x^2+1\pmod{f(x)}\\
&\equiv x^4\pmod{f(x)}\\
x^{32}x^4&\equiv\big(x^4+x^3+x^2+x+1\big)x^4\pmod{f(x)}\\
&\equiv x^8+x^7+x^6+x^5+x^4\pmod{f(x)}\\
&\equiv1\pmod{f(x)}\\
x^{100}&=x^{64}x^{32}x^4\\
&\equiv x^4\times1\pmod{f(x)}\ .
\end{align}
Once we discovered that $\ x^{12}\equiv1\pmod{f(x)}\ $, we could have obtained $\ x^{100}=x^4\big(x^{12}\big)^8\equiv x^4\pmod{f(x)}\ $ immediately, without wading through this last series of computations.  I have included them, however, to illustrate the sort of calculation that would be needed for a less obliging polynomial.  We can now read off the columns of $\ A^{100}\ $ from the coefficients of the polynomials
\begin{align}
x^{100}&\equiv x^4\pmod{f(x)}\\
x^{101}&\equiv x^5\pmod{f(x)}\\
&\equiv x^3+x^2+1\pmod{f(x)}\\
x^{102}&\equiv x^6\pmod{f(x)}\\
&=x^4+x^3+x\pmod{f(x)}\\
x^{103}&\equiv x^7\pmod{f(x)}\\
&=x^4+x^3+1\pmod{f(x)}\\
x^{104}&\equiv x^8\pmod{f(x)}\\
&=x^4+x^3+x^2+x+1\pmod{f(x)}\ .
\end{align}
That is,
$$
A=\pmatrix{0&0&0&0&1\\1&0&0&0&0\\0&1&0&0&1\\0&0&1&0&1\\0&0&0&1&0}^{100}=\pmatrix{0&1&0&1&1\\0&0&1&0&1\\0&1&0&0&1\\0&1&1&1&1\\1&0&1&1&1}\ .
$$
Extension to arbitrary binary square matrices
A square matrix $\ A\ $ over any field is always similar to a matrix in Frobenius normal form. This means that there exists an invertible matrix $\ P\ $ with entries in the field such that $$
P^{-1}AP=\pmatrix{C_1&O&\dots&O\\O&C_2&\dots&O\\\vdots&&\ddots&\vdots\\O&O&\dots&C_k}
$$
where $\ C_i\ $ are companion matrices of factors of the minimal polynomial of $\ A\ $. When $\ A\ $ is binary matrix (with entries in $\ GF(2)\ $) then so are $\ P\ $ and $\ C_i\ $.  A large power $\ A^q\ $ of $\ A\ $ can then be computed as
$$
A^q=P\pmatrix{C_1^q&O&\dots&O\\O&C_2^q&\dots&O\\\vdots&&\ddots&\vdots\\O&O&\dots&C_k^q}P^{-1}\ ,
$$
and the powers $\ C_i^q\ $ of the companion matrices can all be computed efficiently by the method explained above.
Comment: That's a lot of good work. Thank you. What is the "shift register's polynomial?"
Comment: Thank you.  I've reworded the answer to avoid referring to the shift register's polynomial and to eliminate some nonsense I had written.  I had forgotten that with the form of companion matrix I chose, it steps the shift register by multiplying its state, represented by a *row vector, on the right*, whereas the  multiplication by $\ x\ $ modulo $\ f(x)\ $ represents the application of the companion matrix to a *column vector on the left*.

Quick exponentiation of bit matrices


Model for population density estimation

Question: A database of (population, area, shape) can be used to map population density by assigning a constant value of population/area to each shape (which is a polygon such as a Census block, tract, county, state, whatever).  Populations are usually not uniformly distributed within their polygons, however.  Dasymetric mapping is the process of refining these density estimates by means of auxiliary data.  It is an important problem in the social sciences as this recent review indicates.
Suppose, then, that we have available an auxiliary map of land cover (or any other discrete factor).  In the simplest case we can use obviously uninhabitable areas like waterbodies to delineate where the population isn't and, accordingly, assign all the population to the remaining areas.  More generally, each Census unit $j$ is carved into $k$ portions having surface areas $x_{ji}$, $i = 1, 2, \ldots, k$.  Our dataset is thereby augmented to a list of tuples
$$(y_{j}, x_{j1}, x_{j2}, \ldots, x_{jk})$$
where $y_{j}$ is the population (assumed measured without error) in unit $j$ and--although this is not strictly the case--we may assume every $x_{ji}$ is also exactly measured.  In these terms, the objective is to partition each $y_{j}$ into a sum
$$ y_j = z_{j1} + z_{j2} + \cdots + z_{jk} $$
where each $z_{ji} \ge 0$ and $z_{ji}$ estimates the population within unit $j$ residing in land cover class $i$.  The estimates need to be unbiased.  This partition refines the population density map by assigning the density $z_{ji}/x_{ji}$ to the intersection of the $j^{\text{th}}$ Census polygon and the $i^{\text{th}}$ land cover class.
This problem differs from standard regression settings in salient ways:

The partitioning of each $y_{j}$ must be exact.
The components of every partition must be non-negative.
There is (by assumption) no error in any of the data: all population counts $y_{j}$ and all areas $x_{ji}$ are correct.

There are many approaches to a solution, such as the "intelligent dasymetric mapping" method, but all those I have read about have ad hoc elements and an obvious potential for bias.  I am seeking answers that suggest creative, computationally tractable statistical methods.  The immediate application concerns a collection of c. $10^{5}$ - $10^{6}$ Census units averaging 40 people apiece (although a sizable fraction have 0 people) and about a dozen land cover classes.
Comment: Also this one: P. A Zandbergen and D. A Ignizio, "Comparison of Dasymetric Mapping Techniques for Small-Area Population Estimates," Cartography and Geographic Information Science 37, no. 3 (2010): 199–214. http://www.ingentaconnect.com/content/acsm/cagis/2010/00000037/00000003/art00004

Which seems to call out for blending.
Comment: This paper might be useful: Hwahwan Kim and Xiaobai Yao, "Pycnophylactic interpolation revisited: integration with the dasymetric-mapping method," International Journal of Remote Sensing 31, no. 21 (2010): 5657. http://www.informaworld.com/10.1080/01431161.2010.496805
Comment: You know, dasymetric mapping ultimately as an ecological inference problem. The recent work of K. Imai might be helpful: http://pan.oxfordjournals.org/content/16/1/41.abstract
Comment: Formatting issue now fixed. It was a bug.
Comment: @Rob Thank you, and thanks to all the people who looked at this: I saw your comments before they were deleted and am grateful for your efforts.
Comment: Whuber, would you be willing to provide an answer to your own question here? It seems that you have gained some insight into this problems since initially posing it. Sorry to enter this as a question instead of a comment, but I don't have enough reputation to comment.
Comment: I'm still working on it.  My initial model was similar to the one @Srikant Vadali proposed, but with an equality constraint on total population imposed.  I implemented it as a Poisson GLM with *linear* link.  It worked rather poorly on realistic test data.  My suspicion is that the population density in an area may depend as much, or even more, on the characteristics of the *surrounding* area as it does on the characteristics of that area itself.  I'm still open to creative suggestions or other references!
Answer: You might want to check work of Mitchel Langford on dasymetric mapping.
He build rasters representing population distribution of Wales and some of his methodological approaches might be useful here.
Update: You might also have a look at work of Jeremy Mennis (especially these$^\dagger$ two articles).

$\dagger$ This page doesn't exist anymore.
Comment: Thank you.  That work provides a pointer into a web of recent research on dasymetric mapping.
Answer: Interesting question. Here is a tentative stab at approaching this from a statistical angle. Suppose that we come up with a way to assign a population count to each area $x_{ji}$. Denote this relationship as below:
$$z_{ji} = f(x_{ji},\beta)$$
Clearly, whatever functional form we impose on $f(.)$ will be at best an approximation to the real relationship and thus the need to incorporate error into the above equation. Thus, the above becomes:
$$z_{ji} = f(x_{ji},\beta) + \epsilon_{ji}$$
where,
$$\epsilon_{ji} \sim N(0,\sigma^2)$$
The distributional error assumption on the error term is for illustrative purposes. If necessary we can change it as appropriate.
However, we need an exact decomposition of $y_{ji}$. Thus, we need to impose a constraint on the error terms and the function $f(.)$ as below:
$$\sum_i{\epsilon_{ji}} = 0$$
$$\sum_i{f(x_{ji},\beta)} = y_j$$
Denote the stacked vector of ${z_{ji}}$ by $z_j$ and the stacked deterministic terms of ${f(x_{ji},\beta)}$ by $f_j$. Thus, we have:
$$z_j \sim N(f_j,\sigma^2 I) I({f_j}' e = y_j) I((z_j-f_j)' e = 0)$$
where,
$e$ is a vector of ones of appropriate dimension.
The first indicator constraint captures the idea that the sum of the deterministic terms should sum to $y_j$ and the second one captures the idea that the error residuals should sum to 0.
Model selection is trickier as we are decomposing the observed $y_j$ exactly. Perhaps, a way to approach model selection is to choose the model that yields the lowest error variance i.e., the one that yields the lowest estimate of $\sigma^2$.
Edit 1
Thinking some more the above formulation can be simplified as it has more constraints than needed.
$$z_{ji} = f(x_{ji},\beta) + \epsilon_{ji}$$
where,
$$\epsilon_{ji} \sim N(0,\sigma^2)$$
Denote the stacked vector of ${z_{ji}}$ by $z_j$ and the stacked deterministic terms of ${f(x_{ji},\beta)}$ by $f_j$. Thus, we have:
$$z_j \sim N(f_j,\sigma^2 I) I({z_j}' e = y_j)$$
where,
$e$ is a vector of ones of appropriate dimension.
The constraint on $z_j$ ensures an exact decomposition.
Comment: @Srikant Thank you.  I was thinking along similar lines when I posed the question and have since tested out a GLM (Poisson distribution with *linear* link) as well as some other models.  Unfortunately, it now looks like any model based solely on land cover type and proportion will not work well: a sample of these data suggests that population patterns depend on a larger spatial context.  At a minimum, then, we would need to include spatially lagged covariates in a linear model.

Model for population density estimation


State space models in dlm package - how to add a non time-varying constant?

Question: I am trying to compare the following two models:

\begin{align}
y_t &= \beta_{0,t} + \beta_{1,t} x_{t} + \epsilon_t\\
\beta_{1, t+1} &= \beta_{1,t} + \eta_t\\
\beta_{0, t+1} &= \beta_{0,t} + \omega_t
\end{align}

\begin{align}
y_t &= \beta_0 + \beta_{1,t} x_{t} + \epsilon_t\\
\beta_{1, t+1} &= \beta_{1,t} + \eta_t\\
\end{align}

That is, I am trying to see if it really helps to also have the constant in my model to be time-varying.
However, I cannot seem to find a way to use a non-time varying coefficient ($\beta_0$ of model 2) in either the dlm or the kfas package. Can anyone tell me if there is a way to do that in either of the packages?
Thanks in advance.
Answer: In your model 1 you could set the variance of  $\omega_t$ at zero, effectively forcing $\beta_0$ to be constant. You might experience problems with the algorithm, though.
Comment: I cannot tell you from memory,  but if you print the system matrices you will see by yourself if that is the model you are after.


Change from baseline cannot use ANCOVA

Question: I have the following model:
final count ~ baseline count + factor

The factor has four levels and there is an interaction between baseline count and factor.
This rules out an ANCOVA analysis.
I have thought about doing a percentage change analysis:
(final count - baseline count)/baseline count ~ factor
Firstly, is this the right thing to do? If not, is there a better way to approach this problem?
Answer: Other questions have addressed modelling the change score, final count - baseline count, as the outcome. In that answer I show why using the change score is non-sensical if you want to include the baseline on the right hand side. Assessing interactions with treatments (or equivalently your factors here) is one situation in which you want to include the baseline on the right hand side. The same arguments apply to not using percent change in this situation.
Even if you did not want to assess interactions with your factors, I would not recommend modelling percent change. Percent change has some undesirable properties. Here are a few off-hand:

it is asymetric, e.g. a change from 4 to 5 is 25%, but a change from 5 to 4 is -20%
it is undefined if the baseline is zero
the variance is not easily defined. You can rewrite the change score as $1 - \text{Post}/\text{Pre}$, so the variance is only defined by the ratio of the Pre and Post values.

For Poisson distributed count data, a simpler change metric is $Z = 2 \cdot (\sqrt{\text{Post}} - \sqrt{\text{Pre}})$. If Pre and Post are from the same Poisson distribution with a mean not too small $Z$ has a normal distribution. (Above 5 is the typical recommendation, but IMO it does not behave too badly with means as low as 2~3. The tails are fatter, but it is still close to symmetric.)
Comment: I forgot to accept this answer Andy. In hindsight perhaps my question was a bit obvious, so thanks for taking the time to clarify everything in a clear and easy to understand answer!

Change from baseline cannot use ANCOVA


Probability Confusion/Clarification

Question: Two pizzas are each divided into eight slices and placed inside separate boxes with the
lids closed, and left in an empty room. People randomly arrive at the room to take a slice.
Each person who arrives randomly chooses a box, opens it, takes a slice of pizza and closes
the box. If all eight slices from a box are taken then the lid is not closed.
The first person to discover an empty box opens the other box. Calculate the probability
that it contains all eight slices.
The answer says $\left(\dfrac{1}{2}\right)^8$. This is what I intuitively thought, until my friend posed the solution $\left(\dfrac{1}{2}\right)^7$ as he is looking at the problem from the standpoint that all the others have to do is pick the same box as the first person, and it doesn't matter which box the first person chooses. I have a feeling that perhaps his situation is more conditional probability, but I'm struggling to get my head around what is correct! Can anyone clarify?
Comment: This is a special case of [Banach's Matchbox Problem](https://en.wikipedia.org/wiki/Banach%27s_matchbox_problem), looking only at the probability the remaining pizza still has all eight slices rather than looking at the more general question of the remaining pizza having $k$ slices left.
Answer: The difference between $\left(\frac{1}{2}\right)^8$ versus $\left(\frac{1}{2}\right)^7$ depends on how we interpret the problem.
Problem 1:

We have two pizzas, each with eight pizza slices.  Each time someone enters the room, they open one of the two boxes uniformly at random, independent of each other persons' choices, and will take a slice if able and then close the lid except in the case where they have taken the last slice in which case they throw out the box.  We ask what the probability is that the first person to come in the room after a box has been thrown out will find a full eight slices of pizza in the remaining box.

For this problem, let us label the two pizza boxes $H$ and $T$ and let the people make the decision which box to eat from based on a coin flip.  Here, we have the two sequences HHHHHHHH and TTTTTTTT both result in the ninth person coming in and seeing one of the boxes missing and the remaining box having all of the slices of pizza in it, with a probability of $\left(\frac{1}{2}\right)^8 + \left(\frac{1}{2}\right)^8 = \left(\frac{1}{2}\right)^7$
The calculation could have been made simpler as your friend had noted by not caring which of the two boxes was chosen by the first person and then for the next seven all of them choosing the same box, yielding the same $\left(\frac{1}{2}\right)^7$

Problem 2:

We have two pizzas, each with eight pizza slices.  Each time someone enters the room, they open of of the two boxes uniformly at random, independent of each other persons' choices, and will take a slice if able and then close the lid... even in the case that they closed the lid on a now empty box.  We ask what the probability is that the first time that someone comes into the room and opens an empty box (that they did not cause to be empty themselves), that the remaining box has all eight slices still in it.

For this problem, similarly to the last, we describe via a sequence of coin flips.  Here however, after the first box becomes empty, we require that the empty box be discovered before slices from the still full box be taken.  In this case we required nine heads in a row or nine tails in a row.  This occurs with probability $\left(\frac{1}{2}\right)^9 + \left(\frac{1}{2}\right)^9 = \left(\frac{1}{2}\right)^8$

Which of these was the intended problem?  That is up to the person who asked the problem, but I interpret it as the first as per the included phrase "If all eight slices from a box are taken then the lid is not closed." which implies to me that no random event needs to occur for the ninth person in the event that the first eight people all ate from the same box since the contents of the empty box are plainly visible due to the lid remaining open.
As for why the linked Matchbox Problem gives the other answer, that is because it follows the style of problem 2 where we do not remove the newly emptied box once it becomes empty and we leave it until we attempt to retrieve a match from it and discover there are none to be had.
Comment: @RedG alright... I understand where the confusion is now... it depends on how the question is interpreted.  I will try to rephrase.
Comment: When I follow the link and let $N=k=8$ I get: $8\choose0$ $\left(\dfrac{1}{2}\right)^8$?
Comment: @RedG Edited$~~$
Answer: The only possible way for a person to discover an empty box and a full one, is if  eight slices have been taken (not more). The second condition is, that every slice was taken from the same box. This adds up to your result $(\frac{1}{2})^8$ since every person must take a slice from the same box.

Probability Confusion/Clarification


Teschl, Ordinary differential equation... Problem 5.23

Question: This is a problem from ordinary differential equations and dynamical systems by Gerald Teschl.
Suppose $f\in C_{p}^{1}[a,b]$. Show that for every $\varepsilon>0$,
$\vert f(x)\vert^{2}\leq \varepsilon \int_{a}^{b}\vert f'(x)\vert^{2}dx +(1/\varepsilon +1/(b-a))\int_{a}^{b}\vert f(x)\vert^{2} dx$.
Hint: $\dfrac{d}{dx}\vert f(x)\vert^{2}=2Re(f(x)f'(x))\leq 2\vert f(x)f'(x)\vert$.
I have tried many thing but nothing works, I would appreciate any help.
Comment: You can try so called "epsilon inequalities" (pl. see https://en.wikipedia.org/wiki/Young's_inequality#Elementary_case)
Answer: There is some mid point $m$ in the interval satisfying the mean value theorem for
$$
\int_a^b |f(s)|^2\,ds = (b-a)\,|f(m)|^2
$$
By the fundamental theorem
$$
f(x)^2-f(m)^2=\int_m^x 2f(s)f'(s)\,ds
$$
which implies via Cauchy-Schwarz
$$
|f(x)|^2\le |f(m)|^2 + 2 \sqrt{\int_m^x|f(s)|^2ds}\sqrt{\int_m^x|f'(s)|^2ds}\le  \frac{\|f\|_2^2}{b-a}+2\,\|f\|_2\,\|f'\|_2
$$
As Serge P. commented, now apply the epsilon variant of the basic Young inequality
$$
0\le(a-bε)^2\implies ab\le\frac{a^2}{2ε}+\frac{εb^2}2
$$
with $a=\|f\|_2$ and $b=\|f'\|_2$ to get the result.
Comment: very elegant proof, thanks.


Taking the limit of an integral using residues, why is this wrong?

Question: I have the integral $\lim\limits_{R\to\infty}\int_{-R}^{R} \frac{\cos(x)}{x^2+a^2} dx$ where $a$ is a positive real number. The strategy was to evaluate the limit of the integral on the boundary of a half-disk of radius $R$ and then subtract the limit of the integral on the half-circle.
What I am confused with, is that when computing the integral on the boundary of the half-disk, I used the residue theorem to compute the integral thinking that $\cos(x)$ is meromorphic everywhere but infinity, and our domain is a finite (open) disk, and then took the limit.  However, the reference I am using, says that $\cos(x)$ has an essential singularity at infinity and thus uses a different strategy (computing the limit for $\frac{\exp(ix)}{x^2+a^2}$ and then taking the real part). I don't understand why I cannot use the residue theorem, and then take the limit in this case, because our domain (although gets unbounded when taking a limit) is a finite disk on which cosine is meromorphic.
Can someone please explain where I am going wrong in my thinking?
Thank you very much.
Comment: Use Eulers formula to rewrite the denominator. The sine is an odd function, so don't worry about that. x²+a² is factorable (x+ai)(x-ai) and there is a pole at x=ai which you plug in. (Now which linar term from the denominator "disappears" for Cauchy' formula??)
Answer: It's very hard to "control" or check how the module of the complex sine or cosine are going to behave. This is one reason why we usually use $\,e^{ix}\,$  instead. So define
$$f(z):=\frac{e^{iz}}{z^2+a^2}\;,\;\;C_R:=[-R,R]\cup\,\left(\gamma_R:=\{z\in\Bbb C\;;\;z=Re^{it}\;,\;0<t<\pi\}\,\right)\;,\;\;0<a<R$$
The only singularity of the above function within the domain enclosed by the above path is at the simple pole
$$z=ai\;\implies\;\;\text{Res}_{z=ai}(f)=\lim_{z\to ai} (z-ai)f(z)\stackrel{\text{l'Hospital}}=\frac{e^{-a}}{2ai}$$
So by CRT we get
$$\frac{\pi e^{-a}}a=\oint\limits_{C_R}f(z)\,dz=\int\limits_{-R}^R\frac{e^{ix}}{x^2+a^2}dz+\int\limits_{\gamma_R}f(z)\,dz$$
But either using Jordan's Lemma or directly by the Estimmation Lemma, we get
$$\left|\,\int\limits_{\gamma_R}f(z)\,dz\,\right|\le \frac{e^{-\text{Im}\,{z}}}{R^2-a^2}\pi R\xrightarrow[R\to\infty]{}0$$
So we finally get
$$\lim_{R\to\infty}\int\limits_{C_R}f(z)\,dz=\int\limits_{-\infty}^\infty\frac{\cos x+i\sin x}{x^2+a^2}dx=\frac{\pi}{ae^a}$$
and comparing real parts we get our integral.
Comment: Does $\lim_{R \to \infty} \int_{\gamma_R} \frac{\cos z}{a^{2}+z^{2}} \ dz$ blow up, or does it just not go to zero? I want to say that it can't blow up since the sum of the residues inside of $C_{R}$ is finite. But that might be irrelevant due to the essential singularity at infinity.
Comment: Well, this is part of my problem with those complex trigonometric functions: I really don't know what the limit is off the top of my head, and it perhaps would involve some rather lengthy/nasty computations (with power series...?) to actually find out what goes on there. One thing seems pretty likely: that in the limit and over that half disk the integral vanishes, but again: I can't prove it right now.
Comment: Knowing that $\int_{-\infty}^{\infty} \frac{\cos x}{x^2+a^{2}} \ dx = \frac{\pi}{ae^{a}}$ and $\text{Res} \Big[\frac{\cos x}{x^2+a^{2}}, ia \Big] =  \frac{1}{2ia} \cosh (a)$ , I want to say that $ \lim_{R \to \infty} \int_{\gamma_R} \frac{\cos z}{a^{2}+z^{2}} \ dz = 2 \pi i \Big(\frac{1}{2ia} \cosh (a) \Big) - \frac{\pi}{ae^{a}}$.  But Wolfram Alpha seems to be telling me I'm wrong.
Comment: @RandomVariable If $z = Re^{i\theta}$ on the arc, then cos(z) takes its maximum value at $\theta = \frac{\pi}{2}$, so at $\frac{\pi}{2}$ the numerator becomes $\cos(iR) = \cosh(R)$. $\lim_{R \to \infty} \frac{\cosh(R)}{R^2} = \infty$, so it seems to me that the integrand blows up as R goes to infinity.
Comment: It does seem to blow up. But how can it blow up if the closed contour $C_{R}$ encloses one pole that has a finite residue?
Comment: @RandomVariable My guess is (and maybe someone better at complex analysis than I can clarify) similar to what you state in your first comment: that the function $\frac{\cos z}{z^2+a^{2}}$ can be considered to have a pole at infinity, and that in the limit as R goes to infinity the contour "intersects" this pole, so the residue theorem doesn't apply.  I definitely could be wrong here, though!
Comment: It has an essential singularity (not a pole) at infinity since $\cos (\frac{1}{z})$ has an essential singularity at the origin. But doesn't $e^{iz}$ have an essential singularity at $\infty$ as well? I really wish someone could clarify this.
Comment: Actually, it does appear to be going to what I stated above.  I parametrized the contour and was having  Wolfram Alpha evaluate the integral for large values of $R$ and for different values of $a$.  But I was integrating from $0$ to $\frac{\pi}{2}$ instead of from $0$ to $\pi$. I feel like an idiot.
Answer: Watching the proof of this fact on my note seems that the point it's you need to control the module of your function as $|z|$ goes to infinity. Now the point is that the cosine in the complex plain behaves slightly different regarding the module as its real counterpart. In fact the module of the cosine can assume arbitrarily great values:
$$\cos(z) = \frac{e^{iz}+e^{-iz}}{2}$$
using modules and basical disequalities we obtain $$ \frac{1}{2} | |e^{ix}e^{-y}|-|e^{-ix}e^y|| \leq \frac{1}{2} | e^{ix}e^{-y} -(-e^{-ix}e^{y})|$$
and the former member can be great as you want (note the double module)
So you need to do this "trick" to conclude the proof and therefore use the result. Note,that you have to calculate the residue of $f(z)e^{iz}$ with this method.


dashboard prompt doesn't work in obiee 11g when using javascript

Question: I am using obiee 11g.
I have a dashboard with some sections.
I am using javascript code embedded in a text object (contains HTML marked) within the section. The javascript code I use is this:
<code><script type="text/javascript">
saw.dashboard.onToggleSection('<your-section-id-here>',null);
</script>
</code>
The aim is to collapse the sections by default and it works fine.
but after I did this, when I select dashboard prompt value, the value could not be passed to reports.
Could anyone tell how to fix it.
Thanks a lot
Answer: That's always the problem when you start scripting something which you aren't supposed to touch in the first place.
Remember that you're doing data anlytics with this tool. You're not creating a vanity "app". Think about what's the actual goal and educate your users.
Comment: you are right about the fact scripting is not the solution. But if OBI have a possibility to collapse/uncollapse some analytics, I can't understand why they don't have put the possibility to choose if it has to be collapse as default or not ! Do you have another solution?
Comment: "why they don't have put the possibility to choose if it has to be collapse as default or not ! "

Because that's what's called user customization which then are stored in saved customizations.


Linear Regression Transformation of Dependent Variable by Natural Log and a Constant

Question: I have a dependent variable (rolling 12 month stock returns) that includes negative values. The returns are right skewed, so for a normal model without negatives, I would take the natural log. However, with the negatives, this is no longer possible. Due to this problem I added a constant such that constant = min(returns) + .01 to the returns and then took the natural log.
I am wondering about the downsides associated with this technique. I am mainly concerned with whether the independent variables are positive or negative and less concerned with having to reverse my transformation (i.e., not as worried about interpretability).
Little more info: I have 15 predictors with little collinearity among the predictors and I am taking snapshots of the data on a monthly basis.
Any recommendations besides OLS are also welcome (I did try glmnet).
Comment: Please don't do this.  Have you considered computing the returns in terms of *ratios* rather than differences?  The ratios will be positively skewed, but then you could apply the logarithm.  That's the usual procedure because it's meaningful, interpretable, and does not involve the arbitrary introduction of a start value like $0.01$. It usually works, too.
Comment: "*...the downsides about the downsides*" --- I presume you didn't mean to repeat "downsides" there
Answer: Major downsides I can think of offhand include:

The magnitude of the natural log of "a tiny bit more than zero" is greater than you may think: |ln(100)| = |ln(0.01)|.

This has substantial implications for the distribution you end up examining. I have trouble posting images from my office, but the histogram of natural logs of values from 1:100 looks different from the histogram of natural logs of values from 0.01:1. With values that you transform just to "cross zero" you end up seeing some oddities that disrupt the distribution of your variable of interest and are not really looking at the logged version of your data so much as an arbitrarily different data set.

I definitely understand the urge to use a log transformation on right-skewed data, but I try to reserve it for situations where I think that my distribution might actually be lognormal. Asserting a logarithmic relationship in data is a pretty strong statement about how you think the thing you're modeling works, and making that assertion just to reduce skew seems like a bad trade to me. Do you truly suspect that it might be the case that your dependent variable is related to your dependent ones on a logarithmic scale?

As for what to do instead, whuber's suggestion in comments to use ratios is a good one. If all you care about is the sign of each value, you could also use a binary indicator for sign (1 = positive, 0 = negative). Other transformations exist for dealing with rightward skew, though those may not suit your needs either. You may also want to look at nonparametric methods for analyzing your data, which do not require Normality (log or otherwise).


Moving Salt Master to a new server

Question: So I am tasked with migrating some servers into AWS. One of the servers is a Salt Master.
Upon investigation, all Salt Minions connect to the Master using DNS name instead of IP Address, so from connectivity standpoint I only need to change the A Record for the Master's FQDN.
However, I know that the Master and the Minions exchange some keys between them for authentication purposes.
How do I move the Master but maintain association? Is it enough to copy the whole /etc/salt directory to the new Master?
Answer: Yeah, copying the /etc/salt directory and naming the new host the same name as the old host (probably "salt") is enough.
The directory /etc/salt contains all of the information you might need, as well as keys within the /etc/salt/pki directory.
You should probably make sure the versions at least sort of match between your old and new deployments. The configuration files are self-documenting, and get updated often.

Moving Salt Master to a new server


If $EX^2 < \infty$ and $EY^2 < \infty$, are we guaranteed that $EXY < \infty$

Question: I was wondering under what conditions does $EX^2 < \infty$ and $EY^2 < \infty$, guarantee that $EXY < \infty$? Or is this just false usually?
I tried to come up with some counterexamples, but had little success.
Comment: Cauchy-Schwarz inequality tells you more.  $|EXY| \le (EX^2)^{1/2}(EY^2)^{1/2}$.  Note.  For something not known to be positive, it is strange to write $EXY < \infty$.
Answer: Hint:
$$|x-y|^2\ge 0$$
so
$$x^2+y^2\ge 2|xy|$$
Answer: Yes. Note the following: For any real numbers $a$ and $b$, the following holds: $|ab| \le a^2+b^2$. This implies the following: At each point $x$, the following string of inequalities hold: $0 \le |X(x)Y(x)| \le X^2(x) + Y^2(x)$.
This implies the following: If both $\mathbb{E}[X^2]$ and $\mathbb{E}[Y^2]$ are both finite, then so must $\mathbb{E}[|XY|]$, and therefore $\mathbb{E}[XY]$.


Flexslider LI styles randomly applied

Question: I've got a css issue that I can't get my head around, and could really use some help.
I'm trying to style flexslider on a drupal 7 installation and some but not all of my list styles are being applied to the images, then it seems like the application is random.
In the flex slider on the bottom of this page:http://www.multibrand.visionmix.us/for-her-all
each LI item has a border but each li is also supposed to have a inset shadow applied to it.
Some LIs do but most don't, and I can't figure out why.
I'm using:
Flexslider 7.x-2.0-alpha3
jQuery Update 7.x-2.3+6-dev set to 1.8
Drupal 7.23
Any ideas would be very welcome
Answer: I've got it sorted out.
The images in the LIs were too bug and covering over the effect. Once I resized the images everything was fine.

Flexslider LI styles randomly applied


CKEditor not populating data received the API

Question: Currently I am using formik to show the data received from the API and it works with the help of
enableReinitialize={true} but I am now using CKEditor with it too and I am expecting the CKEditor to show the data received from the API, Following is my code :
<Formik
                enableReinitialize={true}
                initialValues={id ? updatedSchema : initialValues}
                validationSchema={AddNewSchema}
                onSubmit={(values) => {
                  dispatch(actions.createFAQ(values));
                }}
              >
                {({ handleSubmit, setFieldValue }) => (
                  <>
                    <Form className="form form-label-right">

                        <Field name="question" component={Input} label="Question *" withFeedbackLabel={false} />
                        <ErrorMessage name="question" component="div" className="invalid-feedback" />

                        Answer *
                        <CKEditor
                          name="answer"
                          editor={ClassicEditor}
                          onInit={(editor) => {
                            // You can store the "editor" and use when it is needed.
                            console.log("Editor is ready to use!", editor);
                          }}
                          onChange={(event, editor) => {
                            const data = editor.getData();
                            setFieldValue("answer", data);
                          }}
                        />

                      <ErrorMessage name="answer" component="div" className="invalid-feedback" />

                          <Select name="status" label="Status *" withFeedbackLabel={false}>
                            {status.map((option, key) => (
                              <option key={key} value={option.value}>
                                {option.label}
                              </option>
                            ))}
                          </Select>

                      <button type="submit" className="btn btn-primary ml-2">
                        Save
                      </button>
                    </Form>
                  </>
                )}
              </Formik>

Expected : https://i.stack.imgur.com/hY8ky.png
Actual https://i.stack.imgur.com/RjYhm.png
Comment: Could you give an example on the expected outcome and whats the actual outcome?
Comment: @MaddEye Yes added
Comment: I have fixed the issue with the help of data attribute in CKEditor, will post full answer soon
Answer: In react CKEditor, it has a data property which allows to show initial data, I just used it to show data received from the server / API
 <CKEditor name="answer" editor={ClassicEditor} data={updatedSchema.answer}
      onChange={(event, editor) => {
                const data = editor.getData();
                 setFieldValue("answer", data);
                }}
/>


how to convert NSData of image to CGImageRef with out memory leaks

Question: i converted NSData of image to CGImage as below but i am getting memory leaks there but any one tell me how to convert that with out getting leaks:
CFDataRef imgData;
CGDataProviderRef imgDataProvider;
CGImageRef image1;
imgData = (CFDataRef)[array objectAtIndex:i];
    imgDataProvider = CGDataProviderCreateWithCFData (imgData);
    image1 = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);

Answer: This should work:
CFDataRef imgData;
CGDataProviderRef imgDataProvider;
CGImageRef image1;
imgData = (CFDataRef)[array objectAtIndex:i];
imgDataProvider = CGDataProviderCreateWithCFData (imgData);
CFRelease(imgData);
image1 = CGImageCreateWithPNGDataProvider(imgDataProvider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(imgDataProvider);

Don't forget to use CGImageRelease(image1); after you are done with it.
Comment: +1. @laxcha, read [Memory Management Programming Guide for Core Foundation](https://developer.apple.com/library/mac/#documentation/CoreFOundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html). Core Graphics follows the Core Foundation memory management rules.


Is possible create a method with an undefined type?

Question: I am trying to create a method "Insert" to my WebService connected to external DataBase, but instead of create InsertPerson, InsertAge, Insert anything I am trying to do an unique Insert that the parameter type is defined automatically according with request, for example: if I want to insert a person in db, I call Insert("mytype" name), if after I want to insert age calling Insert("mytype" age) and this type is included when you click at specific button.
If it was confusing I will try explain with one more example: I click on "Insert Age" button who will send to "mytype" that is an integer and value of textbox by parameter, the code will connect to DB and insert that. Something like this:
Click event -> mytype = int
Insert(int age) -> "INSERT INTO db VALUE age"
So, after all this, is possible create something like this? Or exist another better alternative?
Answer: Instead of boxing in object, you can do it generically:
public void Insert<T>(T input)
{
    // DB setup code goes here
    var parameter = new SqlParameter("@input", input);

    // Finish DB code here
}

// Call it like this
myClass.Insert<int>(integerInputData);
myClass.Insert<string>(stringInputData);

It's cleaner than boxing and unboxing in object.
https://msdn.microsoft.com/en-us/library/twcad0zb.aspx
However, if you are just passing it to something that expects object (like SqlParameter), then you can just box it in object.
public void Insert(object input)
{
   // DB setup code goes here
   var parameter = new SqlParameter("@input", input);

   // Finish DB code here
}

// Call it like this
myClass.Insert(integerInputData);
myClass.Insert(stringInputData);


KL divergence of models with both continuous and discrete variables

Question: When $x$ is discrete, KL divergence is $D_{KL}(P||Q)=\sum\limits_{x}P(x)\log \frac{P(x)}{Q(x)}$, when $x$ is continuous, $D_{KL}(P||Q)=\int\limits_{x}p(x)\log \frac{p(x)}{q(x)}dx$. However, when the space of the random variable $x$ is defined on mixed continuous and discrete space, what would be the KL divergence?
For example, $x=(r,a)$, where $r$ is a continuous variable that follows Gaussian distribution, $a$ is a discrete variable that follows Bernoulli distribution. $r$ and $a$ are independent of each other.
Under $P(x)$, $r\sim \mathcal{N}(\mu_1,\sigma^2)$ and $a\sim \text{Bernoulli} (\beta)$, i.e., $$P(r,a)=\left\{\begin{matrix}
\mathcal{N}(\mu_1,\sigma^2))\cdot\beta, \quad a = 1, \forall r\in R\\
\mathcal{N}(\mu_1,\sigma^2))\cdot(1-\beta), \quad a = 0, \forall r\in R
\end{matrix}\right.$$
Under $Q(x)$, $r\sim \mathcal{N}(\mu_2,\sigma^2)$ and $a\sim \text{Bernoulli} (1-\beta)$, i.e., $$Q(r,a)=\left\{\begin{matrix}
\mathcal{N}(\mu_2,\sigma^2))\cdot(1-\beta), \quad a = 1, \forall r\in R\\
\mathcal{N}(\mu_2,\sigma^2))\cdot \beta, \quad a = 0, \forall r\in R
\end{matrix}\right.$$
What is the KL divergence of $P$ and $Q$. Thank you very much fo the help!
Answer: In all cases, the KL divergence $D_{KL}(p \parallel q)$ is defined as the expected value of $\log \frac{p(x)}{q(x)}$, where the expectation is taken with respect to $p$:
$$D_{KL}(p \parallel q) = E_{p(x)} \left[ \log \frac{p(x)}{q(x)} \right]$$
In the discrete case, this involves summation:
$$D_{KL}(p \parallel q) = \sum_x p(x) \log \frac{p(x)}{q(x)}$$
And in the continuous case it involves integration:
$$D_{KL}(p \parallel q) = \int p(x) \log \frac{p(x)}{q(x)} dx$$
You can see that the formulas for discrete and continuous distributions simply follow from the definition of expected value in each of these cases.
The mixed discrete-and-continuous case is no different--both summation and integration are involved, as this is how expected value is defined. For example, consider joint distributions $p(x,y)$ and $q(x,y)$ where $X$ takes values in a discrete set $\mathcal{X}$ and $Y$ takes values in a continuous set $\mathcal{Y} \subseteq \mathbb{R}$. Then the KL divergence is:
$$D_{KL}(p \parallel q) \ = \ \sum_{x \in \mathcal{X}}
\int_\mathcal{Y} p(x,y) \log \frac{p(x,y)}{q(x,y)} dy$$
Comment: Useful answer! :), as a sidenote I think there is a typo in the continuous case, wouldn't it be the following expression?: $$ D_{KL}(p\,\lVert\,q) =\int \color{blue}{p(x)}\log\left(\frac{p(x)}{q(x)}\right) dx$$
Comment: @JavierTG Yep, typo. Fixed it, thanks!


How many ways can 5 people sit around a table - can someone explain this to me better?

Question: A probability problem: In how many different ways can 5 people sit around a round table? Is the symmetry of the table important?
Answer:
If the symmetry of the table is not taken into account the number of possibilities is 5! = 120. In this case it would be the same as ordering people on a line. However if rotation symmetry is taken into account, there are five ways for people to sit at the table which are just rotations of each other. So using symmetry the answer is 24.
So I understand why it's 5! for the first part. Order matters so it's a permutation without replacement, so $\frac{n!}{(n-r)!}$  = $\frac{5!}{(5-5)!}$ = 5! = 120
But what about the last part, is there any way to describe that as a permutation/combination? How exactly is 24 calculated? (it appears to be $\frac{5!}{5}$, but I don't see why)
Thank you in advance for any explanations!
Comment: Welcome to stats.SE!  Please take a moment to view our [tour].
Your question reads as if it were a homework problem.  If that is
the case, please read our [wiki](http://stats.stackexchange.com/tags/self-study/info)
related to self-study and add the [tag:self-study] tag to your question.
Answer: If you count it as you initially did, $5!$, you are over counting. For every arrangement, say, (May, Paul, Tina, Fran, Bob), there are $5$ arrangements counted within $5!$ that are inditinguishable in the circular setting. For instance, (Paul, Tina, Fran, Bob, May) or (Tina, Fran, Bob, May, Paul),  (Fran, Bob, May, Paul, Tina) and (Bob, May, Paul, Tina, Fran) all considered identical to the initial (May, Paul, Tina, Fran, Bob). These are cyclic permutations.
They are counted as $(n-1)!$ Instead of $n!$
This is the same as in your case adjusting the overcounted $5!$, by dividing by $5$:
$$\frac{5!}{5}=4!=24.$$
The premise is that you are simply rotating the face of a clock, leaving the relative positions unchanged. There is no external reference, only who is sitting to the left and to the right of each person.
Comment: That's assuming (and I don't really doubt that you correctly guess the original intent) that being in the seat nearest the window rather than the seat nearest the door makes no difference -- if it did, those 5 arrangements become distinguishable.
Comment: @Glen_b This is what I tried to convey in the last sentence of the last paragraph.
Comment: Yes, you did make it clear that was the assumption there; I guess the question kind of covers the "a position can be distinguished" case


Note not in triads chords

Question: So i'm trying to make an arrangement of some song but i'm confused which note should i play. I've tried to listen the voice and the lyrics from that song then match my humming with the guitar and i found 3 ways to play the melody which is :
a.
<code>-----------3------------   1st string

--0--0--3------3--1--3--    2nd string
</code>
b.
<code>-----------3------------

--1--0--3------3--1--3--
</code>
c.
<code>-----------3------------

--3--0--3------3--1--3--
</code>
The first chord played in G chord
So my questions is

Those 3 ways sounds similar and feels match the vocal melody, so which should i choose?
I've watched on youtube of those fingerstyle cover song but there's people which play melody of option a and the other which play the option b. Why they different in melody? Isn't the song has same note so there should just one melody which is right
Is it alright to play note that not from the triads chord?
Let say the lyric is 'naruto' with G chord but the chord starting to play at 'ruto' so can i play note C for 'na' even if the chord is G ? Because G triads is G B D and C is not G triad but 4th
Comment: Not easy to fully understand, but don't expect every single note in a song to match those in the chord. Very few songs do that.
Comment: Edited few lines
Comment: I assume that that tab is showing the high E string and the b string. If so the only note not in a G7 is the open e string (0). The 3 is G. The 1 is F (the flat seven of G which turns the G major triad into a G dominant 7 (G7) chord. The open string is an E and as others below say is a non chord tone which is fine. The 3 on the b string is a D which is the 5th of the g major triad. Anyway seems that maybe this is a G7 and if this is a blues song that is very common.
Answer: Once a song is written, and most likely recorded, that becomes the 'blueprint' - the original - the definitive- version.
Others, who copy it, may decide to change notes (or even are blissfully unaware that they've done that), so that it isn't exactly like the original. Most tmes, it's just a variation on a couple or a few notes, occasionally it's blatantly wrong. If someone prefers the 'new' version, that's what they like to listen to, or not, as the case may be.
It's happened with music for ever. Bits get changed. Jazz has made a career out of it ! It's often interesting to go back to the very original (usually the written music) and compare that with what someone else has done with it, good, bad or indifferent. Take any 'standard', and you'll hear a myriad of differences between performances - some virtually unrecognisable, but nevertheless music.
It's really down to you to decide which you prefer, and play that. Have the other versions ready too, so when someone may say 'that's not right', you counter with 'how about this, then?'
As in my comment, do not expect every note over a certain chord to be part of that chord. Notes called passing notes are used most of the time - often in non-emphasised parts of the bar - but they're needed in just about every song.
Answer:
Those 3 ways sounds similar and feels match the vocal melody, so which should i choose?

If it's important to you to play the melody faithfully, then you should choose the one that exactly matches your reference (such as a sheet music or recording).
If that is not important to you, you can play a variation that sounds good to you.

I've watched on youtube of those fingerstyle cover song but there's people which play melody of option a and the other which play the option b. Why they different in melody?

Are you sure the melody is the same at every point that it repeats in the original song? Does it perhaps vary in the original?
If not, then when people are playing the "wrong" notes, they may be playing a variation that sounds good to them. (Or they may just be making mistakes!)

Is it alright to play note that not from the triads chord?

Yes, it often sounds very good to play these notes - they are called nonchord tones, and can make a melody sound much more interesting.

Note not in triads chords


Vue: Proper way to mutate props passed to component?

Question: In the pseudocode below, I have a parent component that has initial data of name and ages and I am passing them as props to child-component.
My child-component is taking them as props but what if I want to mutate the props.  For example, say I want to reverse the name prop string.  Or if I want to pop() the last age of the ages array?  Do I have to set those props to initial data in the child component?
Parent
<template>

      <child-component :name=name :ages=ages></child-component>
</template>

data() {
    return {
        name: "Alex",
        ages: [23,41,94],
    }
}

Child
props: {
    name: {type: String},
    ages: {type: Array},
}

Comment: Then you pass them with the `.sync` modifier, e.g. `child-component :name.sync="name"` and use `this.$emit('name', newValue)` from within the child component
Comment: Does this answer your question? [Vue 2 - Mutating props vue-warn](https://stackoverflow.com/questions/39868963/vue-2-mutating-props-vue-warn)
Answer: EDIT: can also be done in pair with computed setters like this
export default {
  props: ['propText'],
  data() {
    return {
      dataText: ''
    }
  },
  computed: {
    textValue: {
      get () {
        return this.dataText || this.propText
      },
      set (newValue) {
        this.dataText = newValue
      }
    }
  }
}

You have several ways, like using your logic into a computed (and displaying it directly), copying the prop value to a data (and mutating/working with it there) or emitting the value back to the parent for a modification into the children then.
This article of Michael Thiessen is nice about the subject: https://michaelnthiessen.com/avoid-mutating-prop-directly/
An example snippet
export default {
  name: 'ReversedList',
  props: {
    list: {
      type: Array,
      required: true,
    }
  },
  computed: {
    reversedList() {
      return this.list.reverse();
    }
  }
};

Comment: thanks @kissu.  If I make it a computed property though, don't I lose the ability to mutate it within that component dynamically?  Like I get I can have the computed property do something like `return this.list.reverse()` but that doesn't feel as flexible as setting it to `data` and then doing whatever I want with it at anytime
Comment: You can actually do a lot of things but you can also copy it to `data` on `mounted` (I guess) or use a combo of computed setter, I just edited my answer to show an example.
Answer: You should not mutate props in children componenets. This is not a good practice.
See Vue 2 - Mutating props vue-warn
Comment: The question is how to mutate props correctly. Not how to mutate props incorrectly. See [two-way binding with the .sync modifier](https://vuejs.org/v2/guide/components-custom-events.html#sync-Modifier)


Eigenvalues and eigenvectors of Circulant Matrix

Question: I have an image with pixels that form a 1D lattice. Pixel intensities are given by `$x=(x_1,x_2,...,x_N)^T$
The mean of x is zero and variances are 1. Neighbors are correlated by covariance matrix $C_{ij}$, which is 1 for all the diagonal components i=j. All the other elements of C are 0, but when $|i-j|=1$, where |i-j| is mod N, $C_{ij} = a $ and $a \lt0.5$
$C$ is a circulant matrix, how do I find an analytical expression for the eigenvalues and eigenvectors of $C$?
Comment: It's not a bad idea to try google and wiki first: http://en.wikipedia.org/wiki/Circulant_matrix You can even search this website: http://math.stackexchange.com/questions/297615/circulant-matrix-eigenvector
Answer: This isn't my field but I see in http://epubs.siam.org/doi/book/10.1137/1.9780898718850 that circulant matrices can be diagonalized by a Fourier matrix and their eigenvalues can be found efficiently via the FFT. See also
E. Brigham, The Fast Fourier Transform and Its Applications, Prentice–Hall, Englewood Cliffs, NJ, 1988.
I hope this points you in the right direction.