void DefaultTestKernelTrialIntegral<IntegrandFunctor>::
    evaluateWithTensorQuadratureRule(
        const GeometricalData<CoordinateType> &testGeomData,
        const GeometricalData<CoordinateType> &trialGeomData,
        const CollectionOf3dArrays<BasisFunctionType> &testValues,
        const CollectionOf3dArrays<BasisFunctionType> &trialValues,
        const CollectionOf4dArrays<KernelType> &kernelValues,
        const std::vector<CoordinateType> &testQuadWeights,
        const std::vector<CoordinateType> &trialQuadWeights,
        Matrix<ResultType> &result) const {
  // Evaluate constants

  const size_t testDofCount = testValues[0].extent(1);
  const size_t trialDofCount = trialValues[0].extent(1);

  const size_t testPointCount = testQuadWeights.size();
  const size_t trialPointCount = trialQuadWeights.size();

  // Assert that array dimensions are correct

  for (size_t i = 0; i < kernelValues.size(); ++i) {
    assert(kernelValues[i].extent(2) == testPointCount);
    assert(kernelValues[i].extent(3) == trialPointCount);
  }
  for (size_t i = 0; i < testValues.size(); ++i)
    assert(testValues[i].extent(2) == testPointCount);
  for (size_t i = 0; i < trialValues.size(); ++i)
    assert(trialValues[i].extent(2) == trialPointCount);

  assert(result.rows() == testDofCount);
  assert(result.cols() == trialDofCount);

  // Integrate

  for (size_t trialDof = 0; trialDof < trialDofCount; ++trialDof)
    for (size_t testDof = 0; testDof < testDofCount; ++testDof) {
      ResultType sum = 0.;
      for (size_t trialPoint = 0; trialPoint < trialPointCount; ++trialPoint) {
        const CoordinateType trialWeight =
            trialGeomData.integrationElements(trialPoint) *
            trialQuadWeights[trialPoint];
        ResultType partialSum = 0.;
        for (size_t testPoint = 0; testPoint < testPointCount; ++testPoint) {
          const CoordinateType testWeight =
              testGeomData.integrationElements(testPoint) *
              testQuadWeights[testPoint];
          partialSum += m_functor.evaluate(
                            testGeomData.const_slice(testPoint),
                            trialGeomData.const_slice(trialPoint),
                            testValues.const_slice(testDof, testPoint),
                            trialValues.const_slice(trialDof, trialPoint),
                            kernelValues.const_slice(testPoint, trialPoint)) *
                        testWeight;
        }
        sum += partialSum * trialWeight;
      }
      result(testDof, trialDof) = sum;
    }
}
void DefaultTestTrialIntegral<IntegrandFunctor>::evaluate(
        const GeometricalData<CoordinateType>& geomData,
        const CollectionOf3dArrays<BasisFunctionType>& testValues,
        const CollectionOf3dArrays<BasisFunctionType>& trialValues,
        const std::vector<CoordinateType>& weights,
        arma::Mat<ResultType>& result) const
{
    const size_t pointCount = weights.size();
    assert(testValues.size() == 1);
    assert(trialValues.size() == 1);

    // We don't care about the number of rows of testValues[0] and trialValues[0]
    // -- it's up to the integrand functor
    const size_t testDofCount = testValues[0].extent(1);
    const size_t trialDofCount = trialValues[0].extent(1);
    assert(testValues[0].extent(2) == pointCount);
    assert(trialValues[0].extent(2) == pointCount);
    for (size_t trialDof = 0; trialDof < trialDofCount; ++trialDof)
        for (size_t testDof = 0; testDof < testDofCount; ++testDof) {
            ResultType sum = 0.;
            for (size_t point = 0; point < pointCount; ++point)
                sum += m_functor.evaluate(
                            geomData.const_slice(point),
                            testValues.const_slice(testDof, point),
                            trialValues.const_slice(trialDof, point)) *
                        geomData.integrationElements(point) *
                        weights[point];
            result(testDof, trialDof) = sum;
        }
}
void DefaultTestKernelTrialIntegral<IntegrandFunctor>::
    evaluateWithNontensorQuadratureRule(
        const GeometricalData<CoordinateType> &testGeomData,
        const GeometricalData<CoordinateType> &trialGeomData,
        const CollectionOf3dArrays<BasisFunctionType> &testValues,
        const CollectionOf3dArrays<BasisFunctionType> &trialValues,
        const CollectionOf3dArrays<KernelType> &kernelValues,
        const std::vector<CoordinateType> &quadWeights,
        Matrix<ResultType> &result) const {
  // Evaluate constants

  const size_t testDofCount = testValues[0].extent(1);
  const size_t trialDofCount = trialValues[0].extent(1);

  const size_t pointCount = quadWeights.size();

  // Assert that array dimensions are correct

  for (size_t i = 0; i < kernelValues.size(); ++i)
    assert(kernelValues[i].extent(2) == pointCount);
  for (size_t i = 0; i < testValues.size(); ++i)
    assert(testValues[i].extent(2) == pointCount);
  for (size_t i = 0; i < trialValues.size(); ++i)
    assert(trialValues[i].extent(2) == pointCount);

  // Integrate

  for (size_t trialDof = 0; trialDof < trialDofCount; ++trialDof)
    for (size_t testDof = 0; testDof < testDofCount; ++testDof) {
      ResultType sum = 0.;
      for (size_t point = 0; point < pointCount; ++point)
        sum += m_functor.evaluate(testGeomData.const_slice(point),
                                  trialGeomData.const_slice(point),
                                  testValues.const_slice(testDof, point),
                                  trialValues.const_slice(trialDof, point),
                                  kernelValues.const_slice(point)) *
               (testGeomData.integrationElements(point) *
                trialGeomData.integrationElements(point) * quadWeights[point]);
      result(testDof, trialDof) = sum;
    }
}
void NumericalTestTrialIntegrator<BasisFunctionType, ResultType, GeometryFactory>::integrate(
        const std::vector<int>& elementIndices,
        const Basis<BasisFunctionType>& testBasis,
        const Basis<BasisFunctionType>& trialBasis,
        arma::Cube<ResultType>& result) const
{
    const size_t pointCount = m_localQuadPoints.n_cols;
    const size_t elementCount = elementIndices.size();

    if (pointCount == 0 || elementCount == 0)
        return;
    // TODO: in the (pathological) case that pointCount == 0 but
    // elementCount != 0, set elements of result to 0.

    // Evaluate constants
    const int componentCount = m_testTransformations.resultDimension(0);
    const int testDofCount = testBasis.size();
    const int trialDofCount = trialBasis.size();

//    if (m_trialTransformations.codomainDimension() != componentCount)
//        throw std::runtime_error("NumericalTestTrialIntegrator::integrate(): "
//                                 "test and trial functions "
//                                 "must have the same number of components");

    BasisData<BasisFunctionType> testBasisData, trialBasisData;
    GeometricalData<CoordinateType> geomData;

    size_t testBasisDeps = 0, trialBasisDeps = 0;
    size_t geomDeps = INTEGRATION_ELEMENTS;

    m_testTransformations.addDependencies(testBasisDeps, geomDeps);
    m_trialTransformations.addDependencies(trialBasisDeps, geomDeps);

    typedef typename GeometryFactory::Geometry Geometry;
    std::auto_ptr<Geometry> geometry(m_geometryFactory.make());

    CollectionOf3dArrays<BasisFunctionType> testValues, trialValues;

    result.set_size(testDofCount, trialDofCount, elementCount);

    testBasis.evaluate(testBasisDeps, m_localQuadPoints, ALL_DOFS, testBasisData);
    trialBasis.evaluate(trialBasisDeps, m_localQuadPoints, ALL_DOFS, trialBasisData);

    // Iterate over the elements
    for (size_t e = 0; e < elementCount; ++e)
    {
        m_rawGeometry.setupGeometry(elementIndices[e], *geometry);
        geometry->getData(geomDeps, m_localQuadPoints, geomData);
        m_testTransformations.evaluate(testBasisData, geomData, testValues);
        m_trialTransformations.evaluate(trialBasisData, geomData, trialValues);

        for (int trialDof = 0; trialDof < trialDofCount; ++trialDof)
            for (int testDof = 0; testDof < testDofCount; ++testDof)
            {
                ResultType sum = 0.;
                for (size_t point = 0; point < pointCount; ++point)
                    for (int dim = 0; dim < componentCount; ++dim)
                        sum +=  m_quadWeights[point] *
                                geomData.integrationElements(point) *
                                conjugate(testValues[0](dim, testDof, point)) *
                                trialValues[0](dim, trialDof, point);
                result(testDof, trialDof, e) = sum;
            }
    }
}