outer function

Matrix outer(
  1. Matrix first,
  2. Matrix second
)

Implementation

Matrix outer(Matrix first, Matrix second) {
  List<Vector> output = [];
  bool firstIsVector = first.rowsNum == 1 || first.columnsNum == 1;
  bool secondIsVector = second.rowsNum == 1 || second.columnsNum == 1;

  if (!firstIsVector && !secondIsVector) {
    List<double> n = [];
    List<double> m = [];

    for (var row in first) {
      for (var element in row) {
        n.add(element);
      }
    }

    for (var row in second) {
      for (var element in row) {
        m.add(element);
      }
    }

    first = Matrix.fromList([n]);
    second = Matrix.fromList([m]);
  }

  for (var v in transposeToRow(first)[0]) {
    List<double> newVector = [];

    for (var u in transposeToRow(second)[0]) {
      newVector.add(u * v);
    }

    output.add(Vector.fromList(newVector));
  }

  return Matrix.fromRows(output);
}