Jul 14
STA version 0.2
icon1 Mikkel Meyer Andersen | icon4 July 14, 2010 at 19:06 (UTC) | icon3 No Comments »
icon3 , ,

Already an update with the following changes from version 0.1:

General:

  • Icon changed
  • Decimal separator always “.” no matter the chosen locale of the phone (for consistency purposes)
  • Screen rotate issues fixed

Distribution tool:

  • Typing error: Continuous distributions density output changed from “F([input]) = …” to “f([input]) = …”
  • Error description at the parameter tab if the parameters are illegal when trying to plot
  • Descriptives gets calculated automatically when sampling data
  • The link under properties has been made clickable
Jul 13
STA (Statistical Toolbox for Android) version 0.1
icon1 Mikkel Meyer Andersen | icon4 July 13, 2010 at 10:50 (UTC) | icon3 No Comments »
icon3 , ,

Finally, a (“beta”) version 0.1 of STA is available on the market. Just search for STA. Please let me know if you run into trouble or would like certain features!

Jul 6
STA: Statistical Toolbox for Android
icon1 Mikkel Meyer Andersen | icon4 July 6, 2010 at 13:32 (UTC) | icon3 3 Comments »
icon3 , ,

After having done some preliminary application development for Android (and finally finished my master’s), I’ve decided to start a new project. And to blog about the creation of this new project. (As an aside I would really like to point out that I haven’t forgot about Watexy, but for now it is not possible to improve it.)

The aim of the project is to develop an Android-application with basic statistical tools (I really miss R on my phone, but the project won’t be a R-clone nevertheless). So far the codename for the application is Statistical Toolbox for Android (or simply STA).

It is not going to be a programming language such as S, but an easy-to-use graphical statistical toolbox. The features I’ve thought about including in the first version are:

  • Quantiles (and fractiles) for a wide range of univariate probability distributions
  • Descriptive statistics (the first two or three empirical moments, correlation measures)
  • A guide for choosing the right statistical test

The features for the later versions could be:

  • Loading datasets (from mail, files on SD-card, or manual input)
  • A range of statistical tests

If any of you have any comments, please do not hesitate to submit a comment here or by mail (use the contact form accessible from the top menu or by sending an e-mail to the reverse of mikl.dk @ scienco ).

Aug 28
qqmultinorm.R version 1.1 – more intelligent plot size
icon1 Mikkel Meyer Andersen | icon4 August 28, 2009 at 07:20 (UTC) | icon3 No Comments »
icon3 ,

I’ve updated the qqmultinorm.R script a bit so that it’s now capable of picking a more optimal plot size (less unused space).

The idea refers to deciding the sides (dimensions) of a rectangle if the areal (number of plots) is known i.e., optimising the dimensions of a rectangle given the areal. We want the perimeter as small as possible (for better viewing), preferably wider than longer if square isn’t possible. A given number n is factorised in to numbers within a given error. For example if the allowed error is 2, then 7 gets factorised in (1,7), (2, 4), (4, 2), and (7, 1) (here redundant factorisation is included). Although 2*4 = 8, but abs(7-8) = 1 <= 2, so it's acceptable. Actually the default error is 10% of the input areal.

The way the proper factorisations is chosen, is by ordering the pairs by ascending the difference of the components i.e, how much the width and height differs. And then ordered ascending by height so that the plot gets wide screen-like instead of poster-like.

The new script is a follows (only find.optimal.mfrow.size(...) and a bit logic in qqmultinorm(...) added):

# File name: qqmultinorm.R
# Version: 1.1
# Last updated: 2009-08-28
#
# This R-code is made by:
# Mikkel Meyer Andersen, Denmark
# mikl [funny-a] math [.] aau [.] dk or 
# mikl [funny-a] mikl [.] dk
#
# Licence: GPLv2
#
# Feel free to use it, but if you do I'll like to hear about it (just for fun).
# If you make corrections, please submit them back so others can enjoy them as well.
 
