C pointers: difference between while(*s++) { ;} and while(*s) { s++;}

Question: I'm going through K & R, and am having difficulty with incrementing pointers. Exercise 5.3 (p. 107) asks you to write a strcat function using pointers.
In pseudocode, the function does the following:

Takes 2 strings as inputs.
Finds the end of string one.
Copies string two onto the end of string one.

I got a working answer:
void strcats(char *s, char *t)
{
    while (*s)            /* finds end of s*/
        s++;
    while ((*s++ = *t++)) /* copies t to end of s*/
        ;
}

But I don't understand why this code doesn't also work:
void strcats(char *s, char *t)
{
    while (*s++)
        ;
    while ((*s++ = *t++))
        ;
}

Clearly, I'm missing something about how pointer incrementation works. I thought the two forms of incrementing s were equivalent. But the second code only prints out string s.
I tried a dummy variable, i, to check whether the function went through both loops. It did. I read over the sections 5.4 and 5.5 of K & R, but I couldn't find anything that sheds light on this.
Can anyone help me figure out why the second version of my function isn't doing what I would like it to? Thanks!
edit: Thanks everyone. It's incredible how long you can stare at a relatively simple error without noticing it. Sometimes there's no better remedy than having someone else glance at it.
Comment: Although K&R is widely regarded as the C bible, it's not a good place to learn how to write good code.  Stuff like `while (*s++ = *t++);` should be consigned to the dustbin of history.
Answer: This:
while(*s++)
    ;

due to post-increment, locates the nul byte at the end of the string, then increments it once more before exiting the loop.  t is copied after then nul:
scontents␀tcontents␀

Printing s will stop at the first nul.
This:
while(*s)
    s++;

breaks from the loop when the 0 is found, so you are left pointing at the nul byte.  t is copied over the nul:
scontentstcontents␀

Answer: It's an off-by-one issue. Your second version increments the pointer every time the test is evaluated. The original increments one fewer time -- the last time when the test evaluates to 0, the increment isn't done. Therefore in the second version, the new string is appended after the original terminating \0, while in the first version, the first character of the new string overwrites that \0.
Comment: oops...of course. I was incrementing my original string past the terminal '\0'. So the second code was concatenating, but nothing was being printed, because printf() stopped at the first '\0'.

I tested by putting s-- in the flawed version after the loop, and everything printed fine.

Thanks. Funny how you can stare at a problem forever without spotting the flaw.
Answer: There's one less operation in while (*s) ++s; When *s is zero, then the loop breaks, while the form while (*s++) breaks but still increments s one last time.
Strictly speaking, the latter form may be incorrect (i.e. UB) if you attempt to form an invalid pointer. This is contrived, of course, but here's an example: char x = 0, * p = &x; while (*x++) { }.
Independent of that, it's best to write clean, readable and deliberate code rather than trying to outsmart yourself. Sometimes you can write nifty code in C that is actually elegant, and other times it's better to spell something out properly. Use your judgement, and ask someone else for feedback (or watch their faces as they look at your code).
Answer: This:
while (*s)
    s++;

stops as soon as *s is '\0', at which point it leaves s there (because it doesn't execute the body of the loop).
This:
while (*s++)
    ;

stops as soon as *s is '\0', but still executes the postincrement ++, so s ends up pointing right after the '\0'. So the string-terminating '\0' never gets overwritten, and it still terminates the string.
Answer: let's assume the following characters in memory:
Address         0x00 0x01 0x02 0x03
-------         ---- ---- ---- ----
0x8000           'a'  'b'  'c'   0
0x8004           ...

While executing loop, it happens in memory.
1.  *s = 'a'
2.  s = 0x8001
3.  *s = 'b'
4.  s = 0x8002
5.  *s = 'c'
6.  s = 0x8003
7.  *s = 0;
8.  s = 0x8004
9.  end loop

While evaluating, *s++ advances the pointer even if the value of *s is 0.
// move s forward until it points one past a 0 character
while (*s++);

It doesn't work at all because s ends up pointing to a different place.
As it summarizes, we get a garbage value as last character in our target string. That garbage string is because of while loop exceed the limit of '\0' by one step forward.
You can eliminate it by using the below code, I think it is efficient
while (*s)
    s++;

It execute as below in memory perspective.
1.  *s = 'a'
2.  s = 0x8001
3.  *s = 'b'
4.  s = 0x8002
5.  *s = 'c'
6.  s = 0x8003
7.  *s = 0
8.  end loop


ImageMagick - Overlay one image onto many

Question: I have a folder of images labelled: test.png and img000.png-img480.png
How can I overlay test.png onto every imgxxx.png individually?
Currently "magick test.png img000.png -compose over -composite result.png" works for a single .png, and I'm not sure how/if mogrify can be used for all of them.
Any help is greatly appreciated (non-imagemagick solutions would also be great!) thanks :)
Comment: Can't you just make a loop (script or else) over the `img*` files?
Answer: Make a copy of a few test files in a separate directory then change to that directory and try this:
magick mogrify -draw 'image Over 180,100 0,0 test.png' img*png

The 180,100 specifies where it will be drawn, and the 0,0 says to use the current size of test.png, i.e. no resizing. You can change the blend mode (i.e. Over here) for different effects. Get a list of available blend modes with:
magick identify -list compose

If you would like the results written in a different directory called RESULTS, use:
mkdir -p RESULTS
magick mogrify -path RESULTS -draw 'image SrcOver 180,100 0,0 test.png' img*png


Laser pointer finding points

Question: I have a two-axis laser pointer.  By using a couple of known points I would like to be able to have it point to any position in a square grid.  Starting with a single axis motion can you please tell me if I would have enough information to solve for the angles of the pink dotted lines?  I suppose if I had another point along the arc would help.
Here is a similar question. Estimation / Calibration of Transformation of 2DOF laser pointing system in 3D space
Picture showing what is needed
Comment: How are the two axes related ?  Are they articulated (meaning if there is rotation about the first axis, will the second axis move physically about the first axis ?
Comment: Yes, they are articulated.
Answer: It makes sense to assume that the two axes are not independent but are articulated.  I am going to attach a reference frame to each and relate their composition to the world coordinate system.  The formula is standard and it is as follows
$p = d_1 + R_1 ( d_2 + R_2 p_2 ) $
where $p$ is the world coordinate position vector, d_1 , d_2 are offsets, and R_1, R_2 are rotation matrices, and $p_2$ is a position vector in the second frame coordinate system.
Now it is assumed that the laser beam is mounted on the second frame and has the equation of a line as follows
$p_2 = d_3 + \alpha v $
where $d_3$ is an offset and $v$ is the direction vector of the laser beam line of action, and $\alpha$ is an arbitrary scalar.
Therefore, the overall relation is
$ p = e + \alpha R v $
where
$e = d_1 + R_1 d_2 + R_1 R_2 d_3 $
$ R = R_1 R_2 $
Now suppose we have a square grid (a square of known side length $a$), and we manually point the laser beam at three of its corners $p_1, p_2, p_3$ where it is assumed that $p_1 p_2 $ is perpendicular to $p_2 p_3$ and $\|p_1 p_2\| = \|p_2 p_3\| = a $.  It also assumed that the axes of the rotation matrix $R_1$ and $R_2$ are known, and for $p_1, p_2, p_3$ the angles specifying the rotation can be measured and are therefore known, so that for each of the three points the matrices $R_1, R_2$ are known exactly, although the coordinates of $p_1, p_2, p_3$ are not known.
Now we can write
$p_1 = e_1 + \alpha_1 R(\theta_1, \phi_1) v $
$p_2 = e_2 + \alpha_2 R(\theta_2, \phi_2) v $
$p_3 = e_3 + \alpha_3 R (\theta_3, \phi_3) v $
The unknowns in the above three equations are $\alpha_1, \alpha_2, \alpha_3 $ as well as the coordinates of $p_1, p_2, p_3$, however we can write the following equations in $\alpha_1, \alpha_2, \alpha_3 $
$\| p_1 - p_2 \| = a $
$\| p_2 - p_3 \| = a $
$ (p_1 p_2) \cdot (p_2 p_3) = 0 $
which are three independent quadratic (second degree) equations in the three unknowns $\alpha_1, \alpha_2, \alpha_3 $ and can be solved numerically (for example, using the Newton-Raphson multivariate method).  Once these scalars ($\alpha_1, \alpha_2, \alpha_3$ ) are found, then the exact coordinates of $p_1, p_2, p_3$ (the corners of the square grid) are now known.
EDIT:
After numerically implementing the above scheme, I found that there can be multiple solutions for $\alpha_1, \alpha_2, \alpha_3$.  Hence, $3$ points are not enough.  We need the fourth corner of the square $p_4$, then we have
$p_4 = e_4 + \alpha_4 R(\theta_4, \phi_4) v $
And we can build a system of $3$ equations relating the vector $p_1 p_2 $ and $p_1 p_4$, namely,
$\| p_1 - p_2 \| = a $
$\| p_1 - p_4 \| = a $
$ (p_1 p_2) \cdot (p_1 p_4) = 0 $
Solving as before will generate solutions for $\alpha_1, \alpha_2, \alpha_4$ which is then cross matched with the previously obtained solutions for $\alpha_1, \alpha_2, \alpha_3$ and this will result in the true values for $\alpha_1, \alpha_2, \alpha_3, \alpha_4$.
Now any point on the grid is expressible as follows
$ p = p_2 + t (p_1 - p_2) + s (p_3 - p_2 ) $
If the grid is $N \times N$, then the $ij$-th grid point is found by setting $t = \dfrac{i}{N}, s = \dfrac{j}{N} $
Back to the basic equation relating the world coordinates with the local frames, we have
$ p = e + \alpha R v = d_1 + R_1(\theta) (d_2 + R_2(\phi) (d_3 + \alpha v) )$
From which it follows that
$ R_1(-\theta) ( p - d_1) - d_2 = R_2 (\phi) (d_3 + \alpha v) $
The vector $ p - d_1 $ is known, so the left hand side parameterizes a circle in $3D$, while the right hand side represents a hyperboloid of one sheet.  The intersection (if it exists) can be found resulting in two possible values for $\theta$, and if we set
$W = R_1 (-\theta) (p - d_1 ) - d_2 $
then, we now have
$ R_2 (\phi) (d_3 + \alpha v) = W $
Therefore,
$ d_3 + \alpha v = R_2 (-\phi) W $
The right hand side describes a circle in $3D$ while the left hand side is just a straight line, which we know intersects the circle.  The intersection can be found by intersecting the line with the plane of the circle resulting in $\alpha$, then $\phi$ can be solved for.

Laser pointer finding points


How to catch system events when a program is being updated and robotize routine steps when you update software?

Question: I'm a support engineer for an accounting program. The program runs under Windows.
I have to update many of this accounting software bases daily. I mean the software is always the same. But users may have, say, 50 informational bases installed. Each base needs updating. It may be some outsorcing company which does bookkeeping for its clients or something. So, for every client they install a special informational base which I have to update.
I would like to robotize my routine operations somehow.
I mean I have to: start each program, in the menu select updating process, seek for updates in a directory, press next, confirm that I have read the instruction notes, confirm that I have backuped the data, then Next, Next and Finish. Then in the main menu I have to restart the program, then agree that the software has been purchased legally. Then restart again. Make another backup copy. Then launch testing process and select several options (always typical in a typical case). Then I have to wait for the test to pass. Then go on to another informational base.
The software developer doesn't provide us with any launch options for silent mode or something. Well, it tkes from 20 minutes to 1 hour to update one informational base.
I have got acquainted with several programming languages. Like Java, C#, some scripting languages. But I have never written a batch file.
Well, I'm just wondering if you could recommend me which technology/language can I use to automatize this if it is humanly possible. Thank you in advance.
Answer:
I would like to robotize my routine operations somehow.

We have a word for that: automation. :)

