The R Project SVN R

Rev

Rev 1820 | Rev 1858 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1820 Rev 1839
Line 19... Line 19...
19
 
19
 
20
#include "Defn.h"
20
#include "Defn.h"
21
#include "Mathlib.h"
21
#include "Mathlib.h"
22
 
22
 
23
 
23
 
-
 
24
/* "GetRowNames" and "GetColNames" are utility routines which */
-
 
25
/* locate and return the row names and column names from the */
-
 
26
/* dimnames attribute of a matrix.  They are useful because */
-
 
27
/* old versions of R used pair-based lists for dimnames */
-
 
28
/* whereas recent versions use vector bassed lists */
-
 
29
 
-
 
30
SEXP GetRowNames(SEXP dimnames)
-
 
31
{
-
 
32
    if (TYPEOF(dimnames) == VECSXP)
-
 
33
	return VECTOR(dimnames)[0];
-
 
34
    else if (TYPEOF(dimnames) == LISTSXP)
-
 
35
	return CAR(dimnames);
-
 
36
    else
-
 
37
	return R_NilValue;	     
-
 
38
}
-
 
39
 
-
 
40
SEXP GetColNames(SEXP dimnames)
-
 
41
{
-
 
42
    if (TYPEOF(dimnames) == VECSXP)
-
 
43
	return VECTOR(dimnames)[1];
-
 
44
    else if (TYPEOF(dimnames) == LISTSXP)
-
 
45
	return CADR(dimnames);
-
 
46
    else
-
 
47
	return R_NilValue;	     
-
 
48
}
-
 
49
 
