operator + method

Varset operator +(
  1. Varset other
)

The blend operator.

Blending assigns varsets to a same dimension in order. The meaning of the variables respectively in that dimension is determined by geometry type.

Implementation

Varset operator +(Varset other) {
  // It creates a polynomial:
  //
  // - [form], append all right form terms to the left ones, normalizes the form,
  // and deduplicates.
  // - [nested], only for distributivity, if nesteds are same, uses that nested,
  // makes sure two nesters are single, appends the two single nesters, normalizes,
  // and deduplicates; if nested are different, makes sure two nesters are same
  // and uses that nesters, appends two nesteds, nomalizes and deduplicaates.
  // - [nesters], see in [nested].

  final AlgForm formRst = (<AlgTerm>[...form, ...other.form].._normalize())
      .collectionItemDeduplicate();

  AlgForm? nestedRst;
  List<AlgForm> nestersRst = [];
  if (nested == null && other.nested == null) {
    // Does nothing.
  } else if (deepCollectionEquals(nested, other.nested)) {
    // Right distributivity: x / y + x / z = x / (y + z).

    nestedRst = nested;
    final leftNester = nesters.single;
    final rightNester = other.nesters.single;
    nestersRst = [
      ([...leftNester, ...rightNester].._normalize())
          .collectionItemDeduplicate()
    ];
  } else {
    // nested != other.nested

    final leftNester = nesters.single;
    final rightNester = other.nesters.single;
    if (deepCollectionEquals(leftNester, rightNester)) {
      // Left distributivity: x / z + y / z = (x + y) / z.

      nestedRst = ([...nested!, ...other.nested!].._normalize())
          .collectionItemDeduplicate();
      nestersRst = nesters;
    } else {
      throw ArgumentError(
          'Two nested operands without distributivity can not blend');
    }
  }

  return Varset._create(
    formRst,
    nestedRst,
    nestersRst,
  );
}