qqchisq <- function(y, main, df=2, continuity.correction = 0.5)
{
  n <- length(y)
  y <- sort(y)
  c <- numeric(n)
 
  for (i in 1:n)
    c[i] <- qchisq((i-continuity.correction)/n, df=df)
 
  plot(c, y, xlab="Theoretical Quantiles", ylab="Sample Quantiles", main=main)
  lines(c(c[1], c[n]), c(c[1], c[n]), type="l")
}
 
dec2bin <- function(x)
{
  if (!is.vector(x) || length(x) != 1 || x < 0)
    stop("x must be a non-negative integer")
 
  N <- length(x)
  ndigits <- floor(log2(x)) + 1
  bin <- numeric(ndigits)
 
  for (i in (ndigits-1):0)
  {
    tmp <- 2^i
 
    if (x %/% tmp >= 1)
    {
      bin[i+1] <- 1
      x <- x - tmp
    }
  }
 
  return(rev(bin))
}
 
# Returns the power set without the empty set
power.set <- function(v)
{
  n <- length(v)  
  N <- 2^n - 1
  ps <- vector("list", N)
 
  for (i in 1:(N-1))
  {
    Nbin <- dec2bin(i)
    Nbin <- c(numeric(n-length(Nbin)), Nbin)
    Nbin <- rev(Nbin)
    ps[[i]] <- v[which(Nbin == 1)]
  }
 
  ps[[N]] <- v
 
  return(ps)
}
 
# Input: n
#  - here the number of plots
# Returns c(h, w)
#  - the optimal choice of rows and cols to use in in mfrow
find.optimal.mfrow.size <- function(n)
{  
  w0 <- ceiling(sqrt(n))
 
  # The area can at max contain of 10% unused space
  max.error <- round(n*0.1)
 
  n.minus.error <- n - max.error
  n.plus.error <- n + max.error
 
  # If n is a square, fine!
  if (w0^2 == n)
    return(c(w0, w0))
 
  # Col 1 and 2: w and h
  # Col 3: The difference between w and h: this should be as small as possible
  candidates <- matrix(ncol=3)
 
  for (w in w0:1)  
  {    
    h <- ceiling(n / w)
    n0 <- w*h
 
    # Because of ceiling we know that n0 >= n
    if (n0 <= n.plus.error)
      candidates <- rbind(candidates, c(h, w, abs(w-h)))
  }
 
  # First row is NA
  candidates <- candidates[-1,]
 
  # Uups, something went wront - well, don't panic
  if (nrow(candidates) == 0)
    return(c(w0, w0))
 
  # First order by abs(w-h) and then by h to get a widescreen-look 
  # instead of a poster-look
  candidates <- candidates[order(candidates[,3], candidates[,1]), ]
 
  return(candidates[1, c(1,2)])
}
 
