aboutsummaryrefslogtreecommitdiff
path: root/system/type/pdp_matrix.c
blob: a6fc8271b49611805c264402a759af5a8c487c4c (plain)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654

#include <string.h>
#include "pdp_matrix.h"
#include "pdp_packet.h"
#include "pdp_symbol.h"
#include "pdp_internals.h" // for pdp_packet_new, which is not a proper constructor
#include "pdp_post.h"
#include "pdp_type.h"

/* the class object */
static t_pdp_class *matrix_class;



/* declare header and subheader variables and exit with errval if invalid */
#define VALID_MATRIX_HEADER(packet, header, subheader, mtype, errval)   \
t_pdp *    header    = pdp_packet_header( packet );			\
t_matrix * subheader = (t_matrix *)pdp_packet_subheader( packet );	\
if (! header ) return errval;						\
if (PDP_MATRIX != header->type) return errval;				\
if (mtype) {if (subheader->type != mtype) return errval;}


int pdp_packet_matrix_isvalid(int p)
{
    VALID_MATRIX_HEADER(p, h, m, 0, 0);
    return 1;
}


void *pdp_packet_matrix_get_gsl_matrix(int p, u32 type)
{
    VALID_MATRIX_HEADER(p, h, m, type, 0);
    return &m->matrix;
}

void *pdp_packet_matrix_get_gsl_vector(int p, u32 type)
{
    VALID_MATRIX_HEADER(p, h, m, type, 0);
    return &m->vector;
}

int pdp_packet_matrix_get_type(int p)
{
    VALID_MATRIX_HEADER(p, h, m, 0, 0);
    return m->type;
}

int pdp_packet_matrix_isvector(int p)
{
    VALID_MATRIX_HEADER(p, h, m, 0, 0);
    return ((m->rows == 1) || (m->columns == 1));
}

int pdp_packet_matrix_ismatrix(int p)
{
    VALID_MATRIX_HEADER(p, h, m, 0, 0);
    return ((m->rows != 1) && (m->columns != 1));
}




/* gsl blas matrix/vector multiplication:

vector.vector

 (const gsl_vector * x, const gsl_vector * y, double * result) 

gsl_blas_sdot
gsl_blas_ddot
gsl_blas_cdot
gsl_blas_zdot
gsl_blas_cdotu
gsl_blas_zdotu

matrix.vector
 (
   CBLAS_TRANSPOSE_t 
   TransA, 
   double alpha, 
   const gsl_matrix * A, 
   const gsl_vector * x, 
   double beta, 
   gsl_vector * y
 )

gsl_blas_sgemv
gsl_blas_dgemv
gsl_blas_cgemv
gsl_blas_zgemv

matrix.matrix
 (
   CBLAS_TRANSPOSE_t TransA, 
   CBLAS_TRANSPOSE_t TransB, 
   double alpha, 
   const gsl_matrix * A, 
   const gsl_matrix * B, 
   double beta, 
   gsl_matrix * C
 )

gsl_blas_sgemm
gsl_blas_dgemm
gsl_blas_cgemm
gsl_blas_zgemm

*/

