How to read a GPIO input pin in ARM using the Raspberry Pi Pico?

Question: I am starting to learn assembly language and ARM using the raspberry pi pico.
I have even been following the book RP2040 Assembly Language programming but there is no instruction about how to read an input pin using only assembly.
Here is my set up: I have a push button that send 3,3V when not pressed and 0V when pressed.
I would like to be able to read the different states this has using a GPIO pin but despite looking into the functions of the datasheet (that I barely understand as it's the first time i'm dealing with it), I don't know if what I am doing makes sense.
Here is my code:
led_loop:
    @  set the control for the push
    ldr r0, =ctrl2      @  control register for GPIO1 0x40014008
    mov r1, #5          @  Function 5, select SIO for GPIO1 2.19.2
    str r1, [r0]        @  Store function_5 in GPIO1 control register
@ shifts over "1" the number of bits of GPIO pin
    mov r2, #1          @  load a 1 into register 1
    lsl r2, r2, #1      @  move the bit over to align with GPIO1
    ldr r0, =sio_base   @  SIO base 0xd0000000
    str r1, [r0, #0x00000004]   @  0x004 GPIO input enable
    ldr r0, =helloworld
    BL printf
    b led_loop          @  do the loop again

.data
    .align 4
helloworld: .asciz "Hello World %d\n"

So far I tried to consult the datasheet but I do not have enough knowledge to recognize if what I am doing is write or wrong.
Comment: is this your first mcu to do this or just another mcu?  if the first then move on to something simpler then come back.   their docs are not good, you have to do a lot of reverse engineering and of course finding scary code just like any other vendor.   the docs are pretty from a formatting and art class stuff, but content is lacking.   try an stm32 nucleo board then come back.    assembly vs C, doesnt matter here its just a programming language you can do anything in asm that you can do in others.   having a printf is quite disturbing.
Comment: control register for GPIO1 0x40014008    0x4001400C is the r/w control register for gpio 1  008 is the status register.
Comment: why are you writing to GPIO_IN?  just read it.  you only need to set the sio function one time.
Comment: Sorry I didn't mention but the "HelloWorld" is for debugging purpose, i would like to see if there is any change at the screen when I push the button. I'll read carefully everything you wrote and then I'll comment back. Thanks for your comments!
Answer: You are on the right track but appear to be using wrong register values or using them the wrong way.  You have a printf so that is very odd, and implies you have a ton, ton, more code which may or may not be helping.
This is C and uses the led on the original picos, which not sure if that is the case anymore.
void PUT32 ( unsigned int, unsigned int );
unsigned int GET32 ( unsigned int );
void DELAY ( unsigned int );

#define RESETS_BASE                 0x4000C000

#define RESETS_RESET_RW             (RESETS_BASE+0x0+0x0000)
#define RESETS_RESET_XOR            (RESETS_BASE+0x0+0x1000)
#define RESETS_RESET_SET            (RESETS_BASE+0x0+0x2000)
#define RESETS_RESET_CLR            (RESETS_BASE+0x0+0x3000)

#define RESETS_RESET_DONE_RW        (RESETS_BASE+0x8+0x0000)
#define RESETS_RESET_DONE_XOR       (RESETS_BASE+0x8+0x1000)
#define RESETS_RESET_DONE_SET       (RESETS_BASE+0x8+0x2000)
#define RESETS_RESET_DONE_CLR       (RESETS_BASE+0x8+0x3000)

#define SIO_BASE                    0xD0000000

#define SIO_GPIO_IN                 (SIO_BASE+0x04)

#define SIO_GPIO_OUT_RW             (SIO_BASE+0x10)
#define SIO_GPIO_OUT_SET            (SIO_BASE+0x14)
#define SIO_GPIO_OUT_CLR            (SIO_BASE+0x18)
#define SIO_GPIO_OUT_XOR            (SIO_BASE+0x1C)

#define SIO_GPIO_OE_RW              (SIO_BASE+0x20)
#define SIO_GPIO_OE_SET             (SIO_BASE+0x24)
#define SIO_GPIO_OE_CLR             (SIO_BASE+0x28)
#define SIO_GPIO_OE_XOR             (SIO_BASE+0x2C)

#define IO_BANK0_BASE               0x40014000

#define IO_BANK0_GPIO25_CTRL_RW     (IO_BANK0_BASE+0x0CC+0x0000)
#define IO_BANK0_GPIO25_CTRL_XOR    (IO_BANK0_BASE+0x0CC+0x1000)
#define IO_BANK0_GPIO25_CTRL_SET    (IO_BANK0_BASE+0x0CC+0x2000)
#define IO_BANK0_GPIO25_CTRL_CLR    (IO_BANK0_BASE+0x0CC+0x3000)

#define IO_BANK0_GPIO1_CTRL_RW     (IO_BANK0_BASE+0x00C+0x0000)
#define IO_BANK0_GPIO1_CTRL_XOR    (IO_BANK0_BASE+0x00C+0x1000)
#define IO_BANK0_GPIO1_CTRL_SET    (IO_BANK0_BASE+0x00C+0x2000)
#define IO_BANK0_GPIO1_CTRL_CLR    (IO_BANK0_BASE+0x00C+0x3000)

int notmain ( void )
{
    unsigned int ra;
    PUT32(RESETS_RESET_CLR,1<<5); //IO_BANK0
    while(1)
    {
        if((GET32(RESETS_RESET_DONE_RW)&(1<<5))!=0) break;
    }
    PUT32(SIO_GPIO_OE_CLR,1<<25);
    PUT32(SIO_GPIO_OUT_CLR,1<<25);
    PUT32(IO_BANK0_GPIO25_CTRL_RW,5); //SIO
    PUT32(SIO_GPIO_OE_SET,1<<25);
    for(ra=0;ra<10;ra++)
    {
        PUT32(SIO_GPIO_OUT_XOR,1<<25);
        DELAY(0x40000);
    }

    PUT32(SIO_GPIO_OE_CLR,1<<1);
    PUT32(SIO_GPIO_OUT_CLR,1<<1);
    PUT32(IO_BANK0_GPIO1_CTRL_RW,5); //SIO
    while(1)
    {
        ra=GET32(SIO_GPIO_IN);
        if(ra&2)
        {
            PUT32(SIO_GPIO_OUT_SET,1<<25);
        }
        else
        {
            PUT32(SIO_GPIO_OUT_CLR,1<<25);
        }
    }
    return(0);
}

finding all of those addresses and info strictly from docs is, well...
PUT32 is just a store word and GET32 is just a load word.  and delay is just a counting to zero loop.
GPIO generally defaults/resets to inputs, the clearing of the out/oe and making it an input I probably got directly from their libraries, is it required?  Do not know, could probably read those registers and see.
You do need to release reset on the I/O block for sure, so maybe you are doing that elsewhere.
The above blinks the led 5 times and then goes into a polling loop reading the gpio and isolating gpio 1 if high then light the led if low then not.
Trivial to convert this to asm and should be able to see the differences in what you did and what I did and decide if you need to clear the out/oe settings.  You had wrong addresses and were writing things you should be reading and IO block out of reset at a minimum.


Model.create is not a function - strapi

Question: I have a few unique fields when creating model in strapi's admin ui.
I realized that when field is not provided during the api call, it'll give an error message of 500 instead of the proper error message.
I do understand why there's an error since I can see the log in backend console and I have scanned through posts such as https://github.com/strapi/strapi/issues/1189 and https://github.com/strapi/strapi/issues/1175
After reading those issues, I believe the best way is to go /api/controllers and create a function such as create to override the one provided but I get an error of Model.create is not a function
I have not do much in the controller yet so code is slim.
module.exports = {
    /* Strapi has default create function
     * But because of the error message it provide is vague, will have to customize the controller */
    create: async (ctx) => {
        try {
            console.log(ctx.request.body, 'ctx');
            const article = await Article.create(ctx.request.body);
            console.log(article, 'article');
        } catch (e) {
            console.log(e, 'error');
        }
    }
};

I have read the issue ticket https://github.com/strapi/strapi/issues/1505
But I am using
strapi: 3.0.0-beta.17.5
node: v10.17.0
npm: 6.11.3
db: sqlite3 (local) postgresql (staging)
Anyone know what I might have done wrong?
Thanks in advance for any help and advise.
Answer: I suggest you check the default controller functions here https://strapi.io/documentation/3.0.0-beta.x/concepts/controllers.html#extending-a-model-controller
You will see how to use service functions to create entries.
I don't suggest you to use Model Global variables.
const { parseMultipartData, sanitizeEntity } = require('strapi-utils');

module.exports = {
  /**
   * Create a record.
   *
   * @return {Object}
   */

  async create(ctx) {
    let entity;
    if (ctx.is('multipart')) {
      const { data, files } = parseMultipartData(ctx);
      entity = await strapi.services.restaurant.create(data, { files });
    } else {
      entity = await strapi.services.restaurant.create(ctx.request.body);
    }
    return sanitizeEntity(entity, { model: strapi.models.restaurant });
  },
};

Comment: I did read that too but guess I had typo or something because it didn't work the first time. So I ended up doing `const newArticle = new Article(ctx.request.body); newArticle.save()`  What you provided works like a charm~  What might be the cons for me doing `newArticle.save()` though?
Comment: P.S. so by doing `create: async(ctx)` is actually overriding and `async create(ctx)` is extending? they look too similar so didn't realize that when I scanned through the documents
Comment: It will not use the query system that manages relations and other stuff.
About defining the `create` function, both are correct and will do the same thing.
Can you set the question as answered, it will help to know this topic id OK ;)
Thank you.

Model.create is not a function - strapi


Help with understanding ABRSM grades?