I mean I have to: start each program, in the menu select updating
  process, seek for updates in a directory, press next, confirm that I
  have read the instruction notes, confirm that I have backuped the
  data, then Next, Next and Finish.

AutoHotKey might be able to help with this part.

The software developer doesn't provide us with any launch options for
  silent mode or something.

Have you requested that he do so? The software dev works for you, right? You ought to be able talk with that person's management and get automation support in as a feature request.


Filter out linearly interpolated historical data points

Question: I am reading in historical sensor data from a plant. I found out that there are intermittent periods where between time t1 and time t2, the data points are linearly interpolated. I came to know, that this is done by the server automatically, when the data is missing between t1 and t2 (eg. sensor being off etc.). I am providing an example below, where the data goes missing between A,B.
Manually filtering out these unwanted data points improves my model quality by quite a lot. I would like to know what is a smart approach to filter out such data points apart from checking the slope ?
Answer: Perhaps more of a programming problem than a statistical one, but there is a simple solution: Write an algorithm that fits a line across a moving window. Wherever the line has a perfect fit, the data has likely been interpolated. How likely? That depends on the number of digits the sensor records and the size of the moving window.
For example, a window of $100$ ms (? I can't see what your unit of measurement is in the question), appears to have quite a lot of variation going on in the non-interpolated sections, but is perfectly linear where interpolated, judging from the figure.
Of course, the larger the window, the larger the minimum size of the interpolations it will be able to pick up (and thus the fewer it will pick up), but the smaller the window, the more likely it will pick up coincidental cases.


Delete the backing Lambda function for a Lambda-backed custom resource

Question: I have an AWS CloudFormation template that creates an OpsWorks stack and deploys an application. To deploy the application, I am using a Lambda function and a custom resource which utilizes that function. My problem is: that Lambda function will only be executed one time during the creation of the stack, and then it will never be used again. Is there any way to delete the Lambda function by AWS CloudFormation at the end of the execution of the stack?
Comment: It will also be called again if you delete the stack, or update the custom resource, so deleting it is probably not ideal. You might consider creating the lambda in a separate stack, and export its ARN and use that, instead of inlining the custom resource; then at least you wont have an extra function for each stack you create that uses the custom resource.
Comment: You **could** delete the function from inside the function itself, immediately after it runs.
But you're really setting yourself up for trouble. When you delete your stack CF will try to call the lambda, if you update it it might also get called, depending on parameters. CustomResources are not supposed to be throwaway scripts.
Another option would be to wrap the call to cloudformation a script, and have that call the deploy. Good luck!
Comment: http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/crpg-ref-requesttypes-delete.html - first line: "Custom resource provider requests with RequestType set to "Delete" are sent when the template developer deletes a stack that contains a custom resource. To successfully delete a stack with a custom resource, the custom resource provider must respond successfully to a delete request."
Comment: Understood. but for our usecase, we have already a lot of lambda functions and having more usless ones is not preferable. the lambda will be created again any way if we create another stack so we don't really need to have it after that it has done its job
Comment: Do you have the aws documentation that says that while deleting a cloudformation stack, the lambda function are being called again?
Comment: Yes but I only send requestType = create
Comment: Your comment resolved my question. I delete the function by itself and i have no problem when deleting the stack. Could you add it as an answer?
Answer:
that Lambda function will only be executed one time during the
  creation of the stack, and then it will never be used again.

^^That's not the case. The backing Lambda function for a Lambda-backed custom resource will be invoked everytime the corresponding resource is touched (i.e. created, updated or deleted). AWS CloudFormation will pass RequestType parameter to that function everytime it sees that the resource is being touched, and pass it one of these values: Create, Update, Delete. Your Lambda function should perform the necessary action taking that param into account. Based on your question it appears that your Lambda function only caters to RequestType = Create?
Also, as per AWS docs, you won't be charged for creating a Lambda function, but only if you actually invoke it. So cost can't be deterring factor for keeping the function around.
On the contrary, if your concern is that you don't want extra clutter, you can try creating a common CloudFormation stack who's job will be to create shared resources, and you can then define that Lambda function over there? I'll have to know about your entire workflow to say for sure if that approach will work or not.
For what it's worth, I'd recommend not deleting the backing function of the Lambda-backed custom resource because it'll be a pain when someone touches the corresponding resource in the future, or wants to create another instance of the same resource type.
Answer: First, I should say Aditya is right, you shouldn't delete the backing Lambda as it's used throughout the lifecycle.
However, if you really really want to do it, one way is to simply have the function delete itself (and related resources, eg, role) after running.
Answer: Some of your assumptions regarding custom resources are not true. In a Lambda backed custom resource, you implement your logic to support creation, update and deletion of the resource. These indications are sent from CloudFormation via the event and give you information about the stack process.

It's important to understand the custom resource life cycle, to prevent your data from being deleted.
Create - that's easy, when a resource is being created an event with request type Create is sent to your function.
Delete - this one is more tricky. When a resource is being deleted a Delete request type is sent. But there are more scenarios other than resource Delete. We will have to explain Update first.
Update - gets called if any of your custom resource properties were changed. For example, in our app we can modify the allowed callback urls, which will trigger the function with an Update request type

I welcome you to read more about best practices in creating custom resources in this blog post


Como encontrar um determinado valor em uma linha que contém uma palavra-chave - python

Question: Como eu poderia achar um valor em uma determinada linha que contenha uma palavra chave
Exemplo (Esse é o arquivo Espelho)

 COTIA CENTRO

EXTRATO         05978754
ITEM 1 ITEM 2 ITEM 3
OPR: 082545444

Eu gostaria de saber como encontrar esse valor do que está a frente do extrato , Exemplo
Se o número tal estiver na mesma linha do Extrato ele retorna verdadeiro , se não estiver retorna falso
Esse é meu código
import re
import json
import os
import os.path
from time import sleep

with open('P:/portal/cupons/API/valores_api.json', 'r') as jsonFileLeitura:
    dadosJson = json.load(jsonFileLeitura)
    valor = dadosJson['valor']

def procura_cupom():
    txt = open("P:/portal/cupons/Arquivos_Espelho/arqEspelho.txt", "r").read()

    # usando finditer ele retorna a posição da palavra

    # Localiza o inicio do cupom
    x = re.finditer(r".*COTIA CENTRO", txt)
    # Localiza o fim do cupom
    z = re.finditer(r"OPR.*", txt)

    espelhos = list(zip(x, z))

    # Testando o valor específico para cada espelho
    for espelho in espelhos:

        txt_espelho = txt[espelho[0].span()[0]: espelho[1].span()[1] + 1]

        if "{}".format(valor) in txt_espelho:
            if os.path.isfile("P:/portal/cupons/Arquivos_Espelho/espelhado.txt"):
                open("P:/portal/cupons/Arquivos_Espelho/espelhado.txt", 'w').close()
            sleep(1)
            # Escreve o Cupom caso encontrado, em um novo arquivo de texto
            open("P:/portal/cupons/Arquivos_Espelho/espelhado.txt", "w+").write(txt_espelho)

        else:
            pass

if True:
    procura_cupom()

Obrigado!
Answer: Se o arquivo estiver apenas com a linha neste formato: "EXTRATO 05978754"
É só ler o arquivo linha a linha:
import re

for linha in open("P:/portal/cupons/Arquivos_Espelho/arqEspelho.txt", "r").readLines():
    if "EXTRATO" in linha.split():
       if re.match(r'^([\s\d]+)$', linha):
          return True


502 Bad Gateway using Beautiful Soup, Python/Django

Question: Beautiful Soup works in the Python shell using Django. I can also successfully import from bs4 import BeautifulSoup into views.py, but when I call something like soup = BeautifulSoup(xml), I get a 502 Bad Gateway error. I talked to my host, and they could not find the problem. Any ideas?
Note the xml is xml = urllib2.urlopen("http://isbndb.com/api/books.xml?access_key=000000&results=details&index1=isbn&value1=0000").read(), but it works in the Python shell (within myproject folder), so I wouldn't think that's the problem.
Comment: That error wouldn't have anything to do with BeautifulSoup. It is likely a problem on the server from which you are retrieving your data.
Comment: I already submitted a support ticket, and they said it's a problem on my front.
Comment: Like I said, I can call `soup = BeautifulSoup(xml)` in my shell but when I call it from `views.py` I get the 502 bad gateway. If I comment it out, I don't get the 502 bad gateway. `from bs4 import Beautiful Soup` works in both views.py and in the shell though.
Answer: This sounds like the interaction between Cython and mod_wsgi described here, and explored in a Beautiful Soup context here.
Comment: I re-explained the problem to my host, and they told me I had to install an older version of Beautiful Soup and so I did. It works now. FWIW, I removed lxml since I don't use it for any of my apps.
Answer: Try copying and pasting that URL into your browser. I get an access key error; fix that and your problem is solved.
Comment: Um. No. The reason it has an error is because I took out the actual API access key and ISBN key. `000000` and `0000` aren't the actual numbers.
Comment: I recommend you try being a bit more polite when asking for help. Have a nice day.


Getting views data as a REST service

Question: I need a way to get the output of a view as a REST service in JSON format.
I read about the RestfulWS and the Services modules, but I didn't find enough documentation.
Are these straight forward, or should I just go with a custom module?
Answer: Perhaps Services Views module could be of interest here:

Views support for the Services module version 3.x and later.
It has currently two features:
  - Create view based resource creating Services display in a view
  - Execute any view of the system via views resource call
After enabling "views" resource in your services endpoint you can do
  GET request to:
<code>http://example.com/<endpoint path>/views/<view name>
</code>
This will execute a view and pass results.

Getting views data as a REST service


Manually calculate the parameters (Std. Error) of lm output in R

Question: I'm trying to expand my understanding of Linear regression and to that end I'm looking at calculating a Linear regression exercise by hand.
Using some dummy data
x <- c(17,13,12,15,16,14,16,16,18,19)
y <- c(94,73,59,80,93,85,66,79,77,91)
model.test <- lm(y ~ x)
summary(model.test)

The output gives me:
Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   30.104     23.824   1.264    0.242
x              3.179      1.514   2.100    0.069

Residual standard error: 9.859 on 8 degrees of freedom
Multiple R-squared:  0.3553,    Adjusted R-squared:  0.2747

F-statistic: 4.409 on 1 and 8 DF,  p-value: 0.06895
I can :

manually calculate the estimates of the intercept and x ok
I understand how to calculate the t value (i.e. estimate/Std. Error)
I also understand how to perform the hypothesis test to obtain a p-value for the Pr(>|t|) column.

My question is how can I calculate the two values in the Std. Error column?
For good measure I had a look at the lm.R  code and I can see:
se <- sqrt(diag(R) * resvar)
resvar <- rss/rdf
rss <-sum(w * r^2)
rdf <- df[2L]

It looks like the formula is contained within sw, however I can't quite figure out what is happening as part of the terms resvar(residial variance?) or the square root of a matrix R from what I can gather.
Thanks in advance
Jonathan
PS I found a related post however it does not contain an answer -> how to manually calculate SE of coeficient from regress data outputs
PPS Manual workings below
observation X   Y   (x-x_mean)  (y-y_mean)  (x-x_mean)*(y-y_mean)   (x-x_mean)^2    (y-y_mean)^2    y_hat   y(hat) - y  y(hat) - y^2)
1           17  94  1.4 14.3    20.02   1.96    204.49  84.0024336  -9.997566399    99.9513339
2           13  73  -2.6    -6.7    17.42   6.76    44.89   71.32039595 -1.679604051    2.821069768
3           12  59  -3.6    -20.7   74.52   12.96   428.49  68.14988654 9.149886536 83.72042362
4           15  80  -0.6    0.3 -0.18   0.36    0.09    77.66141478 -2.338585225    5.468980855
5           16  93  0.4 13.3    5.32    0.16    176.89  80.83192419 -12.16807581    148.062069
6           14  85  -1.6    5.3 -8.48   2.56    28.09   74.49090536 -10.50909464    110.4410701
 7          16  66  0.4 -13.7   -5.48   0.16    187.69  80.83192419 14.83192419 219.9859751
 8          16  79  0.4 -0.7    -0.28   0.16    0.49    80.83192419 1.831924188 3.355946231
 9          18  77  2.4 -2.7    -6.48   5.76    7.29    87.17294301 10.17294301 103.4887696
