C - fprintf() & printf() deleting array element memory

Question: I fairly new to C Programming, but fprintf() & printf() is behaving strangely and I'm so confused on why--I need some help understanding and diagnosing this issue.

fprintf() Deleting Element of Array
First off, I'm passing in a populated malloc allocated four element char** array into a simple function that will write to a file, everything in the array appears normal and all four elements contain the correct data. The function call in main() looks like this. My array in question is header.
Note: I had to cast this normal (char** array) as a constant in this function parameter, due to the function header parameter. Our professor gave us the header file and we cannot change anything in them.
pgmWrite((const char**) header, (const int**) matrix,
    rowPixels, colPixels, outFile);

Next, stopping debugger just before it executes the fprintf() & printf() functions, screenshot showing the array is still populated with my 4 elements.
pgmWrite() - Showing array is still fine
Observe the 4th element of the array after execution of fprintf().
After fprintf() executes, element 3 memory is wiped out.
When run, printf() executes the printing of the array exactly what is shown in the debugger, ending at the 3rd element. Often printing nothing in that spot or in rare cases garbage characters. The behavior of printf() is exactly the same as how fprintf() is working as well.

I'm at a loss here guys, please help me understand what I'm doing wrong. I can only provide these two screenshots, based on me being a new member. I'll try to provide as much information as possible. Thank you. Here is a simplified version of my program. Keep in mind, the professor gave us the function declarations and told us we cannot change them. So, I have to work with what I have here. Also, since this is fileIO, you need to find a *.pgm file to test this.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define rowsInHeader 4
#define maxSizeHeadRow 200

int ** pgmRead( char **header, int *numRows, int *numCols, FILE *in  ){
// INITIALIZING
char *headArr[rowsInHeader][maxSizeHeadRow];
char buffer[100];
int r = 0;
fpos_t pos;
// CREATE: Header
while (r < 4){
// IF: Row in pgm file header lists the dimensions of matrix
if (r == 2){
// CURSOR: Saving pointer location in file (see notes in header for method reference)
fgetpos(in, &pos);
// ASSIGN: Dereference column and row pointers from file
fscanf(in, "%d %d", numCols, numRows);
// CURSOR: Moving back to saved pointer location (see notes in header for method reference)
fsetpos(in, &pos);
}
// ASSIGN: Copying header row into array
fgets(buffer, maxSizeHeadRow, in);
strcpy((char*)headArr[r], buffer);
// POINTER: Reference pointer to headArr[]
header[r] = (char*)headArr[r];
// TRAVERSE: To next row in file
r++;
}
// NOTE: Placeholder for return type
return 0;
}

int pgmWrite( const char **header, const int **pixels, int numRows, int numCols, FILE *out ){
// INITIALIZING
int i = 0;
// WRITE: Header
for (i = 0; i < rowsInHeader; i++){
    fprintf(out, "%s", header[i]);
    printf("%s", header[i]);
}
return 0;
}

int main(int argc, char *argv[]){
char **header = (char**)malloc(rowsInHeader * sizeof(char));
FILE *inFile = fopen("smallFile.pgm", "r");
FILE *outFile = fopen("TestPicture.ascii.pgm", "w");;
int rowPixels = 0;
int colPixels = 0;
int **matrix = NULL;
// READ & WRITE
matrix = pgmRead(header, &rowPixels, &colPixels, inFile);
pgmWrite((const char**)header, (const int**)matrix, rowPixels, colPixels, outFile);
// FINALIZING
fclose(inFile);
free(header);
return 0;
}