# dataset: variables in columns and observations as rows
# subset.min.size, subset.max.size: inclusive limits
# filename: if specified, the plot are saved as a png file with this filename
qqmultinorm <- function(dataset, subset.min.size = 1, subset.max.size = 4, filename = NULL, use.optimale.size = F)
{
  p <- ncol(dataset)
  n <- nrow(dataset)
 
  if (subset.min.size < 1) stop("subset.min.size < 1")
  if (subset.min.size > p) stop("subset.min.size > p")
  if (subset.max.size < 1) stop("subset.max.size < 1")
  if (subset.max.size > p) stop("subset.max.size > p")
  if (subset.min.size > subset.max.size) stop("subset.min.size > subset.max.size")
 
  if (is.null(colnames(dataset)))
    colnames(dataset) <- 1:p
 
  # We have p variables. If all is to be checked against each other,
  # then we have a power-set with 2^p subsets (including the empty set)
 
  # Here we get subset containing indexes of the variables to include
  # Note that power.set doesn't include the empty set.
  subsets <- power.set(1:p)
  subsets.len <- length(subsets)
 
  # To get the plots with the fewest variables first, we do a litte trick:
  # While we find out which plots to include, we build a list
  # where each element of a list is the index of the subset,
  # and the index of the element is the size of the subset.
  # (The +1 is because the limits are includesive!)
  s.included <- vector("list", subset.max.size - subset.min.size + 1)
 
  plots <- 0
 
  for (i in 1:subsets.len)
  {
    s <- subsets[[i]]
    s.len <- length(s)
 
    if (s.len >= subset.min.size && s.len <= subset.max.size)
    {
      plots <- plots + 1
      s.included[[s.len - subset.min.size + 1]] <- c(s.included[[s.len - subset.min.size + 1]], i)
    }
  }
 
  # Now it's possible to build the subset index vector; 
  # we disregard the size of each subset; no more need to know it.
  s.indexes <- c()
 
  for (s in s.included)
  {
    s.indexes <- c(s.indexes, s)
  }
 
  # We want the best view  
  plot.per.row <- ceiling(plots^(1/2))
  plot.per.column <- ceiling(plots^(1/2))
 
  if (use.optimale.size)
  {
    mfrow.parameters <- find.optimal.mfrow.size(plots)
    plot.per.row <- mfrow.parameters[1]
    plot.per.column <- mfrow.parameters[2]
  }
 
  plot.width <- 300
  plot.height <- 200
 
  if (!is.null(filename))
    png(file=paste(filename, ".png", sep=""), bg="white", width = plot.per.row * plot.width, height = plot.per.column * plot.height)
 
  par(mfrow = c(plot.per.row, plot.per.column))  
 
  # There's no need to calculate a whole lot several times:
  ybar <- as.vector(colMeans(dataset))
 
  S <- as.matrix(var(dataset))
 
  current <- 1  
  for (i in s.indexes)
  {
    s <- subsets[[i]]
    s.len <- length(s)
 
    if (s.len > subset.max.size)
      next
 
    cat("Processing subset no.", current, "out of", plots, "\n")
 
    # Container for our values
    squared.dist <- numeric(n)
 
    # qr.solve(A) = A^(-1)  
    Sinv <- qr.solve(S[s,s])
 
    # Then calculate the squared distance for each datapoint
    for (i in 1:n)
    {
      c <- dataset[i,s] - ybar[s]
      squared.dist[i] <- t(c) %*% Sinv %*% c
    }
 
    # Finding the order statistic
    squared.dist <- sort(squared.dist)
 
    qqchisq(squared.dist, paste(colnames(dataset)[s], collapse=", "), s.len)
 
    current <- current + 1
  }
 
  if (!is.null(filename))
    dev.off()
}
 
# Example:
A <- matrix(rnorm(2000, mean=3, sd=2), ncol=8)
qqmultinorm(A, 2, 2, "multinorm-optimal.size", use.optimale.size = T)
qqmultinorm(A, 2, 2, "multinorm", use.optimale.size = F)
Aug 27
qqmultinorm.R – evaluating the normality of a sample
icon1 Mikkel Meyer Andersen | icon4 August 27, 2009 at 11:44 (UTC) | icon3 No Comments »
icon3 ,

I wrote a R-script that can be used to evaluate for multivariate normality of a sample. It’s a kind of generalisation to the qqnorm, but this one just uses sums of the squared statistical distance which is then chi squared distributed with degrees of freedom equalling the number of squared statistical distance summed.

The useful thing about this script, is that it’s able to plot all possible combinations of the variables. It’s possible to specify the minimum number of variables to compare and the maximum number of variables to compare. All possible combinations are found by calculating the power set (using binary representation through the decimal to binary conversion, dec2bin, because if a set has k elements, then its power set has 2^k elements, and the binary representation of the numbers from 1 to 2^k are then used to pick out the subsets in the power set).

Please notice that it’s possible to specify a filename so that the plots are written to a png file. This is by far the easiest thing – and only possibility – if there’s more than a few plots.

I haven’t wrote a lot of documentation besides this, but feel free to ask if in doubt of anything!

# File name: qqmultinorm.R
#
# This R-code is made by:
# Mikkel Meyer Andersen, Denmark
# mikl [funny-a] math [.] aau [.] dk or 
# mikl [funny-a] mikl [.] dk
#
# Licence: GPLv2
#
# Feel free to use it, but if you do I'll like to hear about it (just for fun).
# If you make corrections, please submit them back so others can enjoy them as well.
 