10          19  91  3.4 11.3    38.42   11.56   127.69  90.34345243 -0.656547573    0.431054716

Total       156 797 3.55271E-15 -2.84217E-14    134.8   42.4    1206.1  795.6372042 -1.362795772    777.7266929
Mean        15.6    79.7    3.55271E-16 -2.84217E-15    13.48   4.24    120.61  79.56372042 -0.136279577    77.77266929
Std Dev     2.170509413 11.57631682 2.170509413 11.57631682 26.01030565 4.845341405 135.6597115 6.881620524 9.294807222 74.37605266
Variance    4.711111111 134.0111111 4.711111111 134.0111111 676.536 23.47733333 18403.55733 47.35670104 86.39344129 5531.797209

Answer: From doing some additional digging I found the Standard error of the parameter b can be obtained using the following formula:
SE = sb1 = sqrt [ Σ(yi - ŷi)^2 / (n - 2) ] / sqrt [ Σ(xi - x)^2 ] [1]

The Standard error of the intercept a can be found using the following formula:
SE = sa1 = S_e sqrt (( 1/n ]) + (x_mean)^2 / Σ(xi - x_mean)^2)

The Std error for b computed manually is 1.514 (which looks the same as the R regression out)
The Std error for a computed manually is 23.827 (which is out by 0.003 I can only put this down to a rounding error on my behalf)
References:
[1] http://stattrek.com/regression/slope-confidence-interval.aspx?Tutorial=AP
[2] http://courses.ncssm.edu/math/Talks/PDFS/Standard%20Errors%20for%20Regression%20Equations.pdf


Normality testing on subsets or whole data?

Question: I have data for a continuous dependent variable that I'd like to test for normality. It is a time variable that measures the time of occurrence of an event within 30 seconds. If no event occurs within that timeframe, the result is n.a.
The time variable has been measured for a variety of different conditions, defined by combinations of states of four independent categorical variables.
Will I have to check for normality in the whole material (i.e. not split into subgroups) or should I check within each group? Can normality be tested for when there is a defined cut off at 30 secs?
Answer: If you just look at the distribution of the dependent variable, then you look at the marginal distribution of that variable, and that is never assumed to be normally distributed. If there is a normality assumptions then that typically refers to the distribution of the residuals.
Timing variables are almost never normally distributed: They are usually skewed and negative values are typically impossible. There are additional problems with observations that have not yet experienced the event. All this is better dealt with survival analysis, which does not assume normality.


I'm trying to create two content types at once in one form. One is a parent page the other a child page

Question: So I'm workoing on a story archive website and I've created several interations using the Book module in drupal. However while I like all the stuff book does by default I need to streamline the process of content creation and I'm no longer sure I can do that easily with book module.
I am right now trying to figure out how to create a way to create both the front page and the first child page of a story/book in the system, and create an easier way to add a chapter to a story and tell drupal what the chapter number is. If anyone has any advice on this I would appreciate it emensly.
Answer: The Inline entity form module was built for pretty much that exact use case:

Provides a widget for inline management (creation, modification, removal) of referenced entities.
The primary use case is the parent -> children one (product display -> products, order -> line items, etc.), where the child entities are never managed outside the parent form.
Existing entities can also be referenced.
Comment: YEa, I tried that but I'm ending up confused as I try to setup the reference.


How to pass value between activity and viewpager fragment?

Question: It may be dumb question but am struggling with this how to pass value between activity and viewpager. Now let me explain my requirement i have two tabs namely task and calls .  Having one button in task when user press that it will go to new activity from there will be two forms one is edittext and spinner need to populate listview in task fragment from that activity data so far what i have tried is:
This is my Main activity:
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide
     * fragments for each of the sections. We use a
     * {@link FragmentPagerAdapter} derivative, which will keep every
     * loaded fragment in memory. If this becomes too memory intensive, it
     * may be best to switch to a
     * {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    private SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        // Create the adapter that will return a fragment for each of the three
        // primary sections of the activity.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        //  Bundle bundle=getIntent().getExtras();
        // Intent intent=getIntent();
        //   ActivityView activityView=(ActivityView)intent.getSerializableExtra("yog");
        //     intent.putExtra("yogs",activityView);
        // Bundle bundle=new Bundle();
        // bundle.putSerializable("yogs",activityView);
        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            String yogan = bundle.getString("yog");
            String yogans = bundle.getString("yogs");
            Bundle bundle1 = new Bundle();
            bundle.putString("yoges", yogan);
            bundle.putString("yogesh", yogans);
            Task task = new Task();
            task.setArguments(bundle1);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            // Return a PlaceholderFragment (defined as a static inner class below).
                switch (position){
                    case 0:
                            Task task=new Task();
                            return task;

                    case 1:
                            Calls calls=new Calls();
                            return calls;

                }
        return null;
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return 2;
        }

        @Override
        public CharSequence getPageTitle(int position) {
           switch (position){
               case 0:
                   return "Task";
               case 1:
                   return "Call";

            }
        return null;
            }

        }
    }

This is my task fragment(where listview is gets populated)
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;
public class Task extends Fragment
{
    List<ActivityView>activityViews;
    ActivityView activityView=new ActivityView();
    ActivityListAdapter activityListAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootview= inflater.inflate(R.layout.yog,container,false);
        ListView listview=(ListView)rootview.findViewById(R.id.listView);
        if (activityViews == null)
        {
            activityViews = new ArrayList<ActivityView>();

        }
        if(getArguments()!=null) {
            getArguments().getSerializable("yog");
        }
        activityViews.add(activityView);
            activityListAdapter = new ActivityListAdapter(getActivity(), R.id.listView, activityViews);
            listview.setAdapter(activityListAdapter);
            activityListAdapter.notifyDataSetChanged();
            Button btn=(Button)rootview.findViewById(R.id.button2);
            btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                startActivity(intent);
            }
        });

        return rootview;
    }

    }

This is the activity where data go to populate listview in task fragment:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class DetailActivity extends AppCompatActivity {
    ActivityView activityView = new ActivityView();
    // public static String endpoint;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        final EditText editText = (EditText) findViewById(R.id.editText);
        final Spinner spinner = (Spinner) findViewById(R.id.spinner);
        final ArrayAdapter<CharSequence> arrayAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.yog, R.layout.support_simple_spinner_dropdown_item);
        spinner.setAdapter(arrayAdapter);
        final Button btn = (Button) findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activityView.setDescription(editText.getText().toString());
                activityView.setStatus(spinner.getSelectedItem().toString());
              //  Bundle bundle=new Bundle();
               // bundle.putSerializable("yog", activityView);
                Task task=new Task();
                //task.setArguments(bundle);
                android.support.v4.app. FragmentManager fm=getSupportFragmentManager();
                android.support.v4.app.  FragmentTransaction ft=fm.beginTransaction();
                ft.add(R.id.container,task,"");
                ft.commit();

            }
        });
    }
}

Here how to pass data between activity and fragment of viewpager can anybody help me out?  Thanks in advance!
Answer: You can use a Bundle to pass datas from an Activity to a Fragment:
In the Activity, create your Bundle and add it to the Fragment
Bundle myBundle = new Bundle();
myBundle .putLong( "exampleId", mExampleId);
myBundle .putString("exampleName", mName);
...
myFragment.setArguments( myBundle  );

Then, when the Fragment is created, get the values in your Bundle using the function getArguments() like:
Long exampleId = getArguments().getLong("exampleId");

And even set a default value if you don't find the key in your Bundle
String exampleName = getArguments().getString( "exampleName", "None"));

Comment: need to send fragment of different activities. is it the same way
Answer: You should create a NewInstance method inside your fragment. In short, it could look like this:
public class MyFragment {
    private int value;

    private static final String ARG_VALUE = "argValue";

