
/*
 * Computer Algebra Kit (c) 1998 by David Stes.  All Rights Reserved.
 * $Id: monomc.m,v 1.7 1998/11/13 09:45:37 stes Exp $
 */

#include "cakit.h"

@implementation monomialc
- empty  {
  [self subclassResponsibility:_cmd];
  return nil;
}
- (BOOL)isEmpty  {
  [self subclassResponsibility:_cmd];
  return NO;
}
- (int)numTerms  {
  [self subclassResponsibility:_cmd];
  return 0;
}
- eachTerm  {
  [self subclassResponsibility:_cmd];
  return nil;
}
- insertTerm:aTerm  {
  [self subclassResponsibility:_cmd];
  return nil;
}

- square
{
  id seq, term, res;

  res = [self empty];
  seq = [self eachTerm];
  while (term = [seq next])
    [res insertTerm:[term square]];
  seq = [seq free];
  return res;
}

- multiply:b
{
  id res, seq, term;

  res = [self empty];

  seq = [self eachTerm];
  while (term = [seq next])
    [res insertTerm:[term cheapCopy]];
  seq = [seq free];

  seq = [b eachTerm];
  while (term = [seq next])
    [res insertTerm:[term cheapCopy]];
  seq = [seq free];

  return res;
}

- divide:b
{
  id res, seq, term;

  res = [self empty];

  seq = [self eachTerm];
  while (term = [seq next])
    [res insertTerm:[term cheapCopy]];
  seq = [seq free];

  seq = [b eachTerm];
  while (term = [seq next]) {
    term = [term inverse];
    assert([term check]);
    if ([res insertTerm:term]) {
      continue;
    } else {
      [term free];
      [res free];
      [seq free];
      return nil;
    }
  }
  seq = [seq free];

  return res;
}

@end

