partition.c: (Matching .h file)


/* partition.c Simple matrix partition manipulation routines.
 * © Copyright 1999 by John Halleck
 * All rights reserved.
 */

/* Initial Test Version
 * 0.02 - John Halleck, September 6th, 1999
 *   Added partition add and subtract.
 * 0.01 - John Halleck, July 26th, 1999
 *   Took routines from matrix package.
 */

#include "errors.h"
 /* Standard error codes. */

#include "mat.h"
 /* Matrix definitions. */

/* ========================= Set to a constant ============================== */

/* ---------- Embed one matrix in another ------------ */

error matembed (int rows, int cols, matrix result,
                int rowsfrom, int colsfrom, matrix from,
                int atrow, int atcol) {

  double *lfrom, *ltorow, *ltoelement;
  int index;

  if (!from || !result)         return ERRnil; /* Arrays must exist */
  if (from == result)           return ERRsame; /* and be distinct */
  if (rows<1 || cols<1)         return ERRsize; /* and have good sizes */
  if (rowsfrom<1 || colsfrom<1) return ERRsize;
  if (atrow<0 || atrow >= rows) return ERRrange; /* Place to embed must be */
  if (atcol<0 || atcol >= cols) return ERRrange; /* in the matrix. */
  if (atrow + rowsfrom > rows)  return ERRrange; /* with enough room left */
  if (atcol + colsfrom  > cols) return ERRrange; 

  lfrom  = (double *) from; 
  ltorow = (double *) result   + cols*atrow + atcol;
  while (rowsfrom--) { 
     index = colsfrom; ltoelement = ltorow;
     while(index--) *ltoelement++ = *lfrom++;
     ltorow += cols;
  }

  return NoError;
}

/* ---------- Extract one matrix from another. ------- */
error matextract (int rows, int cols, matrix result,
                  int rowsfrom, int colsfrom, matrix from,
                  int offsetrow, int offsetcol) {

  double *lto, *loffsetrow, *lfromelement;
  int index;

  if (!from || !result)                   return ERRnil;
  if (from == result)                     return ERRsame;
  if (rows<1 || cols<1)                   return ERRsize;
  if (rowsfrom<1 || colsfrom<1)           return ERRsize;
  if (offsetrow<0 || offsetrow>=rowsfrom) return ERRrange;
  if (offsetcol<0 || offsetcol>=colsfrom) return ERRrange;
  if (offsetrow + rows > rowsfrom)        return ERRrange;
  if (offsetcol + cols > colsfrom)        return ERRrange;

  lto        = (double *) result;
  loffsetrow = (double *) from    + colsfrom*offsetrow + offsetcol;
  while (rows--) {
        index = cols; lfromelement = loffsetrow;
        while (index--) *lto++ = *lfromelement++;
        loffsetrow += colsfrom;
  }

  return NoError;
}

/* -----------  Add one matrix to a section of another ------------- */
error partadd  (int rows, int cols, matrix result,
                int rowsfrom, int colsfrom, matrix from,
                int atrow, int atcol) {

  double *lfrom, *ltorow, *ltoelement;
  int index;

  if (!from || !result)         return ERRnil; /* Arrays must exist */
  if (from == result)           return ERRsame; /* and be distinct */
  if (rows<1 || cols<1)         return ERRsize; /* and have good sizes */
  if (rowsfrom<1 || colsfrom<1) return ERRsize;
  if (atrow<0 || atrow >= rows) return ERRrange; /* Place to embed must be */
  if (atcol<0 || atcol >= cols) return ERRrange; /* in the matrix. */
  if (atrow + rowsfrom > rows)  return ERRrange; /* with enough room left */
  if (atcol + colsfrom  > cols) return ERRrange;

  lfrom  = (double *) from;
  ltorow = (double *) result   + cols*atrow + atcol;
  while (rowsfrom--) {
     index = colsfrom; ltoelement = ltorow;
     while(index--) *ltoelement++ += *lfrom++;
     ltorow += cols;
  }

  return NoError;
}

/* -----------  Subtract one matrix from another ---------------------- */
error partsub  (int rows, int cols, matrix result,
                int rowsfrom, int colsfrom, matrix from,
                int atrow, int atcol) {

  double *lfrom, *ltorow, *ltoelement;
  int index;

  if (!from || !result)         return ERRnil; /* Arrays must exist */
  if (from == result)           return ERRsame; /* and be distinct */
  if (rows<1 || cols<1)         return ERRsize; /* and have good sizes */
  if (rowsfrom<1 || colsfrom<1) return ERRsize;
  if (atrow<0 || atrow >= rows) return ERRrange; /* Place to embed must be */
  if (atcol<0 || atcol >= cols) return ERRrange; /* in the matrix. */
  if (atrow + rowsfrom > rows)  return ERRrange; /* with enough room left */
  if (atcol + colsfrom  > cols) return ERRrange;

  lfrom  = (double *) from;
  ltorow = (double *) result   + cols*atrow + atcol;
  while (rowsfrom--) {
     index = colsfrom; ltoelement = ltorow;
     while(index--) *ltoelement++ -= *lfrom++;
     ltorow += cols;
  }

  return NoError;
}


Go to ...


This page is http://www.cc.utah.edu/~nahaj/cave/survey/code/c/partition.c.html
© Copyright 2000 by John Halleck, All Rights Reserved.
This snapshot was last modified on August 23rd, 2000
And the underlying file was last modified on May 11th, 2000