    public static MyFragment NewInstance(int value) {
        Bundle args = new Bundle();
        args.putInt(ARG_VALUE, value);

        MyFragment fragment = new MyFragment();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate() {
        Bundle args = getArguments();
        this.value = args.getInt(ARG_VALUE, 0); // Default 0 if key not found.
    }
}

And you just pass in that value anytime you create the fragment:
MyFragment tenFragment = MyFragment.newInstance(10);

Then it can be used in a fragment transaction or inserted into a FragmentStatePagerAdapter however you need it. The same principle applies to any data type, so you can do this if you need to pass in Strings, or your own serializable object, etc.


Is the Cartesian product of $C^\infty$ functions a $C^\infty$ function?

Question: Define the Cartesian product of two functions $f:\mathbb{R}^a\to\mathbb{R}^b$ and $g:\mathbb{R}^c\to\mathbb{R}^d$ as $$(f\times g)(x,y)=(f(x),g(y)).$$ If the function $f$ and $g$ are $C^\infty$, is the function $f\times g$ also $C^\infty$?
Comment: Yes it is. It has continuous partial derivatives s of al orders.
Comment: Hint: Note that the partial derivatives distribute independently to each component
Comment: The case $h(x,y) = f(x)g(y)$ is supposedly obvious. In general the key step is to express the derivative of $t\mapsto h(tv,tw)$ in term of the partial derivatives, then the result follows easily.
Answer: The following argument shows that $f\times g$ is $C^1$ if $f$ and $g$ are both $C^1$, and then induction and be applied to prove that it is $C^\infty$ if they are $C^\infty$.
Comment: Please, consider typing out the answer using MathJax instead of an image.


no special files in /dev/ after partition created with parted

Question: I've created two partitions with parted:
mklabel gpt
mkpart primary ext4 1 80%
mkpart primary ext4 80% -1

after reboot these are properly shown
parted /dev/sdc print
Model: easyRAID  Q12 (scsi)
Disk /dev/sdc: 4999GB
Sector size (logical/physical): 2048B/2048B
Partition Table: gpt

Number  Start   End     Size    File system  Name     Flags
 1      1049kB  3999GB  3999GB               primary
 2      3999GB  4999GB  1000GB               primary

However there are no device files sdc1 and sdc2 in /dev/ directory. I can try mknod but I'm not sure what parameters should I use.
Of course, as there are no /dev/sdc1-2, so when I try to create DRBD on these I see  following message:
drbdadm create-md homes
open(/dev/sdc1) failed: No such file or directory
Command 'drbdmeta 0 v08 /dev/sdc1 internal create-md' terminated with exit code 20

Can somebody explain me why parted hasn't created these node-files and how should I proceed safely?
I'm working on clean installed CentOS 5.8.
Answer: After a lot of reading I found it.
One need to start partprobe to create the nodes files. After this step I could also start with creation of the DRBD-device.


Change variable value in middle of JavaScript in Android

Question: Further to This JavaScript code, I would like to know how can I change the internal values inside the script in the middle of the running script.
This is the code that I write in order to run the script :
Java Code :
<code>   @SuppressLint("SetJavaScriptEnabled")
    public class MainActivity extends Activity {
WebView myBrowser;
@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.activity_main);

    myBrowser = (WebView)findViewById(R.id.mybrowser);

    final MyJavaScriptInterface myJavaScriptInterface
    = new MyJavaScriptInterface(this);

    myBrowser.getSettings().setJavaScriptEnabled(true);
    myBrowser.addJavascriptInterface(myJavaScriptInterface, "AndroidFunction");
    myBrowser.loadUrl("file:///android_asset/sperm.html");
    myBrowser.loadUrl("javascript:callFromActivity(\""+50+"\")");

 // disable scroll on touch
    myBrowser.setOnTouchListener(new View.OnTouchListener() {

      public boolean onTouch(View v, MotionEvent event) {
        return (event.getAction() == MotionEvent.ACTION_MOVE);
      }
    });
}
public class MyJavaScriptInterface {
      Context mContext;

         MyJavaScriptInterface(Context c) {
             mContext = c;
         }

     }
</code>
}
XML Code :
<code><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
 android:background="@drawable/background">
 <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical"    >
    <WebView
       android:id="@+id/mybrowser"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent" />
</code>

This is the java script :
<code>    <!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  background: #000;

}

ellipse {
  fill: #6633ff;
}

path {
  fill: none;
  stroke: #33ccff;
  stroke-linecap: round;

}

.mid {
  stroke-width: 4px;

}

.tail {
  stroke-width: 2px;

}

</style>
<body >
<script src="d3.js"></script>
<script language="javascript">

    var width = 700,//350,
        height = 620;

    var n = 5,
        m = Math.floor(Math.random() * 8) + 12,
        degrees = 180 / Math.PI;

    var spermatozoa = d3.range(n).map(function() {
      var x = Math.random() * width,
          y = Math.random() * height;
      return {
        vx: Math.random() * 2 - 1,
        vy: Math.random() * 2 - 1,
        path: d3.range(m).map(function() { return [x, y]; }),
        count: 0
      };
    });

    var svg = d3.select("body").append("svg")
        .attr("width", width)
        .attr("height", height);

    var g = svg.selectAll("g")
        .data(spermatozoa)
      .enter().append("g");

    var head = g.append("ellipse")
        .attr("rx", 6.5)
        .attr("ry", 4);

    g.append("path")
        .datum(function(d) { return d.path.slice(0, 3); })
        .attr("class", "mid");

    g.append("path")
        .datum(function(d) { return d.path; })
        .attr("class", "tail");

    var tail = g.selectAll("path");

    d3.timer(function() {
      for (var i = -1; ++i < n;) {
        var spermatozoon = spermatozoa[i],
            path = spermatozoon.path,
            dx = spermatozoon.vx,
            dy = 0,
            x = path[0][0] += dx,
            y = path[0][1] += dy,
            speed = Math.sqrt(dx * dx + dy * dy),
            count =  4,
            k1 = -5 - speed / 1;

        // Bounce off the walls.
        if (x < 0 || x > width) spermatozoon.vx *= -1;
        if (y < 0 || y > height) spermatozoon.vy *= -1;

        // Swim!
        for (var j = 0; ++j < m;) {
          var vx = x - path[j][0],
              vy = y - path[j][1],
              k2 = Math.sin(((spermatozoon.count += count) + j * 3) / 300) / speed;
          path[j][0] = (x += dx / speed * k1) - dy * k2;
          path[j][1] = (y += dy / speed * k1) + dx * k2;
          speed = Math.sqrt((dx = vx) * dx + (dy = vy) * dy);
        }
      }

      head.attr("transform", headTransform);
      tail.attr("d", tailPath);
    });

    function headTransform(d) {
      return "translate(" + d.path[0] + ")rotate(" + 0 + ")";
    }

    function tailPath(d) {
      return "M" + d.join("L");
    }
    function callFromActivity(n1)
    {
        n = n1;
    }

</script>
</code>
I trying to to change the n value from "callFromActivity" function in the javascript.
What should i change?
Thanks!
Answer: Did you tried changing that n value in an html page first?
Comment: No, I followed [This](http://android-er.blogspot.co.il/2011/10/call-javascript-inside-webview-from.html)  guide to do what I want, but this is doesn't work for me.
Comment: OK, first try it on HTML with a button. Find the right way to set the variable. Then use android to change it. I don't think android is the problem.


Winform TabControl style

Question: I'm once again turning to you since I can't find an answer anywhere else. I have a TabControl, but I want to get rid of the ugly orange bar on top of the selected tabs. I would also like to make the tab text BOLD when selected and NORMAL when not.
Is there any easy way to achieve that goal ? I don't want to use the Appearance Buttons or Flat Buttons.
Thanks for your help !
Comment: FYI, I'm under Win XP with VS 2008 (.NET v3.5)
Comment: I found this from Beth Massi : http://bethmassi.blogspot.com/2005/01/disable-tabs-on-tabcontrol.html But I still cannot change the font of the tab to BOLD when selected. I also have a problem with the tab size. My text is reformated on 2 lines when the tab is not selected.
Answer: This appearance is controlled by the Visual Styles theme selected in your operating system.  In general, users do not appreciate any program that ignores their theme settings, especially when they paid money for a custom one.  But you can get what you want, you'll have to set the DrawMode property to OwnerDrawFixed and implement a handler for the DrawItem event.  There's a good example to get you started in the MSDN Library article for this event.  Just change the font assignment in that sample code.
Comment: Thanks, I'll try it. We are in a government business and they have very strict visual rules (dating from almost 10 years). Rest of the system is made in Cool:Gen which have this appearance, so I have to fit with them. Users cannot change anything on their machines. And if they do somehow, they're on their own.
Comment: Sound to me you should just turn visual styles support off.  It wasn't around yet 10 years ago.  Delete the Application.EnableVisualStyles() call in the Main() method in Program.cs
Comment: I got it to work with your suggestion and Beth exemple. Thanks !

Winform TabControl style


ANTLR Template translator match part of grammar

Question: I wrote a grammar for a language and now I want to treat some syntactic sugar constructions, for that I was thinking of writing a template translator.
The problem is I want my template grammar to translate only some constructions of the language and leave the rest as it is.
For example:
I have this as input:
class Main {
   int a[10];
}

and I want to translate that into something like:
class Main {
   Array a = new Array(10);
}

Ideally I would like to do some think like this in ANTLR
grammer Translator
options { output=template;}

    decl
         : TYPE  ID '[' INT ']' -> template(name = {$ID.text}, size ={$INT.text})
              "Array <name> = new Array(<size>);

I would like it to leave the rest of the input that doesn't match rule decl as it is.
How can I achieve this in ANTLR  without writing the full grammar for the language ?
Answer: I would simply handle such things in the parser grammar.
Assuming you're constructing an AST in your parser grammar, I guess you'll have a rule to parse input like Array a = new Array(10); similar to:
decl
  :  TYPE ID '=' expr ';' -> ^(DECL TYPE ID expr)
  ;

where expr eventually matches a term like this:
term
  :  NUMBER
  |  'new' ID '(' (expr (',' expr)*)? ')' -> ^('new' ID expr*)
  |  ...
  ;

To account for your short-hand declaration int a[10];, all you have to do is expand decl like this:
decl
  :  TYPE ID '=' expr     ';' -> ^(DECL TYPE    ID expr)
  |  TYPE ID '[' expr ']' ';' -> ^(DECL 'Array' ID ^(NEW ARRAY expr))
  ;

which will rewrite the input int a[10]; into the following AST:

which is exactly the same as the AST created for input Array a = new Array(10);.
EDIT
Here's a small working demo:
grammar T;

options {
  output=AST;
}

tokens {
  ROOT;
  DECL;
  NEW='new';
  INT='int';
  ARRAY='Array';
}

parse
  :  decl+ EOF -> ^(ROOT decl+)
  ;

decl
  :  type ID '=' expr     ';' -> ^(DECL type  ID expr)
  |  type ID '[' expr ']' ';' -> ^(DECL ARRAY ID ^(NEW ARRAY expr))
  ;

expr
  :  Number
  |  NEW type '(' (expr (',' expr)*)? ')' -> ^(NEW ID expr*)
  ;

type
  :  INT
  |  ARRAY
  |  ID
  ;

ID     : ('a'..'z' | 'A'..'Z')+;
Number : '0'..'9'+;
Space  : (' ' | '\t' | '\r' | '\n') {skip();};

which can be tested with the class:
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class Main {
  public static void main(String[] args) throws Exception {
    String src = "Array a = new Array(10); int a[10];";
    TLexer lexer = new TLexer(new ANTLRStringStream(src));
    TParser parser = new TParser(new CommonTokenStream(lexer));
    CommonTree tree = (CommonTree)parser.parse().getTree();
    DOTTreeGenerator gen = new DOTTreeGenerator();
    StringTemplate st = gen.toDOT(tree);
    System.out.println(st);
  }
}

Comment: I tried that, but I when I try to pass a string in the rewrite rule it would issue an error: reference to undefined token in rewrite rule: 'Array' and same for 'new'
Comment: Actually the syntax is TOKEN["literal"] so I need to do something like:  TYPE ID '[' expr ']' ';' -> ^(DECL TYPE["Array"] ID ^(NEW TYPE["Array"] expr))
Comment: @JohnRetallack, yeah, I made a mistake: I used a literal token in a rewrite rule that wasn't present in the parser rule. I fixed it, and added a working example.
Comment: @JohnRetallack, yes, well done, that `T['text']` might work too.


Are there books specifically on the subject of modulation?

Question: Questions concerning smooth modulations, especially how to effect them, keep coming up in my own efforts at composition. Apart from Max Reger's little treatise (which in some ways is and in other ways isn't helpful), are there any other books specifically on this subject? Or at least a harmony text that delves into this subject extensively. To be clear, I don't need more information about pivot chords, etc., but rather on how to prepare the ground for my modulations in a smooth and convincing way, and on how best to confirm the new key.
Comment: Have you considered the horse's mouth? Pick some masterpieces and study the modulations yourself with regard to harmony, harmonic rhythm, motive, and so on. This isn't an answer to your question because I'm not naming books, but I'm convinced it's the best approach because the knowledge gained comes through your own consideration, and you learn more great music in the process. Once you start to notice trends, you could ask more specific questions here about their validity, for example.
Comment: Thanks! That's always sound advice, of course. But I simply need a bit more formalized guidance. (And I like to learn from books.)
Answer: Frank Shepard has a book on the subject: How To Modulate."
It's available on the internet: https://archive.org/details/howtomodulatesim00shep/page/n4
Comment: Thanks a million times over! Something like this is exactly what I was looking for.