qqchisq <- function(y, main, df=2, continuity.correction = 0.5)
{
  n <- length(y)
  y <- sort(y)
  c <- numeric(n)
 
  for (i in 1:n)
    c[i] <- qchisq((i-continuity.correction)/n, df=df)
 
  plot(c, y, xlab="Theoretical Quantiles", ylab="Sample Quantiles", main=main)
  lines(c(c[1], c[n]), c(c[1], c[n]), type="l")
}
 
dec2bin <- function(x)
{
  if (!is.vector(x) || length(x) != 1 || x < 0)
    stop("x must be a non-negative integer")
 
  N <- length(x)
  ndigits <- floor(log2(x)) + 1
  bin <- numeric(ndigits)
 
  for (i in (ndigits-1):0)
  {
    tmp <- 2^i
 
    if (x %/% tmp >= 1)
    {
      bin[i+1] <- 1
      x <- x - tmp
    }
  }
 
  return(rev(bin))
}
 
# Returns the power set without the empty set
power.set <- function(v)
{
  n <- length(v)  
  N <- 2^n - 1
  ps <- vector("list", N)
 
  for (i in 1:(N-1))
  {
    Nbin <- dec2bin(i)
    Nbin <- c(numeric(n-length(Nbin)), Nbin)
    Nbin <- rev(Nbin)
    ps[[i]] <- v[which(Nbin == 1)]
  }
 
  ps[[N]] <- v
 
  return(ps)
}
 
# dataset: variables in columns and observations as rows
# subset.min.size, subset.max.size: inclusive limits
# filename: if specified, the plot are saved as a png file with this filename
qqmultinorm <- function(dataset, subset.min.size = 1, subset.max.size = 4, filename = NULL)
{
  p <- ncol(dataset)
  n <- nrow(dataset)
 
  if (subset.min.size < 1) stop("subset.min.size < 1")
  if (subset.min.size > p) stop("subset.min.size > p")
  if (subset.max.size < 1) stop("subset.max.size < 1")
  if (subset.max.size > p) stop("subset.max.size > p")
  if (subset.min.size > subset.max.size) stop("subset.min.size > subset.max.size")
 
  if (is.null(colnames(dataset)))
    colnames(dataset) <- 1:p
 
  # We have p variables. If all is to be checked against each other,
  # then we have a power-set with 2^p subsets (including the empty set)
 
  # Here we get subset containing indexes of the variables to include
  # Note that power.set doesn't include the empty set.
  subsets <- power.set(1:p)
  subsets.len <- length(subsets)
 
  # To get the plots with the fewest variables first, we do a litte trick:
  # While we find out which plots to include, we build a list
  # where each element of a list is the index of the subset,
  # and the index of the element is the size of the subset.
  # (The +1 is because the limits are includesive!)
  s.included <- vector("list", subset.max.size - subset.min.size + 1)
 
  plots <- 0
 
  for (i in 1:subsets.len)
  {
    s <- subsets[[i]]
    s.len <- length(s)
 
    if (s.len >= subset.min.size && s.len <= subset.max.size)
    {
      plots <- plots + 1
      s.included[[s.len - subset.min.size + 1]] <- c(s.included[[s.len - subset.min.size + 1]], i)
    }
  }
 
  # Now it's possible to build the subset index vector; 
  # we disregard the size of each subset; no more need to know it.
  s.indexes <- c()
 
  for (s in s.included)
  {
    s.indexes <- c(s.indexes, s)
  }
 
  # We want a squared view
  plot.per.row <- ceiling(plots^(1/2))
  plot.per.column <- ceiling(plots^(1/2))
  plot.width <- 200
  plot.height <- 200
 
  if (!is.null(filename))
    png(file=paste(filename, ".png", sep=""), bg="white", width = plot.per.row * plot.width, height = plot.per.column * plot.height)
 
  par(mfrow = c(plot.per.row, plot.per.column))  
 
  # There's no need to calculate a whole lot several times:
  ybar <- as.vector(colMeans(dataset))
 
  S <- as.matrix(var(dataset))
 
  current <- 1  
  for (i in s.indexes)
  {
    s <- subsets[[i]]
    s.len <- length(s)
 
    if (s.len > subset.max.size)
      next
 
    cat("Processing subset no.", current, "out of", plots, "\n")
 
    # Container for our values
    squared.dist <- numeric(n)
 
    # qr.solve(A) = A^(-1)  
    Sinv <- qr.solve(S[s,s])
 
    # Then calculate the squared distance for each datapoint
    for (i in 1:n)
    {
      c <- dataset[i,s] - ybar[s]
      squared.dist[i] <- t(c) %*% Sinv %*% c
    }
 
    # Finding the order statistic
    squared.dist <- sort(squared.dist)
 
    qqchisq(squared.dist, paste(colnames(dataset)[s], collapse=", "), s.len)
 
    current <- current + 1
  }
 
  if (!is.null(filename))
    dev.off()
}
 
