The R Project SVN R-packages

Rev

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

Rev 1572 Rev 1577
Line 314... Line 314...
314
    Memcpy(Ai, ii, nnz);
314
    Memcpy(Ai, ii, nnz);
315
    for (j = 0; j < n; j++) R_isort(Ai + Ap[j], Ap[j+1] - Ap[j]);
315
    for (j = 0; j < n; j++) R_isort(Ai + Ap[j], Ap[j+1] - Ap[j]);
316
    Free(Aj); Free(ord); Free(ii);
316
    Free(Aj); Free(ord); Free(ii);
317
}
317
}
318
 
318
 
319
 
-
 
320
/**
-
 
321
 * Symmetrize a matrix by copying the strict upper triangle into the
-
 
322
 * lower triangle.
-
 
323
 *
-
 
324
 * @param a pointer to a matrix in Fortran storage mode
-
 
325
 * @param nc number of columns (and rows and leading dimension) in the matrix
-
 
326
 *
-
 
327
 * @return a, symmetrized
-
 
328
 */
-
 
329
double *
-
 
330
nlme_symmetrize(double *a, const int nc)
-
 
331
{
-
 
332
    int i, j;
-
 
333
 
-
 
334
    for (i = 1; i < nc; i++)
-
 
335
	for (j = 0; j < i; j++)
-
 
336
	    a[i + j*nc] = a[j + i*nc];
-
 
337
    return a;
-
 
338
}
-
 
339
 
-
 
340
/**
-
 
341
 * Check the error code returned by an Lapack routine and create an
-
 
342
 * appropriate error message.
-
 
343
 *
-
 
344
 * @param info Error code as returned from the Lapack routine
-
 
345
 * @param laName Character string containing the name of the Lapack routine
-
 
346
 */
-
 
347
void
-
 
348
nlme_check_Lapack_error(int info, const char *laName)
-
 
349
{
-
 
350
    if (info != 0) {
-
 
351
        if (info > 0)
-
 
352
            error(_("error code %d from Lapack routine %s"), info, laName);
-
 
353
        error(_("argument no. %d to Lapack routine %s is illegal"),
-
 
354
              -info, laName);
-
 
355
    }
-
 
356
}
-
 
357
 
-
 
358
void make_array_triangular(double *to, SEXP from)
319
void make_array_triangular(double *to, SEXP from)
359
{
320
{
360
    int i, j, *dims = INTEGER(GET_SLOT(from, Matrix_DimSym));
321
    int i, j, *dims = INTEGER(GET_SLOT(from, Matrix_DimSym));
361
    int n = dims[0], m = dims[1];
322
    int n = dims[0], m = dims[1];
362
 
323
 
Line 380... Line 341...
380
	}
341
	}
381
    }
342
    }
382
}
343
}
383
 
344
 
384
/**
345
/**
385
 * Replace the value of a slot or subslot of an object in place.  This
-
 
386
 * routine purposely does not copy the value of obj.  Use with caution.
-
 
387
 *
-
 
388
 * @param obj object with slot to be replaced
-
 
389
 * @param names vector of names.  The last element is the name of the slot to replace.  The leading elements are the names of slots and subslots of obj.
-
 
390
 * @param value the replacement value for the slot
-
 
391
 *
-
 
392
 * @return obj, with the named slot modified in place.
-
 
393
 */
-
 
394
SEXP
-
 
395
nlme_replaceSlot(SEXP obj, SEXP names, SEXP value)
-
 
396
{
-
 
397
    int lnm1 = length(names) - 1;
-
 
398
 
-
 
399
    if (lnm1 >= 0) {
-
 
400
	SEXP comp = obj;
-
 
401
	int i;
-
 
402
 
-
 
403
	for (i = 0; i < lnm1; i++) {
-
 
404
	    comp = GET_SLOT(comp, install(CHAR(STRING_ELT(names, i))));
-
 
405
	}
-
 
406
	SET_SLOT(comp, install(CHAR(STRING_ELT(names, lnm1))), value);
-
 
407
    }
-
 
408
    return obj;
-
 
409
}
-
 