CUDA, how to implement dynamic array of struct in CUDA kernel

Question: I am trying to implement a structure that holds arrays of data
and I want to implement dynamic array, something like:
struct myStruct {
  float3 *data0, *data1;
};

__global__ void kernel(myStruct input) {
  unsigned int N = 2;
  while(someStatements) {
    data0 = new float3[N];
    // do somethings
    N *= 2;
  }
}

How can I do something like this in a CUDA kernel?
Answer: If you are going to run this code on either a compute capability 2.x or 3,x device, with a recent version of CUDA, your kernel code is very nearly correct. The C++ new operator is supported in CUDA 4.x and 5.0 on Fermi and Kepler hardware. Note that memory which is allocated using new or malloc is allocated on runtime heap on the device. It has the lifespan of the context in which is was created, but you currently cannot directly access it from the CUDA host API (so via cudaMemcpy or similar).
I turned your structure and kernel into a simple example code which you can try for yourself to see how it works:
#include <cstdio>

struct myStruct {
    float *data;
};

__device__
void fill(float * x, unsigned int n)
{
    for(int i=0; i<n; i++) x[i] = (float)i;
}

__global__
void kernel(myStruct *input, const unsigned int imax)
{
    for(unsigned int i=0,N=1; i<imax; i++, N*=2) {
        float * p = new float[N];
        fill(p, N);
        input[i].data = p;
    }
}

__global__
void kernel2(myStruct *input, float *output, const unsigned int imax)
{
    for(unsigned int i=0,N=1; i<imax; i++, N*=2) {
        output[i] = input[i].data[N-1];
    }
}

inline void gpuAssert(cudaError_t code, char * file, int line, bool Abort=true)
{
    if (code != 0) {
        fprintf(stderr, "GPUassert: %s %s %d\n", cudaGetErrorString(code),file,line);
        if (Abort) exit(code);
    }
}
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }

int main(void)
{

    const unsigned int nvals = 16;
    struct myStruct * _s;
    float * _f, * f;

    gpuErrchk( cudaMalloc((void **)&_s, sizeof(struct myStruct) * size_t(nvals)) );
    size_t sz = sizeof(float) * size_t(nvals);
    gpuErrchk( cudaMalloc((void **)&_f, sz) );
    f = new float[nvals];

    kernel<<<1,1>>>(_s, nvals);
    gpuErrchk( cudaPeekAtLastError() );

    kernel2<<<1,1>>>(_s, _f, nvals);
    gpuErrchk( cudaPeekAtLastError() );
    gpuErrchk( cudaMemcpy(f, _f, sz, cudaMemcpyDeviceToHost) );
    gpuErrchk( cudaDeviceReset() );

    for(int i=0; i<nvals; i++) {
        fprintf(stdout, "%d %f\n", i, f[i]);
    }

    return 0;
}

A few points to note:

This code will only compile and run with CUDA 4.x or 5.0 on a Fermi or Kepler GPU
You must pass the correct architecture for your GPU to nvcc to compile it (for example I used nvcc -arch=sm_30 -Xptxas="-v" -o dynstruct dynstruct.cu to compile for a GTX 670 on linux)
The example code uses a "gather" kernel to copy data from the structure in runtime heap to an allocation which the host API can access so that the results can be printed out. This is a work around for the limitation I mentioned earlier regarding cudaMemcpy not being able to copy directly from addresses in runtime heap memory. I was hoping this might be fixed in CUDA 5.0, but the most recent release candidate still has this restriction.


MS Access combobox in a continuous form changes value for all rows when I change value for one row

Question: I've been trying to solve this issue for the past 2 hours but in vain...
So I hope that you can help me with a solution to this problem..
I have a continuous form that shows many rows at a time, I added a combobox to the form that shows some values to choose from another table, while having the default value shown from the same table. Problem is, as soon as I change the value in one of the comboboxes in the form, all the other comboboxes changes value to the same one as the latter...
How do I prevent that??
Thanks a million in advance
Answer: Tony's right.  You can't do what you want with an unbound control.
However, it's not entirely clear what your intent is based on your question.  If changing the combobox should update a field on the current record, then you just need to make it a bound control.  You would do this by setting the combobox's ControlSource property equal to the appropriate field in the underlying table or query.
Comment: It's very common for the first of two cascading combo boxes to be unbound. I would never use a continuous form to edit when I needed that -- instead, I'd use the continuous form/datasheet form as a list, and load the editable data in a subform linked with LinkMaster/LinkChild properties using an expression pointing to the PK of the record selected in the continuous/datasheet form.
Answer: I faced similar issue and got it sorted recently. I had created a tabular form and had introduced a combo box in the form, picking up values from a value list. Whenever I used to select a value of this combo box list, the selection used to get reflecting on all the records. To correct this, you should do the following:-
1) Introduce the combo box field in the source table of your form. Instead of clicking on the drop down box in the tool box, click on the Add existing fields link on the ribbon and select the newly added combo box field from the table. This will add a combo box field and a corresponding label in the form area. For a tabular form, you may want to delete the corresponding label and introduce a non-linked label in the form header.
This solved the issue for me. Hope it will be of some value to you.
May be a couple of years late and am pretty sure that it's been long since you figured out this problem,but it's good to leave it in the thread for someone,who may be in need.
Answer: Can't be done.  This is one of the limitations of Access continuous forms and unbound controls.
Comment: ...and is one of several reasons why I generally don't use editable continuous/datasheet forms, particularly when there are conditional alterations to controls (such as cascading combo boxes).
Answer: Because of this strange behavior I just hide that field.
In the datasheet view:

Right-Click the field header that matches your combobox
Select "Hide Fields"

Now the person can see the records and they can use the form. They don't need a field for the combobox.


Elimination of variables the old-fashioned way