Comment: needing to cast between pointer types is usually a sign of (design/implementation) errors.
Comment: Welcome to Stack Overflow. Please read the [About] page soon.  When you problems with getting code to work, we must see the code.  We do **not** want to see images of your code — we want code that we can copy and edit and study.  Please read up on how to create an MCVE ([How to create a Minimal, Complete, and Verifiable Example?](http://stackoverflow.com/help/mcve)) — and provide us with an MCVE that demonstrates your problem.  It is also important that we know what the input data is and what the result is (and what the result should be).  The problem is not in the code you show in the images.
Comment: Never say that, if it was so don't you think c programmers would know?
Comment: I'm sorry what do you mean? If I said something wrong, I'll be glad to edit my mistake. Can you be more specific on what you mean by, "never say that"?
Comment: Okay, I'll try to segment my code so that others can copy this if need be. Give me a few minutes to fix this up.
Comment: There's no way to debug this without seeing the code. We can't even tell what `i` or `rowsInHeader` is, so we can't tell what the `printf`/`fprintf` statements are doing.
Comment: Show how you create and pass the array in question. Needing the cast is very suspicious.
Comment: I'm sorry for the vagueness guys, I'm brand new to posting code on websites. I just provided a simplified version of my code. You need to download a *.pgm file in order to test this out. Let me know what else I can do.
Comment: What, in your view, is a `.pgm` file and where can such files be downloaded from?  The first line in your main program code: `char **header = (char**)malloc(rowsInHeader * sizeof(char));` is only allocating enough space for single characters, but you need it to allocate that many character pointers.  That's a starting problem – it might be sufficient to account for all the trouble, though there's usually more than one problem when there's a problem like that.  (Bugs cluster together…)
Comment: Another problem is in `pgmRead()`: you define `char *headArr[rowsInHeader][maxSizeHeadRow];` but you probably wanted a 2D array of characters, not of pointers. Fixing this means the casts are not needed. And then you have `header[r] = (char *)headArr[r];` so you are assigning pointers to a function's local variable into the data passed back to `main()`.  That gives you endless trouble because once the function returns, the `headArr` is reused for other purposes; that's why things change when you call `printf()`.  You're abusing data space that doesn't belong to your function any more.
Comment: @JonathanLeffler - Thank you, I'll adjust those suggestions. In the meantime, here is a link to *.pgm file explanations. I guess pgm stands for portable grey map. The files I'm working with are considered "P2" mode, meaning they're in ASCII format. This allows you to open in a text editor the matrix of pixels and you can adjust the intensity of the grey value. http://people.sc.fsu.edu/~jburkardt/data/pgma/pgma.html
Answer: You are not allocating your array correctly. This line:
char **header = (char**)malloc(rowsInHeader * sizeof(char));

makes header point to an uninitialized region of memory , size 4 bytes.
Then inside your PGM function you write:
header[r] = (char*)headArr[r];

The code header[r] means to access the r'th pointer stored in the space pointed to by headArr. But since that space is only 4 bytes big, you're actually writing off into the wild blue yonder.
Also, (char *)headArr[r] is a mistake. If you did not use the cast, your compiler would have warned you about this mistake.  You should avoid using casts in your code, especially using them to make warnings go away. You're saying to the compiler "Ssh, I know what I'm doing" when in fact you don't know what you are doing.
The entire approach with headArr is flawed from the start: even if you had actually written the right code to implement what you were trying, you'd be returning pointers into space which is deallocated when the function returns.
Basically the whole pgmRead function is a complete mess and it'd be easier to start from scratch. But this time, think carefully about when and where you are allocating memory, and what the types are of your expressions, and don't use casts. Let the pgmRead function do all the allocation.
(Unfortunately, based on your description it looks like you will have to use your casts to call the pgmWrite function since that has a mistake in its signature. const int ** should be const int * const *, and similarly for const char **. I'd recommend to actually change pgmWrite's signature accordingly, get your program working, and then once everything is good, then go back to the broken version that you are forced to use.)
Reading C FAQ - arrays and pointers might be useful too.
Comment: Thank you, for this informative info @M.M. While I try to implement your edits into my program, could you if you have time, post a reply to what _you_ would do in pgmRead?

I get what you are saying about the memory stack wiping clean after the return. I just can't for the life of me figure out how to assign an outside double pointer to a 2D array. This is our first assignment in class and also my first program in C. Still getting used to the whole pointer thing. (By the way; assignment was due last night, but I'm still determined to understand this and get this program to work right.)
Comment: @MatthewDonaldson I think `header` is supposed to be a 1-D array. The spec doesn't mention anything about multiple header rows, and the function signature you've been provided is correct for returning a 1-D array by reference. (The return value of `pgmRead` is already in use for returning the data, so `header` is "returned" by using a pointer parameter).  So inside the `pgmRead` function you could start with `char *ptr = malloc(numberofbytesfortheheader);`, then read the header into that space, and then "return" it by doing `*header = ptr;` at the end.
Comment: An issue is what exactly you are going to put in that malloc'd space. It's not clear from what you posted whether you are just supposed to return the exact bytes that are in the file, or whether you're supposed to read the header and convert it into a more usable format (e.g. extract the width, height, and maxgrey as integers).  If the former (which would be annoying), then you'd have to read the file into a temporary buffer until you actually know where the end of the header is, since the header is not a fixed number of bytes.


How do I install php 5.2.10 on Ubuntu 10.04?

Question:
Possible Duplicate:
PHP 5.2x on Ubuntu 10.04 Lucid Lynx

I'm setting up a new Ubuntu server, but it's going to be running a somewhat old PHP application that seems to have conflicts with PHP 5.3.
How do I configure Apt to give me the option to install a previous 5.2.x version of PHP?
Comment: Answered here: http://serverfault.com/questions/138209/php-5-2x-on-ubuntu-10-04-lucid-lynx
Answer: Instruction here
http://mrkandy.wordpress.com/2010/04/16/install-php-5-2-x-in-ubuntu-10-04-lucid/
Comment: This is a really hard solution to maintain.

This page is easier to implement.
https://edge.launchpad.net/~txwikinger/+archive/php5.2


How can I sort these comments so that the most recent appears at the top?

Question: I currently have a MySQL database which contains any comment left on my webpage. I've then written the following script to make them appear on the page:
<?

$con = mysql_connect("localhost","theshitp_user","password");

if (!$con)
{
  die('Could not connect: ' . mysql_error());
}

mysql_select_db("theshitp_posts", $con);

$query = "SELECT * FROM `userposts` LIMIT 0 , 30";

$comments = mysql_query($query);

while($row = mysql_fetch_array($comments, MYSQL_ASSOC))
{

  $comment = $row['comment'];
  $timestamp = $row['timestamp'];

  $comment = htmlspecialchars($row['comment'],ENT_QUOTES);

  echo "

$comment

      $timestamp

  ";
}

mysql_close($con);

?>

This posts the most recent comments at the bottom of the list. How can I modify this script to make it sort the comments in order of newest first?
Comment: Use order by on any date time field.
Comment: What would the code to do that look like? I'm very new to SQL.
Answer: Use order by on any date time field, in your case i think its timestamp, like:
$query = "SELECT * FROM `userposts` ORDER BY timestamp DESC LIMIT 30";

This will show 30 records in descending order with respect to timestamp.
Answer: You can try this query, you will get recent entries:

$query = "SELECT * FROM userposts ORDER BY timestamp DESC LIMIT 0 ,
  30";


Angularjs minification being forced to add strings for services

Question: As is documented in numerous places in angular you must put service names in an array in order to survive minification.
example:
Different kind of Angular Directives patterns
The problem I have with this is that it's really really ugly and you have to essentially put the service names twice. I also don't want to turn off mangling in uglifyjs.
Is there a way to automatically create the brackets in the build process or use some other method to take care of this problem automatically?
Comment: you should be doing it manually
Comment: @Ajaybeniwal what about ngmin as per Vitaliy's answer?
Answer: You can try ngmin - https://github.com/btford/ngmin.
But I prefer to add these annotations manually by myself.
Comment: @Harry if you follow the convention all should work fine.
But if you annotate dependencies manually your code 100% will work after minification.


Netsuite Advanced PDF

Question: I am trying to create a print out for Bill of Materials using Advanced PDF in Netsuite. I am using source code. How can I changed the orientation of my print out from portrait to landscape? I know that when using WYSWYG, I can just simply choose the orientation in the Template Setup but that option is not available when using Source Code.
Thanks!
Answer: You can set the page size with the size attribute on the  tag.

or

Link to documentation

Netsuite Advanced PDF


Smart string search python (maybe) using whoosh

Question: I need to make a search method that is a little smarter than the find() in python. Something that could find similar words more like a search engine. I heard of Whoosh, installed it, but I couldn't even understand where to start from. Could someone either teach me some really basic whoosh string search methods or give me another option?
Comment: What kind of searches do you intend to do with it?
Comment: http://packages.python.org/Whoosh/quickstart.html
Comment: I have this file: http://pastebin.com/F28RXDvu and I want to let the user search for parts of the strings, or the same one with different case...
Comment: Not sure if this is a dupe or not, but you may find the following useful: [Good Python modules for fuzzy string comparison?](http://stackoverflow.com/questions/682367/good-python-modules-for-fuzzy-string-comparison)
Answer: Are you looking for fuzzy string matching?
If so you might be interested in checking out FuzzyWuzzy.
Comment: It worked well for many cases, but I'm still searching for some more automatic smart search.


Likelihood for dependent data above a threshold

Question: Let $(Y_t)$ a real-valued stationary Markov chain and $u$ some positive threshold. We assume that for $y>u$,
$$Y_{t+1}|\{Y_t=y\}\sim\mathcal{N}(\alpha y+\mu y^\beta,\sigma^2 y^{2\beta})$$
I want to find the maximum likelihood estimator of $(\alpha,\beta,\mu,\sigma)$, but I'm not sure how I should write the likelihood function, since my data are not independent, but they're not a Markov chain either (there are only some clusters of observations above the threshold).
What I did in R:
<code>Yt=Y[1:(length(X)-1)]
Yt1=Y[2:length(X)]

ind=which(Yt>u)

LL=function(alpha,beta,mu,sig){
  R=dnorm(Yt1[ind],mean=alpha*Yt[ind]+mu*Yt[ind]^beta,sd = sig^2*Yt[ind]^(2*beta))
  -sum(log(R))
}

estim_ml=mle(minuslogl = LL,start = list(alpha=0.7,beta=0.5,mu=0,sig=1))
</code>
It works and it gives me estimates that are not absurd, but I think this is not correct as it's just the product of densities, just like independent data. Is there a way to find the likelihood function? If not, is what I did a good approximation?
Answer: If I understand correctly, you have a collection of $Y_t:t=1,2,...T$. Moreover, your model for $Y_t$ specifies that it is conditional on $Y_{t-1}$. What you can do is a recursive scheme that is quite common in dynamic time series analysis. You essentially condition observation for observation as follows:
$f(Y_t|\alpha, \beta, \mu, \sigma; \{Y_k\}_{k=1}^{t-1})
= f(Y_t|\alpha, \beta, \mu, \sigma; Y_{t-1})\cdot f(Y_{t-1}|\alpha, \beta, \mu, \sigma; \{Y_k\}_{k=1}^{t-2}) = \dots = \Pi_{t=2}^T f(Y_t|\alpha, \beta, \mu, \sigma; Y_{t-1}):= F(Y)$
The recursion happens at $\dots$, where we extend the conditioning principle to the next $T-1$ observations. Notice that the final product runs from $2$ to $T$, because we have to take $Y_1$ as given (aka, as 'initial condition') for this approach to work. It will appear in the conditional pdf of $Y_2$, but its pdf will not be part of the expression to be maximized. Once F(Y) is obtained, you can apply standard MLE techniques to it.
Comment: Thank you for your answer. I understand what you mean, but how do you account for the fact that I know the distribution of $Y_{t}$ given $Y_{t-1}$ only when $Y_{t-1}>u$? If $\{t_1,\dots,t_n\}$ denotes the set of times where $Y_t>u$, the observations that I need to consider are $y_{t_1},y_{t_1+1},y_{t_2},y_{t_2+1},\dots,y_{t_n},y_{t_n+1}$.
Comment: Are you interested in the distribution of $Y_t|Y_{t-1}$ or of $Y_t|Y_{t-1}, Y_{t-1}>u$? If it's the latter you are interested in, you can still apply the technique for every block of length $k$ for which the first $k-1$ observations are larger than $u$ (call the result $F_j(Y)$) and maximize the product $\Pi_{j=1}^{K}F_j(Y)$ over the parameters of interest. If you are interested in $Y_t|Y_{t-1}$, I will have to look into **censored regression** techniques again. It's been a while ;)


how to determine objects eligible for garbage collection in the given program?

Question: Given:
 public class Trees {
    Trees t;

    public static void main(String[] args) {
        Trees t = new Trees();
        Trees t2 = t.go(t);
        t2 = null;
        // more code here : LINE 11
    }

    Trees go(Trees t) {
        Trees t1 = new Trees();
        Trees t2 = new Trees();
        t1.t = t2;
        t2.t = t1;
        t.t = t2;
        return t1;
    }
}

When line 11 is reached, how many objects are eligible for garbage collection?
Comment: I suggest you to buy and read the SCJP 6 book. It have 50 pages just from the garbage collection process and it's very detailed and helpful.
Comment: What is `t2.t`? Please provide the definition for class `Tree`
Comment: @Desolator. t2.t refers to the reference instance variable `t` in the instance `t2`.
Answer: You have asked how to determine the number of objects eligible for gc. The easiest way to work these questions out is to draw a diagram showing references (t, t1, t2, in your example) and the actual objects themselves. Once an object is not connected to any reference, there is no way for the Java code to access it, so it is then eligible for collection.
This link shows an example and how to draw a diagram
http://radio.javaranch.com/corey/2004/03/25/1080237422000.html
Answer: You create three objects:
Object "a": Trees t = new Trees();
Object "b": Trees t1 = new Trees();
Object "c": Trees t2 = new Trees();

At line 11, none of them are eligible for garbage collection, because the variable t (declared in main) still has a reference to object "a", and both "b" and "c" can be reached from "a".
Answer: Based on your code snippet, when the control reaches your "line 11", no objects can be GCed.
Reason:

Trees t; is a field, and thus cannot be GCed now.
after calling go, t2 will equal null. However, in method go, each t1 and t2 point to the other, plus that the field t is pointing to one of them. So that no object can be GCed, because of reference chain:

t -> t2
t2 -> t1


Isolating channels with Web Audio API

Question: I'm attempting to play audio only in my surround channels, but it seems like anytime I connect to a specific channel it plays out the other ones as well
My set up and audioContext.destination are configured for 4 channels
var merger = context.createChannelMerger(4);
audio.connect(merger, 0, 2);
audio.connect(merger, 0, 3);
merger.connect(context.destination);

my audio is stereo (but I only care about one channel) so I also tried splitting it first
var merger = context.createChannelMerger(4),
    splitter = context.createChannelSplitter(2);
audio.connect(splitter);
splitter.connect(merger, 0, 2);
splitter.connect(merger, 0, 3);
merger.connect(context.destination);

oddly enough, If I only connect it to the left/right channel it plays as expected.
I'm running chrome 45 on windows 10
Answer: I think you're connecting the splitter to the merger incorrectly.  You probably want

splitter.connect(merger, 0, 2);
splitter.connect(merger, 1, 3);

Then the stereo source will be split and sent to channels 2 and 3 of the output.
Comment: Sorry, I forgot to me mention my audio file is actually mono but it's read to be stereo so I don't actually care it's only one channel, if that's what your code does
Answer: Can you share your set up code for AudioContext? It needs a bit of specific setup for the individual routing. See the example below:
var context = new AudioContext();
var maxChannelCount = context.destination.maxChannelCount;

context.destination.channelCount = maxChannelCount;
var merger = context.createChannelMerger(maxChannelCount);
merger.connect(context.destination);

// This will play the sound on 3rd and 4th speakers for 1 second.
var osc = context.createOscillator();
osc.connect(merger, 0, 2);
osc.connect(merger, 0, 3);
osc.start();
osc.stop(1.0);

Isolating channels with Web Audio API


Question about R squared ratio in model comparisons

Question: I am currently working with a few different regression models (regression trees, GBT, linear, etc) in the platform KNIME and now that I have computed the following statistical measures: $R^2$, mean squared and mean absolute error (MSE and MAE respectively), root MSE, and mean signed difference.
This site explains things, however there is a part where the author states:

"In conclusion, R² is the ratio between how good our model is vs how
  good is the naive mean model."

What is meant by the "naive mean model" and how significant is it in model comparison?
Comment: @mkt I will do just that then. I really appreciate you taking the time to help me get acclimated on here :) One last thing: would it be beneficial at all if I link the other questions to this question? (Thinking in the event that one person answers this query, they might also be knowledgable on the rest)
Comment: @mkt Awesome. Well I know I've said it already, but third time's the charm, so thank you once again!
Comment: Welcome to the site, Angela. It's best to post questions separately here, because it allows more specific titles (helps makes the site more searchable) and also because people who may know the answer to one of your questions but not others are less likely to post answers.
Comment: @mkt Thanks for the advice! So should I edit this question and repost an extra two in separate questions? (3 posts total)
Comment: Yes, I think that would be a good idea
Comment: You're welcome! Linking to the other questions is not a bad idea, but I don't know that it would make a strong difference either way.
Answer: The 'naïve mean model' refers to a model where the prediction is just the average of the dependent/target variable for the whole dataset. Think of a regression where the slope is zero and the intercept is just the mean of all the data points - a horizontal line.
This very simple reference model is what you compare a more complex model against. The $R^2$ then compares the new model against this simple/naïve reference model.
This is why it's possible for a model to have a negative $R^2$, if it makes predictions that are worse than just predicting the mean (see the linked answer for a good example).
Answer: Briefly, the naive mean model is guessing the average $y$ value no matter what the values of your predictor variables are.
Longer answer:
The goal of regression is to predict a conditional mean. In other words, for given values of the predictor variables, you might sometimes get 2.1, sometimes 1.9, sometimes 2.2, sometimes 1.8, etc, but, on average, you expect to get 2. Then 2 is your predicted value.
The most naive way to do this prediction is to ignore the predictor variables and always guess the average value of your response variables. This results in high variance, and we use regression to tighten up our estimates. For instance, we expect adults to be taller than toddlers, so if we were to guess the height of an adult, we would want to account for the fact that they ought to be taller. By including this toddler/adult variable, we make fair comparisons to other adults instead of letting many 2-foot-tall toddlers stretch out the distribution and cause less precision in our estimate.


Digit sum formula $n - 9 \sum_{i\ge1} \left \lfloor \frac{n}{10^i} \right \rfloor$

Question: Let $S(n)$ be the sum of digits of n
Prove that $S(n) = n  -  9 \sum_{i\ge 1} \left \lfloor \frac{n}{10^i} \right \rfloor$ for all natural numbers n
I started with induction which works easily if $10$ doesn't divide $n+1$, because then $S(n+1)$ is just $S(n) + 1$.
However, if $10|(n+1)$ then $S(n+1)$ could be anything. Moreover, I have no idea how to use that fact.
If I write e.g. $n = 10k + 9$ then $\sum_{i\ge1} \left \lfloor \frac{10k+10}{10^i} \right \rfloor$ = $\sum_{i\ge1} \left \lfloor \frac{k+1}{10^{i-1}} \right \rfloor$  and I don't know how to proceed
Comment: One fun thing about a result like this is that it seems potentially completely useless, as the formula seems much more tedious than simply adding the digits. Perhaps it has some meaning when one is programming a method to take care of this?
Comment: Maybe it would help to use strong induction, then for the $10\mid (n+1)$ case, use the digit sum formula for $S\left(\frac{n+1}{10}\right)$.
Answer: Using strong induction, assume for all $n = 1,2, \ldots, k$, the sum of digits formula holds:
$$S(n) = n - 9\sum_{i\ge 1} \left\lfloor\frac n{10^i}\right\rfloor$$
The induction step would be to prove that $S(k+1)$ follows the same formula.
For the cases that $10\mid (k+1)$, let $k+1 = 10q$, (and so $1\le q \le k$)
$$\begin{align*}
S(k+1) &= S\left(\frac{k+1}{10}\right)\\
&= S(q)\\
&= q - 9\sum_{i\ge 1} \left\lfloor\frac q{10^i}\right\rfloor\\
&= \frac{k+1}{10} - 9 \sum_{i\ge 1} \left\lfloor\frac {(k+1)/10}{10^i}\right\rfloor\\
&= 10\cdot\frac{k+1}{10}-9\cdot\frac{k+1}{10} - 9 \sum_{j\ge 2} \left\lfloor\frac {k+1}{10^j}\right\rfloor && (j=i+1)\\
&= (k+1) - 9 \sum_{j\ge 1} \left\lfloor\frac {k+1}{10^j}\right\rfloor\\
&= RHS
\end{align*}$$
For the other cases that $10\not\mid(k+1)$, OP already mentioned in the question that $S(k+1) = S(k)+1$.