# Example:
A <- matrix(rnorm(2000, mean=3, sd=2), ncol=8)
qqmultinorm(A, 1, 3, "multinorm-1-3")
#qqmultinorm(A, 4, 4, "multinorm-4")
#qqmultinorm(A, 5, 5, "multinorm-5")
#qqmultinorm(A, 6, 8, "multinorm-6-8")
multinorm-1-3

multinorm-1-3

Aug 3
Splitting up a huge file on one line with sed
icon1 Mikkel Meyer Andersen | icon4 August 3, 2009 at 23:59 (UTC) | icon3 No Comments »
icon3 , ,

Sometimes when I export MySQL to files, I end up having one huge query on one line. It can be quite annoying! I’d rather want it on several lines; in that way it easier to just copy out a fragment of the query. And it also seems that some editors handle several lines better than one huge. Well, in this case my line looked like this:

INSERT INTO TABLE VALUES (...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...),(...);

With this sed-command, every entry got its own line:

sed s/\),\(/\),\\n\(/g export.sql > one-per-line.sql
Oct 4
MySQL Backup Script without mysqldump
icon1 Mikkel Meyer Andersen | icon4 October 4, 2007 at 21:47 (UTC) | icon3 No Comments »
icon3

In a long time I’ve looked for a simple backup script to backup an entire database. I’ve actually not managed to find one script not relying on a system(“mysqldump [...]“) or similar nor a script that wasn’t a part of a huge solution and hence depended on a lot of other code.

So what’s a man to do? “Steal” a bit and make the rest himself. Precisely, but I did it reversely.

But here it is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php	
/**
 * Taken partially from wp-db-backup [1] and is partially self-made [2].
 *
 * [1] uses some libraries, whereas I [2] don't. This is possibly slower and less fail
 * tolerant, but then it doesn't depent on anything other than PHP and MySQL.
 *
 * I [2] am fully aware that it could be done nice with more functions, classes
 * and so on. But this is ment to be simple and do nothing else, so I've
 * permitted myself [2] to write these lines so "messy" :-).
 *
 * [1] : http://www.ilfilosofo.com/blog/wp-db-backup/
 * [2] : Mikkel Meyer Andersen (aka. mikl-dk), http://www.scienco.org 
 */
 
/*
 * Here a MySQL-connection is made and the database chosen
 */
require('../top.php');
 
/*
 * Just a nice little function seperated from the other mess
 */
function secure_addslashes($str = '') 
{
	return str_replace('\'', '\\\'', str_replace('\\', '\\\\', $str));
} 
 
$output = '';
 
/* 
 * From [1]: ensures that the different non-printable chars are printed, e.g. newline
 */
$search = array("\x00", "\x0a", "\x0d", "\x1a");
$replace = array('\0', '\n', '\r', '\Z');
 
/*
 * All the tables from the selected database are selected
 */
$result = @mysql_query("SHOW TABLES") or exception(mysql_error());
 
while ($data = mysql_fetch_array($result))
{
	/* From [1]: Used to differ between using ' to non-integer types 
	 * and use nothing to integer types
	 */
	$result_f = @mysql_query("DESCRIBE `" . $data[0] . "`") or exception(mysql_error());
 
	$ints = array();
 
	while ($data_f = mysql_fetch_assoc($result_f))
	{
		if (
				(false !== strpos(strtolower($data_f['Type']), 'tinyint')) 		||
				(false !== strpos(strtolower($data_f['Type']), 'smallint')) 	||
				(false !== strpos(strtolower($data_f['Type']), 'mediumint')) 	||
				(false !== strpos(strtolower($data_f['Type']), 'int')) 			||
				(false !== strpos(strtolower($data_f['Type']), 'bigint')) 		||
				(false !== strpos(strtolower($data_f['Type']), 'timestamp')) 
			) 
		{
				$ints[strtolower($data_f['Field'])] = "1";
		}
	}	
 
	$result_t = @mysql_query("SHOW CREATE TABLE `" . $data[0] . "`") or exception(mysql_error());
	$data_t = mysql_fetch_array($result_t);
 
	$output .= "DROP TABLE IF EXISTS `" . $data[0] . "`;";
	$output .= "\n\n";
	$output .= $data_t[1] . " ;\n";
	$output .= "\n\n";
 
	$result_d = @mysql_query("SELECT * FROM `" . $data[0] . "`") or exception(mysql_error());
	$entries = 'INSERT INTO `' . $data[0] . '` VALUES (';
 
	while ($data_d = mysql_fetch_assoc($result_d))
	{		
		$values = array();
 
		foreach ($data_d as $key => $value) 
		{		
			if ($ints[strtolower($key)]) 
			{
				$values[] = $value;
			} 
 
			else 
			{
				$values[] = "'" . str_replace($search, $replace, secure_addslashes($value)) . "'";
			}
		}
 
		$output .= " \n" . $entries . implode(', ', $values) . ') ;';
	}
 
	$output .= "\n\n\n";
}
 
echo $output;
?>

And you can add some functionality like this in order to have a compressed file (requires zlib):

100
101
102
103
$filename = date("Y-m-d") . '.gz';
$zp = gzopen($filename, "w9");
gzwrite($zp, $output);
gzclose($zp);
Oct 4
IEnumerable versus implicit conversion
icon1 Mikkel Meyer Andersen | icon4 at 20:02 (UTC) | icon3 No Comments »
icon3

Maybe it’s really just a sick thought, but who cares?! What is actually going to happen if one is enumerating an object which implements both an enumerator-interface and an implicit conversion to a type also implementing an enumerator-interface? At this time I actually know the answer (for Mono at least), but if we should reason about it, it’s almost given that it should use the direct implementation of the enumarator-interface and in that way avoid an implicit conversion.

Now it’s time to shower this post with some code (inspired by [1]):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections;
using System.Collections.Generic;
 
namespace experiments
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			C<int> c = new C<int>(1, 2, 3, 4, 5);
 
			Console.WriteLine("IEnumerable<T>:");
			foreach (int e in c)
			{				
				Console.WriteLine(e);
			}
			Console.WriteLine();
 
			Console.WriteLine("Implicit conversion to List<T>:");			
			foreach (int e in (List<int>)c)
			{
				Console.WriteLine(e);
			}
			Console.WriteLine();
 
			Console.WriteLine("Implicit conversion to T[]:");
			foreach (int e in (int[])c)
			{
				Console.WriteLine(e);
			}
		}
	}	
 
	class C<T> : IEnumerable<T> 		
	{		
		protected List<T> list;
 
		public C(params T[] elements)
		{		
			this.list = new List<T>();
 
			for (int i = 0; i < elements.Length; ++i)
			{
				this.list.Add(elements[i]);
			}
		}
 
		public IEnumerator<T> GetEnumerator() 
		{
			for (int i = this.list.Count - 1; i >= 0; --i)
			{
				yield return this.list[i];
			}
		}
 
		IEnumerator IEnumerable.GetEnumerator() 
		{
			return GetEnumerator();
		}
 
		public static implicit operator List<T>(C<T> c)
		{
			return new List<T>(c.list);
		}		
 
		public static implicit operator T[](C<T> c)
		{
			T[] arr = new T[c.list.Count];			
 
			if (c.list.Count > 0)
			{
				int j = 0;
 
				for (int i = c.list.Count / 2; i >= 0; --i)
				{
					arr[j++] = c.list[i];
				}
 
				for (int i = (c.list.Count / 2) + 1; i < c.list.Count; ++i)
				{
					arr[j++] = c.list[i];
				}
			}
 
			return arr;
		}		
	}
}