Question: I'm trying to eliminate variables in some fairly simple sets of equations. A typical example is:
$$ 9 x^2 + 18 xy + 9 y^2 - 32 = 256z$$
$$ 9 x^2 + 6 xy - 3 y^2 - 8 = 376z$$
$$ 9 x^2 - 6 xy  + y^2 = 512z$$
I'd like to eliminate $x$ and $y$. Mathematica tells me that the answer is $ 161 z^2 -162z + 1 = 0 $. OK. Good.
But how would I do this elimination manually (without Mathematica). I'm hoping that there is some fairly simple mechanical process that I can express in a few hundred lines of C code.
I realize that general elimination procedures are pretty complex, but this sort of problem looks quite special and therefore easier (I hope). Roughly speaking, it's just a system of "linear" equations in the variables $x^2$, $xy$, $y^2$, and $z$. Is there some sort of diagonalization process that can be applied, for example ?
Edit:
From the proposed answer below, I see that things can be simplified by setting $p=3x+3y$ and $q = 3x-y$. Then the equations become:
$$p^2 = 32 + 256z$$
$$pq = 8 + 376z$$
$$q^2 = 512z$$
I can then eliminate $p$ and $q$. Is there always a linear transformation that simplifies the problem in this way? If there is, how do I compute it?
Comment: If you can diagonalize the problem into something like $u^2 = \lambda_1$, $uv=\lambda_2$ and $v^2=\lambda_3$, then you could use that $u^2v^2 = (uv)^2$, thus $\lambda_1\lambda_3 = \lambda_2^2$.
Comment: Yes. But my remaining question is ... how do I know when this diagonalization is possible, and how do I write code to find it ? Specifically, is it always possible ??
Comment: Are you familiar with Gauss-Jordan elimination? The answer by DonAntonio illustrates how it works. (http://en.wikipedia.org/wiki/Gauss%E2%80%93Jordan_elimination)
Comment: I see how Gaussian elimination helps with the "mechanical" approach, but not the clever one. How would it tell me that $p=3x+3y$ and $q=3x-y$ is the magic simplifying change of variables?? I don't think DonAntonio's answer shows that.
Comment: His method reduces the system of equations to a system that will be of the type $1\cdot x^2 + 0\cdot xy + 0\cdot y^2 = f_1(z)$, $xy = f_2(z)$ and $y^2 = f_3(z)$. And then you have that $f_1f_3 = f_2^2$. You don't really need to find that substitution.
Answer: You can use gaussian elimination (or other equivalent methods) to find expressions for $x^2$, $xy$ and $y^2$ in terms of $z$ - treating them as independent variables, and substitute these to get an equation for z. Then you still have to solve for $x$ and $y$.
There is an easier way in this case, but in a general case this makes progress.
Comment: Thanks. Looks promising. I don't really see the "gives a linear equation for z" part, but maybe it will become clear when I work through the details.
Comment: @bubba Deleted linear comment as it is incorrect.
Answer: Mark's idea with the unknowns $\,x^2\,,\,xy\,,\,y^2\,$ , using the augmented matrix:
$$\begin{pmatrix} 9&18&9&\;\;256z+32\\9&6&\!\!\!\!-3&\;\;376z+8\\9&\!\!\!\!-6&1&\;\;512z\end{pmatrix}\stackrel{\begin{cases}R_2-R_1\\R_3-R_1\end{cases}}\longrightarrow \begin{pmatrix} 9&18&9&\;\;256z+32\\0&\!\!\!\!-12&\!\!\!\!-6&\;\;120z-24\\0&\!\!\!\!-24&\!\!\!\!-8&\;\;256z\end{pmatrix}\longrightarrow$$
$$\stackrel{R_3-2R_2}\longrightarrow \begin{pmatrix} 9&18&9&\;\;256z+32\\0&\!\!\!\!-12&\!\!\!\!-6&\;\;120z-24\\0&0&4&\;\;16z+48\end{pmatrix}$$
We get thus
$$\text{From}\,\,R_3\,:\;\;4y^2=16z+48\Longrightarrow y=\pm\sqrt{4z+12}$$
$$\text{From}\,\,R_2\wedge R_3\,\,\text{above}\;\;-12xy-6y^2=120z-24\Longrightarrow $$
$$\pm 2x\sqrt{4z+12}-16z-48=20z-4\Longrightarrow \pm x\sqrt{4z+12}=18z+22\Longrightarrow$$
$$x=\pm\frac{9z+11}{z+3}$$
and etc.
Answer: Clever Observant Solution (as in Mark's comment)
Denote the right-hand sides by $a$, $b$, $c$. In other words, $a = 32+256z$, $b = 8 + 376z$, $c = 512z$. Also, let $p = 3x + 3y$ and $q = 3x - y$. Then the equations become simply:
$$ p^2 = a \quad ; \quad pq = b \quad ; \quad q^2 = c$$
From this, we get $b^2 - ac = 0$. Substituting for $a$, $b$, $c$ gives us the same result we got from Mathematica: $161z^2 -162z + 1 = 0$.
Dumb Mechanical Solution (which is what I need)
Let $M$ be the matrix
$$ M = \begin{pmatrix} 9&18&9\\9&6&\!\!\!\!-3\\9&\!\!\!\!-6&1\end{pmatrix}$$
and let $u = x^2$, $v = xy$, $w = y^2$. Then the original equations can be written as
$M.[u, v, w]^t = [a, b, c]^t$.
Using standard techniques (eigenvalues or Gaussian elimination), we can find a diagonal matrix $D$ and an invertible matrix $P$ such that $P^{-1}MP = D$. The solution of the system of equations is then $[u, v, w]^t = PD^{-1}P^{-1}[a, b, c]^t$. Setting $v^2 = uw$ again gives us the equation $161z^2 -162z + 1 = 0$.
Answer: Factorize the left sides to separate x and y, and it's easy later.
Comment: OK. I wrote the first equation using $(3x+3y)^2$ and the third one using $(3x-y)^2$. Sorry to be dense, but I don't see how that helps.
Comment: you can now express x as something else, and put that instead of x in the other equations
Comment: or, another approach, you can multiply the first equation by $A$, the second by $B$, the third by $C$, and make a system of equations, that they must satisfy in order to cancel out $x^2$, $xy$, and $y^2$, but this will work only if you are lucky.
Comment: In this case we get three equations $(3x+3y)^2=A, (3x+3y)(3x-y)=B, (3x-y)^2=C$. This allows us to put AC=B^2 which gives a quadratic equation in $z$.
Comment: @Mark. Thanks. I just realized that, too. But this requires some insight, so I have no idea how to code it. Your other answer looks better (dumber :-))


Asymptotic behaviour and curve guessing

Question: So I have these data points which I plotted and found that for small $x$, my data behaves like $y=x^4$ but for large $x$, it behaves like $y=x^3$. From this, can I guess the relationship between $y$ and $x$? I tried $y=e^{-x}x^4+x^3$, but this doesn't work since the exponential doesn't kill of the $x^4$ quite enough at large values.
Comment: Where are *these data points* ?
Answer: I've always liked the very simple
$$y=\frac{1}{\frac{1}{x^a}+\frac{1}{x^{b}}}$$
When $x$ is small, the $x^a$-term dominates and you get a slope of $a$ on the log-log plot, whereas when $x$ is large the $x^{b}$-term dominates with a slope of $b$ on the log-log plot.
Of course, without access to your data it's impossible to tell if the $x$-values are sufficiently large to make this feasible. However, that might be fixed with small modifications to the equation.

Asymptotic behaviour and curve guessing


Implicit differentiation of $(3x+2y^2)/(x^2+y^2) = 4$? Two approches, two answers..

Question: My problem is that I get two different answers when I use two different approaches to differentiate this problem with respect to $x$:
$$\left(\frac{3x+2y^2}{x^2+y^2}\right) =4$$
My first thought was to use the quotient rule, which gives me the answer
$$\frac{dy}{dx} = \frac{4x^2y+3x^2-3y^2}{4x^2y-6xy}.$$
However, someone showed me that if you first get rid of the rational expression like this:
$$3x+2y^2 = 4x^2+4y^2$$
and then differentiate, you get
$$\frac{dy}{dx}=\frac{8x-3}{4y}$$ (which is also the answer wolfram alpha gives).
I tried this also on a much simpler rational expression (to check that the problem wasn't just me beigng bad at algebra) and got different results again! Why?
Thanks for any help!
Comment: It seems that you committed error in both calculations.
Comment: @mathlover What is the error in the first (quotient rule) calculation then?
Answer: Good news: the results may seem different, but aren't.

and then differentiate, you get
  $$\frac{dy}{dx}=\frac{8x-3}{4y}$$

There's a small (sign) mistake here, you should find:
$$\frac{dy}{dx}=\frac{3-8x}{4y}$$
This may look different from the form you found via the quotient rule, but remember that you have a relation between $x$ and $y$, the original (implicit) function. So we hope the following equality holds:
$$\begin{array}{crcl} {} & \displaystyle \frac{4x^2y+3x^2-3y^2}{4x^2y-6xy} & = & \displaystyle  \frac{3-8x}{4y} \\[5pt]
{\Leftrightarrow} & 4y\left( 4x^2y+3x^2-3y^2 \right) & = & \left( 4x^2y-6xy \right) \left( 3-8x \right)\end{array}$$
Expanding, moving everything to the left-hand side and simplifying gives:
$$2 y \left( 16 x^3-24 x^2+4 x \cdot \color{blue}{2y^2}+9 x-3 \cdot \color{blue}{2y^2} \right)=0 \tag{$*$}$$
But from:
$$\left(\frac{3x+2y^2}{x^2+y^2}\right) =4$$
we have that:
$$3x+2y^2=4x^2+4y^2 \Rightarrow \color{blue}{2y^2=3x-4x^2}$$
Substitution into $(*)$ and simplifying, will give you $0$.


Agrupar valores de um array multidimensional com mesmo valor e exibir numa tabela

Question: Bom dia,
Eu tenho um array multidimensional, mostrado a seguir:
    array(3) {
    [0]=>
    array(1) {
        [0]=>
        object(stdClass)#35 (3) {
        ["id"]=>
        string(2) "73"
        ["valor"]=>
        string(6) "200.00"
        ["compra_nome"]=>
        string(8) "Chaveiro"
        }
    }
    [1]=>
    array(3) {
        [0]=>
        object(stdClass)#36 (3) {
        ["id"]=>
        string(2) "74"
        ["valor"]=>
        string(6) "500.00"
        ["compra_nome"]=>
        string(17) "Pulseira"
        }
        [1]=>
        object(stdClass)#37 (3) {
        ["id"]=>
        string(2) "74"
        ["valor"]=>
        string(6) "200.00"
        ["compra_nome"]=>
        string(14) "Chave de Fenda"
        }
        [2]=>
        object(stdClass)#38 (3) {
        ["id"]=>
        string(2) "74"
        ["valor"]=>
        string(6) "100.00"
        ["compra_nome"]=>
        string(15) "Chuveiro"
        }
    }
    [2]=>
    array(1) {
        [0]=>
        object(stdClass)#39 (3) {
        ["id"]=>
        string(2) "75"
        ["valor"]=>
        string(6) "100.00"
        ["compra_nome"]=>
        string(14) "Lampada"
        }
    }
    }

Eu preciso armazenar em um array os valores que o id, com isso eu precisaria fazer um foreach para montar uma tabela com os valores, devendo ficar assim:

O meu código de PHP:
$clientes = $this->Oportunidades_model->buscaClientesComCondicoes($data);
$clientes_array =  array();
        foreach ($clientes as $key => $value) {
            // var_dump($value->codigo_cli);
            $clientes_array[] = $this->Oportunidades->buscaCompras($value->codigo_cli,$data);
        }

PS: A variavel que traz o o array multidimensional no inicio da pergunta é  $clientes_array
Sem ideias ;/
Estou utilizando PHP com CodeIgniter mas pode ser php puro tambem..
Edit: está retornando com seu codigo:

Precisaria que retornasse 3 tabelas e junta-se os registros do 73,74,75 em 3 tabelas diferentes igual a imagem a cima;
Answer: Amigo seu array inicial já possui a lista ordenada pelos ids, você só precisa percorrê-lo e montar a tabela.

ID	VALOR	COMPRA NOME

        <?php foreach($clientes_array  as $items):?>
            <?php foreach($items as $item):?>

            <?php print $item->id; ?>
            <?php print $item->valor; ?>
            <?php print $item->compra_nome?>

            <?php endforeach;?>
        <?php endforeach;?>

Caso queira agrupar o registros pelo valor:
$lista = [];

foreach ($clientes_array as $items) {
    foreach ($items as $item) {
        if (!isset($lista[$item->valor]))
        {
            $lista[$item->valor] = [];
        }

        $lista[$item->valor][] = $item;
    }
}

ID	VALOR	COMPRA NOME

        <?php foreach($lista as $items):?>
            <?php foreach($items as $item):?>

            <?php print $item->id; ?>
            <?php print $item->valor; ?>
            <?php print $item->compra_nome?>

            <?php endforeach;?>
        <?php endforeach;?>

Comment: Obrigado pela sua respostsas vinicius! Eu coloquei um edit no POST mostrando como está ficando. São 3 ids diferentes e e 6 tabelas. Eu precisaria que fossem 3 tabelas com os respectivos ids, e os registros que são do mesmo ID ficassem na mesma tabela. Ou seja tabela id 74(possui 3 registros) ai nessa tabela vai ter os 3 registros do ID 74.


Calculating probabilities given CDF