It is possible to merge the two cases with a slight change, by also proving the base case $n=0$ and including it into the assumption.
For the $n=k+1$ case, let $k+1 = 10q + r$, where $q = \left\lfloor \dfrac{k+1}{10}\right\rfloor$ and $r=(k+1)\bmod 10$.
$$\begin{align*}
S(k+1) &= S(q) + r\\
&= q - 9\sum_{i\ge 1} \left\lfloor\frac q{10^i}\right\rfloor + r\\
&= q - 9\sum_{i\ge 1} \left\lfloor\frac {10q/10}{10^i}\right\rfloor + r\\
&= 10q - 9\cdot \frac{10q}{10} - 9\sum_{j\ge 2} \left\lfloor\frac {10q}{10^j}\right\rfloor + r&&(j=i+1)\\
&= 10q - 9\sum_{j\ge 1} \left\lfloor\frac {10q}{10^j}\right\rfloor + r\\
&= 10q+r - 9\sum_{j\ge 1} \left\lfloor\frac {10q}{10^j} + \frac{r}{10^j}\right\rfloor\\
&= (k+1) - 9\sum_{j\ge 1} \left\lfloor\frac {k+1}{10^j}\right\rfloor\\
&= RHS
\end{align*}$$
Answer: Let $n$ be given and let $10^k$ be the largest power of $10$ that divides $n+1$. Then we have:
$$
n=a+10^k d-1
$$
where $a$ is divisible by $10^{k+1}$ and $d\in\{1, 2,\cdots,9\}$. Now clearly:
$$
S(n)=S(a)+S(10^k d-1)
$$
similarly we have:
$$
S(n+1)=S(a)+S(10^k d)
$$
This shows that it suffices to focus on the latter part, so all you need is success for the induction step with $n+1=10^k d$. Thus we check:
$$
\begin{align}
10^k d-9\sum_{i\geq 1}\left\lfloor\frac{10^k d}{10^i}\right\rfloor
&=d\left(10^k-9(1+10+...+10^{k-1})\right)\\
&=d\left(10^k-(10^k - 1)\right)\\
&=d\\
&=S(10^k d)
\end{align}
$$
which is all we need to take care of all the steps in your original induction that were not covered.
Answer: Let $$n=\sum_{k=0}^m \lambda_k\cdot 10^k$$
Then $$S(n)=\sum_{k=0}^m \lambda_k$$
and $$\bigg\lfloor \frac{n}{10^i} \bigg\rfloor=\sum_{k=0}^{m-i}\lambda_{k+i}\cdot 10^k$$
So we are proving that: $$\sum_{k=0}^m \lambda_k\cdot 10^k-\sum_{k=0}^m \lambda_k=9\sum_{i=1}^m \bigg(\sum_{k=0}^{m-i}\lambda_{k+i}\cdot 10^k\bigg)$$
It seems quite nice from here.


Get roles from Keycloak userinfo endpoint

Question: I have keycloack realm with authorization code flow with public access type.
How can I get user roles from keycloak userinfo endpoint without client_secret?
I'm trying to use mappers but can't get this information.
Userinfo endpoint return only this:
{
   "sub":"ace69576-614e-41fe-9584-aac69c34dec1",
   "email_verified":true,
   "name":"John Doe",
   "Token Claim Name":[
      "Realm Role prefixuma_authorization",
      "Realm Role prefixoffline_access"
   ],
   "preferred_username":"user",
   "given_name":"John",
   "family_name":"Doe",
   "email":"email@example.com"
}

Answer:
How can I get user roles from keycloak userinfo endpoint without
client_secret?

Whether or not you can get the user roles from the userinfo endpoint is not related to the type of client (i.e., confidential or public).
To make the user roles (i.e., realm or/and client -related roles) also available from the userinfo endpoint do the following:

Go to the according realm;
Go to the according client;
Go to Mappers;
Click on Create;
As the Mapper Type select User Realm Role;
Set to ON the option Add to userinfo, and click Save;

If the user has client roles assigned to it; Repite the aforementioned steps but this time select User Client Role as the Mapper Type.
Comment: Good answer, thanks! But my old version of keycloak contains a bug described here https://stackoverflow.com/a/64748129/12980819. Maybe this will help someone.


CHMOD in web hosting

Question: my Hosting is use Apache and PHP
i understand what it's mean 777 in CHMOD or drwxrwxrwx or Owner, Group, Public => read, write, execute.

i understand with the RW CHMOD command but what about the X, it for authorization execute the Shell command or my PHP script or both?
When we upload the image through browser, let's says in CMS (wordress, joomla, etc) whe have ability to upload image. when we do it, what is the CHMOD we need? since i assume if access via web browser then we are as public

sorry for my bad english, i don't know how to explain it's more simple.
Comment: @GusDe CooL - normally we dun need the executable write especially in web environment, in CLI `php -q FILE` will do, same as shell, of course for hardy purpose, we often change it to executable (my 2cents, dun scold me)
Comment: it's preferred to refer to permissions as User, Group, Other (ugo) as it's how you can set permissions with `chmod` (eg. `chmod u+x file`). Since you talk about "web hosting", it's possible that the hosting service already set up the right permission for you so you don't have to worry about it.
Answer: Your PHP scripts will be executed whether the "X" permission bits are set on the PHP
files or not.  That's because the script itself is not a standalone executable -- it's
just an input file to the PHP interpreter, so "R" permission is sufficient.
For a shell script that starts with a "shebang" line (for example, #!/bin/bash), the "X" permissions will be checked before the OS allows the script to be executed.
For directories, the "X" permission bits are interpreted differently -- rather than
granting "execute" permission, they grant "search" permission -- is that user allowed
to list the contents of that directory?
Comment: The "X" is not checked before the script is executed. The OS does not recognize it as a command if it does not have 'X'. You can always run the script by explicitly calling the interpreter (eg `bash my_script`). The 'X' on directory does not grant 'search', it grants access. To list a directory, you need 'R' permission.
Answer: usually what I have is
chmod 770 the_folder

and
chown apache_user:apache_group the_folder

and make sure that ftp_user added to apache_group so that you could modify the files usign this command
usermod -G apache_group ftp_user

hope helps! :)

CHMOD in web hosting


Como criar um sistema de notificação semelhante ao facebook

Question: Sou iniciante e preciso desenvolver um sistema para controle de tarefas em php. Gostaria que este sistema tivesse um ícone no canto superior direito, que mostraria um número de tarefas vencidas/vencem naquele dia para o usuário, similar às notificações do facebook.
Existe algo pronto que possa utilizar?
Comment: O que você tentou?
Comment: Neste vídeo tem uma solução em php + javascript que pode ser útil https://www.youtube.com/watch?v=CFFJeHw6VBU. Esta pergunta aqui no SOpt talvez também seja útil http://pt.stackoverflow.com/questions/9475/notifica%C3%A7%C3%B5es-em-tempo-real-semelhante-ao-stack-overflow.
Answer: Testei o código e está funcionando. Qualquer dúvida é só perguntar.
index.php
!DOCTYPE html>

    <title></title>

INFORMACAO EH EXIBIDA AQUI:

get.php
<?php
    $con = mysqli_connect('localhost', 'root', '', 'tarefas');

    $id = $_GET['id'];

    $sql = "select * from tarefas where id = $id";

    $query = mysqli_query($con, $sql);
    $resultado = mysqli_fetch_assoc($query);

        $retorna = $resultado['vencidas'];
        echo $retorna;
?>

Comment: `[...]tarefas vencidas/vencem naquele dia`... Fazer requisições para verificar as tarefas que venceram ou vão vencer **no dia** é um desperdício exagerado. Se o usuário logou de manhã e não houver notificação, a próxima verificação será ao fim do dia, não no segundo seguinte!


Triangle angle calculation knowing only one angle

Question: I have two GPS positions and i want to calculate the angle between the one GPS point and a third point that i'm estimating it location with reference to the second GPS point. The Figure below explains the problem which is calculating angle(Alpha) in the figure. Here is what i know. first I calculated the heading of point A Heading (A) -- initial angle bearing, using old and new GPS coordinate of point A-- then I found the bearing angle from point A to B BeraingAngle(AB,AF).Now since both handing and bearing angle are relive to the north then Angle(Beta)=angle(AB,BF)= abs(Heading(A)-BearingAngle(AB,AF)). Also using haversine method i computed the distance two GPS point A and B dist(A,B). now I know the distance between point B and Cdist(B,C) and the angle(Theta)angle(BC,BE). So is it possible to calculate angle(Alpha) angle(AC,AD).
Example

Heading (A) =270 degrees, Bearing (A,B) =300 degrees, Hence Angle
(Beta)=300-270=30 degrees
dist (A,B)= 15 meters
dist(B,C)=5 meters
angle(Theta)=7 degrees

Note that BE is parallel to FD and BF is parallel to ED
Comment: i really dont understand what we know from the  figure
Comment: It would be very nice if you can just sum up all the known and unknowns in an ordered way.
Comment: You say you have a figure but you didn't add it?
Comment: I have updated the figure, Thanks a lot
Comment: It would be helpful if you proofread the OP and make edits.
Comment: Thanks Xpw for mentioning that, I mixed up angles names.Now it is fixed.
Answer: You don't have enough information.  The line $DEC$ could slide to the right with $C$ rising to keep $\theta$ constant.  There is nothing that constrains it.  Similarly $BF$ can slide either direction without causing a problem.
Comment: I have updated the figure to show what is know and what is not. Would you please explain in more details.
Comment: i know the length of AB and BC. if i can find the length of AD and DC i can find `angle(alpha)` right?
Answer: Proof by pictures:

Renaming some variables

taking components of a

taking components of b

as opposite sides in rectangle are equal

*There is a typo in the picture above, in the right upper corner it is $b \cos(c)$ and not $b\cos(d)$
rewriting

Can you handle now?
Comment: Thank you, I was almost there figuring it out .Yes , It is Arctan (CD/AD)


Por que quando eu tento imprimir fora das condições, o compilador informa que minha variável não foi inicializada?

Question: estou enfrentando o seguinte problema em Java:

Quando tento executar a impressão das variáveis nome, aumento e salario_ajustado dentro das minhas condições, o código é compilado com sucesso e roda normalmente:

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

    double aumento, salario, salarioMinimo, salario_ajustado;

    System.out.printf("Nome do funcionário: ");
    String nome = input.nextLine();

    System.out.printf("Salário Atual: R$");
    salario = input.nextDouble();

    System.out.printf("Valor do salário mínimo: R$");
    salarioMinimo = input.nextDouble();

    if(salario/salarioMinimo<3)
    {
        aumento = salario*0.5;
        salario_ajustado = salario + aumento;
        System.out.println("Funcionário: " +nome);
        System.out.println("Ajuste de: R$" +aumento);
        System.out.println("Salario ajustado: R$" +salario_ajustado);
    }
    else if (salario/salarioMinimo>=3 && salario/salarioMinimo<=10)
    {
        aumento = salario*0.2;
        salario_ajustado = salario + aumento;
        System.out.println("Funcionário: " +nome);
        System.out.println("Ajuste de: R$" +aumento);
        System.out.println("Salario ajustado: R$" +salario_ajustado);
    }
    else if (salario/salarioMinimo>10 && salario/salarioMinimo<=20)
    {
        aumento = salario*0.15;
        salario_ajustado = salario + aumento;
        System.out.println("Funcionário: " +nome);
        System.out.println("Ajuste de: R$" +aumento);
        System.out.println("Salario ajustado: R$" +salario_ajustado);
    }
    else if (salario/salarioMinimo>20)
    {
        aumento = salario*0.1;
        salario_ajustado = salario + aumento;
        System.out.println("Funcionário: " +nome);
        System.out.println("Ajuste de: R$" +aumento);
        System.out.println("Salario ajustado: R$" +salario_ajustado);
    }

}

}```
mas quando tento executar essas mesmas impressões fora do meu condicionamento, o console apresenta o seguinte erro:
     *Main.java:38: error: variable aumento might not have been initialized
     System.out.println("Ajuste de: R$" +aumento);*

Segue o código com a impressão fora do condicionamento:
    import java.util.Scanner;

    public class Main
    {
    public static void main(String[] args)
    {
    Scanner input = new Scanner(System.in);
    double aumento, salario, salarioMinimo, salario_ajustado;

    System.out.printf("Nome do funcionário: ");
    String nome = input.nextLine();

    System.out.printf("Salário Atual: R$");
    salario = input.nextDouble();

    System.out.printf("Valor do salário mínimo: R$");
    salarioMinimo = input.nextDouble();

    if(salario/salarioMinimo<3)
    {
        aumento = salario*0.5;
        salario_ajustado = salario + aumento;
    }
    else if (salario/salarioMinimo>=3 && salario/salarioMinimo<=10)
    {
        aumento = salario*0.2;
    }
    else if (salario/salarioMinimo>10 && salario/salarioMinimo<=20)
    {
        aumento = salario*0.15;
    }
    else if (salario/salarioMinimo>20)
    {
        aumento = salario*0.1;
    }

        System.out.println("Funcionário: " +nome);
        System.out.println("Ajuste de: R$" +aumento);
        System.out.println("Salario ajustado: R$" +salario_ajustado);

}

}
Lembrando que esse exercício é de uma lista iniciante e não há a necessidade de criar uma classe para executar a tarefa requisitada.
Comment: No segundo código, o compilador acha que pode ter alguma situação em que ele não entra em nenhum `if`, e portanto a variável `aumento` não será inicializada. Basta remover o último, deixando apenas o `else`, pois se chegou ali é porque com certeza é maior que 20 e não precisa testar de novo. Aliás, poderia guardar o valor de `salario/salarioMinimo` em uma variável em vez de fazer o mesmo cálculo toda hora (aliás, acho curioso como muita gente não cria variáveis quando precisa e cria quando não precisa, mas divago...)
Comment: Aproveitando, tem como simplificar essas condições: https://pt.stackoverflow.com/a/444359/112052
Answer: No código a variável aumento é declarada mas sua inicialização só ocorre dentro dos blocos com if. O que está acontecendo é que se nenhuma das condições dos  ifs forem satisfeitas a inicialização não acontece, se você modificar a linha (do último if):
else if (salario/salarioMinimo>20)
{
    aumento = salario*0.1;
}

para:
else
{
    aumento = salario*0.1;
}

o código irá funcionar.
O código pode ser melhorado um pouco se escrito da seguinte forma:
import java.util.Scanner;

public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double aumento, salario, salarioMinimo, salario_ajustado;

System.out.printf("Nome do funcionário: ");
String nome = input.nextLine();

System.out.printf("Salário Atual: R$");
salario = input.nextDouble();

System.out.printf("Valor do salário mínimo: R$");
salarioMinimo = input.nextDouble();

double indiceAumento = salario/salarioMinimo;
if(indiceAumento < 3)
{
    aumento = salario*0.5;
}
else if (indiceAumento >= 3 && indiceAumento <= 10)
{
    aumento = salario*0.2;
}
else if (indiceAumento > 10 && indiceAumento <= 20)
{
    aumento = salario*0.15;
}
else
{
    aumento = salario*0.1;
}
salario_ajustado = salario + aumento;
System.out.println("Funcionário: " +nome);
System.out.println("Ajuste de: R$" +aumento);
System.out.println("Salario ajustado: R$" +salario_ajustado);
}

Perceba que a variável salário ajustado também deve ser calculada fora dos ifs por que estava sendo calculada somente dentro do primeiro if, e se o código percorre qualquer outro caminho a variável salario_ajustado faria com que um erro ocorresse.


PCA scores in a for portfolio replication task: stumble over mean-centering question

Question: I'm trying to implement Principal Component Analysis (PCA) in a portfolio replication procedure.
(The replication procedure looks like regression: there is a vector representing payoffs of an asset under different ecocnomic scenarios,  I need to find a linear combination of vectors of another assets that fits the most closely to an initial asset).
Coefficients I get from regression should tell me how many assets I need to buy and sell to get approximately the same payoff as the asset I'm trying to replicate.
There is a problem with regression as the candidate asset matrix is ill-conditioned: assets show high correlation. There's a hope that orthogonal principal components could resolve this problem.
I need to do a PCA decomposition of candidate assets matrix, take only first $n$ components, do optimization and get components coefficients. Then I need to transform the coefficients back into an original basis.
Now the problem: PCA usually works with mean-centered data, but if I subtract means from the original data, I don't know how to interpret resulting coefficients in my case and don't know how to reverse the operation.
So far I'm doing eigen-decomposition of a covariance matrix, then using eigenvectors to make an orthogonal transformation of the data that is not mean-centered. Then I'm running a regression (actually L1 norm optimization) to get coefficients and transform them back into an original basis. The results are not bad, but I can't stop thinking about the problem with mean-centering, if I'm doing it completely wrong.
I was hoping to find a detailed math reasoning for this problem, but unfortunately failed.
I'm very much a noob in this and my math skills are far from being good, so I would really appreciate your help if you can share some insights on the problem of mean-centering in the PCA.
Comment: In the current state this is sooner a discourse with highly uncommon terminology than a question. I'm, particularly, in difficulty to make head or tail of it. Can you make the story clearer?
Comment: @ttnphns Thanks for pointing it out. I was really using too much of terminology. Hope now it's better. I tried to make the problem description clearer. Though I understand that it's still messy. I'm working on it.
Comment: `I'm doing eigen-decomposition of a covariance matrix, then using eigenvectors to make an orthogonal transformation of the data that is not mean-centered` Are you saying that you compute the PC scores by multiplying raw_data*eigenvectors, not centered_data*eigenvectors? (That gives the PCs which are completely correlated with the "true", centered PCs.)
Comment: @ttnphns Exactly. I plotted toy data in 2d and checked the rotation. It works the same as if data was mean-centered, but just a pivot point of rotation is not located in the mean (honestly, I don't know where it is). My concern now is if it can affect optimization results in some way.
After receiving optimized coefficients, i just multiply them by transposed eigenvectors.
Answer: Unstandardized principal component values (PC scores) are given by $\bf XU$, where $\bf X$ is the data matrix and $\bf U$ is the matrix of eigenvectors. If covariance matrix decomposed to $\bf U$, - then $\bf X$ must be centered to give proper PC scores. But you said that you used raw data to compute the PC scores despite that eigenvectors came from covariance matrix. What happens then?
Example is below. Variables are V1, V2. PC1 and PC2 are the scores computed in the usual way; these are centered PC scores. dcPC1 and dcPC2 are the scores computed by "your" way; these are decentered PC scores.
<code>      V1       V2      PC1      PC2    dcPC1    dcPC2

  6.7662   8.6155   2.8843    .3930  10.9224    .8427
  5.9534   6.9533   1.0506    .6401   9.0887   1.0897
  5.1772   4.6352  -1.3083   1.2819   6.7298   1.7315
  5.3906   3.5785  -2.0685   2.0461   5.9696   2.4958
  3.0136   6.5524   -.9154  -1.5821   7.1227  -1.1325
  1.4195   3.8332  -4.0620  -1.3978   3.9761   -.9481
  4.9248   6.0971   -.2327    .2602   7.8054    .7098
  4.5031   8.5152   1.5441  -1.4333   9.5822   -.9837
  6.0504   6.8867   1.0491    .7578   9.0872   1.2074
  1.7513   2.2287  -5.2121   -.2308   2.8260    .2188
  4.4432   7.1862    .4056   -.7451   8.4437   -.2955
  2.8280   5.4160  -1.9635  -1.1054   6.0746   -.6558
  6.8661   3.4229  -1.3786   3.3597   6.6595   3.8093
  3.6724   3.9823  -2.6869    .3930   5.3512    .8426
  5.8395   6.4047    .5311    .8500   8.5692   1.2996
  6.7118  11.4956   5.2492  -1.2516  13.2873   -.8020
  4.7179   8.8247   1.9208  -1.4266   9.9589   -.9770
  1.0230   3.2331  -4.7813  -1.3943   3.2568   -.9446
  7.2815  10.1138   4.4165   -.0105  12.4546    .4391
  8.4197  10.7265   5.5581    .5958  13.5962   1.0454
</code>
The pic displays the principal components drawn in the space of the variables. In both computational cases, the PCs are the same - as axes. They orthogonally cross each other in the centre of the cloud. This is because covariance matrix was analyzed (it implies centering). However, the scores (shown as markers on the PC axes) are different. "Decentered PC1" = "Centered PC1" + 8.04. "Decentered PC2" = "Centered PC2" + 0.45. It is unclear what use could be of such decentered scores, since they do not share their means with each other and with the data cloud.
Answer: @ttnphns Thank you for your answer. You're right, in common case, without mean-centering PCA would lose a lot of sense.
But I think, if I use it to regularize regression, it could work.
Here I wrote a bit of algebra for myself. I hope it's correct, but I would appreciate some comments, cause I'm not completely sure.

Initial problem (doesn't work because A'A is nearly collinear and numerically uninvertible)
$$
y = Ax
$$
Decompose covariance matrix of data (standardized data). To get correct (from the PCA perspective) eigen-everything.
$$
cov(A) = VDV'
$$
Transform raw-data (without mean centering) into an orthogonal basis
$$
AV
$$
(just a linear transformation).
Coefficients $x^*$ for transformed problem are founded because matrix $AV$ is well-conditioned.
$$
y = AVx^*
$$
As (1) and (4) are approximately equal
$$
Ax = AVx^*
$$
so...
$$
x = (A'A)^{-1}A'AVx^*
$$
A'A seems to have a full rank, so theoretically is invertible analiticaly
$$
x = Vx^*
$$
Comment: From your (unusual) notatition one may see that `A` is the multivariate data matrix and `x` are the vector of regression coefficients. `A` implies a rectangular matrix. How can a rectangular matrix ever be invertible or uninvertible (unless we speak of generalized inversion)? In (6) you invert it, uninvertible by your own words. (7) can't follow from (6) because `A^-1 * A` would have been `I`, identity matrix, and not `1`.
Comment: @ttnphns Oh, you right, that was stupid. I wanted to say that A'A is practically non-invertible because of numerical errors. But theoretically it still has a full rank. Can't I premultiply 5 by A' to get A'Ax = A'AVx* and then premultiply A'A by its inverse to cansel them? I'm doing inverse analiticaly, not numerically, so should I consider it to be a problem?
p.s.: Sorry for notation. It's my first question here. Probably, I need some time to figure out how it works.


Lower Bound on the Number of Graph Isomorphism Classes

Question: Are there any non-trivial lower bounds on the number of isomorphism classes for a graph with $N$ vertices?
For example there are at least $N(N-1)/2$ isomorphism classes (counting one for the number of possible edges in our graph) but as $N$ increases, there will clearly be a lot more.
Comment: Here's a magic answer : http://math.stackexchange.com/questions/353053/is-there-a-formula-for-finding-the-number-of-nonisomorphic-simple-graphs-that-ha
Comment: Why's it magic?
Answer: You really need to say what you mean by non-trivial. What do you want to use the bound for? But, gripes aside, $2^{\binom{n}2}/n!$ is a lower bound and asymptotically tight.
Comment: I meant $N(N-1)/2$ as the trivial lower bound. Do you have a reference for this?
Comment: The number of graphs in the isomorphism class of $G$ is $n!/|\mathrm{Aut}(G)|$, so it's at most $n!$. So the number of isomorphism classes is at least $2^n/n!$. I do not think a reference is necessary.
Comment: $2^{{n \choose 2}}/n!$. Yes that is enough.


SQL INSERT failed, I can't find the issue

Question: I'm trying to insert something into a SQL server (using c#) and when I try to it says it requires the (in the query) given parameter and that it can't find it while it is declared.

"Additional information: The parameterized query '(@art varchar(8000),@oms varchar(8000),@rem varchar(8000),@artdk' expects the parameter '@art', which was not supplied."

I checked parameter amount, the database connection and I tried to use another insert command I have as reference but I couldn't get it to work. and I couldn't find the same sort of problem on the internet yet. if someone could help me, it would be much appreciated.
UPDATE:
I changed the artnr.selectedvalue to artnr.text and that sort of got me some progress but now I get this error
UPDATE2:
seems that if I fill every box it tries to insert it works.. yet it does allow null in all database cell's so the issue has been found. just got to work out which one('s) cant have empty.

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Error converting data type varchar to numeric.

it gives this error when I put in all numeric or all text
SqlCommand slinkoopadd = new SqlCommand(@"insert into ART (ART ,OMS ,REM ,ARTDK ,TYPE ,MAG ,PROGRAM ,EH1 ,LEV ,LTD ,INK ,KOR ,SGR ,EH2 ,EF ,VALUTA ,CRNI )
                                                   values (@art,@oms,@rem,@artdk,@type,@mag,@program,@eh1,@lev,@ltd,@ink,@kor,@sgr,@eh2,@ef,@valuta,@crni);", Connectie.connMEVO_ART);
        #region parameters
        slinkoopadd.Parameters.Add("@art", SqlDbType.VarChar).Value = this.artnr.SelectedValue;
        slinkoopadd.Parameters.Add("@oms", SqlDbType.VarChar).Value = this.tekstinkoopoms.Text;
        slinkoopadd.Parameters.Add("@rem", SqlDbType.VarChar).Value = this.tekstinkoopopmerk.Text;
        slinkoopadd.Parameters.Add("@artdk", SqlDbType.VarChar).Value = this.tekstinkoopnummerlev.Text;
        slinkoopadd.Parameters.Add("@type", SqlDbType.VarChar).Value = this.tekstinkooparttype.Text;
        slinkoopadd.Parameters.Add("@mag", SqlDbType.VarChar).Value = this.tekstinkoopmagazijnloc.Text;
        slinkoopadd.Parameters.Add("@program", SqlDbType.VarChar).Value = this.tekstinkoopinternopmerk.Text;
        slinkoopadd.Parameters.Add("@eh1", SqlDbType.VarChar).Value = this.tekstinkoopeenheid.Text;
        slinkoopadd.Parameters.Add("@lev", SqlDbType.VarChar).Value = this.tekstinkoopstandleveran.Text;
        slinkoopadd.Parameters.Add("@ltd", SqlDbType.VarChar).Value = this.tekstinkooplevertijd.Text;
        slinkoopadd.Parameters.Add("@ink", SqlDbType.VarChar).Value = this.tekstinkoopbrutoprijs.Text;
        slinkoopadd.Parameters.Add("@kor", SqlDbType.VarChar).Value = this.tekstinkoopkorting.Text;
        slinkoopadd.Parameters.Add("@sgr", SqlDbType.VarChar).Value = this.tekstinkoopserievoorraad.Text;
        slinkoopadd.Parameters.Add("@eh2", SqlDbType.VarChar).Value = this.tekstinkoopgebruikeh.Text;
        slinkoopadd.Parameters.Add("@ef", SqlDbType.VarChar).Value = this.textinkoopehfactor.Text;
        //slinkoopadd.Parameters.Add("@", SqlDbType.VarChar).Value = this.artnr.Text;//perc. voor vracht
        slinkoopadd.Parameters.Add("@valuta", SqlDbType.VarChar).Value = this.tekstinkoopvaluta.Text;
        slinkoopadd.Parameters.Add("@crni", SqlDbType.VarChar).Value = this.tekstinkoopcrni.Text;
        //slinkoopadd.Parameters.Add("@", SqlDbType.VarChar).Value = this.artnr.Text;//extra kosten
        //slinkoopadd.Parameters.Add("@", SqlDbType.VarChar).Value = this.artnr.Text;//bestelgrootte afroep
        //slinkoopadd.Parameters.Add("@", SqlDbType.VarChar).Value = this.artnr.Text;//prognose jaarverbruik
        //slinkoopadd.Parameters.Add("@", SqlDbType.VarChar).Value = this.artnr.Text;//levertijd nieuwe afr
        #endregion
        drART = slinkoopadd.ExecuteReader();
        MessageBox.Show("Opgeslagen!");
        fillbox();
        while (drART.Read())
        { }
        slinkoopadd.Dispose();

Comment: for those willing to try and help me. i will still be searching online for something that could help me, and i will keep checking this page so if there is some information you need please just ask.
Comment: Have you checked that ` this.artnr.SelectedValue ` is set and has a meaningful value?
Comment: You don't use `ExecuteReader` with `INSERT` statement. Use `ExecuteNonQuery` to execute it. `ExecuteReader` returns some data.
Comment: im trying to use 1 panel for mutliple things so the textbox is used to select an item it has to display normaly but for the insert it is more used like a textbox. i have tried diffrent things like the .text and .selectedtext

and the ExecuteReader does work with it so could you tell me the real diffrence between them?
Comment: If `this.artnr.SelectedValue` is `null`, you must pass `DBNull.Value` instead - if that fails run a Profiler Trace and see exactly whats being dispatched
Comment: its suppost to get the item that someone typed in there as i clear the list before so it should insert the typed text.
Comment: I would breakpoint & double check
Answer: Try it like this?
slinkoopadd.Parameters.Add("@art", SqlDbType.VarChar);
slinkoopadd.Parameters["@art"].Value = this.artnr.SelectedValue;

I'm not sure you can pipe the call like that.
Comment: Why? This is the same as `slinkoopadd.Parameters.Add("@art", SqlDbType.VarChar).Value = this.artnr.SelectedValue;` This won't effect anything.
Comment: I've had issues in the past with piping the call like that, as in Add did not return an object, just a boolean indicating success.
Comment: the parameters i have for this one are in the same format as i used them for all my other query's and they work without a problem atm
Answer: drART = slinkoopadd.ExecuteReader();

You don't expect to read something. You want to insert, that is not a query:
var result = slinkoopadd.ExecuteNonQuery();

Comment: This is _of course_ true. But doesn't point the error OP have :)
Comment: it inserts either way as far as i know and i dont really want to change a thing that i know that works if it doesnt solve the problem. i want to solve the problem first and then i could look into that :)
Comment: @maam27 I guess we simply differ by process. I would not search for errors as long as I *know* something else is not in order.
Comment: if i wasnt sure that the executereader worked or not i would have changed it but i know it will work so i rather go with something i know i work then something i would have no clue about
Answer: i changed some small things in the query and i made sure some of the textboxes get a default value if it doesnt have anything in it so now it works. this is the current query that works.
if (tekstinkoopcrni.Text == "1") { } else if (tekstinkoopcrni.Text == "0") { }
else { tekstinkoopcrni.Text = "0"; }
if (tekstinkoopkorting.Text == "") { tekstinkoopkorting.Text = "0"; }
if (tekstinkoopserievoorraad.Text == "") { tekstinkoopserievoorraad.Text = "0"; }
if (tekstinkoopstandleveran.Text == "") { tekstinkoopstandleveran.Text = "9999"; }
if (tekstinkooplevertijd.Text == "") { tekstinkooplevertijd.Text = "0"; }

SqlCommand slinkoopadd = new SqlCommand(@"insert into ART (ART ,OMS ,REM ,ARTDK ,TYPE ,
MAG ,PROGRAM ,EH1 ,INK ,KOR ,SGR ,EH2 ,EF ,VALUTA ,CRNI  ,LEV ,LTD )
values (@art,@oms,@rem,@artdk,@type,@mag,@program,@eh1,@ink,@kor,@sgr,@eh2,@ef,
@valuta,@crni, @lev,@ltd);", Connectie.connMEVO_ART);

#region parameters
slinkoopadd.Parameters.Add("@art", SqlDbType.VarChar).Value = this.artnr.Text;
slinkoopadd.Parameters.Add("@oms", SqlDbType.VarChar).Value = this.tekstinkoopoms.Text;
slinkoopadd.Parameters.Add("@rem", SqlDbType.VarChar).Value = this.tekstinkoopopmerk.Text;
slinkoopadd.Parameters.Add("@artdk", SqlDbType.VarChar).Value = this.tekstinkoopnummerlev.Text;
slinkoopadd.Parameters.Add("@type", SqlDbType.VarChar).Value = this.tekstinkooparttype.Text;
slinkoopadd.Parameters.Add("@mag", SqlDbType.VarChar).Value = this.tekstinkoopmagazijnloc.Text;
slinkoopadd.Parameters.Add("@program", SqlDbType.VarChar).Value = this.tekstinkoopinternopmerk.Text;
slinkoopadd.Parameters.Add("@eh1", SqlDbType.VarChar).Value = this.tekstinkoopeenheid.Text;
slinkoopadd.Parameters.Add("@lev", SqlDbType.VarChar).Value = this.tekstinkoopstandleveran.Text;
slinkoopadd.Parameters.Add("@ltd", SqlDbType.VarChar).Value = this.tekstinkooplevertijd.Text;
slinkoopadd.Parameters.Add("@ink", SqlDbType.VarChar).Value = this.tekstinkoopbrutoprijs.Text;
slinkoopadd.Parameters.Add("@kor", SqlDbType.VarChar).Value = this.tekstinkoopkorting.Text;
slinkoopadd.Parameters.Add("@sgr", SqlDbType.VarChar).Value = this.tekstinkoopserievoorraad.Text;
slinkoopadd.Parameters.Add("@eh2", SqlDbType.VarChar).Value = this.tekstinkoopgebruikeh.Text;
slinkoopadd.Parameters.Add("@ef", SqlDbType.VarChar).Value = this.textinkoopehfactor.Text;
slinkoopadd.Parameters.Add("@valuta", SqlDbType.VarChar).Value = this.tekstinkoopvaluta.Text;
slinkoopadd.Parameters.Add("@crni", SqlDbType.VarChar).Value = this.tekstinkoopcrni.Text;
#endregion
drART = slinkoopadd.ExecuteReader();
MessageBox.Show("Artikel opgeslagen!");
fillbox();
while (drART.Read())
{ }
slinkoopadd.Dispose();


What does the ° symbol mean , as in F#°?

Question: The internet search engine seems not to recognize it, so I cannot search for it there! I had assumed it was "augmented" but am now very unsure.
Thanks
Comment: Diminishing returns from a search engine, eh?
Comment: @thrig Availability or lack thereof of any information via a web search is not to be a criterion on Stack Exchange. We want to be a (top?) hit on search engines, which means we want to have the answers here even if those answers are available elsewhere.
Comment: _"The internet search engine"_?
Comment: I cannot think that this has not been asked before.
Comment: @thrig - searching for nothing (o) was never going to be easy...
Comment: I found it searching for "music theory chord symbol °" on Google. It even shows up in a helpful table!
Comment: @ToddWilcox - I think thrig was being punny.
Comment: @ToddWilcox   he was most definitely making a pun that was too sharp for you.   We're all in ac-chord.
Comment: @CarlWitthoft - hope your comment doesn't fall flat - could be a natural reaction, barring any other response. Just a breve comment, don't want to cause any treble...
Comment: The symbol ° means diminished.  F#° would be an "F sharp diminished" chord.  The augmented symbol would be +.
Answer: Augmented is '+'. The 'o' is diminished. With your example of F#o the notes involved are F#, A, C. The diminished seventh chord will include Eb, although I can't remember ever seeing the symbol 'o7'.
Comment: Or at least F#, A, and C.  I think most people would omit the Eb unless the chord name had a 7 attached?
Comment: @L3B - thanks, edited. Funny, but I often include that bit when a dim chord is on a chart!
Comment: F#° pretty much implies Eb, as this note does not add any color, and I don't recall having ever seen the symbol °7 (but I have seen +7). Also note that this is a *diminished* seventh, not a minor one. Finally, you surely cannot play E natural with this chord though, or the symbol would have been that of a half-diminished chord, another harmony.
Comment: @AlexandreC. - exactly. the half dim chord is subtly different, often called m7b5.
Comment: Not subtly, but completely different. F#° is eg. a V in G (and also in other tones), and F#m7b5 is a II in E minor.
Comment: @AlexandreC. - subtle was not the apposite word!
Answer: More specific to the question of what the degree sign means is that yes it is a diminished chord and that means that the chord has the notes that form a minor third from the root and a diminished fifth from the root.
So, as Tim mentions, in this case, it will be F#, A (minor third) and F# to C (diminished fifth)
Answer: Different editors/publishers seem to notate certain chords differently, but a fairly common scheme is to describe an C diminished triad (C-Eb-Gb) as C(b5), an C half-diminished 7th (C-Eb-Gb-Bb) as Cm7(b5), and a C full-diminished 7th (C-Eb-Gb-Bbb) as C°.  Note that a C full-diminished-7th is enharmonically the same as a diminished triad with an added major 6th (C-Eb-Gb-A), and because it would be weird to describe a note nine frets up as a "seventh" (rather than a major sixth) but it would also be weird suggest that the top two notes of the chord are only separated by a second when they're three frets apart, using the C° notation is a simple way of saying that chord note contains pitches that are spaced upward at intervals of three frets starting at C without having to worry about what those notes should be called.
Comment: I'd spell C7b5 as C E Gb Bb. Wouldn't it be C Eb Gb Bb for half dim, Cm7b5?
Comment: @Tim: Corrected.  The way I tune my guitar, half-diminished chords and full-diminished chords are easy to play and sound great, but the major-7-flat-5 is quite a bit more awkward so I sometimes forget about that chord.  When writing out my own tabs, I use a slashed circle for half-diminished chords since I find all the stuff with parentheses harder to read while playing.
Comment: Sometimes see it written Cm7-5, obviating parentheses. Slashed circle is good - can't find it on my qwerty board, though! Same with 'degree'.
Comment: @Tim: The Javascript thing I wrote for my guitar tabs takes chord names in ASCII and then substitutes suitable characters for sharps, flats, slashed or unslashed circles, etc.  So typing Abho7 will produce a fingering symbol with a big 6 over it showing the index finger across five strings (6th fret) and the middle finger on the next fret,  and something like A♭⊘₇ (I forget which circle-slash character I used) underneath it.


Problema quando digitam str no Python

Question: Eu escrevi esse código:
while True:
    try:
        n1 = int(input('Digite o número 1/5: '))
    except ValueError as e:
        print("Digite um número:",)
        if ValueError:
            n1 = int(input('Digite o número 1/5: '))
    try:
        n2 = int(input('Digite o número 2/5: '))
    except ValueError as e:
        print("Digite um número:",)
        if ValueError:
            n2 = int(input('Digite o número 2/5: '))
    try:
        n3 = int(input('Digite o número 3/5: '))
    except ValueError as e:
        print("Digite um número:",)
        if ValueError:
            n3 = int(input('Digite o número 3/5: '))
    try:
        n4 = int(input('Digite o número 4/5: '))
    except ValueError as e:
        print("Digite um número:",)
        if ValueError:
            n4 = int(input('Digite o número 4/5: '))
    try:
        n5 = int(input('Digite o número 5/5: '))
    except ValueError as e:
        print("Digite um número:",)
        if ValueError:
            n5 = int(input('Digite o número 5/5: '))
    if n1 < n2 and n1 < n3 and n1 < n4 and n1 < n5:
        print('O menor número é: {}'.format(n1))
    elif n2 < n1 and n2 < n3 and n2 < n4 and n2 < n5:
        print('O menor número é: {}'.format(n2))
    elif n3 < n1 and n3 < n2 and n3 < n4 and n3 < n5:
        print('O menor número é: {}'.format(n3))
    elif n4 < n1 and n4 < n2 and n4 < n3 and n4 < n5:
        print('O menor número é: {}'.format(n4))
    elif n5 < n1 and n5 < n2 and n5 < n3 and n5 < n4:
        print('O menor número é: {}'.format(n5))
    if n1 < n2 < n3 < n4 < n5 :
        print('Está em ordem crescente')
        break
    else:
        print('Não está em ordem crescente')
    break

No caso quando digita 1 vez uma string não da problema, mas quando se digita a string dnv da erro, obs: estou treinando se alguem souber algusn exercicios que possa ajudar tambem agradeço.
Answer: Primeiro, crie uma função para ler números que insista até que o usuário digite algo válido:
def ler_numero(mensagem, tente_novamente = 'Isso daí não é um número. Tente novamente.'):
    while True:
        try:
            return int(input(mensagem))
        except ValueError as e:
            print(tente_novamente)

Com isso, fica mais fácil fazer o que você quer:
while True:
    n1 = ler_numero('Digite o número 1/5: ')
    n2 = ler_numero('Digite o número 2/5: ')
    n3 = ler_numero('Digite o número 3/5: ')
    n4 = ler_numero('Digite o número 4/5: ')
    n5 = ler_numero('Digite o número 5/5: ')
    if n1 < n2 and n1 < n3 and n1 < n4 and n1 < n5:
        print('O menor número é: {}'.format(n1))
    elif n2 < n1 and n2 < n3 and n2 < n4 and n2 < n5:
        print('O menor número é: {}'.format(n2))
    elif n3 < n1 and n3 < n2 and n3 < n4 and n3 < n5:
        print('O menor número é: {}'.format(n3))
    elif n4 < n1 and n4 < n2 and n4 < n3 and n4 < n5:
        print('O menor número é: {}'.format(n4))
    elif n5 < n1 and n5 < n2 and n5 < n3 and n5 < n4:
        print('O menor número é: {}'.format(n5))
    if n1 < n2 < n3 < n4 < n5:
        print('Está em ordem crescente')
        break
    else:
        print('Não está em ordem crescente')
    break

Answer: # Checador de numeros
numeros = []     # -> Todos os números validos vão entrar nessa lista!
numero = 0
contagem = 0
while contagem != 5:
    try:
        numero = int(input(f"Digite um número: ({contagem + 1}/5) "))
        contagem += 1
        numeros.append(numero)
    except ValueError:
        print("Digite um número válido!")

n1 = numeros[0]
n2 = numeros[1]
n3 = numeros[2]
n4 = numeros[3]
n5 = numeros[4]

menor = n1  # O primeiro numero sempre será o menor no começo!

if n2 < menor:
    menor = n2
if n3 < menor:
    menor = n3
if n4 < menor:
    menor = n4
if n5 < menor:
    menor = n5

print(f"O numero menor foi: {menor}")

if n1 < n2 < n3 < n4 < n5:
    print("Os numeros estão em ordem!")
else:
    print("Os numeros não estão em ordem")

Comment: excelente, agora tudo faz sentido :), não tinha pensado nessa posibilidade de colocar todos os números em uma lista.