Question: I've been playing piano for a few years as something I take seriously and sing in the school choir just for fun. My piano teacher and choir director semi-frequently compliment me on how quickly I can grasp music theory, but I have no real way of getting an accurate grasp of my progress. Recently, though, I found out about the ABRSM grades 1-8 and many people tend to use this to evaluate their progress. For example, they say if you want to major in music, knowing at least grade six piano music would help a lot.
Out of curiosity I tried googling songs of various difficulties, but different pieces of the same grade seemed to have varied quite a bit, at least with the mid level grades.
To help clear some confusion, could anyone please provide some popular examples of piano music (with images) so I can know what I can expect to see from each grade level, which will hopefully allow me to evaluate my progress a bit better?
Not sure if this helps, but here is the most advanced piano book I am currently working on:
http://www.alfred.com/Products/Alfreds-Basic-Adult-Piano-Course-Lesson-Book-3--00-34928.aspx
The website provides no information regarding difficulty, so if anyone is familiar with this book and knows what the highest grade level I can expect is, that would be helpful. I am about 20-30% done with the book and have completed the previous two books in the series (it is book 3 out of 3 btw).
Apologies in advance if this comes off as duplicate, off topic, too broad, unclear, or opinionated (my first question on this site broke one of those rules, so I'm trying to not repeat the same mistake).
Update: Should I decide to take an ABRSM exam, is it something you can do online or does it have to be a face to face sort of thing? Also would I have to start with the grade 1 exam or can I choose any grade I want for the first exam?
Comment: "different pieces of the same grade seemed to have varied quite a bit" - if you are looking at "free stuff on the internet", make sure it is the original edition of the music, and not an "easy to play" version.  ABRSM publish their own graded collections of pieces (not just the current exam-syllabus pieces) and if they say one of their own books is "grade 4 standard", that means what it says.
Answer: Not a complete answer, but this might help:
You can find a pdf document here on the abrsm website, which gives full details of the exams, and lists the pieces you need to play to pass at each of the grades 1-8.
It is not possible to take the practical exams online - you have to turn up up at an exam centre and play the pieces in front of an examiner.
There is no need to start at grade 1, and you can skip grades if you want. E.g you could take grade 5 at some time and then take grade 7 next time, if you wish. However, to take any grade above 5 (so 6,7 & 8) in any instrument you must have passed the grade 5 music theory exam. This is to prove you have a good enough understanding of musical theory to be able to understand the pieces you have to play.
It is all explained in the document above, which also explains other requirements, such as scales, arpeggios and aural tests.
Edit:
There is a more recent version of the ABRSM piano syllabus available on their website.
Comment: Since this answer, ABRSM have started offering Performance Grades, which are assessed from recordings. https://gb.abrsm.org/en/our-exams/performancegrades/
Answer: The ABRSM syllabus is very well put together. Each piece in a specific grade may test different particular parts of your instrument's technique. There may be a specific technique that you struggle with that may give you the impression that it is harder while the other piece that tests a skill you have already mastered may feel to you easier.
The modern piece I know sometimes has unusual rhythms that you can struggle with, while I personally struggle sometimes with the correct interpretation of the ornamentation when it comes to Baroque pieces.
There is also slow pieces that are easier to play tempo-wise but they are a page longer than pieces that are a bit more complicated but shorter. So it all evens out in the end.

Update: Should I decide to take an ABRSM exam, is it something you can
  do online or does it have to be a face to face sort of thing? Also
  would I have to start with the grade 1 exam or can I choose any grade
  I want for the first exam?

When I started singing, I trained for a while to get my technique proper and afterwards I started working on Grade 4 repertoire, but this may very well be somewhat of a unique case seeing as I already had many years of musical training under my belt, so I was not completely green.
A teacher that has intimate knowledge on ABRSM various grades for your instrument will be a great asset to you, in deciding where on the graded exams totem pole you currently are.
Answer: From memory, I'd rate the one tune shown in Alfred's 3 as around grade 2/3 in ABRSM schedule. You'll have to play, live, 3 selected pieces, usually a baroque/classical, something from romantic sort of era, and a modern piece, to show a spread of styles. Go in at maybe grade 2, to test the water. There is no need to do every exam, but for the first time, it's somewhat an unknown quantity, unless your teacher has first hand knowledge. There are scales and arpeggios, too which sadly don't bear a lot of relationship to the pieces played - which would give relevance to why scales are important; sight reading of a difficulty about the same level as pieces for a couple of grades below, and aural tests, which often get left to just before the exam by some teachers!
As already mentioned, grade 5 theory is needed to take the higher grades, but as a lot of that theoretical stuff will crop up as you progress, it wouldn't hurt to just take grade 5 as and when - a sit and write exam in a town hopefully near you.
Ball park times seem to be around a year to prepare for most people, but obviously that varies. The examiners are, in my opinion, fairly stringent, and pieces are expected to be pretty well perfect,(as writ) which means practising them to death. However, they are a well respected yardstick in a lot of countries.

Help with understanding ABRSM grades?


Why is the Levi Civita equal to zero when multiplied by the Kronecker Delta?

Question: In an solution to a problem I was attempting it uses the fact that,
$$\epsilon_{ijk}\delta_{ij} = 0$$
The explanation I am given says: "the levi-civita is antisymmetric under swaps of i and j whilst the kronecker delta is symmetric under swaps of i and j".
I understand what that means but I am struggling to use that fact to prove the statement above.
Comment: Do we follow the Einstein convention i.e are we talking about $$\epsilon_{ijk}\delta_{ij}=\sum_{i,j}\epsilon_{ijk}\delta_{ij}$$
Answer: Oh nevermind I've figured it out.
$$\delta_{ij}\epsilon_{ijk} = -\delta_{ij}\epsilon_{jik}=-\delta_{ji}\epsilon_{jik}=-\delta_{ij}\epsilon_{ijk} \\ \therefore \delta_{ij}\epsilon_{ijk} = -\delta_{ij}\epsilon_{ijk} = 0$$
Comment: Indeed. Though it's perhaps simpler (and somewhat more illuminating if you work a lot with $\delta$) to just note that if $\delta{ij}\neq 0$, then $i=j$ and thus $\epsilon_{ijk}=\epsilon_{iik}=\epsilon_{jik}=-\epsilon{ijk}$ so that $\epsilon_{ijk}=0$.
Comment: Ah yes that's a good way to look at it $\delta_{ij} = 1 \rightarrow i = j \rightarrow \epsilon_{ijk} = 0$


Integrate $\int\frac{e^{\arcsin x} + x + 1}{\sqrt{1-x^2}}dx$

Question: does anybody have an advice how I can integrate this $$\int\frac{e^{\arcsin x} + x + 1}{\sqrt{1-x^2}}dx$$ I tried substitution $\arcsin x=t$, but was not able to finish it.
Comment: What are you missing, this change of variable makes it trivial ?
Comment: The substitution should help...
Comment: "I tried substitution arcsinx=t, but was not able to finish it" We really must know where you were stopped, to answer meaningfully your question.
Comment: Yeah, I already got it. Sorry, for trivial question.
Comment: @YvesDaoust it really does)
Comment: @Kugelblitz it did)
Comment: @Did passed it already, anyway thx
Comment: Alright. No problem :)
Comment: Remember, a problem is trivial to some, difficult to others, so don't out yourself down. It's an interesting question for math amateurs like me nonetheless!
Comment: @Kugelblitz thank you for the support. I agree with you ;P
Answer: I got this. When you substitute $arcsinx=t$ you got really good output. $$\int\frac{e^{arcsinx}}{\sqrt{1-x^2}}dx$$ if $arcsinx=t$ => $dt=\frac{dx}{\sqrt{1-x^2}}$ and the integral transforms into $$\int e^tdt=e^{arcsinx} + C_1$$ The whole answer is $$e^{arcsinx} -\sqrt{1-x^2} + arcsinx +C$$ Thats it.


Why prior distribution is not conditioned on X?

Question: I would like to know why in the below formula the prior distribution of theta is not conditioned on X (observations):
$$P(\theta|X, y)=\frac{P(y|X, \theta)P(\theta)}{P(y|X)}$$
In my understanding, the correct formula should be:
$$P(\theta | X, y) = \frac{P(y| X, \theta) P(\theta| X)}{P(y|X)}$$
But I think I am missing something.
Comment: Why do you think this should be correct?
Comment: P(theta | y) = P(y | theta) P(theta) / P(y)  right?

We added new condition to the formula P(theta | y, X). The entire formula has changed but the peior P(theta). Why does this make sense? If we condition to X, then both likelihood and prior should be conditioned to X.
Comment: Is this some sort of regression where X is used to explain Y?
Answer: This must be in the context of a regression model $P(y \mid X, \theta)$ where $X$ is considered a constant. This is usually done for reasons of inference, as explained in What is the difference between conditioning on regressors vs. treating them as fixed?.
Then, for inference on $\theta$ after seeing $X$, it does not matter if you condition $p(\theta)$ on $X$ or not, since you treat $X$ as fixed and known anyhow.
Answer: Regressors in regression are often assumed to be fixed. In other words, they are fixed, real vectors and not random variables. Typically, they are used to predict or explain the random variable $Y$. Notice, that regressors don't have to be fixed but this is the case in ordinary least squares and many of its variants.
Assuming this is regression and $X$ are regressors and $Y$ is the dependent variable, $P(\theta|X) = P(\theta)$ because $X$ is just some fixed value.


A4 page too small

Question: So I'm trying to print (CTRL+P) a page. I have all the resets I need, so I don't need to worry about margin or padding around my document. However, I simply cannot get the size to match an A4 page. Here's what I have tried:
@media print {
    .page {
        height: 297mm;
        width: 210mm;
        margin: 30mm 10mm;
    }
}

I have also tried:
@media print {
    @page {
        height: 297mm;
        width: 210mm;
        margin: 30mm 10mm;
    }
}

I have also tried simply doing:
.page {
    height: 297mm;
    width: 210mm;
    margin: 30mm 10mm;
}

But all of these simply won't make my print fit. When I throw a border: 1px solid #000 on my .page element, the page is pretty much 50% of the A4 page.
What I want to achieve is: I have 1-many .page elements. No matter how many there are, I NEVER want to print an empty page (like if one page suddenly is 1px too tall). How can I achieve something like this?
Comment: My comment was only meant as that, a comment, and I only tried to explain that you need to start with the right figures and then troubleshoot stuff. CSS that tries to accomodate to a 23 centrimetre sheet perhaps is not the best starting point (for instance, the browser will need to make it smaller if it needs to fix in A4).
Comment: In CSS (at least in default box model), margins are added to width and height. So you end up with 10 + 210 + 10, etc.
Comment: @ÁlvaroGonzález Yes that is correct. How would I ensure it's A4 no matter what? I can throw on an `overflow: hidden;` just to ensure, but setting the page size is my number one priority.
Comment: Set width to 190px?
Comment: @ÁlvaroGonzález 1190px?
Comment: @MortenMoulder - Nope, 190**mm**
Comment: @enhzflep Why would I set the width to 190mm, when 210mm is too small and isn't correct? Where do you get the 190mm from? An A4 page is 210mm wide.
Comment: Yes, sorry, I obviously meant mm.
Comment: @MortenMoulder - never crossed this bridge myself, hence my interest. (I just write directly to a pdf with php) I just tried to provide what seemed to be the intended answer.
Comment: @enhzflep No offense, but why would 190mm ever be the intended answer, when 190mm isn't anywhere near the correct size? I've gotten 1190x1683 pixels to work.
Comment: @ÁlvaroGonzález What do you mean? 23 centimetre? Where did you get 23 centimetre from?
Comment: 23 comes [from here](https://duckduckgo.com/?q=(10+%2B+210+%2B+10)). It's the size you set it your own CSS.
Comment: Related: [The Box Model](http://stackoverflow.com/documentation/css/646/the-box-model#t=201612051517101504729)
Comment: @ÁlvaroGonzález Completely ignore the margin for now. How do I set 210x297mm in CSS as the size of my document? That's my question. My question was never "How do I prevent margin from scaling the size of my site" (which you can prevent with box-sizing).
Comment: @MortenMoulder - none taken. You'd have to (and as I see, have already had occasion to) ask Alvaro that. But as is obvious - the mental model I had matched Alvaro's. Namely 10mm margin + 190mm content + 10mm margin = 210mm total width. At least when using the winapi from C to size the HBITMAP of the printer's HDC, one doesn't include the margins in the page-size. You need to subtract them from the PPI * totalWidth - that is to say, the available page width is generally about 190mm. ;) PDF simplifies the task enormously, which is why I use it - it's far simpler and above all else, _consistent_
Comment: @enhzflep PDF is also something you can just do with PHP, while this isn't something I can do with HTML and CSS ;)
Comment: @MortenMoulder - yep. Precisely why I wrote a low-level class from scratch in PHP to deal with it. When outputting student's timetables, we couldn't be sure of their printers nor the individual margin settings. It was a far simpler and more elegant solution to produce documents that were the same for everyone. Sure, we'd show an HTML one for screen-use, but offer a pdf for the purposes of printing. This is going back 7-8 years now, I visited this question as a lazy way of checking the current state of affairs - far too lazy to search for what is only an idle curiosity to me.
Answer: Some printers have default settings that can be set to print without edges. Try to look at printers settings when you try to print again. It has nothing to do with css all the time.
Comment: The issue is not that it's being cut off. As I also stated, I have removed all the default margins and padding, meaning that the top left of my page is actually the top left of the printed document as well (minus the margins when actually printing).

A4 page too small


Prove $\lim_{z\to 2i}(2x+iy^2)=4i $ using definition

Question: Help me with this exercise:
Proof using definition,
$$\lim_{z\to 2i}(2x+iy^2)=4i $$
if given $\epsilon >0$ I can find $\delta>0 $ such that,.
If $0<|z-2i|<\delta$ then $|2x+iy^2-4i|<\epsilon $
Comment: what are x and y?
Comment: So far so good. How do you propose to find $\delta$, after being given $\epsilon$?
Comment: @MyGlasses When $z\to i2$, $y\to 2$.  ;-))
Answer: First, we restrict $z-2i$ so that $|z-2i|\le 1$.  Then, $1\le y\le 3$ and $y+2\le 5$.  Hence, we can write
$$\begin{align}
|2x+iy^2-4i|&=|2(x-0)+i(y^2-4)|\\\\
&\le \sqrt{4(x-0)^2+(y+2)^2(y-2)^2}\\\\
&\le\sqrt{4x^2+25(y-2)^2}\\\\
&\le 5\sqrt{(x-0)^2+(y-2)^2}\\\\
&<\epsilon
\end{align}$$
whenever $|z-i2|=\sqrt{(x-0)^2+(y-2)^2}<\delta=\min\left(1,\frac{\epsilon}{5}\right)$.  And we are done!
Comment: Please let me know how I can improve my answer.  I really want to given you the best answer I can.  -Mark
Answer: If you take $z=x+yi$
$$\lim_{z\to 2i}(2x+iy^2)=4i\\\lim_{(x,y)\to (0,2)}(2x+iy^2)=4i\\
0<|\sqrt{(x-o)^2+(y-2)^2}|<\delta \Rightarrow |2x+iy^2-4i|<\epsilon\\
0<|\sqrt{x^2+(y-2)^2}|<\delta \Rightarrow |2x+i(y^2-4)|<\epsilon\\
0<|\sqrt{x^2+(y-2)^2}|<\delta \Rightarrow \sqrt{(2x)^2+(y^2-4)^2}<\epsilon\\\to \\\sqrt{(2x)^2+(y^2-4)^2}=\sqrt{4x^2+(y+2)^2(y-2)^2}<2\sqrt{4x^2+16(y-2)^2}=\\2.2\sqrt{x^2+4(y-2)^2}<4\sqrt{4x^2+4(y-2)^2}=8\sqrt{x^2+(y-2)^2}<\epsilon\\$$so
$$\sqrt{x^2+(y-2)^2}<\dfrac {\epsilon} {8}$$and you can take $\delta <\dfrac {\epsilon} {8}$
Comment: How is $y+2$ less than $4$?  $y$ is not less than $2$.
Comment: $2.2$ means $2\times2$.?
Comment: Yes I usually use "." for the meaning of $\times$