Question: Let $X$ be a random variable with CDF:
$$
F_{X}(x) = \left\{ \begin{array}{ll}
0, & \textrm{$x<-1$}\\
\frac{1}{2}, & \textrm{$-1\leq x<3$}\\
1-e^{-x}, & \textrm{$ 3\leq x $}
\end{array} \right.
$$
How to compute

$\mathbb{P}(|X-2|>1/2)$
$\mathbb{P}(|X-2|>1)$
$\mathbb{P}(\min(3,X)>4)$
$\mathbb{P}(\min(3,X)<4)$
$\mathbb{P}(\max(3,X)<4)$

EDIT: For the first one I got
$$ \mathbb{P}(|X-2|>1/2) = \mathbb{P}(X-2>1/2)+\mathbb{P}(X-2<-1/2) =\mathbb{P}(X>5/2)+\mathbb{P}(X<3/2)=\\=1-1/2+1/2=1$$
Similarly second one I get $e^{-3}$.
Then third probability I tried using conditional probability like this
$$ \mathbb{P}(\min(3,X)>4)= \mathbb{P}(3>4|X\geq3)+\mathbb{P}(X>4|X\leq3)=0$$
Other min-max probabilities I tried the same way but I think it is wrong approach.
Comment: You would learn a lot more in the long run if you post your work and have the people here guide you to the right track rather than (politely) asking people to spoon-feed you step-by-step solutions to what appears to be homework.
Comment: Alright, I have added my work. By the way, it is not any homework, just an exercise from problem set which I want to do correctly and understand.
Comment: I obtain $\mathbb{P}(|X-2|>1/2)=1$ too, but $\mathbb{P}(|X-2|>1)=1/2+e^{-3}$. Might I suggest using the relation $\{|X-a|>c\}=\{|X-a|\leq c\}^c$ so that $\mathbb{P}(|X-a|>c)=1-\mathbb{P}(|X-a|\leq c)=1-(F(a+c)-F(a-c))$. It is often easier dealing with complements of events like $\{|X-a|>c\}$. The conditional formula you cite is incorrect. You are forgetting to multiply by probability of the conditioned events: $\mathbb{P}(A)=\sum_i \mathbb{P}(A|B_i)\mathbb{P}(B_i).$ Though this comment is too late now.
Comment: But now I understand, thank you!
Answer: Remember that for all $a,b\in\mathbb R$ such that $a<b$, we have $\mathbb P(a<X\le b)=\mathbb P(X\le b)-\mathbb P(X\le a)=F_X(b)-F_X(a)$.
If the inequality is not strict on the left side, we have $\mathbb P(a\le X\le b)=F_X(b)-F_X(a-)$, where $F_X(a-)=\lim_{x\to a-}F_X(x)$.
We also have $\mathbb P(X<b)=F_X(b_-)$.
Then we have
$$\begin{align*}
\mathbb P(\vert X-2\vert>\frac12)&=1-\mathbb P(\vert X-2\vert\le\frac12)=1-\mathbb P(-\frac12\le X-2\le\frac12)\\
&=1-\mathbb P(\frac32\le X\le\frac52)=1-(F_X(\frac52)-F_X(\frac32-))\\
&=1-(\frac12-\frac12)=1.
\end{align*}$$
$$\begin{align*}\mathbb P(\vert X-2\vert>1)&=1-\mathbb P(\vert X-2\vert\le 1)=1-\mathbb P(-1\le X-2\le1)\\
&=1-\mathbb P(1\le X\le 3)=1-(F_X(3)-F_X(1-))=1-(1-\textrm e^{-3}-\frac12)\\
&=\textrm e^{-3}+\frac12\cdot
\end{align*}$$
$\mathbb P(\min(3,X)>4)=0$ since $4>3\ge\min(3,X)$.
$\mathbb P(\min(3,X)<4)=1$ since $\min(3,X)\le 3<4$.
$\mathbb P(\max(3,X)<4)=\mathbb P(X<4)=F_X(4-)=1-\textrm e^{-4}$.
Comment: Thank you very much!

Calculating probabilities given CDF


Can one show that $L(E)=\langle s]$ on a partially ordered set?

Question: This is a follow up question to one I posted earlier. I'm trying to decide that if for $(S,\preceq)$ a partially ordered set and $E\subseteq S$, one has $L(E)=\langle s]$ for some $s\in S$ iff $\inf E$ exists, and in particular, $L(E)=\langle\inf E]$.
I'm simply curious about showing that $L(E)\subseteq\langle\inf E]$ when $\inf E$ exists. Taking some $x\in L(E)$, if $x$ is comparable to $\inf E$, then $x\preceq\inf E$, and so $x\in\langle\inf E]$. But what if $x$ and $\inf E$ are not comparable? Is it still possible to show such an inclusion?
Edit: $\langle s]$ is the set {$x\in S \ | \ x\preceq s$}.
Comment: Sorry, what does the notation $\langle s]$ mean?
Comment: My apologies for not making it clear. I have put the explanation in the original question.
Comment: Good, that is what I guessed.  Thanks for clarifying.
Answer: If I correctly understand your question, it boils down to whether $\inf E$ is "larger" than or equal to every lower bound of $E$.  Isn't this the definition?  A greatest lower bound of a set must be, by definition, comparable to and "larger" than (or equal to) every lower bound.
I think the confusion arises because, as seen in your last question, having an inf does not imply that the set of lower bounds is totally ordered.  It does imply, however, that all lower bounds are comparable to the inf.
So the answer is yes: If $E$ has an inf, then $L(E)=\langle \inf E]$.  Anything smaller than a lower bound is also a lower bound, which gives the right to left inclusion.  The other inclusion follows from the fact that $\inf E$ is the "greatest" lower bound.
Comment: Ah yes, that is definitely why I was confused. I knew an infimum had to be the 'largest' all other lower bounds, but I wasn't quite sure if that meant it would necessarily be comparable as I had never seen that mentioned explicitly. In retrospect, I feel a little silly. Anyway, thanks for answering both.
Comment: @yunone: You're welcome.  I might have better answered the previous question by explicitly mentioning this.


Getting value repeated for next year if value is recurring

Question: I have a record for project and release number and I need to repeat the row in next year if value is recurring.
First image is showing the data that I have it
My expected output is:

Explanation of output: In year 2017 value_type ITA has frequency as Recurring so, This value should be repeated in all next year(i.e 2018, 2019 and 2020). like that in year 2018 OC and PA is recurring so it also need to repeated in 2019 and 2020.
For that I created a new view for only recurring value and tried to join that view with base table. But it is not giving me proper result.
Can anyone please help me with this?
Thanks in advance..
Comment: How about other columns (RUN_RATE)? Do they repeat? And will you have it repeat forever?
Comment: Where does it stop? Perhaps better to have a `Repeat_Until` field that has a year where the repeating finishes.
Comment: Only Recurring value rows are going to repeated...@DVT
Comment: It will stop at the max(years)....In my project the max year is 2020 then it will display record till 2020.....@mendosi
Answer: <code>    DECLARE @EndYear INT =2020 --Also you can get from data by MAX(Year)
    ;WITH tb(PROJECT_ID,RELEASE_NO,[YEAR],VALUE_TYPE,VAL_DES,COST,RUN_TATE,FREQUENCY)
    AS(
       SELECT 111,1,2016,'IT','EXPENSE',0,NULL,NULL UNION
       SELECT 111,1,2016,'IR','INCOME',10000,NULL,NULL UNION
       SELECT 111,1,2016,'OC','EXPENSE',-200000,NULL,NULL UNION
       SELECT 111,1,2016,'Vendor','EXPENSE',-5000,NULL,NULL UNION
       SELECT 111,1,2017,'BC','INCOME',200000,NULL,NULL UNION
       SELECT 111,1,2017,'ITA','INCOME',5000,5000,'Recurring' UNION
       SELECT 111,1,2017,'OC','EXPENSE',-200000,NULL,NULL UNION
       SELECT 111,1,2018,'OC','EXPENSE',-10000,-10000,'Recurring' UNION
       SELECT 111,1,2018,'PA','INCOME',100000,100000,'Recurring' UNION
       SELECT 111,1,2019,'icc','INCOME',500,NULL,NULL UNION
       SELECT 111,1,2020,NULL,NULL,NULL,NULL,NULL
    ),Recurring AS (
       SELECT tb.PROJECT_ID,tb.RELEASE_NO,tb.VALUE_TYPE,tb.VAL_DES,MIN([YEAR]) AS StartYear,MAX(COST) AS COST,MAX(tb.RUN_TATE) AS RUN_TATE
       FROM tb WHERE FREQUENCY='Recurring'
       GROUP BY tb.PROJECT_ID,tb.RELEASE_NO,tb.VALUE_TYPE,tb.VAL_DES
    )
    SELECT * FROM tb union
    SELECT r.PROJECT_ID,r.RELEASE_NO,n.number AS [YEAR],r.VALUE_TYPE,r.VAL_DES,r.COST,r.RUN_TATE,NULL AS FREQUENCY FROM
    Recurring AS r
    OUTER  APPLY (
       SELECT sv.number FROM master.dbo.spt_values AS sv WHERE sv.type='P' AND sv.number BETWEEN r.StartYear+1 AND @EndYear
    )n
</code>

PROJECT_ID  RELEASE_NO  YEAR        VALUE_TYPE VAL_DES COST        RUN_TATE    FREQUENCY
----------- ----------- ----------- ---------- ------- ----------- ----------- ---------
111         1           2016        IR         INCOME  10000       NULL        NULL
111         1           2016        IT         EXPENSE 0           NULL        NULL
111         1           2016        OC         EXPENSE -200000     NULL        NULL
111         1           2016        Vendor     EXPENSE -5000       NULL        NULL
111         1           2017        BC         INCOME  200000      NULL        NULL
111         1           2017        ITA        INCOME  5000        5000        Recurring
111         1           2017        OC         EXPENSE -200000     NULL        NULL
111         1           2018        ITA        INCOME  5000        5000        NULL
111         1           2018        OC         EXPENSE -10000      -10000      Recurring
111         1           2018        PA         INCOME  100000      100000      Recurring
111         1           2019        icc        INCOME  500         NULL        NULL
111         1           2019        ITA        INCOME  5000        5000        NULL
111         1           2019        OC         EXPENSE -10000      -10000      NULL
111         1           2019        PA         INCOME  100000      100000      NULL
111         1           2020        NULL       NULL    NULL        NULL        NULL
111         1           2020        ITA        INCOME  5000        5000        NULL
111         1           2020        OC         EXPENSE -10000      -10000      NULL
111         1           2020        PA         INCOME  100000      100000      NULL
Comment: Thank you very much @Nolan Shang


Reliable Time Protocol Server Online?

Question: I have some OLD legacy hardware that I'd like to keep time synchronized on.  It supports the TIME protocol (not NTP).  Are there any reliable TIME servers online anymore?  If so, where?
-M
Answer: NIST still provide some TIME (RFC-868) services, alongside NTP and DAYTIME.
Just pay attention to the notice in green at the bottom of the page.
Answer: If you have a Unix-like box around, I believe it should be possible to set up your own TIME server fairly easily - Wikipedia suggests that the daemon is installed by default on many distributions, requiring only that you enable it in your inetd.conf.  The time on the Unix box could be kept in sync with NTP.
Comment: Thanks. I appreciate the prompt feedback.  Ideally, I'm looking for something that I wouldn't have to manage on my own.
Comment: Ah I'm sorry, I have no data for you there.
Comment: `s/inedt/inetd/`

Reliable Time Protocol Server Online?


String with substrings in ascending order

Question: Suppose you have several substrings, e.g.,
AEBCA
DEA
BDAEBAE
EDEBAE
AED