/* compute the matrix inverse using the LU decomposition */
/* it only works for double real/complex */
int pdp_packet_matrix_LU_to_inverse(int p)
{
    int new_p;
    u32 n;
    int type = pdp_packet_matrix_get_type(p);
    t_matrix *sheader = (t_matrix *)pdp_packet_subheader(p);
    gsl_matrix *m = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(p, type);
    gsl_matrix *im;
    if (!m) return -1;
    n = m->gsl_rows; 
    if (n != m->gsl_columns) return -1;
    new_p = pdp_packet_new_matrix(n, n, type);
    if (-1 == new_p) return -1;
    im = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(new_p, type);

    switch(type){
    case PDP_MATRIX_TYPE_RDOUBLE:
	gsl_linalg_LU_invert (m, &sheader->perm, im); break;
    case PDP_MATRIX_TYPE_CDOUBLE:
	gsl_linalg_complex_LU_invert ((gsl_matrix_complex *)m, &sheader->perm, (gsl_matrix_complex *)im); break;
    default:
	pdp_packet_mark_unused(new_p);
	new_p = -1;
    }
    return new_p;
}
/* compute the LU decomposition of a square matrix */
/* it only works for double real/complex */
int pdp_packet_matrix_LU(int p)
{
    int p_LU, bytes;
    u32 n;
    int type = pdp_packet_matrix_get_type(p);
    t_matrix *sh_m_LU;
    t_matrix *sh_m = (t_matrix *)pdp_packet_subheader(p);
    gsl_matrix *m = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(p, type);
    gsl_matrix *m_LU;
    if (!m) return -1;
    n = m->gsl_rows; 
    if (n != m->gsl_columns) return -1;
    p_LU = pdp_packet_new_matrix(n, n, type);
    if (-1 == p_LU) return -1;
    sh_m_LU  = (t_matrix *)pdp_packet_subheader(p_LU);
    m_LU = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(p_LU, type);
    /* copy matrix data: move this to copy method */
    memcpy(pdp_packet_data(p_LU), pdp_packet_data(p), sh_m->block.size); 

    switch(type){
    case PDP_MATRIX_TYPE_RDOUBLE:
	gsl_linalg_LU_decomp (m_LU, &sh_m_LU->perm, &sh_m_LU->signum); break; 
    case PDP_MATRIX_TYPE_CDOUBLE:
	gsl_linalg_complex_LU_decomp ((gsl_matrix_complex *)m_LU, &sh_m_LU->perm, &sh_m_LU->signum); break; 
    default:
	pdp_packet_mark_unused(p_LU);
	p_LU = -1;
    }
    return p_LU;
}

int pdp_packet_matrix_LU_solve(int p_matrix, int p_vector)
{
    int type = pdp_packet_matrix_get_type(p_matrix);
    int p_result_vector = pdp_packet_clone_rw(p_vector);
    gsl_matrix *m = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(p_matrix, type);
    gsl_vector *v = (gsl_vector *)pdp_packet_matrix_get_gsl_vector(p_vector, type);
    gsl_vector *rv = (gsl_vector *)pdp_packet_matrix_get_gsl_vector(p_result_vector, type);
    t_matrix *sh_m = (t_matrix *)pdp_packet_subheader(p_matrix);

    if (!(m && v && rv)) goto error;

    switch(type){
    case PDP_MATRIX_TYPE_RDOUBLE:
	if (gsl_linalg_LU_solve (m, &sh_m->perm, v, rv)) goto error; break;
    case PDP_MATRIX_TYPE_CDOUBLE:
	if(gsl_linalg_complex_LU_solve ((gsl_matrix_complex*)m, &sh_m->perm, 
		(gsl_vector_complex *)v, (gsl_vector_complex *)rv)) goto error; break;
    default:
	goto error;
    }
    return p_result_vector;

 error:
    pdp_packet_mark_unused(p_result_vector);
    //post("error");
    return -1;
}

/* matrix matrix mul: C is defining type 
   returns 0 on success */
int pdp_packet_matrix_blas_mm
(
 CBLAS_TRANSPOSE_t TransA,
 CBLAS_TRANSPOSE_t TransB,
 int pA,
 int pB,
 int pC,
 float scale_r,
 float scale_i
)
{
    gsl_complex_float cf_scale = {{scale_r, scale_i}};
    gsl_complex cd_scale = {{(double)scale_r, (double)scale_i}};
    gsl_complex_float cf_one = {{1.0f, 0.0f}};
    gsl_complex cd_one = {{1.0, 0.0}};
    gsl_matrix *mA, *mB, *mC;
    int type;
    type = pdp_packet_matrix_get_type(pC);
    mA = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(pA,type);
    mB = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(pB,type);
    mC = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(pC,type);

    if (!(mA && mB)) return 1;


    switch(type){
    case PDP_MATRIX_TYPE_RFLOAT:
	return gsl_blas_sgemm(TransA, TransB, scale_r, (gsl_matrix_float *)mA, 
			      (gsl_matrix_float *)mB, 1.0f, (gsl_matrix_float *)mC); 
    case PDP_MATRIX_TYPE_RDOUBLE:
	return gsl_blas_dgemm(TransA, TransB, (double)scale_r, (gsl_matrix *)mA, 
			      (gsl_matrix *)mB, 1.0, (gsl_matrix *)mC);
    case PDP_MATRIX_TYPE_CFLOAT:
	return gsl_blas_cgemm(TransA, TransB, cf_scale, (gsl_matrix_complex_float *)mA, 
			      (gsl_matrix_complex_float *)mB, cf_one, (gsl_matrix_complex_float *)mC); 
    case PDP_MATRIX_TYPE_CDOUBLE:
	return gsl_blas_zgemm(TransA, TransB, cd_scale, (gsl_matrix_complex *)mA, 
			      (gsl_matrix_complex *)mB, cd_one, (gsl_matrix_complex *)mC); 
    default:
	return 0;
    }
}

