Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Mata: Recursive structures and pointers


From   Ulrich Kohler <kohler@wz-berlin.de>
To   statalist@hsphsun2.harvard.edu
Subject   st: Mata: Recursive structures and pointers
Date   Thu, 18 May 2006 12:36:38 +0200

hi all,

we have a rather weird problem with recursive Mata structures and 
pointers, which is even somwhat beyond the limits of what we can express in 
English. Allow us to try it anyway.

The short version of our question is, whether there is a better way 
to implement the "setpoint" function described below? We don't have any
experience with pointers in Mata, and our attempts so far might be a 
complet dead end. Is so, just stop us from considering this any further. We 
don't expect that you write the code for us.

The long version is as follows: Suppose we have defined the following 
data structure:

------------------------------------------------xyz.mata---
...
struct listelem {
	real scalar a
	real scalar b
	pointer(struct listelem scalar) scalar nextpoint
}

struct listelem scalar function setelem(real scalar s) {
	struct listelem scalar e
	e.a=s
	e.b=s
	return e;
}

...
-----------------------------------------------------------

This defines the structure "listelem", which implements an element of a 
"linked list" of points (a,b) and a cunstructor-function "setelem".

Now suppose that we have an argument vector defined as -seq = 1, 2, 3, 4-, and 
we want to define the structure "pointlist", which is a linked list of 
"listelem". More specifically, the structure "pointlist" should look as 
follows:

the first  element: [(1,1) pointer to the second element]
the second element: [(2,2) pointer to the third element]
the third element:  [(3,3) pointer to the fourth element]
the fourth element: [(4,4) null pointer ]

In order to circumvent our English language problem, here is the complete Java 
code of what we want to achieve in Mata (you may skip this, if you don't 
understand Java):

------------------------------------------java code-----------
public class listelem {
	
	public int a;
	public int b;
	public listelem nextpoint;
	
	public void setelem(int s){
		a=s;
		b=s;
	}
}


public class pointlist {
	
	public listelem first;		//the first element of the list
	public listelem last;		//the last element of the list
	
	
//initialisation of the first element
	pointlist(int k){
		first = new listelem();
		first.a = k;
		first.b = k;
		last = first;
	}
	

	public static pointlist setpoint(int[] seq){
		
		pointlist v;		//return linked list
		int length;			//length of the argument vector seq
		
		v = new pointlist(seq[0]);
		length = seq.length;
		
//filling out of the list
		for(int i=1; i<length; i++){
			listelem newelem = new listelem();
			newelem.setelem(seq[i]);
			
			v.last.nextpoint = newelem;
			v.last = v.last.nextpoint;
		}
		return v;
	}
-----------------------------------------------------------

Now let us turn to our attempts to implement this in Mata. 
Basically we have defined the structure "pointlist" that implements a "linked 
list" of points (a,b) and a function "setpoint" that fills out this list with 
the values of the argument vector "seq". Below is how far we got. We have 
indicated the line that causes trouble with a comment.

------------------------------------------------xyz.mata---
...
struct pointlist {
	struct listelem scalar first
	pointer(struct listelem scalar) scalar last
}


struct pointlist scalar function setpoint( real vector seq) {

	struct pointlist scalar v
	real scalar i
	real scalar length
	
		length = length(seq)
		v.first.a = seq[1]
		v.first.b = seq[1]
		v.last = &v.first
		liststruct(v)

		
	for (i=2; i<=length; i++) {
		
		struct listelem m
		m= setelem(seq[i])       
		v.last.nextpoint= m        // <- See error message below
		v.last = &last.nextpoint
	}
	return(v)
}
...
-----------------------------------------------------------

Compiling this code we receive the following error message at the indicated 
line:

------------------------------------------------------------
type mismatch:  exp.exp:  pointer found where struct expected
-------------------------------------------------------------

We somehow stick with this error message. Our main problem, however, concerns 
the using of pointers. The pointer "last" should move along the list and 
always point to the last element of the list (it means, it should allow to 
set a new point at the end of the list). It does not work. How can we add a 
new element at the end of the list?

Many regards

Uli and Magdalena

-- 
kohler@wz-berlin.de, luniak@wz-berlin.de
+49 (030) 25491-361
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index