How would you create a minimal-length string that contains the substrings in order?
The context, real or contrived (don't know), was this: Suppose you have several trucks, each of which must be loaded with items in a specific order. The substrings are the loads for each truck, and the letters are the items.
The trucks load by queuing in front of a line of bins, with each bin having only items of like type. The trucks can move forward only. In what order should the bins be arranged to have the minimum number?
My solution (http://www.excelforum.com/excel-programming-vba-macros/1086058-counting-letters-in-strings.html) was to catenate the substrings (24 characters in this example), and then strike every letter that left the substrings ascending in the reduced string. That gave me the 11-character string below, and the substrings are repeated below it:
AEBDAEBAECA
    AEB  CA
   D    E A
  BDAEBAE
 E D EBAE
AE D

Is this an instance of a more general problem that I could research by name? Can anyone suggest a less naive algorithm that might generate a better result?
Thank you.
Comment: Would this question be more appropriate to stackoverflow? The nature of questions and answers I see here suggests I'm way out of my depth.
Comment: I can't understand how you got your 11-character string.  Could you provide more detail? (I don't have access to the linked Excel workbooks.)
Comment: Your 11-character string is not the shortest possible.  For example, one 10-character string is `AEBDCAEBAE` and another is `AEBCDAEBAE`.
Answer: You're looking for the shortest common supersequence (scs).  According to Wikipedia,

For two input sequences, an scs can be formed from a longest common subsequence (lcs) easily.  For example, if $X[1..m]=abcbdab$ and $Y[1..n]=bdcaba$, the lcs is $Z[1..r]=bcba$.  By inserting the non-lcs symbols while preserving the symbol order, we get the scs: $U[1..t]=abdcabdab$.
It is quite clear that $r+t=m+n$ for two input sequences.  However, for three or more input sequences this does not hold. Note also, that the lcs and the scs problems are not dual problems.
For the more general problem of finding a string, $S$ which is a superstring of a set of strings $S_1,S_2,\ldots,S_l$, the problem is NP-Complete.  Also, good approximations can be found for the average case but not for the worst case.
Comment: Thank you for giving my problem a name. The workbook is accessible at https://app.box.com/s/o3uxgpagcqjy34t7h5kcz0hf7ymkrbo4


(Mahalanobis) distance to a multivariate distribution of which I have few sample points

Question: Say I have a set of sample points generated by a multivariate normal distribution D whose parameters I don't know.
I want to be able to measure the distance from an arbitrary point to the distribution D.
One way of doing this would be to get an estimate of the parameters of D, and use it to get the manalanobis distance to the center of D.
However, this gets wrong as the size of the sample gets small, or as the number of parameters go up.
An additional information I can use is a bunch of other similar sets of sample points belonging to similar distributions - so I could use that to get a "prior" over the parameters of D, and then update that according to the sample points I know it generated.
My intuition is that I could have a mahalanobis distance DC using the covariance matrix of my sample points, and a mahalanobis distance DP using the covariance matrix of all the data points I have (not only those belonging to my distribution), and use a weighted sum of them as my distance metric function of the size of my sample from D (if the size is small, I'd better use DP, if it's large, I can use DC). But I'm not sure how to formalize that exactly, and feel I'm missing some conceptual tools.
(this is similar to a standard classification problem, but here I'm not interested in class membership, only in distance to a given class - the other classes are only useful to provide priors)
So, how would you formally describe this problem? Is there a standard solution?
Answer: If you have very little data, it is not that the distance estimate is wrong, but that your estimate is uncertain.  A Bayesian approach would seek to determine the posterior distribution of the distance between the arbitrary point and the multi-variate distribution, rather than a single point estimate, and then marginalise over that posterior in reaching your conclusion.  This posterior distribution reflects the uncertainty in estimating the mean and covariance matrix of the multivariate Gaussian distribution.
I would be wary of using an informative prior.  In a Bayesian analysis, the conclusions are only as strong as the prior assumtions on which they are based; if the prior is questionable - so is the posterior.  Without more information about the problem it is not possible to determine if such a prior is reasonable.
Answer: If you have little data, then you have to do modelling, make assumptions. In a bayesian perspective it can take the form of a more focused prior.
In order to do so, we would need more information about your data:

What is the size of the sample and the dimension of the data ? This determines the degree of modelling you need before letting the data talk.
How was it obtained ...

In your example, you could make assumption on

The covariances: they can be diagonal (or at least almost diagonal in a well chosen basis)  or almost diagonal, or have a particluar block structure.
the covariance difference (that lead to the quadratic term in the classification rule) can be diagonal and have a sparse diagonal (i.e. few non zero terms). This can be justified if in most direction a linear separation of the data is observed.
The "centroid" differences between groups can be sparse also. A zero term means a direction wich does not help to discriminate. For example, if you are in a fourier basis, it is possible that while all frequencies have large variations from one observation to another, only two or three frequencies's variations are explained by the class variable...
...


Unselect or Deselect Worksheet

Question: I have a workbook with x worksheets.
And a button (commandbutton1) in Sheet1 which opens a userform with two checkboxes and a Preview Button to choose between 2 sheets to preview in PDF.
Here's my Preview button code.
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub CommandButton1_Click()
    Dim i As Integer
    Dim CheckBoxName As String

    For i = 1 To 2
        CheckBoxName = "CheckBox" & i
        If Me.Controls(CheckBoxName).Value = True Then
            Sheets(Me.Controls(CheckBoxName).Caption).Select (False)
        End If
    Next i

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="D:\Preview.pdf"

    Unload Me

    Dim File As String
    File = "D:\Preview.pdf"
    ShellExecute 0, "Open", File, "", "", vbNormalNoFocus
End Sub

Now, the problem is that Sheet1 is always included in the preview file since it's the sheet containing commandbutton1.
So is there a way to deselect sheet1 before exporting sheets 6 & 21 ??
I also tried another way but it always stuck at this line (Sheets(Array(SheetNames)).Select)
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub CommandButton1_Click()
    Dim i As Integer
    Dim CheckBoxName As String
    Dim SheetNames As String

    For i = 1 To 2
        CheckBoxName = "CheckBox" & i
        If Me.Controls(CheckBoxName).Value = True Then
            SheetNames = SheetNames & Me.Controls(CheckBoxName).Caption & ","
        End If
    Next i

    SheetNames = Mid(SheetNames, 1, Len(SheetNames) - 1)

    Sheets(Array(SheetNames)).Select

    ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="D:\Preview.pdf"

    Unload Me

    Dim File As String
    File = "D:\Preview.pdf"

    ShellExecute 0, "Open", File, "", "", vbNormalNoFocus
End Sub

Any help with any of these please ??
Thank u in advance.
Answer: The trick is how you pass the parameter False in
Sheets(Me.Controls(CheckBoxName).Caption).Select (False)

Is this what you are trying?
Private Sub CommandButton1_Click()
    Dim i As Integer, j As Integer

    For i = 1 To 6
        CheckBoxName = "CheckBox" & i
        If Me.Controls(CheckBoxName).Value = True Then
            j = j + 1
            Worksheets(Me.Controls(CheckBoxName).Caption).Select (j = 1)
        End If
    Next
    If j = 0 Then Exit Sub

    '
    '~~> Rest of your code
    '
End Sub

Before:

After:

OR

Unselect or Deselect Worksheet


How can a shy singer get feedback?

Question: I like singing, and while I've got more comfortable with the sound of my voice over time, I still don't really like it, and I'm massively self-conscious about anybody hearing me sing.  I'm so jealous of people who don't have great voices but have no qualms bursting into song mid-conversation.
I'd love to get objective feedback about my voice and abilities - I think it would make me more confident - but asking friends to listen to me sing, or even to recordings, is so embarrassing, so it's a bit of a catch-22.
Where or how might I find a way to show my singing to people - preferably strangers - who will give kind, constructive, but truthful feedback (it's no use my mum telling me I'm great, if I'm not).
Comment: Your question is not a music one at all, it's a personal confidence one. Ask your friends, ask anyone - one you realise there will always be positive and negative comments and learn to cope with them you'll be better off.
Comment: close or not - try karaoke. It's low-expectancy. You won't get feedback, but you're unlikely to get any criticism whatsoever except people won't shut up to listen.
Comment: There is a subreddit for this, but I can't recall the name.
Comment: It is a problem that many musicians face, but I suspect that the web is the worst place to start. Way worse than family friends or your Mom. A live audience is the best place to start learning to perform. Don't worry about what you are born with, you cant help that, worry about the performance.
Comment: Send me a recording to email@example.com.
Answer: Have you tried a vocal teacher/coach?  Find a good teacher who will be honest with you.  Ask them to give you the honest truth, upfront.  Also, they should be able to help you improve your singing as well, which will make your situation all the better.
Also, as you sing more and more in public, you should not only become more comfortable, but also a better singer.  Continue to record yourself and listen, to find how you can continue to improve.
Answer: There are several ways to get more comfortable with the sound of your voice and get feedback.  First, try Karaoke.  Not sure where you live but I am sure there are venues which have Karaoke nights.
It was karaoke that made me realize I could sing better than average and started my journey into performing.  It was the genuine feedback I got the first time I sang karaoke that made me aware that other folks thought I had a good voice and boosted my confidence.
So then I bought a karaoke machine and some CDG's and started singing at home which made me even better when I went to karaoke parties or karaoke nights at various venues.
This realization led me to learning guitar and later writing songs - but it all started with karaoke.
Another thing you can do if you are really brave, is post some video's of you singing on YouTube.  Unless you disable comments - you will probably get feedback. Very honest feedback. Fortunately for me, I usually get good feedback on my voice.  But once I tried to sing like an opera singer and I discovered how brutally honest the feedback on YouTube can be.  I deleted that video!  But if you get bad feedback on a particular performance, don't be discouraged.  Try to learn from that experience and perhaps try a different song, or practice singing it differently or in a more comfortable key.
I don't know if they have Open Mic nights where you live, but they have those all over the U.S. - here is a link to a nationwide list (for those in the U.S.A.) [http://www.openmic.us/EventListing][1]
I regularly perform at open mics in my area and usually get feedback from the audience when I get off stage and sit back down.  Folks will come up to me and say "good job man" or something like that.  They don't come up to every performer - so I can tell that their compliments are genuine.
I have also learned that the more I sing in a "performance" situation, whether karaoke or open mic, the more comfortable I get singing and the better I get.  I get more compliments now than I did when I first discovered that I could sing.
In your question you alluded to those who don't have great voices - but they don't let that stop them from singing.  Don't worry so much about being a great singer.  If you can sing and come close to staying in key - you are ahead of most.  The main thing is to learn to enjoy singing without worrying so much about being on par with some of the greatest  singers in the world.
Speaking of folks who don't have great voices - take Bob Dylan for example!  A great song writer (true genius). He could have hired a professional studio vocalist to sing on his recordings, but he chose to sing them himself.  Not to take anything away from Dylan as an artist, songwriter, and performer, but for me personally, his voice is a little harsh on most of the recordings I have heard (Lay, Lady Lay being a notable exception).
Most likely you already sound better than the great Bob Dylan.  So sing early, sing late, sing often, and have fun singing whenever the opportunity presents itself.
Comment: I have such a similar experience to yours that after reading the first couple paragraphs I was worried that I had written this answer and then forgotten I'd written it! +1 for karaoke. A great way to hone singing and performance skills, gain comfort with being on stage, learn one's range, etc.
Answer: Have you thought about joining a choir? You may feel more comfortable singing in a group, will get support and feedback from the other choir singers and gain stage experience. There may even be an opportunity to sing a solo part. In my choir that is the case.
Answer: You will have great difficulty in getting a truthful assessment.  Some will feel encouragement is the priority, some will prefer 'the painful truth' and will veer too far in the other direction - I'm afraid 'Pretty good overall' can easily get buried in a list of things that need improvement!
Get out there and perform.  When you become a karaoke or open-mic 'regular' it will soon become apparent what the audience think.  And, of course, the real proof comes when you're offered your first paid job!
Answer: Singing Carrots are offering feedback from professional vocal coaches on your singing recording
It's not free, but you will get a comprehensive analysis and will not have to be worried about exposing yourself. You can submit an audio recording anonymously.
Comment: I hate to be *that guy*, but I feel it's mandatory on Stack Exchange. Do you have any affiliation with them or is it a website you found accidentaly?

How can a shy singer get feedback?