/* matrix vector mul: C is defining type 
   returns 0 on success */
int pdp_packet_matrix_blas_mv
(
 CBLAS_TRANSPOSE_t TransA,
 int pA,
 int pb,
 int pc,
 float scale_r,
 float scale_i
)
{
    gsl_complex_float cf_scale = {{scale_r, scale_i}};
    gsl_complex cd_scale = {{(double)scale_r, (double)scale_i}};
    gsl_complex_float cf_one = {{1.0f, 0.0f}};
    gsl_complex cd_one = {{1.0, 0.0}};
    gsl_matrix *mA;
    gsl_vector *vb, *vc;
    int type;
    type = pdp_packet_matrix_get_type(pA);
    mA = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(pA,type);
    vb = (gsl_vector *)pdp_packet_matrix_get_gsl_vector(pb,type);
    vc = (gsl_vector *)pdp_packet_matrix_get_gsl_vector(pc,type);

    if (!(vb && vc)) return 1;


    switch(type){
    case PDP_MATRIX_TYPE_RFLOAT:
	return gsl_blas_sgemv(TransA, scale_r, (gsl_matrix_float *)mA, 
			      (gsl_vector_float *)vb, 1.0f, (gsl_vector_float *)vc); 
    case PDP_MATRIX_TYPE_RDOUBLE:
	return gsl_blas_dgemv(TransA, (double)scale_r, (gsl_matrix *)mA, 
			      (gsl_vector *)vb, 1.0, (gsl_vector *)vc);
    case PDP_MATRIX_TYPE_CFLOAT:
	return gsl_blas_cgemv(TransA, cf_scale, (gsl_matrix_complex_float *)mA, 
			      (gsl_vector_complex_float *)vb, cf_one, (gsl_vector_complex_float *)vc); 
    case PDP_MATRIX_TYPE_CDOUBLE:
	return gsl_blas_zgemv(TransA, cd_scale, (gsl_matrix_complex *)mA, 
			      (gsl_vector_complex *)vb, cd_one, (gsl_vector_complex *)vc); 
    default:
	return 0;
    }
}



t_pdp_symbol *_pdp_matrix_get_description(t_pdp *header)
{
    t_matrix *m = (t_matrix *)(&header->info.raw);
    char description[100];
    char *c = description;
    int encoding;

    if (!header) return pdp_gensym("invalid");
    else if (!header->desc){
	/* if description is not defined, try to reconstruct it (for backwards compat) */
	if (header->type == PDP_MATRIX){

	    c += sprintf(c, "matrix");

	    switch(m->type){
	    case PDP_MATRIX_TYPE_RFLOAT: c += sprintf(c, "/float/real"); break;
	    case PDP_MATRIX_TYPE_CFLOAT: c += sprintf(c, "/float/complex"); break;
	    case PDP_MATRIX_TYPE_RDOUBLE: c += sprintf(c, "/double/real"); break;
	    case PDP_MATRIX_TYPE_CDOUBLE: c += sprintf(c, "/double/complex"); break;
	    default:
		c += sprintf(c, "/unknown"); goto exit;
	    }

	    c += sprintf(c, "/%dx%d", (int)m->rows, (int)m->columns);
	    
	exit:
	    return pdp_gensym(description);
	} 
	else return pdp_gensym("unknown");
    }
    else return header->desc;
}



static void _pdp_matrix_copy(t_pdp *dst, t_pdp *src);
static void _pdp_matrix_clone(t_pdp *dst, t_pdp *src);
static void _pdp_matrix_reinit(t_pdp *dst);
static void _pdp_matrix_cleanup(t_pdp *dst);

static size_t _pdp_matrix_mdata_byte_size(u32 rows, u32 columns, u32 type)
{
    size_t dsize = rows * columns;
    switch (type){
    case PDP_MATRIX_TYPE_RFLOAT:   dsize *= sizeof(float);    break;
    case PDP_MATRIX_TYPE_CFLOAT:   dsize *= 2*sizeof(float);  break;
    case PDP_MATRIX_TYPE_RDOUBLE:  dsize *= sizeof(double);   break;
    case PDP_MATRIX_TYPE_CDOUBLE:  dsize *= 2*sizeof(double); break;
    default: dsize = 0;
    }
    return dsize;
}