Problema quando digitam str no Python


Group Standard Deviations by Factors

Question: I have a model of the form:
library(nlme)
lme1 <- lme(
  y ~ 1,
  random  = ~ Var1 + Var2 | J,
  weights = varIdent(form= ~ 1|Var1*Var2),
  data = df
)

I want to estimate the standard deviations of the groups J based on factors Var1 and Var2 (the random argument in the model), and the standard deviations of the observations within groups based on factors Var1 and Var2 (the weights argument in the model).
In this example Var1 and Var2 both have two levels a and b.
I can get the standard deviations of the observations within groups using:
sigma(lme1)*coef(lme1$modelStruct$varStruct, unconstrained=FALSE, allCoef=TRUE)

# Which gives:
#      a*a       a*b       b*a       b*b
# 9.205590  8.052488 12.427807 11.533546

How would I find the standard deviations of the groups?
VarCorr(lme1)

#J = pdLogChol(Var1 + Var2)
#            Variance  StdDev   Corr
#(Intercept) 548.99374 23.43062 (Intr) Var1b
#Var1b       412.81370 20.31782  0.612
#Var2b       272.71690 16.51414 -0.810 -0.764
#Residual     84.74288  9.20559

This is a simulated example, so I know about what the values should be, but don't know how to calculate them from the model.
Answer: I think I just figured it out. The results seem to make sense.
#The matrix for the portion of the model would be of the form:
#   (Intercept) Var1b Var2b
#           1     0     0
#           1     0     1
#           1     1     0
#           1     1     1