24
SEXP do_matrix(SEXP call, SEXP op, SEXP args, SEXP rho)
50
SEXP do_matrix(SEXP call, SEXP op, SEXP args, SEXP rho)
25
{
51
{
26
    SEXP vals, snr, snc;
52
    SEXP vals, snr, snc;
27
    int nr, nc, byrow, lendat;
53
    int nr, nc, byrow, lendat;
28
 
54
 
Line 31... Line 57...
31
    snr = CADR(args);
57
    snr = CADR(args);
32
    snc = CADDR(args);
58
    snc = CADDR(args);
33
    byrow = asInteger(CADR(CDDR(args)));
59
    byrow = asInteger(CADR(CDDR(args)));
34
 
60
 
35
    if (isVector(vals) || isList(vals)) {
61
    if (isVector(vals) || isList(vals)) {
36
	if(length(vals) < 0)
62
	if (length(vals) < 0)
37
	    errorcall(call, "argument has length zero\n");
63
	    errorcall(call, "argument has length zero\n");
38
    } else errorcall(call, "invalid matrix element type\n");
64
    } else errorcall(call, "invalid matrix element type\n");
39
 
65
 
40
    if (!isNumeric(snr) || !isNumeric(snc))
66
    if (!isNumeric(snr) || !isNumeric(snc))
41
	error("non-numeric matrix extent\n");
67
	error("non-numeric matrix extent\n");
42
 
68
 
43
    lendat = length(vals);
69
    lendat = length(vals);
44
    nr = asInteger(snr);
70
    nr = asInteger(snr);
45
    nc = asInteger(snc);
71
    nc = asInteger(snc);
46
 
72
 
47
    if( lendat > 1 && (nr*nc) % lendat != 0 ) {
73
    if (lendat > 1 && (nr * nc) % lendat != 0) {
48
	if( ((lendat>nr) && (lendat/nr)*nr != lendat ) ||
74
	if (((lendat > nr) && (lendat / nr) * nr != lendat) ||
49
	    ((lendat< nr) && (nr/lendat) * lendat != nr ))
75
	    ((lendat < nr) && (nr / lendat) * lendat != nr))
50
	    warning("Replacement length not a multiple of the elements to replace in matrix(...) \n");
76
	    warning("Replacement length not a multiple of the elements to replace in matrix(...) \n");
51
	else if( ((lendat>nc) && (lendat/nc)*nc != lendat ) ||
77
	else if (((lendat > nc) && (lendat / nc) * nc != lendat) ||
52
		 ((lendat< nc) && (nc/lendat) * lendat != nc ))
78
		 ((lendat < nc) && (nc / lendat) * lendat != nc))
53
	    warning("Replacement length not a multiple of the elements to replace in matrix(...) \n");
79
	    warning("Replacement length not a multiple of the elements to replace in matrix(...) \n");
54
    } 
80
    } 
55
	else if ((lendat>1) && (nr*nc==0)){
81
	else if ((lendat > 1) && (nr * nc == 0)){
56
	  warning("Replacement length not a multiple of the elements to replace in matrix(...) \n");
82
	  warning("Replacement length not a multiple of the elements to replace in matrix(...) \n");
57
	}
83
	}
58
	else if (lendat ==0 && nr*nc>0){
84
	else if (lendat == 0 && nr * nc > 0){
59
	  error("No data to replace in matrix(...)\n");
85
	  error("No data to replace in matrix(...)\n");
60
	}
86
	}
61
 
87
 
62
    PROTECT(snr = allocMatrix(TYPEOF(vals), nr, nc));
88
    PROTECT(snr = allocMatrix(TYPEOF(vals), nr, nc));
63
    if(isVector(vals))
89
    if (isVector(vals))
64
	copyMatrix(snr, vals, byrow);
90
	copyMatrix(snr, vals, byrow);
65
    else
91
    else
66
	copyListMatrix(snr, vals, byrow);
92
	copyListMatrix(snr, vals, byrow);
67
    UNPROTECT(1);
93
    UNPROTECT(1);
68
    return snr;
94
    return snr;
Line 109... Line 135...
109
/* attribute.  Note that this function mutates x. */
135
/* attribute.  Note that this function mutates x. */
110
/* Duplication should occur before this is called. */
136
/* Duplication should occur before this is called. */
111
 
137
 
112
SEXP DropDims(SEXP x)
138
SEXP DropDims(SEXP x)
113
{
139
{
114
    SEXP p, q, r, dims, dimnames;
140
    SEXP p, q, dims, dimnames;
115
    int i, n;
141
    int i, n, ndims;
116
 
142
 
117
    PROTECT(x);
143
    PROTECT(x);
118
 
-
 
119
    dims = getAttrib(x, R_DimSymbol);
144
    dims = getAttrib(x, R_DimSymbol);
120
    dimnames = getAttrib(x, R_DimNamesSymbol);
145
    dimnames = getAttrib(x, R_DimNamesSymbol);
121
 
146
 
122
    /* Check that dropping will actually do something. */
147
    /* Check that dropping will actually do something. */
123
    /* (1) Check that there is a "dim" attribute. */
148
    /* (1) Check that there is a "dim" attribute. */
124
 
149
 
125
    if(dims == R_NilValue) {
150
    if (dims == R_NilValue) {
126
	UNPROTECT(1);
151
	UNPROTECT(1);
127
	return x;
152
	return x;
128
    }
153
    }
-
 
154
    ndims = LENGTH(dims);
129
 
155
 
130
    /* (2) Check that there are redundant extents */
156
    /* (2) Check whether there are redundant extents */
131
    n = 0;
157
    n = 0;
132
    for(i = 0 ; i < LENGTH(dims) ; i++)
158
    for (i = 0; i < ndims; i++)
133
	if(INTEGER(dims)[i] != 1) n++;
159
	if (INTEGER(dims)[i] != 1) n++;
134
    if(n == LENGTH(dims)) {
160
    if (n == ndims) {
135
	UNPROTECT(1);
161
	UNPROTECT(1);
136
	return x;
162
	return x;
137
    }
163
    }
138
 
164
 
139
    if(n <= 1) {  /* vector */
165
    if (n <= 1) {
-
 
166
	/* We have reduced to a vector result. */
140
	SEXP newnames = R_NilValue;
167
	SEXP newnames = R_NilValue;
141
	if (dimnames != R_NilValue) {
168
	if (dimnames != R_NilValue) {
142
	    n = length(dims);
169
	    n = length(dims);
143
#ifdef NEWLIST
170
	    if (TYPEOF(dimnames) == VECSXP) {
144
	    for(i = 0 ; i < n ; i++) {
171
		for (i = 0; i < n; i++) {
145
		if(INTEGER(dims)[i] != 1) {
172
		    if (INTEGER(dims)[i] != 1) {
146
		    newnames = VECTOR(dimnames)[i];
173
			newnames = VECTOR(dimnames)[i];
147
		    break;
174
			break;
-
 
175
		    }
148
		}
176
		}
149
	    }
177
	    }
150
#else
178
	    else {
151
	    q = dimnames;
179
		q = dimnames;
152
	    for(i = 0 ; i < n ; i++) {
180
		for (i = 0; i < n; i++) {
153
		if(INTEGER(dims)[i] != 1) {
181
		    if (INTEGER(dims)[i] != 1) {
154
		    newnames = CAR(q);
182
			newnames = CAR(q);
155
		    break;
183
			break;
-
 
184
		    }
-
 
185
		    q = CDR(q);
156
		}
186
		}
157
		q = CDR(q);
-
 
158
	    }
187
	    }
159
#endif
-
 
160
	}
188
	}
161
	PROTECT(newnames);
189
	PROTECT(newnames);
162
	setAttrib(x, R_DimNamesSymbol, R_NilValue);
190
	setAttrib(x, R_DimNamesSymbol, R_NilValue);
163
	setAttrib(x, R_DimSymbol, R_NilValue);
191
	setAttrib(x, R_DimSymbol, R_NilValue);
164
	setAttrib(x, R_NamesSymbol, newnames);
192
	setAttrib(x, R_NamesSymbol, newnames);
165
	UNPROTECT(1);
193
	UNPROTECT(1);
166
    }
194
    }
-
 
195
    else {
167
    else {        /* array */
196
	/* We have a lower dimensional array. */
168
	SEXP newdims, newdimnames;
197
	SEXP newdims, newdimnames;
169
	PROTECT(newdims = allocVector(INTSXP, n));
198
	int j = 0;
170
	n = 0;
199
	n = 0;
-
 
200
	PROTECT(newdims = allocVector(INTSXP, n));
171
	for(i = 0 ; i < LENGTH(dims) ; i++)
201
	for (i = 0; i < ndims; i++)
172
	    if(INTEGER(dims)[i] != 1)
202
	    if (INTEGER(dims)[i] != 1)
173
		INTEGER(newdims)[n++] = INTEGER(dims)[i];
203
		INTEGER(newdims)[n++] = INTEGER(dims)[i];
174
	if(dimnames != R_NilValue) {
204
	if (TYPEOF(dimnames) == VECSXP) {
175
#ifdef NEWLIST
-
 
176
	    int j = 0;
-
 
177
	    PROTECT(newdimnames = allocVector(VECSXP, n));
205
	    PROTECT(newdimnames = allocVector(VECSXP, n));
178
	    for(i = 0 ; i < LENGTH(dims) ; i++) {
206
	    for (i = 0; i < ndims; i++) {
179
		if(INTEGER(dims)[i] != 1) {
207
		if (INTEGER(dims)[i] != 1)
180
		    VECTOR(newdimnames)[j++] = VECTOR(dimnames)[i];
208
		    VECTOR(newdimnames)[j++] = VECTOR(dimnames)[i];
181
		}
-
 
182
	    }
209
	    }
183
#else
210
	}
-
 
211
	else if (TYPEOF(dimnames) == LISTSXP) {
184
	    PROTECT(newdimnames = allocList(n));
212
	    PROTECT(newdimnames = allocVector(VECSXP, n));
185
	    q = dimnames;
213
	    q = dimnames;	    
186
	    r = newdimnames;
-
 
187
	    for(i=0 ; i<LENGTH(dims) ; i++) {
214
	    for (i = 0; i < ndims; i++) {
188
		if(INTEGER(dims)[i] != 1) {
215
		if (INTEGER(dims)[i] != 1)
189
		    CAR(r) = CAR(q);
216
		    VECTOR(newdimnames)[j++] = CAR(q);
190
		    r = CDR(r);
-
 
191
		}
-
 
192
		q = CDR(q);
217
		q = CDR(q);
193
	    }
218
	    }
194
#endif
-
 
195
	}
219
	}
-
 
220
	else
-
 
221
	    newdimnames = R_NilValue;
196
	setAttrib(x, R_DimNamesSymbol, R_NilValue);
222
	setAttrib(x, R_DimNamesSymbol, R_NilValue);
197
	setAttrib(x, R_DimSymbol, newdims);
223
	setAttrib(x, R_DimSymbol, newdims);
198
	if (dimnames != R_NilValue) {
224
	if (dimnames != R_NilValue) {
199
	    setAttrib(x, R_DimNamesSymbol, newdimnames);
225
	    setAttrib(x, R_DimNamesSymbol, newdimnames);
200
	    UNPROTECT(1);
226
	    UNPROTECT(1);
Line 210... Line 236...
210
    SEXP x, xdims;
236
    SEXP x, xdims;
211
    int i, n, shorten;
237
    int i, n, shorten;
212
 
238
 
213
    checkArity(op, args);
239
    checkArity(op, args);
214
    x = CAR(args);
240
    x = CAR(args);
215
    if((xdims = getAttrib(x, R_DimSymbol)) != R_NilValue) {
241
    if ((xdims = getAttrib(x, R_DimSymbol)) != R_NilValue) {
216
	n = LENGTH(xdims);
242
	n = LENGTH(xdims);
217
	shorten = 0;
243
	shorten = 0;
218
	for(i=0 ; i<n ; i++)
244
	for (i = 0; i < n; i++)
219
	    if(INTEGER(xdims)[i] == 1) shorten = 1;
245
	    if (INTEGER(xdims)[i] == 1) shorten = 1;
220
	if(shorten) {
246
	if (shorten) {
221
	    if(NAMED(x)) x = duplicate(x);
247
	    if (NAMED(x)) x = duplicate(x);
222
	    x = DropDims(x);
248
	    x = DropDims(x);
223
	}
249
	}
224
    }
250
    }
225
    return x;
251
    return x;
226
}
252
}
Line 302... Line 328...
302
		complex *y, int nry, int ncy, complex *z)
328
		complex *y, int nry, int ncy, complex *z)
303
{
329
{
304
    int i, j, k;
330
    int i, j, k;
305
    double xij_r, xij_i, yjk_r, yjk_i, sum_i, sum_r;
331
    double xij_r, xij_i, yjk_r, yjk_i, sum_i, sum_r;
306
 
332
 
307
    for (i=0; i<nrx; i++)
333
    for (i = 0; i < nrx; i++)
308
	for (k=0; k<ncy; k++) {
334
	for (k = 0; k < ncy; k++) {
309
	    z[i+k*nrx].r = NA_REAL;
335
	    z[i + k * nrx].r = NA_REAL;
310
	    z[i+k*nrx].i = NA_REAL;
336
	    z[i + k * nrx].i = NA_REAL;
311
	    sum_r = 0.0;
337
	    sum_r = 0.0;
312
	    sum_i = 0.0;
338
	    sum_i = 0.0;
313
	    for (j=0; j<ncx; j++) {
339
	    for (j = 0; j < ncx; j++) {
314
		xij_r = x[i+j*nrx].r;
340
		xij_r = x[i + j * nrx].r;
315
		xij_i = x[i+j*nrx].i;
341
		xij_i = x[i + j * nrx].i;
316
		yjk_r = y[j+k*nry].r;
342
		yjk_r = y[j + k * nry].r;
317
		yjk_i = y[j+k*nry].i;
343
		yjk_i = y[j + k * nry].i;
318
#ifndef IEEE_754
344
#ifndef IEEE_754
319
		if (ISNAN(xij_r) || ISNAN(xij_i)
345
		if (ISNAN(xij_r) || ISNAN(xij_i)
320
		    || ISNAN(yjk_r) || ISNAN(yjk_i))
346
		    || ISNAN(yjk_r) || ISNAN(yjk_i))
321
		    goto next_ik;
347
		    goto next_ik;
322
#endif
348
#endif
323
		sum_r += (xij_r * yjk_r - xij_i * yjk_i);
349
		sum_r += (xij_r * yjk_r - xij_i * yjk_i);
324
		sum_i += (xij_r * yjk_i + xij_i * yjk_r);
350
		sum_i += (xij_r * yjk_i + xij_i * yjk_r);
325
	    }
351
	    }
326
	    z[i+k*nrx].r = sum_r;
352
	    z[i + k * nrx].r = sum_r;
327
	    z[i+k*nrx].i = sum_i;
353
	    z[i + k * nrx].i = sum_i;
328
#ifndef IEEE_754
354
#ifndef IEEE_754
329
	next_ik:
355
	next_ik:
330
	    ;
356
	    ;
331
#endif
357
#endif
332
	}
358
	}
Line 408... Line 434...
408
    ydims = getAttrib(y, R_DimSymbol);
434
    ydims = getAttrib(y, R_DimSymbol);
409
    ldx = length(xdims);
435
    ldx = length(xdims);
410
    ldy = length(ydims);
436
    ldy = length(ydims);
411
 
437
 
412
    if (ldx != 2 && ldy != 2) {
438
    if (ldx != 2 && ldy != 2) {
413
	if(PRIMVAL(op) == 0) {
439
	if (PRIMVAL(op) == 0) {
414
	    nrx = 1;
440
	    nrx = 1;
415
	    ncx = LENGTH(x);
441
	    ncx = LENGTH(x);
416
	}
442
	}
417
	else {
443
	else {
418
	    nrx = LENGTH(x);
444
	    nrx = LENGTH(x);
Line 424... Line 450...
424
    else if (ldx != 2) {
450
    else if (ldx != 2) {
425
	nry = INTEGER(ydims)[0];
451
	nry = INTEGER(ydims)[0];
426
	ncy = INTEGER(ydims)[1];
452
	ncy = INTEGER(ydims)[1];
427
	nrx = 0;
453
	nrx = 0;
428
	ncx = 0;
454
	ncx = 0;
429
	if(PRIMVAL(op) == 0) {
455
	if (PRIMVAL(op) == 0) {
430
	    if(LENGTH(x) == nry) {
456
	    if (LENGTH(x) == nry) {
431
		nrx = 1;
457
		nrx = 1;
432
		ncx = LENGTH(x);
458
		ncx = LENGTH(x);
433
	    } 
459
	    } 
434
	    else if (LENGTH(x) == ncy) {
460
	    else if (LENGTH(x) == ncy) {
435
		ncx=1;
461
		ncx=1;
436
		nrx=LENGTH(x);
462
		nrx=LENGTH(x);
437
	    }
463
	    }
438
	    if( nry*ncy == 1 ) {
464
	    if (nry * ncy == 1) {
439
		nrx = LENGTH(x);
465
		nrx = LENGTH(x);
440
		ncx = 1;
466
		ncx = 1;
441
	    }
467
	    }
442
	}
468
	}
443
	else {
469
	else {
444
	    if(LENGTH(x) == nry) {
470
	    if (LENGTH(x) == nry) {
445
		nrx = LENGTH(x);
471
		nrx = LENGTH(x);
446
		ncx = 1;
472
		ncx = 1;
447
	    }
473
	    }
448
	}
474
	}
449
    }
475
    }
450
    else if (ldy != 2) {
476
    else if (ldy != 2) {
451
	nrx = INTEGER(xdims)[0];
477
	nrx = INTEGER(xdims)[0];
452
	ncx = INTEGER(xdims)[1];
478
	ncx = INTEGER(xdims)[1];
453
	nry = 0;
479
	nry = 0;
454
	ncy = 0;
480
	ncy = 0;
455
	if(PRIMVAL(op) == 0) {
481
	if (PRIMVAL(op) == 0) {
456
	    if (LENGTH(y) == ncx) {
482
	    if (LENGTH(y) == ncx) {
457
		nry = LENGTH(y);
483
		nry = LENGTH(y);
458
		ncy = 1;
484
		ncy = 1;
459
	    }
485
	    }
460
	    else if (LENGTH(y) == nrx){
486
	    else if (LENGTH(y) == nrx){
461
		ncy = LENGTH(y);
487
		ncy = LENGTH(y);
462
		nry = 1;
488
		nry = 1;
463
	    }
489
	    }
464
	    if ( nrx*ncx == 1 ) {
490
	    if (nrx * ncx == 1) {
465
		ncy = LENGTH(y);
491
		ncy = LENGTH(y);
466
		nry = 1;
492
		nry = 1;
467
	    }
493
	    }
468
	}
494
	}
469
	else {
495
	else {
Line 478... Line 504...
478
	ncx = INTEGER(xdims)[1];
504
	ncx = INTEGER(xdims)[1];
479
	nry = INTEGER(ydims)[0];
505
	nry = INTEGER(ydims)[0];
480
	ncy = INTEGER(ydims)[1];
506
	ncy = INTEGER(ydims)[1];
481
    }
507
    }
482
 
508
 
483
    if(PRIMVAL(op) == 0) {
509
    if (PRIMVAL(op) == 0) {
484
	if(ncx != nry)
510
	if (ncx != nry)
485
	    errorcall(call, "non-conformable arguments\n");
511
	    errorcall(call, "non-conformable arguments\n");
486
    }
512
    }
487
    else {
513
    else {
488
	if(nrx != nry)
514
	if (nrx != nry)
489
	    errorcall(call, "non-conformable arguments\n");
515
	    errorcall(call, "non-conformable arguments\n");
490
    }
516
    }
491
 
517
 
492
    if(isComplex(CAR(args)) || isComplex(CADR(args)))
518
    if (isComplex(CAR(args)) || isComplex(CADR(args)))
493
	mode = CPLXSXP;
519
	mode = CPLXSXP;
494
    else
520
    else
495
	mode = REALSXP;
521
	mode = REALSXP;
496
    CAR(args) = coerceVector(CAR(args), mode);
522
    CAR(args) = coerceVector(CAR(args), mode);
497
    CADR(args) = coerceVector(CADR(args), mode);
523
    CADR(args) = coerceVector(CADR(args), mode);
498
 
524
 
499
    if(PRIMVAL(op) == 0) {
525
    if (PRIMVAL(op) == 0) {
500
	PROTECT(ans = allocMatrix(mode, nrx, ncy));
526
	PROTECT(ans = allocMatrix(mode, nrx, ncy));
501
	if(mode == CPLXSXP)
527
	if (mode == CPLXSXP)
502
	    cmatprod(COMPLEX(CAR(args)), nrx, ncx,
528
	    cmatprod(COMPLEX(CAR(args)), nrx, ncx,
503
		     COMPLEX(CADR(args)), nry, ncy, COMPLEX(ans));
529
		     COMPLEX(CADR(args)), nry, ncy, COMPLEX(ans));
504
	else
530
	else
505
	    matprod(REAL(CAR(args)), nrx, ncx,
531
	    matprod(REAL(CAR(args)), nrx, ncx,
506
		    REAL(CADR(args)), nry, ncy, REAL(ans));
532
		    REAL(CADR(args)), nry, ncy, REAL(ans));
Line 510... Line 536...
510
	    setAttrib(ans, R_DimNamesSymbol, list2(CAR(xdims), CADR(ydims)));
536
	    setAttrib(ans, R_DimNamesSymbol, list2(CAR(xdims), CADR(ydims)));
511
	}
537
	}
512
    }
538
    }
513
    else {
539
    else {
514
	PROTECT(ans = allocMatrix(mode, ncx, ncy));
540
	PROTECT(ans = allocMatrix(mode, ncx, ncy));
515
	if(mode == CPLXSXP)
541
	if (mode == CPLXSXP)
516
	    ccrossprod(COMPLEX(CAR(args)), nrx, ncx,
542
	    ccrossprod(COMPLEX(CAR(args)), nrx, ncx,
517
		       COMPLEX(CADR(args)), nry, ncy, COMPLEX(ans));
543
		       COMPLEX(CADR(args)), nry, ncy, COMPLEX(ans));
518
	else
544
	else
519
	    crossprod(REAL(CAR(args)), nrx, ncx,
545
	    crossprod(REAL(CAR(args)), nrx, ncx,
520
		      REAL(CADR(args)), nry, ncy, REAL(ans));
546
		      REAL(CADR(args)), nry, ncy, REAL(ans));
Line 529... Line 555...
529
}
555
}
530
 
556
 
531
SEXP do_transpose(SEXP call, SEXP op, SEXP args, SEXP rho)
557
SEXP do_transpose(SEXP call, SEXP op, SEXP args, SEXP rho)
532
{
558
{
533
    SEXP a, r, dims, dn;
559
    SEXP a, r, dims, dn;
534
    int i, len=0, ncol=0, nrow=0;
560
    int i, len = 0, ncol=0, nrow=0;
535
 
561
 
536
    checkArity(op, args);
562
    checkArity(op, args);
537
    a = CAR(args);
563
    a = CAR(args);
538
 
564
 
539
    if(isVector(a)) {
565
    if (isVector(a)) {
540
	dims = getAttrib(a, R_DimSymbol);
566
	dims = getAttrib(a, R_DimSymbol);
541
	switch(length(dims)) {
567
	switch(length(dims)) {
542
	case 0:
568
	case 0:
543
	case 1:
569
	case 1:
544
	    nrow = len = length(a);
570
	    nrow = len = length(a);
Line 551... Line 577...
551
	    break;
577
	    break;
552
	default:
578
	default:
553
	    goto not_matrix;
579
	    goto not_matrix;
554
	}
580
	}
555
    }
581
    }
556
    else if(isList(a)) {
582
    else if (isList(a)) {
557
	dims = getAttrib(a, R_DimSymbol);
583
	dims = getAttrib(a, R_DimSymbol);
558
	if(length(dims) == 2) {
584
	if (length(dims) == 2) {
559
	    errorcall(call, "can't transpose list matrices (yet)\n");
585
	    errorcall(call, "can't transpose list matrices (yet)\n");
560
	}
586
	}
561
	else goto not_matrix;
587
	else goto not_matrix;
562
    }
588
    }
563
    else goto not_matrix;
589
    else goto not_matrix;
Line 586... Line 612...
586
    dims = allocVector(INTSXP, 2);
612
    dims = allocVector(INTSXP, 2);
587
    INTEGER(dims)[0] = ncol;
613
    INTEGER(dims)[0] = ncol;
588
    INTEGER(dims)[1] = nrow;
614
    INTEGER(dims)[1] = nrow;
589
    setAttrib(r, R_DimSymbol, dims);
615
    setAttrib(r, R_DimSymbol, dims);
590
 
616
 
591
    if(!isNull(dn = getAttrib(a, R_DimNamesSymbol))) {
617
    if (!isNull(dn = getAttrib(a, R_DimNamesSymbol))) {
592
	PROTECT(dn = duplicate(dn));
618
	PROTECT(dn = duplicate(dn));
593
	switch(length(dn)) {
619
	switch(length(dn)) {
594
	case 1:
620
	case 1:
595
	    PROTECT(dims = allocList(2));
621
	    PROTECT(dims = allocList(2));
596
	    CADR(dims) = CAR(dn);
622
	    CADR(dims) = CAR(dn);
Line 695... Line 721...
695
	for (i = 0; i < len; i++) {
721
	for (i = 0; i < len; i++) {
696
	    j = swap(i, dimsa, dimsr, perm, ind1, ind2);
722
	    j = swap(i, dimsa, dimsr, perm, ind1, ind2);
697
	    STRING(r)[j] = STRING(a)[i];
723
	    STRING(r)[j] = STRING(a)[i];
698
	}
724
	}
699
	break;
725
	break;
-
 
726
    case VECSXP:
-
 
727
	for (i = 0; i < len; i++) {
-
 
728
	    j = swap(i, dimsa, dimsr, perm, ind1, ind2);
-
 
729
	    VECTOR(r)[j] = VECTOR(a)[i];
-
 
730
	}
700
    default:
731
    default:
701
	errorcall(call, "invalid argument\n");
732
	errorcall(call, "invalid argument\n");
702
    }
733
    }
703
 
734
 
704
    if (INTEGER(CAR(CDDR(args)))[0])
735
    if (INTEGER(CAR(CDDR(args)))[0])