I'm still a novice when it comes to using pointer matrixes, but I'm finding them increasingly useful in Mata programming exercises. This posting is not so much a query but rather an alert—prompted by programming errors I've made—to others who may also be relatively new to using pointer matrixes.
The basic message can be conveyed by a simple example. Consider this code:
which results in
The reasoning is (I believe) straightforward: pointing to the matrix A is not the same thing as pointing to the (unnamed) matrix A:+0.
I suspect this behavior is well known to the pointer experts among you, but hopefully those new to pointers might learn from the errors I've made.
The basic message can be conveyed by a simple example. Consider this code:
Code:
mata pmat1=pmat2=J(2,1,NULL) A=(1,2)\(3,4) for (j=1;j<=2;j++) { A=j:*A pmat1[j]=&A pmat2[j]=&(A:+0) } *pmat1[1] *pmat1[2] *pmat2[1] *pmat2[2] pmat1 pmat2 end
Code:
: *pmat1[1] 1 2 +---------+ 1 | 2 4 | 2 | 6 8 | +---------+ : *pmat1[2] 1 2 +---------+ 1 | 2 4 | 2 | 6 8 | +---------+ : *pmat2[1] 1 2 +---------+ 1 | 1 2 | 2 | 3 4 | +---------+ : *pmat2[2] 1 2 +---------+ 1 | 2 4 | 2 | 6 8 | +---------+ : : pmat1 1 +------------------+ 1 | 0x7f8c07804aa8 | 2 | 0x7f8c07804aa8 | +------------------+ : pmat2 1 +------------------+ 1 | 0x7f8c199b9d78 | 2 | 0x7f8c0945be08 | +------------------+
I suspect this behavior is well known to the pointer experts among you, but hopefully those new to pointers might learn from the errors I've made.
Comment