And the result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
IEnumerable<T>:
5
4
3
2
1
 
Implicit conversion to List<T>:
1
2
3
4
5
 
Implicit conversion to T[]:
3
2
1
4
5

..gets as expected – still a interesting test (in my opinion), though.

[1]: “C# Precisely” by Peter Sestoft and Henrik I. Hansen

Oct 4

The other day I had to use a web service from a C# 2.0 application. No problem – the .NET 2.0-frameworks provides tons of functionality. To jump into the exciting issue, I’d generated the proxy classes using WSDL, and started communicating with the WS. When no errors occured everything worked fine, but if something went wrong I just got an standard non-informative SoapHeaderException with no details whatsoever. Certainly less useful than a wet newspaper.

The problem was that a SOAP-fault (this name avoids mixing up the concepts of SOAP- and run-time exceptions) occurred, but the actual description of the error in the SOAP-response was not deserialized properly and was therefore not included in the run-time exception. The SOAP-response looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<soapenv:envelope
	xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
	xmlns="http://tews6/wsdl"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://schemas.xmlsoap.org/soap/envelope/ http://schemas.xmlsoap.org/soap/envelope/">
	<soapenv:body>
		<soapenv:fault>
			<faultcode>soapenv:Client</faultcode>
			<faultstring>Error performing operation.</faultstring>
			<detail>
				<imsexception version="6.0">
					<exception>
						<name>CheckForDuplicates</name>
						<code>500</code>
						<description><!--[CDATA[User id johndoe is a duplicate. ProcessStep::BLTHValidate TabName:  ERRORLEVEL::Error]]--></description>
					</exception>
				</imsexception>
			</detail>
		</soapenv:fault>
	</soapenv:body>
