void
NumericalTestTrialIntegrator<BasisFunctionType, ResultType, GeometryFactory>::
integrate(const std::vector<int> &elementIndices,
          const Shapeset<BasisFunctionType> &testShapeset,
          const Shapeset<BasisFunctionType> &trialShapeset,
          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 testDofCount = testShapeset.size();
    const int trialDofCount = trialShapeset.size();

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

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

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

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

    CollectionOf3dArrays<BasisFunctionType> testValues, trialValues;

    result.set_size(testDofCount, trialDofCount, elementCount);

    testShapeset.evaluate(testBasisDeps, m_localQuadPoints, ALL_DOFS,
                          testBasisData);
    trialShapeset.evaluate(trialBasisDeps, m_localQuadPoints, ALL_DOFS,
                           trialBasisData);

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

        m_integral.evaluate(geomData, testValues, trialValues, m_quadWeights,
                            result.slice(e));
    }
}
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;
            }
    }
}