How can I handle date-dependent status activation on a DB?

Question: My problem is two-fold. I'm trying to handle it as elegantly as possible.
Give a date of the below format,
 YYYYMMDD hh:mm:ss

how can I find the corresponding date a variable number of days away? For example, if given a date, I need to be able to find the corresponding date of that format "x" days away from then.
This generated date is what I would store in an activation_date column in the database. Then I want to query the database nightly to see if it is that date yet. If so, I would change the status of an item in a different column from pending to active.
This seems like the best way to handle my problem but it doesn't seem like it will be very straightforward to solve.
The alternative would be that I create a days_till_activation column in the DB and have a script that nightly decrements the value in that column for each entry until zero is reached. Once zero is reached, the script would change the status. The problem with this is that is seems like a really poor way to deal with the database. It involves a lot of writing to the database rather than doing it only when required.
Anyway, any thoughts would be appreciated. I need to handle the solution in java but pseudocode would be fine.
Comment: Do you want `days` difference between current day and the date stored?
Answer:
Give a date of the below format,
YYYYMMDD hh:mm:ss

That's your first problem. Don't think of dates in a particular format. Make sure your database schema uses an appropriate date/time field type, and use an appropriate type in your Java code too. You should never need to see a string representation unless you're showing it to the user.
Fortunately, this part is easy:

how can I find the corresponding date a variable number of days away?

I would suggest using Joda Time to represent your date and time values as far as possible. That makes it easy:
LocalDate date = ...;
LocalDate activation = date.plusDays(activationDays);

You could just use java.util.Date/Calendar for this, but I would suggest converting to Joda Time types as soon as you can when you extract data from your database, and convert back to the native Java types at the last possible moment. That way you can avoid the hideousness of the Java types for as much of the time as possible.
Comment: +1 for pointing out using `date/time` fields in database and in `java` code..and also for pointing at Joda Time..it is really a way out from lots of Date\Time problems
Answer: In situations like this I would opt for an integer type column holding the expiration timestamp (currentTimeMilis() + x * MILLIES_PER_DAY) making your query very easy (where expiration < ? binding the parameter to currentTimeMilis().)
Chosing for a date/time type column adds complexity to your solution making the behaviour dependent on the database install (OS language changes can make a difference.)


How to estimate variance of underlying noise?