</soapenv:envelope>

I read about this several places, and all these said that it was just the way .NET worked in that area. Sounds a bit odd, but nevertheless I instead started to figure out how to solve it.

The solution is actually quite simple, but it took some time to figure out how it should be done. It consists of three parts:

  • A custom Exception so that it can be caught separately
  • A SoapExtension that handled serialization and deserialization of the SOAP-messages and in that way were possible to intervene if a SOAP-fault occurred and throw the custom exception instead of a generic SoapException
  • A SoapExtensionAttribute used to mark the web methods should use the SOAP-extension

It actually works very well! The only drawback is that every web method has to be marked with the attribute – but just another opportunity to celebrate sed!

Sep 19
PDT (PHP Development Tools) in Eclipse
icon1 Mikkel Meyer Andersen | icon4 September 19, 2007 at 15:33 (UTC) | icon3 No Comments »
icon3

I’ve actually never used Eclipse before. I’ve had it installed, but never used it in a production. Not until today at least. I’ve heard a lot of good things about Eclipse, so when I  read that a new PHP-development plug-in for Eclipse was released, I simply had to try it.

Having used it for just a couple of hours I’m still impressed and smiling! I’m really surprised – well, I think it’s both because I had no expectations at all and because it’s actually a good product!

I installed it in Eclipse by performing these steps:

  1.  Install Eclipse
  2. Open it
  3. Help -> Software Updates -> Find and Install…
  4. “Search for new features…”
  5. New Remote Site with http://update.phpeclipse.net/update/nightly as URL
  6. Finish and install all the PDT-components
  7. Restart Eclipse
  8. Window -> Open Perspective -> Other
  9. Select PHP

As far as I remember that was all! Nice and easy!

Now the next step ought to be to find out whether C-development in Eclipse is as enjoying as well :-)

« Previous Entries