static size_t _pdp_matrix_pdata_vector_size(u32 rows, u32 columns)
{
    return (rows>columns ? rows : columns);
}


static void _pdp_matrix_init(t_pdp *dst, u32 rows, u32 columns, u32 type)
{
    int i;
    t_matrix *m = (t_matrix *)(&dst->info.raw);
    void *d = ((void *)dst) + PDP_HEADER_SIZE;
    int matrix_bytesize = _pdp_matrix_mdata_byte_size(rows, columns, type);
    int permsize =  _pdp_matrix_pdata_vector_size(rows, columns);

    /* set meta data */
    m->type = type;
    m->rows = rows;
    m->columns = columns;

    /* set the block data */
    m->block.size = matrix_bytesize;
    m->block.data = (double *)d;

    /* set the vector view */
    m->vector.size = (1==columns) ? rows : columns;
    m->vector.stride = 1;
    m->vector.data = (double *)d;
    m->vector.block = &m->block;
    m->vector.owner = 0;

    /* set the matrix view */
    m->matrix.gsl_rows = rows;
    m->matrix.gsl_columns = columns;
    m->matrix.tda = columns;
    m->matrix.data = (double *)d;
    m->matrix.block = &m->block;
    m->matrix.owner = 0;

    /* set the permutation object & init */
    m->perm.size = permsize;
    m->perm.data = (size_t *)(d + matrix_bytesize);
    for(i=0; i<permsize; i++) m->perm.data[i] = i;
    m->signum = -1;
    
    /* init packet header */
    dst->theclass = matrix_class;
    dst->desc = _pdp_matrix_get_description(dst);

}

static void _pdp_matrix_clone(t_pdp *dst, t_pdp *src)
{
    t_matrix *m = (t_matrix *)(&src->info.raw);
    _pdp_matrix_init(dst, m->rows, m->columns, m->type);

}
static void _pdp_matrix_copy(t_pdp *dst, t_pdp *src)
{
    _pdp_matrix_clone(dst, src);
    memcpy(dst + PDP_HEADER_SIZE, src + PDP_HEADER_SIZE, src->size - PDP_HEADER_SIZE);
    //post("matrix copy successful");
}
static void _pdp_matrix_reinit(t_pdp *dst)
{
    /* nothing to do, assuming data is correct */
}
static void _pdp_matrix_cleanup(t_pdp *dst)
{
    /* no extra memory to free */
}

int pdp_packet_new_matrix(u32 rows, u32 columns, u32 type)
{
    t_pdp *header;
    int dsize, p;

    /* compute the blocksize for the matrix data */
    /* if 0, something went wrong -> return invalid packet */
    if (!(dsize = _pdp_matrix_mdata_byte_size(rows, columns, type))) return -1;
    dsize += sizeof(size_t) * _pdp_matrix_pdata_vector_size(rows, columns);

    p = pdp_packet_new(PDP_MATRIX, dsize);
    if (-1 == p) return -1;
    header = pdp_packet_header(p);
    
    _pdp_matrix_init(header, rows, columns, type);

    return p;
}

int pdp_packet_new_matrix_product_result(CBLAS_TRANSPOSE_t TransA, CBLAS_TRANSPOSE_t TransB, int pA, int pB)
{
    gsl_matrix *mA, *mB;
    u32 type = pdp_packet_matrix_get_type(pA);

    u32 colA, colB, rowA, rowB;
   
    /* check if A is a matrix */
    if (!type) return -1;

    mA = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(pA, type);
    mB = (gsl_matrix *)pdp_packet_matrix_get_gsl_matrix(pB, type);

    /* check if A and B are same type */
    if (!mB) return -1;

    /* get dims A */
    if (TransA == CblasNoTrans){
	rowA = mA->gsl_rows;
	colA = mA->gsl_columns;
    }
    else {
	rowA = mA->gsl_columns;
	colA = mA->gsl_rows;
    }

    /* get dims B */
    if (TransB == CblasNoTrans){
	rowB = mB->gsl_rows;
	colB = mB->gsl_columns;
    }
    else {
	rowB = mB->gsl_columns;
	colB = mB->gsl_rows;
    }

    /* check if sizes are compatible */
    if (colA != rowB) return -1;

    /* create new packet */
    return pdp_packet_new_matrix(rowA, colB, type);
}