Question: I am a beginner to Machine Learning in Python. I have been given set of corresponding values for y and x such that y is a polynomial of an unknown degree in x. Additionally, in the provided data, Gaussian noise of zero mean and an unknown variance has been added, so the y values that are given to me are actually y=f(x) + E (where E is the added Gaussian noise). Is there a way to find the variance for this underlying noise(for instance, any in-built function in python)? Any help would be appreciated.
Comment: First you need to estimate f, when you know it you can simply take the residual variance (assuming heteroscedasticity). If you can't learn f, not much can be done (consider extreme cases like f(x)=0 that leads to zero variance, or f(x) that returns a random value).
Comment: I have found the degree and the coefficients for which I get the best fit.
Comment: The best fit is not a good criterion to select the degree. You will [overfit](https://en.wikipedia.org/wiki/Overfitting).
Answer: If your model is
$$
y_i = f(X_i) + \varepsilon_i
$$
with Gaussian noise $\varepsilon_i$ having mean zero and unknown variance $\sigma^2$, then it translates to
$$
y_i | X_i \sim \mathcal{N}(f(X_i), \sigma^2)
$$
then to calculate the variance of residuals (there is a distinction between error and residuals), you would first need to estimate the mean of the conditional distribution $E(Y|X) = f(X)$, since variance is the squared distance from the mean. To find $E(Y) = E_X(E_Y(Y|X)) = E_X(f(X)) \approx \hat f(X)$. Then you can simply substitute
$$
\newcommand{Var}{\mathrm{Var}}
\Var(Y) = E[(Y - E(Y))^2]
$$
with
$$
\widehat{\Var}(Y) = \mathrm{mean}((Y - \hat f(X))^2)
$$
where $\hat f(X)$ is your estimate of $E(f(X))$.
TLDR; You need to  estimate $f(X)$ and then calculate the variance of residuals and this will be your estimate of the variance.
Comment: Do you assume that $Var(Y) = \sigma^2$? If so, I fail to see how that is. My understanding that the OP needs to know how to find an estimate for $\sigma^2$.
Comment: @rando no I'm not, but the question does not ask about the unconditional variance of $Y$.
Comment: I haven't really caught on how to estimate $\sigma$ based on $Var(Y)$?


Devemos evitar dependências circulares?

Question: Quando trabalhamos com AngularJS é muito comum termos aplicações onde os services contenham apenas chamadas HTTP para o servidor que irá armazenar os dados. Porém, numa aplicação onde não teremos um servidor nosso service poderá ser responsável por muitas lógicas de negócio (não se preocupem em o código ficar visível para o usuário) e devido a isso um service poderá chamar outro e vice versa (isso é má pratica).
Imaginem a seguinte situação:
angular.module('app').service('logAPI',[ function('fileManager') {

    var _logList = [];

    var LogMessage = function(system, type, message, value, now) {
        this.system = system;
        this.type = type;
        this.message = message;
        this.value = value;
        this.time = now;
    };

    this.scheduleLogMessage = function(system, type, message, value) {
        if (_logList.length < 300) {
            var now = utilAPI.getFormattedTime();
            var log = new LogMessage(system, type, message, value);
            _logList.push(log);
        }
    };

    this.processLog = function() {
        if (_logList.length >= 200) {
            fileManager.upload(_logList[0]);
        }
    };

}])

.service('fileManager',[ function('storageAPI') {

    var upload = function(obj) {
        storageAPI.upload(obj).then(function success() {
            logAPI.scheduleLogMessage('a', 'a', 'a', 'a');
        }, function error() {
            logAPI.scheduleLogMessage('a', 'a', 'a', 'a');
        })
    }

}])

.controller('appCtrl', ['', function('logAPI'){

    while(true) {
        logAPI.processLog();
    }

}]);

O angularJS acusará de dependência circular. Existem meios de burlar isso e o angular 'ignorar' essa dependência, porém, isso é uma má prática ? Devemos evitar?

Edição: Melhora do exemplo

Meu logAPI é responsável por gerenciar todo log gerado pelo sistema e depois de uma quantidade X de mensagem, ele começa a fazer o upload desses logs. Quando o upload é feito, ele também pode gerar log para registrar o oque aconteceu. Em geral, tudo no meu sistema gera LOG e por causa disso, eu não consigo pensar numa maneira de quebrar essa dependencia.
Comment: Tlz ajude, [Gerenciamento de Memória](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Memory_Management)
Comment: Fiz alteração da resposta. Verifique se atende o que você precisa.
Answer: A melhor maneira que conheço de evitar referência circular, é criar um módulo à parte, onde fiquem as regras de negocio que são comuns a um ou mais serviços. Não levando apenas ao mundo do Angular.
No blog do Misko Hevery, um dos autores do angular, ele cita exatamente isso.

Segue o link para consulta:
Blog
Edição:
A ideia de um logger é que ele seja como um container de logs e ele se responsabilize por logar tudo.
Já o seu fileManager tem que dar respostas do que ele fez e não logar.
Sendo assim, podemos usar promises dentro do fileManager e devolver uma resposta à logApi, que é responsável por logar.
A estrutura abaixo é mais ou menos um esboço do que queremos. (Desculpe-me o desenho, tive q fazer no paint)

E aqui segue um JSFiddle com um exemplo para você se basear.
Quando clicar no botão "LOGAR" do exemplo, abra o console do navegador
Espero que o ajude
Comment: Sim, no caso vc deve retornar o proprio `$http.get`, que já é uma promise, veja o exemplo http://jsfiddle.net/4awmh27d/3/
Comment: Baseado na minha opinião sobre referência circular, fui pesquisar e em um link do stackoveflow americano foi onde encontrei o blog do Misko Hevery. Achei muito válido colocar aqui.
Comment: Fiz as edições baseado no que você alterou.
Comment: Acho que sua solução vai funcionar para mim, porém, tenho algumas dúvidas. 1. Como você acessa {{vm.resultado}} sendo que no seu controller você não tem nenhum $scope.vm.resultado, 2. O que você retorna de fileManager é uma promise?
Comment: 1. Na função MainController, atribuo uma variável `self`, que representa o controller. Isso me faz evitar ter que acessar o `$scope`. Na verdade é uma técnica de `Binding`, que, em outras linguagens de programação, é implicita. Tem mais informações sobre isso aqui: http://alistapart.com/article/getoutbindingsituations

2. Sim
Comment: Vou testar aqui, gostei da solução. Obrigado pela ajuda!
Comment: Disponha! Estamos todos aqui pra isso. Abraço
Comment: Da pra retornar uma promise de uma chamada http? tipo, ali tu retornou uma promise da chamada de uma função ali, mas e em vez da promise, eu tivesse que ainda fazer um http.get, como ficaria? Eu tentei testar aqui mas não rolou, https://jsfiddle.net/4awmh27d/2/

Devemos evitar dependências circulares?


Redirect nginx to another domain with authorization header

Question: I need to redirect request from nginx to another domain, below is a working solution for redirect request but it doesn't redirect Authorization header from original request, is it possible to redirect to another domain with all request headers from original request?
location /test {
    return 301 https://test.com$request_uri;
}

Comment: You can proxy_pass the request
Comment: @miknik thanks, that worked, can you write it as an answer so I can accept it?
Answer: Instead of using the return directive, which will issue a redirect to the client browser you can use the proxy_pass directive instead which will proxy the original client request to the remote server with the request headers intact.


I want to record a sound in android phone for 6 seconds with sampling rate of 8000hz in .wav

Question: if(checkSelfPermissionFromDevice() )
    {
        btnRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pathSave= Environment.getExternalStorageDirectory()
                        .getAbsolutePath()+"/"
                        + UUID.randomUUID().toString()+"_audio_record.wav";
                mediaRecorder.setAudioChannels(1);
                mediaRecorder.setAudioSamplingRate(8000);
                mediaRecorder.setAudioEncodingBitRate(44100);
                setupMediaRecorder();

                try
                {
                    mediaRecorder.prepare();
                    mediaRecorder.start();
                }
                catch (IOException e)
                {
                    e.printStackTrace();

                }
                btnPlay.setEnabled(false);
                btnStop.setEnabled(false);

                Toast.makeText(MainActivity.this, "Recording...", Toast.LENGTH_SHORT).show();
                //add delay of 6 seconds+then stop recording

                //Toast.makeText(MainActivity.this, "Stopped Recording", Toast.LENGTH_SHORT).show();

                //then enable  play button()
                btnPlay.setEnabled(true);
                btnStop.setEnabled(false);

I want to add 6 seconds delay so that I can stop recording after that or only to record for 6 seconds using builtin function by passing parameters please guide...
Answer: You could use a android.os.Handler to run a some code with 6 seconds delay using the postDelayed function. Handler Documentation
Example code:
Handler h = new Handler();
h.postDelayed(() -> {
    // stop the recording here
}, 6 * 1000);

A crucial point is, that you shouldn't pause the main thread for 6 seconds with something like Thread.sleep() because then the user experience would suffer and the android system will show a dialog that the app is unresponsive. Therefore you should schedule the stopping time (i.e. with a Handler) and let the main thread run in the mean time, such that the UI is updated and responsive.
Comment: I'd guess that wav is not supported as it is not mentioned in the [documentation](https://developer.android.com/reference/android/media/MediaRecorder.OutputFormat) but I am by no means an expert in this domain.
Comment: Can I save my output file in .WAV? like         mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.WAV);
MediaRecorder doesn't support  .wav I guess
Comment: is there any way like we use library to convert on the runtime to .wav?


Hive support for subquery in Join condition?

Question: Error: Error while compiling statement: FAILED: SemanticException in encountered with 0 children (state=42000,code=40000)
Do I need to find a solution to getting the subquery out of the on condition?
select
-- a bunch of stuff min,max,sum and case statements
from tbl0 t0
inner join tbl4  t4  on (t4.aKey = t0.aKey)
left outer join tbl1  t1  on (t0.col0 = t1.col0 and t1.someKey in (select t3.aKey from tbl3 t3 where t3.someCode in ('A1','A2','A3')))
where
not(t4.aCode in ('string1' , 'strin2' , 'string3' , 'string4') and t1.someKey is null) and not (t4.bCode in ('string1' , 'string2') and t1.someCol = 0)

Comment: collect data of all the sub query in some other table and put it in join. If it works for your usecase.
Comment: t4? ...........
Comment: @DuduMarkovitz there are 26 joins, i only put the ones in that are causing the issue as it can get a little crazy...The query is 192 lines in sublime...And yes it serves a unique purpose and is run once an evening.  It's an aggregate for ETL replacement...Also why the names are funky don't want to use vendor logic/naming
Comment: This wasn't a style remark. As you noticed, `t4` was used not defined in your query.
Comment: @DuduMarkovitz yup apologies i threw it in to avoid confusion going forward.
Answer: I'm also getting this error in Hive 1.1.
I think Ashish Singh likely has the best suggestion: change tbl1 to a subquery by moving the IN (SELECT ...) statement inside of that table definition.
Your query would then become:

select
-- a bunch of stuff min,max,sum and case statements
from tbl0 t0
inner join tbl4  t4  on (t4.aKey = t0.aKey)
left outer join (SELECT col0 FROM tbl1 WHERE somekey IN (SELECT t3.aKey FROM tbl3 t3 WHERE t3.someCode in ('A1','A2','A3'))) t1
  on (t0.col0 = t1.col0)
where
not(t4.aCode in ('string1' , 'strin2' , 'string3' , 'string4') and t1.someKey is null) and not (t4.bCode in ('string1' , 'string2') and t1.someCol = 0)


Show that there exists a measurable function $g$ with $|g| \leq 1$ so that $\int_E g d\nu = |\nu|(E)$ for all measurable sets $E$.

Question: Let $(\mathcal{X},\mathcal{M},\nu)$ a measure space where $\nu$ a signed measure. Show that there exists a measurable function $g$ with $|g| \leq 1$ so that $\int_E g d\nu = |\nu|(E)$ for all measurable sets $E$.
Here's my attempt.
Let $E$ a measurable set. Then $|\nu|(E) = sup\sum_{j=1}^\infty |\nu(E_j)|$. Let $\epsilon >0$ Then i can always find a partition $E=\bigcup_{i=1}^\infty G_i$ s.t. $\sum_{i=1}^\infty |\nu(G_i)| \geq |\nu|(E)-\epsilon$. Let $\chi'_{G_i} = sign(\nu(G_i))\chi_{G_i}$ where $\chi_{G_i}$ the characteristic function of set $G_i$. Let $g=\sum_{i=1}^\infty \chi'_{G_i}$. Then $\int_E gd\nu=\sum_{i=1}^\infty |\nu(G_i)|$. Thus $|\nu|(E)\geq \int_E gd\nu \geq |\nu|(E) -\epsilon$, and since e is chosen arbitrary $\int_E gd\nu = |\nu|(E)$.
Is my approach correct? Thank you very much for your help!
Comment: No, it doesn't work this way. Your function $g$ depends on $\epsilon$, i.e. you show that there exists $g_{\epsilon}$ such that $$|\nu|(E) \geq \int g_{\epsilon} \, d\nu \geq |\nu(E)|-\epsilon.$$ Therefore, you cannot conclude $\int_E g \, d\nu = |\nu|(E)$.
Comment: Could anyone suggest a way to solve this ?
Answer: As I already pointed out in a comment, your proof is not correct because the function $g$ does depend on $\epsilon$ and $E$. The following counterexample shows that the claim does not even hold true:
Consider $(\{-1,1\},\mathcal{P}(\{-1,1\}))$ endowed with the measure $$\nu(dy) =\delta_1(dy) - \delta_{-1}(dy).$$
Suppose that there exists a function $g$ such that $\int_E g \, d\nu = |\nu|(E)$ for all measurable sets $E$. For $E=\{\pm 1\}$, we have
$$g(\pm 1) \stackrel{!}{=} |\nu|(\{\pm 1\})=1,$$
i.e. $g(1) = g(-1) = 1$. On the other hand, for $E := \{-1,1\}$,
$$\int_E g \, d\nu = 1 \cdot g(1) + (-1) \cdot g(-1) = 0$$
does not equal
$$|\nu|(E)=2.$$
Remark: There exists a function $g$ such that $$\nu(E) = \int_E g \, d|\nu|.$$ This follows directly from the fact that $\nu$ is absolutely continuous with respect to $|\nu|$ and the Radon-Nikodým theorem.


Convention for stacking k matrices together, is result k-by-..-by.. or ..-by-..-by-k?

Question: If I have an k mxn matrices, and I stack them together, should the result kxmxn or mxnxk? In numpy it's the former, is this a common convention?
IE,
numpy.array([matrix1, matrix2, matrix3])

will give (3,x,x) array
Comment: The `numpy.linalg` gufuncs take `k` stacked inputs as `(k, ..., ...)`, see e.g. [`np.linalg.inv`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.linalg.inv.html), which is probaby the closest to a convention that it is going to get.
Answer: np.array isn't a 'stacking' function.  What you demonstrate is a particular consequence of how it operates.  numpy also has concatentate and its special cases hstack, vstack, and dstack.  2 of those don't add a dimension, and the 3rd adds it at the end.
np.array behaves like np.concatenate([matrix1[None,...], matrix2[None,...]]).  This is, I think, a consequence of a broader tendency to pre-pend dimensions as needed, for example in
np.array(matrix1, ndim=3)  # (1,x,x) shape

In MATLAB/Octave,
[matrix1, matrix2, matrix3]  # like np.hstack
cat(2, matrix1, matrix2, matrix3]   # also horzcat
[matrix1;matrix2]  # np.vstack
cat(1, matrix1 ...)  # also vertcat
cat(3, matrix1 ...)  # like np.dstack

Adding a dimension at the end comes natural to Octave.  Adding a dimension at the start is more awkward in Octave.  Best I can think of, off hand, is
[reshape(matrix1,1,x,x); reshape(matrix2,1,x,x)]

Comment: Thanks for examples. Another place where numpy prepends dimensions is when broadcasting. IE, np.ones((3))+np.ones((2,3)) works, but np.ones((2))+np.ones((2,3)) fails. Couldn't find any other systems that prefer this "prepend on the left" convention, wonder where it came from.
Comment: pre-pend is more natural with `C` ordering, post-pend with `F` ordering.  MATLAB/Octave started as a wrapper for FORTRAN functions.  `numpy` has had broadcasting for a long time.  Octave (and Matlab?) added it more recently.  Initially it was limited to 2d (back in Matlab 3.5 days), while `numpy` has been multi-dimensional all along.  `APL` is an even older array language, but I don't recall how it handled this 'stacking'.


The entertainer — two hands not enough for this part?

Question:
Am I supposed to hold the chord with the right hand and also play the melody with the right hand?
Comment: I dont have my joplin at hand now, but i can't remember to have seen this in my scores.  Is this for sure Urtext?
Answer: There's a pedal on the right just for this kind of scenario! Since the harmony doesn't change throughout the bar, it's o.k. to press that pedal just after you play the chord, to keep it going through the bar. Then lift the pedal just as you play the first notes in the following bar.
If you happen to have a piano with three pedals, and the middle one is a sostenuto pedal (rather than a practice pedal) that will do the job as well - possibly better, as you will elongate only the chord in question. Along with the bass note, of course!
Knowing where in the world you are would make the answer make more sense, regarding the middle pedal.
Comment: I understand that the middle pedal does different things on different pianos (mutes the piano or sustains only specific notes) — though I'm on a digital piano with just a sustain pedal, so not many options for me :/
Comment: There are quite a few digitals that have a switchable system - the 'soft' pedal on a few of mine can be routed to be sotenutos. No need for a soft pedal, though - just drop the volume...
Comment: Some uprights treat the middle pedal as a bass-only damper pedal, which is simpler mechanically than a sostenuto but would work well for this kind of thing.  I wonder what Joplin's piano did?


Given a subgroup $H$ and a normal subgroup $N$, does the normal closure of $H$ in $HN$ intersect $N$ in the normal closure of $H \cap N$ in $N$?

Question: Let $G$ be a group, $N$ be a normal subgroup of $G$, and $H$ be any other subgroup of $G$.    Write $\mathrm{ncl}_G(H)$ for the normal closure of $H$ in $G$.
Is it true that $\mathrm{ncl}_{NH}(H) \cap N = \mathrm{ncl}_{N}(H \cap N)$?
Evidently $\mathrm{ncl}_{NH}(H) \cap N \supseteq \mathrm{ncl}_{N}(H \cap N)$, but the reverse inclusion seems unlikely.  Still, none of the counterexamples I've tried to construct have quite panned out.
Comment: You mean the normal closure of $H \cap N$ in $N$, not in $H$, since $H \cap N$ is clearly already normal in $H$.
Comment: Exactly right!  Thanks for flagging.
Answer: No. In order for this to be false, $G$ would have to be non-abelian. So let $G=S_3$. Let $N$ be generated by $(1,2,3)$ and $H$ by $(1,2)$. Then $HN=G$, and the normal closure of $H$ in $HN$ is $G$. This clearly intersects $N$ in $N$. But $H\cap N=1$. So the normal closure of $H\cap N$ in anything is $1$.
Comment: Wonderful!  This is perfect, thank you!!!  (don't have enough points to up-vote, but would if I could!).
Comment: You can accept the answer by clicking the check mark $\checkmark$ next to it, @Greg.
Comment: Ah, great.  Thanks!


Boot.ini on Windows Server 2003 R2

Question: I have a Windows Server 2003 R2 with 48 GB of RAM; server has been running strong for quite some time. Recently our boot.ini was modified causing issues, most likely by our remote administrators. Now the server is only showing 14 GB of RAM. This has caused major performance issues for our end users.
Our remote administrators have stated "we don't change the boot.ini settings(switches)". However, I know for a fact that all of the local administrators have not modified the switches (due to lack of permissions).
The real question.. Is it possible to "audit" who has modified the boot.ini? If thats not possible, can the boot.ini be set via startup?
Any help would be greatly appreciated. This is an ongoing issue that I would love to resolve.
Answer: Is it possible to "audit" who has modified the boot.ini?

Not in hindsight, no. There are a ways of auditing in future, but unless you have previously configured some kind of monitoring then, no, this will just remain one of those mysteries.
If thats not possible, can the boot.ini be set via startup?

Please clarify what you mean by this as it doesn't really make sense.
Comment: I'm trying to narrow down their "genius" responses. Can the boot.ini be modified during startup to revert back to the previous settings.
Comment: The boot.ini file is a text file. There's no such thing as "revert back to previous settings". You need to know what the contents of the file should be to know whether it's been changed or not. If the file was backed up you could look at it from a backup from before the problem and look at it now and make a comparison.
Comment: I understand its a text file, I have the previous settings and current incorrect one. I'm trying to narrow down why this is "reverting" back to the old settings. I'm sure its the remote admins but I had to ask if there was some crazy way it could be modified.

Boot.ini on Windows Server 2003 R2


feature importance using neural network

Question: Is it a good practice to find the absolute value of the sum of weights of the features on the first hidden layer in neural network to find the importance of features using neural network ?
Answer: Say that all of our features have value 1.  Give features one and two weights 3 and 1, respectively--they lead to node A where they activate with 1*3+1*1=4. We also have features three and four with weights 2 each--they lead to node B and activate with 1*2 + 1*2=4.  In the next layer, node A has weight 0.4 and node B has weight 0.6.  Is feature one more important than both features three and four?
What if there are 7 more layers?
Often, neural networks are used in a setting where features interact so much that the concept of importance is not really clear (e.g., pixel data).  There is however a lot of work on interpreting neural networks.
As far as feature importance; if the features truly have distinct importances, it might be worth using a different classifier to see it (e.g., LASSO).
With a neural network, possibly one could shuffle each feature and see what happens to predictive performance?  This is one way of doing it for random forests.  I have seen some recent papers where the authors, I think, kind of masked features and checked the effect.  Another option suggested here is to calculate the gradient with respect to the inputs.
Comment: but the node A and node B are combination of original features rather than the actual features, so why the weights of nodes will decide the actual features importance?
Comment: A feature is more important if it has a substantial effect on the output of the network (and therefore relationship with the target).  Those combinations are closer to the output than the original features.
Comment: Okay. Thanks. Is there other way of getting features importance from the neural network? how about getting the cumulative weights of each feature across all the layers?
Comment: Perhaps see `Garson, G. David. "Interpreting neural-network connection weights." AI Expert 6.4 (1991): 46-51.`  In my opinion, cumulative weights would be difficult because features are transformed in each layer, so not sure how one could "track" a feature from input to output.

feature importance using neural network


Question about property of 2-increasing, grounded function with margins

Question: The question is about what may very well be an obvious detail in the proof of a lemma from the book An Introduction to Copulas by Roger Nelsen. I will state all the relevant results and definitions below. My question is about a detail in the proof of lemma 2.1.5. Please excuse the lengthy post.
Definition (Several in one)
A 2-place real function $H$ is a function whose domain, Dom$H$, is a subset of the Cartesian product of the extended real numbers, and whose range, Ran$H$, is a subset of the real numbers.
Let $S_{1}$ and $S_{2}$ be nonempty subsets of the extended real numbers, and let $H$ be a two-place real function such that Dom$H = S_{1}\times S_{2}$. Let $B = [x_{1}, x_{2}]\times [y_{1}, y_{2}]$ be a rectangle all of whose vertices are in Dom$H$. Then the H-volume of B is given by $$V_{H}(B) = H(x_{2},y_{2})-H(x_{2},y_{1})-H(x_{1},y_{2})+H(x_{1},y_{1})$$.
A 2-place real function is 2-increasing if $V_{H}(B)\geq 0$ for all rectangles $B$ whose vertices lie in Dom$H$.
Lemma 2.1.3
Let $S_{1}$ and $S_{2}$ be nonempty subsets of the extended real numbers and let $H$ be a 2-increasing function with domain $S_{1}\times S_{2}$. Let $x_{1}, x_{2}$ be in $S_{1}$ with $x_{1}\leq x_{2}$, and let $y_{1}, y_{2}$ be in $S_{2}$ with $y_{1}\leq y_{2}$. Then the function $t \mapsto H(t,y_{2}) - H(t,y_{1})$ is nondecreasing on $S_{1}$, and the function $t \mapsto H(x_{2},t) - H(x_{1},t)$ is nondecreasing on $S_{2}$.
Definition
Suppose $S_{1}$ has a least element $a_{1}$ and that $S_{2}$ has a least element $a_{2}$. We say that a function $H$ from $S_{1}\times S_{2}$ into the real numbers is grounded if $H(x,a_{2}) = 0 = H(a_{1},y)$ for all $(x,y)$ in $S_{1}\times S_{2}$.
Lemma 2.1.4
Let $S_{1}$ and $S_{2}$ be nonempty subsets of the extended real numbers, and let $H$ be a grounded 2-increasing function with domain $S_{1}\times S_{2}$. Then $H$ is non-decreasing in each argument.
Definition
Suppose $S_{1}$ has a greatest element $b_{1}$ and that $S_{2}$ has a greatest element $b_{2}$. We say that a function $H$ from $S_{1}\times S_{2}$ into the real numbers has margins, and that the margins of $H$ are the functions $F$ and $G$ given by:
Dom$F$ = $S_{1}$, and $F(x) = H(x,b_{2})$ for all $x \in S_{1}$;
Dom$G$ = $S_{2}$ and $G(y) = H(b_{1},y)$ for all $y\in S_{2}$.
Lemma 2.1.5
Let $S_{1}$ and $S_{2}$ be nonempty subsets of the extended real numbers, and let $H$ be a grounded 2-increasing function, with margins, whose domain is $S_{1}\times S_{2}$. Let $(x_{1}, y_{1})$ and $(x_{2}, y_{2})$ be any points in $S_{1}\times S_{2}$. Then
$|H(x_{2},y_{2})-H(x_{1},y_{1})|\leq |F(x_{2}-F(x_{1})| + |G(y_{2})-G(y_{1})|$.
My questions concerns a detail in the following proof.
Proof. From the triangle inequality, we have
$|H(x_{2},y_{2})-H(x_{1},y_{1})|\leq |H(x_{2},y_{2})-H(x_{1},y_{2})| + |H(x_{1},y_{2})-H(x_{1},y_{1})|$.
Now assume $x_{1}\leq x_{2}$. Because $H$ is grounded, 2-increasing, and has margins, lemmas 2.1.3 and 2.1.4 yield $0\leq H(x_{2},y_{2})-H(x_{1},y_{2})\leq F(x_{2})-F(x_{1})$...
This last inequality is what caused my confusion. As I understand it, $F(x_{2})\geq H(x_{2},y_{2})$, but we also have $F(x_{1})\geq H(x_{1},y_{2})$, i.e. $-F(x_{1})\leq -H(x_{1},y_{2})$ , so that we cannot simply add/subtract the left hand sides and right hand sides of the inequalities. If my understanding is correct, the inequality in question must be estimated differently, but so far I have not been successful in doing so. This has been bugging me for a while because this seems like it should be so simple, given that one has understood the relevant definitions and prior results. Therefore, this causes me to believe that I may have misunderstood some part of the relevant information I provided. Any help would therefore be much appreciated. Thanks in advance to all that take the time to read this rather long entry.
Answer: I think the following answers the question: Rewriting the condition for being 2-increasing, we get that $H(x_{2},y_{2})-H(x_{1},y_{2}) \geq H(x_{2},y_{1})-H(x_{1},y_{1})$, which holds for any $x_{2}\geq x_{1}$ and $y_{2}\geq y_{1}$. In particular, since $H$ has margins, we know there exists a greatest element in $S_{2}$, denoted $y^{*}$. By the definition of margins, we get $F(x_{2}) = H(x_{2},y^{*})$ and $F(x_{1}) = H(x_{1},y^{*})$. By the rewritten condition for $H$ being 2-increasing, this implies that $F(x_{2})-F(x_{1})\geq H(x_{2},y_{2})-H(x_{1},y_{2})$.


Find all natural numbers $n$ such that $2^n$ divides $3^n -1$

Question:
Find all natural numbers $n$ such that $2^n$ divides $3^n -1$

I think that the only solutions are $n = 0,1,2,4$, but I have no idea on how to prove it.
I tried to write $3^n-1$ as $1+3+3^2+...+3^{n-1}$ and manipulate the sum but found my self at the equally hard problem of finding the power of two dividing $3^k+1$
Comment: https://math.stackexchange.com/questions/1252716/when-does-2n-divide-3n-1 and links to LTE lemma there : https://artofproblemsolving.com/community/c6h324597
Comment: Induction: base case $n=0$ we have $1$ divides $0$, second case (optional base case) $n=1$ we have $2$ divides $2$. Now consider $n+1$. We have $2^{n+1}=2^{n}2.$ We also have $3^{n}3-1.$ Now we know that an odd number squared will always be odd, and multiplied by an odd number will also stay odd. But an odd number subtracted by $1$ is always even and therefore divisible by $2$.
Answer: Well $3^n - 1 = (3-1)(1 + 3 + 3^2 + ... + 3^{n-1}) = 2(1 + 3 + 3^2 + ... + 3^{n-1})$
So $2^n|3^n - 1$ if and only if $2^{n-1}|(1 + 3 + 3^2 + ... + 3^{n-1})$.
If $n$ is odd and greater than one $(1+3 + 3^2 + .... + 3^{n-1})$ is odd so we can assume $n$ is even.
Let $n = 2m$ then $2^{2m}|3^{2m} - 1=(3^m -1)(3^m+1)$.  So $3^m \pm 1$ are both even and only one of them is is divisible by $4$.
So $2^{2m-1}|3^m \pm 1$ so $2^{2m-1} \le 3^m \pm 1$.
But $2^{2m-1} = \frac 12*4^{m} \le 3^m \pm 1$
So $(\frac 43)^m \le 2 \pm \frac 2{3^m} < 2\frac 23$
If $m \ge 3$ then $(\frac 43)^m \ge 2 \frac {10}{27} > 2 \frac 2{3^3}\ge 2 + \frac 2{3^m}$
So $m < 3$
So if $n > 1$ then $n= 2m; m\le 2$.
So solutions must be a subset of $\{0,1,2,4\}$.
And you have already determined that $\{0,1,2,4\}$ are all solutions.
====
There's probably a more elegant way.
My first thought was FTL that as $\gcd(3, 2^n) = 1$ and $\phi(2^n) = 2^{n-1}$ then $3^{2^{n-1}}\equiv 1 \mod 2^n$. So
If $3^m \equiv 1 \mod 2^n$ then $m$ is a multiple of a non-trivial factor of $2^{n-1}$ .i.e. even
but that didn't really get me closer.
Likewise $3^n = (2 + 1)^n = 2^n + \sum{n\choose k} 2^k$ and for $2^n|\sum{n\choose k} 2^k$ seemed like it should yeild something relevent but I wasn't able to put my finger on it exactly.
Similarly $3^n = (4 -1)^n$.
Its a enough to convince me the answers are related to powers of $2$ but not enough to actually prove it.
Answer: The result is clear for $n = 0$. For $ n = 1, 2, 3 \ldots$ let the highest power of $2$ that divides $3^n - 1$ be $2^{p(n)}$. If $n$ is odd, say $n = 2m + 1$, then $3^n - 1 = (3 - 1)(3^{2m} + 3^{2m - 1} + \cdots + 1)$. The sum has an odd number of terms, so $p(n) = 1$. If $n$ is even, say $n = 2m$, then $3^n - 1 = (3^m - 1)(3^m + 1)$. By induction, $3^m \equiv 1 \mod 8$ if $m$ is even and $3^m \equiv 3 \mod 8$ if $m$ is odd. Hence $p(2m) = p(m) + 1$ if $m$ is even, $p(m) + 2$ if m is odd. By applying this repeatedly we get that if $n = 2^ab$, where $a > 0$ and $b$ is odd, then $p(n) = a + 2$. It follows easily that for $n > 0$ we have $p(n) \ge n$ iff $n = 1, 2, 4$.
Answer: Let $\nu_2(n)=\max\{m\in\mathbb{N}: 2^m\mid n\}$. We may tackle the given problem by finding an explicit form  for (or, at least, an explicit recursive algorithm for the determination of) $\nu_2(3^n-1)$. By letting $a_n=3^n-1$ we have
$$ a_{2k+1} = 3\cdot 9^k-1\equiv 2\pmod{8}$$
from which $\nu_2(a_{2k+1})=1$, and
$$ a_{2k} = a_k (3^k+1) $$
from which $\nu_2(a_{2k})=\nu_2(a_k)+\nu_2(3^k+1)$. On the other hand
$$ 3^{2m+1}+1 = 3\cdot 9^m+1 \equiv 4\pmod{8} $$
$$ 3^{2m}+1 = 9^m+1\equiv 2\pmod{8} $$
so $\nu_2(3^{2m+1}+1)=2$ and $\nu_2(3^{2m}+1)=1$.  In particular
$$ \nu_2(a_n) \leq 1+2\log_2(n) $$
and the only solutions of $2^n\mid (3^n-1)$ are associated to $n\leq 7$, hence they can be found by direct inspection. In explicit terms,
$$ \nu_2(a_n) = 2+\nu_2(n)-\mathbb{1}_{\text{odd}}(n).$$
Answer: I write this with two ad-hoc invented notations:

define $[n:m]=1$ if $m|n \qquad$ and $[n:m]=0$ if $m\not |n \qquad$              ("Iverson-brackets")
define $\{n,m\} = A $ if $n$ contains $m$ to the power of $A$; or "$n=x \cdot m^A$" (or "valuation" $v_m(n)$)

Evaluating Fermat's little theorem and Euler's totient theorem and the concept of "order of cyclic subgroups modulo prime" we can find:
 $$ A=\{3^n -1,2\} = 1 + [n:2] + \{ n:2 \} $$
which means also, that $A$ is logarithmic in $n$ and the number of solutions must be finite and occur in small $A$ and $n$.
Actual enumeration of small cases gives
$$ \begin{array}{rr|l}
 n & 3^n-1 & A & (=1+ [n:2] + \{n,2\} ) \\ \hline
 0 & 0 & \;^\dagger & \;^\dagger  \\
 1 & 2 & 1 &  1 + 0 + 0 \\
 2 & 8 & 3 &  1 + 1 + 1 \\
 3 & 26 & 1 &  1 + 0 + 0 \\
 4 & 80 & 4 &  1 + 1 + 2 \\
 5 & 242 & 1 &  1 + 0 + 0 \\
 6 & 728 & 3 &  1 + 1 + 1 \\
 \vdots \\
\end{array}$$
( $\;^\dagger$: this is not defined here and might be assumed zero or infinite)
From $n=5$ the smaller increase in $A$ than that of $n$ comes into account  and thus no more equality $A=n$ can occur.
(For a more explicite discussion see a short text of mine)


Análise de cluster por grupos

Question: Estou a tentar uma análise de cluster para vários grupos dentro de um dataframe, com o objetivo de retornar as características desta análise (ex. os grupos resultantes da mesma) em um banco de dados através da função tidy (broom).
dput
dataset=structure(list(a = c(28L, 19L, 92L, 35L, 42L, 82L, 91L, 98L,
58L, 58L, 92L, 61L, 67L, 73L, 4L, 35L, 9L, 17L, 7L, 82L, 24L,
51L, 45L, 1L, 97L, 97L, 99L, 5L, 67L, 97L, 95L, 77L, 56L, 67L,
80L, 22L, 87L, 31L, 97L, 15L, 12L, 94L, 18L, 86L, 1L, 99L, 2L,
88L, 84L, 65L, 59L, 38L, 8L, 46L, 66L, 30L, 32L, 36L, 17L, 35L,
40L, 16L, 60L, 28L, 47L, 56L, 82L, 88L, 76L, 38L, 88L, 61L, 26L,
64L, 24L, 48L, 30L, 68L, 88L, 42L, 62L, 12L, 76L, 37L, 25L, 91L,
18L, 76L, 13L, 24L, 49L, 89L, 35L, 88L, 19L, 24L, 62L, 91L, 99L,
18L), b = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("group1",
"group2", "group3", "group4"), class = "factor"), c = c(61L,
28L, 82L, 38L, 22L, 79L, 7L, 12L, 73L, 78L, 17L, 28L, 30L, 11L,
99L, 47L, 42L, 51L, 13L, 16L, 35L, 51L, 92L, 41L, 45L, 27L, 17L,
37L, 27L, 53L, 23L, 50L, 81L, 25L, 93L, 11L, 80L, 35L, 32L, 9L,
56L, 18L, 17L, 63L, 49L, 11L, 26L, 93L, 45L, 7L, 43L, 90L, 31L,
80L, 53L, 66L, 62L, 13L, 54L, 7L, 20L, 37L, 79L, 52L, 35L, 8L,
6L, 46L, 35L, 3L, 18L, 82L, 92L, 80L, 8L, 87L, 89L, 20L, 26L,
86L, 29L, 55L, 46L, 83L, 66L, 25L, 17L, 68L, 21L, 83L, 26L, 97L,
54L, 71L, 19L, 6L, 20L, 86L, 83L, 8L)), class = "data.frame", row.names = c(NA,
-100L))

Tentei isso:
library(dplyr)
library(broom)

res1<-dataset%>%
group_by(b)%>%
do(cluster=
       kmeans(dataset[,c(1,3)],centers=3))

res2<-tidy(res1,cluster)

Mas não consigo o que quero (o dataframe resultante deveria ter 100 linhas, cada uma com o seu respectivo grupo, derivado da análise). Existe um erro no meu código, ou, esta função não é adequada para executar esta ação.
Answer: Esta função não é adequada para esta ação, pelo menos não do jeito que está sendo usada aqui. O truque é usar a função nest do pacote tidyr:
library(dplyr)
library(tidyr
cluster <- dataset %>%
  nest(a, c) %>%
  mutate(model = map(data, kmeans, 3),
         centers = map(model, tidy))

Com ela, eu consigo dizer como o R deve agrupar em uma lista (neste caso, uma lista com levels(dataset$b) elementos) as colunas que me interessam para fazer a minha clusterização. Depois uso mutate para, de fato, encontrar a clusterização destes dados.
Veja que o resultado é condizente com o esperado:
cluster %>%
  unnest(centers)

        b       x1       x2 size withinss cluster
1  group1 82.62500 20.75000    8 2529.375       1
2  group1 56.50000 83.83333    6 5278.333       2
3  group1 24.36364 39.00000   11 4378.545       3
4  group2 86.40000 23.20000   10 2952.000       1
5  group2 13.25000 30.00000    8 2861.500       2
6  group2 81.57143 73.28571    7 2947.143       3
7  group3 40.20000 73.70000   10 4321.700       1
8  group3 76.50000 33.50000    6 2337.000       2
9  group3 33.33333 18.00000    9 3200.000       3
10 group4 87.25000 62.75000    8 5699.000       1
11 group4 31.62500 75.37500    8 2415.750       2
12 group4 37.00000 18.44444    9 4592.222       3

O problema é que ainda não temos o que realmente te interessa, que é o cluster ao qual pertence cada observação. Mas nós temos os centros de cada cluster. Assim, podemos prever, através da distância euclidiana, qual é o cluster de cada observação. Para isso, usaremos a função cl_predict do pacote clue:
dataset %>%
  filter(b=="group1") %>%
  select(-b) %>%
  cl_predict(cluster$model[[1]], .)

Class ids:
 [1] 3 3 2 3 3 2 1 1 2 2 1 1 1 1 2 3 3 3 3 1 3 3 2 3 1

Eu não consegui fazer esta predição para todos os modelos ao mesmo tempo. Para obter todas as 100 predições necessárias, teria que, de alguma maneira, fazer o cluster$model[[1]] variar, seja de alguma maneira tidy, seja usando um for.
Outra coisa que também não sei como fazer é clusterizar os dados com números de clusters diferentes por grupo. Foram procurados 3 clusters em todos os 4 grupos da variável b. Não sei se isto seria algo razoável de ser fazer na prática.
Mas estas duas tarefas deixarei a cargo do leitor : )

Análise de cluster por grupos


Why isn't my EM algorithm increasing the log-likelihood after each iteration?

Question: I used EM algorithm to estimate the parameters of the following time invariant process.
$$y=Ct+e_y$$
$$x=Pt+e_x$$
where $y \in R$, $x \in R^d$, $t\sim N(0,I^{p \times p})$, $e_y \sim N(0,\Sigma_y)$, and $e_x \sim N(0,\Sigma_x)$. Both $\Sigma_x$ and $\Sigma_y$ are diagonal covariance matrices. The set of parameters to be estimated include $\theta=(C,P,\Sigma_x, \Sigma_y)$. I first derived the joint probability density function $p(x,y,t)$ as following.
$$p(x,y,t)=N(Ct,\Sigma_y)N(Pt,\Sigma_x)N(0,I)\\=x^T\Sigma_x^{-1}x+y^T\Sigma_y^{-1}y-2t^T(C^T\Sigma_y^{-1}y+P^T\Sigma_x^{-1}x)+t^T(C^T\Sigma_y^{-1}C+P^T\Sigma_x^{-1}P+I)t$$
$$$$
Knowing that $p(t|x,y)\propto p(x,y,t)$,
$$Var[t|x,y]=(C^T\Sigma_y^{-1}C+P^T\Sigma_x^{-1}P+I)^{-1}$$
$$E[t|x,y]=(C^T\Sigma_y^{-1}C+P^T\Sigma_x^{-1}P+I)^{-1}(C^T\Sigma_y^{-1}y+P^T\Sigma_x^{-1}x)$$
$$Var[t|x,y]=E[(t-E[t|x,y])(t-E[t|x,y])^T]=E[tt^T|x,y]-E[t|x,y]E[t^T|x,y]$$
$$E[tt^T|x,y]=Var[t|x,y]+E[t|x,y]E[t^T|x,y]$$
$$$$
Now I have the required posterior distributions for the M-step, including $E[t] $ and $E[tt^T]$ (for conciseness $|x,y$ is omitted). In the M-step, considering the distributions for $y$ and $x$ are independent of each other, $P$, $C$, $\Sigma_x$ and $\Sigma_y$ can be maximized independent of each other as well. The complete log-likelihood function of the EM algorithm is derived as followings.
$$L(X=[x_n]_{n=1}^N;\theta)=\dfrac{N}{2}\ln|\Sigma_x^{-1}|-\dfrac{1}{2}trace[\Sigma_x^{-1} \sum_{n=1}^N(x_nx_n^T-x_nE[t_n^T]P^T-PE[t_n]x_n^T+PE[t_nt_n^T]P^T)]$$
$$L(Y=[y_n]_{n=1}^N;\theta)=\dfrac{N}{2}\ln|\Sigma_y^{-1}|-\dfrac{1}{2}trace[\Sigma_y^{-1} \sum_{n=1}^N(y_ny_n^T-y_nE[t_n^T]C^T-CE[t_n]y_n^T+CE[t_nt_n^T]C^T)]$$
Subsequently, taking the derivatives of the above equations with respect to each parameter and setting these derivatives to zero yield (derivation similar to equations 13 to 16 of http://mlg.eng.cam.ac.uk/zoubin/course04/tr-96-2.pdf):
$$C_{new}=(\sum_{n=1}^Ny_nE[t_n^T])(\sum_{n=1}^NE[t_nt_n^T])^{-1}$$
$$P_{new}=(\sum_{n=1}^Nx_nE[t_n^T])(\sum_{n=1}^NE[t_nt_n^T])^{-1}$$
$$\Sigma_x=\dfrac{1}{N}\sum_{n=1}^N(x_nx_n^T-P_{new}E[t_n]x_n^T)$$
$$\Sigma_y=\dfrac{1}{N}\sum_{n=1}^N(y_ny_n^T-C_{new}E[t_n]y_n^T)$$
This completes one step of EM. The joint likelihood of the data is computed as following, since $p(x)$ and $p(y)$ are independent of each other (not sure if it is correct).This log-likelihood should increase after each step of EM.
$$L(X,Y;\theta_{new})=N\ln(2\pi)^{-1/2}+N\ln(2\pi)^{-d/2}+N\ln|\Sigma_y+C_{new}C^T_{new}|^{-1/2}+N\ln|\Sigma_x+P_{new}P^T_{new}|^{-1/2}-\dfrac{1}{2}\sum_{n=1}^Nx_n^T(\Sigma_x+P_{new}P^T_{new})^{-1}x_n+y_n^T(\Sigma_y+C_{new}C^T_{new})^{-1}y_n$$
I have implemented this in matlab. The log-likelihood is decreasing after each step. I am attaching the code and the testing data with this question. Could anybody help me identify the problem?
function net=PPLS_EM(X,Y,p,iter,tol)

net=struct('type','PPLS_EM','C',[],'P',[],'Ex',[],'Ey',[],'mux',[],'muy',    [],'LL',[]);
[N,din]=size(X); %N by d input data matrix column [6 9 11 13 16 18] of the d00_te. data
[~,dout]=size(Y); %N by 1 output data matrix column 7 of the d00_te.dat

const1=(2*pi)^(-din/2);
const2=(2*pi)^(-dout/2);
lik=0;
LL=[];
tiny=exp(-700);

Vcur=zeros(p,p,N);
tcur=zeros(p,N);

YY=diag(sum(Y.*Y)');
XX=diag(sum(X.*X)');

if nargin<5
tol=0.0001;
end

if nargin<4
iter=100;
end

if nargin<3
p=2;
end

%% Initilization
C=ones(dout,p);
Ey=ones(dout,1);
Ey=diag(Ey);

P=ones(din,p);
Ex=ones(din,1);
Ex=diag(Ex);

Ex=Ex+(Ex==0)*tiny;
Ey=Ey+(Ey==0)*tiny;

Etxsum=0;
Etysum=0;
Vttsum=0;

for t=1:iter
%% E-step

oldlik=lik;
for n=1:N
    tcur(:,n)=inv(eye(p)+P'*inv(Ex)*P+C'*inv(Ey)*C)*(P'*inv(Ex)*X(n,:)'+C'*inv(Ey)*Y(n,:)');
    Vcur(:,:,n)=inv(eye(p)+P'*inv(Ex)*P+C'*inv(Ey)*C)+tcur(:,n)*tcur(:,n)';

    invx=inv(Ex+P*P');
    invy=inv(Ey+C*C');

    Etx=tcur(:,n)*X(n,:);
    Ety=tcur(:,n)*Y(n,:);

    Etxsum=Etxsum+Etx;
    Etysum=Etysum+Ety;

    Vttsum=Vttsum+Vcur(:,:,n);

    % calculate likelihood
    detiEx=sqrt(det(invx));
    detiEy=sqrt(det(invy));
    if (isreal(detiEx) && detiEx>0)
        lik=lik+log(detiEx)-0.5*sum(sum(X(n,:).*(X(n,:)*invx)))+log(detiEy)-0.5*sum(sum(Y(n,:).*(Y(n,:)*invy))); %log-likelihood of input data matrix
    else
        break;
    end;
end
lik=lik+N*log(const1)+N*log(const2);

if (t<=2)
    likbase=lik;
elseif (lik<oldlik)
    %fprintf('Oops!');
elseif ((lik-likbase)<(1 + tol)*(oldlik-likbase)||~isfinite(lik))
    break;
end;

LL=[LL lik];

%% M-step
C=Etysum'*inv(Vttsum);
P=Etxsum'*inv(Vttsum);
Ex=diag(diag(XX-(P*Etxsum)))/N;
Ey=diag(diag(YY-(C*Etysum)))/N;
end

net.C=C;
net.P=P;
net.Ex=Ex;
net.Ey=Ey;
net.LL=LL;

Data can be accessed at https://www.dropbox.com/s/i36kw4qq6al0pmn/d00_te.dat?dl=0
Results
$C_{new}$ for $p=2$ for 10 steps
val(:,:,1) =
-6.3224    1.7853

val(:,:,2) =
-6.3503    1.7866

val(:,:,3) =
-2.9558    1.9637

val(:,:,4) =
-1.6677    1.0381

val(:,:,5) =
-1.6664    1.0375

val(:,:,6) =
-1.6651    1.0368

val(:,:,7) =
-1.6637    1.0362

val(:,:,8) =
-1.6623    1.0355

val(:,:,9) =
-1.6609    1.0349

val(:,:,10) =
-1.6595    1.0342

$P_{new}$ for 10 steps
val(:,:,1) =
 0.0097   -0.0013
 0.0030    0.0199
 0.1186    0.0240
-6.5193    1.8837
-5.6963    1.8107
 0.2769   -0.0450

val(:,:,2) =
 0.0098   -0.0013
 0.0030    0.0198
 0.1193    0.0237
-6.5530    1.8852
-5.6773    1.8080
 0.2731   -0.0448

val(:,:,3) =
 0.0052    0.0015
-0.0059    0.0052
 0.0591   -0.0072
-3.0491    2.0738
-2.6955    1.7138
 0.1382   -0.0148

val(:,:,4) =
 0.0042    0.0028
-0.0050    0.0045
 0.0338    0.0123
-1.7145    1.1179
-1.5561    0.8878
 0.0927    0.0243

val(:,:,5) =
 0.0042    0.0028
-0.0050    0.0045
 0.0338    0.0123
-1.7131    1.1172
-1.5549    0.8872
 0.0927    0.0244

val(:,:,6) =
 0.0042    0.0028
-0.0050    0.0045
 0.0338    0.0123
-1.7116    1.1165
-1.5537    0.8867
 0.0927    0.0244

val(:,:,7) =
 0.0042    0.0028
-0.0050    0.0045
 0.0338    0.0123
-1.7101    1.1158
-1.5526    0.8861
 0.0927    0.0244

val(:,:,8) =
 0.0042    0.0027
-0.0050    0.0045
 0.0338    0.0123
-1.7086    1.1151
-1.5514    0.8855
 0.0926    0.0244

val(:,:,9) =
 0.0043    0.0027
-0.0049    0.0045
 0.0337    0.0124
-1.7071    1.1144
-1.5501    0.8849
 0.0926    0.0244

val(:,:,10) =
 0.0043    0.0027
-0.0049    0.0045
 0.0337    0.0124
-1.7055    1.1136
-1.5489    0.8843
 0.0926    0.0244

Log likelihood for 10 steps
    -6176.77412636697 + 0.00000000000000i
    -12350.0028284986 + 0.00000000000000i
    -48330.2798829560 + 0.00000000000000i
    -54546.8694822146 + 1507.96447372310i
    -60722.1364253500 + 1507.96447372310i
    -66897.4033684854 + 1507.96447372310i
    -73072.6703116208 + 1507.96447372310i
    -79247.9372547562 + 1507.96447372310i
    -85423.2041978916 + 1507.96447372310i
    -91598.4711410270 + 1507.96447372310i
Comment: Could you provide a log of both the new parameters and the new loglikelihood after each step (lets say for the first 10 steps) ?
Comment: $Cnew$ for $p=2$ for 10 steps
val(:,:,1) =

   -6.3224    1.7853

val(:,:,2) =

   -6.3503    1.7866

val(:,:,3) =

   -2.9558    1.9637

val(:,:,4) =

   -1.6677    1.0381

val(:,:,5) =

   -1.6664    1.0375

val(:,:,6) =

   -1.6651    1.0368

val(:,:,7) =

   -1.6637    1.0362

val(:,:,8) =

   -1.6623    1.0355

val(:,:,9) =

   -1.6609    1.0349

val(:,:,10) =

   -1.6595    1.0342
Comment: $Pnew$ for 3 steps too long to be included
val(:,:,1) =

    0.0097   -0.0013
    0.0030    0.0199
    0.1186    0.0240
   -6.5193    1.8837
   -5.6963    1.8107
    0.2769   -0.0450

val(:,:,2) =

    0.0098   -0.0013
    0.0030    0.0198
    0.1193    0.0237
   -6.5530    1.8852
   -5.6773    1.8080
    0.2731   -0.0448

val(:,:,3) =

    0.0052    0.0015
   -0.0059    0.0052
    0.0591   -0.0072
   -3.0491    2.0738
   -2.6955    1.7138
    0.1382   -0.0148
Answer: Problem Solved
The decreasing Likelihood was due to a coding problem. The accumulation of $lik$ is not only in the for loop for the data samples but also in the for loop for E-M step. This leads to adding the $lik$ of current EM-step onto the $lik$ of the previous EM-step which of course keeps decreasing $lik$. I am attaching the revised code here for anyone interested. No restriction on how you might use it.
function net=PPLS_EM(X,Y,p,iter,tol)

net=struct('type','PPLS_EM','C',[],'P',[],'Ex',[],'Ey',[],'LL',[]);
[N,~]=size(X); %N by p input data matrix

LL=[];
lik=0;

YY=diag(sum(Y.*Y)');
XX=diag(sum(X.*X)');

if nargin<5
tol=0.0001;
end

if nargin<4
iter=100;
end

if nargin<3
p=2;
end

%% Initilization

C=ones(dout,p);
Ey=ones(dout,1);
Ey=diag(Ey);

P=ones(din,p);
Ex=ones(din,1);
Ex=diag(Ex);

for t=1:iter
%% E-step
oldlik=lik;
[lik,Vtt,Etxsum,Etysum]=PPLS_E(C,P,Ex,Ey,X,Y);
if (t<=2)
    likbase=lik;
elseif (lik<oldlik)
    %fprintf('Oops!');
elseif ((lik-likbase)<(1 + tol)*(oldlik-likbase)||~isfinite(lik))
    break;
end;

LL=[LL lik];

%% M-step
C=Etysum'*inv(Vtt);
P=Etxsum'*inv(Vtt);
Ex=diag(diag(XX-(P*Etxsum)))/N;
Ey=diag(diag(YY-(C*Etysum)))/N;
end

net.C=C;
net.P=P;
net.Ex=Ex;
net.Ey=Ey;
net.LL=LL;

And the code for PPLS_E
    function [lik,Vtt,Etxsum,Etysum]=PPLS_E(C,P,Ex,Ey,X,Y)

    [N,din]=size(X);
    [~,dout]=size(Y);

    const1=(2*pi)^(-din/2);
    const2=(2*pi)^(-dout/2);

    lik=0;
    p=size(P,2);

    Vcur=zeros(p,p,N);
    tcur=zeros(p,N);

    tiny=exp(-700);
    I=eye(p);

    Ex=Ex+(Ex==0)*tiny;
    Ey=Ey+(Ey==0)*tiny;

    Etxsum=zeros(p,din);
    Etysum=zeros(p,dout);
    Vtt=0;

    Sigma_XY=I+C'*inv(Ey)*C+P'*inv(Ex)*P;
    for n=1:N
        tcur(:,n)=inv(Sigma_XY)*(P'*inv(Ex)*X(n,:)'+C'*inv(Ey)*Y(n,:)');
        Vcur(:,:,n)=inv(Sigma_XY)+tcur(:,n)*tcur(:,n)';

        Etx=tcur(:,n)*X(n,:);
        Ety=tcur(:,n)*Y(n,:);

        Etxsum=Etxsum+Etx;
        Etysum=Etysum+Ety;

        Vtt=Vtt+Vcur(:,:,n);

        % calculate likelihood
        Ydiff=Y(n,:)'-C*tcur(:,n);
        Xdiff=X(n,:)'-P*tcur(:,n);
        inx=inv(Ex+P*Vcur(:,:,n)*P');
        iny=inv(Ey+C*Vcur(:,:,n)*C');
        detiEx=sqrt(det(inx));
        detiEy=sqrt(det(iny));
        if (isreal(detiEx) && detiEx>0 && isreal(detiEy) && detiEy>0)
            lik=lik+log(detiEx)-0.5*sum(Xdiff'.*(Xdiff'*inx))+log(detiEy)-0.5*sum(Ydiff'.*(Ydiff'*iny)); %log-likelihood of input data matrix
        else
            break;
        end;
    end
    lik=lik+N*log(const1)+N*log(const2);


TCL\TK Resize window : bind

Question: I have problem when I resize my window ,I don't understand why, when I move my window . my another window created with .c bind cir <1> [list window %x %y] do not follow my window .
I believe that I've to use <Configure> as option, but I don't know how to do
Thanks for your help
My code below :
proc window {crx cry} {

set w1 .win
catch {destroy $w1}
toplevel $w1

wm minsize $w1 300 100
wm maxsize $w1 300 100

label $w1.l -text "$crx $cry"

pack $w1.l

}

wm state . zoomed

canvas .c -bg ivory

.c create oval 2 1.5 25 25 -fill #33FF00 -tag cir
.c create oval 30 30 50 50 -fill #33FF00 -tag cir1
.c create oval 60 60 90 90 -fill #33FF00 -tag cir2
.c create oval 90 90 130 130 -fill #33FF00 -tag cir3

pack .c -fill both -expand 1

.c bind cir <1> [list window %x %y]
.c bind cir1 <1> [list window %x %y]
.c bind cir2 <1> [list window %x %y]
.c bind cir3 <1> [list window %x %y]

Answer: Generally speaking, it's bad GUI design to make a whole group of windows move as one; it confuses users. That said…
The <Configure> event is sent to a widget whenever it is "reconfigured", which mostly these days means that its location relative to its parent and size are altered. You have to use the main bind command, and if you bind to a toplevel you need to remember that toplevels also listen to the events generated by their non-toplevel children by default; you usually need some filtering.
bind . <Configure> {
    if {"%W" eq [winfo toplevel %W]} {
        puts "reconfigured %W: (%x,%y) %wx%h"
    }
}

Once you have the filtered events you want, compute the updated geometries and apply them with wm geometry.

TCL\TK Resize window : bind


Intersection of lines on a plane

Question: Suppose we have n lines on a plane such that there are $k_2$ points where two lines intersect, $k_3$ points where three lines intersect, ... , $k_n$ points where n lines intersect. How many segments is the plane divided into by these lines?
Answer: The key here is the Euler characteristic
$$\chi=V-E+F$$
from which you can read the number $F$ of faces. For that you need
\begin{align*}
V&=\sum_{i=2}^n k_i \\
E&=n+\sum_{i=2}^n i\,k_i \\
\chi&=1
\end{align*}
You can get the correct value of $\chi$ from the following consideration: if you were to intersect your whole configuration with one big disk, then you'd get $2n$ points of intersection with its boundary, which contributes to $V$, but also $2n$ edges along the boundary, which contributes to $E$, so these two cancel out. Hence the characteristic for the disk, which is $1$, is correct for your infinite plane as well. Another approach would be stereographic projection, which turns the plane into a sphere (which has $\chi=2$) but which also adds one more point at infinity which counts in $V$. As the easiest solution, you can simple do an experiment on a case you know to obtain the correct value of $\chi$. Or read somewhere else that the characteristic of the plane is $\chi=1$.
The number $E$ of edges can be obtained as follows: originally every line is one edge. But then you intersect them, and every point of intersection between $i$ lines will split each of these $i$ lines into two parts, i.e. add one more segment to each of them. So in the end, the total number of segments, including those which go off to infinity, is represented by the formula given above.
From this you get
$$F=\chi-V+E=1+n+\sum_{i=2}^n(i-1)\,k_i$$
Examples:

$n=2, k_2=1\Rightarrow F=4$
$n=3, k_3=1\Rightarrow F=6$
$n=3, k_2=3\Rightarrow F=7$

Looks good in these cases, so I now trust it should be correct.
One additional note: unless you have parallel lines, you can compute $n$ from the $k_i$, using the equation
$$ n^2-n = \sum_{i=2}^\infty (i^2-i)\,k_i $$
This is a quadratic equation, but in general only one solution will be positive. Only the cases $n=0$ and $n=1$ cannot be distinguished. So if there are any intersections at all, then you can give the number of regions even if you don't know the number of lines explicitly up front.
The equation essentially counts pairs of distinct lines. On the whole there are $n$ choices for a first line and $(n-1)$ choices for a second line to form a pair with that. At each vertex there are $i$ lines and each of them can be paired with $(i-1)$. Since I assumed non-parallel lines, every pair of lines has to intersect at one of the vertices, so the two counts have to agree.
Comment: Why can you use the Euler characteristic? It is defined only for planar graphs and polyhedra.
Comment: @user130039: According to the Wikipedia article I referenced, it's defined for [CW-complexes](https://en.wikipedia.org/wiki/CW-complex). But if you re-read the paragraph about how to find the right $\chi$, you can see that if you intersect with a disk, you get a planar graph, and if you do the spherical projection instead, you get a graph on a sphere which is equivalent to a polyhedron.
Comment: I see. I didn't understand why projecting an infinite plane on a sphere results in a single extra vertex "at infinity", and it is explained nicely [here](http://aleph0.clarku.edu/~djoyce/java/compass/compass4.html), for anyone interested.

Intersection of lines on a plane


Bilingual models

Question: Is there a way for __unicode__ to return spanish or english depending on request.session['django_language']? Should I be designing my models differently or is this the standard way of doing things?
class Tip(models.Model):
    '''A small random tip for users to see on the login page'''
    en_text = models.CharField(max_length=500)
    es_text = models.CharField(max_length=500)

    def __unicode__(self):
        pass

And then from the view to the template...
try:
    tip = Tip.objects.order_by('?')[0]
except:
    tip = None

-->

{{ tip }}

Answer: I'm not sure that this is the greatest way to do it, but I faced a similar situation and did the following:
class Image(models.Model, TranslatedModelMixin):
     artifact = models.ForeignKey(Artifact)
     caption = models.CharField(max_length=500, null=True, blank=True)
     caption_fr = models.CharField(max_length=500, null=True, blank=True)
     image = models.ImageField(upload_to='artifacts/images')

     language_code = 'en'
     translated_fields = ['caption']

     def __unicode__(self):
         return u'Image for %s' % self.artifact.name

 class TranslatedModelMixin(object):
     """
     Given a translated model, overwrites the original language with
     the one requested
     """

     def set_language(self, language_code):
         if language_code == 'en':
             return

         self.language_code = language_code

         for field in self.translated_fields:
             translated_field_key = field + '_' + language_code
             translated_field = getattr(self, translated_field_key)
             setattr(self, field, translated_field)

         return

So if I want the model data in french, I just do image.set_language('fr') and then in my template I can just do {{ image.caption }} and get the translated version. I wouldn't use __unicode__ to present the model in the template.
Comment: Thank you, this works brilliantly. Also, I think I finally am starting to understand mixins :)

Bilingual models


Connection-Specific Database Reader Class

Question: At my job, most of our data revolves around a single ODBC datasource -- however, we have hundreds of VBA macros that are polluted with thousands of explicit instances of ADODB connection & recordset objects (all connected via the same DSN).
There is quite a bit of code that I'd like to refactor that's currently in the form of:
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset

cn.Open "MYDSN"
SQL = "SELECT * FROM [...]"
rs.Open SQL, cn

' utilize rs ...

rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing

I'm imagining a reusable class dll, probably written in C#.NET that would provide a simple interface to accomplish the above more concisely.
My question is, how would you set out to design this class?  I'm fairly new to C#, but would like to avoid leaving the same mess behind for the next person.
Thanks in advance!
Comment: If it is working, what do you see as the advantage to your employers?
Comment: Instead of changing the ODBC DSN on every machine that uses these applications, we could change the database connection information in one spot.
Comment: This must be common across those companies that decided that they didn't need local IT and developers (at least a few) to manage standards. I'm in the exact same boat. We run our operations on VBA macros and it kills me. VBA can be good and robust but the learning curve for it is so gentle that most VBA floating around is macro recorded and forum copy pastes. The source is way to accessible. Well, I have nothing constructive to add other than nod and some empathy to your plight.
Answer: Enterprise Library 5.0 is good place to start.  It has pretty much everything you need built right in.  No need to reinvent the wheel.  http://msdn.microsoft.com/en-us/library/ff664408%28v=pandp.50%29.aspx
Answer: Applying the good old KISS principle - create one static method CreateRecordset(string SQL) that reads the DSN from configuration, opens the connection and return the recordset object for the given SQL.
Comment: I wouldn't recommend making anything that has to do with data access static if you have a bunch of sources invoking it simultaneously.  Unless you add some sort of connection management in there yourself that can lead do some pretty awful fireworks.
Comment: The static method would create a new connection each time. Not reuse a single static instance.
Comment: I'm with @Knagis on this one. You are going to be fighting ADO.Net to get it to work like this, pooled connections, garbage Collection and such. Add a VBA method to return a recordset.


PHP won't show any errors

Question: Here is my code:
echo 'foo';

error_reporting(E_ALL);

echo 'this line doesnt end in a semi colon'

echo 'i should get an error here';

When I run this I get no error.
Not sure how this can be?
Comment: @prodigitalson depends on the site state. for developnemt I prefer on-screen
Comment: Am I the only person who prefers to just `tail -f` the error log? :-)
Comment: `I get no error. ` means `I get all 3 lines printed` or `I get a blank screen`?
Comment: @Col. Shrapnel, I get a blank screen. I added `php_flag dislay errors 1`. Which at first didn't work. Then I added `Order allow,deny Allow from all AllowOverride All ` To my apache2.conf file and restarted apache and now the errors are showing.
Comment: @Col. Shrapnel: Yeah even for development i prefer the log... This is because I like `E_ALL|E_STRICT` but i dont want warning ouput fudging the xhtml. Plus, im normally running `tail` directly in an Eclipse as an external tool :-)
Comment: @John you could add just `AllowOverride All` there and then use .htaccess which is usually more handy.
Answer: ini_set('display_errors', 1);

Do note though that if you do this in the file that has the syntax error, it won't work, as it'll never get executed then. You can also set this true in php.ini (not recommended for production servers), or if you use Apache, in .htaccess with:
php_flag display_errors 1

Comment: so php_flag display_errors 1 should show all errors allways? (cause its not my case)
Comment: No, it only tells that errors are displayed at all. The `error_reporting` configuration value determines what errors are displayed.
Answer: error_reporting directive won't help you to show error messages on-screen. It's responsible for which error to show, not where.
if your PHP runs as Apache module (most likely it does) add the following line into .htaccess file:
php_value display_errors 1

when you switch to production, change it to
php_value display_errors 0
php_value log_errors 1

and watch them it in the error log.
Answer: Do you have any kind of shutdown hooks, error-handling functions or global exception catchers running?
Syntax errors can be quirky in large frameworks :)

PHP won't show any errors


Como passar vetor como argumento no shell script?

Question: Gostaria de passar um vetor no maior estilo JSON como argumento para um script.
Exemplo :
<code>#!/bin/bash

vetor[]=$1

echo ${vetor[*]}

i=0
        for nomes in ${vetor[*]}
        do
            i=$(($i+1))
            echo "Nome   $i é  $nomes"
        done
</code>
E gostaria de executar assim :
<code>> bash MeuScript.sh [" Joao", "Jose", "Carlos"]
</code>
Existe algum modo de se fazer isso ? Pois tenho que passar essa estrutura como argumento e esse vetor tem que ser interpretado como um único argumento.
Answer: Não tem uma forma de passar arrays diretamente para o script. Uma forma de fazer isso é passando o array para uma variável antes, e passar a mesma para o script:
<code>#!/bin/bash

vetor=("$@")

echo "${vetor[@]}"

i=0

for nomes in "${vetor[@]}"
do
    i=$(($i+1))
    echo "Nome $i é $nomes"
done
</code>
E a chamada ficaria assim:
<code>$ nomes=('João' 'Maria' 'José')
$ bash MeuScript.sh "${nomes[@]}"
</code>