410
 
-
 
411
/**
-
 
412
 * Produce a weighted copy of the matrices in MLin in the storage
-
 
413
 * allocated to MLout
-
 
414
 *
-
 
415
 * @param MLin input matrix list
-
 
416
 * @param wts real vector of weights
-
 
417
 * @param adjst adjusted response
-
 
418
 * @param MLout On input a list of matrices of the same dimensions as MLin.
-
 
419
 *
-
 
420
 * @return MLout with its contents overwritten by a weighted copy of
-
 
421
 * MLin according to wts with adjst overwriting the response.
-
 
422
 */
-
 
423
SEXP nlme_weight_matrix_list(SEXP MLin, SEXP wts, SEXP adjst, SEXP MLout)
-
 
424
{
-
 
425
    int i, j, n, nf;
-
 
426
    SEXP lastM;
-
 
427
 
-
 
428
    if (!(isNewList(MLin) && isReal(wts) && isReal(adjst) && isNewList(MLout)))
-
 
429
	error(_("Incorrect argument type"));
-
 
430
    nf = length(MLin);
-
 
431
    if (length(MLout) != nf)
-
 
432
	error(_("Lengths of MLin (%d) and MLout (%d) must match"), nf,
-
 
433
	      length(MLout));
-
 
434
    n = length(wts);
-
 
435
    if (length(adjst) != n)
-
 
436
	error(_("Expected adjst to have length %d, got %d"), n, length(adjst));
-
 
437
    for (i = 0; i < nf; i++) {
-
 
438
	SEXP Min = VECTOR_ELT(MLin, i),
-
 
439
	    Mout = VECTOR_ELT(MLout, i);
-
 
440
	int *din, *dout, k, nc;
-
 
441
 
-
 
442
	if (!(isMatrix(Min) && isReal(Min)))
-
 
443
	    error(_("component %d of MLin is not a numeric matrix"), i + 1);
-
 
444
	din = INTEGER(getAttrib(Min, R_DimSymbol));
-
 
445
	nc = din[1];
-
 
446
	if (din[0] != n)
-
 
447
	    error(_("component %d of MLin has %d rows, expected %d"), i + 1,
-
 
448
		  din[0], n);
-
 
449
	if (!(isMatrix(Mout) && isReal(Mout)))
-
 
450
	    error(_("component %d of MLout is not a numeric matrix"), i + 1);
-
 
451
	dout = INTEGER(getAttrib(Mout, R_DimSymbol));
-
 
452
	if (dout[0] != n)
-
 
453
	    error(_("component %d of MLout has %d rows, expected %d"), i + 1,
-
 
454
		  dout[0], n);
-
 
455
	if (dout[1] != nc)
-
 
456
	    error(_("component %d of MLout has %d columns, expected %d"), i + 1,
-
 
457
		  dout[1], nc);
-
 
458
	for (k = 0; k < nc; k++) {
-
 
459
	    for (j = 0; j < n; j++) {
-
 
460
		REAL(Mout)[j + k * n] = REAL(Min)[j + k * n] * REAL(wts)[j];
-
 
461
	    }
-
 
462
	}
-
 
463
    }
-
 
464
    lastM = VECTOR_ELT(MLout, nf - 1);
-
 
465
    j = INTEGER(getAttrib(lastM, R_DimSymbol))[1] - 1;
-
 
466
    for (i = 0; i < n; i++)
-
 
467
	REAL(lastM)[j*n + i] = REAL(adjst)[i] * REAL(wts)[i];
-
 
468
    return MLout;
-
 
469
}
-
 
470
 
-
 
471
/**
-
 
472
 * Create a named vector of type TYP
346
 * Create a named vector of type TYP
473
 *
347
 *
474
 * @param TYP a vector SEXP type (e.g. REALSXP)
348
 * @param TYP a vector SEXP type (e.g. REALSXP)
475
 * @param names names of list elements with null string appended
349
 * @param names names of list elements with null string appended
476
 *
350
 *