void pdp_packet_matrix_setzero(int p)
{
    t_matrix *m;
    if (!pdp_packet_matrix_isvalid(p)) return;
    m = (t_matrix *) pdp_packet_subheader(p);
    memset(m->block.data, 0, m->block.size);
}



/* type conversion programs */
static int _pdp_packet_convert_matrix_to_greyimage(int packet, t_pdp_symbol *dest_template)
{
    t_pdp *header = pdp_packet_header(packet);
    t_matrix *matrix = (t_matrix *)pdp_packet_subheader(packet);
    void *data = pdp_packet_data(packet);
    s16 *new_data;
    u32 c,r, nbelements;
    int new_p;
    u32 i;

    c = matrix->columns;
    r = matrix->rows;
    nbelements = c*r;

    new_p = pdp_packet_new_image_grey(c,r);
    if (-1 == new_p) return -1;
    new_data = (s16 *)pdp_packet_data(new_p);

    /* convert first channel */
    switch(matrix->type){
    case PDP_MATRIX_TYPE_RFLOAT:
	for (i=0; i<nbelements; i++) (new_data)[i] = (s16)(((float *)data)[i] * (float)0x8000); break;
    case PDP_MATRIX_TYPE_CFLOAT: //only copy real channel
	for (i=0; i<nbelements; i++) (new_data)[i] = (s16)(((float *)data)[i<<1] * (float)0x8000); break;
    case PDP_MATRIX_TYPE_RDOUBLE:
	for (i=0; i<nbelements; i++) (new_data)[i] = (s16)(((double *)data)[i] * (float)0x8000); break;
    case PDP_MATRIX_TYPE_CDOUBLE: //only copy real channel
	for (i=0; i<nbelements; i++) (new_data)[i] = (s16)(((double *)data)[i<<1] * (float)0x8000); break;
    default:
	pdp_packet_mark_unused(new_p);
	new_p = -1;
    }	
    return new_p;
}

static int _pdp_packet_convert_image_to_matrix(int packet, t_pdp_symbol *dest_template, int type)
{
    t_pdp *header = pdp_packet_header(packet);
    t_image *image = pdp_packet_image_info(packet);
    s16 *data = (s16 *)pdp_packet_data(packet);
    void *new_data;
    u32 w,h, nbelements;
    int new_p;
    int encoding = image->encoding;
    u32 i;

    if (!pdp_packet_image_isvalid(packet)) return -1;
    w = image->width;
    h = image->height;
    nbelements = w*h;

    new_p = pdp_packet_new_matrix(h,w, type);
    if (-1 == new_p) return -1;
    new_data = pdp_packet_data(new_p);

    switch (encoding){
    case PDP_IMAGE_YV12:
    case PDP_IMAGE_GREY:
    case PDP_IMAGE_MCHP:
	/* convert first channel */
	switch(type){
	case PDP_MATRIX_TYPE_RFLOAT:
	    for (i=0; i<nbelements; i++)
		((float *)new_data)[i] = ((float)data[i]) * (1.0f / (float)0x8000); break;
	case PDP_MATRIX_TYPE_RDOUBLE:
	    for (i=0; i<nbelements; i++)
		((double *)new_data)[i] = ((double)data[i]) * (1.0f / (double)0x8000); break;
	case PDP_MATRIX_TYPE_CFLOAT:
	    for (i=0; i<nbelements; i++){
		((float *)new_data)[i*2] = ((float)data[i]) * (1.0f / (float)0x8000); 
		((float *)new_data)[i*2+1] = 0.0f;
	    }
	    break;
	case PDP_MATRIX_TYPE_CDOUBLE:
	    for (i=0; i<nbelements; i++){
		((double *)new_data)[i*2] = ((double)data[i]) * (1.0f / (double)0x8000); 
		((double *)new_data)[i*2+1] = 0.0;
	    }
	    break;
	default:
	    pdp_post("_pdp_packet_convert_image_to_matrix: INTERNAL ERROR");
	}
	break;
    default:
	pdp_packet_mark_unused(new_p);
	new_p = -1;
	break;
    }

    return new_p;

}

