#include <iostream>$ g++ array_init_every_time.cpp
using namespace std;
int main()
{
for (int i = 0; i < 3; ++i) {
int a[1] = {13};
cout << a[0] << endl;
}
return 0;
}
$ ./a.out
13
13
13
An occasionally updated blog, mostly related to programming. (Views are my own, not my employer's.)
#include <iostream>$ g++ array_init_every_time.cpp
using namespace std;
int main()
{
for (int i = 0; i < 3; ++i) {
int a[1] = {13};
cout << a[0] << endl;
}
return 0;
}
R:Okay. That's not what I was expecting.
> a <- "Yes"
> f <- function() { return(a) }
> a <- "No"
> f()
[1] "No"
Scheme:Hm. But (in R):
> (define a "Yes")
> (define f (lambda () a))
> (define a "No")
> (f)
"No"
printMatrix(a) := block([as],Naturally, again I have to wonder very strongly if there isn't already a better way to do this.
as : matrixmap(lambda([x],
printf(false, "~a", x)), a),
simplode(maplist(lambda([row],
simplode(row, " & ")),
as),
sconc(" ", "\\\\", newline)))$
diag(d) := block([n], n : length(d),
genmatrix(lambda([i, j],
if i = j then d[i] else 0),
n, n))$
(%i90) diag_matrix(1,2,3);
[ 1 0 0 ]
[ ]
(%o90) [ 0 2 0 ]
[ ]
[ 0 0 3 ]
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int columns;
int rows;
double* data;
} Matrix;
void freeMatrix(Matrix* aMatrix)
{
if (aMatrix) {
free(aMatrix->data);
free(aMatrix);
}
}
int countChar(const char* str, char c)
{
int count = 0;
while (*str) {
if (*str == c) {
++count;
}
++str;
}
return count;
}
#define MAX_LINE 2000
/**
* input -- a stream of comma-separated numbers, rows terminated
* by new lines. The number of rows is determined from
* the first line read.
* naValue -- the value that will be used to replace
* fields left blank.
*
* Returns NULL if there is a problem reading from the stream.
* Check errno for details.
*/
Matrix* readCsv(FILE* input, double naValue) {
Matrix* result = 0;
char buffer[MAX_LINE];
char* line = fgets(buffer, MAX_LINE, input);
if (!line) {
return 0;
}
else {
result = malloc(sizeof(Matrix));
int total = 0;
int capacity = 0;
result->rows = 0;
result->columns = countChar(line, ',') + 1;
capacity = result->columns * 20;
result->data = malloc(sizeof(double) * capacity);
do {
int i;
if ((total + result->columns) > capacity) {
capacity *= 2;
result->data = realloc(result->data,
sizeof(double) * capacity);
}
for (i = 0; i < result->columns; ++i) {
if (!line || ',' == line[0]
|| '\n' == line[0] || '\r' == line[0]) {
result->data[total] = naValue;
}
else {
double value;
if (sscanf(line, "%lf", &value) == 1) {
result->data[total] = value;
}
else {
freeMatrix(result);
return 0;
}
}
++total;
if (line) {
line = index(line, ',');
if (line) {
++line;
}
}
}
++(result->rows);
line = fgets(buffer, MAX_LINE, input);
} while (line && ('\n' != line[0])
&& ('\r' != line[0]));
}
return result;
}
void printMatrix(FILE* out, const Matrix* aMatrix)
{
int i;
for (i = 0; i < aMatrix->rows; ++i) {
int j;
const int offset = i * aMatrix->columns;
for (j = 0; j < aMatrix->columns; ++j) {
fprintf(out, "%f ", aMatrix->data[offset + j]);
}
fprintf(out, "\n");
}
}
int main(int argc, char** argv)
{
if (argc == 2) {
FILE* in = fopen(argv[1], "r");
Matrix* m = readCsv(in, -HUGE_VAL);
fclose(in);
if (m) {
printMatrix(stdout, m);
freeMatrix(m);
}
else {
printf("problem reading %s\n", argv[1]);
}
}
return 0;
}
drice <- function(x, mu, sigmaSq) {Now I wanted to look at the density with a fixed x but multiple values of mu. But for instance drice(1, c(1,2), 1) ==> 0.4657596. What's a good way to handle this? It's pretty ugly, but I think this does what I want:
ifelse(x >= 0,
x / sigmaSq *
exp(-(x*x + mu*mu)/(2*sigmaSq))*besselI(x*mu/sigmaSq, 0),
0)
}
drice <- function(x, mu, sigmaSq) {drice(1, c(1,2), 1) ==> c(0.4657596, 0.1813670) But there has to be a better way.
n <- max(length(x), length(mu), length(sigmaSq))
x <- rep(x, length.out=n)
mu <- rep(mu, length.out=n)
sigmaSq <- rep(mu, length.out=n)
ifelse(x >= 0,
x / sigmaSq *
exp(-(x*x + mu*mu)/(2*sigmaSq))*besselI(x*mu/sigmaSq, 0),
0)
}
##x -- a vector of single element stringsIt's hard to believe these aren't defined (in native code) in the base environment. Of course, R's primary focus isn't string manipulation, but these come up naturally enough when building up labels for plots. Maybe they're worried about internationalization issues? (Or maybe I've just missed the relevant functions.)
charToUpper <- function(x) {
i <- match(x, letters)
ifelse(!is.na(i), LETTERS[i], x)
}
##s -- string to be put in uppercase
upper <- function(s) {
paste(charToUpper(strsplit(s, "")[[1]]), collapse="")
}
titleCase <- function(s) {
n <- nchar(s)
if (n > 0) {
paste(charToUpper(substr(s, 1, 1)),
substr(s, 2, n), sep="")
}
else {
s
}
}