# So for the first row I simply need var(Intercept)
# Second row is var(Intercept + Var2b)
# Third row is var(Intercept + Var1b)
# Fourth row is var(Intercept + Var1b + Var2b)

# For this, what I expect is about:
#23.5 13.9 35.0 25.2

# What I get is:
(vc <- getVarCov(lme1))
sqrt(vc[1,1])                                                       #23.43062
sqrt(sum(diag(vc)[c(1,3)]) + 2*vc[1,3])                             #13.97188
sqrt(sum(diag(vc)[c(1,2)]) + 2*vc[1,2])                             #39.29574
sqrt(sum(diag(vc)[c(1,2,3)]) + 2*vc[1,2] + 2*vc[1,3] + 2*vc[2,3])   #26.03314
```

Group Standard Deviations by Factors


Is there a sound theoretical reason why a capella choirs "drift off" of the starting pitches in a piece?

Question: Is the "comma pump" an empirical problem, or merely theoretical?
It's a standard music theory exercise to show that certain chord progressions don't actually quite work. For example, in the I-IV-ii-V-I progression, following the "naive" method of finding each pitch (tuning each major chord as a pure 4:5:6 ratio and the minor chord as a pure 1/6:1/5:1/4 ratio, and keeping common notes between adjacent chords constant) will lead to the final I chord being flatter than the initial one by a syntonic comma.
Is this actually part of the reason why choirs or singers singing a capella (or anyone else making music without a pervasive absolute pitch reference) tend to drift in pitch unless they're careful and/or skilled? Or is anyone whose pitch sense is good enough for this not to be dominated by other errors also capable of instinctively compensating for it?
Comment: The reverse situation, a group of singers tending to converge on a single key, is discussed in [Large Groups Tending Towards Specific Keys?](https://music.stackexchange.com/questions/39825/large-groups-tending-towards-specific-keys)
Answer: I would say yes, and yes. You've explained the problem pretty clearly, and explained its consequence. Choirs frequently find that they sing everything internally, consistently in-tune throughout a piece, but then at the end of the piece, they discover that they are no longer in tune with the reference pitches upon which they started the piece. The frame of reference has drifted. Inexperienced singers don't understand that this is in fact a natural occurance. In fact many choir directors don't understand it either, and expect their choirs to sing in 12-tone equal temperament as if there was a piano playing along all the time.
However, singers and string players can compensate for the drift in pitch and try to keep pitches in line with the starting pitches, and end the piece in the same pitch framework where they started. It is a struggle, but it is often possible.
Update
Reference this previously asked question, about Arnold Schoenberg's observation about why choirs get "off pitch": Natural vs. Tempered Semi-Tones
As I said previously in my answer to that question:

As a singer with perfect pitch, when I am in this situation,
  particularly in performance, it presents a dilemma: if I perceive the
  other choral singers around me drifting away from the starting pitch,
  tonic and key (taking into account the modulations that may be written
  into the piece), do I fight against the other singers by keeping my
  pitches up, relative to the starting pitch and tonic and key, or do I
  "go with it" and follow the gradual shift in tonic that everyone else
  is making? In most cases, it's better to "go with it" rather than
  "fight it", because it produces a more internally consistent sound.
  While I have perfect pitch, I must concede that if a choir were only
  to sing the 12 pitches on the piano, that would not be a good choir,
  because that is not really what choirs are supposed to do.
Comment: This reminds me of a Kodaly-pedagogy course I took some years ago, mostly intended for the few teachers of that music school (about 8-10 participants). At that time, I also used to tune pianos for that school, and I tuned the old one (known to have issues in keeping tuning, also caused by heating issues of the building) we normally used for that course just the week before the beginning, with the course lasting a few weekends. As you may know, the Kodaly method makes extensive use of voice, and is also mostly based on the concept of relative pitch: mobile do, possibly ignoring the base tuning.
Comment: One of the participants had perfect pitch; attending that course with him was, somehow, extremely formative; he constantly tried to force the pitch while singing together: sometimes because the starting note came from the piano (gradually going out of tune), but most of the times because we drifted off for some reason. Whenever he felt we were too much out of (his) tune, he'd start to sing louder, trying to "restore" tuning, even if everybody else was singing in a "common tune". The teacher had to reprehend him multiple times, but now I know another reason for which perfect pitch is a chimera.
Answer: I would like to add the point that the comma pump may happen in either direction, resulting ascending or descending drift.
However, tonal music is such that the intervals between the roots (the fundamentals of the chords) usually appear in one direction and not in the other: descending fifths or ascending fourths and ascending seconds, mainly. As a result, the comma pumps produced by tonal harmony usually are descending. And choirs singing tonal music indeed usually go down.
There are various ways to resist the drift, but I believe that the best of them involve dissonances, during which the "justness" of the intervals is less crucial than during perfect chords.
Answer: I would just add the (possibly obvious) answer that a capella choirs can also drift off because of singing out of tune.  Typically, they tend to get flatter if the music has lots of jumps to high notes, which they don't quite get up to, and they sometimes get sharper if they are nervous about getting flat.  I've experienced both in concerts.
Answer: Choirs drift mostly for the same reason new string players drift - because the singers' / musicians' ears are untrained, and they just don't hear it.  We take ear training for the same reason we learn to see color in art class, and learn to taste (smell) at wine tastings.
Choirs go flat when they are tired or inexperienced, or sharp with they are excited / energetic (and also untrained).
However, highly trained choirs can also change pitch if the room is tuned (standing tone for instance) just slightly away from the starting key; in which case the choir will drift to the standing tone of the room, which presents problems if the choir is accompanied, or the choral director doesn't know of this phenomenon.  However, if the conductor and choir realize it, and just sing in the standing tone, it will be practically impossible to drift.
Incidentally, frequencies are wavelengths over time, so melodic tones are inherently rhythmic.  Thus, standing tones in the room can also affect rhythmic drift as well.  In this way, tempo and tone are inseparable like the way physicists think of space-time.
A prominent choral director once told me "You pick a tempo and stick with it".  I think, however, this is very incorrect when you go on tour and move from hall to hall.  The hall very much influences tempo, and often times microtuning.
I am an experienced choir director.


FreeBSD determine if reboot was due to power loss

Question: We are running FreeNAS (which is built on FreeBSD) to run our data storage systems. It is running on an APC Smart-UPS 750VA X.
On a couple of occasions, I have been alerted to the fact that the server is down by our monitoring systems. After a few minutes the server is back up and running no problems.
When I run last I can see that the server has just booted and checking /var/log/messages I can see it has run through all of the boot process however I can't see any panics or any reason for it shutting down. It literally goes from being fine to outputting boot information.
So this has led me to wonder if its a power outage that is causing this to happen but how can I determine for sure this is the case? I guess getting an interactive card for the APC UPS and hooking it up to the network would be one way... Any other way of me finding out right now why this has happened?
Comment: No detail on the hardware, whether you have dual power supplies, whether a UPS signal cable is connected, other items in the environment... You shouldn't be asking the internet about your local site's power or hardware issues.
Comment: @ewwhite I'm not asking about my local site's power or hardware issues - I'm asking how I can determine if there are local site power or hardware issues.
Comment: Which only YOU can tell us! We can't tell you if you're having power issues.
Answer: I think there are a few obvious solutions to finding out more:

Hook up an additional device to the same UPS output. If both devices reboot simultaneously, that would be evidence of a power issue.
Hook up some equipment that can monitor the actual voltage output by the UPS output and record changes over time.
Comment: Seems like this could be the answer and I need to hook up some more equipment to monitor the UPS - here goes spending more money :P
Comment: @Chris The first of my two suggestions doesn't require specialized equipment, so there might be a chance you have something around, which would fulfill that role. All it requires is a device that logs the time of each boot. The second of my suggestions require specialized equipment, but it would also provide more accurate answers.
Answer: Your machine can't really tell what happened in a power outage: those electrons just stop showing up.  The UPS might know (if you're losing power, as opposed to a flaky power supply or something) but I don't think you have much hope of the server being able to tell you.
Comment: Thanks for your message. As it is a smart UPS, it is connected to the server and thus we do get notifications when the system switches to battery. So I know the UPS battery wasn't low - I'm just worried the UPS isn't working quite right when there's just a small flicker in power.
Answer: FreeBSD has a great port named sysutils/apcupsd intended to interact with APC smart-UPSes.
Link your UPS with usb-cable to the host. Edit /usr/local/etc/apcupsd/apcupsd.conf:
###
UPSCABLE usb
UPSTYPE usb
DEVICE
POLLTIME 60
LOCKFILE /var/spool/lock
BATTERYLEVEL 15
MINUTES 10
TIMEOUT 0
ANNOY 300
ANNOYDELAY 60
KILLDELAY 120
UPSCLASS standalone
STATTIME 60
STATFILE /var/log/apcupsd.status
###

That config means the next behaviour:

when power is lost for less than ANNOYDELAY seconds, UPS just goes on battery with no signal
after ANNOYDELAY seconds UPS begin to beep.
when (accu level becomes lower than BATTERYLEVEL percent) OR (estimated time on battery is less than MINUTES), apcupsd will wait for KILLDELAY seconds and begin shutdown -h now process.
after that UPS will power off the load and goes into hybernation.
when power is back, UPS power on the load and, if it is configured to boot after power loss, it will be launched normally and cycle is complete.
Comment: Interestingly we already have this running and we also have email notifications when the battery gets low/ goes onto battery/ comes back online and none of this happened! Appreciate your suggestion though.
Comment: Smart-UPS have his own EEPROM and can be configured in some way. Sometimes UPS and apcupsd settings conflicting with unpredictable consequencies.
Comment: @Chris There's also a log of UPS events...
Answer: I don't feel like you've done the bare minimum of troubleshooting here. This has become a bad question because of the scare details presented.

Maybe your server is crashing.
What type of server hardware are you using?
Do you have out-of-band management on this server? (ILO, IPMI, DRAC) If so, what does it say?
How frequently is this happening?
Do any of your other devices in the same room reset? Switches? Other servers? What else is impacted?
Where is your monitoring system located in relation to the server, since it's clearly not having problems?

Obvious thing to do...

Connect the APC black serial cable to your UPS and your FreeNAS server.
Look at the logs of your apcupsd service. (/var/log/serviceapcupsd.events in Linux)

(easy)
2014-10-27 05:18:35 -0400  Power failure.
2014-10-27 05:18:38 -0400  Power is back. UPS running on mains.
2014-10-27 13:20:22 -0400  Power failure.
2014-10-27 13:20:25 -0400  Power is back. UPS running on mains.
2014-10-29 08:00:51 -0400  Power failure.
2014-10-29 08:00:54 -0400  Power is back. UPS running on mains.
2014-10-29 08:02:13 -0400  Power failure.
2014-10-29 08:02:16 -0400  Power is back. UPS running on mains.
2014-11-01 10:05:41 -0400  Power failure.
2014-11-01 10:05:44 -0400  Power is back. UPS running on mains.

Check your battery status. They last 3-5 years. If older, it's not holding your power load under transfer to battery power.
Check your UPS sensitivity to power fluctuations. They default to HIGH. I usually change this to LOW in crappy environments.
Does your FreeNAS server have dual power supplies? (if not, it should) Plug one into the wall to bypass the UPS and eliminate the UPS as a cause. This can also help logging if the server is rebooting.
Upgrade your UPS firmware.
Upgrade your server's firmware.
Call APC support.

(also easy)
[root@General /var/log]# apcaccess status localhost:3551
APC      : 001,052,1316
DATE     : 2014-11-17 08:24:55 -0500
HOSTNAME : General
VERSION  : 3.14.10 (13 September 2011) redhat
UPSNAME  : UPS_IDEN
CABLE    : Custom Cable Smart
DRIVER   : APC Smart UPS (any)
UPSMODE  : Stand Alone
STARTTIME: 2014-09-24 14:14:05 -0400
MODEL    : SMART-UPS 2200 RM XL
STATUS   : ONLINE LOWBATT
LINEV    : 119.6 Volts
LOADPCT  :  60.8 Percent Load Capacity
BCHARGE  : 100.0 Percent
TIMELEFT :   3.0 Minutes
MBATTCHG : 5 Percent
MINTIMEL : 3 Minutes
MAXTIME  : 0 Seconds
MAXLINEV : 120.9 Volts
MINLINEV : 118.9 Volts
OUTPUTV  : 119.6 Volts
SENSE    : High
DWAKE    : 000 Seconds
DSHUTD   : 180 Seconds
DLOWBATT : 02 Minutes
LOTRANS  : 103.0 Volts
HITRANS  : 132.0 Volts
RETPCT   : 000.0 Percent
ITEMP    : 44.5 C Internal
ALARMDEL : 5 seconds
BATTV    : 56.1 Volts
LINEFREQ : 60.0 Hz
LASTXFER : No transfers since turnon
NUMXFERS : 11
XONBATT  : 2014-11-15 09:05:48 -0500
TONBATT  : 0 seconds
CUMONBATT: 44 seconds
XOFFBATT : 2014-11-15 09:05:52 -0500
SELFTEST : NO
STESTI   : 336
STATFLAG : 0x07000048 Status Flag
DIPSW    : 0x00 Dip Switch
REG1     : 0x00 Register 1
REG2     : 0x00 Register 2
REG3     : 0x00 Register 3
MANDATE  : 11/05/05
SERIALNO : QS0545111716
BATTDATE : 11/05/05
NOMOUTV  : 115 Volts
NOMBATTV :  48.0 Volts
EXTBATTS : 0
FIRMWARE : 83.14.D
END APC  : 2014-11-17 08:25:04 -0500

Comment: It is very scary log for me. You have 100% BCHARGE, TIMELEFT = 3 min and DSHUTD = 180 sec. That mean that UPS drain the batteries before normal shutdown even begins... Seems that batteries should be replaced and calibrated.
Comment: Thanks for your feedback - this is very useful. It's a brand new APC unit - no battery issues or anything that I know of. It is clear now that I will have to run more diagnostics.

1) Server crashing - no logs to suggest this
2) Server hardware - Dell PowerEdge 510 and APC 750 VA RM
3) OOB on server - DRAC setup, not recording any issues
4) Frequency - Every 3+ days
5) Other resets - Switch is unmanaged so can't get stats. Only server in rack is the Dell
6) Monitoring system remote but corresponds with the local servers uptime
Comment: @Kondybas Yes, this is a scary log. It's a very bad example using 9 year-old batteries.
Answer: Save your logs to disk.
You may change log path to disk by this util or change path manually. At next reboot you can find out a reason.


solve system equation $\sin(x) – \sin(2y) = 2y−x $, $ \cos(x) + 5\sin(y) = 4 $

Question: Can you tell me how to solve this system equation ?
$$ \sin(x) – \sin(2y) = 2y−x $$
$$ \cos(x) + 5\sin(y) = 4 $$
Answer: Hint: From the first equation, we have $x+\sin x=2y+\sin2y$, implying that $x=2y$, since the function $f:\mathbb R\to\mathbb R,~f(t)=t+\sin t$ is bijective.


Freeze in C++ program using huge vector

Question: I have an issue with a C++ program. I think it's a problem of memory.
In my program i'm used to create some enormous std::vector (i use reserve to allocate some memory). With vector size of 1 000 000, it's ok but if i increase this number (about ten millions), my program will freeze my PC and i can do nothing except waiting for a crash (or end of the program if i'm lucky). My vector contains a structure called Point which contain a vector of double.
I used valgrind to check if there is a memory lack. But no. According to it, there is no problem. Maybe using a vector of objects is not advised ? Or maybe is there some system parameters to check or something ? Or simply, the vector is too big for the computer ?
What do you think about this ?
Comment: "if there is a memory lack" yes, I would think that a lack of more memory is the reason.
Comment: ...In my program i'm used to create some enormous std::vector...  Why for heaven's sake are you doing this...?
Comment: @Nim I would go to `deque` first.
Comment: @user1158692 - `sizeof` gives the actual size of a struct. Most people get it wrong when they try to figure it out for themselves.
Comment: In addition to comments above: even if such a huge contiguous block of memory would theoretically fit in memory based on a size comparison, that is still no guarantee of nice program behavior. Consider that the amount of memory that is available to your program is always less than physical memory. Furthermore, you are always competing with other processes for memory. Lastly, you may trigger the OS to start swapping; which kills performance.
Comment: What are the specs of your machine? How much RAM?
Comment: It may be that your system cannot allocate a contiguous block of that size, if so, you could try using a `list`, obviously this will have a performance impact...
Comment: Can you also show some minimal code which will reproduce the problem
Comment: How many doubles are there inside each of your struct object?
Comment: It should be a fairly easy task to calculate the ballpark of the expected size of your vector - add the sizes of the members and multiply by vector size
Comment: @BoBTFish, or that...
Comment: @PeteBecker Well, yeah, but maybe right enough...  : )
Comment: Do you really need all the data in memory *at the same time*?  Most programs with large data, only process the data in "chunks".
Comment: @PeteBecker for an intensive applied maths calculation. I work on big vectors but linear algebra library like Eigen or others didnt have tools for my purpose.
Comment: @pippin1289 I think i have a RAM of 4Go. I will check this tomorrow at work.
Comment: @AbhishekBansal 8 doubles
Comment: @ThomasMatthews , well i need all my data for each step of my solver. However, i can maybe use "chunks" for the initialisation of all my data, but i don't know how to do.
Comment: @user3046300 In that case I'd seriously suggest you go over your design, with the explicit goal of: (1) reworking it to allow you to introduce 'divide and conquer' in your calculations or (2) restating the problem in such a way that the amount of vectors required is reduced significantly or (3) both (1) and (2).
Comment: @user3046300 Based on your comments consider updating your question to include your exact problem. Instead of trying to solve your current  memory-related problem, inquire about a solution to your real problem instead.
Answer: Disclaimer
Note that this answer assumes a few things about your machine; the exact memory usage and error potential depends on your environment. And of course it is even easier to crash when you don't compute on 2d-Points, but e.g. 4d-points, which are common in computer graphics for example, or even larger Points for other numeric purposes.
About your problem
That's quite a lot of memory to allocate:
#include <iostream>
#include <vector>
struct Point {
    std::vector<double> coords;
};
int main () {
    std::cout << sizeof(Point) << std::endl;
}

This prints 12, which is the size in bytes of an empty Point. If you have 2-dimensional points, add another 2*sizeof(double)=8 to that per element, i.e. you now have a total of 20 bytes per Point.
With 10s of millions of elements, you request 200s of millions of bytes of data, e.g. for 20 million elements, you request 400 million bytes. While this does not exceed the maximum index into an std::vector, it is possible that the OS does not have that much contiguous memory free for you.
Also, your vectors memory needs to be copied quite often in order to be able to grow.  This happens for example when you push_back, so when you already have a 400MiB vector, upon the next push_back you might have your old version of the vector, plus the newly allocated 400MiB*X memory, so you may easily exceed the 1000MiB temporarilly, et cetera.
Optimizations (high level; preferred)
Do you need to actually store the data all time? Can you use a similar algorithm which does not require so much storage? Can you refactor your code so that storage is reduced? Can you core some data out when you know it will take some time until you need it again?
Optimizations (low level)
If you know the number of elements before creating your outer vector, use the std::vector constructor which you can tell an initial size:
vector<Foo> foo(12) // initialize have 12 elements

Of course you can optimize a lot for memory; e.g. if you know you always only have 2d-Points, just have two doubles as members: 20 bytes -> 16 bytes. When you do not really need the precision of double, use float: 16 bytes -> 8 bytes. That's an optimization to $2/5$:
// struct Point { std::vector<double> coords; };   <-- old
struct Point { float x, y; }; // <-- new

If this is still not enough, an ad-hoc solution could be std::deque, or another, non-contiguous container: No temporal memory "doubling" because no resizing needed; also no need for the OS to find you such contiguous block of memory.
You can also use compression mechanisms, or indexed data, or fixed point numbers. But it depends on your exact circumstances.
struct Point { signed char x, y; }; // <-- or even this? examine a proper type
struct Point { short x_index, y_index; };

Comment: I would go with `std::deque` before trying to change all of the logic around to use `std::list`.
Answer: Without seeing your code, this is just speculation, but I suspect it is in large part due to your attempt to allocate a massive amount of memory that is contiguous.  std::vector is guaranteed to be in contiguous memory, so if you try to allocate a large amount of space, the operating system has to try to find a block of memory that large that it can use.  This may not be a problem for 2MB, but if you are suddenly trying to allocate 200MB or 2GB of contiguous memory ...
Additionally, anytime you add a new element to the vector and it is forced to resize, all of the existing elements must be copied into the new space allocated.  If you have 9 million elements and adding the 9,000,001 element requires a resize, that is 9 million elements that have to be moved.  As your vector gets larger, this copy time takes longer.
Try using std::deque instead.  It is will basically allocate pages (that will be contiguous), but each page can be allocated wherever it can fit.

Freeze in C++ program using huge vector


Help with understanding summation formula breakdown

Question: $S_{xy} = \sum_{i=1}^n (x_i-\bar{x})(y_i-\bar{y}) = \sum_{i=1}^n x_iy_i - \frac{1}{n}(\sum_{i=1}^n x_i) (\sum_{i=1}^n y_i)$
I don't understand how it becomes $\frac{1}{n}(\sum_{i=1}^n x_i) (\sum_{i=1}^n y_i)$
$S_{xx} = \sum_{i=1}^n (x_i-\bar{x})^2 = \sum_{i=1}^n x_i^2 - \frac{1}{n}(\sum_{i=1}^n x_i)^2$
I also don't understand how the above was expanded.
Comment: Does this answer your question? [How to explain the equation about summation?](https://math.stackexchange.com/questions/2779123/how-to-explain-the-equation-about-summation). Note that $\bar{x} = \frac{1}{n}\sum_{i=1}^{n}x_i$ and $\bar{y} = \frac{1}{n}\sum_{i=1}^{n}y_i$ to see how the other question is basically the same as yours.
Answer: $\sum_i (x_i-\bar{x})(y_i-\bar{y}) = \sum_i \left(x_iy_i - \bar{x}y_i - x_i\bar{y}+\bar{x}\bar{y}\right)$
$=\sum_i(x_iy_i)-\sum_i(\bar{x}y_i)-\sum_i(x_i\bar{y})+\sum_i(\bar{x}\bar{y})$
Now... recognize that the terms $\bar{x}$ and $\bar{y}$ are in fact just constants and so may be factored outside of their respective summations
$=\sum_i(x_iy_i)-\bar{x}\sum_i(y_i) - \bar{y}\sum_i(x_i) + \bar{x}\bar{y}\sum_i(1)$
Recall that $\sum_{i=1}^n(1)=n$.  Next, recall that $\bar{x}=\frac{1}{n}\sum_i(x_i)$ and similarly for $\bar{y}$.  Rearranging a bit by multiplying both sides by $n$ we get that $\sum_i(x_i)=n\bar{x}$ so we continue as:
$=\sum_i(x_iy_i)-n\bar{x}\bar{y} - n\bar{x}\bar{y}+n\bar{x}\bar{y}$
One pair of these cancel, and rewriting $\bar{x}$ and $\bar{y}$ as sums instead gives us
$=\sum_i(x_iy_i)-n(\frac{1}{n}\sum_i(x_i))(\frac{1}{n}\sum_i(y_i))$
$=\sum_i(x_iy_i)-\frac{1}{n}(\sum_ix_i)(\sum_iy_i)$ as desired

The second identity involving only $x$'s is exactly the same as the first identity, just using $x$'s in place of $y$'s and simplifying with exponents where necessary.
Answer: $$\begin{align}S_{xy}&=\sum_{i=1}^n (x_i-\bar x)(y_i - \bar y)
\\&=\sum_{i=1}^n (x_i y_i-\bar xy_i -x_i\bar y + \bar x\bar y)
\\&=\sum_{i=1}^n x_i y_i-\bar x\sum_{i=1}^ny_i -\bar y\sum_{i=1}^nx_i + \sum_{i=1}^n\bar x\bar y
\\&=\sum_{i=1}^n x_i y_i-n\bar x\bar y -\frac1n\left(\sum_{i=1}^ny_i\right)\left(\sum_{i=1}^nx_i\right) + n\bar x\bar y
\\&=\sum_{i=1}^n x_i y_i -\frac1n\left(\sum_{i=1}^ny_i\right)\left(\sum_{i=1}^nx_i\right)
\end{align}$$
The other equality is seen by substituting $y_i = x_i$.


Kaleidoscope Example Code Compile Errors on MacOS with LLVM(8|10)

Question: Compiling kaleidoscope tutorial code fails with clang++ -g -O3 toy.cpp $(llvm-config --cxxflags) -std=c++17 (as the example goes) and outputs the following error:
Undefined symbols for architecture x86_64:
  "llvm::DisableABIBreakingChecks", referenced from:
      llvm::VerifyDisableABIBreakingChecks in toy-e1a114.o
ld: symbol(s) not found for architecture x86_64
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)

LLVM were installed with brew install llvm (version 10 when this question is posted), later changed to brew install llvm@8.
Interesting thing is, removing header llvm/ADT/STLExtras.h actually solves this. But I'm afraid removing this will not be a general solution.
Answer: I think the problem might be that $(llvm-config --cxxflags) doesn't work properly in macOS. I don't know exactly what it is with --cxxflags, but I personally experienced a problem that $(llvm-config --libfiles) didn't return the correct path to the shared library libLLVM in macOS (that command worked in Linux anyway).
However, I suggest to use CMake, which you will need anyway when working with LLVM. Below is a sample CMake code copied from the LLVM website. I followed this CMake code and can compile my project in macOS.
cmake_minimum_required(VERSION 3.4.3)
project(SimpleProject)

find_package(LLVM REQUIRED CONFIG)

message(STATUS "Found LLVM ${LLVM_PACKAGE_VERSION}")
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")

# Set your project compile flags.
# E.g. if using the C++ header files
# you will need to enable C++11 support
# for your compiler.

include_directories(${LLVM_INCLUDE_DIRS})
add_definitions(${LLVM_DEFINITIONS})

# Now build our tools
add_executable(simple-tool tool.cpp)

# Find the libraries that correspond to the LLVM components
# that we wish to use
llvm_map_components_to_libnames(llvm_libs support core irreader)

# Link against LLVM libraries
target_link_libraries(simple-tool ${llvm_libs})


Click button to change CSS in D7

Question: I'm trying to create a clickable link that changes the css using jquery. I've tried many approaches and nothing seems to work. Hopefully someone here can push me into the right direction.
Button html (in a block):
Change CSS

Button code (in change_css.js):
(function($) {
    Drupal.behaviors.my_node_toggle = {
        attach: function(context, settings) {
            $("#change_css", context).click(function(){
                $('a').css({"color":"#ff0"});
            });
        }
    };
})(jQuery);

The script is being loaded, but nothing happens when I click on the link.
Comment: I suggest you put some debug code , like `console.log("click fired correctly");` in click handler function to sure that click handler work correctlty
Answer: Your problem resides in the way you are calling the css function in jQuery. Try this, it works for me:
(function($) {
  Drupal.behaviors.my_node_toggle = {
    attach: function(context, settings) {
      $("#change_css", context).click(function() {
        $('a').css("color","#ff0");
      });
    }
  };
})(jQuery);

Click button to change CSS in D7