static int _pdp_packet_convert_image_to_floatmatrix(int packet, t_pdp_symbol *dest_template){
    return _pdp_packet_convert_image_to_matrix(packet, dest_template, PDP_MATRIX_TYPE_RFLOAT);}
static int _pdp_packet_convert_image_to_doublematrix(int packet, t_pdp_symbol *dest_template){
    return _pdp_packet_convert_image_to_matrix(packet, dest_template, PDP_MATRIX_TYPE_RDOUBLE);}
static int _pdp_packet_convert_image_to_complexfloatmatrix(int packet, t_pdp_symbol *dest_template){
    return _pdp_packet_convert_image_to_matrix(packet, dest_template, PDP_MATRIX_TYPE_CFLOAT);}
static int _pdp_packet_convert_image_to_complexdoublematrix(int packet, t_pdp_symbol *dest_template){
    return _pdp_packet_convert_image_to_matrix(packet, dest_template, PDP_MATRIX_TYPE_CDOUBLE);}


static int _pdp_packet_matrix_convert_fallback(int packet, t_pdp_symbol *dest_template)
{
    pdp_post("can't convert image type %s to %s",
	 pdp_packet_get_description(packet)->s_name, dest_template->s_name);

    return -1;
}


/* this seems like a nice spot to place a dummy gsl signal handler */
static void _gsl_error_handler (const char * reason, 
              const char * file, 
              int line, 
              int gsl_errno)
{
    //pdp_post("gsl error:\nREASON: %s\nFILE:%s\nLINE:%d\nERRNO:%d", reason, file, line, gsl_errno);
}

static int pdp_matrix_factory(t_pdp_symbol *type)
{
    return -1;
}
void pdp_matrix_setup(void)
{
    t_pdp_conversion_program *program;


    /* setup the class */
    matrix_class = pdp_class_new(pdp_gensym("matrix/*/*/*"), pdp_matrix_factory);
    matrix_class->copy = _pdp_matrix_copy;
    //matrix_class->clone = _pdp_matrix_clone; // is now solved through (symbol based) constructor
    matrix_class->wakeup = _pdp_matrix_reinit;
    matrix_class->cleanup = _pdp_matrix_cleanup;
    

    /* image -> matrix: default = double/real (most ops available) */
    program = pdp_conversion_program_new(_pdp_packet_convert_image_to_doublematrix, 0);
    pdp_type_register_conversion(pdp_gensym("image/*/*"), pdp_gensym("matrix/double/real/*"), program);
    program = pdp_conversion_program_new(_pdp_packet_convert_image_to_floatmatrix, 0);
    pdp_type_register_conversion(pdp_gensym("image/*/*"), pdp_gensym("matrix/float/real/*"), program);
    program = pdp_conversion_program_new(_pdp_packet_convert_image_to_complexdoublematrix, 0);
    pdp_type_register_conversion(pdp_gensym("image/*/*"), pdp_gensym("matrix/double/complex/*"), program);
    program = pdp_conversion_program_new(_pdp_packet_convert_image_to_complexfloatmatrix, 0);
    pdp_type_register_conversion(pdp_gensym("image/*/*"), pdp_gensym("matrix/float/complex/*"), program);

    /* matrix -> image */
    program = pdp_conversion_program_new(_pdp_packet_convert_matrix_to_greyimage, 0);
    pdp_type_register_conversion(pdp_gensym("matrix/*/*/*"), pdp_gensym("image/grey/*"), program);

    /* fallbacks */
    program = pdp_conversion_program_new(_pdp_packet_matrix_convert_fallback, 0);
    pdp_type_register_conversion(pdp_gensym("matrix/*/*/*"), pdp_gensym("image/*/*"), program);
    pdp_type_register_conversion(pdp_gensym("matrix/*/*/*"), pdp_gensym("bitmap/*/*"), program);
    pdp_type_register_conversion(pdp_gensym("image/*/*"), pdp_gensym("matrix/*/*/*/*"), program);
    pdp_type_register_conversion(pdp_gensym("bitmap/*/*"), pdp_gensym("matrix/*/*/*/*"), program);

    /* setup gsl handler */
    if(gsl_set_error_handler(_gsl_error_handler)){
	pdp_post("pdp_matrix_setup: WARNING: overriding gsl error handler.");
    }    

}